blob: d4dd0add2aa2a98887ed929b471a04a1aff4ddc3 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000142 {VV_NAME("t_class", VAR_NUMBER), NULL, VV_RO},
143 {VV_NAME("t_object", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaard787e402021-12-24 21:36:12 +0000144 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
148 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
149 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
150 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
151 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
152 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
153 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
154 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
155 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
156 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
157 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
158 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000159 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200160 {VV_NAME("python3_version", VAR_NUMBER), NULL, VV_RO},
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200161 {VV_NAME("t_typealias", VAR_NUMBER), NULL, VV_RO},
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100162 {VV_NAME("t_enum", VAR_NUMBER), NULL, VV_RO},
163 {VV_NAME("t_enumvalue", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164};
165
166// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000167#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200168#define vv_nr vv_di.di_tv.vval.v_number
169#define vv_float vv_di.di_tv.vval.v_float
170#define vv_str vv_di.di_tv.vval.v_string
171#define vv_list vv_di.di_tv.vval.v_list
172#define vv_dict vv_di.di_tv.vval.v_dict
173#define vv_blob vv_di.di_tv.vval.v_blob
174#define vv_tv vv_di.di_tv
175
176static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200178#define vimvarht vimvardict.dv_hashtab
179
180// for VIM_VERSION_ defines
181#include "version.h"
182
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200183static void list_glob_vars(int *first);
184static void list_buf_vars(int *first);
185static void list_win_vars(int *first);
186static void list_tab_vars(int *first);
187static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100188static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200189static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
190static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200191static void list_one_var(dictitem_T *v, char *prefix, int *first);
192static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
193
194/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200195 * Initialize global and vim special variables
196 */
197 void
198evalvars_init(void)
199{
200 int i;
201 struct vimvar *p;
202
203 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
204 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
205 vimvardict.dv_lock = VAR_FIXED;
206 hash_init(&compat_hashtab);
207
208 for (i = 0; i < VV_LEN; ++i)
209 {
210 p = &vimvars[i];
211 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
212 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100213 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200214 getout(1);
215 }
216 STRCPY(p->vv_di.di_key, p->vv_name);
217 if (p->vv_flags & VV_RO)
218 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
219 else if (p->vv_flags & VV_RO_SBX)
220 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
221 else
222 p->vv_di.di_flags = DI_FLAGS_FIX;
223
224 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000225 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000226 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227 if (p->vv_flags & VV_COMPAT)
228 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000229 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200230 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200231 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
232 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233
234 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
235 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100236 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200237 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
238 set_vim_var_list(VV_ERRORS, list_alloc());
239 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
240
241 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
242 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
243 set_vim_var_nr(VV_NONE, VVAL_NONE);
244 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100245 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
246 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100247 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000248 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
249 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
250 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000251 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200252
253 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
254 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
255 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
256 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
257 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
258 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
259 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
260 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
261 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
262 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
263 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000264 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
265 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200266 set_vim_var_nr(VV_TYPE_TYPEALIAS, VAR_TYPE_TYPEALIAS);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100267 set_vim_var_nr(VV_TYPE_ENUM, VAR_TYPE_ENUM);
268 set_vim_var_nr(VV_TYPE_ENUMVALUE, VAR_TYPE_ENUMVALUE);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200269
270 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
271
Drew Vogele30d1022021-10-24 20:35:07 +0100272 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
273
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200274#ifdef FEAT_PYTHON3
275 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
276#endif
277
Bram Moolenaar439c0362020-06-06 15:58:03 +0200278 // Default for v:register is not 0 but '"'. This is adjusted once the
279 // clipboard has been setup by calling reset_reg_var().
280 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200281}
282
283#if defined(EXITFREE) || defined(PROTO)
284/*
285 * Free all vim variables information on exit
286 */
287 void
288evalvars_clear(void)
289{
290 int i;
291 struct vimvar *p;
292
293 for (i = 0; i < VV_LEN; ++i)
294 {
295 p = &vimvars[i];
296 if (p->vv_di.di_tv.v_type == VAR_STRING)
297 VIM_CLEAR(p->vv_str);
298 else if (p->vv_di.di_tv.v_type == VAR_LIST)
299 {
300 list_unref(p->vv_list);
301 p->vv_list = NULL;
302 }
303 }
304 hash_clear(&vimvarht);
305 hash_init(&vimvarht); // garbage_collect() will access it
306 hash_clear(&compat_hashtab);
307
308 // global variables
309 vars_clear(&globvarht);
310
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100311 // Script-local variables. Clear all the variables here.
312 // The scriptvar_T is cleared later in free_scriptnames(), because a
313 // variable in one script might hold a reference to the whole scope of
314 // another script.
315 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200316 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200317}
318#endif
319
320 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200321garbage_collect_globvars(int copyID)
322{
323 return set_ref_in_ht(&globvarht, copyID, NULL);
324}
325
326 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327garbage_collect_vimvars(int copyID)
328{
329 return set_ref_in_ht(&vimvarht, copyID, NULL);
330}
331
332 int
333garbage_collect_scriptvars(int copyID)
334{
Bram Moolenaared234f22020-10-15 20:42:20 +0200335 int i;
336 int idx;
337 int abort = FALSE;
338 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100340 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200341 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200342 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
343
Bram Moolenaared234f22020-10-15 20:42:20 +0200344 si = SCRIPT_ITEM(i);
345 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
346 {
347 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
348
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100349 if (sv->sv_name != NULL)
350 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200351 }
352 }
353
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200354 return abort;
355}
356
357/*
358 * Set an internal variable to a string value. Creates the variable if it does
359 * not already exist.
360 */
361 void
362set_internal_string_var(char_u *name, char_u *value)
363{
364 char_u *val;
365 typval_T *tvp;
366
367 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000368 if (val == NULL)
369 return;
370
371 tvp = alloc_string_tv(val);
372 if (tvp == NULL)
373 return;
374
375 set_var(name, tvp, FALSE);
376 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200377}
378
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200379 int
380eval_charconvert(
381 char_u *enc_from,
382 char_u *enc_to,
383 char_u *fname_from,
384 char_u *fname_to)
385{
386 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000387 sctx_T saved_sctx = current_sctx;
388 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200389
390 set_vim_var_string(VV_CC_FROM, enc_from, -1);
391 set_vim_var_string(VV_CC_TO, enc_to, -1);
392 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
393 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000394 ctx = get_option_sctx("charconvert");
395 if (ctx != NULL)
396 current_sctx = *ctx;
397
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100398 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200399 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000400
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200401 set_vim_var_string(VV_CC_FROM, NULL, -1);
402 set_vim_var_string(VV_CC_TO, NULL, -1);
403 set_vim_var_string(VV_FNAME_IN, NULL, -1);
404 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000405 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200406
407 if (err)
408 return FAIL;
409 return OK;
410}
411
412# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
413 int
414eval_printexpr(char_u *fname, char_u *args)
415{
416 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000417 sctx_T saved_sctx = current_sctx;
418 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200419
420 set_vim_var_string(VV_FNAME_IN, fname, -1);
421 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000422 ctx = get_option_sctx("printexpr");
423 if (ctx != NULL)
424 current_sctx = *ctx;
425
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100426 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200427 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000428
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200429 set_vim_var_string(VV_FNAME_IN, NULL, -1);
430 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000431 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200432
433 if (err)
434 {
435 mch_remove(fname);
436 return FAIL;
437 }
438 return OK;
439}
440# endif
441
442# if defined(FEAT_DIFF) || defined(PROTO)
443 void
444eval_diff(
445 char_u *origfile,
446 char_u *newfile,
447 char_u *outfile)
448{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449 sctx_T saved_sctx = current_sctx;
450 sctx_T *ctx;
451 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200452
453 set_vim_var_string(VV_FNAME_IN, origfile, -1);
454 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
455 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000456
457 ctx = get_option_sctx("diffexpr");
458 if (ctx != NULL)
459 current_sctx = *ctx;
460
461 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100462 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000463 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000464
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465 set_vim_var_string(VV_FNAME_IN, NULL, -1);
466 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
467 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000468 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200469}
470
471 void
472eval_patch(
473 char_u *origfile,
474 char_u *difffile,
475 char_u *outfile)
476{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000477 sctx_T saved_sctx = current_sctx;
478 sctx_T *ctx;
479 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200480
481 set_vim_var_string(VV_FNAME_IN, origfile, -1);
482 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
483 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000484
485 ctx = get_option_sctx("patchexpr");
486 if (ctx != NULL)
487 current_sctx = *ctx;
488
489 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100490 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000491 free_tv(tv);
492
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200493 set_vim_var_string(VV_FNAME_IN, NULL, -1);
494 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
495 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000496 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200497}
498# endif
499
500#if defined(FEAT_SPELL) || defined(PROTO)
501/*
502 * Evaluate an expression to a list with suggestions.
503 * For the "expr:" part of 'spellsuggest'.
504 * Returns NULL when there is an error.
505 */
506 list_T *
507eval_spell_expr(char_u *badword, char_u *expr)
508{
509 typval_T save_val;
510 typval_T rettv;
511 list_T *list = NULL;
512 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000513 sctx_T saved_sctx = current_sctx;
514 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100515 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200516
517 // Set "v:val" to the bad word.
518 prepare_vimvar(VV_VAL, &save_val);
519 set_vim_var_string(VV_VAL, badword, -1);
520 if (p_verbose == 0)
521 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 ctx = get_option_sctx("spellsuggest");
523 if (ctx != NULL)
524 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200525
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100526 r = may_call_simple_func(p, &rettv);
527 if (r == NOTDONE)
528 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
529 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200530 {
531 if (rettv.v_type != VAR_LIST)
532 clear_tv(&rettv);
533 else
534 list = rettv.vval.v_list;
535 }
536
537 if (p_verbose == 0)
538 --emsg_off;
539 clear_tv(get_vim_var_tv(VV_VAL));
540 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000541 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200542
543 return list;
544}
545
546/*
547 * "list" is supposed to contain two items: a word and a number. Return the
548 * word in "pp" and the number as the return value.
549 * Return -1 if anything isn't right.
550 * Used to get the good word and score from the eval_spell_expr() result.
551 */
552 int
553get_spellword(list_T *list, char_u **pp)
554{
555 listitem_T *li;
556
557 li = list->lv_first;
558 if (li == NULL)
559 return -1;
560 *pp = tv_get_string(&li->li_tv);
561
562 li = li->li_next;
563 if (li == NULL)
564 return -1;
565 return (int)tv_get_number(&li->li_tv);
566}
567#endif
568
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200569/*
570 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200571 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200572 * When not used yet add the variable to the v: hashtable.
573 */
574 void
575prepare_vimvar(int idx, typval_T *save_tv)
576{
577 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200578 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000579 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000580 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200581}
582
583/*
584 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200585 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200586 * When no longer defined, remove the variable from the v: hashtable.
587 */
588 void
589restore_vimvar(int idx, typval_T *save_tv)
590{
591 hashitem_T *hi;
592
593 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000594 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
595 return;
596
597 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
598 if (HASHITEM_EMPTY(hi))
599 internal_error("restore_vimvar()");
600 else
601 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200602}
603
604/*
605 * List Vim variables.
606 */
607 static void
608list_vim_vars(int *first)
609{
610 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
611}
612
613/*
614 * List script-local variables, if there is a script.
615 */
616 static void
617list_script_vars(int *first)
618{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200619 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200620 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
621 "s:", FALSE, first);
622}
623
624/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100625 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
626 * But only when an identifier character follows.
627 */
628 int
629is_scoped_variable(char_u *name)
630{
631 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
632 && name[1] == ':'
633 && eval_isnamec(name[2]);
634}
635
636/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100637 * Evaluate one Vim expression {expr} in string "p" and append the
638 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100639 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100640 * Return a pointer to the character after "}", NULL for an error.
641 */
642 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100643eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100644{
645 char_u *block_start = skipwhite(p + 1); // skip the opening {
646 char_u *block_end = block_start;
647 char_u *expr_val;
648
649 if (*block_start == NUL)
650 {
651 semsg(_(e_missing_close_curly_str), p);
652 return NULL;
653 }
654 if (skip_expr(&block_end, NULL) == FAIL)
655 return NULL;
656 block_end = skipwhite(block_end);
657 if (*block_end != '}')
658 {
659 semsg(_(e_missing_close_curly_str), p);
660 return NULL;
661 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100662 if (evaluate)
663 {
664 *block_end = NUL;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100665 expr_val = eval_to_string(block_start, TRUE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100666 *block_end = '}';
667 if (expr_val == NULL)
668 return NULL;
669 ga_concat(gap, expr_val);
670 vim_free(expr_val);
671 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100672
673 return block_end + 1;
674}
675
676/*
677 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
678 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
679 * Used for a heredoc assignment.
680 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100681 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100682 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100683eval_all_expr_in_str(char_u *str)
684{
685 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100686 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100687
688 ga_init2(&ga, 1, 80);
689 p = str;
690
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100691 while (*p != NUL)
692 {
LemonBoy2eaef102022-05-06 13:14:50 +0100693 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100694 int escaped_brace = FALSE;
695
696 // Look for a block start.
697 lit_start = p;
698 while (*p != '{' && *p != '}' && *p != NUL)
699 ++p;
700
701 if (*p != NUL && *p == p[1])
702 {
703 // Escaped brace, unescape and continue.
704 // Include the brace in the literal string.
705 ++p;
706 escaped_brace = TRUE;
707 }
708 else if (*p == '}')
709 {
710 semsg(_(e_stray_closing_curly_str), str);
711 ga_clear(&ga);
712 return NULL;
713 }
714
715 // Append the literal part.
716 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
717
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100718 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100719 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100720
LemonBoy2eaef102022-05-06 13:14:50 +0100721 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722 {
LemonBoy2eaef102022-05-06 13:14:50 +0100723 // Skip the second brace.
724 ++p;
725 continue;
726 }
727
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100728 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100729 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100730 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100731 {
732 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100733 return NULL;
734 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100735 }
736 ga_append(&ga, NUL);
737
738 return ga.ga_data;
739}
740
741/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 * Get a list of lines from a HERE document. The here document is a list of
743 * lines surrounded by a marker.
744 * cmd << {marker}
745 * {line1}
746 * {line2}
747 * ....
748 * {marker}
749 *
750 * The {marker} is a string. If the optional 'trim' word is supplied before the
751 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100752 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200753 *
754 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100755 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200756 * missing, then '.' is accepted as a marker.
757 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100758 * When compiling a heredoc assignment to a variable in a Vim9 def function,
759 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
760 * string values from the heredoc, vim9 instructions are generated. On success
761 * the returned list will be empty.
762 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100763 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100766heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100768 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200769 char_u *marker;
770 list_T *l;
771 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100772 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200773 int marker_indent_len = 0;
774 int text_indent_len = 0;
775 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200776 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200777 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100778 int evalstr = FALSE;
779 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100780 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
781 int count = 0;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200782 int heredoc_in_string = FALSE;
783 char_u *line_arg = NULL;
zeertzjq1f5175d2024-04-13 17:52:26 +0200784 char_u *nl_ptr = vim_strchr(cmd, '\n');
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785
zeertzjq1f5175d2024-04-13 17:52:26 +0200786 if (nl_ptr != NULL)
787 {
788 heredoc_in_string = TRUE;
789 line_arg = nl_ptr + 1;
790 *nl_ptr = NUL;
791 }
792 else if (eap->ea_getline == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200793 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000794 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200795 return NULL;
796 }
797
798 // Check for the optional 'trim' word before the marker
799 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200800
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100801 while (TRUE)
802 {
803 if (STRNCMP(cmd, "trim", 4) == 0
804 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100806 cmd = skipwhite(cmd + 4);
807
808 // Trim the indentation from all the lines in the here document.
809 // The amount of indentation trimmed is the same as the indentation
810 // of the first line after the :let command line. To find the end
811 // marker the indent of the :let command line is trimmed.
812 p = *eap->cmdlinep;
813 while (VIM_ISWHITE(*p))
814 {
815 p++;
816 marker_indent_len++;
817 }
818 text_indent_len = -1;
819
820 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100822 if (STRNCMP(cmd, "eval", 4) == 0
823 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
824 {
825 cmd = skipwhite(cmd + 4);
826 evalstr = TRUE;
827 continue;
828 }
829 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200830 }
831
832 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200833 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 {
835 marker = skipwhite(cmd);
zeertzjq1f5175d2024-04-13 17:52:26 +0200836 p = skiptowhite(marker);
837 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000839 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840 return NULL;
841 }
842 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200843 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200844 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000845 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846 return NULL;
847 }
848 }
849 else
850 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200851 // When getting lines for an embedded script, if the marker is missing,
852 // accept '.' as the marker.
853 if (script_get)
854 marker = dot;
855 else
856 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000857 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200858 return NULL;
859 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200860 }
861
862 l = list_alloc();
863 if (l == NULL)
864 return NULL;
865
866 for (;;)
867 {
868 int mi = 0;
869 int ti = 0;
870
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200871 if (heredoc_in_string)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200873 char_u *next_line;
874
875 // heredoc in a string separated by newlines. Get the next line
876 // from the string.
877
878 if (*line_arg == NUL)
879 {
880 semsg(_(e_missing_end_marker_str), marker);
881 break;
882 }
883
884 theline = line_arg;
885 next_line = vim_strchr(theline, '\n');
886 if (next_line == NULL)
887 line_arg += STRLEN(line_arg);
888 else
889 {
890 *next_line = NUL;
891 line_arg = next_line + 1;
892 }
893 }
894 else
895 {
896 vim_free(theline);
897 theline = eap->ea_getline(NUL, eap->cookie, 0, FALSE);
898 if (theline == NULL)
899 {
900 semsg(_(e_missing_end_marker_str), marker);
901 break;
902 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200903 }
904
905 // with "trim": skip the indent matching the :let line to find the
906 // marker
907 if (marker_indent_len > 0
908 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
909 mi = marker_indent_len;
910 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200911 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200912
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100913 // If expression evaluation failed in the heredoc, then skip till the
914 // end marker.
915 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100916 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100917
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200918 if (text_indent_len == -1 && *theline != NUL)
919 {
920 // set the text indent from the first line.
921 p = theline;
922 text_indent_len = 0;
923 while (VIM_ISWHITE(*p))
924 {
925 p++;
926 text_indent_len++;
927 }
928 text_indent = vim_strnsave(theline, text_indent_len);
929 }
930 // with "trim": skip the indent matching the first line
931 if (text_indent != NULL)
932 for (ti = 0; ti < text_indent_len; ++ti)
933 if (theline[ti] != text_indent[ti])
934 break;
935
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100936 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100937 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100938 {
LemonBoy2eaef102022-05-06 13:14:50 +0100939 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100940 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100941 vim_free(theline);
942 vim_free(text_indent);
943 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100944 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100945 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100946 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100947 else
948 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200949 int free_str = FALSE;
950
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100951 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100952 {
953 str = eval_all_expr_in_str(str);
954 if (str == NULL)
955 {
956 // expression evaluation failed
957 eval_failed = TRUE;
958 continue;
959 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200960 free_str = TRUE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100961 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100962
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100963 if (list_append_string(l, str, -1) == FAIL)
964 break;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200965 if (free_str)
966 vim_free(str);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100967 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200969 if (heredoc_in_string)
970 // Next command follows the heredoc in the string.
971 eap->nextcmd = line_arg;
972 else
973 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200974 vim_free(text_indent);
975
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100976 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
977 generate_NEWLIST(cctx, count, FALSE);
978
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100979 if (eval_failed)
980 {
981 // expression evaluation in the heredoc failed
982 list_free(l);
983 return NULL;
984 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200985 return l;
986}
987
988/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200989 * Vim9 variable declaration:
990 * ":var name"
991 * ":var name: type"
992 * ":var name = expr"
993 * ":var name: type = expr"
994 * etc.
995 */
996 void
997ex_var(exarg_T *eap)
998{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000999 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +00001000 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +00001001
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001002 if (!in_vim9script())
1003 {
1004 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
1005 return;
1006 }
Bram Moolenaare1d12112022-03-05 11:37:48 +00001007 has_var = checkforcmd_noparen(&p, "var", 3);
1008 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00001009 {
1010 emsg(_(e_cannot_declare_variable_on_command_line));
1011 return;
1012 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001013 ex_let(eap);
1014}
1015
1016/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 * ":let" list all variable values
1018 * ":let var1 var2" list variable values
1019 * ":let var = expr" assignment command.
1020 * ":let var += expr" assignment command.
1021 * ":let var -= expr" assignment command.
1022 * ":let var *= expr" assignment command.
1023 * ":let var /= expr" assignment command.
1024 * ":let var %= expr" assignment command.
1025 * ":let var .= expr" assignment command.
1026 * ":let var ..= expr" assignment command.
1027 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +01001029 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001030 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001031 * ":final var = expr" assignment command.
1032 * ":final [var1, var2] = expr" unpack list.
1033 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001034 * ":const" list all variable values
1035 * ":const var1 var2" list variable values
1036 * ":const var = expr" assignment command.
1037 * ":const [var1, var2] = expr" unpack list.
1038 */
1039 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001040ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001041{
1042 char_u *arg = eap->arg;
1043 char_u *expr = NULL;
1044 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001045 int var_count = 0;
1046 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001047 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 char_u *argend;
1049 int first = TRUE;
1050 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001051 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001052 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001053 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001055 if (eap->cmdidx == CMD_final && !vim9script)
1056 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001057 // In legacy Vim script ":final" is short for ":finally".
1058 ex_finally(eap);
1059 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001060 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001061 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001062 {
1063 emsg(_(e_cannot_use_let_in_vim9_script));
1064 return;
1065 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001066
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001067 if (eap->cmdidx == CMD_const)
1068 flags |= ASSIGN_CONST;
1069 else if (eap->cmdidx == CMD_final)
1070 flags |= ASSIGN_FINAL;
1071
1072 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001074 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001076 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001077 if (argend == NULL)
1078 return;
1079 if (argend > arg && argend[-1] == '.') // for var.='str'
1080 --argend;
1081 expr = skipwhite(argend);
1082 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001083 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001084 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001085 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1086 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001087 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001088 {
1089 // ":let" without "=": list variables
1090 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001091 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001092 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001093 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001094 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001095 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001096 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001097 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001098 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001099 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001100 else
1101 // Vim9 declaration ":var name: type"
1102 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001103 }
1104 else
1105 {
1106 // ":let var1 var2" - list values
1107 arg = list_arg_vars(eap, arg, &first);
1108 }
1109 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001110 else if (!eap->skip)
1111 {
1112 // ":let"
1113 list_glob_vars(&first);
1114 list_buf_vars(&first);
1115 list_win_vars(&first);
1116 list_tab_vars(&first);
1117 list_script_vars(&first);
1118 list_func_vars(&first);
1119 list_vim_vars(&first);
1120 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001121 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001122 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001123 }
zeertzjq474891b2023-04-12 21:36:03 +01001124
1125 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001127 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001128 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001129
Bram Moolenaard9876422022-10-12 12:58:54 +01001130 // :let text =<< [trim] [eval] END
1131 // :var text =<< [trim] [eval] END
1132 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1133 || !IS_WHITE_OR_NUL(expr[3])))
1134 semsg(_(e_white_space_required_before_and_after_str_at_str),
1135 "=<<", expr);
1136 else
1137 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1138
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001139 if (l != NULL)
1140 {
1141 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001142 if (!eap->skip)
1143 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001144 // errors are for the assignment, not the end marker
1145 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001146 op[0] = '=';
1147 op[1] = NUL;
1148 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001150 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001151 clear_tv(&rettv);
1152 }
zeertzjq474891b2023-04-12 21:36:03 +01001153 return;
1154 }
1155
1156 evalarg_T evalarg;
1157 int len = 1;
1158
1159 CLEAR_FIELD(rettv);
1160
1161 int cur_lnum;
1162
1163 op[0] = '=';
1164 op[1] = NUL;
1165 if (*expr != '=')
1166 {
1167 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1168 {
1169 // +=, /=, etc. require an existing variable
1170 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1171 }
1172 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1173 {
1174 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1175 ++len;
1176 if (expr[0] == '.' && expr[1] == '.') // ..=
1177 {
1178 ++expr;
1179 ++len;
1180 }
1181 }
1182 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001183 }
1184 else
zeertzjq474891b2023-04-12 21:36:03 +01001185 ++expr;
1186
1187 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1188 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001189 {
zeertzjq474891b2023-04-12 21:36:03 +01001190 vim_strncpy(op, expr - len, len);
1191 semsg(_(e_white_space_required_before_and_after_str_at_str),
1192 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001193 }
zeertzjq474891b2023-04-12 21:36:03 +01001194
1195 if (eap->skip)
1196 ++emsg_skip;
1197 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1198 expr = skipwhite_and_linebreak(expr, &evalarg);
1199 cur_lnum = SOURCING_LNUM;
1200 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1201 if (eap->skip)
1202 --emsg_skip;
1203 clear_evalarg(&evalarg, eap);
1204
1205 // Restore the line number so that any type error is given for the
1206 // declaration, not the expression.
1207 SOURCING_LNUM = cur_lnum;
1208
1209 if (!eap->skip && eval_res != FAIL)
1210 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1211 flags, op);
1212 if (eval_res != FAIL)
1213 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001214}
1215
1216/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001217 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001218 * Handles both "var" with any type and "[var, var; var]" with a list type.
1219 * When "op" is not NULL it points to a string with characters that
1220 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1221 * or concatenate.
1222 * Returns OK or FAIL;
1223 */
1224 int
1225ex_let_vars(
1226 char_u *arg_start,
1227 typval_T *tv,
1228 int copy, // copy values from "tv", don't move
1229 int semicolon, // from skip_var_list()
1230 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001231 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001232 char_u *op)
1233{
1234 char_u *arg = arg_start;
1235 list_T *l;
1236 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001237 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001238 listitem_T *item;
1239 typval_T ltv;
1240
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001241 if (tv->v_type == VAR_VOID)
1242 {
1243 emsg(_(e_cannot_use_void_value));
1244 return FAIL;
1245 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001246 if (*arg != '[')
1247 {
1248 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001249 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001250 return FAIL;
1251 return OK;
1252 }
1253
1254 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1255 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1256 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001257 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001258 return FAIL;
1259 }
1260
1261 i = list_len(l);
1262 if (semicolon == 0 && var_count < i)
1263 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001264 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001265 return FAIL;
1266 }
1267 if (var_count - semicolon > i)
1268 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001269 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001270 return FAIL;
1271 }
1272
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001273 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001274 item = l->lv_first;
1275 while (*arg != ']')
1276 {
1277 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001278 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001279 arg = ex_let_one(arg, &item->li_tv, TRUE,
1280 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 item = item->li_next;
1282 if (arg == NULL)
1283 return FAIL;
1284
1285 arg = skipwhite(arg);
1286 if (*arg == ';')
1287 {
1288 // Put the rest of the list (may be empty) in the var after ';'.
1289 // Create a new list for this.
1290 l = list_alloc();
1291 if (l == NULL)
1292 return FAIL;
1293 while (item != NULL)
1294 {
1295 list_append_tv(l, &item->li_tv);
1296 item = item->li_next;
1297 }
1298
1299 ltv.v_type = VAR_LIST;
1300 ltv.v_lock = 0;
1301 ltv.vval.v_list = l;
1302 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001303 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001305 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1306 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001307 clear_tv(&ltv);
1308 if (arg == NULL)
1309 return FAIL;
1310 break;
1311 }
1312 else if (*arg != ',' && *arg != ']')
1313 {
1314 internal_error("ex_let_vars()");
1315 return FAIL;
1316 }
1317 }
1318
1319 return OK;
1320}
1321
1322/*
1323 * Skip over assignable variable "var" or list of variables "[var, var]".
1324 * Used for ":let varvar = expr" and ":for varvar in expr".
1325 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001326 * for "[var, var; var]" set "semicolon" to 1.
1327 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001328 * Return NULL for an error.
1329 */
1330 char_u *
1331skip_var_list(
1332 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001334 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001335 int *semicolon,
1336 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001337{
1338 char_u *p, *s;
1339
1340 if (*arg == '[')
1341 {
1342 // "[var, var]": find the matching ']'.
1343 p = arg;
1344 for (;;)
1345 {
1346 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001347 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 if (s == p)
1349 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001350 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001351 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 return NULL;
1353 }
1354 ++*var_count;
1355
1356 p = skipwhite(s);
1357 if (*p == ']')
1358 break;
1359 else if (*p == ';')
1360 {
1361 if (*semicolon == 1)
1362 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001363 if (!silent)
1364 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365 return NULL;
1366 }
1367 *semicolon = 1;
1368 }
1369 else if (*p != ',')
1370 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001371 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001372 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001373 return NULL;
1374 }
1375 }
1376 return p + 1;
1377 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001378
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001379 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001380}
1381
1382/*
1383 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1384 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001386 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001387 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001390 char_u *end;
1391 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001393 if (*arg == '@' && arg[1] != NUL)
1394 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001395
1396 // termcap option name may have non-alpha characters
1397 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1398 return arg + 5;
1399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001402
1403 // "a: type" is declaring variable "a" with a type, not "a:".
1404 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001405 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001406 --end;
1407
Bram Moolenaar585587d2021-01-17 20:52:13 +01001408 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001410 if (*skipwhite(end) == ':')
1411 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 }
1413 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414}
1415
1416/*
1417 * List variables for hashtab "ht" with prefix "prefix".
1418 * If "empty" is TRUE also list NULL strings as empty strings.
1419 */
1420 void
1421list_hashtable_vars(
1422 hashtab_T *ht,
1423 char *prefix,
1424 int empty,
1425 int *first)
1426{
1427 hashitem_T *hi;
1428 dictitem_T *di;
1429 int todo;
1430 char_u buf[IOSIZE];
1431
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001432 int save_ht_flags = ht->ht_flags;
1433 ht->ht_flags |= HTFLAGS_FROZEN;
1434
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 todo = (int)ht->ht_used;
1436 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1437 {
1438 if (!HASHITEM_EMPTY(hi))
1439 {
1440 --todo;
1441 di = HI2DI(hi);
1442
1443 // apply :filter /pat/ to variable name
1444 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1445 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1446 if (message_filtered(buf))
1447 continue;
1448
1449 if (empty || di->di_tv.v_type != VAR_STRING
1450 || di->di_tv.vval.v_string != NULL)
1451 list_one_var(di, prefix, first);
1452 }
1453 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001454
1455 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456}
1457
1458/*
1459 * List global variables.
1460 */
1461 static void
1462list_glob_vars(int *first)
1463{
1464 list_hashtable_vars(&globvarht, "", TRUE, first);
1465}
1466
1467/*
1468 * List buffer variables.
1469 */
1470 static void
1471list_buf_vars(int *first)
1472{
1473 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1474}
1475
1476/*
1477 * List window variables.
1478 */
1479 static void
1480list_win_vars(int *first)
1481{
1482 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1483}
1484
1485/*
1486 * List tab page variables.
1487 */
1488 static void
1489list_tab_vars(int *first)
1490{
1491 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1492}
1493
1494/*
1495 * List variables in "arg".
1496 */
1497 static char_u *
1498list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1499{
1500 int error = FALSE;
1501 int len;
1502 char_u *name;
1503 char_u *name_start;
1504 char_u *arg_subsc;
1505 char_u *tofree;
1506 typval_T tv;
1507
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001508 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001509 {
1510 if (error || eap->skip)
1511 {
1512 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1513 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1514 {
1515 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001516 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001517 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001518 break;
1519 }
1520 }
1521 else
1522 {
1523 // get_name_len() takes care of expanding curly braces
1524 name_start = name = arg;
1525 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1526 if (len <= 0)
1527 {
1528 // This is mainly to keep test 49 working: when expanding
1529 // curly braces fails overrule the exception error message.
1530 if (len < 0 && !aborting())
1531 {
1532 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001533 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534 break;
1535 }
1536 error = TRUE;
1537 }
1538 else
1539 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001540 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001541 if (tofree != NULL)
1542 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001543 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001544 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001545 error = TRUE;
1546 else
1547 {
1548 // handle d.key, l[idx], f(expr)
1549 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001550 if (handle_subscript(&arg, name_start, &tv,
1551 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 error = TRUE;
1553 else
1554 {
1555 if (arg == arg_subsc && len == 2 && name[1] == ':')
1556 {
1557 switch (*name)
1558 {
1559 case 'g': list_glob_vars(first); break;
1560 case 'b': list_buf_vars(first); break;
1561 case 'w': list_win_vars(first); break;
1562 case 't': list_tab_vars(first); break;
1563 case 'v': list_vim_vars(first); break;
1564 case 's': list_script_vars(first); break;
1565 case 'l': list_func_vars(first); break;
1566 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001567 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568 }
1569 }
1570 else
1571 {
1572 char_u numbuf[NUMBUFLEN];
1573 char_u *tf;
1574 int c;
1575 char_u *s;
1576
1577 s = echo_string(&tv, &tf, numbuf, 0);
1578 c = *arg;
1579 *arg = NUL;
1580 list_one_var_a("",
1581 arg == arg_subsc ? name : name_start,
1582 tv.v_type,
1583 s == NULL ? (char_u *)"" : s,
1584 first);
1585 *arg = c;
1586 vim_free(tf);
1587 }
1588 clear_tv(&tv);
1589 }
1590 }
1591 }
1592
1593 vim_free(tofree);
1594 }
1595
1596 arg = skipwhite(arg);
1597 }
1598
1599 return arg;
1600}
1601
1602/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001603 * Set an environment variable, part of ex_let_one().
1604 */
1605 static char_u *
1606ex_let_env(
1607 char_u *arg,
1608 typval_T *tv,
1609 int flags,
1610 char_u *endchars,
1611 char_u *op)
1612{
1613 char_u *arg_end = NULL;
1614 char_u *name;
1615 int len;
1616
1617 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1618 && (flags & ASSIGN_FOR_LOOP) == 0)
1619 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001620 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001621 return NULL;
1622 }
1623
1624 // Find the end of the name.
1625 ++arg;
1626 name = arg;
1627 len = get_env_len(&arg);
1628 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001629 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001630 else
1631 {
1632 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001633 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001634 else if (endchars != NULL
1635 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1636 emsg(_(e_unexpected_characters_in_let));
1637 else if (!check_secure())
1638 {
1639 char_u *tofree = NULL;
1640 int c1 = name[len];
1641 char_u *p;
1642
1643 name[len] = NUL;
1644 p = tv_get_string_chk(tv);
1645 if (p != NULL && op != NULL && *op == '.')
1646 {
1647 int mustfree = FALSE;
1648 char_u *s = vim_getenv(name, &mustfree);
1649
1650 if (s != NULL)
1651 {
1652 p = tofree = concat_str(s, p);
1653 if (mustfree)
1654 vim_free(s);
1655 }
1656 }
1657 if (p != NULL)
1658 {
1659 vim_setenv_ext(name, p);
1660 arg_end = arg;
1661 }
1662 name[len] = c1;
1663 vim_free(tofree);
1664 }
1665 }
1666 return arg_end;
1667}
1668
1669/*
1670 * Set an option, part of ex_let_one().
1671 */
1672 static char_u *
1673ex_let_option(
1674 char_u *arg,
1675 typval_T *tv,
1676 int flags,
1677 char_u *endchars,
1678 char_u *op)
1679{
1680 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001681 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001682 char_u *arg_end = NULL;
1683
1684 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1685 && (flags & ASSIGN_FOR_LOOP) == 0)
1686 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001687 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001688 return NULL;
1689 }
1690
1691 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001692 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001693 if (p == NULL || (endchars != NULL
1694 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001695 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001696 emsg(_(e_unexpected_characters_in_let));
1697 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001698 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001699
1700 int c1;
1701 long n = 0;
1702 getoption_T opt_type;
1703 long numval;
1704 char_u *stringval = NULL;
1705 char_u *s = NULL;
1706 int failed = FALSE;
1707 int opt_p_flags;
1708 char_u *tofree = NULL;
1709 char_u numbuf[NUMBUFLEN];
1710
1711 c1 = *p;
1712 *p = NUL;
1713
1714 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1715 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1716 {
1717 semsg(_(e_unknown_option_str_2), arg);
1718 goto theend;
1719 }
1720 if (op != NULL && *op != '='
1721 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1722 || (opt_type == gov_string && *op != '.')))
1723 {
1724 semsg(_(e_wrong_variable_type_for_str_equal), op);
1725 goto theend;
1726 }
1727
1728 if ((opt_type == gov_bool
1729 || opt_type == gov_number
1730 || opt_type == gov_hidden_bool
1731 || opt_type == gov_hidden_number)
1732 && (tv->v_type != VAR_STRING || !in_vim9script()))
1733 {
1734 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1735 // bool, possibly hidden
1736 n = (long)tv_get_bool_chk(tv, &failed);
1737 else
1738 // number, possibly hidden
1739 n = (long)tv_get_number_chk(tv, &failed);
1740 if (failed)
1741 goto theend;
1742 }
1743
1744 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1745 || tv->v_type == VAR_FUNC))
1746 {
1747 // If the option can be set to a function reference or a lambda
1748 // and the passed value is a function reference, then convert it to
1749 // the name (string) of the function reference.
1750 s = tv2string(tv, &tofree, numbuf, 0);
1751 if (s == NULL)
1752 goto theend;
1753 }
1754 // Avoid setting a string option to the text "v:false" or similar.
1755 // In Vim9 script also don't convert a number to string.
1756 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1757 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1758 {
1759 s = tv_get_string_chk(tv);
1760 if (s == NULL)
1761 goto theend;
1762 }
1763 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1764 {
1765 emsg(_(e_string_required));
1766 goto theend;
1767 }
1768
1769 if (op != NULL && *op != '=')
1770 {
1771 // number, in legacy script also bool
1772 if (opt_type == gov_number
1773 || (opt_type == gov_bool && !in_vim9script()))
1774 {
1775 switch (*op)
1776 {
1777 case '+': n = numval + n; break;
1778 case '-': n = numval - n; break;
1779 case '*': n = numval * n; break;
1780 case '/': n = (long)num_divide(numval, n, &failed); break;
1781 case '%': n = (long)num_modulus(numval, n, &failed); break;
1782 }
1783 s = NULL;
1784 if (failed)
1785 goto theend;
1786 }
1787 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1788 {
1789 // string
1790 s = concat_str(stringval, s);
1791 vim_free(stringval);
1792 stringval = s;
1793 }
1794 }
1795
1796 char *err = set_option_value(arg, n, s, scope);
1797 arg_end = p;
1798 if (err != NULL)
1799 emsg(_(err));
1800
1801theend:
1802 *p = c1;
1803 vim_free(stringval);
1804 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001805 return arg_end;
1806}
1807
1808/*
1809 * Set a register, part of ex_let_one().
1810 */
1811 static char_u *
1812ex_let_register(
1813 char_u *arg,
1814 typval_T *tv,
1815 int flags,
1816 char_u *endchars,
1817 char_u *op)
1818{
1819 char_u *arg_end = NULL;
1820
1821 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1822 && (flags & ASSIGN_FOR_LOOP) == 0)
1823 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001824 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001825 return NULL;
1826 }
1827 ++arg;
1828 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001829 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001830 else if (endchars != NULL
1831 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1832 emsg(_(e_unexpected_characters_in_let));
1833 else
1834 {
1835 char_u *ptofree = NULL;
1836 char_u *p;
1837
1838 p = tv_get_string_chk(tv);
1839 if (p != NULL && op != NULL && *op == '.')
1840 {
1841 char_u *s = get_reg_contents(*arg == '@'
1842 ? '"' : *arg, GREG_EXPR_SRC);
1843
1844 if (s != NULL)
1845 {
1846 p = ptofree = concat_str(s, p);
1847 vim_free(s);
1848 }
1849 }
1850 if (p != NULL)
1851 {
1852 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1853 arg_end = arg + 1;
1854 }
1855 vim_free(ptofree);
1856 }
1857 return arg_end;
1858}
1859
1860/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1862 * Returns a pointer to the char just after the var name.
1863 * Returns NULL if there is an error.
1864 */
1865 static char_u *
1866ex_let_one(
1867 char_u *arg, // points to variable name
1868 typval_T *tv, // value to assign to variable
1869 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001870 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001872 char_u *op, // "+", "-", "." or NULL
1873 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001874{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001877 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001878 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001879 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1880 {
1881 vim9_declare_error(arg);
1882 return NULL;
1883 }
1884
Ernie Rael9ed53752023-12-11 17:40:46 +01001885 if (check_typval_is_value(tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001886 return NULL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001887
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001888 if (*arg == '$')
1889 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001890 // ":let $VAR = expr": Set environment variable.
1891 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 else if (*arg == '&')
1894 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001895 // ":let &option = expr": Set option value.
1896 // ":let &l:option = expr": Set local option value.
1897 // ":let &g:option = expr": Set global option value.
1898 // ":for &ts in range(8)": Set option value for for loop
1899 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001900 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001901 else if (*arg == '@')
1902 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001903 // ":let @r = expr": Set register contents.
1904 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001905 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001906 else if (eval_isnamec1(*arg) || *arg == '{')
1907 {
1908 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001909 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001910 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1911 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001912 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001913 if (op != NULL && *op != '=')
1914 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001915
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001916 // ":let var = expr": Set internal variable.
1917 // ":let var: type = expr": Set internal variable with type.
1918 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001919 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001920 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001921 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 if (endchars != NULL && vim_strchr(endchars,
1923 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001924 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001925 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001926 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001927 else
1928 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001929 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001930 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 }
1932 }
1933 clear_lval(&lv);
1934 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001936 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937
1938 return arg_end;
1939}
1940
1941/*
1942 * ":unlet[!] var1 ... " command.
1943 */
1944 void
1945ex_unlet(exarg_T *eap)
1946{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001947 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001948}
1949
1950/*
1951 * ":lockvar" and ":unlockvar" commands
1952 */
1953 void
1954ex_lockvar(exarg_T *eap)
1955{
1956 char_u *arg = eap->arg;
1957 int deep = 2;
1958
1959 if (eap->forceit)
1960 deep = -1;
1961 else if (vim_isdigit(*arg))
1962 {
1963 deep = getdigits(&arg);
1964 arg = skipwhite(arg);
1965 }
1966
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001967 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968}
1969
1970/*
1971 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001972 * Also used for Vim9 script. "callback" is invoked as:
1973 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001974 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001975 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001976ex_unletlock(
1977 exarg_T *eap,
1978 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001979 int deep,
1980 int glv_flags,
1981 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1982 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001983{
1984 char_u *arg = argstart;
1985 char_u *name_end;
1986 int error = FALSE;
1987 lval_T lv;
1988
1989 do
1990 {
1991 if (*arg == '$')
1992 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001993 lv.ll_name = arg;
1994 lv.ll_tv = NULL;
1995 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996 if (get_env_len(&arg) == 0)
1997 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001998 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001999 return;
2000 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002001 if (!error && !eap->skip
2002 && callback(&lv, arg, eap, deep, cookie) == FAIL)
2003 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002004 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002005 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002006 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002007 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002008 // Parse the name and find the end.
2009 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01002010 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002011 if (lv.ll_name == NULL)
2012 error = TRUE; // error but continue parsing
2013 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
2014 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002015 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002016 if (name_end != NULL)
2017 {
2018 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00002019 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002020 }
2021 if (!(eap->skip || error))
2022 clear_lval(&lv);
2023 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002024 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002025
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002026 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002027 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002028 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002029
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002030 if (!eap->skip)
2031 clear_lval(&lv);
2032 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002033
2034 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002035 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002036
Bram Moolenaar63b91732021-08-05 20:40:03 +02002037 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002038}
2039
2040 static int
2041do_unlet_var(
2042 lval_T *lp,
2043 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002044 exarg_T *eap,
2045 int deep UNUSED,
2046 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002047{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002048 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002049 int ret = OK;
2050 int cc;
2051
2052 if (lp->ll_tv == NULL)
2053 {
2054 cc = *name_end;
2055 *name_end = NUL;
2056
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002057 // Environment variable, normal name or expanded name.
2058 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002059 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002060 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002061 ret = FAIL;
2062 *name_end = cc;
2063 }
2064 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002065 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002066 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002067 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002068 return FAIL;
2069 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002070 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2071 !lp->ll_empty2, lp->ll_n2);
2072 else if (lp->ll_list != NULL)
2073 // unlet a List item.
2074 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002075 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002076 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002077 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002078
2079 return ret;
2080}
2081
2082/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002083 * Unlet one item or a range of items from a list.
2084 * Return OK or FAIL.
2085 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002086 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002087list_unlet_range(
2088 list_T *l,
2089 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002090 long n1_arg,
2091 int has_n2,
2092 long n2)
2093{
2094 listitem_T *li = li_first;
2095 int n1 = n1_arg;
2096
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002097 // Delete a range of List items.
2098 li = li_first;
2099 n1 = n1_arg;
2100 while (li != NULL && (!has_n2 || n2 >= n1))
2101 {
2102 listitem_T *next = li->li_next;
2103
2104 listitem_remove(l, li);
2105 li = next;
2106 ++n1;
2107 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002108}
2109/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002110 * "unlet" a variable. Return OK if it existed, FAIL if not.
2111 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2112 */
2113 int
2114do_unlet(char_u *name, int forceit)
2115{
2116 hashtab_T *ht;
2117 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002118 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002119 dict_T *d;
2120 dictitem_T *di;
2121
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002122 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002123 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002124 return FAIL;
2125
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002126 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002127
2128 // can't :unlet a script variable in Vim9 script from a function
2129 if (ht == get_script_local_ht()
2130 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2131 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2132 == SCRIPT_VERSION_VIM9
2133 && check_vim9_unlet(name) == FAIL)
2134 return FAIL;
2135
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002136 if (ht != NULL && *varname != NUL)
2137 {
2138 d = get_current_funccal_dict(ht);
2139 if (d == NULL)
2140 {
2141 if (ht == &globvarht)
2142 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002143 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002144 d = &vimvardict;
2145 else
2146 {
2147 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2148 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2149 }
2150 if (d == NULL)
2151 {
2152 internal_error("do_unlet()");
2153 return FAIL;
2154 }
2155 }
2156 hi = hash_find(ht, varname);
2157 if (HASHITEM_EMPTY(hi))
2158 hi = find_hi_in_scoped_ht(name, &ht);
2159 if (hi != NULL && !HASHITEM_EMPTY(hi))
2160 {
2161 di = HI2DI(hi);
2162 if (var_check_fixed(di->di_flags, name, FALSE)
2163 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002164 || value_check_lock(d->dv_lock, name, FALSE)
2165 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002166 return FAIL;
2167
2168 delete_var(ht, hi);
2169 return OK;
2170 }
2171 }
2172 if (forceit)
2173 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002174 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002175 return FAIL;
2176}
2177
Ernie Raelee865f32023-09-29 19:53:55 +02002178 static void
2179report_lockvar_member(char *msg, lval_T *lp)
2180{
2181 int did_alloc = FALSE;
2182 char_u *vname = (char_u *)"";
2183 char_u *class_name = lp->ll_class != NULL
2184 ? lp->ll_class->class_name : (char_u *)"";
2185 if (lp->ll_name != NULL)
2186 {
2187 if (lp->ll_name_end == NULL)
2188 vname = lp->ll_name;
2189 else
2190 {
2191 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2192 if (vname == NULL)
2193 return;
2194 did_alloc = TRUE;
2195 }
2196 }
2197 semsg(_(msg), vname, class_name);
2198 if (did_alloc)
2199 vim_free(vname);
2200}
2201
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002202/*
2203 * Lock or unlock variable indicated by "lp".
2204 * "deep" is the levels to go (-1 for unlimited);
2205 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2206 */
2207 static int
2208do_lock_var(
2209 lval_T *lp,
2210 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002211 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002212 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002213 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002214{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002215 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002216 int ret = OK;
2217 int cc;
2218 dictitem_T *di;
2219
Ernie Raelee865f32023-09-29 19:53:55 +02002220#ifdef LOG_LOCKVAR
2221 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2222#endif
2223
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002224 if (lp->ll_tv == NULL)
2225 {
2226 cc = *name_end;
2227 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002228 if (*lp->ll_name == '$')
2229 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002230 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002231 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002232 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002233 else
2234 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002235 // Normal name or expanded name.
2236 di = find_var(lp->ll_name, NULL, TRUE);
2237 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002238 {
2239 if (in_vim9script())
2240 semsg(_(e_cannot_find_variable_to_unlock_str),
2241 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002242 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002243 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002244 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002245 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002246 if ((di->di_flags & DI_FLAGS_FIX)
2247 && di->di_tv.v_type != VAR_DICT
2248 && di->di_tv.v_type != VAR_LIST)
2249 {
2250 // For historic reasons this error is not given for a list
2251 // or dict. E.g., the b: dict could be locked/unlocked.
2252 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2253 ret = FAIL;
2254 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002255 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002256 {
2257 if (in_vim9script())
2258 {
2259 svar_T *sv = find_typval_in_script(&di->di_tv,
2260 0, FALSE);
2261
2262 if (sv != NULL && sv->sv_const != 0)
2263 {
2264 semsg(_(e_cannot_change_readonly_variable_str),
2265 lp->ll_name);
2266 ret = FAIL;
2267 }
2268 }
2269
2270 if (ret == OK)
2271 {
2272 if (lock)
2273 di->di_flags |= DI_FLAGS_LOCK;
2274 else
2275 di->di_flags &= ~DI_FLAGS_LOCK;
2276 if (deep != 0)
2277 item_lock(&di->di_tv, deep, lock, FALSE);
2278 }
2279 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002280 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002281 }
2282 *name_end = cc;
2283 }
Ernie Raelee865f32023-09-29 19:53:55 +02002284 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002285 {
2286 // nothing to do
2287 }
Ernie Raelee865f32023-09-29 19:53:55 +02002288 else if (lp->ll_is_root)
2289 // (un)lock the item.
2290 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002291 else if (lp->ll_range)
2292 {
2293 listitem_T *li = lp->ll_li;
2294
2295 // (un)lock a range of List items.
2296 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2297 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002298 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002299 li = li->li_next;
2300 ++lp->ll_n1;
2301 }
2302 }
2303 else if (lp->ll_list != NULL)
2304 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002305 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002306 else if (lp->ll_object != NULL) // This check must be before ll_class.
2307 {
2308 // (un)lock an object variable.
2309 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2310 ret = FAIL;
2311 }
2312 else if (lp->ll_class != NULL)
2313 {
2314 // (un)lock a class variable.
2315 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2316 ret = FAIL;
2317 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002318 else
Ernie Raelee865f32023-09-29 19:53:55 +02002319 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002320 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002321 if (lp->ll_di == NULL)
2322 {
2323 emsg(_(e_dictionary_required));
2324 ret = FAIL;
2325 }
2326 else
2327 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2328 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002329
2330 return ret;
2331}
2332
2333/*
2334 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002335 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2336 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002337 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002338 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002339item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002340{
2341 static int recurse = 0;
2342 list_T *l;
2343 listitem_T *li;
2344 dict_T *d;
2345 blob_T *b;
2346 hashitem_T *hi;
2347 int todo;
2348
Ernie Raelee865f32023-09-29 19:53:55 +02002349#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002350 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002351#endif
2352
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002353 if (recurse >= DICT_MAXNEST)
2354 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002355 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002356 return;
2357 }
2358 if (deep == 0)
2359 return;
2360 ++recurse;
2361
2362 // lock/unlock the item itself
2363 if (lock)
2364 tv->v_lock |= VAR_LOCKED;
2365 else
2366 tv->v_lock &= ~VAR_LOCKED;
2367
2368 switch (tv->v_type)
2369 {
2370 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002371 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002373 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002375 case VAR_STRING:
2376 case VAR_FUNC:
2377 case VAR_PARTIAL:
2378 case VAR_FLOAT:
2379 case VAR_SPECIAL:
2380 case VAR_JOB:
2381 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002382 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002383 case VAR_CLASS:
2384 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002385 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002386 break;
2387
2388 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002389 if ((b = tv->vval.v_blob) != NULL
2390 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002391 {
2392 if (lock)
2393 b->bv_lock |= VAR_LOCKED;
2394 else
2395 b->bv_lock &= ~VAR_LOCKED;
2396 }
2397 break;
2398 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002399 if ((l = tv->vval.v_list) != NULL
2400 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002401 {
2402 if (lock)
2403 l->lv_lock |= VAR_LOCKED;
2404 else
2405 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002406 if (deep < 0 || deep > 1)
2407 {
2408 if (l->lv_first == &range_list_item)
2409 l->lv_lock |= VAR_ITEMS_LOCKED;
2410 else
2411 {
2412 // recursive: lock/unlock the items the List contains
2413 CHECK_LIST_MATERIALIZE(l);
2414 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2415 deep - 1, lock, check_refcount);
2416 }
2417 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002418 }
2419 break;
2420 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002421 if ((d = tv->vval.v_dict) != NULL
2422 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002423 {
2424 if (lock)
2425 d->dv_lock |= VAR_LOCKED;
2426 else
2427 d->dv_lock &= ~VAR_LOCKED;
2428 if (deep < 0 || deep > 1)
2429 {
2430 // recursive: lock/unlock the items the List contains
2431 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002432 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002433 {
2434 if (!HASHITEM_EMPTY(hi))
2435 {
2436 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002437 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2438 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002439 }
2440 }
2441 }
2442 }
2443 }
2444 --recurse;
2445}
2446
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002447#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2448/*
2449 * Delete all "menutrans_" variables.
2450 */
2451 void
2452del_menutrans_vars(void)
2453{
2454 hashitem_T *hi;
2455 int todo;
2456
2457 hash_lock(&globvarht);
2458 todo = (int)globvarht.ht_used;
2459 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2460 {
2461 if (!HASHITEM_EMPTY(hi))
2462 {
2463 --todo;
2464 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2465 delete_var(&globvarht, hi);
2466 }
2467 }
2468 hash_unlock(&globvarht);
2469}
2470#endif
2471
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002472/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002473 * Local string buffer for the next two functions to store a variable name
2474 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2475 * get_user_var_name().
2476 */
2477
2478static char_u *varnamebuf = NULL;
2479static int varnamebuflen = 0;
2480
2481/*
2482 * Function to concatenate a prefix and a variable name.
2483 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002484 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002485cat_prefix_varname(int prefix, char_u *name)
2486{
2487 int len;
2488
2489 len = (int)STRLEN(name) + 3;
2490 if (len > varnamebuflen)
2491 {
2492 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002493 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002494 varnamebuf = alloc(len);
2495 if (varnamebuf == NULL)
2496 {
2497 varnamebuflen = 0;
2498 return NULL;
2499 }
2500 varnamebuflen = len;
2501 }
2502 *varnamebuf = prefix;
2503 varnamebuf[1] = ':';
2504 STRCPY(varnamebuf + 2, name);
2505 return varnamebuf;
2506}
2507
2508/*
2509 * Function given to ExpandGeneric() to obtain the list of user defined
2510 * (global/buffer/window/built-in) variable names.
2511 */
2512 char_u *
2513get_user_var_name(expand_T *xp, int idx)
2514{
2515 static long_u gdone;
2516 static long_u bdone;
2517 static long_u wdone;
2518 static long_u tdone;
2519 static int vidx;
2520 static hashitem_T *hi;
2521 hashtab_T *ht;
2522
2523 if (idx == 0)
2524 {
2525 gdone = bdone = wdone = vidx = 0;
2526 tdone = 0;
2527 }
2528
2529 // Global variables
2530 if (gdone < globvarht.ht_used)
2531 {
2532 if (gdone++ == 0)
2533 hi = globvarht.ht_array;
2534 else
2535 ++hi;
2536 while (HASHITEM_EMPTY(hi))
2537 ++hi;
2538 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2539 return cat_prefix_varname('g', hi->hi_key);
2540 return hi->hi_key;
2541 }
2542
2543 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002544 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002545 if (bdone < ht->ht_used)
2546 {
2547 if (bdone++ == 0)
2548 hi = ht->ht_array;
2549 else
2550 ++hi;
2551 while (HASHITEM_EMPTY(hi))
2552 ++hi;
2553 return cat_prefix_varname('b', hi->hi_key);
2554 }
2555
2556 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002557 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002558 if (wdone < ht->ht_used)
2559 {
2560 if (wdone++ == 0)
2561 hi = ht->ht_array;
2562 else
2563 ++hi;
2564 while (HASHITEM_EMPTY(hi))
2565 ++hi;
2566 return cat_prefix_varname('w', hi->hi_key);
2567 }
2568
2569 // t: variables
2570 ht = &curtab->tp_vars->dv_hashtab;
2571 if (tdone < ht->ht_used)
2572 {
2573 if (tdone++ == 0)
2574 hi = ht->ht_array;
2575 else
2576 ++hi;
2577 while (HASHITEM_EMPTY(hi))
2578 ++hi;
2579 return cat_prefix_varname('t', hi->hi_key);
2580 }
2581
2582 // v: variables
2583 if (vidx < VV_LEN)
2584 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2585
2586 VIM_CLEAR(varnamebuf);
2587 varnamebuflen = 0;
2588 return NULL;
2589}
2590
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002591 char *
2592get_var_special_name(int nr)
2593{
2594 switch (nr)
2595 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002596 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2597 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002598 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002599 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002600 }
2601 internal_error("get_var_special_name()");
2602 return "42";
2603}
2604
2605/*
2606 * Returns the global variable dictionary
2607 */
2608 dict_T *
2609get_globvar_dict(void)
2610{
2611 return &globvardict;
2612}
2613
2614/*
2615 * Returns the global variable hash table
2616 */
2617 hashtab_T *
2618get_globvar_ht(void)
2619{
2620 return &globvarht;
2621}
2622
2623/*
2624 * Returns the v: variable dictionary
2625 */
2626 dict_T *
2627get_vimvar_dict(void)
2628{
2629 return &vimvardict;
2630}
2631
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002632/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002634 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 */
2636 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002637find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002639 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2640 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641
2642 if (di == NULL)
2643 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002644 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2646 return (int)(vv - vimvars);
2647}
2648
2649
2650/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002651 * Set type of v: variable to "type".
2652 */
2653 void
2654set_vim_var_type(int idx, vartype_T type)
2655{
Bram Moolenaard787e402021-12-24 21:36:12 +00002656 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002657}
2658
2659/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002660 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002661 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002662 */
2663 void
2664set_vim_var_nr(int idx, varnumber_T val)
2665{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002666 vimvars[idx].vv_nr = val;
2667}
2668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 char *
2670get_vim_var_name(int idx)
2671{
2672 return vimvars[idx].vv_name;
2673}
2674
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002675/*
2676 * Get typval_T v: variable value.
2677 */
2678 typval_T *
2679get_vim_var_tv(int idx)
2680{
2681 return &vimvars[idx].vv_tv;
2682}
2683
Bram Moolenaard787e402021-12-24 21:36:12 +00002684 type_T *
2685get_vim_var_type(int idx, garray_T *type_list)
2686{
2687 if (vimvars[idx].vv_type != NULL)
2688 return vimvars[idx].vv_type;
2689 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2690}
2691
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002692/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002693 * Set v: variable to "tv". Only accepts the same type.
2694 * Takes over the value of "tv".
2695 */
2696 int
2697set_vim_var_tv(int idx, typval_T *tv)
2698{
Bram Moolenaard787e402021-12-24 21:36:12 +00002699 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002700 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002701 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002702 clear_tv(tv);
2703 return FAIL;
2704 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002705 // VV_RO is also checked when compiling, but let's check here as well.
2706 if (vimvars[idx].vv_flags & VV_RO)
2707 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002708 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002709 return FAIL;
2710 }
2711 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2712 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002713 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002714 return FAIL;
2715 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002716 clear_tv(&vimvars[idx].vv_di.di_tv);
2717 vimvars[idx].vv_di.di_tv = *tv;
2718 return OK;
2719}
2720
2721/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002722 * Get number v: variable value.
2723 */
2724 varnumber_T
2725get_vim_var_nr(int idx)
2726{
2727 return vimvars[idx].vv_nr;
2728}
2729
2730/*
2731 * Get string v: variable value. Uses a static buffer, can only be used once.
2732 * If the String variable has never been set, return an empty string.
2733 * Never returns NULL;
2734 */
2735 char_u *
2736get_vim_var_str(int idx)
2737{
2738 return tv_get_string(&vimvars[idx].vv_tv);
2739}
2740
2741/*
2742 * Get List v: variable value. Caller must take care of reference count when
2743 * needed.
2744 */
2745 list_T *
2746get_vim_var_list(int idx)
2747{
2748 return vimvars[idx].vv_list;
2749}
2750
2751/*
2752 * Get Dict v: variable value. Caller must take care of reference count when
2753 * needed.
2754 */
2755 dict_T *
2756get_vim_var_dict(int idx)
2757{
2758 return vimvars[idx].vv_dict;
2759}
2760
2761/*
2762 * Set v:char to character "c".
2763 */
2764 void
2765set_vim_var_char(int c)
2766{
2767 char_u buf[MB_MAXBYTES + 1];
2768
2769 if (has_mbyte)
2770 buf[(*mb_char2bytes)(c, buf)] = NUL;
2771 else
2772 {
2773 buf[0] = c;
2774 buf[1] = NUL;
2775 }
2776 set_vim_var_string(VV_CHAR, buf, -1);
2777}
2778
2779/*
2780 * Set v:count to "count" and v:count1 to "count1".
2781 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2782 */
2783 void
2784set_vcount(
2785 long count,
2786 long count1,
2787 int set_prevcount)
2788{
2789 if (set_prevcount)
2790 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2791 vimvars[VV_COUNT].vv_nr = count;
2792 vimvars[VV_COUNT1].vv_nr = count1;
2793}
2794
2795/*
2796 * Save variables that might be changed as a side effect. Used when executing
2797 * a timer callback.
2798 */
2799 void
2800save_vimvars(vimvars_save_T *vvsave)
2801{
2802 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2803 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2804 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2805}
2806
2807/*
2808 * Restore variables saved by save_vimvars().
2809 */
2810 void
2811restore_vimvars(vimvars_save_T *vvsave)
2812{
2813 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2814 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2815 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2816}
2817
2818/*
2819 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2820 * value.
2821 */
2822 void
2823set_vim_var_string(
2824 int idx,
2825 char_u *val,
2826 int len) // length of "val" to use or -1 (whole string)
2827{
2828 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002829 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002830 if (val == NULL)
2831 vimvars[idx].vv_str = NULL;
2832 else if (len == -1)
2833 vimvars[idx].vv_str = vim_strsave(val);
2834 else
2835 vimvars[idx].vv_str = vim_strnsave(val, len);
2836}
2837
2838/*
2839 * Set List v: variable to "val".
2840 */
2841 void
2842set_vim_var_list(int idx, list_T *val)
2843{
2844 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002845 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002846 vimvars[idx].vv_list = val;
2847 if (val != NULL)
2848 ++val->lv_refcount;
2849}
2850
2851/*
2852 * Set Dictionary v: variable to "val".
2853 */
2854 void
2855set_vim_var_dict(int idx, dict_T *val)
2856{
2857 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002858 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002859 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002860 if (val == NULL)
2861 return;
2862
2863 ++val->dv_refcount;
2864 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002865}
2866
2867/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002868 * Set the v:argv list.
2869 */
2870 void
2871set_argv_var(char **argv, int argc)
2872{
2873 list_T *l = list_alloc();
2874 int i;
2875
2876 if (l == NULL)
2877 getout(1);
2878 l->lv_lock = VAR_FIXED;
2879 for (i = 0; i < argc; ++i)
2880 {
2881 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2882 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002883 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002884 }
2885 set_vim_var_list(VV_ARGV, l);
2886}
2887
2888/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002889 * Reset v:register, taking the 'clipboard' setting into account.
2890 */
2891 void
2892reset_reg_var(void)
2893{
2894 int regname = 0;
2895
2896 // Adjust the register according to 'clipboard', so that when
2897 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2898#ifdef FEAT_CLIPBOARD
2899 adjust_clip_reg(&regname);
2900#endif
2901 set_reg_var(regname);
2902}
2903
2904/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002905 * Set v:register if needed.
2906 */
2907 void
2908set_reg_var(int c)
2909{
2910 char_u regname;
2911
2912 if (c == 0 || c == ' ')
2913 regname = '"';
2914 else
2915 regname = c;
2916 // Avoid free/alloc when the value is already right.
2917 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2918 set_vim_var_string(VV_REG, &regname, 1);
2919}
2920
2921/*
2922 * Get or set v:exception. If "oldval" == NULL, return the current value.
2923 * Otherwise, restore the value to "oldval" and return NULL.
2924 * Must always be called in pairs to save and restore v:exception! Does not
2925 * take care of memory allocations.
2926 */
2927 char_u *
2928v_exception(char_u *oldval)
2929{
2930 if (oldval == NULL)
2931 return vimvars[VV_EXCEPTION].vv_str;
2932
2933 vimvars[VV_EXCEPTION].vv_str = oldval;
2934 return NULL;
2935}
2936
2937/*
2938 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2939 * Otherwise, restore the value to "oldval" and return NULL.
2940 * Must always be called in pairs to save and restore v:throwpoint! Does not
2941 * take care of memory allocations.
2942 */
2943 char_u *
2944v_throwpoint(char_u *oldval)
2945{
2946 if (oldval == NULL)
2947 return vimvars[VV_THROWPOINT].vv_str;
2948
2949 vimvars[VV_THROWPOINT].vv_str = oldval;
2950 return NULL;
2951}
2952
2953/*
2954 * Set v:cmdarg.
2955 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2956 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2957 * Must always be called in pairs!
2958 */
2959 char_u *
2960set_cmdarg(exarg_T *eap, char_u *oldarg)
2961{
2962 char_u *oldval;
2963 char_u *newval;
2964 unsigned len;
2965
2966 oldval = vimvars[VV_CMDARG].vv_str;
2967 if (eap == NULL)
2968 {
2969 vim_free(oldval);
2970 vimvars[VV_CMDARG].vv_str = oldarg;
2971 return NULL;
2972 }
2973
2974 if (eap->force_bin == FORCE_BIN)
2975 len = 6;
2976 else if (eap->force_bin == FORCE_NOBIN)
2977 len = 8;
2978 else
2979 len = 0;
2980
2981 if (eap->read_edit)
2982 len += 7;
2983
2984 if (eap->force_ff != 0)
2985 len += 10; // " ++ff=unix"
2986 if (eap->force_enc != 0)
2987 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2988 if (eap->bad_char != 0)
2989 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2990
2991 newval = alloc(len + 1);
2992 if (newval == NULL)
2993 return NULL;
2994
2995 if (eap->force_bin == FORCE_BIN)
2996 sprintf((char *)newval, " ++bin");
2997 else if (eap->force_bin == FORCE_NOBIN)
2998 sprintf((char *)newval, " ++nobin");
2999 else
3000 *newval = NUL;
3001
3002 if (eap->read_edit)
3003 STRCAT(newval, " ++edit");
3004
3005 if (eap->force_ff != 0)
3006 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
3007 eap->force_ff == 'u' ? "unix"
3008 : eap->force_ff == 'd' ? "dos"
3009 : "mac");
3010 if (eap->force_enc != 0)
3011 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
3012 eap->cmd + eap->force_enc);
3013 if (eap->bad_char == BAD_KEEP)
3014 STRCPY(newval + STRLEN(newval), " ++bad=keep");
3015 else if (eap->bad_char == BAD_DROP)
3016 STRCPY(newval + STRLEN(newval), " ++bad=drop");
3017 else if (eap->bad_char != 0)
3018 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
3019 vimvars[VV_CMDARG].vv_str = newval;
3020 return oldval;
3021}
3022
3023/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003024 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003025 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
3026 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003027 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3028 */
3029 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003030eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003031 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00003032 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003033 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003034 typval_T *rettv, // NULL when only checking existence
3035 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003036 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003037{
3038 int ret = OK;
3039 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003040 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02003041 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00003042 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003043 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003044
Bram Moolenaar94674f22023-01-06 18:42:20 +00003045 if (len > 0)
3046 {
3047 // truncate the name, so that we can use strcmp()
3048 cc = name[len];
3049 name[len] = NUL;
3050 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003051
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003052 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003053 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003054 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003055 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003056 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3057
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003058 if (v != NULL)
3059 {
3060 tv = &v->di_tv;
3061 if (dip != NULL)
3062 *dip = v;
3063 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003064 else
3065 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003066 }
3067
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003068 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003070 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003071 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3072
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003073 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003074 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075
3076 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003077 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003079 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003080 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003081 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003082 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003083 ht = &SCRIPT_VARS(sid);
3084 if (ht != NULL)
3085 {
3086 dictitem_T *v = find_var_in_ht(ht, 0, name,
3087 flags & EVAL_VAR_NOAUTOLOAD);
3088
3089 if (v != NULL)
3090 {
3091 tv = &v->di_tv;
3092 if (dip != NULL)
3093 *dip = v;
3094 }
3095 else
3096 ht = NULL;
3097 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003098 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003099 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003100 {
3101 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003102 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003103 ret = FAIL;
3104 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003105 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003106 else
3107 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003108 if (rettv != NULL)
3109 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003110 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003111 rettv->v_type = VAR_ANY;
3112 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3113 }
3114 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003117 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003118 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003119 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003120 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003121
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003122 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003123 // function name. For a global non-autoload function "g:" is
3124 // required.
3125 if (ufunc != NULL && (has_g_prefix
3126 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003127 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003128 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003129 if (rettv != NULL)
3130 {
3131 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003132 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003133 // Keep the "g:", otherwise script-local may be
3134 // assumed.
3135 rettv->vval.v_string = vim_strsave(name);
3136 else
3137 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003138 if (rettv->vval.v_string != NULL)
3139 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003140 }
3141 }
3142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 }
3144
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003145 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003146 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003147 if (tv == NULL)
3148 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003149 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003150 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003151 ret = FAIL;
3152 }
3153 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003154 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003155 svar_T *sv = NULL;
3156 int was_assigned = FALSE;
3157
Bram Moolenaar11005b02021-07-11 20:59:00 +02003158 if (ht != NULL && ht == get_script_local_ht()
3159 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003160 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003161 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003162 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003163 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003164 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003165 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3166 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003167 }
3168
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003169 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3170 && sid != 0)
3171 {
3172 // type alias or class imported from another script. Check
3173 // whether it is exported from the other script.
3174 sv = find_typval_in_script(tv, sid, TRUE);
3175 if (sv == NULL)
3176 {
3177 ret = FAIL;
3178 goto done;
3179 }
3180 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3181 {
3182 semsg(_(e_item_not_exported_in_script_str), name);
3183 ret = FAIL;
3184 goto done;
3185 }
3186 }
3187
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003188 // If a list or dict variable wasn't initialized and has meaningful
3189 // type, do it now. Not for global variables, they are not
3190 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003191 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003192 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003193 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003194 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003195 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003196 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003197 tv->vval.v_dict = dict_alloc();
3198 if (tv->vval.v_dict != NULL)
3199 {
3200 ++tv->vval.v_dict->dv_refcount;
3201 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003202 if (sv != NULL)
3203 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003204 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003205 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003206 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003207 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003208 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003209 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003210 tv->vval.v_list = list_alloc();
3211 if (tv->vval.v_list != NULL)
3212 {
3213 ++tv->vval.v_list->lv_refcount;
3214 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003215 if (sv != NULL)
3216 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003217 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003218 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003219 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003220 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003221 || !in_vim9script()))
3222 {
3223 tv->vval.v_blob = blob_alloc();
3224 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003225 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003226 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003227 if (sv != NULL)
3228 sv->sv_flags |= SVFLAG_ASSIGNED;
3229 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003230 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003231 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003232 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003233 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003234 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003235
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003236done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003237 if (len > 0)
3238 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003239
3240 return ret;
3241}
3242
3243/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003244 * Get the value of internal variable "name", also handling "import.name".
3245 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3246 */
3247 int
3248eval_variable_import(
3249 char_u *name,
3250 typval_T *rettv)
3251{
3252 char_u *s = name;
3253 while (ASCII_ISALNUM(*s) || *s == '_')
3254 ++s;
3255 int len = (int)(s - name);
3256
3257 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3258 return FAIL;
3259 if (rettv->v_type == VAR_ANY && *s == '.')
3260 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003261 char_u *ns = s + 1;
3262 s = ns;
3263 while (ASCII_ISALNUM(*s) || *s == '_')
3264 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003265 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003266 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003267 }
3268 return OK;
3269}
3270
3271
3272/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003273 * Check if variable "name[len]" is a local variable or an argument.
3274 * If so, "*eval_lavars_used" is set to TRUE.
3275 */
3276 void
3277check_vars(char_u *name, int len)
3278{
3279 int cc;
3280 char_u *varname;
3281 hashtab_T *ht;
3282
3283 if (eval_lavars_used == NULL)
3284 return;
3285
3286 // truncate the name, so that we can use strcmp()
3287 cc = name[len];
3288 name[len] = NUL;
3289
3290 ht = find_var_ht(name, &varname);
3291 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3292 {
3293 if (find_var(name, NULL, TRUE) != NULL)
3294 *eval_lavars_used = TRUE;
3295 }
3296
3297 name[len] = cc;
3298}
3299
3300/*
3301 * Find variable "name" in the list of variables.
3302 * Return a pointer to it if found, NULL if not found.
3303 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003304 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003305 */
3306 dictitem_T *
3307find_var(char_u *name, hashtab_T **htp, int no_autoload)
3308{
3309 char_u *varname;
3310 hashtab_T *ht;
3311 dictitem_T *ret = NULL;
3312
3313 ht = find_var_ht(name, &varname);
3314 if (htp != NULL)
3315 *htp = ht;
3316 if (ht == NULL)
3317 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003318 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003319 if (ret != NULL)
3320 return ret;
3321
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003322 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003323 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003324 if (ret != NULL)
3325 return ret;
3326
3327 // in Vim9 script items without a scope can be script-local
3328 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3329 {
3330 ht = get_script_local_ht();
3331 if (ht != NULL)
3332 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003333 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003334 if (ret != NULL)
3335 {
3336 if (htp != NULL)
3337 *htp = ht;
3338 return ret;
3339 }
3340 }
3341 }
3342
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003343 // When using "vim9script autoload" script-local items are prefixed but can
3344 // be used with s:name.
3345 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003346 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003347 {
3348 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
3349
3350 if (si->sn_autoload_prefix != NULL)
3351 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003352 char_u *base_name = (name[0] == 's' && name[1] == ':')
3353 ? name + 2 : name;
3354 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003355
3356 if (auto_name != NULL)
3357 {
3358 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003359 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00003360 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003361 if (ret != NULL)
3362 {
3363 if (htp != NULL)
3364 *htp = ht;
3365 return ret;
3366 }
3367 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003368 }
3369 }
3370
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003371 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003372}
3373
3374/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003375 * Like find_var() but if the name starts with <SNR>99_ then look in the
3376 * referenced script (used for a funcref).
3377 */
3378 dictitem_T *
3379find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3380{
Keith Thompson184f71c2024-01-04 21:19:04 +01003381 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003382 {
3383 char_u *p = name + 5;
3384 int sid = getdigits(&p);
3385
3386 if (SCRIPT_ID_VALID(sid) && *p == '_')
3387 {
3388 hashtab_T *ht = &SCRIPT_VARS(sid);
3389
3390 if (ht != NULL)
3391 {
3392 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3393
3394 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003395 {
3396 if (htp != NULL)
3397 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003398 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003399 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003400 }
3401 }
3402 }
3403
3404 return find_var(name, htp, no_autoload);
3405}
3406
3407/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003408 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003409 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003410 * Returns NULL if not found.
3411 */
3412 dictitem_T *
3413find_var_in_ht(
3414 hashtab_T *ht,
3415 int htname,
3416 char_u *varname,
3417 int no_autoload)
3418{
3419 hashitem_T *hi;
3420
3421 if (*varname == NUL)
3422 {
3423 // Must be something like "s:", otherwise "ht" would be NULL.
3424 switch (htname)
3425 {
3426 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3427 case 'g': return &globvars_var;
3428 case 'v': return &vimvars_var;
3429 case 'b': return &curbuf->b_bufvar;
3430 case 'w': return &curwin->w_winvar;
3431 case 't': return &curtab->tp_winvar;
3432 case 'l': return get_funccal_local_var();
3433 case 'a': return get_funccal_args_var();
3434 }
3435 return NULL;
3436 }
3437
3438 hi = hash_find(ht, varname);
3439 if (HASHITEM_EMPTY(hi))
3440 {
3441 // For global variables we may try auto-loading the script. If it
3442 // worked find the variable again. Don't auto-load a script if it was
3443 // loaded already, otherwise it would be loaded every time when
3444 // checking if a function name is a Funcref variable.
3445 if (ht == &globvarht && !no_autoload)
3446 {
3447 // Note: script_autoload() may make "hi" invalid. It must either
3448 // be obtained again or not used.
3449 if (!script_autoload(varname, FALSE) || aborting())
3450 return NULL;
3451 hi = hash_find(ht, varname);
3452 }
3453 if (HASHITEM_EMPTY(hi))
3454 return NULL;
3455 }
3456 return HI2DI(hi);
3457}
3458
3459/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 * Get the script-local hashtab. NULL if not in a script context.
3461 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003462 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463get_script_local_ht(void)
3464{
3465 scid_T sid = current_sctx.sc_sid;
3466
Bram Moolenaare3d46852020-08-29 13:39:17 +02003467 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 return &SCRIPT_VARS(sid);
3469 return NULL;
3470}
3471
3472/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003473 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003474 * When "cmd" is TRUE it must look like a command, a function must be followed
3475 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003476 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003478 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003479lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003480 char_u *name,
3481 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003482 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003483 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484{
3485 hashtab_T *ht = get_script_local_ht();
3486 char_u buffer[30];
3487 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003488 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003490 int is_global = FALSE;
3491 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492
3493 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003494 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 if (len < sizeof(buffer) - 1)
3496 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003497 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 vim_strncpy(buffer, name, len);
3499 p = buffer;
3500 }
3501 else
3502 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003503 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003505 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 }
3507
3508 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003509 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510
3511 // if not script-local, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003512 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003513 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 if (p != buffer)
3515 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003516
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003517 // Find a function, so that a following "->" works.
3518 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3519 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003520 if (res != OK)
3521 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003522 p = skipwhite(name + len);
3523
3524 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003525 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003526 // Do not check for an internal function, since it might also be a
3527 // valid command, such as ":split" versus "split()".
3528 // Skip "g:" before a function name.
3529 if (name[0] == 'g' && name[1] == ':')
3530 {
3531 is_global = TRUE;
3532 fname = name + 2;
3533 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003534 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003535 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003536 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003537 }
3538
Bram Moolenaar709664c2020-12-12 14:33:41 +01003539 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540}
3541
3542/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003543 * Find the hashtab used for a variable name.
3544 * Return NULL if the name is not valid.
3545 * Set "varname" to the start of name without ':'.
3546 */
3547 hashtab_T *
3548find_var_ht(char_u *name, char_u **varname)
3549{
3550 hashitem_T *hi;
3551 hashtab_T *ht;
3552
3553 if (name[0] == NUL)
3554 return NULL;
3555 if (name[1] != ':')
3556 {
3557 // The name must not start with a colon or #.
3558 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3559 return NULL;
3560 *varname = name;
3561
3562 // "version" is "v:version" in all scopes if scriptversion < 3.
3563 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003564 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003565 {
3566 hi = hash_find(&compat_hashtab, name);
3567 if (!HASHITEM_EMPTY(hi))
3568 return &compat_hashtab;
3569 }
3570
3571 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003572 if (ht != NULL)
3573 return ht; // local variable
3574
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003575 // In Vim9 script items at the script level are script-local, except
3576 // for autoload names.
3577 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 {
3579 ht = get_script_local_ht();
3580 if (ht != NULL)
3581 return ht;
3582 }
3583
3584 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003585 }
3586 *varname = name + 2;
3587 if (*name == 'g') // global variable
3588 return &globvarht;
3589 // There must be no ':' or '#' in the rest of the name, unless g: is used
3590 if (vim_strchr(name + 2, ':') != NULL
3591 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3592 return NULL;
3593 if (*name == 'b') // buffer variable
3594 return &curbuf->b_vars->dv_hashtab;
3595 if (*name == 'w') // window variable
3596 return &curwin->w_vars->dv_hashtab;
3597 if (*name == 't') // tab page variable
3598 return &curtab->tp_vars->dv_hashtab;
3599 if (*name == 'v') // v: variable
3600 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003601 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003602 && get_current_funccal()->fc_func->uf_def_status
3603 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003605 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 if (*name == 'a') // a: function argument
3607 return get_funccal_args_ht();
3608 if (*name == 'l') // l: local function variable
3609 return get_funccal_local_ht();
3610 }
3611 if (*name == 's') // script variable
3612 {
3613 ht = get_script_local_ht();
3614 if (ht != NULL)
3615 return ht;
3616 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003617 return NULL;
3618}
3619
3620/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003621 * Get the string value of a (global/local) variable.
3622 * Note: see tv_get_string() for how long the pointer remains valid.
3623 * Returns NULL when it doesn't exist.
3624 */
3625 char_u *
3626get_var_value(char_u *name)
3627{
3628 dictitem_T *v;
3629
3630 v = find_var(name, NULL, FALSE);
3631 if (v == NULL)
3632 return NULL;
3633 return tv_get_string(&v->di_tv);
3634}
3635
3636/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003637 * Allocate a new hashtab for a sourced script. It will be used while
3638 * sourcing this script and when executing functions defined in the script.
3639 */
3640 void
3641new_script_vars(scid_T id)
3642{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003643 scriptvar_T *sv;
3644
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003645 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3646 if (sv == NULL)
3647 return;
3648 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003649 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003650}
3651
3652/*
3653 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3654 * point to it.
3655 */
3656 void
3657init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3658{
3659 hash_init(&dict->dv_hashtab);
3660 dict->dv_lock = 0;
3661 dict->dv_scope = scope;
3662 dict->dv_refcount = DO_NOT_FREE_CNT;
3663 dict->dv_copyID = 0;
3664 dict_var->di_tv.vval.v_dict = dict;
3665 dict_var->di_tv.v_type = VAR_DICT;
3666 dict_var->di_tv.v_lock = VAR_FIXED;
3667 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3668 dict_var->di_key[0] = NUL;
3669}
3670
3671/*
3672 * Unreference a dictionary initialized by init_var_dict().
3673 */
3674 void
3675unref_var_dict(dict_T *dict)
3676{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003677 // Now the dict needs to be freed if no one else is using it, go back to
3678 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003679 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3680 dict_unref(dict);
3681}
3682
3683/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003684 * Clean up a list of internal variables.
3685 * Frees all allocated variables and the value they contain.
3686 * Clears hashtab "ht", does not free it.
3687 */
3688 void
3689vars_clear(hashtab_T *ht)
3690{
3691 vars_clear_ext(ht, TRUE);
3692}
3693
3694/*
3695 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3696 */
3697 void
3698vars_clear_ext(hashtab_T *ht, int free_val)
3699{
3700 int todo;
3701 hashitem_T *hi;
3702 dictitem_T *v;
3703
3704 hash_lock(ht);
3705 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003706 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003707 {
3708 if (!HASHITEM_EMPTY(hi))
3709 {
3710 --todo;
3711
3712 // Free the variable. Don't remove it from the hashtab,
3713 // ht_array might change then. hash_clear() takes care of it
3714 // later.
3715 v = HI2DI(hi);
3716 if (free_val)
3717 clear_tv(&v->di_tv);
3718 if (v->di_flags & DI_FLAGS_ALLOC)
3719 vim_free(v);
3720 }
3721 }
3722 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003723 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003724}
3725
3726/*
3727 * Delete a variable from hashtab "ht" at item "hi".
3728 * Clear the variable value and free the dictitem.
3729 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003730 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003731delete_var(hashtab_T *ht, hashitem_T *hi)
3732{
3733 dictitem_T *di = HI2DI(hi);
3734
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003735 if (hash_remove(ht, hi, "delete variable") != OK)
3736 return;
3737
3738 clear_tv(&di->di_tv);
3739 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003740}
3741
3742/*
3743 * List the value of one internal variable.
3744 */
3745 static void
3746list_one_var(dictitem_T *v, char *prefix, int *first)
3747{
3748 char_u *tofree;
3749 char_u *s;
3750 char_u numbuf[NUMBUFLEN];
3751
3752 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3753 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3754 s == NULL ? (char_u *)"" : s, first);
3755 vim_free(tofree);
3756}
3757
3758 static void
3759list_one_var_a(
3760 char *prefix,
3761 char_u *name,
3762 int type,
3763 char_u *string,
3764 int *first) // when TRUE clear rest of screen and set to FALSE
3765{
3766 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3767 msg_start();
3768 msg_puts(prefix);
3769 if (name != NULL) // "a:" vars don't have a name stored
3770 msg_puts((char *)name);
3771 msg_putchar(' ');
3772 msg_advance(22);
3773 if (type == VAR_NUMBER)
3774 msg_putchar('#');
3775 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3776 msg_putchar('*');
3777 else if (type == VAR_LIST)
3778 {
3779 msg_putchar('[');
3780 if (*string == '[')
3781 ++string;
3782 }
3783 else if (type == VAR_DICT)
3784 {
3785 msg_putchar('{');
3786 if (*string == '{')
3787 ++string;
3788 }
3789 else
3790 msg_putchar(' ');
3791
3792 msg_outtrans(string);
3793
3794 if (type == VAR_FUNC || type == VAR_PARTIAL)
3795 msg_puts("()");
3796 if (*first)
3797 {
3798 msg_clr_eos();
3799 *first = FALSE;
3800 }
3801}
3802
3803/*
zeertzjqedcba962023-09-24 23:13:51 +02003804 * Addition handling for setting a v: variable.
3805 * Return TRUE if the variable should be set normally,
3806 * FALSE if nothing else needs to be done.
3807 */
3808 int
3809before_set_vvar(
3810 char_u *varname,
3811 dictitem_T *di,
3812 typval_T *tv,
3813 int copy,
3814 int *type_error)
3815{
3816 if (di->di_tv.v_type == VAR_STRING)
3817 {
3818 VIM_CLEAR(di->di_tv.vval.v_string);
3819 if (copy || tv->v_type != VAR_STRING)
3820 {
3821 char_u *val = tv_get_string(tv);
3822
3823 // Careful: when assigning to v:errmsg and
3824 // tv_get_string() causes an error message the variable
3825 // will already be set.
3826 if (di->di_tv.vval.v_string == NULL)
3827 di->di_tv.vval.v_string = vim_strsave(val);
3828 }
3829 else
3830 {
3831 // Take over the string to avoid an extra alloc/free.
3832 di->di_tv.vval.v_string = tv->vval.v_string;
3833 tv->vval.v_string = NULL;
3834 }
3835 return FALSE;
3836 }
3837 else if (di->di_tv.v_type == VAR_NUMBER)
3838 {
3839 di->di_tv.vval.v_number = tv_get_number(tv);
3840 if (STRCMP(varname, "searchforward") == 0)
3841 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3842#ifdef FEAT_SEARCH_EXTRA
3843 else if (STRCMP(varname, "hlsearch") == 0)
3844 {
3845 no_hlsearch = !di->di_tv.vval.v_number;
3846 redraw_all_later(UPD_SOME_VALID);
3847 }
3848#endif
3849 return FALSE;
3850 }
3851 else if (di->di_tv.v_type != tv->v_type)
3852 {
3853 *type_error = TRUE;
3854 return FALSE;
3855 }
3856 return TRUE;
3857}
3858
3859/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003860 * Set variable "name" to value in "tv".
3861 * If the variable already exists, the value is updated.
3862 * Otherwise the variable is created.
3863 */
3864 void
3865set_var(
3866 char_u *name,
3867 typval_T *tv,
3868 int copy) // make copy of value in "tv"
3869{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003870 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003871}
3872
3873/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003874 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003875 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003876 * If the variable already exists and "is_const" is FALSE the value is updated.
3877 * Otherwise the variable is created.
3878 */
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003879 int
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003880set_var_const(
3881 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003882 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003883 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003884 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003885 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003886 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003887 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003888{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003889 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003890 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003891 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003893 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003894 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003895 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003896 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003897 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003898 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003899 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003900 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003901 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003902 int free_tv_arg = !copy; // free tv_arg if not used
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003903 int rc = FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003904
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003905 if (sid != 0)
3906 {
3907 if (SCRIPT_ID_VALID(sid))
3908 ht = &SCRIPT_VARS(sid);
3909 varname = name;
3910 }
3911 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003912 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003913 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003914
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003915 if (in_vim9script() && is_export
3916 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3917 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00003918 ->sn_autoload_prefix != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003919 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003920 // In a vim9 autoload script an exported variable is put in the
3921 // global namespace with the autoload prefix.
3922 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003923 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003924 if (varname == NULL)
3925 goto failed;
3926 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003927 ht = &globvarht;
3928 }
3929 else
3930 ht = find_var_ht(name, &varname);
3931 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003932 if (ht == NULL || *varname == NUL)
3933 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003934 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003935 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003936 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003937 is_script_local = ht == get_script_local_ht() || sid != 0
3938 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003939
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003940 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003941 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003942 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003943 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003944 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003945 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003946 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003947 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003948 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003949 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003950 // Do not make g:var, w:var, b:var or t:var final.
3951 flags &= ~ASSIGN_FINAL;
3952
Bram Moolenaare535db82021-03-31 21:07:24 +02003953 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003954 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3955 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003956 // For "[a, _] = list" the underscore is ignored.
3957 if ((flags & ASSIGN_UNPACK) == 0)
3958 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003959 goto failed;
3960 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003963
Bram Moolenaar24e93162021-07-18 20:40:33 +02003964 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003965 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003966 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003967
Bram Moolenaar24e93162021-07-18 20:40:33 +02003968 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003969 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003970 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003971 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003973 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003974 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003975 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003976 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3977 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003978 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00003979 if (!in_vim9script())
3980 {
3981 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
3982 name);
3983 goto failed;
3984 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003985 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003986
Bram Moolenaar993faa32022-02-21 15:59:11 +00003987 // Search in parent scope which is possible to reference from lambda
3988 if (di == NULL)
3989 di = find_var_in_scoped_ht(name, TRUE);
3990
3991 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
3992 && var_wrong_func_name(name, di == NULL))
3993 goto failed;
3994
3995 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003996 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00003997 // Destination is a bool and the value is not, but it can be
3998 // converted.
3999 CLEAR_FIELD(bool_tv);
4000 bool_tv.v_type = VAR_BOOL;
4001 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
4002 tv = &bool_tv;
4003 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004004
Bram Moolenaar993faa32022-02-21 15:59:11 +00004005 if (di != NULL)
4006 {
4007 // Item already exists. Allowed to replace when reloading.
4008 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004009 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004010 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
4011 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004012 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004013 emsg(_(e_cannot_modify_existing_variable));
4014 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004015 }
4016
Bram Moolenaar993faa32022-02-21 15:59:11 +00004017 if (is_script_local && vim9script
4018 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02004019 {
4020 semsg(_(e_redefining_script_item_str), name);
4021 goto failed;
4022 }
4023
Ernie Raele75fde62023-12-21 17:18:54 +01004024 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004025 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004026
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004027 // List and Blob types can be modified in-place using the "+="
4028 // compound operator. For other types, this is not allowed.
4029 int type_inplace_modifiable =
4030 (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
4031
4032 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
4033 && ((flags & ASSIGN_COMPOUND_OP) == 0
4034 || !type_inplace_modifiable))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004035 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004036 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01004037 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004038
4039 if (sv != NULL)
4040 {
4041 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02004042 if (var_idx > 0)
4043 {
4044 where.wt_index = var_idx;
4045 where.wt_kind = WT_VARIABLE;
4046 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004047 if (check_script_var_type(sv, tv, name, where) == FAIL)
4048 goto failed;
4049 if (type == NULL)
4050 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004051 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004052 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004053 }
4054
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004055 // Modifying a final variable with a List value using the "+="
4056 // operator is allowed. For other types, it is not allowed.
4057 if (((flags & ASSIGN_FOR_LOOP) == 0
4058 && ((flags & ASSIGN_COMPOUND_OP) == 0
4059 || !type_inplace_modifiable))
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004060 ? var_check_permission(di, name) == FAIL
4061 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004062 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004063 }
4064 else
4065 {
4066 // can only redefine once
4067 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004068
Bram Moolenaar993faa32022-02-21 15:59:11 +00004069 // A Vim9 script-local variable is also present in sn_all_vars
4070 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004071 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004072 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004073 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004074 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004075 }
4076
Bram Moolenaar993faa32022-02-21 15:59:11 +00004077 // existing variable, need to clear the value
4078
zeertzjqedcba962023-09-24 23:13:51 +02004079 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004080 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004081 int type_error = FALSE;
4082 if (ht == &vimvarht
4083 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004084 {
zeertzjqedcba962023-09-24 23:13:51 +02004085 if (type_error)
4086 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4087 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004088 }
4089
4090 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004091
4092 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4093 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4094 {
4095 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4096
4097 update_script_var_block_id(name, si->sn_current_block_id);
4098 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004099 }
4100 else
4101 {
4102 // Item not found, check if a function already exists.
4103 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4104 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4105 {
4106 semsg(_(e_redefining_script_item_str), name);
4107 goto failed;
4108 }
4109
4110 // add a new variable
4111 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4112 {
4113 semsg(_(e_unknown_variable_str), name);
4114 goto failed;
4115 }
4116
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004117 if (check_hashtab_frozen(ht, "add variable"))
4118 goto failed;
4119
Bram Moolenaar993faa32022-02-21 15:59:11 +00004120 // Can't add "v:" or "a:" variable.
4121 if (ht == &vimvarht || ht == get_funccal_args_ht())
4122 {
4123 semsg(_(e_illegal_variable_name_str), name);
4124 goto failed;
4125 }
4126
4127 // Make sure the variable name is valid. In Vim9 script an
4128 // autoload variable must be prefixed with "g:" unless in an
4129 // autoload script.
4130 if (!valid_varname(varname, -1, !vim9script
4131 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4132 goto failed;
4133
zeertzjq1b438a82023-02-01 13:11:15 +00004134 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004135 if (di == NULL)
4136 goto failed;
4137 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004138 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004139 {
4140 vim_free(di);
4141 goto failed;
4142 }
4143 di->di_flags = DI_FLAGS_ALLOC;
4144 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4145 di->di_flags |= DI_FLAGS_LOCK;
4146
4147 // A Vim9 script-local variable is also added to sn_all_vars and
4148 // sn_var_vals. It may set "type" from "tv".
4149 if (var_in_vim9script || var_in_autoload)
4150 update_vim9_script_var(TRUE, di,
4151 var_in_autoload ? name : di->di_key, flags,
4152 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004153 }
4154
Bram Moolenaar993faa32022-02-21 15:59:11 +00004155 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004156 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004157 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004158 else
4159 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004160 *dest_tv = *tv;
4161 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004162 init_tv(tv);
4163 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004164 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004165
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004166 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004167 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004168
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004169 // ":const var = value" locks the value
4170 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004171 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004172 // Like :lockvar! name: lock the value and what it contains, but only
4173 // if the reference count is up to one. That locks only literal
4174 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004175 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004176
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004177 rc = OK;
4178
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004179failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004180 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004181 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004182 clear_tv(tv_arg);
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004183
4184 return rc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004185}
4186
4187/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004188 * Check in this order for backwards compatibility:
4189 * - Whether the variable is read-only
4190 * - Whether the variable value is locked
4191 * - Whether the variable is locked
4192 */
4193 int
4194var_check_permission(dictitem_T *di, char_u *name)
4195{
4196 if (var_check_ro(di->di_flags, name, FALSE)
4197 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4198 || var_check_lock(di->di_flags, name, FALSE))
4199 return FAIL;
4200 return OK;
4201}
4202
4203/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004204 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4205 * Also give an error message.
4206 */
4207 int
4208var_check_ro(int flags, char_u *name, int use_gettext)
4209{
4210 if (flags & DI_FLAGS_RO)
4211 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004212 if (name == NULL)
4213 emsg(_(e_cannot_change_readonly_variable));
4214 else
4215 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004216 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004217 return TRUE;
4218 }
4219 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4220 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004221 if (name == NULL)
4222 emsg(_(e_cannot_set_variable_in_sandbox));
4223 else
4224 semsg(_(e_cannot_set_variable_in_sandbox_str),
4225 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004226 return TRUE;
4227 }
4228 return FALSE;
4229}
4230
4231/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004232 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4233 * Also give an error message.
4234 */
4235 int
4236var_check_lock(int flags, char_u *name, int use_gettext)
4237{
4238 if (flags & DI_FLAGS_LOCK)
4239 {
4240 semsg(_(e_variable_is_locked_str),
4241 use_gettext ? (char_u *)_(name) : name);
4242 return TRUE;
4243 }
4244 return FALSE;
4245}
4246
4247/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004248 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4249 * Also give an error message.
4250 */
4251 int
4252var_check_fixed(int flags, char_u *name, int use_gettext)
4253{
4254 if (flags & DI_FLAGS_FIX)
4255 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004256 if (name == NULL)
4257 emsg(_(e_cannot_delete_variable));
4258 else
4259 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004260 use_gettext ? (char_u *)_(name) : name);
4261 return TRUE;
4262 }
4263 return FALSE;
4264}
4265
4266/*
4267 * Check if a funcref is assigned to a valid variable name.
4268 * Return TRUE and give an error if not.
4269 */
4270 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004271var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004272 char_u *name, // points to start of variable name
4273 int new_var) // TRUE when creating the variable
4274{
Bram Moolenaar32154662021-03-28 21:14:06 +02004275 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4276 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004277 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004278 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4279 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004280 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004281 ? name[2] : name[0])
4282 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004283 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004284 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004285 return TRUE;
4286 }
4287 // Don't allow hiding a function. When "v" is not NULL we might be
4288 // assigning another function to the same var, the type is checked
4289 // below.
4290 if (new_var && function_exists(name, FALSE))
4291 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004292 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004293 name);
4294 return TRUE;
4295 }
4296 return FALSE;
4297}
4298
4299/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004300 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4301 * value. Also give an error message, using "name" or _("name") when
4302 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004303 */
4304 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004305value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004306{
4307 if (lock & VAR_LOCKED)
4308 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004309 if (name == NULL)
4310 emsg(_(e_value_is_locked));
4311 else
4312 semsg(_(e_value_is_locked_str),
4313 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004314 return TRUE;
4315 }
4316 if (lock & VAR_FIXED)
4317 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004318 if (name == NULL)
4319 emsg(_(e_cannot_change_value));
4320 else
4321 semsg(_(e_cannot_change_value_of_str),
4322 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004323 return TRUE;
4324 }
4325 return FALSE;
4326}
4327
4328/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004329 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004330 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004331 * Return FALSE and give an error if not.
4332 */
4333 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004334valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004335{
4336 char_u *p;
4337
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004338 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004339 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004340 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004341 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004342 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004343 return FALSE;
4344 }
4345 return TRUE;
4346}
4347
4348/*
LemonBoy47d4e312022-05-04 18:12:55 +01004349 * Implements the logic to retrieve local variable and option values.
4350 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004351 */
4352 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004353get_var_from(
4354 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004355 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004356 typval_T *deftv, // Default value if not found.
4357 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4358 tabpage_T *tp, // can be NULL
4359 win_T *win,
4360 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004361{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004362 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004363 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004364 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004365 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004366 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004367
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004368 ++emsg_off;
4369
4370 rettv->v_type = VAR_STRING;
4371 rettv->vval.v_string = NULL;
4372
LemonBoy47d4e312022-05-04 18:12:55 +01004373 if (varname != NULL && tp != NULL && win != NULL
4374 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004375 {
4376 // Set curwin to be our win, temporarily. Also set the tabpage,
4377 // otherwise the window is not valid. Only do this when needed,
4378 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004379 // If we have a buffer reference avoid the switching, we're saving and
4380 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004381 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004382 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004383 {
LemonBoy47d4e312022-05-04 18:12:55 +01004384 // Handle options. There are no tab-local options.
4385 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004386 {
LemonBoy47d4e312022-05-04 18:12:55 +01004387 buf_T *save_curbuf = curbuf;
4388
4389 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004390 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004391 curbuf = buf;
4392
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004393 if (varname[1] == NUL)
4394 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004395 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004396 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004397
4398 if (opts != NULL)
4399 {
4400 rettv_dict_set(rettv, opts);
4401 done = TRUE;
4402 }
4403 }
LemonBoy47d4e312022-05-04 18:12:55 +01004404 else if (eval_option(&varname, rettv, TRUE) == OK)
4405 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004406 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004407
4408 curbuf = save_curbuf;
4409 }
4410 else if (*varname == NUL)
4411 {
4412 // Empty string: return a dict with all the local variables.
4413 if (htname == 'b')
4414 v = &buf->b_bufvar;
4415 else if (htname == 'w')
4416 v = &win->w_winvar;
4417 else
4418 v = &tp->tp_winvar;
4419 copy_tv(&v->di_tv, rettv);
4420 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004421 }
4422 else
4423 {
LemonBoy47d4e312022-05-04 18:12:55 +01004424 hashtab_T *ht;
4425
4426 if (htname == 'b')
4427 ht = &buf->b_vars->dv_hashtab;
4428 else if (htname == 'w')
4429 ht = &win->w_vars->dv_hashtab;
4430 else
4431 ht = &tp->tp_vars->dv_hashtab;
4432
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004433 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004434 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004435 if (v != NULL)
4436 {
4437 copy_tv(&v->di_tv, rettv);
4438 done = TRUE;
4439 }
4440 }
4441 }
4442
4443 if (need_switch_win)
4444 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004445 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004446 }
4447
LemonBoy47d4e312022-05-04 18:12:55 +01004448 if (!done && deftv->v_type != VAR_UNKNOWN)
4449 // use the default value
4450 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004451
4452 --emsg_off;
4453}
4454
4455/*
LemonBoy47d4e312022-05-04 18:12:55 +01004456 * getwinvar() and gettabwinvar()
4457 */
4458 static void
4459getwinvar(
4460 typval_T *argvars,
4461 typval_T *rettv,
4462 int off) // 1 for gettabwinvar()
4463{
4464 char_u *varname;
4465 tabpage_T *tp;
4466 win_T *win;
4467
4468 if (off == 1)
4469 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4470 else
4471 tp = curtab;
4472 win = find_win_by_nr(&argvars[off], tp);
4473 varname = tv_get_string_chk(&argvars[off + 1]);
4474
4475 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4476}
4477
4478/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004479 * Set option "varname" to the value of "varp" for the current buffer/window.
4480 */
4481 static void
4482set_option_from_tv(char_u *varname, typval_T *varp)
4483{
4484 long numval = 0;
4485 char_u *strval;
4486 char_u nbuf[NUMBUFLEN];
4487 int error = FALSE;
4488
zeertzjq4c7cb372023-06-14 16:39:54 +01004489 int opt_idx = findoption(varname);
4490 if (opt_idx < 0)
4491 {
4492 semsg(_(e_unknown_option_str_2), varname);
4493 return;
4494 }
4495 int opt_p_flags = get_option_flags(opt_idx);
4496
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004497 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004498 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004499 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004500 {
4501 emsg(_(e_string_required));
4502 return;
4503 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004504 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004505 strval = (char_u *)"0"; // avoid using "false"
4506 }
4507 else
4508 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004509 if ((opt_p_flags & (P_NUM|P_BOOL))
4510 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004511 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004512 if (!error)
4513 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004514 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004515 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004516 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004517}
4518
4519/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004520 * "setwinvar()" and "settabwinvar()" functions
4521 */
4522 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004523setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004524{
4525 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004526 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004527 int need_switch_win;
4528 char_u *varname, *winvarname;
4529 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004530 tabpage_T *tp = NULL;
4531
4532 if (check_secure())
4533 return;
4534
4535 if (off == 1)
4536 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4537 else
4538 tp = curtab;
4539 win = find_win_by_nr(&argvars[off], tp);
4540 varname = tv_get_string_chk(&argvars[off + 1]);
4541 varp = &argvars[off + 2];
4542
zeertzjqea720ae2023-01-03 10:54:09 +00004543 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004544 return;
4545
4546 need_switch_win = !(tp == curtab && win == curwin);
4547 if (!need_switch_win
4548 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004549 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004550 if (*varname == '&')
4551 set_option_from_tv(varname + 1, varp);
4552 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004553 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004554 winvarname = alloc(STRLEN(varname) + 3);
4555 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004556 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004557 STRCPY(winvarname, "w:");
4558 STRCPY(winvarname + 2, varname);
4559 set_var(winvarname, varp, TRUE);
4560 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004561 }
4562 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004563 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004564 if (need_switch_win)
4565 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004566}
4567
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004568/*
4569 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4570 * v:option_type, and v:option_command.
4571 */
4572 void
4573reset_v_option_vars(void)
4574{
4575 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4576 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4577 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4578 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4579 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4580 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4581}
4582
4583/*
4584 * Add an assert error to v:errors.
4585 */
4586 void
4587assert_error(garray_T *gap)
4588{
4589 struct vimvar *vp = &vimvars[VV_ERRORS];
4590
Bram Moolenaard787e402021-12-24 21:36:12 +00004591 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004592 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004593 set_vim_var_list(VV_ERRORS, list_alloc());
4594 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4595}
4596
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004597 int
4598var_exists(char_u *var)
4599{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004600 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004601 char_u *name;
4602 char_u *tofree;
4603 typval_T tv;
4604 int len = 0;
4605 int n = FALSE;
4606
4607 // get_name_len() takes care of expanding curly braces
4608 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004609 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004610 if (len > 0)
4611 {
4612 if (tofree != NULL)
4613 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004614 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004615 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004616 if (n)
4617 {
4618 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004619 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004620 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4621 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004622 if (n)
4623 clear_tv(&tv);
4624 }
4625 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004626 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004627 n = FALSE;
4628
4629 vim_free(tofree);
4630 return n;
4631}
4632
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004633static lval_T *redir_lval = NULL;
4634#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4635static garray_T redir_ga; // only valid when redir_lval is not NULL
4636static char_u *redir_endp = NULL;
4637static char_u *redir_varname = NULL;
4638
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004639 int
4640alloc_redir_lval(void)
4641{
4642 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4643 if (redir_lval == NULL)
4644 return FAIL;
4645 return OK;
4646}
4647
4648 void
4649clear_redir_lval(void)
4650{
4651 VIM_CLEAR(redir_lval);
4652}
4653
4654 void
4655init_redir_ga(void)
4656{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004657 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004658}
4659
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004660/*
4661 * Start recording command output to a variable
4662 * When "append" is TRUE append to an existing variable.
4663 * Returns OK if successfully completed the setup. FAIL otherwise.
4664 */
4665 int
4666var_redir_start(char_u *name, int append)
4667{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004668 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004669 typval_T tv;
4670
4671 // Catch a bad name early.
4672 if (!eval_isnamec1(*name))
4673 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004674 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004675 return FAIL;
4676 }
4677
4678 // Make a copy of the name, it is used in redir_lval until redir ends.
4679 redir_varname = vim_strsave(name);
4680 if (redir_varname == NULL)
4681 return FAIL;
4682
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004683 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004684 {
4685 var_redir_stop();
4686 return FAIL;
4687 }
4688
4689 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004690 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004691
4692 // Parse the variable name (can be a dict or list entry).
4693 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4694 FNE_CHECK_START);
4695 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4696 {
4697 clear_lval(redir_lval);
4698 if (redir_endp != NULL && *redir_endp != NUL)
4699 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004700 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004701 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004702 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004703 redir_endp = NULL; // don't store a value, only cleanup
4704 var_redir_stop();
4705 return FAIL;
4706 }
4707
4708 // check if we can write to the variable: set it to or append an empty
4709 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004710 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004711 tv.v_type = VAR_STRING;
4712 tv.vval.v_string = (char_u *)"";
4713 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004714 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004715 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004716 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004717 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004718 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004719 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004720 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004721 {
4722 redir_endp = NULL; // don't store a value, only cleanup
4723 var_redir_stop();
4724 return FAIL;
4725 }
4726
4727 return OK;
4728}
4729
4730/*
4731 * Append "value[value_len]" to the variable set by var_redir_start().
4732 * The actual appending is postponed until redirection ends, because the value
4733 * appended may in fact be the string we write to, changing it may cause freed
4734 * memory to be used:
4735 * :redir => foo
4736 * :let foo
4737 * :redir END
4738 */
4739 void
4740var_redir_str(char_u *value, int value_len)
4741{
4742 int len;
4743
4744 if (redir_lval == NULL)
4745 return;
4746
4747 if (value_len == -1)
4748 len = (int)STRLEN(value); // Append the entire string
4749 else
4750 len = value_len; // Append only "value_len" characters
4751
4752 if (ga_grow(&redir_ga, len) == OK)
4753 {
4754 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4755 redir_ga.ga_len += len;
4756 }
4757 else
4758 var_redir_stop();
4759}
4760
4761/*
4762 * Stop redirecting command output to a variable.
4763 * Frees the allocated memory.
4764 */
4765 void
4766var_redir_stop(void)
4767{
4768 typval_T tv;
4769
4770 if (EVALCMD_BUSY)
4771 {
4772 redir_lval = NULL;
4773 return;
4774 }
4775
4776 if (redir_lval != NULL)
4777 {
4778 // If there was no error: assign the text to the variable.
4779 if (redir_endp != NULL)
4780 {
4781 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4782 tv.v_type = VAR_STRING;
4783 tv.vval.v_string = redir_ga.ga_data;
4784 // Call get_lval() again, if it's inside a Dict or List it may
4785 // have changed.
4786 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4787 FALSE, FALSE, 0, FNE_CHECK_START);
4788 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004789 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004790 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004791 clear_lval(redir_lval);
4792 }
4793
4794 // free the collected output
4795 VIM_CLEAR(redir_ga.ga_data);
4796
4797 VIM_CLEAR(redir_lval);
4798 }
4799 VIM_CLEAR(redir_varname);
4800}
4801
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004802/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004803 * Get the collected redirected text and clear redir_ga.
4804 */
4805 char_u *
4806get_clear_redir_ga(void)
4807{
4808 char_u *res;
4809
4810 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4811 res = redir_ga.ga_data;
4812 redir_ga.ga_data = NULL;
4813 return res;
4814}
4815
4816/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004817 * "gettabvar()" function
4818 */
4819 void
4820f_gettabvar(typval_T *argvars, typval_T *rettv)
4821{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004822 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004823 tabpage_T *tp;
4824 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004825
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004826 if (in_vim9script()
4827 && (check_for_number_arg(argvars, 0) == FAIL
4828 || check_for_string_arg(argvars, 1) == FAIL))
4829 return;
4830
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004831 varname = tv_get_string_chk(&argvars[1]);
4832 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004833 if (tp != NULL)
4834 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4835 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004836
LemonBoy47d4e312022-05-04 18:12:55 +01004837 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004838}
4839
4840/*
4841 * "gettabwinvar()" function
4842 */
4843 void
4844f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4845{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004846 if (in_vim9script()
4847 && (check_for_number_arg(argvars, 0) == FAIL
4848 || check_for_number_arg(argvars, 1) == FAIL
4849 || check_for_string_arg(argvars, 2) == FAIL))
4850 return;
4851
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004852 getwinvar(argvars, rettv, 1);
4853}
4854
4855/*
4856 * "getwinvar()" function
4857 */
4858 void
4859f_getwinvar(typval_T *argvars, typval_T *rettv)
4860{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004861 if (in_vim9script()
4862 && (check_for_number_arg(argvars, 0) == FAIL
4863 || check_for_string_arg(argvars, 1) == FAIL))
4864 return;
4865
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004866 getwinvar(argvars, rettv, 0);
4867}
4868
4869/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004870 * "getbufvar()" function
4871 */
4872 void
4873f_getbufvar(typval_T *argvars, typval_T *rettv)
4874{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004875 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004876 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004877
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004878 if (in_vim9script()
4879 && (check_for_buffer_arg(argvars, 0) == FAIL
4880 || check_for_string_arg(argvars, 1) == FAIL))
4881 return;
4882
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004883 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004884 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004885
LemonBoy47d4e312022-05-04 18:12:55 +01004886 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004887}
4888
4889/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004890 * "settabvar()" function
4891 */
4892 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004893f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004894{
4895 tabpage_T *save_curtab;
4896 tabpage_T *tp;
zeertzjqb47fbb42024-02-12 22:50:26 +01004897 tabpage_T *save_lu_tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004898 char_u *varname, *tabvarname;
4899 typval_T *varp;
4900
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004901 if (check_secure())
4902 return;
4903
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004904 if (in_vim9script()
4905 && (check_for_number_arg(argvars, 0) == FAIL
4906 || check_for_string_arg(argvars, 1) == FAIL))
4907 return;
4908
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004909 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4910 varname = tv_get_string_chk(&argvars[1]);
4911 varp = &argvars[2];
4912
zeertzjqea720ae2023-01-03 10:54:09 +00004913 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004914 return;
4915
4916 save_curtab = curtab;
zeertzjqb47fbb42024-02-12 22:50:26 +01004917 save_lu_tp = lastused_tabpage;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004918 goto_tabpage_tp(tp, FALSE, FALSE);
4919
4920 tabvarname = alloc(STRLEN(varname) + 3);
4921 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004922 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004923 STRCPY(tabvarname, "t:");
4924 STRCPY(tabvarname + 2, varname);
4925 set_var(tabvarname, varp, TRUE);
4926 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004927 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004928
zeertzjqb47fbb42024-02-12 22:50:26 +01004929 // Restore current tabpage and last accessed tabpage.
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004930 if (valid_tabpage(save_curtab))
zeertzjqb47fbb42024-02-12 22:50:26 +01004931 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004932 goto_tabpage_tp(save_curtab, FALSE, FALSE);
zeertzjqb47fbb42024-02-12 22:50:26 +01004933 if (valid_tabpage(save_lu_tp))
4934 lastused_tabpage = save_lu_tp;
4935 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004936}
4937
4938/*
4939 * "settabwinvar()" function
4940 */
4941 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004942f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004943{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004944 if (in_vim9script()
4945 && (check_for_number_arg(argvars, 0) == FAIL
4946 || check_for_number_arg(argvars, 1) == FAIL
4947 || check_for_string_arg(argvars, 2) == FAIL))
4948 return;
4949
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004950 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004951}
4952
4953/*
4954 * "setwinvar()" function
4955 */
4956 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004957f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004958{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004959 if (in_vim9script()
4960 && (check_for_number_arg(argvars, 0) == FAIL
4961 || check_for_string_arg(argvars, 1) == FAIL))
4962 return;
4963
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004964 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004965}
4966
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004967/*
4968 * "setbufvar()" function
4969 */
4970 void
4971f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4972{
4973 buf_T *buf;
4974 char_u *varname, *bufvarname;
4975 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004976
4977 if (check_secure())
4978 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004979
4980 if (in_vim9script()
4981 && (check_for_buffer_arg(argvars, 0) == FAIL
4982 || check_for_string_arg(argvars, 1) == FAIL))
4983 return;
4984
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004985 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004986 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004987 varp = &argvars[2];
4988
zeertzjqea720ae2023-01-03 10:54:09 +00004989 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004990 return;
4991
4992 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004993 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004994 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01004995 // safe the current window position, it could
4996 // change because of 'scrollbind' window-local
4997 // options
4998 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004999
5000 // Set curbuf to be our buf, temporarily.
5001 aucmd_prepbuf(&aco, buf);
5002 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005003 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005004 // Only when it worked to set "curbuf".
5005 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005006
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005007 // reset notion of buffer
5008 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005009 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005010 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005011 }
5012 else
5013 {
5014 bufvarname = alloc(STRLEN(varname) + 3);
5015 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005016 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005017 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02005018
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005019 curbuf = buf;
5020 STRCPY(bufvarname, "b:");
5021 STRCPY(bufvarname + 2, varname);
5022 set_var(bufvarname, varp, TRUE);
5023 vim_free(bufvarname);
5024 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005025 }
5026 }
5027}
5028
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005029/*
5030 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005031 * When "arg" is zero "res.cb_name" is set to an empty string.
5032 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
5033 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005034 */
5035 callback_T
5036get_callback(typval_T *arg)
5037{
Bram Moolenaar14e579092020-03-07 16:59:25 +01005038 callback_T res;
5039 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005040
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005041 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005042 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
5043 {
5044 res.cb_partial = arg->vval.v_partial;
5045 ++res.cb_partial->pt_refcount;
5046 res.cb_name = partial_name(res.cb_partial);
5047 }
5048 else
5049 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01005050 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01005051 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01005052 r = FAIL;
5053 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005054 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005055 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005056 if (arg->v_type == VAR_STRING)
5057 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005058 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005059 if (name != NULL)
5060 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005061 res.cb_name = name;
5062 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005063 }
5064 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005065 func_ref(res.cb_name);
5066 }
5067 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005068 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005069 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005070 r = FAIL;
5071
5072 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005073 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005074 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005075 res.cb_name = NULL;
5076 }
5077 }
5078 return res;
5079}
5080
5081/*
5082 * Copy a callback into a typval_T.
5083 */
5084 void
5085put_callback(callback_T *cb, typval_T *tv)
5086{
5087 if (cb->cb_partial != NULL)
5088 {
5089 tv->v_type = VAR_PARTIAL;
5090 tv->vval.v_partial = cb->cb_partial;
5091 ++tv->vval.v_partial->pt_refcount;
5092 }
5093 else
5094 {
5095 tv->v_type = VAR_FUNC;
5096 tv->vval.v_string = vim_strsave(cb->cb_name);
5097 func_ref(cb->cb_name);
5098 }
5099}
5100
5101/*
5102 * Make a copy of "src" into "dest", allocating the function name if needed,
5103 * without incrementing the refcount.
5104 */
5105 void
5106set_callback(callback_T *dest, callback_T *src)
5107{
5108 if (src->cb_partial == NULL)
5109 {
5110 // just a function name, make a copy
5111 dest->cb_name = vim_strsave(src->cb_name);
5112 dest->cb_free_name = TRUE;
5113 }
5114 else
5115 {
5116 // cb_name is a pointer into cb_partial
5117 dest->cb_name = src->cb_name;
5118 dest->cb_free_name = FALSE;
5119 }
5120 dest->cb_partial = src->cb_partial;
5121}
5122
5123/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005124 * Copy callback from "src" to "dest", incrementing the refcounts.
5125 */
5126 void
5127copy_callback(callback_T *dest, callback_T *src)
5128{
5129 dest->cb_partial = src->cb_partial;
5130 if (dest->cb_partial != NULL)
5131 {
5132 dest->cb_name = src->cb_name;
5133 dest->cb_free_name = FALSE;
5134 ++dest->cb_partial->pt_refcount;
5135 }
5136 else
5137 {
5138 dest->cb_name = vim_strsave(src->cb_name);
5139 dest->cb_free_name = TRUE;
5140 func_ref(src->cb_name);
5141 }
5142}
5143
5144/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005145 * When a callback refers to an autoload import, change the function name to
5146 * the "path#name" form. Uses the current script context.
5147 * Only works when the name is allocated.
5148 */
5149 void
5150expand_autload_callback(callback_T *cb)
5151{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005152 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005153 char_u *p;
5154 imported_T *import;
5155
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005156 if (!in_vim9script() || cb->cb_name == NULL
5157 || (!cb->cb_free_name
5158 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005159 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005160 if (cb->cb_partial != NULL)
5161 name = cb->cb_partial->pt_name;
5162 else
5163 name = cb->cb_name;
5164 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005165 if (p == NULL)
5166 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005167
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005168 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005169 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5170 return;
5171
5172 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5173 if (si->sn_autoload_prefix == NULL)
5174 return;
5175
5176 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5177 if (newname == NULL)
5178 return;
5179
5180 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005181 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005182 if (cb->cb_name == cb->cb_partial->pt_name)
5183 cb->cb_name = newname;
5184 vim_free(cb->cb_partial->pt_name);
5185 cb->cb_partial->pt_name = newname;
5186 }
5187 else
5188 {
5189 vim_free(cb->cb_name);
5190 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005191 }
5192}
5193
5194/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005195 * Unref/free "callback" returned by get_callback() or set_callback().
5196 */
5197 void
5198free_callback(callback_T *callback)
5199{
5200 if (callback->cb_partial != NULL)
5201 {
5202 partial_unref(callback->cb_partial);
5203 callback->cb_partial = NULL;
5204 }
5205 else if (callback->cb_name != NULL)
5206 func_unref(callback->cb_name);
5207 if (callback->cb_free_name)
5208 {
5209 vim_free(callback->cb_name);
5210 callback->cb_free_name = FALSE;
5211 }
5212 callback->cb_name = NULL;
5213}
5214
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005215#endif // FEAT_EVAL