blob: fc977c66aeb085cf030b21938dd2bcd291fbec6e [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000142 {VV_NAME("t_class", VAR_NUMBER), NULL, VV_RO},
143 {VV_NAME("t_object", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaard787e402021-12-24 21:36:12 +0000144 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
148 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
149 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
150 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
151 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
152 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
153 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
154 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
155 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
156 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
157 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
158 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000159 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200160 {VV_NAME("python3_version", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200161};
162
163// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000164#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200165#define vv_nr vv_di.di_tv.vval.v_number
166#define vv_float vv_di.di_tv.vval.v_float
167#define vv_str vv_di.di_tv.vval.v_string
168#define vv_list vv_di.di_tv.vval.v_list
169#define vv_dict vv_di.di_tv.vval.v_dict
170#define vv_blob vv_di.di_tv.vval.v_blob
171#define vv_tv vv_di.di_tv
172
173static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200174static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200175#define vimvarht vimvardict.dv_hashtab
176
177// for VIM_VERSION_ defines
178#include "version.h"
179
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200180static void list_glob_vars(int *first);
181static void list_buf_vars(int *first);
182static void list_win_vars(int *first);
183static void list_tab_vars(int *first);
184static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100185static 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 +0200186static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
187static 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 +0200188static void list_one_var(dictitem_T *v, char *prefix, int *first);
189static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
190
191/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200192 * Initialize global and vim special variables
193 */
194 void
195evalvars_init(void)
196{
197 int i;
198 struct vimvar *p;
199
200 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
201 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
202 vimvardict.dv_lock = VAR_FIXED;
203 hash_init(&compat_hashtab);
204
205 for (i = 0; i < VV_LEN; ++i)
206 {
207 p = &vimvars[i];
208 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
209 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100210 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200211 getout(1);
212 }
213 STRCPY(p->vv_di.di_key, p->vv_name);
214 if (p->vv_flags & VV_RO)
215 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
216 else if (p->vv_flags & VV_RO_SBX)
217 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
218 else
219 p->vv_di.di_flags = DI_FLAGS_FIX;
220
221 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000222 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000223 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200224 if (p->vv_flags & VV_COMPAT)
225 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000226 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200228 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
229 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200230
231 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
232 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100233 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200234 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
235 set_vim_var_list(VV_ERRORS, list_alloc());
236 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
237
238 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
239 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
240 set_vim_var_nr(VV_NONE, VVAL_NONE);
241 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100242 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
243 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100244 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000245 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
246 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
247 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000248 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200249
250 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
251 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
252 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
253 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
254 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
255 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
256 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
257 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
258 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
259 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
260 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000261 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
262 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200263
264 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
265
Drew Vogele30d1022021-10-24 20:35:07 +0100266 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
267
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200268#ifdef FEAT_PYTHON3
269 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
270#endif
271
Bram Moolenaar439c0362020-06-06 15:58:03 +0200272 // Default for v:register is not 0 but '"'. This is adjusted once the
273 // clipboard has been setup by calling reset_reg_var().
274 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200275}
276
277#if defined(EXITFREE) || defined(PROTO)
278/*
279 * Free all vim variables information on exit
280 */
281 void
282evalvars_clear(void)
283{
284 int i;
285 struct vimvar *p;
286
287 for (i = 0; i < VV_LEN; ++i)
288 {
289 p = &vimvars[i];
290 if (p->vv_di.di_tv.v_type == VAR_STRING)
291 VIM_CLEAR(p->vv_str);
292 else if (p->vv_di.di_tv.v_type == VAR_LIST)
293 {
294 list_unref(p->vv_list);
295 p->vv_list = NULL;
296 }
297 }
298 hash_clear(&vimvarht);
299 hash_init(&vimvarht); // garbage_collect() will access it
300 hash_clear(&compat_hashtab);
301
302 // global variables
303 vars_clear(&globvarht);
304
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100305 // Script-local variables. Clear all the variables here.
306 // The scriptvar_T is cleared later in free_scriptnames(), because a
307 // variable in one script might hold a reference to the whole scope of
308 // another script.
309 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200310 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200311}
312#endif
313
314 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200315garbage_collect_globvars(int copyID)
316{
317 return set_ref_in_ht(&globvarht, copyID, NULL);
318}
319
320 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200321garbage_collect_vimvars(int copyID)
322{
323 return set_ref_in_ht(&vimvarht, copyID, NULL);
324}
325
326 int
327garbage_collect_scriptvars(int copyID)
328{
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 int i;
330 int idx;
331 int abort = FALSE;
332 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200333
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100334 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200335 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200336 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
337
Bram Moolenaared234f22020-10-15 20:42:20 +0200338 si = SCRIPT_ITEM(i);
339 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
340 {
341 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
342
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100343 if (sv->sv_name != NULL)
344 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200345 }
346 }
347
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200348 return abort;
349}
350
351/*
352 * Set an internal variable to a string value. Creates the variable if it does
353 * not already exist.
354 */
355 void
356set_internal_string_var(char_u *name, char_u *value)
357{
358 char_u *val;
359 typval_T *tvp;
360
361 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000362 if (val == NULL)
363 return;
364
365 tvp = alloc_string_tv(val);
366 if (tvp == NULL)
367 return;
368
369 set_var(name, tvp, FALSE);
370 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200371}
372
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200373 int
374eval_charconvert(
375 char_u *enc_from,
376 char_u *enc_to,
377 char_u *fname_from,
378 char_u *fname_to)
379{
380 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000381 sctx_T saved_sctx = current_sctx;
382 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200383
384 set_vim_var_string(VV_CC_FROM, enc_from, -1);
385 set_vim_var_string(VV_CC_TO, enc_to, -1);
386 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
387 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000388 ctx = get_option_sctx("charconvert");
389 if (ctx != NULL)
390 current_sctx = *ctx;
391
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100392 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200393 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000394
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200395 set_vim_var_string(VV_CC_FROM, NULL, -1);
396 set_vim_var_string(VV_CC_TO, NULL, -1);
397 set_vim_var_string(VV_FNAME_IN, NULL, -1);
398 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000399 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200400
401 if (err)
402 return FAIL;
403 return OK;
404}
405
406# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
407 int
408eval_printexpr(char_u *fname, char_u *args)
409{
410 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000411 sctx_T saved_sctx = current_sctx;
412 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200413
414 set_vim_var_string(VV_FNAME_IN, fname, -1);
415 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000416 ctx = get_option_sctx("printexpr");
417 if (ctx != NULL)
418 current_sctx = *ctx;
419
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100420 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200421 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000422
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200423 set_vim_var_string(VV_FNAME_IN, NULL, -1);
424 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000425 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200426
427 if (err)
428 {
429 mch_remove(fname);
430 return FAIL;
431 }
432 return OK;
433}
434# endif
435
436# if defined(FEAT_DIFF) || defined(PROTO)
437 void
438eval_diff(
439 char_u *origfile,
440 char_u *newfile,
441 char_u *outfile)
442{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000443 sctx_T saved_sctx = current_sctx;
444 sctx_T *ctx;
445 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200446
447 set_vim_var_string(VV_FNAME_IN, origfile, -1);
448 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
449 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000450
451 ctx = get_option_sctx("diffexpr");
452 if (ctx != NULL)
453 current_sctx = *ctx;
454
455 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100456 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000457 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000458
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200459 set_vim_var_string(VV_FNAME_IN, NULL, -1);
460 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
461 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000462 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200463}
464
465 void
466eval_patch(
467 char_u *origfile,
468 char_u *difffile,
469 char_u *outfile)
470{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000471 sctx_T saved_sctx = current_sctx;
472 sctx_T *ctx;
473 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200474
475 set_vim_var_string(VV_FNAME_IN, origfile, -1);
476 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
477 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000478
479 ctx = get_option_sctx("patchexpr");
480 if (ctx != NULL)
481 current_sctx = *ctx;
482
483 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100484 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000485 free_tv(tv);
486
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200487 set_vim_var_string(VV_FNAME_IN, NULL, -1);
488 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
489 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000490 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200491}
492# endif
493
494#if defined(FEAT_SPELL) || defined(PROTO)
495/*
496 * Evaluate an expression to a list with suggestions.
497 * For the "expr:" part of 'spellsuggest'.
498 * Returns NULL when there is an error.
499 */
500 list_T *
501eval_spell_expr(char_u *badword, char_u *expr)
502{
503 typval_T save_val;
504 typval_T rettv;
505 list_T *list = NULL;
506 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000507 sctx_T saved_sctx = current_sctx;
508 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100509 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200510
511 // Set "v:val" to the bad word.
512 prepare_vimvar(VV_VAL, &save_val);
513 set_vim_var_string(VV_VAL, badword, -1);
514 if (p_verbose == 0)
515 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000516 ctx = get_option_sctx("spellsuggest");
517 if (ctx != NULL)
518 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200519
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100520 r = may_call_simple_func(p, &rettv);
521 if (r == NOTDONE)
522 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
523 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200524 {
525 if (rettv.v_type != VAR_LIST)
526 clear_tv(&rettv);
527 else
528 list = rettv.vval.v_list;
529 }
530
531 if (p_verbose == 0)
532 --emsg_off;
533 clear_tv(get_vim_var_tv(VV_VAL));
534 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000535 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200536
537 return list;
538}
539
540/*
541 * "list" is supposed to contain two items: a word and a number. Return the
542 * word in "pp" and the number as the return value.
543 * Return -1 if anything isn't right.
544 * Used to get the good word and score from the eval_spell_expr() result.
545 */
546 int
547get_spellword(list_T *list, char_u **pp)
548{
549 listitem_T *li;
550
551 li = list->lv_first;
552 if (li == NULL)
553 return -1;
554 *pp = tv_get_string(&li->li_tv);
555
556 li = li->li_next;
557 if (li == NULL)
558 return -1;
559 return (int)tv_get_number(&li->li_tv);
560}
561#endif
562
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200563/*
564 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200565 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200566 * When not used yet add the variable to the v: hashtable.
567 */
568 void
569prepare_vimvar(int idx, typval_T *save_tv)
570{
571 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200572 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000573 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000574 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200575}
576
577/*
578 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200579 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200580 * When no longer defined, remove the variable from the v: hashtable.
581 */
582 void
583restore_vimvar(int idx, typval_T *save_tv)
584{
585 hashitem_T *hi;
586
587 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000588 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
589 return;
590
591 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
592 if (HASHITEM_EMPTY(hi))
593 internal_error("restore_vimvar()");
594 else
595 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200596}
597
598/*
599 * List Vim variables.
600 */
601 static void
602list_vim_vars(int *first)
603{
604 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
605}
606
607/*
608 * List script-local variables, if there is a script.
609 */
610 static void
611list_script_vars(int *first)
612{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200613 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200614 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
615 "s:", FALSE, first);
616}
617
618/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100619 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
620 * But only when an identifier character follows.
621 */
622 int
623is_scoped_variable(char_u *name)
624{
625 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
626 && name[1] == ':'
627 && eval_isnamec(name[2]);
628}
629
630/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100631 * Evaluate one Vim expression {expr} in string "p" and append the
632 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100633 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100634 * Return a pointer to the character after "}", NULL for an error.
635 */
636 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100637eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100638{
639 char_u *block_start = skipwhite(p + 1); // skip the opening {
640 char_u *block_end = block_start;
641 char_u *expr_val;
642
643 if (*block_start == NUL)
644 {
645 semsg(_(e_missing_close_curly_str), p);
646 return NULL;
647 }
648 if (skip_expr(&block_end, NULL) == FAIL)
649 return NULL;
650 block_end = skipwhite(block_end);
651 if (*block_end != '}')
652 {
653 semsg(_(e_missing_close_curly_str), p);
654 return NULL;
655 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100656 if (evaluate)
657 {
658 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100659 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100660 *block_end = '}';
661 if (expr_val == NULL)
662 return NULL;
663 ga_concat(gap, expr_val);
664 vim_free(expr_val);
665 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100666
667 return block_end + 1;
668}
669
670/*
671 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
672 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
673 * Used for a heredoc assignment.
674 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100675 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100676 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100677eval_all_expr_in_str(char_u *str)
678{
679 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100680 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100681
682 ga_init2(&ga, 1, 80);
683 p = str;
684
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100685 while (*p != NUL)
686 {
LemonBoy2eaef102022-05-06 13:14:50 +0100687 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100688 int escaped_brace = FALSE;
689
690 // Look for a block start.
691 lit_start = p;
692 while (*p != '{' && *p != '}' && *p != NUL)
693 ++p;
694
695 if (*p != NUL && *p == p[1])
696 {
697 // Escaped brace, unescape and continue.
698 // Include the brace in the literal string.
699 ++p;
700 escaped_brace = TRUE;
701 }
702 else if (*p == '}')
703 {
704 semsg(_(e_stray_closing_curly_str), str);
705 ga_clear(&ga);
706 return NULL;
707 }
708
709 // Append the literal part.
710 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
711
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100712 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100713 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100714
LemonBoy2eaef102022-05-06 13:14:50 +0100715 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100716 {
LemonBoy2eaef102022-05-06 13:14:50 +0100717 // Skip the second brace.
718 ++p;
719 continue;
720 }
721
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100722 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100723 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100724 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100725 {
726 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100727 return NULL;
728 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100729 }
730 ga_append(&ga, NUL);
731
732 return ga.ga_data;
733}
734
735/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200736 * Get a list of lines from a HERE document. The here document is a list of
737 * lines surrounded by a marker.
738 * cmd << {marker}
739 * {line1}
740 * {line2}
741 * ....
742 * {marker}
743 *
744 * The {marker} is a string. If the optional 'trim' word is supplied before the
745 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100746 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200747 *
748 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100749 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200750 * missing, then '.' is accepted as a marker.
751 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100752 * When compiling a heredoc assignment to a variable in a Vim9 def function,
753 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
754 * string values from the heredoc, vim9 instructions are generated. On success
755 * the returned list will be empty.
756 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100757 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200758 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100760heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200761{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100762 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200763 char_u *marker;
764 list_T *l;
765 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100766 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767 int marker_indent_len = 0;
768 int text_indent_len = 0;
769 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200770 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200771 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100772 int evalstr = FALSE;
773 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100774 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
775 int count = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200776
777 if (eap->getline == NULL)
778 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000779 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200780 return NULL;
781 }
782
783 // Check for the optional 'trim' word before the marker
784 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100786 while (TRUE)
787 {
788 if (STRNCMP(cmd, "trim", 4) == 0
789 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100791 cmd = skipwhite(cmd + 4);
792
793 // Trim the indentation from all the lines in the here document.
794 // The amount of indentation trimmed is the same as the indentation
795 // of the first line after the :let command line. To find the end
796 // marker the indent of the :let command line is trimmed.
797 p = *eap->cmdlinep;
798 while (VIM_ISWHITE(*p))
799 {
800 p++;
801 marker_indent_len++;
802 }
803 text_indent_len = -1;
804
805 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200806 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100807 if (STRNCMP(cmd, "eval", 4) == 0
808 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
809 {
810 cmd = skipwhite(cmd + 4);
811 evalstr = TRUE;
812 continue;
813 }
814 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 }
816
817 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200818 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200819 {
820 marker = skipwhite(cmd);
821 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200822 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200823 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000824 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 return NULL;
826 }
827 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200828 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200829 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000830 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200831 return NULL;
832 }
833 }
834 else
835 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200836 // When getting lines for an embedded script, if the marker is missing,
837 // accept '.' as the marker.
838 if (script_get)
839 marker = dot;
840 else
841 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000842 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200843 return NULL;
844 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200845 }
846
847 l = list_alloc();
848 if (l == NULL)
849 return NULL;
850
851 for (;;)
852 {
853 int mi = 0;
854 int ti = 0;
855
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100856 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200857 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
858 if (theline == NULL)
859 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000860 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200861 break;
862 }
863
864 // with "trim": skip the indent matching the :let line to find the
865 // marker
866 if (marker_indent_len > 0
867 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
868 mi = marker_indent_len;
869 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200870 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200871
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100872 // If expression evaluation failed in the heredoc, then skip till the
873 // end marker.
874 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100875 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100876
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200877 if (text_indent_len == -1 && *theline != NUL)
878 {
879 // set the text indent from the first line.
880 p = theline;
881 text_indent_len = 0;
882 while (VIM_ISWHITE(*p))
883 {
884 p++;
885 text_indent_len++;
886 }
887 text_indent = vim_strnsave(theline, text_indent_len);
888 }
889 // with "trim": skip the indent matching the first line
890 if (text_indent != NULL)
891 for (ti = 0; ti < text_indent_len; ++ti)
892 if (theline[ti] != text_indent[ti])
893 break;
894
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100895 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100896 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100897 {
LemonBoy2eaef102022-05-06 13:14:50 +0100898 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100899 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100900 vim_free(theline);
901 vim_free(text_indent);
902 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100903 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100904 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100905 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100906 else
907 {
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100908 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100909 {
910 str = eval_all_expr_in_str(str);
911 if (str == NULL)
912 {
913 // expression evaluation failed
914 eval_failed = TRUE;
915 continue;
916 }
917 vim_free(theline);
918 theline = str;
919 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100920
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100921 if (list_append_string(l, str, -1) == FAIL)
922 break;
923 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200924 }
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100925 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200926 vim_free(text_indent);
927
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100928 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
929 generate_NEWLIST(cctx, count, FALSE);
930
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100931 if (eval_failed)
932 {
933 // expression evaluation in the heredoc failed
934 list_free(l);
935 return NULL;
936 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200937 return l;
938}
939
940/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200941 * Vim9 variable declaration:
942 * ":var name"
943 * ":var name: type"
944 * ":var name = expr"
945 * ":var name: type = expr"
946 * etc.
947 */
948 void
949ex_var(exarg_T *eap)
950{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000951 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +0000952 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +0000953
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200954 if (!in_vim9script())
955 {
956 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
957 return;
958 }
Bram Moolenaare1d12112022-03-05 11:37:48 +0000959 has_var = checkforcmd_noparen(&p, "var", 3);
960 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +0000961 {
962 emsg(_(e_cannot_declare_variable_on_command_line));
963 return;
964 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200965 ex_let(eap);
966}
967
968/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969 * ":let" list all variable values
970 * ":let var1 var2" list variable values
971 * ":let var = expr" assignment command.
972 * ":let var += expr" assignment command.
973 * ":let var -= expr" assignment command.
974 * ":let var *= expr" assignment command.
975 * ":let var /= expr" assignment command.
976 * ":let var %= expr" assignment command.
977 * ":let var .= expr" assignment command.
978 * ":let var ..= expr" assignment command.
979 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100981 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200982 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200983 * ":final var = expr" assignment command.
984 * ":final [var1, var2] = expr" unpack list.
985 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200986 * ":const" list all variable values
987 * ":const var1 var2" list variable values
988 * ":const var = expr" assignment command.
989 * ":const [var1, var2] = expr" unpack list.
990 */
991 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200992ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200993{
994 char_u *arg = eap->arg;
995 char_u *expr = NULL;
996 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200997 int var_count = 0;
998 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200999 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001000 char_u *argend;
1001 int first = TRUE;
1002 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001003 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001004 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001005 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001007 if (eap->cmdidx == CMD_final && !vim9script)
1008 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001009 // In legacy Vim script ":final" is short for ":finally".
1010 ex_finally(eap);
1011 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001012 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001013 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001014 {
1015 emsg(_(e_cannot_use_let_in_vim9_script));
1016 return;
1017 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001018
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001019 if (eap->cmdidx == CMD_const)
1020 flags |= ASSIGN_CONST;
1021 else if (eap->cmdidx == CMD_final)
1022 flags |= ASSIGN_FINAL;
1023
1024 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001026 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001028 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001029 if (argend == NULL)
1030 return;
1031 if (argend > arg && argend[-1] == '.') // for var.='str'
1032 --argend;
1033 expr = skipwhite(argend);
1034 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001035 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001036 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001037 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1038 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001039 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001040 {
1041 // ":let" without "=": list variables
1042 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001043 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001044 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001045 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001046 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001047 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001048 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001049 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001050 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001051 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001052 else
1053 // Vim9 declaration ":var name: type"
1054 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001055 }
1056 else
1057 {
1058 // ":let var1 var2" - list values
1059 arg = list_arg_vars(eap, arg, &first);
1060 }
1061 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062 else if (!eap->skip)
1063 {
1064 // ":let"
1065 list_glob_vars(&first);
1066 list_buf_vars(&first);
1067 list_win_vars(&first);
1068 list_tab_vars(&first);
1069 list_script_vars(&first);
1070 list_func_vars(&first);
1071 list_vim_vars(&first);
1072 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001073 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001074 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075 }
zeertzjq474891b2023-04-12 21:36:03 +01001076
1077 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001078 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001079 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001080 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001081
Bram Moolenaard9876422022-10-12 12:58:54 +01001082 // :let text =<< [trim] [eval] END
1083 // :var text =<< [trim] [eval] END
1084 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1085 || !IS_WHITE_OR_NUL(expr[3])))
1086 semsg(_(e_white_space_required_before_and_after_str_at_str),
1087 "=<<", expr);
1088 else
1089 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1090
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001091 if (l != NULL)
1092 {
1093 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001094 if (!eap->skip)
1095 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001096 // errors are for the assignment, not the end marker
1097 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001098 op[0] = '=';
1099 op[1] = NUL;
1100 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001102 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001103 clear_tv(&rettv);
1104 }
zeertzjq474891b2023-04-12 21:36:03 +01001105 return;
1106 }
1107
1108 evalarg_T evalarg;
1109 int len = 1;
1110
1111 CLEAR_FIELD(rettv);
1112
1113 int cur_lnum;
1114
1115 op[0] = '=';
1116 op[1] = NUL;
1117 if (*expr != '=')
1118 {
1119 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1120 {
1121 // +=, /=, etc. require an existing variable
1122 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1123 }
1124 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1125 {
1126 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1127 ++len;
1128 if (expr[0] == '.' && expr[1] == '.') // ..=
1129 {
1130 ++expr;
1131 ++len;
1132 }
1133 }
1134 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001135 }
1136 else
zeertzjq474891b2023-04-12 21:36:03 +01001137 ++expr;
1138
1139 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1140 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001141 {
zeertzjq474891b2023-04-12 21:36:03 +01001142 vim_strncpy(op, expr - len, len);
1143 semsg(_(e_white_space_required_before_and_after_str_at_str),
1144 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001145 }
zeertzjq474891b2023-04-12 21:36:03 +01001146
1147 if (eap->skip)
1148 ++emsg_skip;
1149 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1150 expr = skipwhite_and_linebreak(expr, &evalarg);
1151 cur_lnum = SOURCING_LNUM;
1152 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1153 if (eap->skip)
1154 --emsg_skip;
1155 clear_evalarg(&evalarg, eap);
1156
1157 // Restore the line number so that any type error is given for the
1158 // declaration, not the expression.
1159 SOURCING_LNUM = cur_lnum;
1160
1161 if (!eap->skip && eval_res != FAIL)
1162 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1163 flags, op);
1164 if (eval_res != FAIL)
1165 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001166}
1167
1168/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001169 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001170 * Handles both "var" with any type and "[var, var; var]" with a list type.
1171 * When "op" is not NULL it points to a string with characters that
1172 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1173 * or concatenate.
1174 * Returns OK or FAIL;
1175 */
1176 int
1177ex_let_vars(
1178 char_u *arg_start,
1179 typval_T *tv,
1180 int copy, // copy values from "tv", don't move
1181 int semicolon, // from skip_var_list()
1182 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001183 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001184 char_u *op)
1185{
1186 char_u *arg = arg_start;
1187 list_T *l;
1188 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001189 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001190 listitem_T *item;
1191 typval_T ltv;
1192
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001193 if (tv->v_type == VAR_VOID)
1194 {
1195 emsg(_(e_cannot_use_void_value));
1196 return FAIL;
1197 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 if (*arg != '[')
1199 {
1200 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001201 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 return FAIL;
1203 return OK;
1204 }
1205
1206 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1207 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1208 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001209 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 return FAIL;
1211 }
1212
1213 i = list_len(l);
1214 if (semicolon == 0 && var_count < i)
1215 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001216 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001217 return FAIL;
1218 }
1219 if (var_count - semicolon > i)
1220 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001221 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001222 return FAIL;
1223 }
1224
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001225 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001226 item = l->lv_first;
1227 while (*arg != ']')
1228 {
1229 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001230 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001231 arg = ex_let_one(arg, &item->li_tv, TRUE,
1232 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 item = item->li_next;
1234 if (arg == NULL)
1235 return FAIL;
1236
1237 arg = skipwhite(arg);
1238 if (*arg == ';')
1239 {
1240 // Put the rest of the list (may be empty) in the var after ';'.
1241 // Create a new list for this.
1242 l = list_alloc();
1243 if (l == NULL)
1244 return FAIL;
1245 while (item != NULL)
1246 {
1247 list_append_tv(l, &item->li_tv);
1248 item = item->li_next;
1249 }
1250
1251 ltv.v_type = VAR_LIST;
1252 ltv.v_lock = 0;
1253 ltv.vval.v_list = l;
1254 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001255 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001256
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001257 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1258 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001259 clear_tv(&ltv);
1260 if (arg == NULL)
1261 return FAIL;
1262 break;
1263 }
1264 else if (*arg != ',' && *arg != ']')
1265 {
1266 internal_error("ex_let_vars()");
1267 return FAIL;
1268 }
1269 }
1270
1271 return OK;
1272}
1273
1274/*
1275 * Skip over assignable variable "var" or list of variables "[var, var]".
1276 * Used for ":let varvar = expr" and ":for varvar in expr".
1277 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001278 * for "[var, var; var]" set "semicolon" to 1.
1279 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001280 * Return NULL for an error.
1281 */
1282 char_u *
1283skip_var_list(
1284 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001285 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001287 int *semicolon,
1288 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289{
1290 char_u *p, *s;
1291
1292 if (*arg == '[')
1293 {
1294 // "[var, var]": find the matching ']'.
1295 p = arg;
1296 for (;;)
1297 {
1298 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001299 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001300 if (s == p)
1301 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001302 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001303 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304 return NULL;
1305 }
1306 ++*var_count;
1307
1308 p = skipwhite(s);
1309 if (*p == ']')
1310 break;
1311 else if (*p == ';')
1312 {
1313 if (*semicolon == 1)
1314 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001315 if (!silent)
1316 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001317 return NULL;
1318 }
1319 *semicolon = 1;
1320 }
1321 else if (*p != ',')
1322 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001323 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001324 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001325 return NULL;
1326 }
1327 }
1328 return p + 1;
1329 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001330
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001331 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001332}
1333
1334/*
1335 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1336 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001338 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001339 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001341{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001342 char_u *end;
1343 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345 if (*arg == '@' && arg[1] != NUL)
1346 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001347
1348 // termcap option name may have non-alpha characters
1349 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1350 return arg + 5;
1351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001353 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001354
1355 // "a: type" is declaring variable "a" with a type, not "a:".
1356 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001357 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001358 --end;
1359
Bram Moolenaar585587d2021-01-17 20:52:13 +01001360 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001362 if (*skipwhite(end) == ':')
1363 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 }
1365 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366}
1367
1368/*
1369 * List variables for hashtab "ht" with prefix "prefix".
1370 * If "empty" is TRUE also list NULL strings as empty strings.
1371 */
1372 void
1373list_hashtable_vars(
1374 hashtab_T *ht,
1375 char *prefix,
1376 int empty,
1377 int *first)
1378{
1379 hashitem_T *hi;
1380 dictitem_T *di;
1381 int todo;
1382 char_u buf[IOSIZE];
1383
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001384 int save_ht_flags = ht->ht_flags;
1385 ht->ht_flags |= HTFLAGS_FROZEN;
1386
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001387 todo = (int)ht->ht_used;
1388 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1389 {
1390 if (!HASHITEM_EMPTY(hi))
1391 {
1392 --todo;
1393 di = HI2DI(hi);
1394
1395 // apply :filter /pat/ to variable name
1396 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1397 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1398 if (message_filtered(buf))
1399 continue;
1400
1401 if (empty || di->di_tv.v_type != VAR_STRING
1402 || di->di_tv.vval.v_string != NULL)
1403 list_one_var(di, prefix, first);
1404 }
1405 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001406
1407 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408}
1409
1410/*
1411 * List global variables.
1412 */
1413 static void
1414list_glob_vars(int *first)
1415{
1416 list_hashtable_vars(&globvarht, "", TRUE, first);
1417}
1418
1419/*
1420 * List buffer variables.
1421 */
1422 static void
1423list_buf_vars(int *first)
1424{
1425 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1426}
1427
1428/*
1429 * List window variables.
1430 */
1431 static void
1432list_win_vars(int *first)
1433{
1434 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1435}
1436
1437/*
1438 * List tab page variables.
1439 */
1440 static void
1441list_tab_vars(int *first)
1442{
1443 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1444}
1445
1446/*
1447 * List variables in "arg".
1448 */
1449 static char_u *
1450list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1451{
1452 int error = FALSE;
1453 int len;
1454 char_u *name;
1455 char_u *name_start;
1456 char_u *arg_subsc;
1457 char_u *tofree;
1458 typval_T tv;
1459
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001460 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 {
1462 if (error || eap->skip)
1463 {
1464 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1465 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1466 {
1467 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001468 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001469 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470 break;
1471 }
1472 }
1473 else
1474 {
1475 // get_name_len() takes care of expanding curly braces
1476 name_start = name = arg;
1477 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1478 if (len <= 0)
1479 {
1480 // This is mainly to keep test 49 working: when expanding
1481 // curly braces fails overrule the exception error message.
1482 if (len < 0 && !aborting())
1483 {
1484 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001485 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001486 break;
1487 }
1488 error = TRUE;
1489 }
1490 else
1491 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001492 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001493 if (tofree != NULL)
1494 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001495 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001496 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001497 error = TRUE;
1498 else
1499 {
1500 // handle d.key, l[idx], f(expr)
1501 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001502 if (handle_subscript(&arg, name_start, &tv,
1503 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504 error = TRUE;
1505 else
1506 {
1507 if (arg == arg_subsc && len == 2 && name[1] == ':')
1508 {
1509 switch (*name)
1510 {
1511 case 'g': list_glob_vars(first); break;
1512 case 'b': list_buf_vars(first); break;
1513 case 'w': list_win_vars(first); break;
1514 case 't': list_tab_vars(first); break;
1515 case 'v': list_vim_vars(first); break;
1516 case 's': list_script_vars(first); break;
1517 case 'l': list_func_vars(first); break;
1518 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001519 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001520 }
1521 }
1522 else
1523 {
1524 char_u numbuf[NUMBUFLEN];
1525 char_u *tf;
1526 int c;
1527 char_u *s;
1528
1529 s = echo_string(&tv, &tf, numbuf, 0);
1530 c = *arg;
1531 *arg = NUL;
1532 list_one_var_a("",
1533 arg == arg_subsc ? name : name_start,
1534 tv.v_type,
1535 s == NULL ? (char_u *)"" : s,
1536 first);
1537 *arg = c;
1538 vim_free(tf);
1539 }
1540 clear_tv(&tv);
1541 }
1542 }
1543 }
1544
1545 vim_free(tofree);
1546 }
1547
1548 arg = skipwhite(arg);
1549 }
1550
1551 return arg;
1552}
1553
1554/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001555 * Set an environment variable, part of ex_let_one().
1556 */
1557 static char_u *
1558ex_let_env(
1559 char_u *arg,
1560 typval_T *tv,
1561 int flags,
1562 char_u *endchars,
1563 char_u *op)
1564{
1565 char_u *arg_end = NULL;
1566 char_u *name;
1567 int len;
1568
1569 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1570 && (flags & ASSIGN_FOR_LOOP) == 0)
1571 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001572 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001573 return NULL;
1574 }
1575
1576 // Find the end of the name.
1577 ++arg;
1578 name = arg;
1579 len = get_env_len(&arg);
1580 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001581 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001582 else
1583 {
1584 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001585 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001586 else if (endchars != NULL
1587 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1588 emsg(_(e_unexpected_characters_in_let));
1589 else if (!check_secure())
1590 {
1591 char_u *tofree = NULL;
1592 int c1 = name[len];
1593 char_u *p;
1594
1595 name[len] = NUL;
1596 p = tv_get_string_chk(tv);
1597 if (p != NULL && op != NULL && *op == '.')
1598 {
1599 int mustfree = FALSE;
1600 char_u *s = vim_getenv(name, &mustfree);
1601
1602 if (s != NULL)
1603 {
1604 p = tofree = concat_str(s, p);
1605 if (mustfree)
1606 vim_free(s);
1607 }
1608 }
1609 if (p != NULL)
1610 {
1611 vim_setenv_ext(name, p);
1612 arg_end = arg;
1613 }
1614 name[len] = c1;
1615 vim_free(tofree);
1616 }
1617 }
1618 return arg_end;
1619}
1620
1621/*
1622 * Set an option, part of ex_let_one().
1623 */
1624 static char_u *
1625ex_let_option(
1626 char_u *arg,
1627 typval_T *tv,
1628 int flags,
1629 char_u *endchars,
1630 char_u *op)
1631{
1632 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001633 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001634 char_u *arg_end = NULL;
1635
1636 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1637 && (flags & ASSIGN_FOR_LOOP) == 0)
1638 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001639 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001640 return NULL;
1641 }
1642
1643 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001644 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001645 if (p == NULL || (endchars != NULL
1646 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001647 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001648 emsg(_(e_unexpected_characters_in_let));
1649 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001650 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001651
1652 int c1;
1653 long n = 0;
1654 getoption_T opt_type;
1655 long numval;
1656 char_u *stringval = NULL;
1657 char_u *s = NULL;
1658 int failed = FALSE;
1659 int opt_p_flags;
1660 char_u *tofree = NULL;
1661 char_u numbuf[NUMBUFLEN];
1662
1663 c1 = *p;
1664 *p = NUL;
1665
1666 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1667 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1668 {
1669 semsg(_(e_unknown_option_str_2), arg);
1670 goto theend;
1671 }
1672 if (op != NULL && *op != '='
1673 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1674 || (opt_type == gov_string && *op != '.')))
1675 {
1676 semsg(_(e_wrong_variable_type_for_str_equal), op);
1677 goto theend;
1678 }
1679
1680 if ((opt_type == gov_bool
1681 || opt_type == gov_number
1682 || opt_type == gov_hidden_bool
1683 || opt_type == gov_hidden_number)
1684 && (tv->v_type != VAR_STRING || !in_vim9script()))
1685 {
1686 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1687 // bool, possibly hidden
1688 n = (long)tv_get_bool_chk(tv, &failed);
1689 else
1690 // number, possibly hidden
1691 n = (long)tv_get_number_chk(tv, &failed);
1692 if (failed)
1693 goto theend;
1694 }
1695
1696 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1697 || tv->v_type == VAR_FUNC))
1698 {
1699 // If the option can be set to a function reference or a lambda
1700 // and the passed value is a function reference, then convert it to
1701 // the name (string) of the function reference.
1702 s = tv2string(tv, &tofree, numbuf, 0);
1703 if (s == NULL)
1704 goto theend;
1705 }
1706 // Avoid setting a string option to the text "v:false" or similar.
1707 // In Vim9 script also don't convert a number to string.
1708 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1709 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1710 {
1711 s = tv_get_string_chk(tv);
1712 if (s == NULL)
1713 goto theend;
1714 }
1715 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1716 {
1717 emsg(_(e_string_required));
1718 goto theend;
1719 }
1720
1721 if (op != NULL && *op != '=')
1722 {
1723 // number, in legacy script also bool
1724 if (opt_type == gov_number
1725 || (opt_type == gov_bool && !in_vim9script()))
1726 {
1727 switch (*op)
1728 {
1729 case '+': n = numval + n; break;
1730 case '-': n = numval - n; break;
1731 case '*': n = numval * n; break;
1732 case '/': n = (long)num_divide(numval, n, &failed); break;
1733 case '%': n = (long)num_modulus(numval, n, &failed); break;
1734 }
1735 s = NULL;
1736 if (failed)
1737 goto theend;
1738 }
1739 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1740 {
1741 // string
1742 s = concat_str(stringval, s);
1743 vim_free(stringval);
1744 stringval = s;
1745 }
1746 }
1747
1748 char *err = set_option_value(arg, n, s, scope);
1749 arg_end = p;
1750 if (err != NULL)
1751 emsg(_(err));
1752
1753theend:
1754 *p = c1;
1755 vim_free(stringval);
1756 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001757 return arg_end;
1758}
1759
1760/*
1761 * Set a register, part of ex_let_one().
1762 */
1763 static char_u *
1764ex_let_register(
1765 char_u *arg,
1766 typval_T *tv,
1767 int flags,
1768 char_u *endchars,
1769 char_u *op)
1770{
1771 char_u *arg_end = NULL;
1772
1773 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1774 && (flags & ASSIGN_FOR_LOOP) == 0)
1775 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001776 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001777 return NULL;
1778 }
1779 ++arg;
1780 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001781 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001782 else if (endchars != NULL
1783 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1784 emsg(_(e_unexpected_characters_in_let));
1785 else
1786 {
1787 char_u *ptofree = NULL;
1788 char_u *p;
1789
1790 p = tv_get_string_chk(tv);
1791 if (p != NULL && op != NULL && *op == '.')
1792 {
1793 char_u *s = get_reg_contents(*arg == '@'
1794 ? '"' : *arg, GREG_EXPR_SRC);
1795
1796 if (s != NULL)
1797 {
1798 p = ptofree = concat_str(s, p);
1799 vim_free(s);
1800 }
1801 }
1802 if (p != NULL)
1803 {
1804 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1805 arg_end = arg + 1;
1806 }
1807 vim_free(ptofree);
1808 }
1809 return arg_end;
1810}
1811
1812/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001813 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1814 * Returns a pointer to the char just after the var name.
1815 * Returns NULL if there is an error.
1816 */
1817 static char_u *
1818ex_let_one(
1819 char_u *arg, // points to variable name
1820 typval_T *tv, // value to assign to variable
1821 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001822 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001824 char_u *op, // "+", "-", "." or NULL
1825 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001826{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001828
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001829 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001830 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001831 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1832 {
1833 vim9_declare_error(arg);
1834 return NULL;
1835 }
1836
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001837 if (*arg == '$')
1838 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001839 // ":let $VAR = expr": Set environment variable.
1840 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 else if (*arg == '&')
1843 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001844 // ":let &option = expr": Set option value.
1845 // ":let &l:option = expr": Set local option value.
1846 // ":let &g:option = expr": Set global option value.
1847 // ":for &ts in range(8)": Set option value for for loop
1848 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001850 else if (*arg == '@')
1851 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001852 // ":let @r = expr": Set register contents.
1853 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855 else if (eval_isnamec1(*arg) || *arg == '{')
1856 {
1857 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001858 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001859 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1860 ? GLV_NO_DECL : 0;
1861 if (op != NULL && *op != '=')
1862 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001864 // ":let var = expr": Set internal variable.
1865 // ":let var: type = expr": Set internal variable with type.
1866 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001867 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001868 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001869 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 if (endchars != NULL && vim_strchr(endchars,
1871 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001872 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001873 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001874 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 else
1876 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001877 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001878 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 }
1880 }
1881 clear_lval(&lv);
1882 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001883 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001884 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001885
1886 return arg_end;
1887}
1888
1889/*
1890 * ":unlet[!] var1 ... " command.
1891 */
1892 void
1893ex_unlet(exarg_T *eap)
1894{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001895 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001896}
1897
1898/*
1899 * ":lockvar" and ":unlockvar" commands
1900 */
1901 void
1902ex_lockvar(exarg_T *eap)
1903{
1904 char_u *arg = eap->arg;
1905 int deep = 2;
1906
1907 if (eap->forceit)
1908 deep = -1;
1909 else if (vim_isdigit(*arg))
1910 {
1911 deep = getdigits(&arg);
1912 arg = skipwhite(arg);
1913 }
1914
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001915 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001916}
1917
1918/*
1919 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001920 * Also used for Vim9 script. "callback" is invoked as:
1921 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001922 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001923 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001924ex_unletlock(
1925 exarg_T *eap,
1926 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001927 int deep,
1928 int glv_flags,
1929 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1930 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931{
1932 char_u *arg = argstart;
1933 char_u *name_end;
1934 int error = FALSE;
1935 lval_T lv;
1936
1937 do
1938 {
1939 if (*arg == '$')
1940 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001941 lv.ll_name = arg;
1942 lv.ll_tv = NULL;
1943 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001944 if (get_env_len(&arg) == 0)
1945 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001946 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001947 return;
1948 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001949 if (!error && !eap->skip
1950 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1951 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001952 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001954 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001955 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001956 // Parse the name and find the end.
1957 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001958 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001959 if (lv.ll_name == NULL)
1960 error = TRUE; // error but continue parsing
1961 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1962 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001963 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001964 if (name_end != NULL)
1965 {
1966 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001967 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001968 }
1969 if (!(eap->skip || error))
1970 clear_lval(&lv);
1971 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001973
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001974 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001975 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001976 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001977
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001978 if (!eap->skip)
1979 clear_lval(&lv);
1980 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001981
1982 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001983 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984
Bram Moolenaar63b91732021-08-05 20:40:03 +02001985 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001986}
1987
1988 static int
1989do_unlet_var(
1990 lval_T *lp,
1991 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001992 exarg_T *eap,
1993 int deep UNUSED,
1994 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001995{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001996 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001997 int ret = OK;
1998 int cc;
1999
2000 if (lp->ll_tv == NULL)
2001 {
2002 cc = *name_end;
2003 *name_end = NUL;
2004
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002005 // Environment variable, normal name or expanded name.
2006 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002007 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002008 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002009 ret = FAIL;
2010 *name_end = cc;
2011 }
2012 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002013 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002014 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002015 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002016 return FAIL;
2017 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002018 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2019 !lp->ll_empty2, lp->ll_n2);
2020 else if (lp->ll_list != NULL)
2021 // unlet a List item.
2022 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002023 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002024 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002025 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002026
2027 return ret;
2028}
2029
2030/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002031 * Unlet one item or a range of items from a list.
2032 * Return OK or FAIL.
2033 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002034 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002035list_unlet_range(
2036 list_T *l,
2037 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002038 long n1_arg,
2039 int has_n2,
2040 long n2)
2041{
2042 listitem_T *li = li_first;
2043 int n1 = n1_arg;
2044
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002045 // Delete a range of List items.
2046 li = li_first;
2047 n1 = n1_arg;
2048 while (li != NULL && (!has_n2 || n2 >= n1))
2049 {
2050 listitem_T *next = li->li_next;
2051
2052 listitem_remove(l, li);
2053 li = next;
2054 ++n1;
2055 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002056}
2057/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002058 * "unlet" a variable. Return OK if it existed, FAIL if not.
2059 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2060 */
2061 int
2062do_unlet(char_u *name, int forceit)
2063{
2064 hashtab_T *ht;
2065 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002066 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002067 dict_T *d;
2068 dictitem_T *di;
2069
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002070 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002071 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002072 return FAIL;
2073
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002074 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002075
2076 // can't :unlet a script variable in Vim9 script from a function
2077 if (ht == get_script_local_ht()
2078 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2079 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2080 == SCRIPT_VERSION_VIM9
2081 && check_vim9_unlet(name) == FAIL)
2082 return FAIL;
2083
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002084 if (ht != NULL && *varname != NUL)
2085 {
2086 d = get_current_funccal_dict(ht);
2087 if (d == NULL)
2088 {
2089 if (ht == &globvarht)
2090 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002091 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002092 d = &vimvardict;
2093 else
2094 {
2095 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2096 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2097 }
2098 if (d == NULL)
2099 {
2100 internal_error("do_unlet()");
2101 return FAIL;
2102 }
2103 }
2104 hi = hash_find(ht, varname);
2105 if (HASHITEM_EMPTY(hi))
2106 hi = find_hi_in_scoped_ht(name, &ht);
2107 if (hi != NULL && !HASHITEM_EMPTY(hi))
2108 {
2109 di = HI2DI(hi);
2110 if (var_check_fixed(di->di_flags, name, FALSE)
2111 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002112 || value_check_lock(d->dv_lock, name, FALSE)
2113 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002114 return FAIL;
2115
2116 delete_var(ht, hi);
2117 return OK;
2118 }
2119 }
2120 if (forceit)
2121 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002122 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002123 return FAIL;
2124}
2125
Ernie Raelee865f32023-09-29 19:53:55 +02002126 static void
2127report_lockvar_member(char *msg, lval_T *lp)
2128{
2129 int did_alloc = FALSE;
2130 char_u *vname = (char_u *)"";
2131 char_u *class_name = lp->ll_class != NULL
2132 ? lp->ll_class->class_name : (char_u *)"";
2133 if (lp->ll_name != NULL)
2134 {
2135 if (lp->ll_name_end == NULL)
2136 vname = lp->ll_name;
2137 else
2138 {
2139 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2140 if (vname == NULL)
2141 return;
2142 did_alloc = TRUE;
2143 }
2144 }
2145 semsg(_(msg), vname, class_name);
2146 if (did_alloc)
2147 vim_free(vname);
2148}
2149
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002150/*
2151 * Lock or unlock variable indicated by "lp".
2152 * "deep" is the levels to go (-1 for unlimited);
2153 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2154 */
2155 static int
2156do_lock_var(
2157 lval_T *lp,
2158 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002159 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002160 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002161 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002162{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002163 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002164 int ret = OK;
2165 int cc;
2166 dictitem_T *di;
2167
Ernie Raelee865f32023-09-29 19:53:55 +02002168#ifdef LOG_LOCKVAR
2169 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2170#endif
2171
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002172 if (lp->ll_tv == NULL)
2173 {
2174 cc = *name_end;
2175 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002176 if (*lp->ll_name == '$')
2177 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002178 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002179 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002180 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002181 else
2182 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002183 // Normal name or expanded name.
2184 di = find_var(lp->ll_name, NULL, TRUE);
2185 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002186 {
2187 if (in_vim9script())
2188 semsg(_(e_cannot_find_variable_to_unlock_str),
2189 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002190 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002191 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002192 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002193 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002194 if ((di->di_flags & DI_FLAGS_FIX)
2195 && di->di_tv.v_type != VAR_DICT
2196 && di->di_tv.v_type != VAR_LIST)
2197 {
2198 // For historic reasons this error is not given for a list
2199 // or dict. E.g., the b: dict could be locked/unlocked.
2200 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2201 ret = FAIL;
2202 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002203 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002204 {
2205 if (in_vim9script())
2206 {
2207 svar_T *sv = find_typval_in_script(&di->di_tv,
2208 0, FALSE);
2209
2210 if (sv != NULL && sv->sv_const != 0)
2211 {
2212 semsg(_(e_cannot_change_readonly_variable_str),
2213 lp->ll_name);
2214 ret = FAIL;
2215 }
2216 }
2217
2218 if (ret == OK)
2219 {
2220 if (lock)
2221 di->di_flags |= DI_FLAGS_LOCK;
2222 else
2223 di->di_flags &= ~DI_FLAGS_LOCK;
2224 if (deep != 0)
2225 item_lock(&di->di_tv, deep, lock, FALSE);
2226 }
2227 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002228 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002229 }
2230 *name_end = cc;
2231 }
Ernie Raelee865f32023-09-29 19:53:55 +02002232 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002233 {
2234 // nothing to do
2235 }
Ernie Raelee865f32023-09-29 19:53:55 +02002236 else if (lp->ll_is_root)
2237 // (un)lock the item.
2238 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002239 else if (lp->ll_range)
2240 {
2241 listitem_T *li = lp->ll_li;
2242
2243 // (un)lock a range of List items.
2244 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2245 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002246 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002247 li = li->li_next;
2248 ++lp->ll_n1;
2249 }
2250 }
2251 else if (lp->ll_list != NULL)
2252 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002253 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002254 else if (lp->ll_object != NULL) // This check must be before ll_class.
2255 {
2256 // (un)lock an object variable.
2257 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2258 ret = FAIL;
2259 }
2260 else if (lp->ll_class != NULL)
2261 {
2262 // (un)lock a class variable.
2263 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2264 ret = FAIL;
2265 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002266 else
Ernie Raelee865f32023-09-29 19:53:55 +02002267 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002268 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002269 if (lp->ll_di == NULL)
2270 {
2271 emsg(_(e_dictionary_required));
2272 ret = FAIL;
2273 }
2274 else
2275 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2276 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002277
2278 return ret;
2279}
2280
Ernie Raelee865f32023-09-29 19:53:55 +02002281#ifdef LOG_LOCKVAR
2282 static char *
2283vartype_tostring(vartype_T vartype)
2284{
2285 return
2286 vartype == VAR_BOOL ? "v_number"
2287 : vartype == VAR_SPECIAL ? "v_number"
2288 : vartype == VAR_NUMBER ? "v_number"
2289 : vartype == VAR_FLOAT ? "v_float"
2290 : vartype == VAR_STRING ? "v_string"
2291 : vartype == VAR_BLOB ? "v_blob"
2292 : vartype == VAR_FUNC ? "v_string"
2293 : vartype == VAR_PARTIAL ? "v_partial"
2294 : vartype == VAR_LIST ? "v_list"
2295 : vartype == VAR_DICT ? "v_dict"
2296 : vartype == VAR_JOB ? "v_job"
2297 : vartype == VAR_CHANNEL ? "v_channel"
2298 : vartype == VAR_INSTR ? "v_instr"
2299 : vartype == VAR_CLASS ? "v_class"
2300 : vartype == VAR_OBJECT ? "v_object"
2301 : "";
2302}
2303#endif
2304
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002305/*
2306 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002307 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2308 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002309 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002310 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002311item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002312{
2313 static int recurse = 0;
2314 list_T *l;
2315 listitem_T *li;
2316 dict_T *d;
2317 blob_T *b;
2318 hashitem_T *hi;
2319 int todo;
2320
Ernie Raelee865f32023-09-29 19:53:55 +02002321#ifdef LOG_LOCKVAR
2322 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_tostring(tv->v_type));
2323#endif
2324
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002325 if (recurse >= DICT_MAXNEST)
2326 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002327 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002328 return;
2329 }
2330 if (deep == 0)
2331 return;
2332 ++recurse;
2333
2334 // lock/unlock the item itself
2335 if (lock)
2336 tv->v_lock |= VAR_LOCKED;
2337 else
2338 tv->v_lock &= ~VAR_LOCKED;
2339
2340 switch (tv->v_type)
2341 {
2342 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002343 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002345 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002346 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002347 case VAR_STRING:
2348 case VAR_FUNC:
2349 case VAR_PARTIAL:
2350 case VAR_FLOAT:
2351 case VAR_SPECIAL:
2352 case VAR_JOB:
2353 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002354 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002355 case VAR_CLASS:
2356 case VAR_OBJECT:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002357 break;
2358
2359 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002360 if ((b = tv->vval.v_blob) != NULL
2361 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002362 {
2363 if (lock)
2364 b->bv_lock |= VAR_LOCKED;
2365 else
2366 b->bv_lock &= ~VAR_LOCKED;
2367 }
2368 break;
2369 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002370 if ((l = tv->vval.v_list) != NULL
2371 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002372 {
2373 if (lock)
2374 l->lv_lock |= VAR_LOCKED;
2375 else
2376 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002377 if (deep < 0 || deep > 1)
2378 {
2379 if (l->lv_first == &range_list_item)
2380 l->lv_lock |= VAR_ITEMS_LOCKED;
2381 else
2382 {
2383 // recursive: lock/unlock the items the List contains
2384 CHECK_LIST_MATERIALIZE(l);
2385 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2386 deep - 1, lock, check_refcount);
2387 }
2388 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002389 }
2390 break;
2391 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002392 if ((d = tv->vval.v_dict) != NULL
2393 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002394 {
2395 if (lock)
2396 d->dv_lock |= VAR_LOCKED;
2397 else
2398 d->dv_lock &= ~VAR_LOCKED;
2399 if (deep < 0 || deep > 1)
2400 {
2401 // recursive: lock/unlock the items the List contains
2402 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002403 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002404 {
2405 if (!HASHITEM_EMPTY(hi))
2406 {
2407 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002408 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2409 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002410 }
2411 }
2412 }
2413 }
2414 }
2415 --recurse;
2416}
2417
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002418#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2419/*
2420 * Delete all "menutrans_" variables.
2421 */
2422 void
2423del_menutrans_vars(void)
2424{
2425 hashitem_T *hi;
2426 int todo;
2427
2428 hash_lock(&globvarht);
2429 todo = (int)globvarht.ht_used;
2430 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2431 {
2432 if (!HASHITEM_EMPTY(hi))
2433 {
2434 --todo;
2435 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2436 delete_var(&globvarht, hi);
2437 }
2438 }
2439 hash_unlock(&globvarht);
2440}
2441#endif
2442
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002443/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002444 * Local string buffer for the next two functions to store a variable name
2445 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2446 * get_user_var_name().
2447 */
2448
2449static char_u *varnamebuf = NULL;
2450static int varnamebuflen = 0;
2451
2452/*
2453 * Function to concatenate a prefix and a variable name.
2454 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002455 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002456cat_prefix_varname(int prefix, char_u *name)
2457{
2458 int len;
2459
2460 len = (int)STRLEN(name) + 3;
2461 if (len > varnamebuflen)
2462 {
2463 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002464 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002465 varnamebuf = alloc(len);
2466 if (varnamebuf == NULL)
2467 {
2468 varnamebuflen = 0;
2469 return NULL;
2470 }
2471 varnamebuflen = len;
2472 }
2473 *varnamebuf = prefix;
2474 varnamebuf[1] = ':';
2475 STRCPY(varnamebuf + 2, name);
2476 return varnamebuf;
2477}
2478
2479/*
2480 * Function given to ExpandGeneric() to obtain the list of user defined
2481 * (global/buffer/window/built-in) variable names.
2482 */
2483 char_u *
2484get_user_var_name(expand_T *xp, int idx)
2485{
2486 static long_u gdone;
2487 static long_u bdone;
2488 static long_u wdone;
2489 static long_u tdone;
2490 static int vidx;
2491 static hashitem_T *hi;
2492 hashtab_T *ht;
2493
2494 if (idx == 0)
2495 {
2496 gdone = bdone = wdone = vidx = 0;
2497 tdone = 0;
2498 }
2499
2500 // Global variables
2501 if (gdone < globvarht.ht_used)
2502 {
2503 if (gdone++ == 0)
2504 hi = globvarht.ht_array;
2505 else
2506 ++hi;
2507 while (HASHITEM_EMPTY(hi))
2508 ++hi;
2509 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2510 return cat_prefix_varname('g', hi->hi_key);
2511 return hi->hi_key;
2512 }
2513
2514 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002515 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002516 if (bdone < ht->ht_used)
2517 {
2518 if (bdone++ == 0)
2519 hi = ht->ht_array;
2520 else
2521 ++hi;
2522 while (HASHITEM_EMPTY(hi))
2523 ++hi;
2524 return cat_prefix_varname('b', hi->hi_key);
2525 }
2526
2527 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002528 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002529 if (wdone < ht->ht_used)
2530 {
2531 if (wdone++ == 0)
2532 hi = ht->ht_array;
2533 else
2534 ++hi;
2535 while (HASHITEM_EMPTY(hi))
2536 ++hi;
2537 return cat_prefix_varname('w', hi->hi_key);
2538 }
2539
2540 // t: variables
2541 ht = &curtab->tp_vars->dv_hashtab;
2542 if (tdone < ht->ht_used)
2543 {
2544 if (tdone++ == 0)
2545 hi = ht->ht_array;
2546 else
2547 ++hi;
2548 while (HASHITEM_EMPTY(hi))
2549 ++hi;
2550 return cat_prefix_varname('t', hi->hi_key);
2551 }
2552
2553 // v: variables
2554 if (vidx < VV_LEN)
2555 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2556
2557 VIM_CLEAR(varnamebuf);
2558 varnamebuflen = 0;
2559 return NULL;
2560}
2561
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002562 char *
2563get_var_special_name(int nr)
2564{
2565 switch (nr)
2566 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002567 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2568 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002569 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002570 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002571 }
2572 internal_error("get_var_special_name()");
2573 return "42";
2574}
2575
2576/*
2577 * Returns the global variable dictionary
2578 */
2579 dict_T *
2580get_globvar_dict(void)
2581{
2582 return &globvardict;
2583}
2584
2585/*
2586 * Returns the global variable hash table
2587 */
2588 hashtab_T *
2589get_globvar_ht(void)
2590{
2591 return &globvarht;
2592}
2593
2594/*
2595 * Returns the v: variable dictionary
2596 */
2597 dict_T *
2598get_vimvar_dict(void)
2599{
2600 return &vimvardict;
2601}
2602
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002603/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002605 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 */
2607 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002608find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002610 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2611 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612
2613 if (di == NULL)
2614 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002615 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2617 return (int)(vv - vimvars);
2618}
2619
2620
2621/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002622 * Set type of v: variable to "type".
2623 */
2624 void
2625set_vim_var_type(int idx, vartype_T type)
2626{
Bram Moolenaard787e402021-12-24 21:36:12 +00002627 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002628}
2629
2630/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002631 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002632 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002633 */
2634 void
2635set_vim_var_nr(int idx, varnumber_T val)
2636{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002637 vimvars[idx].vv_nr = val;
2638}
2639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 char *
2641get_vim_var_name(int idx)
2642{
2643 return vimvars[idx].vv_name;
2644}
2645
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002646/*
2647 * Get typval_T v: variable value.
2648 */
2649 typval_T *
2650get_vim_var_tv(int idx)
2651{
2652 return &vimvars[idx].vv_tv;
2653}
2654
Bram Moolenaard787e402021-12-24 21:36:12 +00002655 type_T *
2656get_vim_var_type(int idx, garray_T *type_list)
2657{
2658 if (vimvars[idx].vv_type != NULL)
2659 return vimvars[idx].vv_type;
2660 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2661}
2662
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002663/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002664 * Set v: variable to "tv". Only accepts the same type.
2665 * Takes over the value of "tv".
2666 */
2667 int
2668set_vim_var_tv(int idx, typval_T *tv)
2669{
Bram Moolenaard787e402021-12-24 21:36:12 +00002670 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002671 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002672 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002673 clear_tv(tv);
2674 return FAIL;
2675 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002676 // VV_RO is also checked when compiling, but let's check here as well.
2677 if (vimvars[idx].vv_flags & VV_RO)
2678 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002679 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002680 return FAIL;
2681 }
2682 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2683 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002684 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002685 return FAIL;
2686 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002687 clear_tv(&vimvars[idx].vv_di.di_tv);
2688 vimvars[idx].vv_di.di_tv = *tv;
2689 return OK;
2690}
2691
2692/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002693 * Get number v: variable value.
2694 */
2695 varnumber_T
2696get_vim_var_nr(int idx)
2697{
2698 return vimvars[idx].vv_nr;
2699}
2700
2701/*
2702 * Get string v: variable value. Uses a static buffer, can only be used once.
2703 * If the String variable has never been set, return an empty string.
2704 * Never returns NULL;
2705 */
2706 char_u *
2707get_vim_var_str(int idx)
2708{
2709 return tv_get_string(&vimvars[idx].vv_tv);
2710}
2711
2712/*
2713 * Get List v: variable value. Caller must take care of reference count when
2714 * needed.
2715 */
2716 list_T *
2717get_vim_var_list(int idx)
2718{
2719 return vimvars[idx].vv_list;
2720}
2721
2722/*
2723 * Get Dict v: variable value. Caller must take care of reference count when
2724 * needed.
2725 */
2726 dict_T *
2727get_vim_var_dict(int idx)
2728{
2729 return vimvars[idx].vv_dict;
2730}
2731
2732/*
2733 * Set v:char to character "c".
2734 */
2735 void
2736set_vim_var_char(int c)
2737{
2738 char_u buf[MB_MAXBYTES + 1];
2739
2740 if (has_mbyte)
2741 buf[(*mb_char2bytes)(c, buf)] = NUL;
2742 else
2743 {
2744 buf[0] = c;
2745 buf[1] = NUL;
2746 }
2747 set_vim_var_string(VV_CHAR, buf, -1);
2748}
2749
2750/*
2751 * Set v:count to "count" and v:count1 to "count1".
2752 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2753 */
2754 void
2755set_vcount(
2756 long count,
2757 long count1,
2758 int set_prevcount)
2759{
2760 if (set_prevcount)
2761 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2762 vimvars[VV_COUNT].vv_nr = count;
2763 vimvars[VV_COUNT1].vv_nr = count1;
2764}
2765
2766/*
2767 * Save variables that might be changed as a side effect. Used when executing
2768 * a timer callback.
2769 */
2770 void
2771save_vimvars(vimvars_save_T *vvsave)
2772{
2773 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2774 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2775 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2776}
2777
2778/*
2779 * Restore variables saved by save_vimvars().
2780 */
2781 void
2782restore_vimvars(vimvars_save_T *vvsave)
2783{
2784 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2785 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2786 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2787}
2788
2789/*
2790 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2791 * value.
2792 */
2793 void
2794set_vim_var_string(
2795 int idx,
2796 char_u *val,
2797 int len) // length of "val" to use or -1 (whole string)
2798{
2799 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002800 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002801 if (val == NULL)
2802 vimvars[idx].vv_str = NULL;
2803 else if (len == -1)
2804 vimvars[idx].vv_str = vim_strsave(val);
2805 else
2806 vimvars[idx].vv_str = vim_strnsave(val, len);
2807}
2808
2809/*
2810 * Set List v: variable to "val".
2811 */
2812 void
2813set_vim_var_list(int idx, list_T *val)
2814{
2815 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002816 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002817 vimvars[idx].vv_list = val;
2818 if (val != NULL)
2819 ++val->lv_refcount;
2820}
2821
2822/*
2823 * Set Dictionary v: variable to "val".
2824 */
2825 void
2826set_vim_var_dict(int idx, dict_T *val)
2827{
2828 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002829 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002830 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002831 if (val == NULL)
2832 return;
2833
2834 ++val->dv_refcount;
2835 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002836}
2837
2838/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002839 * Set the v:argv list.
2840 */
2841 void
2842set_argv_var(char **argv, int argc)
2843{
2844 list_T *l = list_alloc();
2845 int i;
2846
2847 if (l == NULL)
2848 getout(1);
2849 l->lv_lock = VAR_FIXED;
2850 for (i = 0; i < argc; ++i)
2851 {
2852 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2853 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002854 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002855 }
2856 set_vim_var_list(VV_ARGV, l);
2857}
2858
2859/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002860 * Reset v:register, taking the 'clipboard' setting into account.
2861 */
2862 void
2863reset_reg_var(void)
2864{
2865 int regname = 0;
2866
2867 // Adjust the register according to 'clipboard', so that when
2868 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2869#ifdef FEAT_CLIPBOARD
2870 adjust_clip_reg(&regname);
2871#endif
2872 set_reg_var(regname);
2873}
2874
2875/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002876 * Set v:register if needed.
2877 */
2878 void
2879set_reg_var(int c)
2880{
2881 char_u regname;
2882
2883 if (c == 0 || c == ' ')
2884 regname = '"';
2885 else
2886 regname = c;
2887 // Avoid free/alloc when the value is already right.
2888 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2889 set_vim_var_string(VV_REG, &regname, 1);
2890}
2891
2892/*
2893 * Get or set v:exception. If "oldval" == NULL, return the current value.
2894 * Otherwise, restore the value to "oldval" and return NULL.
2895 * Must always be called in pairs to save and restore v:exception! Does not
2896 * take care of memory allocations.
2897 */
2898 char_u *
2899v_exception(char_u *oldval)
2900{
2901 if (oldval == NULL)
2902 return vimvars[VV_EXCEPTION].vv_str;
2903
2904 vimvars[VV_EXCEPTION].vv_str = oldval;
2905 return NULL;
2906}
2907
2908/*
2909 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2910 * Otherwise, restore the value to "oldval" and return NULL.
2911 * Must always be called in pairs to save and restore v:throwpoint! Does not
2912 * take care of memory allocations.
2913 */
2914 char_u *
2915v_throwpoint(char_u *oldval)
2916{
2917 if (oldval == NULL)
2918 return vimvars[VV_THROWPOINT].vv_str;
2919
2920 vimvars[VV_THROWPOINT].vv_str = oldval;
2921 return NULL;
2922}
2923
2924/*
2925 * Set v:cmdarg.
2926 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2927 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2928 * Must always be called in pairs!
2929 */
2930 char_u *
2931set_cmdarg(exarg_T *eap, char_u *oldarg)
2932{
2933 char_u *oldval;
2934 char_u *newval;
2935 unsigned len;
2936
2937 oldval = vimvars[VV_CMDARG].vv_str;
2938 if (eap == NULL)
2939 {
2940 vim_free(oldval);
2941 vimvars[VV_CMDARG].vv_str = oldarg;
2942 return NULL;
2943 }
2944
2945 if (eap->force_bin == FORCE_BIN)
2946 len = 6;
2947 else if (eap->force_bin == FORCE_NOBIN)
2948 len = 8;
2949 else
2950 len = 0;
2951
2952 if (eap->read_edit)
2953 len += 7;
2954
2955 if (eap->force_ff != 0)
2956 len += 10; // " ++ff=unix"
2957 if (eap->force_enc != 0)
2958 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2959 if (eap->bad_char != 0)
2960 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2961
2962 newval = alloc(len + 1);
2963 if (newval == NULL)
2964 return NULL;
2965
2966 if (eap->force_bin == FORCE_BIN)
2967 sprintf((char *)newval, " ++bin");
2968 else if (eap->force_bin == FORCE_NOBIN)
2969 sprintf((char *)newval, " ++nobin");
2970 else
2971 *newval = NUL;
2972
2973 if (eap->read_edit)
2974 STRCAT(newval, " ++edit");
2975
2976 if (eap->force_ff != 0)
2977 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2978 eap->force_ff == 'u' ? "unix"
2979 : eap->force_ff == 'd' ? "dos"
2980 : "mac");
2981 if (eap->force_enc != 0)
2982 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2983 eap->cmd + eap->force_enc);
2984 if (eap->bad_char == BAD_KEEP)
2985 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2986 else if (eap->bad_char == BAD_DROP)
2987 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2988 else if (eap->bad_char != 0)
2989 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2990 vimvars[VV_CMDARG].vv_str = newval;
2991 return oldval;
2992}
2993
2994/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002995 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002996 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2997 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002998 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2999 */
3000 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003001eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003002 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00003003 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003004 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003005 typval_T *rettv, // NULL when only checking existence
3006 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003007 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003008{
3009 int ret = OK;
3010 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003011 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02003012 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00003013 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003014 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003015
Bram Moolenaar94674f22023-01-06 18:42:20 +00003016 if (len > 0)
3017 {
3018 // truncate the name, so that we can use strcmp()
3019 cc = name[len];
3020 name[len] = NUL;
3021 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003022
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003023 // Check for local variable when debugging.
3024 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003026 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003027 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3028
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003029 if (v != NULL)
3030 {
3031 tv = &v->di_tv;
3032 if (dip != NULL)
3033 *dip = v;
3034 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003035 else
3036 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003037 }
3038
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003039 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003041 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003042 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3043
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003044 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003045 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046
3047 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003048 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003050 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003051 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003052 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003053 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003054 ht = &SCRIPT_VARS(sid);
3055 if (ht != NULL)
3056 {
3057 dictitem_T *v = find_var_in_ht(ht, 0, name,
3058 flags & EVAL_VAR_NOAUTOLOAD);
3059
3060 if (v != NULL)
3061 {
3062 tv = &v->di_tv;
3063 if (dip != NULL)
3064 *dip = v;
3065 }
3066 else
3067 ht = NULL;
3068 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003069 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003070 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003071 {
3072 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003073 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003074 ret = FAIL;
3075 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003076 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003077 else
3078 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003079 if (rettv != NULL)
3080 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003081 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003082 rettv->v_type = VAR_ANY;
3083 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3084 }
3085 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003088 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003089 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003090 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003091 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003092
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003093 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003094 // function name. For a global non-autoload function "g:" is
3095 // required.
3096 if (ufunc != NULL && (has_g_prefix
3097 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003098 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003099 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003100 if (rettv != NULL)
3101 {
3102 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003103 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003104 // Keep the "g:", otherwise script-local may be
3105 // assumed.
3106 rettv->vval.v_string = vim_strsave(name);
3107 else
3108 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003109 if (rettv->vval.v_string != NULL)
3110 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003111 }
3112 }
3113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 }
3115
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003116 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003117 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003118 if (tv == NULL)
3119 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003120 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003121 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003122 ret = FAIL;
3123 }
3124 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003125 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003126 svar_T *sv = NULL;
3127 int was_assigned = FALSE;
3128
Bram Moolenaar11005b02021-07-11 20:59:00 +02003129 if (ht != NULL && ht == get_script_local_ht()
3130 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003131 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003132 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003133 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003134 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003135 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003136 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3137 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003138 }
3139
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003140 // If a list or dict variable wasn't initialized and has meaningful
3141 // type, do it now. Not for global variables, they are not
3142 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003143 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003144 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003145 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003146 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003147 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003148 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003149 tv->vval.v_dict = dict_alloc();
3150 if (tv->vval.v_dict != NULL)
3151 {
3152 ++tv->vval.v_dict->dv_refcount;
3153 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003154 if (sv != NULL)
3155 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003156 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003157 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003158 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003159 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003160 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003161 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003162 tv->vval.v_list = list_alloc();
3163 if (tv->vval.v_list != NULL)
3164 {
3165 ++tv->vval.v_list->lv_refcount;
3166 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003167 if (sv != NULL)
3168 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003169 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003170 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003171 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003172 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003173 || !in_vim9script()))
3174 {
3175 tv->vval.v_blob = blob_alloc();
3176 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003177 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003178 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003179 if (sv != NULL)
3180 sv->sv_flags |= SVFLAG_ASSIGNED;
3181 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003182 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003183 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003184 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003185 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003186 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003187
Bram Moolenaar94674f22023-01-06 18:42:20 +00003188 if (len > 0)
3189 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190
3191 return ret;
3192}
3193
3194/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003195 * Get the value of internal variable "name", also handling "import.name".
3196 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3197 */
3198 int
3199eval_variable_import(
3200 char_u *name,
3201 typval_T *rettv)
3202{
3203 char_u *s = name;
3204 while (ASCII_ISALNUM(*s) || *s == '_')
3205 ++s;
3206 int len = (int)(s - name);
3207
3208 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3209 return FAIL;
3210 if (rettv->v_type == VAR_ANY && *s == '.')
3211 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003212 char_u *ns = s + 1;
3213 s = ns;
3214 while (ASCII_ISALNUM(*s) || *s == '_')
3215 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003216 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003217 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003218 }
3219 return OK;
3220}
3221
3222
3223/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003224 * Check if variable "name[len]" is a local variable or an argument.
3225 * If so, "*eval_lavars_used" is set to TRUE.
3226 */
3227 void
3228check_vars(char_u *name, int len)
3229{
3230 int cc;
3231 char_u *varname;
3232 hashtab_T *ht;
3233
3234 if (eval_lavars_used == NULL)
3235 return;
3236
3237 // truncate the name, so that we can use strcmp()
3238 cc = name[len];
3239 name[len] = NUL;
3240
3241 ht = find_var_ht(name, &varname);
3242 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3243 {
3244 if (find_var(name, NULL, TRUE) != NULL)
3245 *eval_lavars_used = TRUE;
3246 }
3247
3248 name[len] = cc;
3249}
3250
3251/*
3252 * Find variable "name" in the list of variables.
3253 * Return a pointer to it if found, NULL if not found.
3254 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003255 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003256 */
3257 dictitem_T *
3258find_var(char_u *name, hashtab_T **htp, int no_autoload)
3259{
3260 char_u *varname;
3261 hashtab_T *ht;
3262 dictitem_T *ret = NULL;
3263
3264 ht = find_var_ht(name, &varname);
3265 if (htp != NULL)
3266 *htp = ht;
3267 if (ht == NULL)
3268 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003269 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003270 if (ret != NULL)
3271 return ret;
3272
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003273 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003274 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003275 if (ret != NULL)
3276 return ret;
3277
3278 // in Vim9 script items without a scope can be script-local
3279 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3280 {
3281 ht = get_script_local_ht();
3282 if (ht != NULL)
3283 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003284 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003285 if (ret != NULL)
3286 {
3287 if (htp != NULL)
3288 *htp = ht;
3289 return ret;
3290 }
3291 }
3292 }
3293
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003294 // When using "vim9script autoload" script-local items are prefixed but can
3295 // be used with s:name.
3296 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003297 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003298 {
3299 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3300
3301 if (si->sn_autoload_prefix != NULL)
3302 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003303 char_u *base_name = (name[0] == 's' && name[1] == ':')
3304 ? name + 2 : name;
3305 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003306
3307 if (auto_name != NULL)
3308 {
3309 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003310 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003311 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003312 if (ret != NULL)
3313 {
3314 if (htp != NULL)
3315 *htp = ht;
3316 return ret;
3317 }
3318 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003319 }
3320 }
3321
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003322 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003323}
3324
3325/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003326 * Like find_var() but if the name starts with <SNR>99_ then look in the
3327 * referenced script (used for a funcref).
3328 */
3329 dictitem_T *
3330find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3331{
3332 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
3333 {
3334 char_u *p = name + 5;
3335 int sid = getdigits(&p);
3336
3337 if (SCRIPT_ID_VALID(sid) && *p == '_')
3338 {
3339 hashtab_T *ht = &SCRIPT_VARS(sid);
3340
3341 if (ht != NULL)
3342 {
3343 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3344
3345 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003346 {
3347 if (htp != NULL)
3348 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003349 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003350 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003351 }
3352 }
3353 }
3354
3355 return find_var(name, htp, no_autoload);
3356}
3357
3358/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003359 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003360 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003361 * Returns NULL if not found.
3362 */
3363 dictitem_T *
3364find_var_in_ht(
3365 hashtab_T *ht,
3366 int htname,
3367 char_u *varname,
3368 int no_autoload)
3369{
3370 hashitem_T *hi;
3371
3372 if (*varname == NUL)
3373 {
3374 // Must be something like "s:", otherwise "ht" would be NULL.
3375 switch (htname)
3376 {
3377 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3378 case 'g': return &globvars_var;
3379 case 'v': return &vimvars_var;
3380 case 'b': return &curbuf->b_bufvar;
3381 case 'w': return &curwin->w_winvar;
3382 case 't': return &curtab->tp_winvar;
3383 case 'l': return get_funccal_local_var();
3384 case 'a': return get_funccal_args_var();
3385 }
3386 return NULL;
3387 }
3388
3389 hi = hash_find(ht, varname);
3390 if (HASHITEM_EMPTY(hi))
3391 {
3392 // For global variables we may try auto-loading the script. If it
3393 // worked find the variable again. Don't auto-load a script if it was
3394 // loaded already, otherwise it would be loaded every time when
3395 // checking if a function name is a Funcref variable.
3396 if (ht == &globvarht && !no_autoload)
3397 {
3398 // Note: script_autoload() may make "hi" invalid. It must either
3399 // be obtained again or not used.
3400 if (!script_autoload(varname, FALSE) || aborting())
3401 return NULL;
3402 hi = hash_find(ht, varname);
3403 }
3404 if (HASHITEM_EMPTY(hi))
3405 return NULL;
3406 }
3407 return HI2DI(hi);
3408}
3409
3410/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 * Get the script-local hashtab. NULL if not in a script context.
3412 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003413 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003414get_script_local_ht(void)
3415{
3416 scid_T sid = current_sctx.sc_sid;
3417
Bram Moolenaare3d46852020-08-29 13:39:17 +02003418 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 return &SCRIPT_VARS(sid);
3420 return NULL;
3421}
3422
3423/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003424 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003425 * When "cmd" is TRUE it must look like a command, a function must be followed
3426 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003427 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003429 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003430lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003431 char_u *name,
3432 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003433 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003434 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435{
3436 hashtab_T *ht = get_script_local_ht();
3437 char_u buffer[30];
3438 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003439 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003441 int is_global = FALSE;
3442 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003443
3444 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003445 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003446 if (len < sizeof(buffer) - 1)
3447 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003448 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 vim_strncpy(buffer, name, len);
3450 p = buffer;
3451 }
3452 else
3453 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003454 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003456 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003457 }
3458
3459 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003460 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003461
3462 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003463 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003464 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 if (p != buffer)
3466 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003467
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003468 // Find a function, so that a following "->" works.
3469 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3470 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003471 if (res != OK)
3472 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003473 p = skipwhite(name + len);
3474
3475 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003476 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003477 // Do not check for an internal function, since it might also be a
3478 // valid command, such as ":split" versus "split()".
3479 // Skip "g:" before a function name.
3480 if (name[0] == 'g' && name[1] == ':')
3481 {
3482 is_global = TRUE;
3483 fname = name + 2;
3484 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003485 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003486 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003487 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003488 }
3489
Bram Moolenaar709664c2020-12-12 14:33:41 +01003490 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491}
3492
3493/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003494 * Find the hashtab used for a variable name.
3495 * Return NULL if the name is not valid.
3496 * Set "varname" to the start of name without ':'.
3497 */
3498 hashtab_T *
3499find_var_ht(char_u *name, char_u **varname)
3500{
3501 hashitem_T *hi;
3502 hashtab_T *ht;
3503
3504 if (name[0] == NUL)
3505 return NULL;
3506 if (name[1] != ':')
3507 {
3508 // The name must not start with a colon or #.
3509 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3510 return NULL;
3511 *varname = name;
3512
3513 // "version" is "v:version" in all scopes if scriptversion < 3.
3514 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003515 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003516 {
3517 hi = hash_find(&compat_hashtab, name);
3518 if (!HASHITEM_EMPTY(hi))
3519 return &compat_hashtab;
3520 }
3521
3522 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 if (ht != NULL)
3524 return ht; // local variable
3525
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003526 // In Vim9 script items at the script level are script-local, except
3527 // for autoload names.
3528 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 {
3530 ht = get_script_local_ht();
3531 if (ht != NULL)
3532 return ht;
3533 }
3534
3535 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003536 }
3537 *varname = name + 2;
3538 if (*name == 'g') // global variable
3539 return &globvarht;
3540 // There must be no ':' or '#' in the rest of the name, unless g: is used
3541 if (vim_strchr(name + 2, ':') != NULL
3542 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3543 return NULL;
3544 if (*name == 'b') // buffer variable
3545 return &curbuf->b_vars->dv_hashtab;
3546 if (*name == 'w') // window variable
3547 return &curwin->w_vars->dv_hashtab;
3548 if (*name == 't') // tab page variable
3549 return &curtab->tp_vars->dv_hashtab;
3550 if (*name == 'v') // v: variable
3551 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003552 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003553 && get_current_funccal()->fc_func->uf_def_status
3554 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003556 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 if (*name == 'a') // a: function argument
3558 return get_funccal_args_ht();
3559 if (*name == 'l') // l: local function variable
3560 return get_funccal_local_ht();
3561 }
3562 if (*name == 's') // script variable
3563 {
3564 ht = get_script_local_ht();
3565 if (ht != NULL)
3566 return ht;
3567 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003568 return NULL;
3569}
3570
3571/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003572 * Get the string value of a (global/local) variable.
3573 * Note: see tv_get_string() for how long the pointer remains valid.
3574 * Returns NULL when it doesn't exist.
3575 */
3576 char_u *
3577get_var_value(char_u *name)
3578{
3579 dictitem_T *v;
3580
3581 v = find_var(name, NULL, FALSE);
3582 if (v == NULL)
3583 return NULL;
3584 return tv_get_string(&v->di_tv);
3585}
3586
3587/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003588 * Allocate a new hashtab for a sourced script. It will be used while
3589 * sourcing this script and when executing functions defined in the script.
3590 */
3591 void
3592new_script_vars(scid_T id)
3593{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003594 scriptvar_T *sv;
3595
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003596 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3597 if (sv == NULL)
3598 return;
3599 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003600 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003601}
3602
3603/*
3604 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3605 * point to it.
3606 */
3607 void
3608init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3609{
3610 hash_init(&dict->dv_hashtab);
3611 dict->dv_lock = 0;
3612 dict->dv_scope = scope;
3613 dict->dv_refcount = DO_NOT_FREE_CNT;
3614 dict->dv_copyID = 0;
3615 dict_var->di_tv.vval.v_dict = dict;
3616 dict_var->di_tv.v_type = VAR_DICT;
3617 dict_var->di_tv.v_lock = VAR_FIXED;
3618 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3619 dict_var->di_key[0] = NUL;
3620}
3621
3622/*
3623 * Unreference a dictionary initialized by init_var_dict().
3624 */
3625 void
3626unref_var_dict(dict_T *dict)
3627{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003628 // Now the dict needs to be freed if no one else is using it, go back to
3629 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003630 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3631 dict_unref(dict);
3632}
3633
3634/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003635 * Clean up a list of internal variables.
3636 * Frees all allocated variables and the value they contain.
3637 * Clears hashtab "ht", does not free it.
3638 */
3639 void
3640vars_clear(hashtab_T *ht)
3641{
3642 vars_clear_ext(ht, TRUE);
3643}
3644
3645/*
3646 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3647 */
3648 void
3649vars_clear_ext(hashtab_T *ht, int free_val)
3650{
3651 int todo;
3652 hashitem_T *hi;
3653 dictitem_T *v;
3654
3655 hash_lock(ht);
3656 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003657 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003658 {
3659 if (!HASHITEM_EMPTY(hi))
3660 {
3661 --todo;
3662
3663 // Free the variable. Don't remove it from the hashtab,
3664 // ht_array might change then. hash_clear() takes care of it
3665 // later.
3666 v = HI2DI(hi);
3667 if (free_val)
3668 clear_tv(&v->di_tv);
3669 if (v->di_flags & DI_FLAGS_ALLOC)
3670 vim_free(v);
3671 }
3672 }
3673 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003674 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003675}
3676
3677/*
3678 * Delete a variable from hashtab "ht" at item "hi".
3679 * Clear the variable value and free the dictitem.
3680 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003681 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003682delete_var(hashtab_T *ht, hashitem_T *hi)
3683{
3684 dictitem_T *di = HI2DI(hi);
3685
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003686 if (hash_remove(ht, hi, "delete variable") != OK)
3687 return;
3688
3689 clear_tv(&di->di_tv);
3690 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003691}
3692
3693/*
3694 * List the value of one internal variable.
3695 */
3696 static void
3697list_one_var(dictitem_T *v, char *prefix, int *first)
3698{
3699 char_u *tofree;
3700 char_u *s;
3701 char_u numbuf[NUMBUFLEN];
3702
3703 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3704 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3705 s == NULL ? (char_u *)"" : s, first);
3706 vim_free(tofree);
3707}
3708
3709 static void
3710list_one_var_a(
3711 char *prefix,
3712 char_u *name,
3713 int type,
3714 char_u *string,
3715 int *first) // when TRUE clear rest of screen and set to FALSE
3716{
3717 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3718 msg_start();
3719 msg_puts(prefix);
3720 if (name != NULL) // "a:" vars don't have a name stored
3721 msg_puts((char *)name);
3722 msg_putchar(' ');
3723 msg_advance(22);
3724 if (type == VAR_NUMBER)
3725 msg_putchar('#');
3726 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3727 msg_putchar('*');
3728 else if (type == VAR_LIST)
3729 {
3730 msg_putchar('[');
3731 if (*string == '[')
3732 ++string;
3733 }
3734 else if (type == VAR_DICT)
3735 {
3736 msg_putchar('{');
3737 if (*string == '{')
3738 ++string;
3739 }
3740 else
3741 msg_putchar(' ');
3742
3743 msg_outtrans(string);
3744
3745 if (type == VAR_FUNC || type == VAR_PARTIAL)
3746 msg_puts("()");
3747 if (*first)
3748 {
3749 msg_clr_eos();
3750 *first = FALSE;
3751 }
3752}
3753
3754/*
zeertzjqedcba962023-09-24 23:13:51 +02003755 * Addition handling for setting a v: variable.
3756 * Return TRUE if the variable should be set normally,
3757 * FALSE if nothing else needs to be done.
3758 */
3759 int
3760before_set_vvar(
3761 char_u *varname,
3762 dictitem_T *di,
3763 typval_T *tv,
3764 int copy,
3765 int *type_error)
3766{
3767 if (di->di_tv.v_type == VAR_STRING)
3768 {
3769 VIM_CLEAR(di->di_tv.vval.v_string);
3770 if (copy || tv->v_type != VAR_STRING)
3771 {
3772 char_u *val = tv_get_string(tv);
3773
3774 // Careful: when assigning to v:errmsg and
3775 // tv_get_string() causes an error message the variable
3776 // will already be set.
3777 if (di->di_tv.vval.v_string == NULL)
3778 di->di_tv.vval.v_string = vim_strsave(val);
3779 }
3780 else
3781 {
3782 // Take over the string to avoid an extra alloc/free.
3783 di->di_tv.vval.v_string = tv->vval.v_string;
3784 tv->vval.v_string = NULL;
3785 }
3786 return FALSE;
3787 }
3788 else if (di->di_tv.v_type == VAR_NUMBER)
3789 {
3790 di->di_tv.vval.v_number = tv_get_number(tv);
3791 if (STRCMP(varname, "searchforward") == 0)
3792 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3793#ifdef FEAT_SEARCH_EXTRA
3794 else if (STRCMP(varname, "hlsearch") == 0)
3795 {
3796 no_hlsearch = !di->di_tv.vval.v_number;
3797 redraw_all_later(UPD_SOME_VALID);
3798 }
3799#endif
3800 return FALSE;
3801 }
3802 else if (di->di_tv.v_type != tv->v_type)
3803 {
3804 *type_error = TRUE;
3805 return FALSE;
3806 }
3807 return TRUE;
3808}
3809
3810/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003811 * Set variable "name" to value in "tv".
3812 * If the variable already exists, the value is updated.
3813 * Otherwise the variable is created.
3814 */
3815 void
3816set_var(
3817 char_u *name,
3818 typval_T *tv,
3819 int copy) // make copy of value in "tv"
3820{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003821 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003822}
3823
3824/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003825 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003826 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003827 * If the variable already exists and "is_const" is FALSE the value is updated.
3828 * Otherwise the variable is created.
3829 */
3830 void
3831set_var_const(
3832 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003833 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003834 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003835 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003836 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003837 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003838 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003839{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003840 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003841 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003842 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003844 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003845 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003846 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003847 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003849 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003850 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003851 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003852 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003853 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003854
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003855 if (sid != 0)
3856 {
3857 if (SCRIPT_ID_VALID(sid))
3858 ht = &SCRIPT_VARS(sid);
3859 varname = name;
3860 }
3861 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003862 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003863 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003864
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003865 if (in_vim9script() && is_export
3866 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3867 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003868 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003869 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003870 // In a vim9 autoload script an exported variable is put in the
3871 // global namespace with the autoload prefix.
3872 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003873 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003874 if (varname == NULL)
3875 goto failed;
3876 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003877 ht = &globvarht;
3878 }
3879 else
3880 ht = find_var_ht(name, &varname);
3881 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003882 if (ht == NULL || *varname == NUL)
3883 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003884 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003885 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003886 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003887 is_script_local = ht == get_script_local_ht() || sid != 0
3888 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003889
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003890 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003891 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003892 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003893 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003894 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003895 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003896 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003897 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003898 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003899 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003900 // Do not make g:var, w:var, b:var or t:var final.
3901 flags &= ~ASSIGN_FINAL;
3902
Bram Moolenaare535db82021-03-31 21:07:24 +02003903 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003904 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3905 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003906 // For "[a, _] = list" the underscore is ignored.
3907 if ((flags & ASSIGN_UNPACK) == 0)
3908 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003909 goto failed;
3910 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003913
Bram Moolenaar24e93162021-07-18 20:40:33 +02003914 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003915 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003916 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003917
Bram Moolenaar24e93162021-07-18 20:40:33 +02003918 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003919 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003920 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003921 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003923 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003924 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003926 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3927 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003928 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003929 if (!in_vim9script())
3930 {
3931 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3932 name);
3933 goto failed;
3934 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936
Bram Moolenaar993faa32022-02-21 15:59:11 +00003937 // Search in parent scope which is possible to reference from lambda
3938 if (di == NULL)
3939 di = find_var_in_scoped_ht(name, TRUE);
3940
3941 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3942 && var_wrong_func_name(name, di == NULL))
3943 goto failed;
3944
3945 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003946 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003947 // Destination is a bool and the value is not, but it can be
3948 // converted.
3949 CLEAR_FIELD(bool_tv);
3950 bool_tv.v_type = VAR_BOOL;
3951 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3952 tv = &bool_tv;
3953 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003954
Bram Moolenaar993faa32022-02-21 15:59:11 +00003955 if (di != NULL)
3956 {
3957 // Item already exists. Allowed to replace when reloading.
3958 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003959 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003960 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3961 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003962 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003963 emsg(_(e_cannot_modify_existing_variable));
3964 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003965 }
3966
Bram Moolenaar993faa32022-02-21 15:59:11 +00003967 if (is_script_local && vim9script
3968 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003969 {
3970 semsg(_(e_redefining_script_item_str), name);
3971 goto failed;
3972 }
3973
Bram Moolenaar993faa32022-02-21 15:59:11 +00003974 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003975 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003976 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01003977 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00003978
3979 if (sv != NULL)
3980 {
3981 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02003982 if (var_idx > 0)
3983 {
3984 where.wt_index = var_idx;
3985 where.wt_kind = WT_VARIABLE;
3986 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00003987 if (check_script_var_type(sv, tv, name, where) == FAIL)
3988 goto failed;
3989 if (type == NULL)
3990 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003991 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003992 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003993 }
3994
Bram Moolenaar993faa32022-02-21 15:59:11 +00003995 if ((flags & ASSIGN_FOR_LOOP) == 0
Bram Moolenaar16d2c022023-06-05 19:46:18 +01003996 ? var_check_permission(di, name) == FAIL
3997 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003998 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003999 }
4000 else
4001 {
4002 // can only redefine once
4003 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004004
Bram Moolenaar993faa32022-02-21 15:59:11 +00004005 // A Vim9 script-local variable is also present in sn_all_vars
4006 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004007 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004008 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004009 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004010 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004011 }
4012
Bram Moolenaar993faa32022-02-21 15:59:11 +00004013 // existing variable, need to clear the value
4014
zeertzjqedcba962023-09-24 23:13:51 +02004015 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004016 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004017 int type_error = FALSE;
4018 if (ht == &vimvarht
4019 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004020 {
zeertzjqedcba962023-09-24 23:13:51 +02004021 if (type_error)
4022 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4023 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004024 }
4025
4026 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004027
4028 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4029 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4030 {
4031 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4032
4033 update_script_var_block_id(name, si->sn_current_block_id);
4034 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004035 }
4036 else
4037 {
4038 // Item not found, check if a function already exists.
4039 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4040 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4041 {
4042 semsg(_(e_redefining_script_item_str), name);
4043 goto failed;
4044 }
4045
4046 // add a new variable
4047 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4048 {
4049 semsg(_(e_unknown_variable_str), name);
4050 goto failed;
4051 }
4052
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004053 if (check_hashtab_frozen(ht, "add variable"))
4054 goto failed;
4055
Bram Moolenaar993faa32022-02-21 15:59:11 +00004056 // Can't add "v:" or "a:" variable.
4057 if (ht == &vimvarht || ht == get_funccal_args_ht())
4058 {
4059 semsg(_(e_illegal_variable_name_str), name);
4060 goto failed;
4061 }
4062
4063 // Make sure the variable name is valid. In Vim9 script an
4064 // autoload variable must be prefixed with "g:" unless in an
4065 // autoload script.
4066 if (!valid_varname(varname, -1, !vim9script
4067 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4068 goto failed;
4069
zeertzjq1b438a82023-02-01 13:11:15 +00004070 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004071 if (di == NULL)
4072 goto failed;
4073 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004074 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004075 {
4076 vim_free(di);
4077 goto failed;
4078 }
4079 di->di_flags = DI_FLAGS_ALLOC;
4080 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4081 di->di_flags |= DI_FLAGS_LOCK;
4082
4083 // A Vim9 script-local variable is also added to sn_all_vars and
4084 // sn_var_vals. It may set "type" from "tv".
4085 if (var_in_vim9script || var_in_autoload)
4086 update_vim9_script_var(TRUE, di,
4087 var_in_autoload ? name : di->di_key, flags,
4088 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004089 }
4090
Bram Moolenaar993faa32022-02-21 15:59:11 +00004091 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004092 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004093 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004094 else
4095 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004096 *dest_tv = *tv;
4097 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004098 init_tv(tv);
4099 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004100 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004101
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004102 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004103 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004104
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004105 // ":const var = value" locks the value
4106 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004107 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004108 // Like :lockvar! name: lock the value and what it contains, but only
4109 // if the reference count is up to one. That locks only literal
4110 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004111 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004112
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004113failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004114 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004115 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004116 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004117}
4118
4119/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004120 * Check in this order for backwards compatibility:
4121 * - Whether the variable is read-only
4122 * - Whether the variable value is locked
4123 * - Whether the variable is locked
4124 */
4125 int
4126var_check_permission(dictitem_T *di, char_u *name)
4127{
4128 if (var_check_ro(di->di_flags, name, FALSE)
4129 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4130 || var_check_lock(di->di_flags, name, FALSE))
4131 return FAIL;
4132 return OK;
4133}
4134
4135/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004136 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4137 * Also give an error message.
4138 */
4139 int
4140var_check_ro(int flags, char_u *name, int use_gettext)
4141{
4142 if (flags & DI_FLAGS_RO)
4143 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004144 if (name == NULL)
4145 emsg(_(e_cannot_change_readonly_variable));
4146 else
4147 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004148 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004149 return TRUE;
4150 }
4151 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4152 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004153 if (name == NULL)
4154 emsg(_(e_cannot_set_variable_in_sandbox));
4155 else
4156 semsg(_(e_cannot_set_variable_in_sandbox_str),
4157 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004158 return TRUE;
4159 }
4160 return FALSE;
4161}
4162
4163/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004164 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4165 * Also give an error message.
4166 */
4167 int
4168var_check_lock(int flags, char_u *name, int use_gettext)
4169{
4170 if (flags & DI_FLAGS_LOCK)
4171 {
4172 semsg(_(e_variable_is_locked_str),
4173 use_gettext ? (char_u *)_(name) : name);
4174 return TRUE;
4175 }
4176 return FALSE;
4177}
4178
4179/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004180 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4181 * Also give an error message.
4182 */
4183 int
4184var_check_fixed(int flags, char_u *name, int use_gettext)
4185{
4186 if (flags & DI_FLAGS_FIX)
4187 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004188 if (name == NULL)
4189 emsg(_(e_cannot_delete_variable));
4190 else
4191 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004192 use_gettext ? (char_u *)_(name) : name);
4193 return TRUE;
4194 }
4195 return FALSE;
4196}
4197
4198/*
4199 * Check if a funcref is assigned to a valid variable name.
4200 * Return TRUE and give an error if not.
4201 */
4202 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004203var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004204 char_u *name, // points to start of variable name
4205 int new_var) // TRUE when creating the variable
4206{
Bram Moolenaar32154662021-03-28 21:14:06 +02004207 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4208 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004209 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004210 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4211 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004212 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004213 ? name[2] : name[0])
4214 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004215 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004216 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004217 return TRUE;
4218 }
4219 // Don't allow hiding a function. When "v" is not NULL we might be
4220 // assigning another function to the same var, the type is checked
4221 // below.
4222 if (new_var && function_exists(name, FALSE))
4223 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004224 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004225 name);
4226 return TRUE;
4227 }
4228 return FALSE;
4229}
4230
4231/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004232 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4233 * value. Also give an error message, using "name" or _("name") when
4234 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004235 */
4236 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004237value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004238{
4239 if (lock & VAR_LOCKED)
4240 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004241 if (name == NULL)
4242 emsg(_(e_value_is_locked));
4243 else
4244 semsg(_(e_value_is_locked_str),
4245 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004246 return TRUE;
4247 }
4248 if (lock & VAR_FIXED)
4249 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004250 if (name == NULL)
4251 emsg(_(e_cannot_change_value));
4252 else
4253 semsg(_(e_cannot_change_value_of_str),
4254 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004255 return TRUE;
4256 }
4257 return FALSE;
4258}
4259
4260/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004261 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004262 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004263 * Return FALSE and give an error if not.
4264 */
4265 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004266valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004267{
4268 char_u *p;
4269
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004270 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004271 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004272 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004273 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004274 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004275 return FALSE;
4276 }
4277 return TRUE;
4278}
4279
4280/*
LemonBoy47d4e312022-05-04 18:12:55 +01004281 * Implements the logic to retrieve local variable and option values.
4282 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004283 */
4284 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004285get_var_from(
4286 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004287 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004288 typval_T *deftv, // Default value if not found.
4289 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4290 tabpage_T *tp, // can be NULL
4291 win_T *win,
4292 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004293{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004294 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004295 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004296 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004297 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004298 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004299
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004300 ++emsg_off;
4301
4302 rettv->v_type = VAR_STRING;
4303 rettv->vval.v_string = NULL;
4304
LemonBoy47d4e312022-05-04 18:12:55 +01004305 if (varname != NULL && tp != NULL && win != NULL
4306 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004307 {
4308 // Set curwin to be our win, temporarily. Also set the tabpage,
4309 // otherwise the window is not valid. Only do this when needed,
4310 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004311 // If we have a buffer reference avoid the switching, we're saving and
4312 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004313 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004314 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004315 {
LemonBoy47d4e312022-05-04 18:12:55 +01004316 // Handle options. There are no tab-local options.
4317 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004318 {
LemonBoy47d4e312022-05-04 18:12:55 +01004319 buf_T *save_curbuf = curbuf;
4320
4321 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004322 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004323 curbuf = buf;
4324
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004325 if (varname[1] == NUL)
4326 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004327 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004328 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004329
4330 if (opts != NULL)
4331 {
4332 rettv_dict_set(rettv, opts);
4333 done = TRUE;
4334 }
4335 }
LemonBoy47d4e312022-05-04 18:12:55 +01004336 else if (eval_option(&varname, rettv, TRUE) == OK)
4337 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004338 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004339
4340 curbuf = save_curbuf;
4341 }
4342 else if (*varname == NUL)
4343 {
4344 // Empty string: return a dict with all the local variables.
4345 if (htname == 'b')
4346 v = &buf->b_bufvar;
4347 else if (htname == 'w')
4348 v = &win->w_winvar;
4349 else
4350 v = &tp->tp_winvar;
4351 copy_tv(&v->di_tv, rettv);
4352 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004353 }
4354 else
4355 {
LemonBoy47d4e312022-05-04 18:12:55 +01004356 hashtab_T *ht;
4357
4358 if (htname == 'b')
4359 ht = &buf->b_vars->dv_hashtab;
4360 else if (htname == 'w')
4361 ht = &win->w_vars->dv_hashtab;
4362 else
4363 ht = &tp->tp_vars->dv_hashtab;
4364
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004365 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004366 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004367 if (v != NULL)
4368 {
4369 copy_tv(&v->di_tv, rettv);
4370 done = TRUE;
4371 }
4372 }
4373 }
4374
4375 if (need_switch_win)
4376 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004377 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004378 }
4379
LemonBoy47d4e312022-05-04 18:12:55 +01004380 if (!done && deftv->v_type != VAR_UNKNOWN)
4381 // use the default value
4382 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004383
4384 --emsg_off;
4385}
4386
4387/*
LemonBoy47d4e312022-05-04 18:12:55 +01004388 * getwinvar() and gettabwinvar()
4389 */
4390 static void
4391getwinvar(
4392 typval_T *argvars,
4393 typval_T *rettv,
4394 int off) // 1 for gettabwinvar()
4395{
4396 char_u *varname;
4397 tabpage_T *tp;
4398 win_T *win;
4399
4400 if (off == 1)
4401 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4402 else
4403 tp = curtab;
4404 win = find_win_by_nr(&argvars[off], tp);
4405 varname = tv_get_string_chk(&argvars[off + 1]);
4406
4407 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4408}
4409
4410/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004411 * Set option "varname" to the value of "varp" for the current buffer/window.
4412 */
4413 static void
4414set_option_from_tv(char_u *varname, typval_T *varp)
4415{
4416 long numval = 0;
4417 char_u *strval;
4418 char_u nbuf[NUMBUFLEN];
4419 int error = FALSE;
4420
zeertzjq4c7cb372023-06-14 16:39:54 +01004421 int opt_idx = findoption(varname);
4422 if (opt_idx < 0)
4423 {
4424 semsg(_(e_unknown_option_str_2), varname);
4425 return;
4426 }
4427 int opt_p_flags = get_option_flags(opt_idx);
4428
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004429 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004430 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004431 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004432 {
4433 emsg(_(e_string_required));
4434 return;
4435 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004436 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004437 strval = (char_u *)"0"; // avoid using "false"
4438 }
4439 else
4440 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004441 if ((opt_p_flags & (P_NUM|P_BOOL))
4442 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004443 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004444 if (!error)
4445 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004446 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004447 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004448 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004449}
4450
4451/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004452 * "setwinvar()" and "settabwinvar()" functions
4453 */
4454 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004455setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004456{
4457 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004458 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004459 int need_switch_win;
4460 char_u *varname, *winvarname;
4461 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004462 tabpage_T *tp = NULL;
4463
4464 if (check_secure())
4465 return;
4466
4467 if (off == 1)
4468 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4469 else
4470 tp = curtab;
4471 win = find_win_by_nr(&argvars[off], tp);
4472 varname = tv_get_string_chk(&argvars[off + 1]);
4473 varp = &argvars[off + 2];
4474
zeertzjqea720ae2023-01-03 10:54:09 +00004475 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004476 return;
4477
4478 need_switch_win = !(tp == curtab && win == curwin);
4479 if (!need_switch_win
4480 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004481 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004482 if (*varname == '&')
4483 set_option_from_tv(varname + 1, varp);
4484 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004485 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004486 winvarname = alloc(STRLEN(varname) + 3);
4487 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004488 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004489 STRCPY(winvarname, "w:");
4490 STRCPY(winvarname + 2, varname);
4491 set_var(winvarname, varp, TRUE);
4492 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004493 }
4494 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004495 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004496 if (need_switch_win)
4497 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004498}
4499
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004500/*
4501 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4502 * v:option_type, and v:option_command.
4503 */
4504 void
4505reset_v_option_vars(void)
4506{
4507 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4508 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4509 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4510 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4511 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4512 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4513}
4514
4515/*
4516 * Add an assert error to v:errors.
4517 */
4518 void
4519assert_error(garray_T *gap)
4520{
4521 struct vimvar *vp = &vimvars[VV_ERRORS];
4522
Bram Moolenaard787e402021-12-24 21:36:12 +00004523 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004524 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004525 set_vim_var_list(VV_ERRORS, list_alloc());
4526 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4527}
4528
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004529 int
4530var_exists(char_u *var)
4531{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004532 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004533 char_u *name;
4534 char_u *tofree;
4535 typval_T tv;
4536 int len = 0;
4537 int n = FALSE;
4538
4539 // get_name_len() takes care of expanding curly braces
4540 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004541 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004542 if (len > 0)
4543 {
4544 if (tofree != NULL)
4545 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004546 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004547 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004548 if (n)
4549 {
4550 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004551 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004552 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4553 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004554 if (n)
4555 clear_tv(&tv);
4556 }
4557 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004558 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004559 n = FALSE;
4560
4561 vim_free(tofree);
4562 return n;
4563}
4564
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004565static lval_T *redir_lval = NULL;
4566#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4567static garray_T redir_ga; // only valid when redir_lval is not NULL
4568static char_u *redir_endp = NULL;
4569static char_u *redir_varname = NULL;
4570
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004571 int
4572alloc_redir_lval(void)
4573{
4574 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4575 if (redir_lval == NULL)
4576 return FAIL;
4577 return OK;
4578}
4579
4580 void
4581clear_redir_lval(void)
4582{
4583 VIM_CLEAR(redir_lval);
4584}
4585
4586 void
4587init_redir_ga(void)
4588{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004589 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004590}
4591
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004592/*
4593 * Start recording command output to a variable
4594 * When "append" is TRUE append to an existing variable.
4595 * Returns OK if successfully completed the setup. FAIL otherwise.
4596 */
4597 int
4598var_redir_start(char_u *name, int append)
4599{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004600 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004601 typval_T tv;
4602
4603 // Catch a bad name early.
4604 if (!eval_isnamec1(*name))
4605 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004606 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004607 return FAIL;
4608 }
4609
4610 // Make a copy of the name, it is used in redir_lval until redir ends.
4611 redir_varname = vim_strsave(name);
4612 if (redir_varname == NULL)
4613 return FAIL;
4614
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004615 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004616 {
4617 var_redir_stop();
4618 return FAIL;
4619 }
4620
4621 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004622 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004623
4624 // Parse the variable name (can be a dict or list entry).
4625 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4626 FNE_CHECK_START);
4627 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4628 {
4629 clear_lval(redir_lval);
4630 if (redir_endp != NULL && *redir_endp != NUL)
4631 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004632 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004633 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004634 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004635 redir_endp = NULL; // don't store a value, only cleanup
4636 var_redir_stop();
4637 return FAIL;
4638 }
4639
4640 // check if we can write to the variable: set it to or append an empty
4641 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004642 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004643 tv.v_type = VAR_STRING;
4644 tv.vval.v_string = (char_u *)"";
4645 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004646 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004647 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004648 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004649 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004650 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004651 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004652 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004653 {
4654 redir_endp = NULL; // don't store a value, only cleanup
4655 var_redir_stop();
4656 return FAIL;
4657 }
4658
4659 return OK;
4660}
4661
4662/*
4663 * Append "value[value_len]" to the variable set by var_redir_start().
4664 * The actual appending is postponed until redirection ends, because the value
4665 * appended may in fact be the string we write to, changing it may cause freed
4666 * memory to be used:
4667 * :redir => foo
4668 * :let foo
4669 * :redir END
4670 */
4671 void
4672var_redir_str(char_u *value, int value_len)
4673{
4674 int len;
4675
4676 if (redir_lval == NULL)
4677 return;
4678
4679 if (value_len == -1)
4680 len = (int)STRLEN(value); // Append the entire string
4681 else
4682 len = value_len; // Append only "value_len" characters
4683
4684 if (ga_grow(&redir_ga, len) == OK)
4685 {
4686 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4687 redir_ga.ga_len += len;
4688 }
4689 else
4690 var_redir_stop();
4691}
4692
4693/*
4694 * Stop redirecting command output to a variable.
4695 * Frees the allocated memory.
4696 */
4697 void
4698var_redir_stop(void)
4699{
4700 typval_T tv;
4701
4702 if (EVALCMD_BUSY)
4703 {
4704 redir_lval = NULL;
4705 return;
4706 }
4707
4708 if (redir_lval != NULL)
4709 {
4710 // If there was no error: assign the text to the variable.
4711 if (redir_endp != NULL)
4712 {
4713 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4714 tv.v_type = VAR_STRING;
4715 tv.vval.v_string = redir_ga.ga_data;
4716 // Call get_lval() again, if it's inside a Dict or List it may
4717 // have changed.
4718 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4719 FALSE, FALSE, 0, FNE_CHECK_START);
4720 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004722 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004723 clear_lval(redir_lval);
4724 }
4725
4726 // free the collected output
4727 VIM_CLEAR(redir_ga.ga_data);
4728
4729 VIM_CLEAR(redir_lval);
4730 }
4731 VIM_CLEAR(redir_varname);
4732}
4733
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004734/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004735 * Get the collected redirected text and clear redir_ga.
4736 */
4737 char_u *
4738get_clear_redir_ga(void)
4739{
4740 char_u *res;
4741
4742 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4743 res = redir_ga.ga_data;
4744 redir_ga.ga_data = NULL;
4745 return res;
4746}
4747
4748/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004749 * "gettabvar()" function
4750 */
4751 void
4752f_gettabvar(typval_T *argvars, typval_T *rettv)
4753{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004754 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004755 tabpage_T *tp;
4756 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004757
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004758 if (in_vim9script()
4759 && (check_for_number_arg(argvars, 0) == FAIL
4760 || check_for_string_arg(argvars, 1) == FAIL))
4761 return;
4762
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004763 varname = tv_get_string_chk(&argvars[1]);
4764 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004765 if (tp != NULL)
4766 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4767 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004768
LemonBoy47d4e312022-05-04 18:12:55 +01004769 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004770}
4771
4772/*
4773 * "gettabwinvar()" function
4774 */
4775 void
4776f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4777{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004778 if (in_vim9script()
4779 && (check_for_number_arg(argvars, 0) == FAIL
4780 || check_for_number_arg(argvars, 1) == FAIL
4781 || check_for_string_arg(argvars, 2) == FAIL))
4782 return;
4783
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004784 getwinvar(argvars, rettv, 1);
4785}
4786
4787/*
4788 * "getwinvar()" function
4789 */
4790 void
4791f_getwinvar(typval_T *argvars, typval_T *rettv)
4792{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004793 if (in_vim9script()
4794 && (check_for_number_arg(argvars, 0) == FAIL
4795 || check_for_string_arg(argvars, 1) == FAIL))
4796 return;
4797
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004798 getwinvar(argvars, rettv, 0);
4799}
4800
4801/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004802 * "getbufvar()" function
4803 */
4804 void
4805f_getbufvar(typval_T *argvars, typval_T *rettv)
4806{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004807 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004808 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004809
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004810 if (in_vim9script()
4811 && (check_for_buffer_arg(argvars, 0) == FAIL
4812 || check_for_string_arg(argvars, 1) == FAIL))
4813 return;
4814
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004815 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004816 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004817
LemonBoy47d4e312022-05-04 18:12:55 +01004818 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004819}
4820
4821/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004822 * "settabvar()" function
4823 */
4824 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004825f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004826{
4827 tabpage_T *save_curtab;
4828 tabpage_T *tp;
4829 char_u *varname, *tabvarname;
4830 typval_T *varp;
4831
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004832 if (check_secure())
4833 return;
4834
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004835 if (in_vim9script()
4836 && (check_for_number_arg(argvars, 0) == FAIL
4837 || check_for_string_arg(argvars, 1) == FAIL))
4838 return;
4839
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004840 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4841 varname = tv_get_string_chk(&argvars[1]);
4842 varp = &argvars[2];
4843
zeertzjqea720ae2023-01-03 10:54:09 +00004844 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004845 return;
4846
4847 save_curtab = curtab;
4848 goto_tabpage_tp(tp, FALSE, FALSE);
4849
4850 tabvarname = alloc(STRLEN(varname) + 3);
4851 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004852 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004853 STRCPY(tabvarname, "t:");
4854 STRCPY(tabvarname + 2, varname);
4855 set_var(tabvarname, varp, TRUE);
4856 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004857 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004858
4859 // Restore current tabpage
4860 if (valid_tabpage(save_curtab))
4861 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004862}
4863
4864/*
4865 * "settabwinvar()" function
4866 */
4867 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004868f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004869{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004870 if (in_vim9script()
4871 && (check_for_number_arg(argvars, 0) == FAIL
4872 || check_for_number_arg(argvars, 1) == FAIL
4873 || check_for_string_arg(argvars, 2) == FAIL))
4874 return;
4875
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004876 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004877}
4878
4879/*
4880 * "setwinvar()" function
4881 */
4882 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004883f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004884{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004885 if (in_vim9script()
4886 && (check_for_number_arg(argvars, 0) == FAIL
4887 || check_for_string_arg(argvars, 1) == FAIL))
4888 return;
4889
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004890 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004891}
4892
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004893/*
4894 * "setbufvar()" function
4895 */
4896 void
4897f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4898{
4899 buf_T *buf;
4900 char_u *varname, *bufvarname;
4901 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004902
4903 if (check_secure())
4904 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004905
4906 if (in_vim9script()
4907 && (check_for_buffer_arg(argvars, 0) == FAIL
4908 || check_for_string_arg(argvars, 1) == FAIL))
4909 return;
4910
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004911 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004912 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004913 varp = &argvars[2];
4914
zeertzjqea720ae2023-01-03 10:54:09 +00004915 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004916 return;
4917
4918 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004919 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004920 aco_save_T aco;
4921
4922 // Set curbuf to be our buf, temporarily.
4923 aucmd_prepbuf(&aco, buf);
4924 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004925 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004926 // Only when it worked to set "curbuf".
4927 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004928
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004929 // reset notion of buffer
4930 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004931 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004932 }
4933 else
4934 {
4935 bufvarname = alloc(STRLEN(varname) + 3);
4936 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004937 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004938 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02004939
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004940 curbuf = buf;
4941 STRCPY(bufvarname, "b:");
4942 STRCPY(bufvarname + 2, varname);
4943 set_var(bufvarname, varp, TRUE);
4944 vim_free(bufvarname);
4945 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004946 }
4947 }
4948}
4949
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004950/*
4951 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004952 * When "arg" is zero "res.cb_name" is set to an empty string.
4953 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
4954 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004955 */
4956 callback_T
4957get_callback(typval_T *arg)
4958{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004959 callback_T res;
4960 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004961
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004962 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004963 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4964 {
4965 res.cb_partial = arg->vval.v_partial;
4966 ++res.cb_partial->pt_refcount;
4967 res.cb_name = partial_name(res.cb_partial);
4968 }
4969 else
4970 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01004971 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4972 && isdigit(*arg->vval.v_string))
4973 r = FAIL;
4974 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004975 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004976 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004977 if (arg->v_type == VAR_STRING)
4978 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004979 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004980 if (name != NULL)
4981 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00004982 res.cb_name = name;
4983 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004984 }
4985 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004986 func_ref(res.cb_name);
4987 }
4988 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004989 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004990 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004991 r = FAIL;
4992
4993 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004994 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004995 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004996 res.cb_name = NULL;
4997 }
4998 }
4999 return res;
5000}
5001
5002/*
5003 * Copy a callback into a typval_T.
5004 */
5005 void
5006put_callback(callback_T *cb, typval_T *tv)
5007{
5008 if (cb->cb_partial != NULL)
5009 {
5010 tv->v_type = VAR_PARTIAL;
5011 tv->vval.v_partial = cb->cb_partial;
5012 ++tv->vval.v_partial->pt_refcount;
5013 }
5014 else
5015 {
5016 tv->v_type = VAR_FUNC;
5017 tv->vval.v_string = vim_strsave(cb->cb_name);
5018 func_ref(cb->cb_name);
5019 }
5020}
5021
5022/*
5023 * Make a copy of "src" into "dest", allocating the function name if needed,
5024 * without incrementing the refcount.
5025 */
5026 void
5027set_callback(callback_T *dest, callback_T *src)
5028{
5029 if (src->cb_partial == NULL)
5030 {
5031 // just a function name, make a copy
5032 dest->cb_name = vim_strsave(src->cb_name);
5033 dest->cb_free_name = TRUE;
5034 }
5035 else
5036 {
5037 // cb_name is a pointer into cb_partial
5038 dest->cb_name = src->cb_name;
5039 dest->cb_free_name = FALSE;
5040 }
5041 dest->cb_partial = src->cb_partial;
5042}
5043
5044/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005045 * Copy callback from "src" to "dest", incrementing the refcounts.
5046 */
5047 void
5048copy_callback(callback_T *dest, callback_T *src)
5049{
5050 dest->cb_partial = src->cb_partial;
5051 if (dest->cb_partial != NULL)
5052 {
5053 dest->cb_name = src->cb_name;
5054 dest->cb_free_name = FALSE;
5055 ++dest->cb_partial->pt_refcount;
5056 }
5057 else
5058 {
5059 dest->cb_name = vim_strsave(src->cb_name);
5060 dest->cb_free_name = TRUE;
5061 func_ref(src->cb_name);
5062 }
5063}
5064
5065/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005066 * When a callback refers to an autoload import, change the function name to
5067 * the "path#name" form. Uses the current script context.
5068 * Only works when the name is allocated.
5069 */
5070 void
5071expand_autload_callback(callback_T *cb)
5072{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005073 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005074 char_u *p;
5075 imported_T *import;
5076
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005077 if (!in_vim9script() || cb->cb_name == NULL
5078 || (!cb->cb_free_name
5079 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005080 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005081 if (cb->cb_partial != NULL)
5082 name = cb->cb_partial->pt_name;
5083 else
5084 name = cb->cb_name;
5085 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005086 if (p == NULL)
5087 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005088
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005089 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005090 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5091 return;
5092
5093 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5094 if (si->sn_autoload_prefix == NULL)
5095 return;
5096
5097 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5098 if (newname == NULL)
5099 return;
5100
5101 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005102 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005103 if (cb->cb_name == cb->cb_partial->pt_name)
5104 cb->cb_name = newname;
5105 vim_free(cb->cb_partial->pt_name);
5106 cb->cb_partial->pt_name = newname;
5107 }
5108 else
5109 {
5110 vim_free(cb->cb_name);
5111 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005112 }
5113}
5114
5115/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005116 * Unref/free "callback" returned by get_callback() or set_callback().
5117 */
5118 void
5119free_callback(callback_T *callback)
5120{
5121 if (callback->cb_partial != NULL)
5122 {
5123 partial_unref(callback->cb_partial);
5124 callback->cb_partial = NULL;
5125 }
5126 else if (callback->cb_name != NULL)
5127 func_unref(callback->cb_name);
5128 if (callback->cb_free_name)
5129 {
5130 vim_free(callback->cb_name);
5131 callback->cb_free_name = FALSE;
5132 }
5133 callback->cb_name = NULL;
5134}
5135
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005136#endif // FEAT_EVAL