blob: 68df1cb52e285fb21f5f9131ba70dd56a8ca0a9b [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000142 {VV_NAME("t_class", VAR_NUMBER), NULL, VV_RO},
143 {VV_NAME("t_object", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaard787e402021-12-24 21:36:12 +0000144 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
148 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
149 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
150 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
151 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
152 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
153 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
154 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
155 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
156 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
157 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
158 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000159 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200160};
161
162// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000163#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vv_nr vv_di.di_tv.vval.v_number
165#define vv_float vv_di.di_tv.vval.v_float
166#define vv_str vv_di.di_tv.vval.v_string
167#define vv_list vv_di.di_tv.vval.v_list
168#define vv_dict vv_di.di_tv.vval.v_dict
169#define vv_blob vv_di.di_tv.vval.v_blob
170#define vv_tv vv_di.di_tv
171
172static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200173static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200174#define vimvarht vimvardict.dv_hashtab
175
176// for VIM_VERSION_ defines
177#include "version.h"
178
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200179static void list_glob_vars(int *first);
180static void list_buf_vars(int *first);
181static void list_win_vars(int *first);
182static void list_tab_vars(int *first);
183static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100184static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200185static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
186static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200187static void list_one_var(dictitem_T *v, char *prefix, int *first);
188static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
189
190/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200191 * Initialize global and vim special variables
192 */
193 void
194evalvars_init(void)
195{
196 int i;
197 struct vimvar *p;
198
199 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
200 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
201 vimvardict.dv_lock = VAR_FIXED;
202 hash_init(&compat_hashtab);
203
204 for (i = 0; i < VV_LEN; ++i)
205 {
206 p = &vimvars[i];
207 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
208 {
209 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
210 getout(1);
211 }
212 STRCPY(p->vv_di.di_key, p->vv_name);
213 if (p->vv_flags & VV_RO)
214 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
215 else if (p->vv_flags & VV_RO_SBX)
216 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
217 else
218 p->vv_di.di_flags = DI_FLAGS_FIX;
219
220 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000221 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000222 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 if (p->vv_flags & VV_COMPAT)
224 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000225 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200226 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200227 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
228 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200229
230 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
231 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100232 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
234 set_vim_var_list(VV_ERRORS, list_alloc());
235 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
236
237 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
238 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
239 set_vim_var_nr(VV_NONE, VVAL_NONE);
240 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100241 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
242 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100243 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000244 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
245 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
246 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000247 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200248
249 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
250 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
251 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
252 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
253 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
254 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
255 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
256 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
257 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
258 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
259 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000260 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
261 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200262
263 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
264
Drew Vogele30d1022021-10-24 20:35:07 +0100265 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
266
Bram Moolenaar439c0362020-06-06 15:58:03 +0200267 // Default for v:register is not 0 but '"'. This is adjusted once the
268 // clipboard has been setup by calling reset_reg_var().
269 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200270}
271
272#if defined(EXITFREE) || defined(PROTO)
273/*
274 * Free all vim variables information on exit
275 */
276 void
277evalvars_clear(void)
278{
279 int i;
280 struct vimvar *p;
281
282 for (i = 0; i < VV_LEN; ++i)
283 {
284 p = &vimvars[i];
285 if (p->vv_di.di_tv.v_type == VAR_STRING)
286 VIM_CLEAR(p->vv_str);
287 else if (p->vv_di.di_tv.v_type == VAR_LIST)
288 {
289 list_unref(p->vv_list);
290 p->vv_list = NULL;
291 }
292 }
293 hash_clear(&vimvarht);
294 hash_init(&vimvarht); // garbage_collect() will access it
295 hash_clear(&compat_hashtab);
296
297 // global variables
298 vars_clear(&globvarht);
299
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100300 // Script-local variables. Clear all the variables here.
301 // The scriptvar_T is cleared later in free_scriptnames(), because a
302 // variable in one script might hold a reference to the whole scope of
303 // another script.
304 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200305 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200306}
307#endif
308
309 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200310garbage_collect_globvars(int copyID)
311{
312 return set_ref_in_ht(&globvarht, copyID, NULL);
313}
314
315 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200316garbage_collect_vimvars(int copyID)
317{
318 return set_ref_in_ht(&vimvarht, copyID, NULL);
319}
320
321 int
322garbage_collect_scriptvars(int copyID)
323{
Bram Moolenaared234f22020-10-15 20:42:20 +0200324 int i;
325 int idx;
326 int abort = FALSE;
327 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200328
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100329 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200330 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200331 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
332
Bram Moolenaared234f22020-10-15 20:42:20 +0200333 si = SCRIPT_ITEM(i);
334 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
335 {
336 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
337
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100338 if (sv->sv_name != NULL)
339 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200340 }
341 }
342
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200343 return abort;
344}
345
346/*
347 * Set an internal variable to a string value. Creates the variable if it does
348 * not already exist.
349 */
350 void
351set_internal_string_var(char_u *name, char_u *value)
352{
353 char_u *val;
354 typval_T *tvp;
355
356 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000357 if (val == NULL)
358 return;
359
360 tvp = alloc_string_tv(val);
361 if (tvp == NULL)
362 return;
363
364 set_var(name, tvp, FALSE);
365 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200366}
367
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200368 int
369eval_charconvert(
370 char_u *enc_from,
371 char_u *enc_to,
372 char_u *fname_from,
373 char_u *fname_to)
374{
375 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000376 sctx_T saved_sctx = current_sctx;
377 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200378
379 set_vim_var_string(VV_CC_FROM, enc_from, -1);
380 set_vim_var_string(VV_CC_TO, enc_to, -1);
381 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
382 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000383 ctx = get_option_sctx("charconvert");
384 if (ctx != NULL)
385 current_sctx = *ctx;
386
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100387 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200388 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000389
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200390 set_vim_var_string(VV_CC_FROM, NULL, -1);
391 set_vim_var_string(VV_CC_TO, NULL, -1);
392 set_vim_var_string(VV_FNAME_IN, NULL, -1);
393 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000394 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200395
396 if (err)
397 return FAIL;
398 return OK;
399}
400
401# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
402 int
403eval_printexpr(char_u *fname, char_u *args)
404{
405 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000406 sctx_T saved_sctx = current_sctx;
407 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200408
409 set_vim_var_string(VV_FNAME_IN, fname, -1);
410 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000411 ctx = get_option_sctx("printexpr");
412 if (ctx != NULL)
413 current_sctx = *ctx;
414
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100415 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200416 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000417
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200418 set_vim_var_string(VV_FNAME_IN, NULL, -1);
419 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000420 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200421
422 if (err)
423 {
424 mch_remove(fname);
425 return FAIL;
426 }
427 return OK;
428}
429# endif
430
431# if defined(FEAT_DIFF) || defined(PROTO)
432 void
433eval_diff(
434 char_u *origfile,
435 char_u *newfile,
436 char_u *outfile)
437{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000438 sctx_T saved_sctx = current_sctx;
439 sctx_T *ctx;
440 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200441
442 set_vim_var_string(VV_FNAME_IN, origfile, -1);
443 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
444 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000445
446 ctx = get_option_sctx("diffexpr");
447 if (ctx != NULL)
448 current_sctx = *ctx;
449
450 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100451 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000452 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454 set_vim_var_string(VV_FNAME_IN, NULL, -1);
455 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
456 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000457 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200458}
459
460 void
461eval_patch(
462 char_u *origfile,
463 char_u *difffile,
464 char_u *outfile)
465{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000466 sctx_T saved_sctx = current_sctx;
467 sctx_T *ctx;
468 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200469
470 set_vim_var_string(VV_FNAME_IN, origfile, -1);
471 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
472 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000473
474 ctx = get_option_sctx("patchexpr");
475 if (ctx != NULL)
476 current_sctx = *ctx;
477
478 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100479 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000480 free_tv(tv);
481
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482 set_vim_var_string(VV_FNAME_IN, NULL, -1);
483 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
484 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000485 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200486}
487# endif
488
489#if defined(FEAT_SPELL) || defined(PROTO)
490/*
491 * Evaluate an expression to a list with suggestions.
492 * For the "expr:" part of 'spellsuggest'.
493 * Returns NULL when there is an error.
494 */
495 list_T *
496eval_spell_expr(char_u *badword, char_u *expr)
497{
498 typval_T save_val;
499 typval_T rettv;
500 list_T *list = NULL;
501 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000502 sctx_T saved_sctx = current_sctx;
503 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100504 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200505
506 // Set "v:val" to the bad word.
507 prepare_vimvar(VV_VAL, &save_val);
508 set_vim_var_string(VV_VAL, badword, -1);
509 if (p_verbose == 0)
510 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000511 ctx = get_option_sctx("spellsuggest");
512 if (ctx != NULL)
513 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200514
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100515 r = may_call_simple_func(p, &rettv);
516 if (r == NOTDONE)
517 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
518 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200519 {
520 if (rettv.v_type != VAR_LIST)
521 clear_tv(&rettv);
522 else
523 list = rettv.vval.v_list;
524 }
525
526 if (p_verbose == 0)
527 --emsg_off;
528 clear_tv(get_vim_var_tv(VV_VAL));
529 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000530 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200531
532 return list;
533}
534
535/*
536 * "list" is supposed to contain two items: a word and a number. Return the
537 * word in "pp" and the number as the return value.
538 * Return -1 if anything isn't right.
539 * Used to get the good word and score from the eval_spell_expr() result.
540 */
541 int
542get_spellword(list_T *list, char_u **pp)
543{
544 listitem_T *li;
545
546 li = list->lv_first;
547 if (li == NULL)
548 return -1;
549 *pp = tv_get_string(&li->li_tv);
550
551 li = li->li_next;
552 if (li == NULL)
553 return -1;
554 return (int)tv_get_number(&li->li_tv);
555}
556#endif
557
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200558/*
559 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200560 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 * When not used yet add the variable to the v: hashtable.
562 */
563 void
564prepare_vimvar(int idx, typval_T *save_tv)
565{
566 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200567 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000568 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000569 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200570}
571
572/*
573 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200574 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200575 * When no longer defined, remove the variable from the v: hashtable.
576 */
577 void
578restore_vimvar(int idx, typval_T *save_tv)
579{
580 hashitem_T *hi;
581
582 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000583 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
584 return;
585
586 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
587 if (HASHITEM_EMPTY(hi))
588 internal_error("restore_vimvar()");
589 else
590 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200591}
592
593/*
594 * List Vim variables.
595 */
596 static void
597list_vim_vars(int *first)
598{
599 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
600}
601
602/*
603 * List script-local variables, if there is a script.
604 */
605 static void
606list_script_vars(int *first)
607{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200608 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200609 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
610 "s:", FALSE, first);
611}
612
613/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100614 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
615 * But only when an identifier character follows.
616 */
617 int
618is_scoped_variable(char_u *name)
619{
620 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
621 && name[1] == ':'
622 && eval_isnamec(name[2]);
623}
624
625/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100626 * Evaluate one Vim expression {expr} in string "p" and append the
627 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100628 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100629 * Return a pointer to the character after "}", NULL for an error.
630 */
631 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100632eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100633{
634 char_u *block_start = skipwhite(p + 1); // skip the opening {
635 char_u *block_end = block_start;
636 char_u *expr_val;
637
638 if (*block_start == NUL)
639 {
640 semsg(_(e_missing_close_curly_str), p);
641 return NULL;
642 }
643 if (skip_expr(&block_end, NULL) == FAIL)
644 return NULL;
645 block_end = skipwhite(block_end);
646 if (*block_end != '}')
647 {
648 semsg(_(e_missing_close_curly_str), p);
649 return NULL;
650 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100651 if (evaluate)
652 {
653 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100654 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100655 *block_end = '}';
656 if (expr_val == NULL)
657 return NULL;
658 ga_concat(gap, expr_val);
659 vim_free(expr_val);
660 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100661
662 return block_end + 1;
663}
664
665/*
666 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
667 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
668 * Used for a heredoc assignment.
669 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100670 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100671 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100672eval_all_expr_in_str(char_u *str)
673{
674 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100675 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100676
677 ga_init2(&ga, 1, 80);
678 p = str;
679
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100680 while (*p != NUL)
681 {
LemonBoy2eaef102022-05-06 13:14:50 +0100682 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100683 int escaped_brace = FALSE;
684
685 // Look for a block start.
686 lit_start = p;
687 while (*p != '{' && *p != '}' && *p != NUL)
688 ++p;
689
690 if (*p != NUL && *p == p[1])
691 {
692 // Escaped brace, unescape and continue.
693 // Include the brace in the literal string.
694 ++p;
695 escaped_brace = TRUE;
696 }
697 else if (*p == '}')
698 {
699 semsg(_(e_stray_closing_curly_str), str);
700 ga_clear(&ga);
701 return NULL;
702 }
703
704 // Append the literal part.
705 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
706
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100707 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100708 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100709
LemonBoy2eaef102022-05-06 13:14:50 +0100710 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100711 {
LemonBoy2eaef102022-05-06 13:14:50 +0100712 // Skip the second brace.
713 ++p;
714 continue;
715 }
716
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100717 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100718 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100719 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100720 {
721 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722 return NULL;
723 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100724 }
725 ga_append(&ga, NUL);
726
727 return ga.ga_data;
728}
729
730/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200731 * Get a list of lines from a HERE document. The here document is a list of
732 * lines surrounded by a marker.
733 * cmd << {marker}
734 * {line1}
735 * {line2}
736 * ....
737 * {marker}
738 *
739 * The {marker} is a string. If the optional 'trim' word is supplied before the
740 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100741 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200742 *
743 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100744 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200745 * missing, then '.' is accepted as a marker.
746 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100747 * When compiling a heredoc assignment to a variable in a Vim9 def function,
748 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
749 * string values from the heredoc, vim9 instructions are generated. On success
750 * the returned list will be empty.
751 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100752 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200753 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100755heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200756{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100757 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200758 char_u *marker;
759 list_T *l;
760 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100761 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 int marker_indent_len = 0;
763 int text_indent_len = 0;
764 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200765 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200766 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100767 int evalstr = FALSE;
768 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100769 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
770 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771
772 if (eap->getline == NULL)
773 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000774 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 return NULL;
776 }
777
778 // Check for the optional 'trim' word before the marker
779 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100781 while (TRUE)
782 {
783 if (STRNCMP(cmd, "trim", 4) == 0
784 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100786 cmd = skipwhite(cmd + 4);
787
788 // Trim the indentation from all the lines in the here document.
789 // The amount of indentation trimmed is the same as the indentation
790 // of the first line after the :let command line. To find the end
791 // marker the indent of the :let command line is trimmed.
792 p = *eap->cmdlinep;
793 while (VIM_ISWHITE(*p))
794 {
795 p++;
796 marker_indent_len++;
797 }
798 text_indent_len = -1;
799
800 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200801 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100802 if (STRNCMP(cmd, "eval", 4) == 0
803 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
804 {
805 cmd = skipwhite(cmd + 4);
806 evalstr = TRUE;
807 continue;
808 }
809 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 }
811
812 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200813 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200814 {
815 marker = skipwhite(cmd);
816 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200817 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200818 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000819 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200820 return NULL;
821 }
822 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200823 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200824 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000825 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 return NULL;
827 }
828 }
829 else
830 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200831 // When getting lines for an embedded script, if the marker is missing,
832 // accept '.' as the marker.
833 if (script_get)
834 marker = dot;
835 else
836 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000837 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200838 return NULL;
839 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840 }
841
842 l = list_alloc();
843 if (l == NULL)
844 return NULL;
845
846 for (;;)
847 {
848 int mi = 0;
849 int ti = 0;
850
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100851 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200852 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
853 if (theline == NULL)
854 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000855 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200856 break;
857 }
858
859 // with "trim": skip the indent matching the :let line to find the
860 // marker
861 if (marker_indent_len > 0
862 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
863 mi = marker_indent_len;
864 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200865 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200866
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100867 // If expression evaluation failed in the heredoc, then skip till the
868 // end marker.
869 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100870 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100871
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 if (text_indent_len == -1 && *theline != NUL)
873 {
874 // set the text indent from the first line.
875 p = theline;
876 text_indent_len = 0;
877 while (VIM_ISWHITE(*p))
878 {
879 p++;
880 text_indent_len++;
881 }
882 text_indent = vim_strnsave(theline, text_indent_len);
883 }
884 // with "trim": skip the indent matching the first line
885 if (text_indent != NULL)
886 for (ti = 0; ti < text_indent_len; ++ti)
887 if (theline[ti] != text_indent[ti])
888 break;
889
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100890 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100891 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100892 {
LemonBoy2eaef102022-05-06 13:14:50 +0100893 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100894 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100895 vim_free(theline);
896 vim_free(text_indent);
897 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100898 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100899 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100900 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100901 else
902 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100903 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100904 {
905 str = eval_all_expr_in_str(str);
906 if (str == NULL)
907 {
908 // expression evaluation failed
909 eval_failed = TRUE;
910 continue;
911 }
912 vim_free(theline);
913 theline = str;
914 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100915
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100916 if (list_append_string(l, str, -1) == FAIL)
917 break;
918 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200919 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100920 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200921 vim_free(text_indent);
922
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100923 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
924 generate_NEWLIST(cctx, count, FALSE);
925
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100926 if (eval_failed)
927 {
928 // expression evaluation in the heredoc failed
929 list_free(l);
930 return NULL;
931 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200932 return l;
933}
934
935/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200936 * Vim9 variable declaration:
937 * ":var name"
938 * ":var name: type"
939 * ":var name = expr"
940 * ":var name: type = expr"
941 * etc.
942 */
943 void
944ex_var(exarg_T *eap)
945{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000946 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000947 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000948
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200949 if (!in_vim9script())
950 {
951 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
952 return;
953 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000954 has_var = checkforcmd_noparen(&p, "var", 3);
955 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000956 {
957 emsg(_(e_cannot_declare_variable_on_command_line));
958 return;
959 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200960 ex_let(eap);
961}
962
963/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 * ":let" list all variable values
965 * ":let var1 var2" list variable values
966 * ":let var = expr" assignment command.
967 * ":let var += expr" assignment command.
968 * ":let var -= expr" assignment command.
969 * ":let var *= expr" assignment command.
970 * ":let var /= expr" assignment command.
971 * ":let var %= expr" assignment command.
972 * ":let var .= expr" assignment command.
973 * ":let var ..= expr" assignment command.
974 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100976 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200977 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200978 * ":final var = expr" assignment command.
979 * ":final [var1, var2] = expr" unpack list.
980 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 * ":const" list all variable values
982 * ":const var1 var2" list variable values
983 * ":const var = expr" assignment command.
984 * ":const [var1, var2] = expr" unpack list.
985 */
986 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200987ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988{
989 char_u *arg = eap->arg;
990 char_u *expr = NULL;
991 typval_T rettv;
992 int i;
993 int var_count = 0;
994 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200995 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200996 char_u *argend;
997 int first = TRUE;
998 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200999 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001000 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001001 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001002
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001003 if (eap->cmdidx == CMD_final && !vim9script)
1004 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001005 // In legacy Vim script ":final" is short for ":finally".
1006 ex_finally(eap);
1007 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001008 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001009 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001010 {
1011 emsg(_(e_cannot_use_let_in_vim9_script));
1012 return;
1013 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001014
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001015 if (eap->cmdidx == CMD_const)
1016 flags |= ASSIGN_CONST;
1017 else if (eap->cmdidx == CMD_final)
1018 flags |= ASSIGN_FINAL;
1019
1020 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001022 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001024 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 if (argend == NULL)
1026 return;
1027 if (argend > arg && argend[-1] == '.') // for var.='str'
1028 --argend;
1029 expr = skipwhite(argend);
1030 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001031 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001032 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001033 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1034 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001035 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001036 {
1037 // ":let" without "=": list variables
1038 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001039 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001040 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001041 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001042 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001043 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001044 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001045 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001046 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001047 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001048 else
1049 // Vim9 declaration ":var name: type"
1050 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001051 }
1052 else
1053 {
1054 // ":let var1 var2" - list values
1055 arg = list_arg_vars(eap, arg, &first);
1056 }
1057 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058 else if (!eap->skip)
1059 {
1060 // ":let"
1061 list_glob_vars(&first);
1062 list_buf_vars(&first);
1063 list_win_vars(&first);
1064 list_tab_vars(&first);
1065 list_script_vars(&first);
1066 list_func_vars(&first);
1067 list_vim_vars(&first);
1068 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001069 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 }
1071 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
1072 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001073 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001074 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075
Bram Moolenaard9876422022-10-12 12:58:54 +01001076 // :let text =<< [trim] [eval] END
1077 // :var text =<< [trim] [eval] END
1078 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1079 || !IS_WHITE_OR_NUL(expr[3])))
1080 semsg(_(e_white_space_required_before_and_after_str_at_str),
1081 "=<<", expr);
1082 else
1083 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1084
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001085 if (l != NULL)
1086 {
1087 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001088 if (!eap->skip)
1089 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001090 // errors are for the assignment, not the end marker
1091 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001092 op[0] = '=';
1093 op[1] = NUL;
1094 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001095 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001096 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001097 clear_tv(&rettv);
1098 }
1099 }
1100 else
1101 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001102 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001103 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001104
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02001105 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +02001106 i = FAIL;
1107 if (has_assign || concat)
1108 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001109 int cur_lnum;
1110
Bram Moolenaar32e35112020-05-14 22:41:15 +02001111 op[0] = '=';
1112 op[1] = NUL;
1113 if (*expr != '=')
1114 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001115 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +02001116 {
1117 // +=, /=, etc. require an existing variable
Bram Moolenaara9fa8c52023-01-02 18:10:04 +00001118 semsg(_(e_cannot_use_operator_on_new_variable_str),
1119 eap->arg);
Bram Moolenaar122616d2020-08-21 21:32:50 +02001120 }
1121 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +02001122 {
1123 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001124 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001125 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001126 {
Bram Moolenaar32e35112020-05-14 22:41:15 +02001127 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001128 ++len;
1129 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001130 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001131 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001132 }
1133 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001134 ++expr;
1135
Bram Moolenaar7f2c3412021-11-29 16:01:49 +00001136 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001137 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001138 {
1139 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +01001140 semsg(_(e_white_space_required_before_and_after_str_at_str),
1141 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001142 }
Bram Moolenaar32e35112020-05-14 22:41:15 +02001143
1144 if (eap->skip)
1145 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +02001146 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +02001147 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001148 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001149 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001150 if (eap->skip)
1151 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +02001152 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +01001153
1154 // Restore the line number so that any type error is given for the
1155 // declaration, not the expression.
1156 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001157 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001158 if (eap->skip)
1159 {
1160 if (i != FAIL)
1161 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001162 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001163 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001164 {
1165 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001166 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001167 clear_tv(&rettv);
1168 }
1169 }
1170}
1171
1172/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001173 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001174 * Handles both "var" with any type and "[var, var; var]" with a list type.
1175 * When "op" is not NULL it points to a string with characters that
1176 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1177 * or concatenate.
1178 * Returns OK or FAIL;
1179 */
1180 int
1181ex_let_vars(
1182 char_u *arg_start,
1183 typval_T *tv,
1184 int copy, // copy values from "tv", don't move
1185 int semicolon, // from skip_var_list()
1186 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001187 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 char_u *op)
1189{
1190 char_u *arg = arg_start;
1191 list_T *l;
1192 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001193 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001194 listitem_T *item;
1195 typval_T ltv;
1196
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001197 if (tv->v_type == VAR_VOID)
1198 {
1199 emsg(_(e_cannot_use_void_value));
1200 return FAIL;
1201 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 if (*arg != '[')
1203 {
1204 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001205 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001206 return FAIL;
1207 return OK;
1208 }
1209
1210 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1211 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1212 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001213 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001214 return FAIL;
1215 }
1216
1217 i = list_len(l);
1218 if (semicolon == 0 && var_count < i)
1219 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001220 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 return FAIL;
1222 }
1223 if (var_count - semicolon > i)
1224 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001225 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001226 return FAIL;
1227 }
1228
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001229 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001230 item = l->lv_first;
1231 while (*arg != ']')
1232 {
1233 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001234 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001235 arg = ex_let_one(arg, &item->li_tv, TRUE,
1236 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 item = item->li_next;
1238 if (arg == NULL)
1239 return FAIL;
1240
1241 arg = skipwhite(arg);
1242 if (*arg == ';')
1243 {
1244 // Put the rest of the list (may be empty) in the var after ';'.
1245 // Create a new list for this.
1246 l = list_alloc();
1247 if (l == NULL)
1248 return FAIL;
1249 while (item != NULL)
1250 {
1251 list_append_tv(l, &item->li_tv);
1252 item = item->li_next;
1253 }
1254
1255 ltv.v_type = VAR_LIST;
1256 ltv.v_lock = 0;
1257 ltv.vval.v_list = l;
1258 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001259 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001261 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1262 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001263 clear_tv(&ltv);
1264 if (arg == NULL)
1265 return FAIL;
1266 break;
1267 }
1268 else if (*arg != ',' && *arg != ']')
1269 {
1270 internal_error("ex_let_vars()");
1271 return FAIL;
1272 }
1273 }
1274
1275 return OK;
1276}
1277
1278/*
1279 * Skip over assignable variable "var" or list of variables "[var, var]".
1280 * Used for ":let varvar = expr" and ":for varvar in expr".
1281 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001282 * for "[var, var; var]" set "semicolon" to 1.
1283 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001284 * Return NULL for an error.
1285 */
1286 char_u *
1287skip_var_list(
1288 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001291 int *semicolon,
1292 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293{
1294 char_u *p, *s;
1295
1296 if (*arg == '[')
1297 {
1298 // "[var, var]": find the matching ']'.
1299 p = arg;
1300 for (;;)
1301 {
1302 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001303 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304 if (s == p)
1305 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001306 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001307 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001308 return NULL;
1309 }
1310 ++*var_count;
1311
1312 p = skipwhite(s);
1313 if (*p == ']')
1314 break;
1315 else if (*p == ';')
1316 {
1317 if (*semicolon == 1)
1318 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001319 if (!silent)
1320 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001321 return NULL;
1322 }
1323 *semicolon = 1;
1324 }
1325 else if (*p != ',')
1326 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001327 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001328 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001329 return NULL;
1330 }
1331 }
1332 return p + 1;
1333 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001334
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001335 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336}
1337
1338/*
1339 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1340 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001343 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001346 char_u *end;
1347 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001349 if (*arg == '@' && arg[1] != NUL)
1350 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001351
1352 // termcap option name may have non-alpha characters
1353 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1354 return arg + 5;
1355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001358
1359 // "a: type" is declaring variable "a" with a type, not "a:".
1360 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001361 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001362 --end;
1363
Bram Moolenaar585587d2021-01-17 20:52:13 +01001364 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001366 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001367 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 }
1369 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001370}
1371
1372/*
1373 * List variables for hashtab "ht" with prefix "prefix".
1374 * If "empty" is TRUE also list NULL strings as empty strings.
1375 */
1376 void
1377list_hashtable_vars(
1378 hashtab_T *ht,
1379 char *prefix,
1380 int empty,
1381 int *first)
1382{
1383 hashitem_T *hi;
1384 dictitem_T *di;
1385 int todo;
1386 char_u buf[IOSIZE];
1387
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001388 int save_ht_flags = ht->ht_flags;
1389 ht->ht_flags |= HTFLAGS_FROZEN;
1390
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001391 todo = (int)ht->ht_used;
1392 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1393 {
1394 if (!HASHITEM_EMPTY(hi))
1395 {
1396 --todo;
1397 di = HI2DI(hi);
1398
1399 // apply :filter /pat/ to variable name
1400 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1401 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1402 if (message_filtered(buf))
1403 continue;
1404
1405 if (empty || di->di_tv.v_type != VAR_STRING
1406 || di->di_tv.vval.v_string != NULL)
1407 list_one_var(di, prefix, first);
1408 }
1409 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001410
1411 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001412}
1413
1414/*
1415 * List global variables.
1416 */
1417 static void
1418list_glob_vars(int *first)
1419{
1420 list_hashtable_vars(&globvarht, "", TRUE, first);
1421}
1422
1423/*
1424 * List buffer variables.
1425 */
1426 static void
1427list_buf_vars(int *first)
1428{
1429 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1430}
1431
1432/*
1433 * List window variables.
1434 */
1435 static void
1436list_win_vars(int *first)
1437{
1438 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1439}
1440
1441/*
1442 * List tab page variables.
1443 */
1444 static void
1445list_tab_vars(int *first)
1446{
1447 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1448}
1449
1450/*
1451 * List variables in "arg".
1452 */
1453 static char_u *
1454list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1455{
1456 int error = FALSE;
1457 int len;
1458 char_u *name;
1459 char_u *name_start;
1460 char_u *arg_subsc;
1461 char_u *tofree;
1462 typval_T tv;
1463
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001464 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 {
1466 if (error || eap->skip)
1467 {
1468 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1469 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1470 {
1471 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001472 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001473 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474 break;
1475 }
1476 }
1477 else
1478 {
1479 // get_name_len() takes care of expanding curly braces
1480 name_start = name = arg;
1481 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1482 if (len <= 0)
1483 {
1484 // This is mainly to keep test 49 working: when expanding
1485 // curly braces fails overrule the exception error message.
1486 if (len < 0 && !aborting())
1487 {
1488 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001489 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001490 break;
1491 }
1492 error = TRUE;
1493 }
1494 else
1495 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001496 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001497 if (tofree != NULL)
1498 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001499 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001500 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001501 error = TRUE;
1502 else
1503 {
1504 // handle d.key, l[idx], f(expr)
1505 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001506 if (handle_subscript(&arg, name_start, &tv,
1507 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001508 error = TRUE;
1509 else
1510 {
1511 if (arg == arg_subsc && len == 2 && name[1] == ':')
1512 {
1513 switch (*name)
1514 {
1515 case 'g': list_glob_vars(first); break;
1516 case 'b': list_buf_vars(first); break;
1517 case 'w': list_win_vars(first); break;
1518 case 't': list_tab_vars(first); break;
1519 case 'v': list_vim_vars(first); break;
1520 case 's': list_script_vars(first); break;
1521 case 'l': list_func_vars(first); break;
1522 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001523 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524 }
1525 }
1526 else
1527 {
1528 char_u numbuf[NUMBUFLEN];
1529 char_u *tf;
1530 int c;
1531 char_u *s;
1532
1533 s = echo_string(&tv, &tf, numbuf, 0);
1534 c = *arg;
1535 *arg = NUL;
1536 list_one_var_a("",
1537 arg == arg_subsc ? name : name_start,
1538 tv.v_type,
1539 s == NULL ? (char_u *)"" : s,
1540 first);
1541 *arg = c;
1542 vim_free(tf);
1543 }
1544 clear_tv(&tv);
1545 }
1546 }
1547 }
1548
1549 vim_free(tofree);
1550 }
1551
1552 arg = skipwhite(arg);
1553 }
1554
1555 return arg;
1556}
1557
1558/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001559 * Set an environment variable, part of ex_let_one().
1560 */
1561 static char_u *
1562ex_let_env(
1563 char_u *arg,
1564 typval_T *tv,
1565 int flags,
1566 char_u *endchars,
1567 char_u *op)
1568{
1569 char_u *arg_end = NULL;
1570 char_u *name;
1571 int len;
1572
1573 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1574 && (flags & ASSIGN_FOR_LOOP) == 0)
1575 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001576 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001577 return NULL;
1578 }
1579
1580 // Find the end of the name.
1581 ++arg;
1582 name = arg;
1583 len = get_env_len(&arg);
1584 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001585 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001586 else
1587 {
1588 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001589 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001590 else if (endchars != NULL
1591 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1592 emsg(_(e_unexpected_characters_in_let));
1593 else if (!check_secure())
1594 {
1595 char_u *tofree = NULL;
1596 int c1 = name[len];
1597 char_u *p;
1598
1599 name[len] = NUL;
1600 p = tv_get_string_chk(tv);
1601 if (p != NULL && op != NULL && *op == '.')
1602 {
1603 int mustfree = FALSE;
1604 char_u *s = vim_getenv(name, &mustfree);
1605
1606 if (s != NULL)
1607 {
1608 p = tofree = concat_str(s, p);
1609 if (mustfree)
1610 vim_free(s);
1611 }
1612 }
1613 if (p != NULL)
1614 {
1615 vim_setenv_ext(name, p);
1616 arg_end = arg;
1617 }
1618 name[len] = c1;
1619 vim_free(tofree);
1620 }
1621 }
1622 return arg_end;
1623}
1624
1625/*
1626 * Set an option, part of ex_let_one().
1627 */
1628 static char_u *
1629ex_let_option(
1630 char_u *arg,
1631 typval_T *tv,
1632 int flags,
1633 char_u *endchars,
1634 char_u *op)
1635{
1636 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001637 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001638 char_u *arg_end = NULL;
1639
1640 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1641 && (flags & ASSIGN_FOR_LOOP) == 0)
1642 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001643 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001644 return NULL;
1645 }
1646
1647 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001648 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001649 if (p == NULL || (endchars != NULL
1650 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1651 emsg(_(e_unexpected_characters_in_let));
1652 else
1653 {
1654 int c1;
1655 long n = 0;
1656 getoption_T opt_type;
1657 long numval;
1658 char_u *stringval = NULL;
1659 char_u *s = NULL;
1660 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001661 int opt_p_flags;
1662 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001663 char_u numbuf[NUMBUFLEN];
1664
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001665 c1 = *p;
1666 *p = NUL;
1667
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001668 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1669 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001670 if ((opt_type == gov_bool
1671 || opt_type == gov_number
1672 || opt_type == gov_hidden_bool
1673 || opt_type == gov_hidden_number)
1674 && (tv->v_type != VAR_STRING || !in_vim9script()))
1675 {
1676 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1677 // bool, possibly hidden
1678 n = (long)tv_get_bool(tv);
1679 else
1680 // number, possibly hidden
1681 n = (long)tv_get_number(tv);
1682 }
1683
Bram Moolenaaref082e12021-12-12 21:02:03 +00001684 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001685 || tv->v_type == VAR_FUNC))
1686 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001687 // If the option can be set to a function reference or a lambda
1688 // and the passed value is a function reference, then convert it to
1689 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001690 s = tv2string(tv, &tofree, numbuf, 0);
1691 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001692 // Avoid setting a string option to the text "v:false" or similar.
1693 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001694 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001695 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1696 s = tv_get_string_chk(tv);
1697
1698 if (op != NULL && *op != '=')
1699 {
1700 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1701 || (opt_type == gov_string && *op != '.'))
1702 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001703 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001704 failed = TRUE; // don't set the value
1705
1706 }
1707 else
1708 {
1709 // number, in legacy script also bool
1710 if (opt_type == gov_number
1711 || (opt_type == gov_bool && !in_vim9script()))
1712 {
1713 switch (*op)
1714 {
1715 case '+': n = numval + n; break;
1716 case '-': n = numval - n; break;
1717 case '*': n = numval * n; break;
1718 case '/': n = (long)num_divide(numval, n,
1719 &failed); break;
1720 case '%': n = (long)num_modulus(numval, n,
1721 &failed); break;
1722 }
1723 s = NULL;
1724 }
1725 else if (opt_type == gov_string
1726 && stringval != NULL && s != NULL)
1727 {
1728 // string
1729 s = concat_str(stringval, s);
1730 vim_free(stringval);
1731 stringval = s;
1732 }
1733 }
1734 }
1735
1736 if (!failed)
1737 {
1738 if (opt_type != gov_string || s != NULL)
1739 {
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001740 char *err = set_option_value(arg, n, s, scope);
1741
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001742 arg_end = p;
Bram Moolenaar8ccbbeb2022-03-02 19:49:38 +00001743 if (err != NULL)
1744 emsg(_(err));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001745 }
1746 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001747 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001748 }
1749 *p = c1;
1750 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001751 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001752 }
1753 return arg_end;
1754}
1755
1756/*
1757 * Set a register, part of ex_let_one().
1758 */
1759 static char_u *
1760ex_let_register(
1761 char_u *arg,
1762 typval_T *tv,
1763 int flags,
1764 char_u *endchars,
1765 char_u *op)
1766{
1767 char_u *arg_end = NULL;
1768
1769 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1770 && (flags & ASSIGN_FOR_LOOP) == 0)
1771 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001772 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001773 return NULL;
1774 }
1775 ++arg;
1776 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001777 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001778 else if (endchars != NULL
1779 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1780 emsg(_(e_unexpected_characters_in_let));
1781 else
1782 {
1783 char_u *ptofree = NULL;
1784 char_u *p;
1785
1786 p = tv_get_string_chk(tv);
1787 if (p != NULL && op != NULL && *op == '.')
1788 {
1789 char_u *s = get_reg_contents(*arg == '@'
1790 ? '"' : *arg, GREG_EXPR_SRC);
1791
1792 if (s != NULL)
1793 {
1794 p = ptofree = concat_str(s, p);
1795 vim_free(s);
1796 }
1797 }
1798 if (p != NULL)
1799 {
1800 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1801 arg_end = arg + 1;
1802 }
1803 vim_free(ptofree);
1804 }
1805 return arg_end;
1806}
1807
1808/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1810 * Returns a pointer to the char just after the var name.
1811 * Returns NULL if there is an error.
1812 */
1813 static char_u *
1814ex_let_one(
1815 char_u *arg, // points to variable name
1816 typval_T *tv, // value to assign to variable
1817 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001818 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001820 char_u *op, // "+", "-", "." or NULL
1821 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001822{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001824
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001825 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001826 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001827 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1828 {
1829 vim9_declare_error(arg);
1830 return NULL;
1831 }
1832
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833 if (*arg == '$')
1834 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001835 // ":let $VAR = expr": Set environment variable.
1836 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001837 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001838 else if (*arg == '&')
1839 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001840 // ":let &option = expr": Set option value.
1841 // ":let &l:option = expr": Set local option value.
1842 // ":let &g:option = expr": Set global option value.
1843 // ":for &ts in range(8)": Set option value for for loop
1844 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 else if (*arg == '@')
1847 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001848 // ":let @r = expr": Set register contents.
1849 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 else if (eval_isnamec1(*arg) || *arg == '{')
1852 {
1853 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001854 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001855 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1856 ? GLV_NO_DECL : 0;
1857 if (op != NULL && *op != '=')
1858 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001859
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001860 // ":let var = expr": Set internal variable.
1861 // ":let var: type = expr": Set internal variable with type.
1862 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001863 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001864 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 if (endchars != NULL && vim_strchr(endchars,
1867 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001868 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001869 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001870 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871 else
1872 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001873 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001874 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 }
1876 }
1877 clear_lval(&lv);
1878 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001880 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001881
1882 return arg_end;
1883}
1884
1885/*
1886 * ":unlet[!] var1 ... " command.
1887 */
1888 void
1889ex_unlet(exarg_T *eap)
1890{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001891 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892}
1893
1894/*
1895 * ":lockvar" and ":unlockvar" commands
1896 */
1897 void
1898ex_lockvar(exarg_T *eap)
1899{
1900 char_u *arg = eap->arg;
1901 int deep = 2;
1902
1903 if (eap->forceit)
1904 deep = -1;
1905 else if (vim_isdigit(*arg))
1906 {
1907 deep = getdigits(&arg);
1908 arg = skipwhite(arg);
1909 }
1910
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001911 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001912}
1913
1914/*
1915 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001916 * Also used for Vim9 script. "callback" is invoked as:
1917 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001918 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001919 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001920ex_unletlock(
1921 exarg_T *eap,
1922 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001923 int deep,
1924 int glv_flags,
1925 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1926 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001927{
1928 char_u *arg = argstart;
1929 char_u *name_end;
1930 int error = FALSE;
1931 lval_T lv;
1932
1933 do
1934 {
1935 if (*arg == '$')
1936 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001937 lv.ll_name = arg;
1938 lv.ll_tv = NULL;
1939 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001940 if (get_env_len(&arg) == 0)
1941 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001942 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001943 return;
1944 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001945 if (!error && !eap->skip
1946 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1947 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001948 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001949 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001950 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001951 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001952 // Parse the name and find the end.
1953 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001954 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001955 if (lv.ll_name == NULL)
1956 error = TRUE; // error but continue parsing
1957 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1958 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001959 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001960 if (name_end != NULL)
1961 {
1962 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001963 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001964 }
1965 if (!(eap->skip || error))
1966 clear_lval(&lv);
1967 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001970 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001971 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001972 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001973
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001974 if (!eap->skip)
1975 clear_lval(&lv);
1976 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001977
1978 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001979 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001980
Bram Moolenaar63b91732021-08-05 20:40:03 +02001981 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001982}
1983
1984 static int
1985do_unlet_var(
1986 lval_T *lp,
1987 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001988 exarg_T *eap,
1989 int deep UNUSED,
1990 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001991{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001992 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001993 int ret = OK;
1994 int cc;
1995
1996 if (lp->ll_tv == NULL)
1997 {
1998 cc = *name_end;
1999 *name_end = NUL;
2000
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002001 // Environment variable, normal name or expanded name.
2002 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002003 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002004 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002005 ret = FAIL;
2006 *name_end = cc;
2007 }
2008 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002009 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002010 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002011 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002012 return FAIL;
2013 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002014 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2015 !lp->ll_empty2, lp->ll_n2);
2016 else if (lp->ll_list != NULL)
2017 // unlet a List item.
2018 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002019 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002020 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002021 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002022
2023 return ret;
2024}
2025
2026/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002027 * Unlet one item or a range of items from a list.
2028 * Return OK or FAIL.
2029 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002030 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002031list_unlet_range(
2032 list_T *l,
2033 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002034 long n1_arg,
2035 int has_n2,
2036 long n2)
2037{
2038 listitem_T *li = li_first;
2039 int n1 = n1_arg;
2040
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002041 // Delete a range of List items.
2042 li = li_first;
2043 n1 = n1_arg;
2044 while (li != NULL && (!has_n2 || n2 >= n1))
2045 {
2046 listitem_T *next = li->li_next;
2047
2048 listitem_remove(l, li);
2049 li = next;
2050 ++n1;
2051 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002052}
2053/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002054 * "unlet" a variable. Return OK if it existed, FAIL if not.
2055 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2056 */
2057 int
2058do_unlet(char_u *name, int forceit)
2059{
2060 hashtab_T *ht;
2061 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002062 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002063 dict_T *d;
2064 dictitem_T *di;
2065
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002066 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002067 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002068 return FAIL;
2069
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002070 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002071
2072 // can't :unlet a script variable in Vim9 script from a function
2073 if (ht == get_script_local_ht()
2074 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2075 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2076 == SCRIPT_VERSION_VIM9
2077 && check_vim9_unlet(name) == FAIL)
2078 return FAIL;
2079
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002080 if (ht != NULL && *varname != NUL)
2081 {
2082 d = get_current_funccal_dict(ht);
2083 if (d == NULL)
2084 {
2085 if (ht == &globvarht)
2086 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002087 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002088 d = &vimvardict;
2089 else
2090 {
2091 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2092 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2093 }
2094 if (d == NULL)
2095 {
2096 internal_error("do_unlet()");
2097 return FAIL;
2098 }
2099 }
2100 hi = hash_find(ht, varname);
2101 if (HASHITEM_EMPTY(hi))
2102 hi = find_hi_in_scoped_ht(name, &ht);
2103 if (hi != NULL && !HASHITEM_EMPTY(hi))
2104 {
2105 di = HI2DI(hi);
2106 if (var_check_fixed(di->di_flags, name, FALSE)
2107 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002108 || value_check_lock(d->dv_lock, name, FALSE)
2109 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002110 return FAIL;
2111
2112 delete_var(ht, hi);
2113 return OK;
2114 }
2115 }
2116 if (forceit)
2117 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002118 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002119 return FAIL;
2120}
2121
2122/*
2123 * Lock or unlock variable indicated by "lp".
2124 * "deep" is the levels to go (-1 for unlimited);
2125 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2126 */
2127 static int
2128do_lock_var(
2129 lval_T *lp,
2130 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002131 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002132 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002133 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002134{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002135 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002136 int ret = OK;
2137 int cc;
2138 dictitem_T *di;
2139
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002140 if (lp->ll_tv == NULL)
2141 {
2142 cc = *name_end;
2143 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002144 if (*lp->ll_name == '$')
2145 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002146 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002147 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002148 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002149 else
2150 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002151 // Normal name or expanded name.
2152 di = find_var(lp->ll_name, NULL, TRUE);
2153 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002154 {
2155 if (in_vim9script())
2156 semsg(_(e_cannot_find_variable_to_unlock_str),
2157 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002158 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002159 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002160 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002161 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002162 if ((di->di_flags & DI_FLAGS_FIX)
2163 && di->di_tv.v_type != VAR_DICT
2164 && di->di_tv.v_type != VAR_LIST)
2165 {
2166 // For historic reasons this error is not given for a list
2167 // or dict. E.g., the b: dict could be locked/unlocked.
2168 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2169 ret = FAIL;
2170 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002171 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002172 {
2173 if (in_vim9script())
2174 {
2175 svar_T *sv = find_typval_in_script(&di->di_tv,
2176 0, FALSE);
2177
2178 if (sv != NULL && sv->sv_const != 0)
2179 {
2180 semsg(_(e_cannot_change_readonly_variable_str),
2181 lp->ll_name);
2182 ret = FAIL;
2183 }
2184 }
2185
2186 if (ret == OK)
2187 {
2188 if (lock)
2189 di->di_flags |= DI_FLAGS_LOCK;
2190 else
2191 di->di_flags &= ~DI_FLAGS_LOCK;
2192 if (deep != 0)
2193 item_lock(&di->di_tv, deep, lock, FALSE);
2194 }
2195 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002196 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002197 }
2198 *name_end = cc;
2199 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002200 else if (deep == 0)
2201 {
2202 // nothing to do
2203 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002204 else if (lp->ll_range)
2205 {
2206 listitem_T *li = lp->ll_li;
2207
2208 // (un)lock a range of List items.
2209 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2210 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002211 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002212 li = li->li_next;
2213 ++lp->ll_n1;
2214 }
2215 }
2216 else if (lp->ll_list != NULL)
2217 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002218 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002219 else
2220 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002221 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002222
2223 return ret;
2224}
2225
2226/*
2227 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002228 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2229 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002230 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002231 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002232item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002233{
2234 static int recurse = 0;
2235 list_T *l;
2236 listitem_T *li;
2237 dict_T *d;
2238 blob_T *b;
2239 hashitem_T *hi;
2240 int todo;
2241
2242 if (recurse >= DICT_MAXNEST)
2243 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002244 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002245 return;
2246 }
2247 if (deep == 0)
2248 return;
2249 ++recurse;
2250
2251 // lock/unlock the item itself
2252 if (lock)
2253 tv->v_lock |= VAR_LOCKED;
2254 else
2255 tv->v_lock &= ~VAR_LOCKED;
2256
2257 switch (tv->v_type)
2258 {
2259 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002260 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002262 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002264 case VAR_STRING:
2265 case VAR_FUNC:
2266 case VAR_PARTIAL:
2267 case VAR_FLOAT:
2268 case VAR_SPECIAL:
2269 case VAR_JOB:
2270 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002271 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002272 case VAR_CLASS:
2273 case VAR_OBJECT:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002274 break;
2275
2276 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002277 if ((b = tv->vval.v_blob) != NULL
2278 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002279 {
2280 if (lock)
2281 b->bv_lock |= VAR_LOCKED;
2282 else
2283 b->bv_lock &= ~VAR_LOCKED;
2284 }
2285 break;
2286 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002287 if ((l = tv->vval.v_list) != NULL
2288 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002289 {
2290 if (lock)
2291 l->lv_lock |= VAR_LOCKED;
2292 else
2293 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002294 if (deep < 0 || deep > 1)
2295 {
2296 if (l->lv_first == &range_list_item)
2297 l->lv_lock |= VAR_ITEMS_LOCKED;
2298 else
2299 {
2300 // recursive: lock/unlock the items the List contains
2301 CHECK_LIST_MATERIALIZE(l);
2302 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2303 deep - 1, lock, check_refcount);
2304 }
2305 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002306 }
2307 break;
2308 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002309 if ((d = tv->vval.v_dict) != NULL
2310 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002311 {
2312 if (lock)
2313 d->dv_lock |= VAR_LOCKED;
2314 else
2315 d->dv_lock &= ~VAR_LOCKED;
2316 if (deep < 0 || deep > 1)
2317 {
2318 // recursive: lock/unlock the items the List contains
2319 todo = (int)d->dv_hashtab.ht_used;
2320 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2321 {
2322 if (!HASHITEM_EMPTY(hi))
2323 {
2324 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002325 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2326 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002327 }
2328 }
2329 }
2330 }
2331 }
2332 --recurse;
2333}
2334
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002335#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2336/*
2337 * Delete all "menutrans_" variables.
2338 */
2339 void
2340del_menutrans_vars(void)
2341{
2342 hashitem_T *hi;
2343 int todo;
2344
2345 hash_lock(&globvarht);
2346 todo = (int)globvarht.ht_used;
2347 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2348 {
2349 if (!HASHITEM_EMPTY(hi))
2350 {
2351 --todo;
2352 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2353 delete_var(&globvarht, hi);
2354 }
2355 }
2356 hash_unlock(&globvarht);
2357}
2358#endif
2359
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002360/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002361 * Local string buffer for the next two functions to store a variable name
2362 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2363 * get_user_var_name().
2364 */
2365
2366static char_u *varnamebuf = NULL;
2367static int varnamebuflen = 0;
2368
2369/*
2370 * Function to concatenate a prefix and a variable name.
2371 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002372 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002373cat_prefix_varname(int prefix, char_u *name)
2374{
2375 int len;
2376
2377 len = (int)STRLEN(name) + 3;
2378 if (len > varnamebuflen)
2379 {
2380 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002381 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002382 varnamebuf = alloc(len);
2383 if (varnamebuf == NULL)
2384 {
2385 varnamebuflen = 0;
2386 return NULL;
2387 }
2388 varnamebuflen = len;
2389 }
2390 *varnamebuf = prefix;
2391 varnamebuf[1] = ':';
2392 STRCPY(varnamebuf + 2, name);
2393 return varnamebuf;
2394}
2395
2396/*
2397 * Function given to ExpandGeneric() to obtain the list of user defined
2398 * (global/buffer/window/built-in) variable names.
2399 */
2400 char_u *
2401get_user_var_name(expand_T *xp, int idx)
2402{
2403 static long_u gdone;
2404 static long_u bdone;
2405 static long_u wdone;
2406 static long_u tdone;
2407 static int vidx;
2408 static hashitem_T *hi;
2409 hashtab_T *ht;
2410
2411 if (idx == 0)
2412 {
2413 gdone = bdone = wdone = vidx = 0;
2414 tdone = 0;
2415 }
2416
2417 // Global variables
2418 if (gdone < globvarht.ht_used)
2419 {
2420 if (gdone++ == 0)
2421 hi = globvarht.ht_array;
2422 else
2423 ++hi;
2424 while (HASHITEM_EMPTY(hi))
2425 ++hi;
2426 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2427 return cat_prefix_varname('g', hi->hi_key);
2428 return hi->hi_key;
2429 }
2430
2431 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002432 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002433 if (bdone < ht->ht_used)
2434 {
2435 if (bdone++ == 0)
2436 hi = ht->ht_array;
2437 else
2438 ++hi;
2439 while (HASHITEM_EMPTY(hi))
2440 ++hi;
2441 return cat_prefix_varname('b', hi->hi_key);
2442 }
2443
2444 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002445 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002446 if (wdone < ht->ht_used)
2447 {
2448 if (wdone++ == 0)
2449 hi = ht->ht_array;
2450 else
2451 ++hi;
2452 while (HASHITEM_EMPTY(hi))
2453 ++hi;
2454 return cat_prefix_varname('w', hi->hi_key);
2455 }
2456
2457 // t: variables
2458 ht = &curtab->tp_vars->dv_hashtab;
2459 if (tdone < ht->ht_used)
2460 {
2461 if (tdone++ == 0)
2462 hi = ht->ht_array;
2463 else
2464 ++hi;
2465 while (HASHITEM_EMPTY(hi))
2466 ++hi;
2467 return cat_prefix_varname('t', hi->hi_key);
2468 }
2469
2470 // v: variables
2471 if (vidx < VV_LEN)
2472 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2473
2474 VIM_CLEAR(varnamebuf);
2475 varnamebuflen = 0;
2476 return NULL;
2477}
2478
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002479 char *
2480get_var_special_name(int nr)
2481{
2482 switch (nr)
2483 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002484 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2485 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002486 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002487 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002488 }
2489 internal_error("get_var_special_name()");
2490 return "42";
2491}
2492
2493/*
2494 * Returns the global variable dictionary
2495 */
2496 dict_T *
2497get_globvar_dict(void)
2498{
2499 return &globvardict;
2500}
2501
2502/*
2503 * Returns the global variable hash table
2504 */
2505 hashtab_T *
2506get_globvar_ht(void)
2507{
2508 return &globvarht;
2509}
2510
2511/*
2512 * Returns the v: variable dictionary
2513 */
2514 dict_T *
2515get_vimvar_dict(void)
2516{
2517 return &vimvardict;
2518}
2519
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002520/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002522 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 */
2524 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002525find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002527 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2528 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529
2530 if (di == NULL)
2531 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002532 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2534 return (int)(vv - vimvars);
2535}
2536
2537
2538/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002539 * Set type of v: variable to "type".
2540 */
2541 void
2542set_vim_var_type(int idx, vartype_T type)
2543{
Bram Moolenaard787e402021-12-24 21:36:12 +00002544 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002545}
2546
2547/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002548 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002549 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002550 */
2551 void
2552set_vim_var_nr(int idx, varnumber_T val)
2553{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002554 vimvars[idx].vv_nr = val;
2555}
2556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 char *
2558get_vim_var_name(int idx)
2559{
2560 return vimvars[idx].vv_name;
2561}
2562
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002563/*
2564 * Get typval_T v: variable value.
2565 */
2566 typval_T *
2567get_vim_var_tv(int idx)
2568{
2569 return &vimvars[idx].vv_tv;
2570}
2571
Bram Moolenaard787e402021-12-24 21:36:12 +00002572 type_T *
2573get_vim_var_type(int idx, garray_T *type_list)
2574{
2575 if (vimvars[idx].vv_type != NULL)
2576 return vimvars[idx].vv_type;
2577 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2578}
2579
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002580/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002581 * Set v: variable to "tv". Only accepts the same type.
2582 * Takes over the value of "tv".
2583 */
2584 int
2585set_vim_var_tv(int idx, typval_T *tv)
2586{
Bram Moolenaard787e402021-12-24 21:36:12 +00002587 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002588 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002589 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002590 clear_tv(tv);
2591 return FAIL;
2592 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002593 // VV_RO is also checked when compiling, but let's check here as well.
2594 if (vimvars[idx].vv_flags & VV_RO)
2595 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002596 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002597 return FAIL;
2598 }
2599 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2600 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002601 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002602 return FAIL;
2603 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002604 clear_tv(&vimvars[idx].vv_di.di_tv);
2605 vimvars[idx].vv_di.di_tv = *tv;
2606 return OK;
2607}
2608
2609/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002610 * Get number v: variable value.
2611 */
2612 varnumber_T
2613get_vim_var_nr(int idx)
2614{
2615 return vimvars[idx].vv_nr;
2616}
2617
2618/*
2619 * Get string v: variable value. Uses a static buffer, can only be used once.
2620 * If the String variable has never been set, return an empty string.
2621 * Never returns NULL;
2622 */
2623 char_u *
2624get_vim_var_str(int idx)
2625{
2626 return tv_get_string(&vimvars[idx].vv_tv);
2627}
2628
2629/*
2630 * Get List v: variable value. Caller must take care of reference count when
2631 * needed.
2632 */
2633 list_T *
2634get_vim_var_list(int idx)
2635{
2636 return vimvars[idx].vv_list;
2637}
2638
2639/*
2640 * Get Dict v: variable value. Caller must take care of reference count when
2641 * needed.
2642 */
2643 dict_T *
2644get_vim_var_dict(int idx)
2645{
2646 return vimvars[idx].vv_dict;
2647}
2648
2649/*
2650 * Set v:char to character "c".
2651 */
2652 void
2653set_vim_var_char(int c)
2654{
2655 char_u buf[MB_MAXBYTES + 1];
2656
2657 if (has_mbyte)
2658 buf[(*mb_char2bytes)(c, buf)] = NUL;
2659 else
2660 {
2661 buf[0] = c;
2662 buf[1] = NUL;
2663 }
2664 set_vim_var_string(VV_CHAR, buf, -1);
2665}
2666
2667/*
2668 * Set v:count to "count" and v:count1 to "count1".
2669 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2670 */
2671 void
2672set_vcount(
2673 long count,
2674 long count1,
2675 int set_prevcount)
2676{
2677 if (set_prevcount)
2678 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2679 vimvars[VV_COUNT].vv_nr = count;
2680 vimvars[VV_COUNT1].vv_nr = count1;
2681}
2682
2683/*
2684 * Save variables that might be changed as a side effect. Used when executing
2685 * a timer callback.
2686 */
2687 void
2688save_vimvars(vimvars_save_T *vvsave)
2689{
2690 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2691 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2692 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2693}
2694
2695/*
2696 * Restore variables saved by save_vimvars().
2697 */
2698 void
2699restore_vimvars(vimvars_save_T *vvsave)
2700{
2701 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2702 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2703 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2704}
2705
2706/*
2707 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2708 * value.
2709 */
2710 void
2711set_vim_var_string(
2712 int idx,
2713 char_u *val,
2714 int len) // length of "val" to use or -1 (whole string)
2715{
2716 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002717 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002718 if (val == NULL)
2719 vimvars[idx].vv_str = NULL;
2720 else if (len == -1)
2721 vimvars[idx].vv_str = vim_strsave(val);
2722 else
2723 vimvars[idx].vv_str = vim_strnsave(val, len);
2724}
2725
2726/*
2727 * Set List v: variable to "val".
2728 */
2729 void
2730set_vim_var_list(int idx, list_T *val)
2731{
2732 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002733 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002734 vimvars[idx].vv_list = val;
2735 if (val != NULL)
2736 ++val->lv_refcount;
2737}
2738
2739/*
2740 * Set Dictionary v: variable to "val".
2741 */
2742 void
2743set_vim_var_dict(int idx, dict_T *val)
2744{
2745 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002746 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002747 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002748 if (val == NULL)
2749 return;
2750
2751 ++val->dv_refcount;
2752 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002753}
2754
2755/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002756 * Set the v:argv list.
2757 */
2758 void
2759set_argv_var(char **argv, int argc)
2760{
2761 list_T *l = list_alloc();
2762 int i;
2763
2764 if (l == NULL)
2765 getout(1);
2766 l->lv_lock = VAR_FIXED;
2767 for (i = 0; i < argc; ++i)
2768 {
2769 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2770 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002771 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002772 }
2773 set_vim_var_list(VV_ARGV, l);
2774}
2775
2776/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002777 * Reset v:register, taking the 'clipboard' setting into account.
2778 */
2779 void
2780reset_reg_var(void)
2781{
2782 int regname = 0;
2783
2784 // Adjust the register according to 'clipboard', so that when
2785 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2786#ifdef FEAT_CLIPBOARD
2787 adjust_clip_reg(&regname);
2788#endif
2789 set_reg_var(regname);
2790}
2791
2792/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002793 * Set v:register if needed.
2794 */
2795 void
2796set_reg_var(int c)
2797{
2798 char_u regname;
2799
2800 if (c == 0 || c == ' ')
2801 regname = '"';
2802 else
2803 regname = c;
2804 // Avoid free/alloc when the value is already right.
2805 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2806 set_vim_var_string(VV_REG, &regname, 1);
2807}
2808
2809/*
2810 * Get or set v:exception. If "oldval" == NULL, return the current value.
2811 * Otherwise, restore the value to "oldval" and return NULL.
2812 * Must always be called in pairs to save and restore v:exception! Does not
2813 * take care of memory allocations.
2814 */
2815 char_u *
2816v_exception(char_u *oldval)
2817{
2818 if (oldval == NULL)
2819 return vimvars[VV_EXCEPTION].vv_str;
2820
2821 vimvars[VV_EXCEPTION].vv_str = oldval;
2822 return NULL;
2823}
2824
2825/*
2826 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2827 * Otherwise, restore the value to "oldval" and return NULL.
2828 * Must always be called in pairs to save and restore v:throwpoint! Does not
2829 * take care of memory allocations.
2830 */
2831 char_u *
2832v_throwpoint(char_u *oldval)
2833{
2834 if (oldval == NULL)
2835 return vimvars[VV_THROWPOINT].vv_str;
2836
2837 vimvars[VV_THROWPOINT].vv_str = oldval;
2838 return NULL;
2839}
2840
2841/*
2842 * Set v:cmdarg.
2843 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2844 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2845 * Must always be called in pairs!
2846 */
2847 char_u *
2848set_cmdarg(exarg_T *eap, char_u *oldarg)
2849{
2850 char_u *oldval;
2851 char_u *newval;
2852 unsigned len;
2853
2854 oldval = vimvars[VV_CMDARG].vv_str;
2855 if (eap == NULL)
2856 {
2857 vim_free(oldval);
2858 vimvars[VV_CMDARG].vv_str = oldarg;
2859 return NULL;
2860 }
2861
2862 if (eap->force_bin == FORCE_BIN)
2863 len = 6;
2864 else if (eap->force_bin == FORCE_NOBIN)
2865 len = 8;
2866 else
2867 len = 0;
2868
2869 if (eap->read_edit)
2870 len += 7;
2871
2872 if (eap->force_ff != 0)
2873 len += 10; // " ++ff=unix"
2874 if (eap->force_enc != 0)
2875 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2876 if (eap->bad_char != 0)
2877 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2878
2879 newval = alloc(len + 1);
2880 if (newval == NULL)
2881 return NULL;
2882
2883 if (eap->force_bin == FORCE_BIN)
2884 sprintf((char *)newval, " ++bin");
2885 else if (eap->force_bin == FORCE_NOBIN)
2886 sprintf((char *)newval, " ++nobin");
2887 else
2888 *newval = NUL;
2889
2890 if (eap->read_edit)
2891 STRCAT(newval, " ++edit");
2892
2893 if (eap->force_ff != 0)
2894 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2895 eap->force_ff == 'u' ? "unix"
2896 : eap->force_ff == 'd' ? "dos"
2897 : "mac");
2898 if (eap->force_enc != 0)
2899 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2900 eap->cmd + eap->force_enc);
2901 if (eap->bad_char == BAD_KEEP)
2902 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2903 else if (eap->bad_char == BAD_DROP)
2904 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2905 else if (eap->bad_char != 0)
2906 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2907 vimvars[VV_CMDARG].vv_str = newval;
2908 return oldval;
2909}
2910
2911/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002912 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002913 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2914 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002915 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2916 */
2917 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002918eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002919 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00002920 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002921 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002922 typval_T *rettv, // NULL when only checking existence
2923 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002924 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002925{
2926 int ret = OK;
2927 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002928 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002929 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00002930 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002931 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002932
Bram Moolenaar94674f22023-01-06 18:42:20 +00002933 if (len > 0)
2934 {
2935 // truncate the name, so that we can use strcmp()
2936 cc = name[len];
2937 name[len] = NUL;
2938 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002939
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002940 // Check for local variable when debugging.
2941 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002942 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002943 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002944 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2945
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002946 if (v != NULL)
2947 {
2948 tv = &v->di_tv;
2949 if (dip != NULL)
2950 *dip = v;
2951 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002952 else
2953 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002954 }
2955
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002956 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002958 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002959 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2960
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002961 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002962 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963
2964 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002965 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002967 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002968 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002969 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002970 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002971 ht = &SCRIPT_VARS(sid);
2972 if (ht != NULL)
2973 {
2974 dictitem_T *v = find_var_in_ht(ht, 0, name,
2975 flags & EVAL_VAR_NOAUTOLOAD);
2976
2977 if (v != NULL)
2978 {
2979 tv = &v->di_tv;
2980 if (dip != NULL)
2981 *dip = v;
2982 }
2983 else
2984 ht = NULL;
2985 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002986 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002987 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002988 {
2989 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002990 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002991 ret = FAIL;
2992 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002993 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002994 else
2995 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002996 if (rettv != NULL)
2997 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01002998 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002999 rettv->v_type = VAR_ANY;
3000 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3001 }
3002 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003005 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003006 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003007 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003008 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003009
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003010 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003011 // function name. For a global non-autoload function "g:" is
3012 // required.
3013 if (ufunc != NULL && (has_g_prefix
3014 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003015 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003016 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003017 if (rettv != NULL)
3018 {
3019 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003020 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003021 // Keep the "g:", otherwise script-local may be
3022 // assumed.
3023 rettv->vval.v_string = vim_strsave(name);
3024 else
3025 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003026 if (rettv->vval.v_string != NULL)
3027 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003028 }
3029 }
3030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 }
3032
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003033 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003034 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003035 if (tv == NULL)
3036 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003037 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003038 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003039 ret = FAIL;
3040 }
3041 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003042 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003043 svar_T *sv = NULL;
3044 int was_assigned = FALSE;
3045
Bram Moolenaar11005b02021-07-11 20:59:00 +02003046 if (ht != NULL && ht == get_script_local_ht()
3047 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003048 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003049 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003050 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003051 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003052 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003053 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3054 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003055 }
3056
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003057 // If a list or dict variable wasn't initialized and has meaningful
3058 // type, do it now. Not for global variables, they are not
3059 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003060 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003061 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003062 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003063 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003064 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003065 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003066 tv->vval.v_dict = dict_alloc();
3067 if (tv->vval.v_dict != NULL)
3068 {
3069 ++tv->vval.v_dict->dv_refcount;
3070 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003071 if (sv != NULL)
3072 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003073 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003074 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003075 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003076 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003077 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003078 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003079 tv->vval.v_list = list_alloc();
3080 if (tv->vval.v_list != NULL)
3081 {
3082 ++tv->vval.v_list->lv_refcount;
3083 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003084 if (sv != NULL)
3085 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003086 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003087 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003088 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003089 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003090 || !in_vim9script()))
3091 {
3092 tv->vval.v_blob = blob_alloc();
3093 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003094 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003095 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003096 if (sv != NULL)
3097 sv->sv_flags |= SVFLAG_ASSIGNED;
3098 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003099 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003100 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003101 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003102 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003104
Bram Moolenaar94674f22023-01-06 18:42:20 +00003105 if (len > 0)
3106 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003107
3108 return ret;
3109}
3110
3111/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003112 * Get the value of internal variable "name", also handling "import.name".
3113 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3114 */
3115 int
3116eval_variable_import(
3117 char_u *name,
3118 typval_T *rettv)
3119{
3120 char_u *s = name;
3121 while (ASCII_ISALNUM(*s) || *s == '_')
3122 ++s;
3123 int len = (int)(s - name);
3124
3125 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3126 return FAIL;
3127 if (rettv->v_type == VAR_ANY && *s == '.')
3128 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003129 char_u *ns = s + 1;
3130 s = ns;
3131 while (ASCII_ISALNUM(*s) || *s == '_')
3132 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003133 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003134 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003135 }
3136 return OK;
3137}
3138
3139
3140/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003141 * Check if variable "name[len]" is a local variable or an argument.
3142 * If so, "*eval_lavars_used" is set to TRUE.
3143 */
3144 void
3145check_vars(char_u *name, int len)
3146{
3147 int cc;
3148 char_u *varname;
3149 hashtab_T *ht;
3150
3151 if (eval_lavars_used == NULL)
3152 return;
3153
3154 // truncate the name, so that we can use strcmp()
3155 cc = name[len];
3156 name[len] = NUL;
3157
3158 ht = find_var_ht(name, &varname);
3159 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3160 {
3161 if (find_var(name, NULL, TRUE) != NULL)
3162 *eval_lavars_used = TRUE;
3163 }
3164
3165 name[len] = cc;
3166}
3167
3168/*
3169 * Find variable "name" in the list of variables.
3170 * Return a pointer to it if found, NULL if not found.
3171 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003172 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003173 */
3174 dictitem_T *
3175find_var(char_u *name, hashtab_T **htp, int no_autoload)
3176{
3177 char_u *varname;
3178 hashtab_T *ht;
3179 dictitem_T *ret = NULL;
3180
3181 ht = find_var_ht(name, &varname);
3182 if (htp != NULL)
3183 *htp = ht;
3184 if (ht == NULL)
3185 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003186 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003187 if (ret != NULL)
3188 return ret;
3189
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003190 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003191 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003192 if (ret != NULL)
3193 return ret;
3194
3195 // in Vim9 script items without a scope can be script-local
3196 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3197 {
3198 ht = get_script_local_ht();
3199 if (ht != NULL)
3200 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003201 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003202 if (ret != NULL)
3203 {
3204 if (htp != NULL)
3205 *htp = ht;
3206 return ret;
3207 }
3208 }
3209 }
3210
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003211 // When using "vim9script autoload" script-local items are prefixed but can
3212 // be used with s:name.
3213 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003214 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003215 {
3216 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3217
3218 if (si->sn_autoload_prefix != NULL)
3219 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003220 char_u *base_name = (name[0] == 's' && name[1] == ':')
3221 ? name + 2 : name;
3222 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003223
3224 if (auto_name != NULL)
3225 {
3226 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003227 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003228 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003229 if (ret != NULL)
3230 {
3231 if (htp != NULL)
3232 *htp = ht;
3233 return ret;
3234 }
3235 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003236 }
3237 }
3238
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003239 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003240}
3241
3242/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003243 * Like find_var() but if the name starts with <SNR>99_ then look in the
3244 * referenced script (used for a funcref).
3245 */
3246 dictitem_T *
3247find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3248{
3249 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3250 {
3251 char_u *p = name + 5;
3252 int sid = getdigits(&p);
3253
3254 if (SCRIPT_ID_VALID(sid) && *p == '_')
3255 {
3256 hashtab_T *ht = &SCRIPT_VARS(sid);
3257
3258 if (ht != NULL)
3259 {
3260 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3261
3262 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003263 {
3264 if (htp != NULL)
3265 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003266 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003267 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003268 }
3269 }
3270 }
3271
3272 return find_var(name, htp, no_autoload);
3273}
3274
3275/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003276 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003277 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003278 * Returns NULL if not found.
3279 */
3280 dictitem_T *
3281find_var_in_ht(
3282 hashtab_T *ht,
3283 int htname,
3284 char_u *varname,
3285 int no_autoload)
3286{
3287 hashitem_T *hi;
3288
3289 if (*varname == NUL)
3290 {
3291 // Must be something like "s:", otherwise "ht" would be NULL.
3292 switch (htname)
3293 {
3294 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3295 case 'g': return &globvars_var;
3296 case 'v': return &vimvars_var;
3297 case 'b': return &curbuf->b_bufvar;
3298 case 'w': return &curwin->w_winvar;
3299 case 't': return &curtab->tp_winvar;
3300 case 'l': return get_funccal_local_var();
3301 case 'a': return get_funccal_args_var();
3302 }
3303 return NULL;
3304 }
3305
3306 hi = hash_find(ht, varname);
3307 if (HASHITEM_EMPTY(hi))
3308 {
3309 // For global variables we may try auto-loading the script. If it
3310 // worked find the variable again. Don't auto-load a script if it was
3311 // loaded already, otherwise it would be loaded every time when
3312 // checking if a function name is a Funcref variable.
3313 if (ht == &globvarht && !no_autoload)
3314 {
3315 // Note: script_autoload() may make "hi" invalid. It must either
3316 // be obtained again or not used.
3317 if (!script_autoload(varname, FALSE) || aborting())
3318 return NULL;
3319 hi = hash_find(ht, varname);
3320 }
3321 if (HASHITEM_EMPTY(hi))
3322 return NULL;
3323 }
3324 return HI2DI(hi);
3325}
3326
3327/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 * Get the script-local hashtab. NULL if not in a script context.
3329 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003330 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331get_script_local_ht(void)
3332{
3333 scid_T sid = current_sctx.sc_sid;
3334
Bram Moolenaare3d46852020-08-29 13:39:17 +02003335 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 return &SCRIPT_VARS(sid);
3337 return NULL;
3338}
3339
3340/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003341 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003342 * When "cmd" is TRUE it must look like a command, a function must be followed
3343 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003344 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003345 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003346 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003347lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003348 char_u *name,
3349 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003350 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003351 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352{
3353 hashtab_T *ht = get_script_local_ht();
3354 char_u buffer[30];
3355 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003356 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003358 int is_global = FALSE;
3359 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360
3361 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003362 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 if (len < sizeof(buffer) - 1)
3364 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003365 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 vim_strncpy(buffer, name, len);
3367 p = buffer;
3368 }
3369 else
3370 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003371 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003372 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003373 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 }
3375
3376 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003377 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378
3379 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003380 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003381 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003382 if (p != buffer)
3383 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003384
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003385 // Find a function, so that a following "->" works.
3386 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3387 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003388 if (res != OK)
3389 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003390 p = skipwhite(name + len);
3391
3392 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003393 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003394 // Do not check for an internal function, since it might also be a
3395 // valid command, such as ":split" versus "split()".
3396 // Skip "g:" before a function name.
3397 if (name[0] == 'g' && name[1] == ':')
3398 {
3399 is_global = TRUE;
3400 fname = name + 2;
3401 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003402 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003403 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003404 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003405 }
3406
Bram Moolenaar709664c2020-12-12 14:33:41 +01003407 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408}
3409
3410/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003411 * Find the hashtab used for a variable name.
3412 * Return NULL if the name is not valid.
3413 * Set "varname" to the start of name without ':'.
3414 */
3415 hashtab_T *
3416find_var_ht(char_u *name, char_u **varname)
3417{
3418 hashitem_T *hi;
3419 hashtab_T *ht;
3420
3421 if (name[0] == NUL)
3422 return NULL;
3423 if (name[1] != ':')
3424 {
3425 // The name must not start with a colon or #.
3426 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3427 return NULL;
3428 *varname = name;
3429
3430 // "version" is "v:version" in all scopes if scriptversion < 3.
3431 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003432 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003433 {
3434 hi = hash_find(&compat_hashtab, name);
3435 if (!HASHITEM_EMPTY(hi))
3436 return &compat_hashtab;
3437 }
3438
3439 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 if (ht != NULL)
3441 return ht; // local variable
3442
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003443 // In Vim9 script items at the script level are script-local, except
3444 // for autoload names.
3445 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 {
3447 ht = get_script_local_ht();
3448 if (ht != NULL)
3449 return ht;
3450 }
3451
3452 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003453 }
3454 *varname = name + 2;
3455 if (*name == 'g') // global variable
3456 return &globvarht;
3457 // There must be no ':' or '#' in the rest of the name, unless g: is used
3458 if (vim_strchr(name + 2, ':') != NULL
3459 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3460 return NULL;
3461 if (*name == 'b') // buffer variable
3462 return &curbuf->b_vars->dv_hashtab;
3463 if (*name == 'w') // window variable
3464 return &curwin->w_vars->dv_hashtab;
3465 if (*name == 't') // tab page variable
3466 return &curtab->tp_vars->dv_hashtab;
3467 if (*name == 'v') // v: variable
3468 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003469 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003470 && get_current_funccal()->fc_func->uf_def_status
3471 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003472 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003473 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 if (*name == 'a') // a: function argument
3475 return get_funccal_args_ht();
3476 if (*name == 'l') // l: local function variable
3477 return get_funccal_local_ht();
3478 }
3479 if (*name == 's') // script variable
3480 {
3481 ht = get_script_local_ht();
3482 if (ht != NULL)
3483 return ht;
3484 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003485 return NULL;
3486}
3487
3488/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003489 * Get the string value of a (global/local) variable.
3490 * Note: see tv_get_string() for how long the pointer remains valid.
3491 * Returns NULL when it doesn't exist.
3492 */
3493 char_u *
3494get_var_value(char_u *name)
3495{
3496 dictitem_T *v;
3497
3498 v = find_var(name, NULL, FALSE);
3499 if (v == NULL)
3500 return NULL;
3501 return tv_get_string(&v->di_tv);
3502}
3503
3504/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003505 * Allocate a new hashtab for a sourced script. It will be used while
3506 * sourcing this script and when executing functions defined in the script.
3507 */
3508 void
3509new_script_vars(scid_T id)
3510{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003511 scriptvar_T *sv;
3512
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003513 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3514 if (sv == NULL)
3515 return;
3516 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003517 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003518}
3519
3520/*
3521 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3522 * point to it.
3523 */
3524 void
3525init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3526{
3527 hash_init(&dict->dv_hashtab);
3528 dict->dv_lock = 0;
3529 dict->dv_scope = scope;
3530 dict->dv_refcount = DO_NOT_FREE_CNT;
3531 dict->dv_copyID = 0;
3532 dict_var->di_tv.vval.v_dict = dict;
3533 dict_var->di_tv.v_type = VAR_DICT;
3534 dict_var->di_tv.v_lock = VAR_FIXED;
3535 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3536 dict_var->di_key[0] = NUL;
3537}
3538
3539/*
3540 * Unreference a dictionary initialized by init_var_dict().
3541 */
3542 void
3543unref_var_dict(dict_T *dict)
3544{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003545 // Now the dict needs to be freed if no one else is using it, go back to
3546 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003547 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3548 dict_unref(dict);
3549}
3550
3551/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003552 * Clean up a list of internal variables.
3553 * Frees all allocated variables and the value they contain.
3554 * Clears hashtab "ht", does not free it.
3555 */
3556 void
3557vars_clear(hashtab_T *ht)
3558{
3559 vars_clear_ext(ht, TRUE);
3560}
3561
3562/*
3563 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3564 */
3565 void
3566vars_clear_ext(hashtab_T *ht, int free_val)
3567{
3568 int todo;
3569 hashitem_T *hi;
3570 dictitem_T *v;
3571
3572 hash_lock(ht);
3573 todo = (int)ht->ht_used;
3574 for (hi = ht->ht_array; todo > 0; ++hi)
3575 {
3576 if (!HASHITEM_EMPTY(hi))
3577 {
3578 --todo;
3579
3580 // Free the variable. Don't remove it from the hashtab,
3581 // ht_array might change then. hash_clear() takes care of it
3582 // later.
3583 v = HI2DI(hi);
3584 if (free_val)
3585 clear_tv(&v->di_tv);
3586 if (v->di_flags & DI_FLAGS_ALLOC)
3587 vim_free(v);
3588 }
3589 }
3590 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003591 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003592}
3593
3594/*
3595 * Delete a variable from hashtab "ht" at item "hi".
3596 * Clear the variable value and free the dictitem.
3597 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003598 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003599delete_var(hashtab_T *ht, hashitem_T *hi)
3600{
3601 dictitem_T *di = HI2DI(hi);
3602
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003603 if (hash_remove(ht, hi, "delete variable") != OK)
3604 return;
3605
3606 clear_tv(&di->di_tv);
3607 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003608}
3609
3610/*
3611 * List the value of one internal variable.
3612 */
3613 static void
3614list_one_var(dictitem_T *v, char *prefix, int *first)
3615{
3616 char_u *tofree;
3617 char_u *s;
3618 char_u numbuf[NUMBUFLEN];
3619
3620 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3621 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3622 s == NULL ? (char_u *)"" : s, first);
3623 vim_free(tofree);
3624}
3625
3626 static void
3627list_one_var_a(
3628 char *prefix,
3629 char_u *name,
3630 int type,
3631 char_u *string,
3632 int *first) // when TRUE clear rest of screen and set to FALSE
3633{
3634 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3635 msg_start();
3636 msg_puts(prefix);
3637 if (name != NULL) // "a:" vars don't have a name stored
3638 msg_puts((char *)name);
3639 msg_putchar(' ');
3640 msg_advance(22);
3641 if (type == VAR_NUMBER)
3642 msg_putchar('#');
3643 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3644 msg_putchar('*');
3645 else if (type == VAR_LIST)
3646 {
3647 msg_putchar('[');
3648 if (*string == '[')
3649 ++string;
3650 }
3651 else if (type == VAR_DICT)
3652 {
3653 msg_putchar('{');
3654 if (*string == '{')
3655 ++string;
3656 }
3657 else
3658 msg_putchar(' ');
3659
3660 msg_outtrans(string);
3661
3662 if (type == VAR_FUNC || type == VAR_PARTIAL)
3663 msg_puts("()");
3664 if (*first)
3665 {
3666 msg_clr_eos();
3667 *first = FALSE;
3668 }
3669}
3670
3671/*
3672 * Set variable "name" to value in "tv".
3673 * If the variable already exists, the value is updated.
3674 * Otherwise the variable is created.
3675 */
3676 void
3677set_var(
3678 char_u *name,
3679 typval_T *tv,
3680 int copy) // make copy of value in "tv"
3681{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003682 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003683}
3684
3685/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003686 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003687 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003688 * If the variable already exists and "is_const" is FALSE the value is updated.
3689 * Otherwise the variable is created.
3690 */
3691 void
3692set_var_const(
3693 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003694 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003695 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003696 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003697 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003698 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003699 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003700{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003701 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003702 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003703 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003705 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003706 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003707 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003708 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003710 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003711 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003712 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003713 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003714 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003715
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003716 if (sid != 0)
3717 {
3718 if (SCRIPT_ID_VALID(sid))
3719 ht = &SCRIPT_VARS(sid);
3720 varname = name;
3721 }
3722 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003723 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003724 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003725
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003726 if (in_vim9script() && is_export
3727 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3728 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003729 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003730 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003731 // In a vim9 autoload script an exported variable is put in the
3732 // global namespace with the autoload prefix.
3733 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003734 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003735 if (varname == NULL)
3736 goto failed;
3737 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003738 ht = &globvarht;
3739 }
3740 else
3741 ht = find_var_ht(name, &varname);
3742 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003743 if (ht == NULL || *varname == NUL)
3744 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003745 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003746 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003747 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003748 is_script_local = ht == get_script_local_ht() || sid != 0
3749 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003750
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003751 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003752 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003753 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003754 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003755 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003756 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003757 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003758 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003759 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003760 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003761 // Do not make g:var, w:var, b:var or t:var final.
3762 flags &= ~ASSIGN_FINAL;
3763
Bram Moolenaare535db82021-03-31 21:07:24 +02003764 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003765 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3766 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003767 // For "[a, _] = list" the underscore is ignored.
3768 if ((flags & ASSIGN_UNPACK) == 0)
3769 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003770 goto failed;
3771 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003772
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003773 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003774
Bram Moolenaar24e93162021-07-18 20:40:33 +02003775 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003776 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003777 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003778
Bram Moolenaar24e93162021-07-18 20:40:33 +02003779 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003780 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003781 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003782 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003784 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003785 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003787 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3788 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003789 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003790 if (!in_vim9script())
3791 {
3792 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3793 name);
3794 goto failed;
3795 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797
Bram Moolenaar993faa32022-02-21 15:59:11 +00003798 // Search in parent scope which is possible to reference from lambda
3799 if (di == NULL)
3800 di = find_var_in_scoped_ht(name, TRUE);
3801
3802 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3803 && var_wrong_func_name(name, di == NULL))
3804 goto failed;
3805
3806 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003807 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003808 // Destination is a bool and the value is not, but it can be
3809 // converted.
3810 CLEAR_FIELD(bool_tv);
3811 bool_tv.v_type = VAR_BOOL;
3812 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3813 tv = &bool_tv;
3814 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003815
Bram Moolenaar993faa32022-02-21 15:59:11 +00003816 if (di != NULL)
3817 {
3818 // Item already exists. Allowed to replace when reloading.
3819 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003820 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003821 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3822 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003823 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003824 emsg(_(e_cannot_modify_existing_variable));
3825 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003826 }
3827
Bram Moolenaar993faa32022-02-21 15:59:11 +00003828 if (is_script_local && vim9script
3829 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003830 {
3831 semsg(_(e_redefining_script_item_str), name);
3832 goto failed;
3833 }
3834
Bram Moolenaar993faa32022-02-21 15:59:11 +00003835 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003836 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003837 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003838 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003839
3840 if (sv != NULL)
3841 {
3842 // check the type and adjust to bool if needed
3843 where.wt_index = var_idx;
3844 where.wt_variable = TRUE;
3845 if (check_script_var_type(sv, tv, name, where) == FAIL)
3846 goto failed;
3847 if (type == NULL)
3848 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003849 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003850 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003851 }
3852
Bram Moolenaar993faa32022-02-21 15:59:11 +00003853 if ((flags & ASSIGN_FOR_LOOP) == 0
3854 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003855 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003856 }
3857 else
3858 {
3859 // can only redefine once
3860 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003861
Bram Moolenaar993faa32022-02-21 15:59:11 +00003862 // A Vim9 script-local variable is also present in sn_all_vars
3863 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003864 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003865 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003866 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003867 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003868 }
3869
Bram Moolenaar993faa32022-02-21 15:59:11 +00003870 // existing variable, need to clear the value
3871
3872 // Handle setting internal di: variables separately where needed to
3873 // prevent changing the type.
3874 if (ht == &vimvarht)
3875 {
3876 if (di->di_tv.v_type == VAR_STRING)
3877 {
3878 VIM_CLEAR(di->di_tv.vval.v_string);
3879 if (copy || tv->v_type != VAR_STRING)
3880 {
3881 char_u *val = tv_get_string(tv);
3882
3883 // Careful: when assigning to v:errmsg and
3884 // tv_get_string() causes an error message the variable
3885 // will already be set.
3886 if (di->di_tv.vval.v_string == NULL)
3887 di->di_tv.vval.v_string = vim_strsave(val);
3888 }
3889 else
3890 {
3891 // Take over the string to avoid an extra alloc/free.
3892 di->di_tv.vval.v_string = tv->vval.v_string;
3893 tv->vval.v_string = NULL;
3894 }
3895 goto failed;
3896 }
3897 else if (di->di_tv.v_type == VAR_NUMBER)
3898 {
3899 di->di_tv.vval.v_number = tv_get_number(tv);
3900 if (STRCMP(varname, "searchforward") == 0)
3901 set_search_direction(di->di_tv.vval.v_number
3902 ? '/' : '?');
3903#ifdef FEAT_SEARCH_EXTRA
3904 else if (STRCMP(varname, "hlsearch") == 0)
3905 {
3906 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003907 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003908 }
3909#endif
3910 goto failed;
3911 }
3912 else if (di->di_tv.v_type != tv->v_type)
3913 {
3914 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3915 goto failed;
3916 }
3917 }
3918
3919 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01003920
3921 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
3922 && SCRIPT_ID_VALID(current_sctx.sc_sid))
3923 {
3924 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3925
3926 update_script_var_block_id(name, si->sn_current_block_id);
3927 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003928 }
3929 else
3930 {
3931 // Item not found, check if a function already exists.
3932 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3933 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3934 {
3935 semsg(_(e_redefining_script_item_str), name);
3936 goto failed;
3937 }
3938
3939 // add a new variable
3940 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3941 {
3942 semsg(_(e_unknown_variable_str), name);
3943 goto failed;
3944 }
3945
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003946 if (check_hashtab_frozen(ht, "add variable"))
3947 goto failed;
3948
Bram Moolenaar993faa32022-02-21 15:59:11 +00003949 // Can't add "v:" or "a:" variable.
3950 if (ht == &vimvarht || ht == get_funccal_args_ht())
3951 {
3952 semsg(_(e_illegal_variable_name_str), name);
3953 goto failed;
3954 }
3955
3956 // Make sure the variable name is valid. In Vim9 script an
3957 // autoload variable must be prefixed with "g:" unless in an
3958 // autoload script.
3959 if (!valid_varname(varname, -1, !vim9script
3960 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3961 goto failed;
3962
3963 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3964 if (di == NULL)
3965 goto failed;
3966 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003967 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003968 {
3969 vim_free(di);
3970 goto failed;
3971 }
3972 di->di_flags = DI_FLAGS_ALLOC;
3973 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3974 di->di_flags |= DI_FLAGS_LOCK;
3975
3976 // A Vim9 script-local variable is also added to sn_all_vars and
3977 // sn_var_vals. It may set "type" from "tv".
3978 if (var_in_vim9script || var_in_autoload)
3979 update_vim9_script_var(TRUE, di,
3980 var_in_autoload ? name : di->di_key, flags,
3981 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003982 }
3983
Bram Moolenaar993faa32022-02-21 15:59:11 +00003984 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003985 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003986 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003987 else
3988 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003989 *dest_tv = *tv;
3990 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003991 init_tv(tv);
3992 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003993 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003994
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003995 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003996 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003997
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003998 // ":const var = value" locks the value
3999 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004000 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004001 // Like :lockvar! name: lock the value and what it contains, but only
4002 // if the reference count is up to one. That locks only literal
4003 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004004 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004005
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004006failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004007 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004008 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004009 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004010}
4011
4012/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004013 * Check in this order for backwards compatibility:
4014 * - Whether the variable is read-only
4015 * - Whether the variable value is locked
4016 * - Whether the variable is locked
4017 */
4018 int
4019var_check_permission(dictitem_T *di, char_u *name)
4020{
4021 if (var_check_ro(di->di_flags, name, FALSE)
4022 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4023 || var_check_lock(di->di_flags, name, FALSE))
4024 return FAIL;
4025 return OK;
4026}
4027
4028/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004029 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4030 * Also give an error message.
4031 */
4032 int
4033var_check_ro(int flags, char_u *name, int use_gettext)
4034{
4035 if (flags & DI_FLAGS_RO)
4036 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004037 if (name == NULL)
4038 emsg(_(e_cannot_change_readonly_variable));
4039 else
4040 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004041 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004042 return TRUE;
4043 }
4044 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4045 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004046 if (name == NULL)
4047 emsg(_(e_cannot_set_variable_in_sandbox));
4048 else
4049 semsg(_(e_cannot_set_variable_in_sandbox_str),
4050 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004051 return TRUE;
4052 }
4053 return FALSE;
4054}
4055
4056/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004057 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4058 * Also give an error message.
4059 */
4060 int
4061var_check_lock(int flags, char_u *name, int use_gettext)
4062{
4063 if (flags & DI_FLAGS_LOCK)
4064 {
4065 semsg(_(e_variable_is_locked_str),
4066 use_gettext ? (char_u *)_(name) : name);
4067 return TRUE;
4068 }
4069 return FALSE;
4070}
4071
4072/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004073 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4074 * Also give an error message.
4075 */
4076 int
4077var_check_fixed(int flags, char_u *name, int use_gettext)
4078{
4079 if (flags & DI_FLAGS_FIX)
4080 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004081 if (name == NULL)
4082 emsg(_(e_cannot_delete_variable));
4083 else
4084 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004085 use_gettext ? (char_u *)_(name) : name);
4086 return TRUE;
4087 }
4088 return FALSE;
4089}
4090
4091/*
4092 * Check if a funcref is assigned to a valid variable name.
4093 * Return TRUE and give an error if not.
4094 */
4095 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004096var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097 char_u *name, // points to start of variable name
4098 int new_var) // TRUE when creating the variable
4099{
Bram Moolenaar32154662021-03-28 21:14:06 +02004100 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4101 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004102 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004103 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4104 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004105 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004106 ? name[2] : name[0])
4107 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004108 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004109 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004110 return TRUE;
4111 }
4112 // Don't allow hiding a function. When "v" is not NULL we might be
4113 // assigning another function to the same var, the type is checked
4114 // below.
4115 if (new_var && function_exists(name, FALSE))
4116 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004117 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004118 name);
4119 return TRUE;
4120 }
4121 return FALSE;
4122}
4123
4124/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004125 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4126 * value. Also give an error message, using "name" or _("name") when
4127 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004128 */
4129 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004130value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004131{
4132 if (lock & VAR_LOCKED)
4133 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004134 if (name == NULL)
4135 emsg(_(e_value_is_locked));
4136 else
4137 semsg(_(e_value_is_locked_str),
4138 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004139 return TRUE;
4140 }
4141 if (lock & VAR_FIXED)
4142 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004143 if (name == NULL)
4144 emsg(_(e_cannot_change_value));
4145 else
4146 semsg(_(e_cannot_change_value_of_str),
4147 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004148 return TRUE;
4149 }
4150 return FALSE;
4151}
4152
4153/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004154 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004155 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004156 * Return FALSE and give an error if not.
4157 */
4158 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004159valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004160{
4161 char_u *p;
4162
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004163 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004164 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004165 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004166 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004167 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004168 return FALSE;
4169 }
4170 return TRUE;
4171}
4172
4173/*
LemonBoy47d4e312022-05-04 18:12:55 +01004174 * Implements the logic to retrieve local variable and option values.
4175 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004176 */
4177 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004178get_var_from(
4179 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004180 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004181 typval_T *deftv, // Default value if not found.
4182 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4183 tabpage_T *tp, // can be NULL
4184 win_T *win,
4185 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004186{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004187 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004188 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004189 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004190 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004191 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004192
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004193 ++emsg_off;
4194
4195 rettv->v_type = VAR_STRING;
4196 rettv->vval.v_string = NULL;
4197
LemonBoy47d4e312022-05-04 18:12:55 +01004198 if (varname != NULL && tp != NULL && win != NULL
4199 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004200 {
4201 // Set curwin to be our win, temporarily. Also set the tabpage,
4202 // otherwise the window is not valid. Only do this when needed,
4203 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004204 // If we have a buffer reference avoid the switching, we're saving and
4205 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004206 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004207 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004208 {
LemonBoy47d4e312022-05-04 18:12:55 +01004209 // Handle options. There are no tab-local options.
4210 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004211 {
LemonBoy47d4e312022-05-04 18:12:55 +01004212 buf_T *save_curbuf = curbuf;
4213
4214 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004215 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004216 curbuf = buf;
4217
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004218 if (varname[1] == NUL)
4219 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004220 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004221 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004222
4223 if (opts != NULL)
4224 {
4225 rettv_dict_set(rettv, opts);
4226 done = TRUE;
4227 }
4228 }
LemonBoy47d4e312022-05-04 18:12:55 +01004229 else if (eval_option(&varname, rettv, TRUE) == OK)
4230 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004231 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004232
4233 curbuf = save_curbuf;
4234 }
4235 else if (*varname == NUL)
4236 {
4237 // Empty string: return a dict with all the local variables.
4238 if (htname == 'b')
4239 v = &buf->b_bufvar;
4240 else if (htname == 'w')
4241 v = &win->w_winvar;
4242 else
4243 v = &tp->tp_winvar;
4244 copy_tv(&v->di_tv, rettv);
4245 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004246 }
4247 else
4248 {
LemonBoy47d4e312022-05-04 18:12:55 +01004249 hashtab_T *ht;
4250
4251 if (htname == 'b')
4252 ht = &buf->b_vars->dv_hashtab;
4253 else if (htname == 'w')
4254 ht = &win->w_vars->dv_hashtab;
4255 else
4256 ht = &tp->tp_vars->dv_hashtab;
4257
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004258 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004259 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004260 if (v != NULL)
4261 {
4262 copy_tv(&v->di_tv, rettv);
4263 done = TRUE;
4264 }
4265 }
4266 }
4267
4268 if (need_switch_win)
4269 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004270 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004271 }
4272
LemonBoy47d4e312022-05-04 18:12:55 +01004273 if (!done && deftv->v_type != VAR_UNKNOWN)
4274 // use the default value
4275 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004276
4277 --emsg_off;
4278}
4279
4280/*
LemonBoy47d4e312022-05-04 18:12:55 +01004281 * getwinvar() and gettabwinvar()
4282 */
4283 static void
4284getwinvar(
4285 typval_T *argvars,
4286 typval_T *rettv,
4287 int off) // 1 for gettabwinvar()
4288{
4289 char_u *varname;
4290 tabpage_T *tp;
4291 win_T *win;
4292
4293 if (off == 1)
4294 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4295 else
4296 tp = curtab;
4297 win = find_win_by_nr(&argvars[off], tp);
4298 varname = tv_get_string_chk(&argvars[off + 1]);
4299
4300 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4301}
4302
4303/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004304 * Set option "varname" to the value of "varp" for the current buffer/window.
4305 */
4306 static void
4307set_option_from_tv(char_u *varname, typval_T *varp)
4308{
4309 long numval = 0;
4310 char_u *strval;
4311 char_u nbuf[NUMBUFLEN];
4312 int error = FALSE;
4313
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004314 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004315 {
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004316 if (is_string_option(varname))
4317 {
4318 emsg(_(e_string_required));
4319 return;
4320 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004321 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004322 strval = (char_u *)"0"; // avoid using "false"
4323 }
4324 else
4325 {
4326 if (!in_vim9script() || varp->v_type != VAR_STRING)
4327 numval = (long)tv_get_number_chk(varp, &error);
4328 strval = tv_get_string_buf_chk(varp, nbuf);
4329 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004330 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004331 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004332}
4333
4334/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004335 * "setwinvar()" and "settabwinvar()" functions
4336 */
4337 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004338setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004339{
4340 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004341 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004342 int need_switch_win;
4343 char_u *varname, *winvarname;
4344 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004345 tabpage_T *tp = NULL;
4346
4347 if (check_secure())
4348 return;
4349
4350 if (off == 1)
4351 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4352 else
4353 tp = curtab;
4354 win = find_win_by_nr(&argvars[off], tp);
4355 varname = tv_get_string_chk(&argvars[off + 1]);
4356 varp = &argvars[off + 2];
4357
zeertzjqea720ae2023-01-03 10:54:09 +00004358 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004359 return;
4360
4361 need_switch_win = !(tp == curtab && win == curwin);
4362 if (!need_switch_win
4363 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004364 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004365 if (*varname == '&')
4366 set_option_from_tv(varname + 1, varp);
4367 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004368 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004369 winvarname = alloc(STRLEN(varname) + 3);
4370 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004371 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004372 STRCPY(winvarname, "w:");
4373 STRCPY(winvarname + 2, varname);
4374 set_var(winvarname, varp, TRUE);
4375 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004376 }
4377 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004378 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004379 if (need_switch_win)
4380 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004381}
4382
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004383/*
4384 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4385 * v:option_type, and v:option_command.
4386 */
4387 void
4388reset_v_option_vars(void)
4389{
4390 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4391 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4392 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4393 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4394 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4395 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4396}
4397
4398/*
4399 * Add an assert error to v:errors.
4400 */
4401 void
4402assert_error(garray_T *gap)
4403{
4404 struct vimvar *vp = &vimvars[VV_ERRORS];
4405
Bram Moolenaard787e402021-12-24 21:36:12 +00004406 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004407 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004408 set_vim_var_list(VV_ERRORS, list_alloc());
4409 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4410}
4411
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004412 int
4413var_exists(char_u *var)
4414{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004415 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004416 char_u *name;
4417 char_u *tofree;
4418 typval_T tv;
4419 int len = 0;
4420 int n = FALSE;
4421
4422 // get_name_len() takes care of expanding curly braces
4423 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004424 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004425 if (len > 0)
4426 {
4427 if (tofree != NULL)
4428 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004429 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004430 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004431 if (n)
4432 {
4433 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004434 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004435 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4436 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004437 if (n)
4438 clear_tv(&tv);
4439 }
4440 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004441 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004442 n = FALSE;
4443
4444 vim_free(tofree);
4445 return n;
4446}
4447
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004448static lval_T *redir_lval = NULL;
4449#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4450static garray_T redir_ga; // only valid when redir_lval is not NULL
4451static char_u *redir_endp = NULL;
4452static char_u *redir_varname = NULL;
4453
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004454 int
4455alloc_redir_lval(void)
4456{
4457 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4458 if (redir_lval == NULL)
4459 return FAIL;
4460 return OK;
4461}
4462
4463 void
4464clear_redir_lval(void)
4465{
4466 VIM_CLEAR(redir_lval);
4467}
4468
4469 void
4470init_redir_ga(void)
4471{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004472 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004473}
4474
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004475/*
4476 * Start recording command output to a variable
4477 * When "append" is TRUE append to an existing variable.
4478 * Returns OK if successfully completed the setup. FAIL otherwise.
4479 */
4480 int
4481var_redir_start(char_u *name, int append)
4482{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004483 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004484 typval_T tv;
4485
4486 // Catch a bad name early.
4487 if (!eval_isnamec1(*name))
4488 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004489 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004490 return FAIL;
4491 }
4492
4493 // Make a copy of the name, it is used in redir_lval until redir ends.
4494 redir_varname = vim_strsave(name);
4495 if (redir_varname == NULL)
4496 return FAIL;
4497
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004498 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004499 {
4500 var_redir_stop();
4501 return FAIL;
4502 }
4503
4504 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004505 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004506
4507 // Parse the variable name (can be a dict or list entry).
4508 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4509 FNE_CHECK_START);
4510 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4511 {
4512 clear_lval(redir_lval);
4513 if (redir_endp != NULL && *redir_endp != NUL)
4514 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004515 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004516 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004517 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004518 redir_endp = NULL; // don't store a value, only cleanup
4519 var_redir_stop();
4520 return FAIL;
4521 }
4522
4523 // check if we can write to the variable: set it to or append an empty
4524 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004525 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004526 tv.v_type = VAR_STRING;
4527 tv.vval.v_string = (char_u *)"";
4528 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004529 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004530 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004531 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004532 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004533 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004534 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004535 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004536 {
4537 redir_endp = NULL; // don't store a value, only cleanup
4538 var_redir_stop();
4539 return FAIL;
4540 }
4541
4542 return OK;
4543}
4544
4545/*
4546 * Append "value[value_len]" to the variable set by var_redir_start().
4547 * The actual appending is postponed until redirection ends, because the value
4548 * appended may in fact be the string we write to, changing it may cause freed
4549 * memory to be used:
4550 * :redir => foo
4551 * :let foo
4552 * :redir END
4553 */
4554 void
4555var_redir_str(char_u *value, int value_len)
4556{
4557 int len;
4558
4559 if (redir_lval == NULL)
4560 return;
4561
4562 if (value_len == -1)
4563 len = (int)STRLEN(value); // Append the entire string
4564 else
4565 len = value_len; // Append only "value_len" characters
4566
4567 if (ga_grow(&redir_ga, len) == OK)
4568 {
4569 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4570 redir_ga.ga_len += len;
4571 }
4572 else
4573 var_redir_stop();
4574}
4575
4576/*
4577 * Stop redirecting command output to a variable.
4578 * Frees the allocated memory.
4579 */
4580 void
4581var_redir_stop(void)
4582{
4583 typval_T tv;
4584
4585 if (EVALCMD_BUSY)
4586 {
4587 redir_lval = NULL;
4588 return;
4589 }
4590
4591 if (redir_lval != NULL)
4592 {
4593 // If there was no error: assign the text to the variable.
4594 if (redir_endp != NULL)
4595 {
4596 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4597 tv.v_type = VAR_STRING;
4598 tv.vval.v_string = redir_ga.ga_data;
4599 // Call get_lval() again, if it's inside a Dict or List it may
4600 // have changed.
4601 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4602 FALSE, FALSE, 0, FNE_CHECK_START);
4603 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004605 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004606 clear_lval(redir_lval);
4607 }
4608
4609 // free the collected output
4610 VIM_CLEAR(redir_ga.ga_data);
4611
4612 VIM_CLEAR(redir_lval);
4613 }
4614 VIM_CLEAR(redir_varname);
4615}
4616
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004617/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004618 * Get the collected redirected text and clear redir_ga.
4619 */
4620 char_u *
4621get_clear_redir_ga(void)
4622{
4623 char_u *res;
4624
4625 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4626 res = redir_ga.ga_data;
4627 redir_ga.ga_data = NULL;
4628 return res;
4629}
4630
4631/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004632 * "gettabvar()" function
4633 */
4634 void
4635f_gettabvar(typval_T *argvars, typval_T *rettv)
4636{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004637 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004638 tabpage_T *tp;
4639 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004640
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004641 if (in_vim9script()
4642 && (check_for_number_arg(argvars, 0) == FAIL
4643 || check_for_string_arg(argvars, 1) == FAIL))
4644 return;
4645
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004646 varname = tv_get_string_chk(&argvars[1]);
4647 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004648 if (tp != NULL)
4649 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4650 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004651
LemonBoy47d4e312022-05-04 18:12:55 +01004652 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004653}
4654
4655/*
4656 * "gettabwinvar()" function
4657 */
4658 void
4659f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4660{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004661 if (in_vim9script()
4662 && (check_for_number_arg(argvars, 0) == FAIL
4663 || check_for_number_arg(argvars, 1) == FAIL
4664 || check_for_string_arg(argvars, 2) == FAIL))
4665 return;
4666
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004667 getwinvar(argvars, rettv, 1);
4668}
4669
4670/*
4671 * "getwinvar()" function
4672 */
4673 void
4674f_getwinvar(typval_T *argvars, typval_T *rettv)
4675{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004676 if (in_vim9script()
4677 && (check_for_number_arg(argvars, 0) == FAIL
4678 || check_for_string_arg(argvars, 1) == FAIL))
4679 return;
4680
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004681 getwinvar(argvars, rettv, 0);
4682}
4683
4684/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004685 * "getbufvar()" function
4686 */
4687 void
4688f_getbufvar(typval_T *argvars, typval_T *rettv)
4689{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004690 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004691 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004692
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004693 if (in_vim9script()
4694 && (check_for_buffer_arg(argvars, 0) == FAIL
4695 || check_for_string_arg(argvars, 1) == FAIL))
4696 return;
4697
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004698 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004699 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004700
LemonBoy47d4e312022-05-04 18:12:55 +01004701 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004702}
4703
4704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004705 * "settabvar()" function
4706 */
4707 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004708f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004709{
4710 tabpage_T *save_curtab;
4711 tabpage_T *tp;
4712 char_u *varname, *tabvarname;
4713 typval_T *varp;
4714
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004715 if (check_secure())
4716 return;
4717
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004718 if (in_vim9script()
4719 && (check_for_number_arg(argvars, 0) == FAIL
4720 || check_for_string_arg(argvars, 1) == FAIL))
4721 return;
4722
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004723 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4724 varname = tv_get_string_chk(&argvars[1]);
4725 varp = &argvars[2];
4726
zeertzjqea720ae2023-01-03 10:54:09 +00004727 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004728 return;
4729
4730 save_curtab = curtab;
4731 goto_tabpage_tp(tp, FALSE, FALSE);
4732
4733 tabvarname = alloc(STRLEN(varname) + 3);
4734 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004735 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004736 STRCPY(tabvarname, "t:");
4737 STRCPY(tabvarname + 2, varname);
4738 set_var(tabvarname, varp, TRUE);
4739 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004740 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004741
4742 // Restore current tabpage
4743 if (valid_tabpage(save_curtab))
4744 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004745}
4746
4747/*
4748 * "settabwinvar()" function
4749 */
4750 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004751f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004752{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004753 if (in_vim9script()
4754 && (check_for_number_arg(argvars, 0) == FAIL
4755 || check_for_number_arg(argvars, 1) == FAIL
4756 || check_for_string_arg(argvars, 2) == FAIL))
4757 return;
4758
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004759 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004760}
4761
4762/*
4763 * "setwinvar()" function
4764 */
4765 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004766f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004767{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004768 if (in_vim9script()
4769 && (check_for_number_arg(argvars, 0) == FAIL
4770 || check_for_string_arg(argvars, 1) == FAIL))
4771 return;
4772
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004773 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004774}
4775
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004776/*
4777 * "setbufvar()" function
4778 */
4779 void
4780f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4781{
4782 buf_T *buf;
4783 char_u *varname, *bufvarname;
4784 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004785
4786 if (check_secure())
4787 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004788
4789 if (in_vim9script()
4790 && (check_for_buffer_arg(argvars, 0) == FAIL
4791 || check_for_string_arg(argvars, 1) == FAIL))
4792 return;
4793
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004794 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004795 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004796 varp = &argvars[2];
4797
zeertzjqea720ae2023-01-03 10:54:09 +00004798 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004799 return;
4800
4801 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004802 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004803 aco_save_T aco;
4804
4805 // Set curbuf to be our buf, temporarily.
4806 aucmd_prepbuf(&aco, buf);
4807 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004808 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004809 // Only when it worked to set "curbuf".
4810 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004811
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004812 // reset notion of buffer
4813 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004814 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004815 }
4816 else
4817 {
4818 bufvarname = alloc(STRLEN(varname) + 3);
4819 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004820 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004821 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004822
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004823 curbuf = buf;
4824 STRCPY(bufvarname, "b:");
4825 STRCPY(bufvarname + 2, varname);
4826 set_var(bufvarname, varp, TRUE);
4827 vim_free(bufvarname);
4828 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004829 }
4830 }
4831}
4832
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004833/*
4834 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004835 * When "arg" is zero "res.cb_name" is set to an empty string.
4836 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4837 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004838 */
4839 callback_T
4840get_callback(typval_T *arg)
4841{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004842 callback_T res;
4843 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004844
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004845 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004846 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4847 {
4848 res.cb_partial = arg->vval.v_partial;
4849 ++res.cb_partial->pt_refcount;
4850 res.cb_name = partial_name(res.cb_partial);
4851 }
4852 else
4853 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01004854 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4855 && isdigit(*arg->vval.v_string))
4856 r = FAIL;
4857 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004858 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004859 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004860 if (arg->v_type == VAR_STRING)
4861 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004862 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004863 if (name != NULL)
4864 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004865 res.cb_name = name;
4866 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004867 }
4868 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004869 func_ref(res.cb_name);
4870 }
4871 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004872 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004873 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004874 r = FAIL;
4875
4876 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004877 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004878 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004879 res.cb_name = NULL;
4880 }
4881 }
4882 return res;
4883}
4884
4885/*
4886 * Copy a callback into a typval_T.
4887 */
4888 void
4889put_callback(callback_T *cb, typval_T *tv)
4890{
4891 if (cb->cb_partial != NULL)
4892 {
4893 tv->v_type = VAR_PARTIAL;
4894 tv->vval.v_partial = cb->cb_partial;
4895 ++tv->vval.v_partial->pt_refcount;
4896 }
4897 else
4898 {
4899 tv->v_type = VAR_FUNC;
4900 tv->vval.v_string = vim_strsave(cb->cb_name);
4901 func_ref(cb->cb_name);
4902 }
4903}
4904
4905/*
4906 * Make a copy of "src" into "dest", allocating the function name if needed,
4907 * without incrementing the refcount.
4908 */
4909 void
4910set_callback(callback_T *dest, callback_T *src)
4911{
4912 if (src->cb_partial == NULL)
4913 {
4914 // just a function name, make a copy
4915 dest->cb_name = vim_strsave(src->cb_name);
4916 dest->cb_free_name = TRUE;
4917 }
4918 else
4919 {
4920 // cb_name is a pointer into cb_partial
4921 dest->cb_name = src->cb_name;
4922 dest->cb_free_name = FALSE;
4923 }
4924 dest->cb_partial = src->cb_partial;
4925}
4926
4927/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004928 * Copy callback from "src" to "dest", incrementing the refcounts.
4929 */
4930 void
4931copy_callback(callback_T *dest, callback_T *src)
4932{
4933 dest->cb_partial = src->cb_partial;
4934 if (dest->cb_partial != NULL)
4935 {
4936 dest->cb_name = src->cb_name;
4937 dest->cb_free_name = FALSE;
4938 ++dest->cb_partial->pt_refcount;
4939 }
4940 else
4941 {
4942 dest->cb_name = vim_strsave(src->cb_name);
4943 dest->cb_free_name = TRUE;
4944 func_ref(src->cb_name);
4945 }
4946}
4947
4948/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004949 * When a callback refers to an autoload import, change the function name to
4950 * the "path#name" form. Uses the current script context.
4951 * Only works when the name is allocated.
4952 */
4953 void
4954expand_autload_callback(callback_T *cb)
4955{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004956 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004957 char_u *p;
4958 imported_T *import;
4959
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004960 if (!in_vim9script() || cb->cb_name == NULL
4961 || (!cb->cb_free_name
4962 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004963 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004964 if (cb->cb_partial != NULL)
4965 name = cb->cb_partial->pt_name;
4966 else
4967 name = cb->cb_name;
4968 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004969 if (p == NULL)
4970 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004971
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004972 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004973 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
4974 return;
4975
4976 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4977 if (si->sn_autoload_prefix == NULL)
4978 return;
4979
4980 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
4981 if (newname == NULL)
4982 return;
4983
4984 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004985 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004986 if (cb->cb_name == cb->cb_partial->pt_name)
4987 cb->cb_name = newname;
4988 vim_free(cb->cb_partial->pt_name);
4989 cb->cb_partial->pt_name = newname;
4990 }
4991 else
4992 {
4993 vim_free(cb->cb_name);
4994 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004995 }
4996}
4997
4998/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004999 * Unref/free "callback" returned by get_callback() or set_callback().
5000 */
5001 void
5002free_callback(callback_T *callback)
5003{
5004 if (callback->cb_partial != NULL)
5005 {
5006 partial_unref(callback->cb_partial);
5007 callback->cb_partial = NULL;
5008 }
5009 else if (callback->cb_name != NULL)
5010 func_unref(callback->cb_name);
5011 if (callback->cb_free_name)
5012 {
5013 vim_free(callback->cb_name);
5014 callback->cb_free_name = FALSE;
5015 }
5016 callback->cb_name = NULL;
5017}
5018
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005019#endif // FEAT_EVAL