blob: cb31966bd22f8eca9c93df72fb125fb6c089dbec [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000142 {VV_NAME("t_class", VAR_NUMBER), NULL, VV_RO},
143 {VV_NAME("t_object", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaard787e402021-12-24 21:36:12 +0000144 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
148 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
149 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
150 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
151 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
152 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
153 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
154 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
155 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
156 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
157 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
158 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000159 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200160};
161
162// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000163#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vv_nr vv_di.di_tv.vval.v_number
165#define vv_float vv_di.di_tv.vval.v_float
166#define vv_str vv_di.di_tv.vval.v_string
167#define vv_list vv_di.di_tv.vval.v_list
168#define vv_dict vv_di.di_tv.vval.v_dict
169#define vv_blob vv_di.di_tv.vval.v_blob
170#define vv_tv vv_di.di_tv
171
172static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200173static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200174#define vimvarht vimvardict.dv_hashtab
175
176// for VIM_VERSION_ defines
177#include "version.h"
178
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200179static void list_glob_vars(int *first);
180static void list_buf_vars(int *first);
181static void list_win_vars(int *first);
182static void list_tab_vars(int *first);
183static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100184static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200185static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
186static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200187static void list_one_var(dictitem_T *v, char *prefix, int *first);
188static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
189
190/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200191 * Initialize global and vim special variables
192 */
193 void
194evalvars_init(void)
195{
196 int i;
197 struct vimvar *p;
198
199 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
200 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
201 vimvardict.dv_lock = VAR_FIXED;
202 hash_init(&compat_hashtab);
203
204 for (i = 0; i < VV_LEN; ++i)
205 {
206 p = &vimvars[i];
207 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
208 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100209 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200210 getout(1);
211 }
212 STRCPY(p->vv_di.di_key, p->vv_name);
213 if (p->vv_flags & VV_RO)
214 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
215 else if (p->vv_flags & VV_RO_SBX)
216 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
217 else
218 p->vv_di.di_flags = DI_FLAGS_FIX;
219
220 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000221 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000222 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 if (p->vv_flags & VV_COMPAT)
224 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000225 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200226 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200227 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
228 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200229
230 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
231 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100232 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
234 set_vim_var_list(VV_ERRORS, list_alloc());
235 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
236
237 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
238 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
239 set_vim_var_nr(VV_NONE, VVAL_NONE);
240 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100241 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
242 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100243 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000244 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
245 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
246 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000247 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200248
249 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
250 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
251 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
252 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
253 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
254 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
255 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
256 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
257 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
258 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
259 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000260 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
261 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200262
263 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
264
Drew Vogele30d1022021-10-24 20:35:07 +0100265 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
266
Bram Moolenaar439c0362020-06-06 15:58:03 +0200267 // Default for v:register is not 0 but '"'. This is adjusted once the
268 // clipboard has been setup by calling reset_reg_var().
269 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200270}
271
272#if defined(EXITFREE) || defined(PROTO)
273/*
274 * Free all vim variables information on exit
275 */
276 void
277evalvars_clear(void)
278{
279 int i;
280 struct vimvar *p;
281
282 for (i = 0; i < VV_LEN; ++i)
283 {
284 p = &vimvars[i];
285 if (p->vv_di.di_tv.v_type == VAR_STRING)
286 VIM_CLEAR(p->vv_str);
287 else if (p->vv_di.di_tv.v_type == VAR_LIST)
288 {
289 list_unref(p->vv_list);
290 p->vv_list = NULL;
291 }
292 }
293 hash_clear(&vimvarht);
294 hash_init(&vimvarht); // garbage_collect() will access it
295 hash_clear(&compat_hashtab);
296
297 // global variables
298 vars_clear(&globvarht);
299
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100300 // Script-local variables. Clear all the variables here.
301 // The scriptvar_T is cleared later in free_scriptnames(), because a
302 // variable in one script might hold a reference to the whole scope of
303 // another script.
304 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200305 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200306}
307#endif
308
309 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200310garbage_collect_globvars(int copyID)
311{
312 return set_ref_in_ht(&globvarht, copyID, NULL);
313}
314
315 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200316garbage_collect_vimvars(int copyID)
317{
318 return set_ref_in_ht(&vimvarht, copyID, NULL);
319}
320
321 int
322garbage_collect_scriptvars(int copyID)
323{
Bram Moolenaared234f22020-10-15 20:42:20 +0200324 int i;
325 int idx;
326 int abort = FALSE;
327 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200328
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100329 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200330 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200331 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
332
Bram Moolenaared234f22020-10-15 20:42:20 +0200333 si = SCRIPT_ITEM(i);
334 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
335 {
336 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
337
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100338 if (sv->sv_name != NULL)
339 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200340 }
341 }
342
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200343 return abort;
344}
345
346/*
347 * Set an internal variable to a string value. Creates the variable if it does
348 * not already exist.
349 */
350 void
351set_internal_string_var(char_u *name, char_u *value)
352{
353 char_u *val;
354 typval_T *tvp;
355
356 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000357 if (val == NULL)
358 return;
359
360 tvp = alloc_string_tv(val);
361 if (tvp == NULL)
362 return;
363
364 set_var(name, tvp, FALSE);
365 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200366}
367
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200368 int
369eval_charconvert(
370 char_u *enc_from,
371 char_u *enc_to,
372 char_u *fname_from,
373 char_u *fname_to)
374{
375 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000376 sctx_T saved_sctx = current_sctx;
377 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200378
379 set_vim_var_string(VV_CC_FROM, enc_from, -1);
380 set_vim_var_string(VV_CC_TO, enc_to, -1);
381 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
382 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000383 ctx = get_option_sctx("charconvert");
384 if (ctx != NULL)
385 current_sctx = *ctx;
386
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100387 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200388 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000389
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200390 set_vim_var_string(VV_CC_FROM, NULL, -1);
391 set_vim_var_string(VV_CC_TO, NULL, -1);
392 set_vim_var_string(VV_FNAME_IN, NULL, -1);
393 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000394 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200395
396 if (err)
397 return FAIL;
398 return OK;
399}
400
401# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
402 int
403eval_printexpr(char_u *fname, char_u *args)
404{
405 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000406 sctx_T saved_sctx = current_sctx;
407 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200408
409 set_vim_var_string(VV_FNAME_IN, fname, -1);
410 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000411 ctx = get_option_sctx("printexpr");
412 if (ctx != NULL)
413 current_sctx = *ctx;
414
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100415 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200416 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000417
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200418 set_vim_var_string(VV_FNAME_IN, NULL, -1);
419 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000420 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200421
422 if (err)
423 {
424 mch_remove(fname);
425 return FAIL;
426 }
427 return OK;
428}
429# endif
430
431# if defined(FEAT_DIFF) || defined(PROTO)
432 void
433eval_diff(
434 char_u *origfile,
435 char_u *newfile,
436 char_u *outfile)
437{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000438 sctx_T saved_sctx = current_sctx;
439 sctx_T *ctx;
440 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200441
442 set_vim_var_string(VV_FNAME_IN, origfile, -1);
443 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
444 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000445
446 ctx = get_option_sctx("diffexpr");
447 if (ctx != NULL)
448 current_sctx = *ctx;
449
450 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100451 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000452 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000453
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454 set_vim_var_string(VV_FNAME_IN, NULL, -1);
455 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
456 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000457 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200458}
459
460 void
461eval_patch(
462 char_u *origfile,
463 char_u *difffile,
464 char_u *outfile)
465{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000466 sctx_T saved_sctx = current_sctx;
467 sctx_T *ctx;
468 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200469
470 set_vim_var_string(VV_FNAME_IN, origfile, -1);
471 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
472 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000473
474 ctx = get_option_sctx("patchexpr");
475 if (ctx != NULL)
476 current_sctx = *ctx;
477
478 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100479 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000480 free_tv(tv);
481
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482 set_vim_var_string(VV_FNAME_IN, NULL, -1);
483 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
484 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000485 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200486}
487# endif
488
489#if defined(FEAT_SPELL) || defined(PROTO)
490/*
491 * Evaluate an expression to a list with suggestions.
492 * For the "expr:" part of 'spellsuggest'.
493 * Returns NULL when there is an error.
494 */
495 list_T *
496eval_spell_expr(char_u *badword, char_u *expr)
497{
498 typval_T save_val;
499 typval_T rettv;
500 list_T *list = NULL;
501 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000502 sctx_T saved_sctx = current_sctx;
503 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100504 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200505
506 // Set "v:val" to the bad word.
507 prepare_vimvar(VV_VAL, &save_val);
508 set_vim_var_string(VV_VAL, badword, -1);
509 if (p_verbose == 0)
510 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000511 ctx = get_option_sctx("spellsuggest");
512 if (ctx != NULL)
513 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200514
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100515 r = may_call_simple_func(p, &rettv);
516 if (r == NOTDONE)
517 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
518 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200519 {
520 if (rettv.v_type != VAR_LIST)
521 clear_tv(&rettv);
522 else
523 list = rettv.vval.v_list;
524 }
525
526 if (p_verbose == 0)
527 --emsg_off;
528 clear_tv(get_vim_var_tv(VV_VAL));
529 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000530 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200531
532 return list;
533}
534
535/*
536 * "list" is supposed to contain two items: a word and a number. Return the
537 * word in "pp" and the number as the return value.
538 * Return -1 if anything isn't right.
539 * Used to get the good word and score from the eval_spell_expr() result.
540 */
541 int
542get_spellword(list_T *list, char_u **pp)
543{
544 listitem_T *li;
545
546 li = list->lv_first;
547 if (li == NULL)
548 return -1;
549 *pp = tv_get_string(&li->li_tv);
550
551 li = li->li_next;
552 if (li == NULL)
553 return -1;
554 return (int)tv_get_number(&li->li_tv);
555}
556#endif
557
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200558/*
559 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200560 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200561 * When not used yet add the variable to the v: hashtable.
562 */
563 void
564prepare_vimvar(int idx, typval_T *save_tv)
565{
566 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200567 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000568 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000569 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200570}
571
572/*
573 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200574 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200575 * When no longer defined, remove the variable from the v: hashtable.
576 */
577 void
578restore_vimvar(int idx, typval_T *save_tv)
579{
580 hashitem_T *hi;
581
582 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000583 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
584 return;
585
586 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
587 if (HASHITEM_EMPTY(hi))
588 internal_error("restore_vimvar()");
589 else
590 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200591}
592
593/*
594 * List Vim variables.
595 */
596 static void
597list_vim_vars(int *first)
598{
599 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
600}
601
602/*
603 * List script-local variables, if there is a script.
604 */
605 static void
606list_script_vars(int *first)
607{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200608 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200609 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
610 "s:", FALSE, first);
611}
612
613/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100614 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
615 * But only when an identifier character follows.
616 */
617 int
618is_scoped_variable(char_u *name)
619{
620 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
621 && name[1] == ':'
622 && eval_isnamec(name[2]);
623}
624
625/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100626 * Evaluate one Vim expression {expr} in string "p" and append the
627 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100628 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100629 * Return a pointer to the character after "}", NULL for an error.
630 */
631 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100632eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100633{
634 char_u *block_start = skipwhite(p + 1); // skip the opening {
635 char_u *block_end = block_start;
636 char_u *expr_val;
637
638 if (*block_start == NUL)
639 {
640 semsg(_(e_missing_close_curly_str), p);
641 return NULL;
642 }
643 if (skip_expr(&block_end, NULL) == FAIL)
644 return NULL;
645 block_end = skipwhite(block_end);
646 if (*block_end != '}')
647 {
648 semsg(_(e_missing_close_curly_str), p);
649 return NULL;
650 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100651 if (evaluate)
652 {
653 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100654 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100655 *block_end = '}';
656 if (expr_val == NULL)
657 return NULL;
658 ga_concat(gap, expr_val);
659 vim_free(expr_val);
660 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100661
662 return block_end + 1;
663}
664
665/*
666 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
667 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
668 * Used for a heredoc assignment.
669 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100670 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100671 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100672eval_all_expr_in_str(char_u *str)
673{
674 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100675 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100676
677 ga_init2(&ga, 1, 80);
678 p = str;
679
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100680 while (*p != NUL)
681 {
LemonBoy2eaef102022-05-06 13:14:50 +0100682 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100683 int escaped_brace = FALSE;
684
685 // Look for a block start.
686 lit_start = p;
687 while (*p != '{' && *p != '}' && *p != NUL)
688 ++p;
689
690 if (*p != NUL && *p == p[1])
691 {
692 // Escaped brace, unescape and continue.
693 // Include the brace in the literal string.
694 ++p;
695 escaped_brace = TRUE;
696 }
697 else if (*p == '}')
698 {
699 semsg(_(e_stray_closing_curly_str), str);
700 ga_clear(&ga);
701 return NULL;
702 }
703
704 // Append the literal part.
705 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
706
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100707 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100708 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100709
LemonBoy2eaef102022-05-06 13:14:50 +0100710 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100711 {
LemonBoy2eaef102022-05-06 13:14:50 +0100712 // Skip the second brace.
713 ++p;
714 continue;
715 }
716
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100717 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100718 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100719 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100720 {
721 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722 return NULL;
723 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100724 }
725 ga_append(&ga, NUL);
726
727 return ga.ga_data;
728}
729
730/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200731 * Get a list of lines from a HERE document. The here document is a list of
732 * lines surrounded by a marker.
733 * cmd << {marker}
734 * {line1}
735 * {line2}
736 * ....
737 * {marker}
738 *
739 * The {marker} is a string. If the optional 'trim' word is supplied before the
740 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100741 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200742 *
743 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100744 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200745 * missing, then '.' is accepted as a marker.
746 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100747 * When compiling a heredoc assignment to a variable in a Vim9 def function,
748 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
749 * string values from the heredoc, vim9 instructions are generated. On success
750 * the returned list will be empty.
751 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100752 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200753 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100755heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200756{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100757 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200758 char_u *marker;
759 list_T *l;
760 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100761 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 int marker_indent_len = 0;
763 int text_indent_len = 0;
764 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200765 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200766 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100767 int evalstr = FALSE;
768 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100769 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
770 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771
772 if (eap->getline == NULL)
773 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000774 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 return NULL;
776 }
777
778 // Check for the optional 'trim' word before the marker
779 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100781 while (TRUE)
782 {
783 if (STRNCMP(cmd, "trim", 4) == 0
784 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100786 cmd = skipwhite(cmd + 4);
787
788 // Trim the indentation from all the lines in the here document.
789 // The amount of indentation trimmed is the same as the indentation
790 // of the first line after the :let command line. To find the end
791 // marker the indent of the :let command line is trimmed.
792 p = *eap->cmdlinep;
793 while (VIM_ISWHITE(*p))
794 {
795 p++;
796 marker_indent_len++;
797 }
798 text_indent_len = -1;
799
800 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200801 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100802 if (STRNCMP(cmd, "eval", 4) == 0
803 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
804 {
805 cmd = skipwhite(cmd + 4);
806 evalstr = TRUE;
807 continue;
808 }
809 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 }
811
812 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200813 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200814 {
815 marker = skipwhite(cmd);
816 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200817 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200818 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000819 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200820 return NULL;
821 }
822 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200823 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200824 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000825 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 return NULL;
827 }
828 }
829 else
830 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200831 // When getting lines for an embedded script, if the marker is missing,
832 // accept '.' as the marker.
833 if (script_get)
834 marker = dot;
835 else
836 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000837 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200838 return NULL;
839 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840 }
841
842 l = list_alloc();
843 if (l == NULL)
844 return NULL;
845
846 for (;;)
847 {
848 int mi = 0;
849 int ti = 0;
850
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100851 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200852 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
853 if (theline == NULL)
854 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000855 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200856 break;
857 }
858
859 // with "trim": skip the indent matching the :let line to find the
860 // marker
861 if (marker_indent_len > 0
862 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
863 mi = marker_indent_len;
864 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200865 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200866
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100867 // If expression evaluation failed in the heredoc, then skip till the
868 // end marker.
869 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100870 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100871
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 if (text_indent_len == -1 && *theline != NUL)
873 {
874 // set the text indent from the first line.
875 p = theline;
876 text_indent_len = 0;
877 while (VIM_ISWHITE(*p))
878 {
879 p++;
880 text_indent_len++;
881 }
882 text_indent = vim_strnsave(theline, text_indent_len);
883 }
884 // with "trim": skip the indent matching the first line
885 if (text_indent != NULL)
886 for (ti = 0; ti < text_indent_len; ++ti)
887 if (theline[ti] != text_indent[ti])
888 break;
889
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100890 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100891 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100892 {
LemonBoy2eaef102022-05-06 13:14:50 +0100893 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100894 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100895 vim_free(theline);
896 vim_free(text_indent);
897 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100898 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100899 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100900 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100901 else
902 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100903 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100904 {
905 str = eval_all_expr_in_str(str);
906 if (str == NULL)
907 {
908 // expression evaluation failed
909 eval_failed = TRUE;
910 continue;
911 }
912 vim_free(theline);
913 theline = str;
914 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100915
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100916 if (list_append_string(l, str, -1) == FAIL)
917 break;
918 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200919 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100920 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200921 vim_free(text_indent);
922
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100923 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
924 generate_NEWLIST(cctx, count, FALSE);
925
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100926 if (eval_failed)
927 {
928 // expression evaluation in the heredoc failed
929 list_free(l);
930 return NULL;
931 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200932 return l;
933}
934
935/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200936 * Vim9 variable declaration:
937 * ":var name"
938 * ":var name: type"
939 * ":var name = expr"
940 * ":var name: type = expr"
941 * etc.
942 */
943 void
944ex_var(exarg_T *eap)
945{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000946 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000947 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000948
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200949 if (!in_vim9script())
950 {
951 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
952 return;
953 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000954 has_var = checkforcmd_noparen(&p, "var", 3);
955 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000956 {
957 emsg(_(e_cannot_declare_variable_on_command_line));
958 return;
959 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200960 ex_let(eap);
961}
962
963/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 * ":let" list all variable values
965 * ":let var1 var2" list variable values
966 * ":let var = expr" assignment command.
967 * ":let var += expr" assignment command.
968 * ":let var -= expr" assignment command.
969 * ":let var *= expr" assignment command.
970 * ":let var /= expr" assignment command.
971 * ":let var %= expr" assignment command.
972 * ":let var .= expr" assignment command.
973 * ":let var ..= expr" assignment command.
974 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100976 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200977 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200978 * ":final var = expr" assignment command.
979 * ":final [var1, var2] = expr" unpack list.
980 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 * ":const" list all variable values
982 * ":const var1 var2" list variable values
983 * ":const var = expr" assignment command.
984 * ":const [var1, var2] = expr" unpack list.
985 */
986 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200987ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988{
989 char_u *arg = eap->arg;
990 char_u *expr = NULL;
991 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992 int var_count = 0;
993 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200994 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 char_u *argend;
996 int first = TRUE;
997 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200998 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100999 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001000 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001002 if (eap->cmdidx == CMD_final && !vim9script)
1003 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001004 // In legacy Vim script ":final" is short for ":finally".
1005 ex_finally(eap);
1006 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001007 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001008 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001009 {
1010 emsg(_(e_cannot_use_let_in_vim9_script));
1011 return;
1012 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001013
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001014 if (eap->cmdidx == CMD_const)
1015 flags |= ASSIGN_CONST;
1016 else if (eap->cmdidx == CMD_final)
1017 flags |= ASSIGN_FINAL;
1018
1019 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001021 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001023 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001024 if (argend == NULL)
1025 return;
1026 if (argend > arg && argend[-1] == '.') // for var.='str'
1027 --argend;
1028 expr = skipwhite(argend);
1029 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001030 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001032 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1033 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001034 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001035 {
1036 // ":let" without "=": list variables
1037 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001038 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001039 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001040 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001041 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001042 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001043 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001044 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001045 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001046 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001047 else
1048 // Vim9 declaration ":var name: type"
1049 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001050 }
1051 else
1052 {
1053 // ":let var1 var2" - list values
1054 arg = list_arg_vars(eap, arg, &first);
1055 }
1056 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057 else if (!eap->skip)
1058 {
1059 // ":let"
1060 list_glob_vars(&first);
1061 list_buf_vars(&first);
1062 list_win_vars(&first);
1063 list_tab_vars(&first);
1064 list_script_vars(&first);
1065 list_func_vars(&first);
1066 list_vim_vars(&first);
1067 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001068 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001069 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001070 }
zeertzjq474891b2023-04-12 21:36:03 +01001071
1072 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001074 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001075 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001076
Bram Moolenaard9876422022-10-12 12:58:54 +01001077 // :let text =<< [trim] [eval] END
1078 // :var text =<< [trim] [eval] END
1079 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1080 || !IS_WHITE_OR_NUL(expr[3])))
1081 semsg(_(e_white_space_required_before_and_after_str_at_str),
1082 "=<<", expr);
1083 else
1084 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1085
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 if (l != NULL)
1087 {
1088 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001089 if (!eap->skip)
1090 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001091 // errors are for the assignment, not the end marker
1092 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001093 op[0] = '=';
1094 op[1] = NUL;
1095 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001097 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001098 clear_tv(&rettv);
1099 }
zeertzjq474891b2023-04-12 21:36:03 +01001100 return;
1101 }
1102
1103 evalarg_T evalarg;
1104 int len = 1;
1105
1106 CLEAR_FIELD(rettv);
1107
1108 int cur_lnum;
1109
1110 op[0] = '=';
1111 op[1] = NUL;
1112 if (*expr != '=')
1113 {
1114 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1115 {
1116 // +=, /=, etc. require an existing variable
1117 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1118 }
1119 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1120 {
1121 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1122 ++len;
1123 if (expr[0] == '.' && expr[1] == '.') // ..=
1124 {
1125 ++expr;
1126 ++len;
1127 }
1128 }
1129 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001130 }
1131 else
zeertzjq474891b2023-04-12 21:36:03 +01001132 ++expr;
1133
1134 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1135 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001136 {
zeertzjq474891b2023-04-12 21:36:03 +01001137 vim_strncpy(op, expr - len, len);
1138 semsg(_(e_white_space_required_before_and_after_str_at_str),
1139 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001140 }
zeertzjq474891b2023-04-12 21:36:03 +01001141
1142 if (eap->skip)
1143 ++emsg_skip;
1144 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1145 expr = skipwhite_and_linebreak(expr, &evalarg);
1146 cur_lnum = SOURCING_LNUM;
1147 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1148 if (eap->skip)
1149 --emsg_skip;
1150 clear_evalarg(&evalarg, eap);
1151
1152 // Restore the line number so that any type error is given for the
1153 // declaration, not the expression.
1154 SOURCING_LNUM = cur_lnum;
1155
1156 if (!eap->skip && eval_res != FAIL)
1157 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1158 flags, op);
1159 if (eval_res != FAIL)
1160 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001161}
1162
1163/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001164 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001165 * Handles both "var" with any type and "[var, var; var]" with a list type.
1166 * When "op" is not NULL it points to a string with characters that
1167 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1168 * or concatenate.
1169 * Returns OK or FAIL;
1170 */
1171 int
1172ex_let_vars(
1173 char_u *arg_start,
1174 typval_T *tv,
1175 int copy, // copy values from "tv", don't move
1176 int semicolon, // from skip_var_list()
1177 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001178 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001179 char_u *op)
1180{
1181 char_u *arg = arg_start;
1182 list_T *l;
1183 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001184 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001185 listitem_T *item;
1186 typval_T ltv;
1187
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001188 if (tv->v_type == VAR_VOID)
1189 {
1190 emsg(_(e_cannot_use_void_value));
1191 return FAIL;
1192 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001193 if (*arg != '[')
1194 {
1195 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001196 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001197 return FAIL;
1198 return OK;
1199 }
1200
1201 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1202 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1203 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001204 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001205 return FAIL;
1206 }
1207
1208 i = list_len(l);
1209 if (semicolon == 0 && var_count < i)
1210 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001211 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001212 return FAIL;
1213 }
1214 if (var_count - semicolon > i)
1215 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001216 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001217 return FAIL;
1218 }
1219
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001220 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 item = l->lv_first;
1222 while (*arg != ']')
1223 {
1224 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001225 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001226 arg = ex_let_one(arg, &item->li_tv, TRUE,
1227 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001228 item = item->li_next;
1229 if (arg == NULL)
1230 return FAIL;
1231
1232 arg = skipwhite(arg);
1233 if (*arg == ';')
1234 {
1235 // Put the rest of the list (may be empty) in the var after ';'.
1236 // Create a new list for this.
1237 l = list_alloc();
1238 if (l == NULL)
1239 return FAIL;
1240 while (item != NULL)
1241 {
1242 list_append_tv(l, &item->li_tv);
1243 item = item->li_next;
1244 }
1245
1246 ltv.v_type = VAR_LIST;
1247 ltv.v_lock = 0;
1248 ltv.vval.v_list = l;
1249 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001250 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001251
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001252 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1253 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001254 clear_tv(&ltv);
1255 if (arg == NULL)
1256 return FAIL;
1257 break;
1258 }
1259 else if (*arg != ',' && *arg != ']')
1260 {
1261 internal_error("ex_let_vars()");
1262 return FAIL;
1263 }
1264 }
1265
1266 return OK;
1267}
1268
1269/*
1270 * Skip over assignable variable "var" or list of variables "[var, var]".
1271 * Used for ":let varvar = expr" and ":for varvar in expr".
1272 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001273 * for "[var, var; var]" set "semicolon" to 1.
1274 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001275 * Return NULL for an error.
1276 */
1277 char_u *
1278skip_var_list(
1279 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001282 int *semicolon,
1283 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001284{
1285 char_u *p, *s;
1286
1287 if (*arg == '[')
1288 {
1289 // "[var, var]": find the matching ']'.
1290 p = arg;
1291 for (;;)
1292 {
1293 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001294 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 if (s == p)
1296 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001297 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001298 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001299 return NULL;
1300 }
1301 ++*var_count;
1302
1303 p = skipwhite(s);
1304 if (*p == ']')
1305 break;
1306 else if (*p == ';')
1307 {
1308 if (*semicolon == 1)
1309 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001310 if (!silent)
1311 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001312 return NULL;
1313 }
1314 *semicolon = 1;
1315 }
1316 else if (*p != ',')
1317 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001318 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001319 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001320 return NULL;
1321 }
1322 }
1323 return p + 1;
1324 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001325
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001326 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001327}
1328
1329/*
1330 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1331 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001333 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001334 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001337 char_u *end;
1338 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340 if (*arg == '@' && arg[1] != NUL)
1341 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001342
1343 // termcap option name may have non-alpha characters
1344 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1345 return arg + 5;
1346
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001349
1350 // "a: type" is declaring variable "a" with a type, not "a:".
1351 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001352 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001353 --end;
1354
Bram Moolenaar585587d2021-01-17 20:52:13 +01001355 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001357 if (*skipwhite(end) == ':')
1358 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 }
1360 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001361}
1362
1363/*
1364 * List variables for hashtab "ht" with prefix "prefix".
1365 * If "empty" is TRUE also list NULL strings as empty strings.
1366 */
1367 void
1368list_hashtable_vars(
1369 hashtab_T *ht,
1370 char *prefix,
1371 int empty,
1372 int *first)
1373{
1374 hashitem_T *hi;
1375 dictitem_T *di;
1376 int todo;
1377 char_u buf[IOSIZE];
1378
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001379 int save_ht_flags = ht->ht_flags;
1380 ht->ht_flags |= HTFLAGS_FROZEN;
1381
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001382 todo = (int)ht->ht_used;
1383 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1384 {
1385 if (!HASHITEM_EMPTY(hi))
1386 {
1387 --todo;
1388 di = HI2DI(hi);
1389
1390 // apply :filter /pat/ to variable name
1391 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1392 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1393 if (message_filtered(buf))
1394 continue;
1395
1396 if (empty || di->di_tv.v_type != VAR_STRING
1397 || di->di_tv.vval.v_string != NULL)
1398 list_one_var(di, prefix, first);
1399 }
1400 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001401
1402 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403}
1404
1405/*
1406 * List global variables.
1407 */
1408 static void
1409list_glob_vars(int *first)
1410{
1411 list_hashtable_vars(&globvarht, "", TRUE, first);
1412}
1413
1414/*
1415 * List buffer variables.
1416 */
1417 static void
1418list_buf_vars(int *first)
1419{
1420 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1421}
1422
1423/*
1424 * List window variables.
1425 */
1426 static void
1427list_win_vars(int *first)
1428{
1429 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1430}
1431
1432/*
1433 * List tab page variables.
1434 */
1435 static void
1436list_tab_vars(int *first)
1437{
1438 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1439}
1440
1441/*
1442 * List variables in "arg".
1443 */
1444 static char_u *
1445list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1446{
1447 int error = FALSE;
1448 int len;
1449 char_u *name;
1450 char_u *name_start;
1451 char_u *arg_subsc;
1452 char_u *tofree;
1453 typval_T tv;
1454
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001455 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456 {
1457 if (error || eap->skip)
1458 {
1459 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1460 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1461 {
1462 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001463 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001464 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 break;
1466 }
1467 }
1468 else
1469 {
1470 // get_name_len() takes care of expanding curly braces
1471 name_start = name = arg;
1472 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1473 if (len <= 0)
1474 {
1475 // This is mainly to keep test 49 working: when expanding
1476 // curly braces fails overrule the exception error message.
1477 if (len < 0 && !aborting())
1478 {
1479 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001480 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001481 break;
1482 }
1483 error = TRUE;
1484 }
1485 else
1486 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001487 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488 if (tofree != NULL)
1489 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001490 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001491 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001492 error = TRUE;
1493 else
1494 {
1495 // handle d.key, l[idx], f(expr)
1496 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001497 if (handle_subscript(&arg, name_start, &tv,
1498 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001499 error = TRUE;
1500 else
1501 {
1502 if (arg == arg_subsc && len == 2 && name[1] == ':')
1503 {
1504 switch (*name)
1505 {
1506 case 'g': list_glob_vars(first); break;
1507 case 'b': list_buf_vars(first); break;
1508 case 'w': list_win_vars(first); break;
1509 case 't': list_tab_vars(first); break;
1510 case 'v': list_vim_vars(first); break;
1511 case 's': list_script_vars(first); break;
1512 case 'l': list_func_vars(first); break;
1513 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001514 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001515 }
1516 }
1517 else
1518 {
1519 char_u numbuf[NUMBUFLEN];
1520 char_u *tf;
1521 int c;
1522 char_u *s;
1523
1524 s = echo_string(&tv, &tf, numbuf, 0);
1525 c = *arg;
1526 *arg = NUL;
1527 list_one_var_a("",
1528 arg == arg_subsc ? name : name_start,
1529 tv.v_type,
1530 s == NULL ? (char_u *)"" : s,
1531 first);
1532 *arg = c;
1533 vim_free(tf);
1534 }
1535 clear_tv(&tv);
1536 }
1537 }
1538 }
1539
1540 vim_free(tofree);
1541 }
1542
1543 arg = skipwhite(arg);
1544 }
1545
1546 return arg;
1547}
1548
1549/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001550 * Set an environment variable, part of ex_let_one().
1551 */
1552 static char_u *
1553ex_let_env(
1554 char_u *arg,
1555 typval_T *tv,
1556 int flags,
1557 char_u *endchars,
1558 char_u *op)
1559{
1560 char_u *arg_end = NULL;
1561 char_u *name;
1562 int len;
1563
1564 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1565 && (flags & ASSIGN_FOR_LOOP) == 0)
1566 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001567 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001568 return NULL;
1569 }
1570
1571 // Find the end of the name.
1572 ++arg;
1573 name = arg;
1574 len = get_env_len(&arg);
1575 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001576 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001577 else
1578 {
1579 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001580 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001581 else if (endchars != NULL
1582 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1583 emsg(_(e_unexpected_characters_in_let));
1584 else if (!check_secure())
1585 {
1586 char_u *tofree = NULL;
1587 int c1 = name[len];
1588 char_u *p;
1589
1590 name[len] = NUL;
1591 p = tv_get_string_chk(tv);
1592 if (p != NULL && op != NULL && *op == '.')
1593 {
1594 int mustfree = FALSE;
1595 char_u *s = vim_getenv(name, &mustfree);
1596
1597 if (s != NULL)
1598 {
1599 p = tofree = concat_str(s, p);
1600 if (mustfree)
1601 vim_free(s);
1602 }
1603 }
1604 if (p != NULL)
1605 {
1606 vim_setenv_ext(name, p);
1607 arg_end = arg;
1608 }
1609 name[len] = c1;
1610 vim_free(tofree);
1611 }
1612 }
1613 return arg_end;
1614}
1615
1616/*
1617 * Set an option, part of ex_let_one().
1618 */
1619 static char_u *
1620ex_let_option(
1621 char_u *arg,
1622 typval_T *tv,
1623 int flags,
1624 char_u *endchars,
1625 char_u *op)
1626{
1627 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001628 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001629 char_u *arg_end = NULL;
1630
1631 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1632 && (flags & ASSIGN_FOR_LOOP) == 0)
1633 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001634 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001635 return NULL;
1636 }
1637
1638 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001639 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001640 if (p == NULL || (endchars != NULL
1641 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001642 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001643 emsg(_(e_unexpected_characters_in_let));
1644 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001645 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001646
1647 int c1;
1648 long n = 0;
1649 getoption_T opt_type;
1650 long numval;
1651 char_u *stringval = NULL;
1652 char_u *s = NULL;
1653 int failed = FALSE;
1654 int opt_p_flags;
1655 char_u *tofree = NULL;
1656 char_u numbuf[NUMBUFLEN];
1657
1658 c1 = *p;
1659 *p = NUL;
1660
1661 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1662 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1663 {
1664 semsg(_(e_unknown_option_str_2), arg);
1665 goto theend;
1666 }
1667 if (op != NULL && *op != '='
1668 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1669 || (opt_type == gov_string && *op != '.')))
1670 {
1671 semsg(_(e_wrong_variable_type_for_str_equal), op);
1672 goto theend;
1673 }
1674
1675 if ((opt_type == gov_bool
1676 || opt_type == gov_number
1677 || opt_type == gov_hidden_bool
1678 || opt_type == gov_hidden_number)
1679 && (tv->v_type != VAR_STRING || !in_vim9script()))
1680 {
1681 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1682 // bool, possibly hidden
1683 n = (long)tv_get_bool_chk(tv, &failed);
1684 else
1685 // number, possibly hidden
1686 n = (long)tv_get_number_chk(tv, &failed);
1687 if (failed)
1688 goto theend;
1689 }
1690
1691 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1692 || tv->v_type == VAR_FUNC))
1693 {
1694 // If the option can be set to a function reference or a lambda
1695 // and the passed value is a function reference, then convert it to
1696 // the name (string) of the function reference.
1697 s = tv2string(tv, &tofree, numbuf, 0);
1698 if (s == NULL)
1699 goto theend;
1700 }
1701 // Avoid setting a string option to the text "v:false" or similar.
1702 // In Vim9 script also don't convert a number to string.
1703 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1704 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1705 {
1706 s = tv_get_string_chk(tv);
1707 if (s == NULL)
1708 goto theend;
1709 }
1710 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1711 {
1712 emsg(_(e_string_required));
1713 goto theend;
1714 }
1715
1716 if (op != NULL && *op != '=')
1717 {
1718 // number, in legacy script also bool
1719 if (opt_type == gov_number
1720 || (opt_type == gov_bool && !in_vim9script()))
1721 {
1722 switch (*op)
1723 {
1724 case '+': n = numval + n; break;
1725 case '-': n = numval - n; break;
1726 case '*': n = numval * n; break;
1727 case '/': n = (long)num_divide(numval, n, &failed); break;
1728 case '%': n = (long)num_modulus(numval, n, &failed); break;
1729 }
1730 s = NULL;
1731 if (failed)
1732 goto theend;
1733 }
1734 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1735 {
1736 // string
1737 s = concat_str(stringval, s);
1738 vim_free(stringval);
1739 stringval = s;
1740 }
1741 }
1742
1743 char *err = set_option_value(arg, n, s, scope);
1744 arg_end = p;
1745 if (err != NULL)
1746 emsg(_(err));
1747
1748theend:
1749 *p = c1;
1750 vim_free(stringval);
1751 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001752 return arg_end;
1753}
1754
1755/*
1756 * Set a register, part of ex_let_one().
1757 */
1758 static char_u *
1759ex_let_register(
1760 char_u *arg,
1761 typval_T *tv,
1762 int flags,
1763 char_u *endchars,
1764 char_u *op)
1765{
1766 char_u *arg_end = NULL;
1767
1768 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1769 && (flags & ASSIGN_FOR_LOOP) == 0)
1770 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001771 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001772 return NULL;
1773 }
1774 ++arg;
1775 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001776 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001777 else if (endchars != NULL
1778 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1779 emsg(_(e_unexpected_characters_in_let));
1780 else
1781 {
1782 char_u *ptofree = NULL;
1783 char_u *p;
1784
1785 p = tv_get_string_chk(tv);
1786 if (p != NULL && op != NULL && *op == '.')
1787 {
1788 char_u *s = get_reg_contents(*arg == '@'
1789 ? '"' : *arg, GREG_EXPR_SRC);
1790
1791 if (s != NULL)
1792 {
1793 p = ptofree = concat_str(s, p);
1794 vim_free(s);
1795 }
1796 }
1797 if (p != NULL)
1798 {
1799 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1800 arg_end = arg + 1;
1801 }
1802 vim_free(ptofree);
1803 }
1804 return arg_end;
1805}
1806
1807/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1809 * Returns a pointer to the char just after the var name.
1810 * Returns NULL if there is an error.
1811 */
1812 static char_u *
1813ex_let_one(
1814 char_u *arg, // points to variable name
1815 typval_T *tv, // value to assign to variable
1816 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001817 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001818 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001819 char_u *op, // "+", "-", "." or NULL
1820 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001821{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001822 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001824 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001825 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001826 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1827 {
1828 vim9_declare_error(arg);
1829 return NULL;
1830 }
1831
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 if (*arg == '$')
1833 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001834 // ":let $VAR = expr": Set environment variable.
1835 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001836 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001837 else if (*arg == '&')
1838 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001839 // ":let &option = expr": Set option value.
1840 // ":let &l:option = expr": Set local option value.
1841 // ":let &g:option = expr": Set global option value.
1842 // ":for &ts in range(8)": Set option value for for loop
1843 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001844 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001845 else if (*arg == '@')
1846 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001847 // ":let @r = expr": Set register contents.
1848 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 else if (eval_isnamec1(*arg) || *arg == '{')
1851 {
1852 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001853 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001854 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1855 ? GLV_NO_DECL : 0;
1856 if (op != NULL && *op != '=')
1857 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001859 // ":let var = expr": Set internal variable.
1860 // ":let var: type = expr": Set internal variable with type.
1861 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001862 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001863 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001864 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 if (endchars != NULL && vim_strchr(endchars,
1866 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001867 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001868 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001869 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001870 else
1871 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001872 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001873 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001874 }
1875 }
1876 clear_lval(&lv);
1877 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001879 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880
1881 return arg_end;
1882}
1883
1884/*
1885 * ":unlet[!] var1 ... " command.
1886 */
1887 void
1888ex_unlet(exarg_T *eap)
1889{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001890 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001891}
1892
1893/*
1894 * ":lockvar" and ":unlockvar" commands
1895 */
1896 void
1897ex_lockvar(exarg_T *eap)
1898{
1899 char_u *arg = eap->arg;
1900 int deep = 2;
1901
1902 if (eap->forceit)
1903 deep = -1;
1904 else if (vim_isdigit(*arg))
1905 {
1906 deep = getdigits(&arg);
1907 arg = skipwhite(arg);
1908 }
1909
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001910 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001911}
1912
1913/*
1914 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001915 * Also used for Vim9 script. "callback" is invoked as:
1916 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001917 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001918 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001919ex_unletlock(
1920 exarg_T *eap,
1921 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001922 int deep,
1923 int glv_flags,
1924 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1925 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001926{
1927 char_u *arg = argstart;
1928 char_u *name_end;
1929 int error = FALSE;
1930 lval_T lv;
1931
1932 do
1933 {
1934 if (*arg == '$')
1935 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001936 lv.ll_name = arg;
1937 lv.ll_tv = NULL;
1938 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939 if (get_env_len(&arg) == 0)
1940 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001941 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001942 return;
1943 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001944 if (!error && !eap->skip
1945 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1946 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001947 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001948 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001949 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001950 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001951 // Parse the name and find the end.
1952 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001953 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001954 if (lv.ll_name == NULL)
1955 error = TRUE; // error but continue parsing
1956 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1957 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001958 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001959 if (name_end != NULL)
1960 {
1961 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001962 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001963 }
1964 if (!(eap->skip || error))
1965 clear_lval(&lv);
1966 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001967 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001969 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001970 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001971 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001973 if (!eap->skip)
1974 clear_lval(&lv);
1975 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001976
1977 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001978 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001979
Bram Moolenaar63b91732021-08-05 20:40:03 +02001980 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001981}
1982
1983 static int
1984do_unlet_var(
1985 lval_T *lp,
1986 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001987 exarg_T *eap,
1988 int deep UNUSED,
1989 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001990{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001991 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001992 int ret = OK;
1993 int cc;
1994
1995 if (lp->ll_tv == NULL)
1996 {
1997 cc = *name_end;
1998 *name_end = NUL;
1999
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002000 // Environment variable, normal name or expanded name.
2001 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002002 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002003 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002004 ret = FAIL;
2005 *name_end = cc;
2006 }
2007 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002008 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002009 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002010 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002011 return FAIL;
2012 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002013 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2014 !lp->ll_empty2, lp->ll_n2);
2015 else if (lp->ll_list != NULL)
2016 // unlet a List item.
2017 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002018 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002019 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002020 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002021
2022 return ret;
2023}
2024
2025/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002026 * Unlet one item or a range of items from a list.
2027 * Return OK or FAIL.
2028 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002029 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002030list_unlet_range(
2031 list_T *l,
2032 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002033 long n1_arg,
2034 int has_n2,
2035 long n2)
2036{
2037 listitem_T *li = li_first;
2038 int n1 = n1_arg;
2039
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002040 // Delete a range of List items.
2041 li = li_first;
2042 n1 = n1_arg;
2043 while (li != NULL && (!has_n2 || n2 >= n1))
2044 {
2045 listitem_T *next = li->li_next;
2046
2047 listitem_remove(l, li);
2048 li = next;
2049 ++n1;
2050 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002051}
2052/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002053 * "unlet" a variable. Return OK if it existed, FAIL if not.
2054 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2055 */
2056 int
2057do_unlet(char_u *name, int forceit)
2058{
2059 hashtab_T *ht;
2060 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002061 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002062 dict_T *d;
2063 dictitem_T *di;
2064
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002065 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002066 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002067 return FAIL;
2068
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002069 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002070
2071 // can't :unlet a script variable in Vim9 script from a function
2072 if (ht == get_script_local_ht()
2073 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2074 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2075 == SCRIPT_VERSION_VIM9
2076 && check_vim9_unlet(name) == FAIL)
2077 return FAIL;
2078
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002079 if (ht != NULL && *varname != NUL)
2080 {
2081 d = get_current_funccal_dict(ht);
2082 if (d == NULL)
2083 {
2084 if (ht == &globvarht)
2085 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002086 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002087 d = &vimvardict;
2088 else
2089 {
2090 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2091 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2092 }
2093 if (d == NULL)
2094 {
2095 internal_error("do_unlet()");
2096 return FAIL;
2097 }
2098 }
2099 hi = hash_find(ht, varname);
2100 if (HASHITEM_EMPTY(hi))
2101 hi = find_hi_in_scoped_ht(name, &ht);
2102 if (hi != NULL && !HASHITEM_EMPTY(hi))
2103 {
2104 di = HI2DI(hi);
2105 if (var_check_fixed(di->di_flags, name, FALSE)
2106 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002107 || value_check_lock(d->dv_lock, name, FALSE)
2108 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002109 return FAIL;
2110
2111 delete_var(ht, hi);
2112 return OK;
2113 }
2114 }
2115 if (forceit)
2116 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002117 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002118 return FAIL;
2119}
2120
2121/*
2122 * Lock or unlock variable indicated by "lp".
2123 * "deep" is the levels to go (-1 for unlimited);
2124 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2125 */
2126 static int
2127do_lock_var(
2128 lval_T *lp,
2129 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002130 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002131 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002132 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002133{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002134 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002135 int ret = OK;
2136 int cc;
2137 dictitem_T *di;
2138
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002139 if (lp->ll_tv == NULL)
2140 {
2141 cc = *name_end;
2142 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002143 if (*lp->ll_name == '$')
2144 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002145 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002146 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002147 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002148 else
2149 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002150 // Normal name or expanded name.
2151 di = find_var(lp->ll_name, NULL, TRUE);
2152 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002153 {
2154 if (in_vim9script())
2155 semsg(_(e_cannot_find_variable_to_unlock_str),
2156 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002157 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002158 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002159 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002160 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002161 if ((di->di_flags & DI_FLAGS_FIX)
2162 && di->di_tv.v_type != VAR_DICT
2163 && di->di_tv.v_type != VAR_LIST)
2164 {
2165 // For historic reasons this error is not given for a list
2166 // or dict. E.g., the b: dict could be locked/unlocked.
2167 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2168 ret = FAIL;
2169 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002170 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002171 {
2172 if (in_vim9script())
2173 {
2174 svar_T *sv = find_typval_in_script(&di->di_tv,
2175 0, FALSE);
2176
2177 if (sv != NULL && sv->sv_const != 0)
2178 {
2179 semsg(_(e_cannot_change_readonly_variable_str),
2180 lp->ll_name);
2181 ret = FAIL;
2182 }
2183 }
2184
2185 if (ret == OK)
2186 {
2187 if (lock)
2188 di->di_flags |= DI_FLAGS_LOCK;
2189 else
2190 di->di_flags &= ~DI_FLAGS_LOCK;
2191 if (deep != 0)
2192 item_lock(&di->di_tv, deep, lock, FALSE);
2193 }
2194 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002195 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002196 }
2197 *name_end = cc;
2198 }
Bram Moolenaara187c432020-09-16 21:08:28 +02002199 else if (deep == 0)
2200 {
2201 // nothing to do
2202 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002203 else if (lp->ll_range)
2204 {
2205 listitem_T *li = lp->ll_li;
2206
2207 // (un)lock a range of List items.
2208 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2209 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002210 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002211 li = li->li_next;
2212 ++lp->ll_n1;
2213 }
2214 }
2215 else if (lp->ll_list != NULL)
2216 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002217 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002218 else
2219 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002220 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002221
2222 return ret;
2223}
2224
2225/*
2226 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002227 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2228 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002229 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002230 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002231item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002232{
2233 static int recurse = 0;
2234 list_T *l;
2235 listitem_T *li;
2236 dict_T *d;
2237 blob_T *b;
2238 hashitem_T *hi;
2239 int todo;
2240
2241 if (recurse >= DICT_MAXNEST)
2242 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002243 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002244 return;
2245 }
2246 if (deep == 0)
2247 return;
2248 ++recurse;
2249
2250 // lock/unlock the item itself
2251 if (lock)
2252 tv->v_lock |= VAR_LOCKED;
2253 else
2254 tv->v_lock &= ~VAR_LOCKED;
2255
2256 switch (tv->v_type)
2257 {
2258 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002259 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002261 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002263 case VAR_STRING:
2264 case VAR_FUNC:
2265 case VAR_PARTIAL:
2266 case VAR_FLOAT:
2267 case VAR_SPECIAL:
2268 case VAR_JOB:
2269 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002270 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002271 case VAR_CLASS:
2272 case VAR_OBJECT:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002273 break;
2274
2275 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002276 if ((b = tv->vval.v_blob) != NULL
2277 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002278 {
2279 if (lock)
2280 b->bv_lock |= VAR_LOCKED;
2281 else
2282 b->bv_lock &= ~VAR_LOCKED;
2283 }
2284 break;
2285 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002286 if ((l = tv->vval.v_list) != NULL
2287 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002288 {
2289 if (lock)
2290 l->lv_lock |= VAR_LOCKED;
2291 else
2292 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002293 if (deep < 0 || deep > 1)
2294 {
2295 if (l->lv_first == &range_list_item)
2296 l->lv_lock |= VAR_ITEMS_LOCKED;
2297 else
2298 {
2299 // recursive: lock/unlock the items the List contains
2300 CHECK_LIST_MATERIALIZE(l);
2301 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2302 deep - 1, lock, check_refcount);
2303 }
2304 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002305 }
2306 break;
2307 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002308 if ((d = tv->vval.v_dict) != NULL
2309 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002310 {
2311 if (lock)
2312 d->dv_lock |= VAR_LOCKED;
2313 else
2314 d->dv_lock &= ~VAR_LOCKED;
2315 if (deep < 0 || deep > 1)
2316 {
2317 // recursive: lock/unlock the items the List contains
2318 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002319 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002320 {
2321 if (!HASHITEM_EMPTY(hi))
2322 {
2323 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002324 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2325 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002326 }
2327 }
2328 }
2329 }
2330 }
2331 --recurse;
2332}
2333
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002334#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2335/*
2336 * Delete all "menutrans_" variables.
2337 */
2338 void
2339del_menutrans_vars(void)
2340{
2341 hashitem_T *hi;
2342 int todo;
2343
2344 hash_lock(&globvarht);
2345 todo = (int)globvarht.ht_used;
2346 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2347 {
2348 if (!HASHITEM_EMPTY(hi))
2349 {
2350 --todo;
2351 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2352 delete_var(&globvarht, hi);
2353 }
2354 }
2355 hash_unlock(&globvarht);
2356}
2357#endif
2358
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002359/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002360 * Local string buffer for the next two functions to store a variable name
2361 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2362 * get_user_var_name().
2363 */
2364
2365static char_u *varnamebuf = NULL;
2366static int varnamebuflen = 0;
2367
2368/*
2369 * Function to concatenate a prefix and a variable name.
2370 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002371 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002372cat_prefix_varname(int prefix, char_u *name)
2373{
2374 int len;
2375
2376 len = (int)STRLEN(name) + 3;
2377 if (len > varnamebuflen)
2378 {
2379 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002380 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002381 varnamebuf = alloc(len);
2382 if (varnamebuf == NULL)
2383 {
2384 varnamebuflen = 0;
2385 return NULL;
2386 }
2387 varnamebuflen = len;
2388 }
2389 *varnamebuf = prefix;
2390 varnamebuf[1] = ':';
2391 STRCPY(varnamebuf + 2, name);
2392 return varnamebuf;
2393}
2394
2395/*
2396 * Function given to ExpandGeneric() to obtain the list of user defined
2397 * (global/buffer/window/built-in) variable names.
2398 */
2399 char_u *
2400get_user_var_name(expand_T *xp, int idx)
2401{
2402 static long_u gdone;
2403 static long_u bdone;
2404 static long_u wdone;
2405 static long_u tdone;
2406 static int vidx;
2407 static hashitem_T *hi;
2408 hashtab_T *ht;
2409
2410 if (idx == 0)
2411 {
2412 gdone = bdone = wdone = vidx = 0;
2413 tdone = 0;
2414 }
2415
2416 // Global variables
2417 if (gdone < globvarht.ht_used)
2418 {
2419 if (gdone++ == 0)
2420 hi = globvarht.ht_array;
2421 else
2422 ++hi;
2423 while (HASHITEM_EMPTY(hi))
2424 ++hi;
2425 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2426 return cat_prefix_varname('g', hi->hi_key);
2427 return hi->hi_key;
2428 }
2429
2430 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002431 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002432 if (bdone < ht->ht_used)
2433 {
2434 if (bdone++ == 0)
2435 hi = ht->ht_array;
2436 else
2437 ++hi;
2438 while (HASHITEM_EMPTY(hi))
2439 ++hi;
2440 return cat_prefix_varname('b', hi->hi_key);
2441 }
2442
2443 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002444 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002445 if (wdone < ht->ht_used)
2446 {
2447 if (wdone++ == 0)
2448 hi = ht->ht_array;
2449 else
2450 ++hi;
2451 while (HASHITEM_EMPTY(hi))
2452 ++hi;
2453 return cat_prefix_varname('w', hi->hi_key);
2454 }
2455
2456 // t: variables
2457 ht = &curtab->tp_vars->dv_hashtab;
2458 if (tdone < ht->ht_used)
2459 {
2460 if (tdone++ == 0)
2461 hi = ht->ht_array;
2462 else
2463 ++hi;
2464 while (HASHITEM_EMPTY(hi))
2465 ++hi;
2466 return cat_prefix_varname('t', hi->hi_key);
2467 }
2468
2469 // v: variables
2470 if (vidx < VV_LEN)
2471 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2472
2473 VIM_CLEAR(varnamebuf);
2474 varnamebuflen = 0;
2475 return NULL;
2476}
2477
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002478 char *
2479get_var_special_name(int nr)
2480{
2481 switch (nr)
2482 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002483 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2484 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002485 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002486 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002487 }
2488 internal_error("get_var_special_name()");
2489 return "42";
2490}
2491
2492/*
2493 * Returns the global variable dictionary
2494 */
2495 dict_T *
2496get_globvar_dict(void)
2497{
2498 return &globvardict;
2499}
2500
2501/*
2502 * Returns the global variable hash table
2503 */
2504 hashtab_T *
2505get_globvar_ht(void)
2506{
2507 return &globvarht;
2508}
2509
2510/*
2511 * Returns the v: variable dictionary
2512 */
2513 dict_T *
2514get_vimvar_dict(void)
2515{
2516 return &vimvardict;
2517}
2518
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002519/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002521 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 */
2523 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002524find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002526 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2527 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528
2529 if (di == NULL)
2530 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002531 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2533 return (int)(vv - vimvars);
2534}
2535
2536
2537/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002538 * Set type of v: variable to "type".
2539 */
2540 void
2541set_vim_var_type(int idx, vartype_T type)
2542{
Bram Moolenaard787e402021-12-24 21:36:12 +00002543 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002544}
2545
2546/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002547 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002548 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002549 */
2550 void
2551set_vim_var_nr(int idx, varnumber_T val)
2552{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002553 vimvars[idx].vv_nr = val;
2554}
2555
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 char *
2557get_vim_var_name(int idx)
2558{
2559 return vimvars[idx].vv_name;
2560}
2561
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002562/*
2563 * Get typval_T v: variable value.
2564 */
2565 typval_T *
2566get_vim_var_tv(int idx)
2567{
2568 return &vimvars[idx].vv_tv;
2569}
2570
Bram Moolenaard787e402021-12-24 21:36:12 +00002571 type_T *
2572get_vim_var_type(int idx, garray_T *type_list)
2573{
2574 if (vimvars[idx].vv_type != NULL)
2575 return vimvars[idx].vv_type;
2576 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2577}
2578
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002579/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002580 * Set v: variable to "tv". Only accepts the same type.
2581 * Takes over the value of "tv".
2582 */
2583 int
2584set_vim_var_tv(int idx, typval_T *tv)
2585{
Bram Moolenaard787e402021-12-24 21:36:12 +00002586 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002587 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002588 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002589 clear_tv(tv);
2590 return FAIL;
2591 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002592 // VV_RO is also checked when compiling, but let's check here as well.
2593 if (vimvars[idx].vv_flags & VV_RO)
2594 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002595 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002596 return FAIL;
2597 }
2598 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2599 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002600 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002601 return FAIL;
2602 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002603 clear_tv(&vimvars[idx].vv_di.di_tv);
2604 vimvars[idx].vv_di.di_tv = *tv;
2605 return OK;
2606}
2607
2608/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002609 * Get number v: variable value.
2610 */
2611 varnumber_T
2612get_vim_var_nr(int idx)
2613{
2614 return vimvars[idx].vv_nr;
2615}
2616
2617/*
2618 * Get string v: variable value. Uses a static buffer, can only be used once.
2619 * If the String variable has never been set, return an empty string.
2620 * Never returns NULL;
2621 */
2622 char_u *
2623get_vim_var_str(int idx)
2624{
2625 return tv_get_string(&vimvars[idx].vv_tv);
2626}
2627
2628/*
2629 * Get List v: variable value. Caller must take care of reference count when
2630 * needed.
2631 */
2632 list_T *
2633get_vim_var_list(int idx)
2634{
2635 return vimvars[idx].vv_list;
2636}
2637
2638/*
2639 * Get Dict v: variable value. Caller must take care of reference count when
2640 * needed.
2641 */
2642 dict_T *
2643get_vim_var_dict(int idx)
2644{
2645 return vimvars[idx].vv_dict;
2646}
2647
2648/*
2649 * Set v:char to character "c".
2650 */
2651 void
2652set_vim_var_char(int c)
2653{
2654 char_u buf[MB_MAXBYTES + 1];
2655
2656 if (has_mbyte)
2657 buf[(*mb_char2bytes)(c, buf)] = NUL;
2658 else
2659 {
2660 buf[0] = c;
2661 buf[1] = NUL;
2662 }
2663 set_vim_var_string(VV_CHAR, buf, -1);
2664}
2665
2666/*
2667 * Set v:count to "count" and v:count1 to "count1".
2668 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2669 */
2670 void
2671set_vcount(
2672 long count,
2673 long count1,
2674 int set_prevcount)
2675{
2676 if (set_prevcount)
2677 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2678 vimvars[VV_COUNT].vv_nr = count;
2679 vimvars[VV_COUNT1].vv_nr = count1;
2680}
2681
2682/*
2683 * Save variables that might be changed as a side effect. Used when executing
2684 * a timer callback.
2685 */
2686 void
2687save_vimvars(vimvars_save_T *vvsave)
2688{
2689 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2690 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2691 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2692}
2693
2694/*
2695 * Restore variables saved by save_vimvars().
2696 */
2697 void
2698restore_vimvars(vimvars_save_T *vvsave)
2699{
2700 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2701 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2702 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2703}
2704
2705/*
2706 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2707 * value.
2708 */
2709 void
2710set_vim_var_string(
2711 int idx,
2712 char_u *val,
2713 int len) // length of "val" to use or -1 (whole string)
2714{
2715 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002716 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002717 if (val == NULL)
2718 vimvars[idx].vv_str = NULL;
2719 else if (len == -1)
2720 vimvars[idx].vv_str = vim_strsave(val);
2721 else
2722 vimvars[idx].vv_str = vim_strnsave(val, len);
2723}
2724
2725/*
2726 * Set List v: variable to "val".
2727 */
2728 void
2729set_vim_var_list(int idx, list_T *val)
2730{
2731 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002732 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002733 vimvars[idx].vv_list = val;
2734 if (val != NULL)
2735 ++val->lv_refcount;
2736}
2737
2738/*
2739 * Set Dictionary v: variable to "val".
2740 */
2741 void
2742set_vim_var_dict(int idx, dict_T *val)
2743{
2744 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002745 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002746 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002747 if (val == NULL)
2748 return;
2749
2750 ++val->dv_refcount;
2751 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002752}
2753
2754/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002755 * Set the v:argv list.
2756 */
2757 void
2758set_argv_var(char **argv, int argc)
2759{
2760 list_T *l = list_alloc();
2761 int i;
2762
2763 if (l == NULL)
2764 getout(1);
2765 l->lv_lock = VAR_FIXED;
2766 for (i = 0; i < argc; ++i)
2767 {
2768 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2769 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002770 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002771 }
2772 set_vim_var_list(VV_ARGV, l);
2773}
2774
2775/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002776 * Reset v:register, taking the 'clipboard' setting into account.
2777 */
2778 void
2779reset_reg_var(void)
2780{
2781 int regname = 0;
2782
2783 // Adjust the register according to 'clipboard', so that when
2784 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2785#ifdef FEAT_CLIPBOARD
2786 adjust_clip_reg(&regname);
2787#endif
2788 set_reg_var(regname);
2789}
2790
2791/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002792 * Set v:register if needed.
2793 */
2794 void
2795set_reg_var(int c)
2796{
2797 char_u regname;
2798
2799 if (c == 0 || c == ' ')
2800 regname = '"';
2801 else
2802 regname = c;
2803 // Avoid free/alloc when the value is already right.
2804 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2805 set_vim_var_string(VV_REG, &regname, 1);
2806}
2807
2808/*
2809 * Get or set v:exception. If "oldval" == NULL, return the current value.
2810 * Otherwise, restore the value to "oldval" and return NULL.
2811 * Must always be called in pairs to save and restore v:exception! Does not
2812 * take care of memory allocations.
2813 */
2814 char_u *
2815v_exception(char_u *oldval)
2816{
2817 if (oldval == NULL)
2818 return vimvars[VV_EXCEPTION].vv_str;
2819
2820 vimvars[VV_EXCEPTION].vv_str = oldval;
2821 return NULL;
2822}
2823
2824/*
2825 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2826 * Otherwise, restore the value to "oldval" and return NULL.
2827 * Must always be called in pairs to save and restore v:throwpoint! Does not
2828 * take care of memory allocations.
2829 */
2830 char_u *
2831v_throwpoint(char_u *oldval)
2832{
2833 if (oldval == NULL)
2834 return vimvars[VV_THROWPOINT].vv_str;
2835
2836 vimvars[VV_THROWPOINT].vv_str = oldval;
2837 return NULL;
2838}
2839
2840/*
2841 * Set v:cmdarg.
2842 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2843 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2844 * Must always be called in pairs!
2845 */
2846 char_u *
2847set_cmdarg(exarg_T *eap, char_u *oldarg)
2848{
2849 char_u *oldval;
2850 char_u *newval;
2851 unsigned len;
2852
2853 oldval = vimvars[VV_CMDARG].vv_str;
2854 if (eap == NULL)
2855 {
2856 vim_free(oldval);
2857 vimvars[VV_CMDARG].vv_str = oldarg;
2858 return NULL;
2859 }
2860
2861 if (eap->force_bin == FORCE_BIN)
2862 len = 6;
2863 else if (eap->force_bin == FORCE_NOBIN)
2864 len = 8;
2865 else
2866 len = 0;
2867
2868 if (eap->read_edit)
2869 len += 7;
2870
2871 if (eap->force_ff != 0)
2872 len += 10; // " ++ff=unix"
2873 if (eap->force_enc != 0)
2874 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2875 if (eap->bad_char != 0)
2876 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2877
2878 newval = alloc(len + 1);
2879 if (newval == NULL)
2880 return NULL;
2881
2882 if (eap->force_bin == FORCE_BIN)
2883 sprintf((char *)newval, " ++bin");
2884 else if (eap->force_bin == FORCE_NOBIN)
2885 sprintf((char *)newval, " ++nobin");
2886 else
2887 *newval = NUL;
2888
2889 if (eap->read_edit)
2890 STRCAT(newval, " ++edit");
2891
2892 if (eap->force_ff != 0)
2893 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2894 eap->force_ff == 'u' ? "unix"
2895 : eap->force_ff == 'd' ? "dos"
2896 : "mac");
2897 if (eap->force_enc != 0)
2898 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2899 eap->cmd + eap->force_enc);
2900 if (eap->bad_char == BAD_KEEP)
2901 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2902 else if (eap->bad_char == BAD_DROP)
2903 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2904 else if (eap->bad_char != 0)
2905 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2906 vimvars[VV_CMDARG].vv_str = newval;
2907 return oldval;
2908}
2909
2910/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002911 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002912 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2913 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002914 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2915 */
2916 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002917eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002918 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00002919 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002920 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002921 typval_T *rettv, // NULL when only checking existence
2922 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002923 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002924{
2925 int ret = OK;
2926 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002927 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002928 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00002929 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002930 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002931
Bram Moolenaar94674f22023-01-06 18:42:20 +00002932 if (len > 0)
2933 {
2934 // truncate the name, so that we can use strcmp()
2935 cc = name[len];
2936 name[len] = NUL;
2937 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002938
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002939 // Check for local variable when debugging.
2940 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002941 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002942 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002943 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2944
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002945 if (v != NULL)
2946 {
2947 tv = &v->di_tv;
2948 if (dip != NULL)
2949 *dip = v;
2950 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002951 else
2952 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002953 }
2954
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002955 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002957 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002958 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2959
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002960 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00002961 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962
2963 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002964 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002966 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002967 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002968 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002969 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002970 ht = &SCRIPT_VARS(sid);
2971 if (ht != NULL)
2972 {
2973 dictitem_T *v = find_var_in_ht(ht, 0, name,
2974 flags & EVAL_VAR_NOAUTOLOAD);
2975
2976 if (v != NULL)
2977 {
2978 tv = &v->di_tv;
2979 if (dip != NULL)
2980 *dip = v;
2981 }
2982 else
2983 ht = NULL;
2984 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002985 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002986 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002987 {
2988 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002989 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002990 ret = FAIL;
2991 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002992 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002993 else
2994 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002995 if (rettv != NULL)
2996 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01002997 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002998 rettv->v_type = VAR_ANY;
2999 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3000 }
3001 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003002 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003004 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003005 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003006 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003007 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003008
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003009 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003010 // function name. For a global non-autoload function "g:" is
3011 // required.
3012 if (ufunc != NULL && (has_g_prefix
3013 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003014 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003015 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003016 if (rettv != NULL)
3017 {
3018 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003019 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003020 // Keep the "g:", otherwise script-local may be
3021 // assumed.
3022 rettv->vval.v_string = vim_strsave(name);
3023 else
3024 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003025 if (rettv->vval.v_string != NULL)
3026 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003027 }
3028 }
3029 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 }
3031
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003032 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003033 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003034 if (tv == NULL)
3035 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003036 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003037 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003038 ret = FAIL;
3039 }
3040 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003041 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003042 svar_T *sv = NULL;
3043 int was_assigned = FALSE;
3044
Bram Moolenaar11005b02021-07-11 20:59:00 +02003045 if (ht != NULL && ht == get_script_local_ht()
3046 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003047 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003048 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003049 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003050 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003051 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003052 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3053 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003054 }
3055
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003056 // If a list or dict variable wasn't initialized and has meaningful
3057 // type, do it now. Not for global variables, they are not
3058 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003059 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003060 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003061 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003062 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003063 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003064 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003065 tv->vval.v_dict = dict_alloc();
3066 if (tv->vval.v_dict != NULL)
3067 {
3068 ++tv->vval.v_dict->dv_refcount;
3069 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003070 if (sv != NULL)
3071 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003072 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003073 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003074 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003075 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003076 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003077 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003078 tv->vval.v_list = list_alloc();
3079 if (tv->vval.v_list != NULL)
3080 {
3081 ++tv->vval.v_list->lv_refcount;
3082 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003083 if (sv != NULL)
3084 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003085 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003086 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003087 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003088 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003089 || !in_vim9script()))
3090 {
3091 tv->vval.v_blob = blob_alloc();
3092 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003093 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003094 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003095 if (sv != NULL)
3096 sv->sv_flags |= SVFLAG_ASSIGNED;
3097 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003098 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003099 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003100 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003101 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003102 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103
Bram Moolenaar94674f22023-01-06 18:42:20 +00003104 if (len > 0)
3105 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003106
3107 return ret;
3108}
3109
3110/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003111 * Get the value of internal variable "name", also handling "import.name".
3112 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3113 */
3114 int
3115eval_variable_import(
3116 char_u *name,
3117 typval_T *rettv)
3118{
3119 char_u *s = name;
3120 while (ASCII_ISALNUM(*s) || *s == '_')
3121 ++s;
3122 int len = (int)(s - name);
3123
3124 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3125 return FAIL;
3126 if (rettv->v_type == VAR_ANY && *s == '.')
3127 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003128 char_u *ns = s + 1;
3129 s = ns;
3130 while (ASCII_ISALNUM(*s) || *s == '_')
3131 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003132 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003133 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003134 }
3135 return OK;
3136}
3137
3138
3139/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003140 * Check if variable "name[len]" is a local variable or an argument.
3141 * If so, "*eval_lavars_used" is set to TRUE.
3142 */
3143 void
3144check_vars(char_u *name, int len)
3145{
3146 int cc;
3147 char_u *varname;
3148 hashtab_T *ht;
3149
3150 if (eval_lavars_used == NULL)
3151 return;
3152
3153 // truncate the name, so that we can use strcmp()
3154 cc = name[len];
3155 name[len] = NUL;
3156
3157 ht = find_var_ht(name, &varname);
3158 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3159 {
3160 if (find_var(name, NULL, TRUE) != NULL)
3161 *eval_lavars_used = TRUE;
3162 }
3163
3164 name[len] = cc;
3165}
3166
3167/*
3168 * Find variable "name" in the list of variables.
3169 * Return a pointer to it if found, NULL if not found.
3170 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003171 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003172 */
3173 dictitem_T *
3174find_var(char_u *name, hashtab_T **htp, int no_autoload)
3175{
3176 char_u *varname;
3177 hashtab_T *ht;
3178 dictitem_T *ret = NULL;
3179
3180 ht = find_var_ht(name, &varname);
3181 if (htp != NULL)
3182 *htp = ht;
3183 if (ht == NULL)
3184 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003185 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003186 if (ret != NULL)
3187 return ret;
3188
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003189 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003190 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003191 if (ret != NULL)
3192 return ret;
3193
3194 // in Vim9 script items without a scope can be script-local
3195 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3196 {
3197 ht = get_script_local_ht();
3198 if (ht != NULL)
3199 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003200 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003201 if (ret != NULL)
3202 {
3203 if (htp != NULL)
3204 *htp = ht;
3205 return ret;
3206 }
3207 }
3208 }
3209
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003210 // When using "vim9script autoload" script-local items are prefixed but can
3211 // be used with s:name.
3212 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003213 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003214 {
3215 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3216
3217 if (si->sn_autoload_prefix != NULL)
3218 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003219 char_u *base_name = (name[0] == 's' && name[1] == ':')
3220 ? name + 2 : name;
3221 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003222
3223 if (auto_name != NULL)
3224 {
3225 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003226 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003227 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003228 if (ret != NULL)
3229 {
3230 if (htp != NULL)
3231 *htp = ht;
3232 return ret;
3233 }
3234 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003235 }
3236 }
3237
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003238 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003239}
3240
3241/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003242 * Like find_var() but if the name starts with <SNR>99_ then look in the
3243 * referenced script (used for a funcref).
3244 */
3245 dictitem_T *
3246find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3247{
3248 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3249 {
3250 char_u *p = name + 5;
3251 int sid = getdigits(&p);
3252
3253 if (SCRIPT_ID_VALID(sid) && *p == '_')
3254 {
3255 hashtab_T *ht = &SCRIPT_VARS(sid);
3256
3257 if (ht != NULL)
3258 {
3259 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3260
3261 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003262 {
3263 if (htp != NULL)
3264 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003265 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003266 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003267 }
3268 }
3269 }
3270
3271 return find_var(name, htp, no_autoload);
3272}
3273
3274/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003275 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003276 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003277 * Returns NULL if not found.
3278 */
3279 dictitem_T *
3280find_var_in_ht(
3281 hashtab_T *ht,
3282 int htname,
3283 char_u *varname,
3284 int no_autoload)
3285{
3286 hashitem_T *hi;
3287
3288 if (*varname == NUL)
3289 {
3290 // Must be something like "s:", otherwise "ht" would be NULL.
3291 switch (htname)
3292 {
3293 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3294 case 'g': return &globvars_var;
3295 case 'v': return &vimvars_var;
3296 case 'b': return &curbuf->b_bufvar;
3297 case 'w': return &curwin->w_winvar;
3298 case 't': return &curtab->tp_winvar;
3299 case 'l': return get_funccal_local_var();
3300 case 'a': return get_funccal_args_var();
3301 }
3302 return NULL;
3303 }
3304
3305 hi = hash_find(ht, varname);
3306 if (HASHITEM_EMPTY(hi))
3307 {
3308 // For global variables we may try auto-loading the script. If it
3309 // worked find the variable again. Don't auto-load a script if it was
3310 // loaded already, otherwise it would be loaded every time when
3311 // checking if a function name is a Funcref variable.
3312 if (ht == &globvarht && !no_autoload)
3313 {
3314 // Note: script_autoload() may make "hi" invalid. It must either
3315 // be obtained again or not used.
3316 if (!script_autoload(varname, FALSE) || aborting())
3317 return NULL;
3318 hi = hash_find(ht, varname);
3319 }
3320 if (HASHITEM_EMPTY(hi))
3321 return NULL;
3322 }
3323 return HI2DI(hi);
3324}
3325
3326/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 * Get the script-local hashtab. NULL if not in a script context.
3328 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003329 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330get_script_local_ht(void)
3331{
3332 scid_T sid = current_sctx.sc_sid;
3333
Bram Moolenaare3d46852020-08-29 13:39:17 +02003334 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 return &SCRIPT_VARS(sid);
3336 return NULL;
3337}
3338
3339/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003340 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003341 * When "cmd" is TRUE it must look like a command, a function must be followed
3342 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003343 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003345 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003346lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003347 char_u *name,
3348 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003349 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003350 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351{
3352 hashtab_T *ht = get_script_local_ht();
3353 char_u buffer[30];
3354 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003355 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003357 int is_global = FALSE;
3358 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359
3360 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003361 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 if (len < sizeof(buffer) - 1)
3363 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003364 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 vim_strncpy(buffer, name, len);
3366 p = buffer;
3367 }
3368 else
3369 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003370 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003372 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003373 }
3374
3375 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003376 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003377
3378 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003379 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003380 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 if (p != buffer)
3382 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003383
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003384 // Find a function, so that a following "->" works.
3385 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3386 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003387 if (res != OK)
3388 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003389 p = skipwhite(name + len);
3390
3391 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003392 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003393 // Do not check for an internal function, since it might also be a
3394 // valid command, such as ":split" versus "split()".
3395 // Skip "g:" before a function name.
3396 if (name[0] == 'g' && name[1] == ':')
3397 {
3398 is_global = TRUE;
3399 fname = name + 2;
3400 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003401 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003402 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003403 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003404 }
3405
Bram Moolenaar709664c2020-12-12 14:33:41 +01003406 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407}
3408
3409/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003410 * Find the hashtab used for a variable name.
3411 * Return NULL if the name is not valid.
3412 * Set "varname" to the start of name without ':'.
3413 */
3414 hashtab_T *
3415find_var_ht(char_u *name, char_u **varname)
3416{
3417 hashitem_T *hi;
3418 hashtab_T *ht;
3419
3420 if (name[0] == NUL)
3421 return NULL;
3422 if (name[1] != ':')
3423 {
3424 // The name must not start with a colon or #.
3425 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3426 return NULL;
3427 *varname = name;
3428
3429 // "version" is "v:version" in all scopes if scriptversion < 3.
3430 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003431 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003432 {
3433 hi = hash_find(&compat_hashtab, name);
3434 if (!HASHITEM_EMPTY(hi))
3435 return &compat_hashtab;
3436 }
3437
3438 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003439 if (ht != NULL)
3440 return ht; // local variable
3441
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003442 // In Vim9 script items at the script level are script-local, except
3443 // for autoload names.
3444 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 {
3446 ht = get_script_local_ht();
3447 if (ht != NULL)
3448 return ht;
3449 }
3450
3451 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003452 }
3453 *varname = name + 2;
3454 if (*name == 'g') // global variable
3455 return &globvarht;
3456 // There must be no ':' or '#' in the rest of the name, unless g: is used
3457 if (vim_strchr(name + 2, ':') != NULL
3458 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3459 return NULL;
3460 if (*name == 'b') // buffer variable
3461 return &curbuf->b_vars->dv_hashtab;
3462 if (*name == 'w') // window variable
3463 return &curwin->w_vars->dv_hashtab;
3464 if (*name == 't') // tab page variable
3465 return &curtab->tp_vars->dv_hashtab;
3466 if (*name == 'v') // v: variable
3467 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003468 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003469 && get_current_funccal()->fc_func->uf_def_status
3470 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003472 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 if (*name == 'a') // a: function argument
3474 return get_funccal_args_ht();
3475 if (*name == 'l') // l: local function variable
3476 return get_funccal_local_ht();
3477 }
3478 if (*name == 's') // script variable
3479 {
3480 ht = get_script_local_ht();
3481 if (ht != NULL)
3482 return ht;
3483 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003484 return NULL;
3485}
3486
3487/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003488 * Get the string value of a (global/local) variable.
3489 * Note: see tv_get_string() for how long the pointer remains valid.
3490 * Returns NULL when it doesn't exist.
3491 */
3492 char_u *
3493get_var_value(char_u *name)
3494{
3495 dictitem_T *v;
3496
3497 v = find_var(name, NULL, FALSE);
3498 if (v == NULL)
3499 return NULL;
3500 return tv_get_string(&v->di_tv);
3501}
3502
3503/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003504 * Allocate a new hashtab for a sourced script. It will be used while
3505 * sourcing this script and when executing functions defined in the script.
3506 */
3507 void
3508new_script_vars(scid_T id)
3509{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003510 scriptvar_T *sv;
3511
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003512 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3513 if (sv == NULL)
3514 return;
3515 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003516 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003517}
3518
3519/*
3520 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3521 * point to it.
3522 */
3523 void
3524init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3525{
3526 hash_init(&dict->dv_hashtab);
3527 dict->dv_lock = 0;
3528 dict->dv_scope = scope;
3529 dict->dv_refcount = DO_NOT_FREE_CNT;
3530 dict->dv_copyID = 0;
3531 dict_var->di_tv.vval.v_dict = dict;
3532 dict_var->di_tv.v_type = VAR_DICT;
3533 dict_var->di_tv.v_lock = VAR_FIXED;
3534 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3535 dict_var->di_key[0] = NUL;
3536}
3537
3538/*
3539 * Unreference a dictionary initialized by init_var_dict().
3540 */
3541 void
3542unref_var_dict(dict_T *dict)
3543{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003544 // Now the dict needs to be freed if no one else is using it, go back to
3545 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003546 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3547 dict_unref(dict);
3548}
3549
3550/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003551 * Clean up a list of internal variables.
3552 * Frees all allocated variables and the value they contain.
3553 * Clears hashtab "ht", does not free it.
3554 */
3555 void
3556vars_clear(hashtab_T *ht)
3557{
3558 vars_clear_ext(ht, TRUE);
3559}
3560
3561/*
3562 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3563 */
3564 void
3565vars_clear_ext(hashtab_T *ht, int free_val)
3566{
3567 int todo;
3568 hashitem_T *hi;
3569 dictitem_T *v;
3570
3571 hash_lock(ht);
3572 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003573 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003574 {
3575 if (!HASHITEM_EMPTY(hi))
3576 {
3577 --todo;
3578
3579 // Free the variable. Don't remove it from the hashtab,
3580 // ht_array might change then. hash_clear() takes care of it
3581 // later.
3582 v = HI2DI(hi);
3583 if (free_val)
3584 clear_tv(&v->di_tv);
3585 if (v->di_flags & DI_FLAGS_ALLOC)
3586 vim_free(v);
3587 }
3588 }
3589 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003590 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003591}
3592
3593/*
3594 * Delete a variable from hashtab "ht" at item "hi".
3595 * Clear the variable value and free the dictitem.
3596 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003597 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003598delete_var(hashtab_T *ht, hashitem_T *hi)
3599{
3600 dictitem_T *di = HI2DI(hi);
3601
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003602 if (hash_remove(ht, hi, "delete variable") != OK)
3603 return;
3604
3605 clear_tv(&di->di_tv);
3606 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003607}
3608
3609/*
3610 * List the value of one internal variable.
3611 */
3612 static void
3613list_one_var(dictitem_T *v, char *prefix, int *first)
3614{
3615 char_u *tofree;
3616 char_u *s;
3617 char_u numbuf[NUMBUFLEN];
3618
3619 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3620 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3621 s == NULL ? (char_u *)"" : s, first);
3622 vim_free(tofree);
3623}
3624
3625 static void
3626list_one_var_a(
3627 char *prefix,
3628 char_u *name,
3629 int type,
3630 char_u *string,
3631 int *first) // when TRUE clear rest of screen and set to FALSE
3632{
3633 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3634 msg_start();
3635 msg_puts(prefix);
3636 if (name != NULL) // "a:" vars don't have a name stored
3637 msg_puts((char *)name);
3638 msg_putchar(' ');
3639 msg_advance(22);
3640 if (type == VAR_NUMBER)
3641 msg_putchar('#');
3642 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3643 msg_putchar('*');
3644 else if (type == VAR_LIST)
3645 {
3646 msg_putchar('[');
3647 if (*string == '[')
3648 ++string;
3649 }
3650 else if (type == VAR_DICT)
3651 {
3652 msg_putchar('{');
3653 if (*string == '{')
3654 ++string;
3655 }
3656 else
3657 msg_putchar(' ');
3658
3659 msg_outtrans(string);
3660
3661 if (type == VAR_FUNC || type == VAR_PARTIAL)
3662 msg_puts("()");
3663 if (*first)
3664 {
3665 msg_clr_eos();
3666 *first = FALSE;
3667 }
3668}
3669
3670/*
3671 * Set variable "name" to value in "tv".
3672 * If the variable already exists, the value is updated.
3673 * Otherwise the variable is created.
3674 */
3675 void
3676set_var(
3677 char_u *name,
3678 typval_T *tv,
3679 int copy) // make copy of value in "tv"
3680{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003681 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003682}
3683
3684/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003685 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003686 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003687 * If the variable already exists and "is_const" is FALSE the value is updated.
3688 * Otherwise the variable is created.
3689 */
3690 void
3691set_var_const(
3692 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003693 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003694 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003695 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003696 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003697 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003698 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003699{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003700 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003701 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003702 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003703 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003704 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003705 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003706 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003707 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003709 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003710 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003711 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003712 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003713 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003714
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003715 if (sid != 0)
3716 {
3717 if (SCRIPT_ID_VALID(sid))
3718 ht = &SCRIPT_VARS(sid);
3719 varname = name;
3720 }
3721 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003722 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003723 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003724
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003725 if (in_vim9script() && is_export
3726 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3727 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003728 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003729 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003730 // In a vim9 autoload script an exported variable is put in the
3731 // global namespace with the autoload prefix.
3732 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003733 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003734 if (varname == NULL)
3735 goto failed;
3736 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003737 ht = &globvarht;
3738 }
3739 else
3740 ht = find_var_ht(name, &varname);
3741 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003742 if (ht == NULL || *varname == NUL)
3743 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003744 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003745 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003746 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003747 is_script_local = ht == get_script_local_ht() || sid != 0
3748 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003749
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003750 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003751 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003752 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003753 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003754 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003755 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003756 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003757 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003758 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003759 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003760 // Do not make g:var, w:var, b:var or t:var final.
3761 flags &= ~ASSIGN_FINAL;
3762
Bram Moolenaare535db82021-03-31 21:07:24 +02003763 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003764 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3765 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003766 // For "[a, _] = list" the underscore is ignored.
3767 if ((flags & ASSIGN_UNPACK) == 0)
3768 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003769 goto failed;
3770 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003773
Bram Moolenaar24e93162021-07-18 20:40:33 +02003774 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003775 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003776 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003777
Bram Moolenaar24e93162021-07-18 20:40:33 +02003778 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003779 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003780 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003781 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003782 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003783 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003784 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003786 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3787 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003788 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003789 if (!in_vim9script())
3790 {
3791 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3792 name);
3793 goto failed;
3794 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796
Bram Moolenaar993faa32022-02-21 15:59:11 +00003797 // Search in parent scope which is possible to reference from lambda
3798 if (di == NULL)
3799 di = find_var_in_scoped_ht(name, TRUE);
3800
3801 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3802 && var_wrong_func_name(name, di == NULL))
3803 goto failed;
3804
3805 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003806 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003807 // Destination is a bool and the value is not, but it can be
3808 // converted.
3809 CLEAR_FIELD(bool_tv);
3810 bool_tv.v_type = VAR_BOOL;
3811 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3812 tv = &bool_tv;
3813 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003814
Bram Moolenaar993faa32022-02-21 15:59:11 +00003815 if (di != NULL)
3816 {
3817 // Item already exists. Allowed to replace when reloading.
3818 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003819 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003820 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3821 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003822 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003823 emsg(_(e_cannot_modify_existing_variable));
3824 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003825 }
3826
Bram Moolenaar993faa32022-02-21 15:59:11 +00003827 if (is_script_local && vim9script
3828 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003829 {
3830 semsg(_(e_redefining_script_item_str), name);
3831 goto failed;
3832 }
3833
Bram Moolenaar993faa32022-02-21 15:59:11 +00003834 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003835 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003836 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003837 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003838
3839 if (sv != NULL)
3840 {
3841 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02003842 if (var_idx > 0)
3843 {
3844 where.wt_index = var_idx;
3845 where.wt_kind = WT_VARIABLE;
3846 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003847 if (check_script_var_type(sv, tv, name, where) == FAIL)
3848 goto failed;
3849 if (type == NULL)
3850 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003851 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003852 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003853 }
3854
Bram Moolenaar993faa32022-02-21 15:59:11 +00003855 if ((flags & ASSIGN_FOR_LOOP) == 0
Bram Moolenaar16d2c022023-06-05 19:46:18 +01003856 ? var_check_permission(di, name) == FAIL
3857 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003858 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003859 }
3860 else
3861 {
3862 // can only redefine once
3863 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003864
Bram Moolenaar993faa32022-02-21 15:59:11 +00003865 // A Vim9 script-local variable is also present in sn_all_vars
3866 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003867 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003868 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003869 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00003870 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003871 }
3872
Bram Moolenaar993faa32022-02-21 15:59:11 +00003873 // existing variable, need to clear the value
3874
3875 // Handle setting internal di: variables separately where needed to
3876 // prevent changing the type.
3877 if (ht == &vimvarht)
3878 {
3879 if (di->di_tv.v_type == VAR_STRING)
3880 {
3881 VIM_CLEAR(di->di_tv.vval.v_string);
3882 if (copy || tv->v_type != VAR_STRING)
3883 {
3884 char_u *val = tv_get_string(tv);
3885
3886 // Careful: when assigning to v:errmsg and
3887 // tv_get_string() causes an error message the variable
3888 // will already be set.
3889 if (di->di_tv.vval.v_string == NULL)
3890 di->di_tv.vval.v_string = vim_strsave(val);
3891 }
3892 else
3893 {
3894 // Take over the string to avoid an extra alloc/free.
3895 di->di_tv.vval.v_string = tv->vval.v_string;
3896 tv->vval.v_string = NULL;
3897 }
3898 goto failed;
3899 }
3900 else if (di->di_tv.v_type == VAR_NUMBER)
3901 {
3902 di->di_tv.vval.v_number = tv_get_number(tv);
3903 if (STRCMP(varname, "searchforward") == 0)
3904 set_search_direction(di->di_tv.vval.v_number
3905 ? '/' : '?');
3906#ifdef FEAT_SEARCH_EXTRA
3907 else if (STRCMP(varname, "hlsearch") == 0)
3908 {
3909 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaara4d158b2022-08-14 14:17:45 +01003910 redraw_all_later(UPD_SOME_VALID);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003911 }
3912#endif
3913 goto failed;
3914 }
3915 else if (di->di_tv.v_type != tv->v_type)
3916 {
3917 semsg(_(e_setting_str_to_value_with_wrong_type), name);
3918 goto failed;
3919 }
3920 }
3921
3922 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01003923
3924 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
3925 && SCRIPT_ID_VALID(current_sctx.sc_sid))
3926 {
3927 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3928
3929 update_script_var_block_id(name, si->sn_current_block_id);
3930 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003931 }
3932 else
3933 {
3934 // Item not found, check if a function already exists.
3935 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
3936 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
3937 {
3938 semsg(_(e_redefining_script_item_str), name);
3939 goto failed;
3940 }
3941
3942 // add a new variable
3943 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3944 {
3945 semsg(_(e_unknown_variable_str), name);
3946 goto failed;
3947 }
3948
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003949 if (check_hashtab_frozen(ht, "add variable"))
3950 goto failed;
3951
Bram Moolenaar993faa32022-02-21 15:59:11 +00003952 // Can't add "v:" or "a:" variable.
3953 if (ht == &vimvarht || ht == get_funccal_args_ht())
3954 {
3955 semsg(_(e_illegal_variable_name_str), name);
3956 goto failed;
3957 }
3958
3959 // Make sure the variable name is valid. In Vim9 script an
3960 // autoload variable must be prefixed with "g:" unless in an
3961 // autoload script.
3962 if (!valid_varname(varname, -1, !vim9script
3963 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
3964 goto failed;
3965
zeertzjq1b438a82023-02-01 13:11:15 +00003966 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003967 if (di == NULL)
3968 goto failed;
3969 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00003970 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00003971 {
3972 vim_free(di);
3973 goto failed;
3974 }
3975 di->di_flags = DI_FLAGS_ALLOC;
3976 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3977 di->di_flags |= DI_FLAGS_LOCK;
3978
3979 // A Vim9 script-local variable is also added to sn_all_vars and
3980 // sn_var_vals. It may set "type" from "tv".
3981 if (var_in_vim9script || var_in_autoload)
3982 update_vim9_script_var(TRUE, di,
3983 var_in_autoload ? name : di->di_key, flags,
3984 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003985 }
3986
Bram Moolenaar993faa32022-02-21 15:59:11 +00003987 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003988 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003989 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003990 else
3991 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003992 *dest_tv = *tv;
3993 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003994 init_tv(tv);
3995 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003996 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003997
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003998 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00003999 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004000
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004001 // ":const var = value" locks the value
4002 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004003 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004004 // Like :lockvar! name: lock the value and what it contains, but only
4005 // if the reference count is up to one. That locks only literal
4006 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004007 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004008
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004009failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004010 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004011 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004012 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004013}
4014
4015/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004016 * Check in this order for backwards compatibility:
4017 * - Whether the variable is read-only
4018 * - Whether the variable value is locked
4019 * - Whether the variable is locked
4020 */
4021 int
4022var_check_permission(dictitem_T *di, char_u *name)
4023{
4024 if (var_check_ro(di->di_flags, name, FALSE)
4025 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4026 || var_check_lock(di->di_flags, name, FALSE))
4027 return FAIL;
4028 return OK;
4029}
4030
4031/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004032 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4033 * Also give an error message.
4034 */
4035 int
4036var_check_ro(int flags, char_u *name, int use_gettext)
4037{
4038 if (flags & DI_FLAGS_RO)
4039 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004040 if (name == NULL)
4041 emsg(_(e_cannot_change_readonly_variable));
4042 else
4043 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004044 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004045 return TRUE;
4046 }
4047 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4048 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004049 if (name == NULL)
4050 emsg(_(e_cannot_set_variable_in_sandbox));
4051 else
4052 semsg(_(e_cannot_set_variable_in_sandbox_str),
4053 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004054 return TRUE;
4055 }
4056 return FALSE;
4057}
4058
4059/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004060 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4061 * Also give an error message.
4062 */
4063 int
4064var_check_lock(int flags, char_u *name, int use_gettext)
4065{
4066 if (flags & DI_FLAGS_LOCK)
4067 {
4068 semsg(_(e_variable_is_locked_str),
4069 use_gettext ? (char_u *)_(name) : name);
4070 return TRUE;
4071 }
4072 return FALSE;
4073}
4074
4075/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004076 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4077 * Also give an error message.
4078 */
4079 int
4080var_check_fixed(int flags, char_u *name, int use_gettext)
4081{
4082 if (flags & DI_FLAGS_FIX)
4083 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004084 if (name == NULL)
4085 emsg(_(e_cannot_delete_variable));
4086 else
4087 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004088 use_gettext ? (char_u *)_(name) : name);
4089 return TRUE;
4090 }
4091 return FALSE;
4092}
4093
4094/*
4095 * Check if a funcref is assigned to a valid variable name.
4096 * Return TRUE and give an error if not.
4097 */
4098 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004099var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004100 char_u *name, // points to start of variable name
4101 int new_var) // TRUE when creating the variable
4102{
Bram Moolenaar32154662021-03-28 21:14:06 +02004103 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4104 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004105 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004106 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4107 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004108 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004109 ? name[2] : name[0])
4110 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004111 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004112 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004113 return TRUE;
4114 }
4115 // Don't allow hiding a function. When "v" is not NULL we might be
4116 // assigning another function to the same var, the type is checked
4117 // below.
4118 if (new_var && function_exists(name, FALSE))
4119 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004120 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004121 name);
4122 return TRUE;
4123 }
4124 return FALSE;
4125}
4126
4127/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004128 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4129 * value. Also give an error message, using "name" or _("name") when
4130 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004131 */
4132 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004133value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004134{
4135 if (lock & VAR_LOCKED)
4136 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004137 if (name == NULL)
4138 emsg(_(e_value_is_locked));
4139 else
4140 semsg(_(e_value_is_locked_str),
4141 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004142 return TRUE;
4143 }
4144 if (lock & VAR_FIXED)
4145 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004146 if (name == NULL)
4147 emsg(_(e_cannot_change_value));
4148 else
4149 semsg(_(e_cannot_change_value_of_str),
4150 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004151 return TRUE;
4152 }
4153 return FALSE;
4154}
4155
4156/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004157 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004158 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004159 * Return FALSE and give an error if not.
4160 */
4161 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004162valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004163{
4164 char_u *p;
4165
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004166 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004167 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004168 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004169 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004170 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004171 return FALSE;
4172 }
4173 return TRUE;
4174}
4175
4176/*
LemonBoy47d4e312022-05-04 18:12:55 +01004177 * Implements the logic to retrieve local variable and option values.
4178 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004179 */
4180 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004181get_var_from(
4182 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004183 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004184 typval_T *deftv, // Default value if not found.
4185 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4186 tabpage_T *tp, // can be NULL
4187 win_T *win,
4188 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004189{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004190 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004191 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004192 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004193 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004194 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004195
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004196 ++emsg_off;
4197
4198 rettv->v_type = VAR_STRING;
4199 rettv->vval.v_string = NULL;
4200
LemonBoy47d4e312022-05-04 18:12:55 +01004201 if (varname != NULL && tp != NULL && win != NULL
4202 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004203 {
4204 // Set curwin to be our win, temporarily. Also set the tabpage,
4205 // otherwise the window is not valid. Only do this when needed,
4206 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004207 // If we have a buffer reference avoid the switching, we're saving and
4208 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004209 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004210 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004211 {
LemonBoy47d4e312022-05-04 18:12:55 +01004212 // Handle options. There are no tab-local options.
4213 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004214 {
LemonBoy47d4e312022-05-04 18:12:55 +01004215 buf_T *save_curbuf = curbuf;
4216
4217 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004218 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004219 curbuf = buf;
4220
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004221 if (varname[1] == NUL)
4222 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004223 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004224 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004225
4226 if (opts != NULL)
4227 {
4228 rettv_dict_set(rettv, opts);
4229 done = TRUE;
4230 }
4231 }
LemonBoy47d4e312022-05-04 18:12:55 +01004232 else if (eval_option(&varname, rettv, TRUE) == OK)
4233 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004234 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004235
4236 curbuf = save_curbuf;
4237 }
4238 else if (*varname == NUL)
4239 {
4240 // Empty string: return a dict with all the local variables.
4241 if (htname == 'b')
4242 v = &buf->b_bufvar;
4243 else if (htname == 'w')
4244 v = &win->w_winvar;
4245 else
4246 v = &tp->tp_winvar;
4247 copy_tv(&v->di_tv, rettv);
4248 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004249 }
4250 else
4251 {
LemonBoy47d4e312022-05-04 18:12:55 +01004252 hashtab_T *ht;
4253
4254 if (htname == 'b')
4255 ht = &buf->b_vars->dv_hashtab;
4256 else if (htname == 'w')
4257 ht = &win->w_vars->dv_hashtab;
4258 else
4259 ht = &tp->tp_vars->dv_hashtab;
4260
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004261 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004262 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004263 if (v != NULL)
4264 {
4265 copy_tv(&v->di_tv, rettv);
4266 done = TRUE;
4267 }
4268 }
4269 }
4270
4271 if (need_switch_win)
4272 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004273 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004274 }
4275
LemonBoy47d4e312022-05-04 18:12:55 +01004276 if (!done && deftv->v_type != VAR_UNKNOWN)
4277 // use the default value
4278 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004279
4280 --emsg_off;
4281}
4282
4283/*
LemonBoy47d4e312022-05-04 18:12:55 +01004284 * getwinvar() and gettabwinvar()
4285 */
4286 static void
4287getwinvar(
4288 typval_T *argvars,
4289 typval_T *rettv,
4290 int off) // 1 for gettabwinvar()
4291{
4292 char_u *varname;
4293 tabpage_T *tp;
4294 win_T *win;
4295
4296 if (off == 1)
4297 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4298 else
4299 tp = curtab;
4300 win = find_win_by_nr(&argvars[off], tp);
4301 varname = tv_get_string_chk(&argvars[off + 1]);
4302
4303 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4304}
4305
4306/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004307 * Set option "varname" to the value of "varp" for the current buffer/window.
4308 */
4309 static void
4310set_option_from_tv(char_u *varname, typval_T *varp)
4311{
4312 long numval = 0;
4313 char_u *strval;
4314 char_u nbuf[NUMBUFLEN];
4315 int error = FALSE;
4316
zeertzjq4c7cb372023-06-14 16:39:54 +01004317 int opt_idx = findoption(varname);
4318 if (opt_idx < 0)
4319 {
4320 semsg(_(e_unknown_option_str_2), varname);
4321 return;
4322 }
4323 int opt_p_flags = get_option_flags(opt_idx);
4324
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004325 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004326 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004327 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004328 {
4329 emsg(_(e_string_required));
4330 return;
4331 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004332 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004333 strval = (char_u *)"0"; // avoid using "false"
4334 }
4335 else
4336 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004337 if ((opt_p_flags & (P_NUM|P_BOOL))
4338 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004339 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004340 if (!error)
4341 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004342 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004343 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004344 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004345}
4346
4347/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004348 * "setwinvar()" and "settabwinvar()" functions
4349 */
4350 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004351setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004352{
4353 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004354 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004355 int need_switch_win;
4356 char_u *varname, *winvarname;
4357 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004358 tabpage_T *tp = NULL;
4359
4360 if (check_secure())
4361 return;
4362
4363 if (off == 1)
4364 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4365 else
4366 tp = curtab;
4367 win = find_win_by_nr(&argvars[off], tp);
4368 varname = tv_get_string_chk(&argvars[off + 1]);
4369 varp = &argvars[off + 2];
4370
zeertzjqea720ae2023-01-03 10:54:09 +00004371 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004372 return;
4373
4374 need_switch_win = !(tp == curtab && win == curwin);
4375 if (!need_switch_win
4376 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004377 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004378 if (*varname == '&')
4379 set_option_from_tv(varname + 1, varp);
4380 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004381 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004382 winvarname = alloc(STRLEN(varname) + 3);
4383 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004384 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004385 STRCPY(winvarname, "w:");
4386 STRCPY(winvarname + 2, varname);
4387 set_var(winvarname, varp, TRUE);
4388 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004389 }
4390 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004391 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004392 if (need_switch_win)
4393 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004394}
4395
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004396/*
4397 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4398 * v:option_type, and v:option_command.
4399 */
4400 void
4401reset_v_option_vars(void)
4402{
4403 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4404 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4405 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4406 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4407 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4408 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4409}
4410
4411/*
4412 * Add an assert error to v:errors.
4413 */
4414 void
4415assert_error(garray_T *gap)
4416{
4417 struct vimvar *vp = &vimvars[VV_ERRORS];
4418
Bram Moolenaard787e402021-12-24 21:36:12 +00004419 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004420 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004421 set_vim_var_list(VV_ERRORS, list_alloc());
4422 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4423}
4424
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004425 int
4426var_exists(char_u *var)
4427{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004428 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004429 char_u *name;
4430 char_u *tofree;
4431 typval_T tv;
4432 int len = 0;
4433 int n = FALSE;
4434
4435 // get_name_len() takes care of expanding curly braces
4436 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004437 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004438 if (len > 0)
4439 {
4440 if (tofree != NULL)
4441 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004442 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004443 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004444 if (n)
4445 {
4446 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004447 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004448 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4449 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004450 if (n)
4451 clear_tv(&tv);
4452 }
4453 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004454 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004455 n = FALSE;
4456
4457 vim_free(tofree);
4458 return n;
4459}
4460
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004461static lval_T *redir_lval = NULL;
4462#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4463static garray_T redir_ga; // only valid when redir_lval is not NULL
4464static char_u *redir_endp = NULL;
4465static char_u *redir_varname = NULL;
4466
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004467 int
4468alloc_redir_lval(void)
4469{
4470 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4471 if (redir_lval == NULL)
4472 return FAIL;
4473 return OK;
4474}
4475
4476 void
4477clear_redir_lval(void)
4478{
4479 VIM_CLEAR(redir_lval);
4480}
4481
4482 void
4483init_redir_ga(void)
4484{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004485 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004486}
4487
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004488/*
4489 * Start recording command output to a variable
4490 * When "append" is TRUE append to an existing variable.
4491 * Returns OK if successfully completed the setup. FAIL otherwise.
4492 */
4493 int
4494var_redir_start(char_u *name, int append)
4495{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004496 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004497 typval_T tv;
4498
4499 // Catch a bad name early.
4500 if (!eval_isnamec1(*name))
4501 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004502 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004503 return FAIL;
4504 }
4505
4506 // Make a copy of the name, it is used in redir_lval until redir ends.
4507 redir_varname = vim_strsave(name);
4508 if (redir_varname == NULL)
4509 return FAIL;
4510
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004511 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004512 {
4513 var_redir_stop();
4514 return FAIL;
4515 }
4516
4517 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004518 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004519
4520 // Parse the variable name (can be a dict or list entry).
4521 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4522 FNE_CHECK_START);
4523 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4524 {
4525 clear_lval(redir_lval);
4526 if (redir_endp != NULL && *redir_endp != NUL)
4527 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004528 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004529 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004530 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004531 redir_endp = NULL; // don't store a value, only cleanup
4532 var_redir_stop();
4533 return FAIL;
4534 }
4535
4536 // check if we can write to the variable: set it to or append an empty
4537 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004538 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004539 tv.v_type = VAR_STRING;
4540 tv.vval.v_string = (char_u *)"";
4541 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004542 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004543 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004544 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004545 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004546 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004547 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004548 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004549 {
4550 redir_endp = NULL; // don't store a value, only cleanup
4551 var_redir_stop();
4552 return FAIL;
4553 }
4554
4555 return OK;
4556}
4557
4558/*
4559 * Append "value[value_len]" to the variable set by var_redir_start().
4560 * The actual appending is postponed until redirection ends, because the value
4561 * appended may in fact be the string we write to, changing it may cause freed
4562 * memory to be used:
4563 * :redir => foo
4564 * :let foo
4565 * :redir END
4566 */
4567 void
4568var_redir_str(char_u *value, int value_len)
4569{
4570 int len;
4571
4572 if (redir_lval == NULL)
4573 return;
4574
4575 if (value_len == -1)
4576 len = (int)STRLEN(value); // Append the entire string
4577 else
4578 len = value_len; // Append only "value_len" characters
4579
4580 if (ga_grow(&redir_ga, len) == OK)
4581 {
4582 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4583 redir_ga.ga_len += len;
4584 }
4585 else
4586 var_redir_stop();
4587}
4588
4589/*
4590 * Stop redirecting command output to a variable.
4591 * Frees the allocated memory.
4592 */
4593 void
4594var_redir_stop(void)
4595{
4596 typval_T tv;
4597
4598 if (EVALCMD_BUSY)
4599 {
4600 redir_lval = NULL;
4601 return;
4602 }
4603
4604 if (redir_lval != NULL)
4605 {
4606 // If there was no error: assign the text to the variable.
4607 if (redir_endp != NULL)
4608 {
4609 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4610 tv.v_type = VAR_STRING;
4611 tv.vval.v_string = redir_ga.ga_data;
4612 // Call get_lval() again, if it's inside a Dict or List it may
4613 // have changed.
4614 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4615 FALSE, FALSE, 0, FNE_CHECK_START);
4616 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004617 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004618 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004619 clear_lval(redir_lval);
4620 }
4621
4622 // free the collected output
4623 VIM_CLEAR(redir_ga.ga_data);
4624
4625 VIM_CLEAR(redir_lval);
4626 }
4627 VIM_CLEAR(redir_varname);
4628}
4629
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004630/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004631 * Get the collected redirected text and clear redir_ga.
4632 */
4633 char_u *
4634get_clear_redir_ga(void)
4635{
4636 char_u *res;
4637
4638 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4639 res = redir_ga.ga_data;
4640 redir_ga.ga_data = NULL;
4641 return res;
4642}
4643
4644/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004645 * "gettabvar()" function
4646 */
4647 void
4648f_gettabvar(typval_T *argvars, typval_T *rettv)
4649{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004650 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004651 tabpage_T *tp;
4652 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004653
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004654 if (in_vim9script()
4655 && (check_for_number_arg(argvars, 0) == FAIL
4656 || check_for_string_arg(argvars, 1) == FAIL))
4657 return;
4658
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004659 varname = tv_get_string_chk(&argvars[1]);
4660 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004661 if (tp != NULL)
4662 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4663 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004664
LemonBoy47d4e312022-05-04 18:12:55 +01004665 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004666}
4667
4668/*
4669 * "gettabwinvar()" function
4670 */
4671 void
4672f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4673{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004674 if (in_vim9script()
4675 && (check_for_number_arg(argvars, 0) == FAIL
4676 || check_for_number_arg(argvars, 1) == FAIL
4677 || check_for_string_arg(argvars, 2) == FAIL))
4678 return;
4679
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004680 getwinvar(argvars, rettv, 1);
4681}
4682
4683/*
4684 * "getwinvar()" function
4685 */
4686 void
4687f_getwinvar(typval_T *argvars, typval_T *rettv)
4688{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004689 if (in_vim9script()
4690 && (check_for_number_arg(argvars, 0) == FAIL
4691 || check_for_string_arg(argvars, 1) == FAIL))
4692 return;
4693
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004694 getwinvar(argvars, rettv, 0);
4695}
4696
4697/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004698 * "getbufvar()" function
4699 */
4700 void
4701f_getbufvar(typval_T *argvars, typval_T *rettv)
4702{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004703 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004704 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004705
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004706 if (in_vim9script()
4707 && (check_for_buffer_arg(argvars, 0) == FAIL
4708 || check_for_string_arg(argvars, 1) == FAIL))
4709 return;
4710
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004711 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004712 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004713
LemonBoy47d4e312022-05-04 18:12:55 +01004714 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004715}
4716
4717/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004718 * "settabvar()" function
4719 */
4720 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004721f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004722{
4723 tabpage_T *save_curtab;
4724 tabpage_T *tp;
4725 char_u *varname, *tabvarname;
4726 typval_T *varp;
4727
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004728 if (check_secure())
4729 return;
4730
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004731 if (in_vim9script()
4732 && (check_for_number_arg(argvars, 0) == FAIL
4733 || check_for_string_arg(argvars, 1) == FAIL))
4734 return;
4735
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004736 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4737 varname = tv_get_string_chk(&argvars[1]);
4738 varp = &argvars[2];
4739
zeertzjqea720ae2023-01-03 10:54:09 +00004740 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004741 return;
4742
4743 save_curtab = curtab;
4744 goto_tabpage_tp(tp, FALSE, FALSE);
4745
4746 tabvarname = alloc(STRLEN(varname) + 3);
4747 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004748 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004749 STRCPY(tabvarname, "t:");
4750 STRCPY(tabvarname + 2, varname);
4751 set_var(tabvarname, varp, TRUE);
4752 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004753 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004754
4755 // Restore current tabpage
4756 if (valid_tabpage(save_curtab))
4757 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004758}
4759
4760/*
4761 * "settabwinvar()" function
4762 */
4763 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004764f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004765{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004766 if (in_vim9script()
4767 && (check_for_number_arg(argvars, 0) == FAIL
4768 || check_for_number_arg(argvars, 1) == FAIL
4769 || check_for_string_arg(argvars, 2) == FAIL))
4770 return;
4771
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004772 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004773}
4774
4775/*
4776 * "setwinvar()" function
4777 */
4778 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004779f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004780{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004781 if (in_vim9script()
4782 && (check_for_number_arg(argvars, 0) == FAIL
4783 || check_for_string_arg(argvars, 1) == FAIL))
4784 return;
4785
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004786 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004787}
4788
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004789/*
4790 * "setbufvar()" function
4791 */
4792 void
4793f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4794{
4795 buf_T *buf;
4796 char_u *varname, *bufvarname;
4797 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004798
4799 if (check_secure())
4800 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004801
4802 if (in_vim9script()
4803 && (check_for_buffer_arg(argvars, 0) == FAIL
4804 || check_for_string_arg(argvars, 1) == FAIL))
4805 return;
4806
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004807 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004808 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004809 varp = &argvars[2];
4810
zeertzjqea720ae2023-01-03 10:54:09 +00004811 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004812 return;
4813
4814 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004815 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004816 aco_save_T aco;
4817
4818 // Set curbuf to be our buf, temporarily.
4819 aucmd_prepbuf(&aco, buf);
4820 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004821 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004822 // Only when it worked to set "curbuf".
4823 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004824
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004825 // reset notion of buffer
4826 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004827 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004828 }
4829 else
4830 {
4831 bufvarname = alloc(STRLEN(varname) + 3);
4832 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004833 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004834 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004835
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004836 curbuf = buf;
4837 STRCPY(bufvarname, "b:");
4838 STRCPY(bufvarname + 2, varname);
4839 set_var(bufvarname, varp, TRUE);
4840 vim_free(bufvarname);
4841 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004842 }
4843 }
4844}
4845
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004846/*
4847 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004848 * When "arg" is zero "res.cb_name" is set to an empty string.
4849 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4850 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004851 */
4852 callback_T
4853get_callback(typval_T *arg)
4854{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004855 callback_T res;
4856 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004857
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004858 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004859 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4860 {
4861 res.cb_partial = arg->vval.v_partial;
4862 ++res.cb_partial->pt_refcount;
4863 res.cb_name = partial_name(res.cb_partial);
4864 }
4865 else
4866 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01004867 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4868 && isdigit(*arg->vval.v_string))
4869 r = FAIL;
4870 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004871 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004872 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004873 if (arg->v_type == VAR_STRING)
4874 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004875 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004876 if (name != NULL)
4877 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004878 res.cb_name = name;
4879 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004880 }
4881 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004882 func_ref(res.cb_name);
4883 }
4884 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004885 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004886 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004887 r = FAIL;
4888
4889 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004890 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004891 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004892 res.cb_name = NULL;
4893 }
4894 }
4895 return res;
4896}
4897
4898/*
4899 * Copy a callback into a typval_T.
4900 */
4901 void
4902put_callback(callback_T *cb, typval_T *tv)
4903{
4904 if (cb->cb_partial != NULL)
4905 {
4906 tv->v_type = VAR_PARTIAL;
4907 tv->vval.v_partial = cb->cb_partial;
4908 ++tv->vval.v_partial->pt_refcount;
4909 }
4910 else
4911 {
4912 tv->v_type = VAR_FUNC;
4913 tv->vval.v_string = vim_strsave(cb->cb_name);
4914 func_ref(cb->cb_name);
4915 }
4916}
4917
4918/*
4919 * Make a copy of "src" into "dest", allocating the function name if needed,
4920 * without incrementing the refcount.
4921 */
4922 void
4923set_callback(callback_T *dest, callback_T *src)
4924{
4925 if (src->cb_partial == NULL)
4926 {
4927 // just a function name, make a copy
4928 dest->cb_name = vim_strsave(src->cb_name);
4929 dest->cb_free_name = TRUE;
4930 }
4931 else
4932 {
4933 // cb_name is a pointer into cb_partial
4934 dest->cb_name = src->cb_name;
4935 dest->cb_free_name = FALSE;
4936 }
4937 dest->cb_partial = src->cb_partial;
4938}
4939
4940/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004941 * Copy callback from "src" to "dest", incrementing the refcounts.
4942 */
4943 void
4944copy_callback(callback_T *dest, callback_T *src)
4945{
4946 dest->cb_partial = src->cb_partial;
4947 if (dest->cb_partial != NULL)
4948 {
4949 dest->cb_name = src->cb_name;
4950 dest->cb_free_name = FALSE;
4951 ++dest->cb_partial->pt_refcount;
4952 }
4953 else
4954 {
4955 dest->cb_name = vim_strsave(src->cb_name);
4956 dest->cb_free_name = TRUE;
4957 func_ref(src->cb_name);
4958 }
4959}
4960
4961/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004962 * When a callback refers to an autoload import, change the function name to
4963 * the "path#name" form. Uses the current script context.
4964 * Only works when the name is allocated.
4965 */
4966 void
4967expand_autload_callback(callback_T *cb)
4968{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004969 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004970 char_u *p;
4971 imported_T *import;
4972
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004973 if (!in_vim9script() || cb->cb_name == NULL
4974 || (!cb->cb_free_name
4975 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004976 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00004977 if (cb->cb_partial != NULL)
4978 name = cb->cb_partial->pt_name;
4979 else
4980 name = cb->cb_name;
4981 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004982 if (p == NULL)
4983 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004984
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004985 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004986 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
4987 return;
4988
4989 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4990 if (si->sn_autoload_prefix == NULL)
4991 return;
4992
4993 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
4994 if (newname == NULL)
4995 return;
4996
4997 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004998 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004999 if (cb->cb_name == cb->cb_partial->pt_name)
5000 cb->cb_name = newname;
5001 vim_free(cb->cb_partial->pt_name);
5002 cb->cb_partial->pt_name = newname;
5003 }
5004 else
5005 {
5006 vim_free(cb->cb_name);
5007 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005008 }
5009}
5010
5011/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005012 * Unref/free "callback" returned by get_callback() or set_callback().
5013 */
5014 void
5015free_callback(callback_T *callback)
5016{
5017 if (callback->cb_partial != NULL)
5018 {
5019 partial_unref(callback->cb_partial);
5020 callback->cb_partial = NULL;
5021 }
5022 else if (callback->cb_name != NULL)
5023 func_unref(callback->cb_name);
5024 if (callback->cb_free_name)
5025 {
5026 vim_free(callback->cb_name);
5027 callback->cb_free_name = FALSE;
5028 }
5029 callback->cb_name = NULL;
5030}
5031
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005032#endif // FEAT_EVAL