blob: 9f03ada09afad080469bc15f51bdf81117e636f2 [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},
zeertzjq20e045f2024-10-28 22:05:26 +0100164 {VV_NAME("cmdcomplete", VAR_BOOL), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200165};
166
167// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000168#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200169#define vv_nr vv_di.di_tv.vval.v_number
170#define vv_float vv_di.di_tv.vval.v_float
171#define vv_str vv_di.di_tv.vval.v_string
172#define vv_list vv_di.di_tv.vval.v_list
173#define vv_dict vv_di.di_tv.vval.v_dict
174#define vv_blob vv_di.di_tv.vval.v_blob
175#define vv_tv vv_di.di_tv
176
177static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200178static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200179#define vimvarht vimvardict.dv_hashtab
180
181// for VIM_VERSION_ defines
182#include "version.h"
183
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200184static void list_glob_vars(int *first);
185static void list_buf_vars(int *first);
186static void list_win_vars(int *first);
187static void list_tab_vars(int *first);
188static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100189static 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 +0200190static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
191static 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 +0200192static void list_one_var(dictitem_T *v, char *prefix, int *first);
193static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
194
195/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200196 * Initialize global and vim special variables
197 */
198 void
199evalvars_init(void)
200{
201 int i;
202 struct vimvar *p;
203
204 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
205 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
206 vimvardict.dv_lock = VAR_FIXED;
207 hash_init(&compat_hashtab);
208
209 for (i = 0; i < VV_LEN; ++i)
210 {
211 p = &vimvars[i];
212 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
213 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100214 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200215 getout(1);
216 }
217 STRCPY(p->vv_di.di_key, p->vv_name);
218 if (p->vv_flags & VV_RO)
219 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
220 else if (p->vv_flags & VV_RO_SBX)
221 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
222 else
223 p->vv_di.di_flags = DI_FLAGS_FIX;
224
225 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000226 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000227 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200228 if (p->vv_flags & VV_COMPAT)
229 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000230 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200232 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
233 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200234
235 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
236 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100237 set_vim_var_nr(VV_EXITING, VVAL_NULL);
zeertzjq20e045f2024-10-28 22:05:26 +0100238 set_vim_var_nr(VV_CMDCOMPLETE, VVAL_FALSE);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200239 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
240 set_vim_var_list(VV_ERRORS, list_alloc());
241 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
242
243 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
244 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
245 set_vim_var_nr(VV_NONE, VVAL_NONE);
246 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100247 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
248 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100249 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000250 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
251 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
252 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000253 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200254
255 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
256 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
257 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
258 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
259 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
260 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
261 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
262 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
263 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
264 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
265 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000266 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
267 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200268 set_vim_var_nr(VV_TYPE_TYPEALIAS, VAR_TYPE_TYPEALIAS);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100269 set_vim_var_nr(VV_TYPE_ENUM, VAR_TYPE_ENUM);
270 set_vim_var_nr(VV_TYPE_ENUMVALUE, VAR_TYPE_ENUMVALUE);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200271
272 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
273
Drew Vogele30d1022021-10-24 20:35:07 +0100274 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
275
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200276#ifdef FEAT_PYTHON3
277 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
278#endif
279
Bram Moolenaar439c0362020-06-06 15:58:03 +0200280 // Default for v:register is not 0 but '"'. This is adjusted once the
281 // clipboard has been setup by calling reset_reg_var().
282 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200283}
284
285#if defined(EXITFREE) || defined(PROTO)
286/*
287 * Free all vim variables information on exit
288 */
289 void
290evalvars_clear(void)
291{
292 int i;
293 struct vimvar *p;
294
295 for (i = 0; i < VV_LEN; ++i)
296 {
297 p = &vimvars[i];
298 if (p->vv_di.di_tv.v_type == VAR_STRING)
299 VIM_CLEAR(p->vv_str);
300 else if (p->vv_di.di_tv.v_type == VAR_LIST)
301 {
302 list_unref(p->vv_list);
303 p->vv_list = NULL;
304 }
305 }
306 hash_clear(&vimvarht);
307 hash_init(&vimvarht); // garbage_collect() will access it
308 hash_clear(&compat_hashtab);
309
310 // global variables
311 vars_clear(&globvarht);
312
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100313 // Script-local variables. Clear all the variables here.
314 // The scriptvar_T is cleared later in free_scriptnames(), because a
315 // variable in one script might hold a reference to the whole scope of
316 // another script.
317 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200318 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200319}
320#endif
321
322 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200323garbage_collect_globvars(int copyID)
324{
325 return set_ref_in_ht(&globvarht, copyID, NULL);
326}
327
328 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200329garbage_collect_vimvars(int copyID)
330{
331 return set_ref_in_ht(&vimvarht, copyID, NULL);
332}
333
334 int
335garbage_collect_scriptvars(int copyID)
336{
Bram Moolenaared234f22020-10-15 20:42:20 +0200337 int i;
338 int idx;
339 int abort = FALSE;
340 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200341
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100342 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200343 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200344 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
345
Bram Moolenaared234f22020-10-15 20:42:20 +0200346 si = SCRIPT_ITEM(i);
347 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
348 {
349 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
350
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100351 if (sv->sv_name != NULL)
352 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200353 }
354 }
355
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200356 return abort;
357}
358
359/*
360 * Set an internal variable to a string value. Creates the variable if it does
361 * not already exist.
362 */
363 void
364set_internal_string_var(char_u *name, char_u *value)
365{
366 char_u *val;
367 typval_T *tvp;
368
369 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000370 if (val == NULL)
371 return;
372
373 tvp = alloc_string_tv(val);
374 if (tvp == NULL)
375 return;
376
377 set_var(name, tvp, FALSE);
378 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200379}
380
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200381 int
382eval_charconvert(
383 char_u *enc_from,
384 char_u *enc_to,
385 char_u *fname_from,
386 char_u *fname_to)
387{
388 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000389 sctx_T saved_sctx = current_sctx;
390 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200391
392 set_vim_var_string(VV_CC_FROM, enc_from, -1);
393 set_vim_var_string(VV_CC_TO, enc_to, -1);
394 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
395 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000396 ctx = get_option_sctx("charconvert");
397 if (ctx != NULL)
398 current_sctx = *ctx;
399
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100400 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200401 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000402
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200403 set_vim_var_string(VV_CC_FROM, NULL, -1);
404 set_vim_var_string(VV_CC_TO, NULL, -1);
405 set_vim_var_string(VV_FNAME_IN, NULL, -1);
406 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000407 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200408
409 if (err)
410 return FAIL;
411 return OK;
412}
413
414# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
415 int
416eval_printexpr(char_u *fname, char_u *args)
417{
418 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000419 sctx_T saved_sctx = current_sctx;
420 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200421
422 set_vim_var_string(VV_FNAME_IN, fname, -1);
423 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000424 ctx = get_option_sctx("printexpr");
425 if (ctx != NULL)
426 current_sctx = *ctx;
427
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100428 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200429 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000430
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200431 set_vim_var_string(VV_FNAME_IN, NULL, -1);
432 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000433 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200434
435 if (err)
436 {
437 mch_remove(fname);
438 return FAIL;
439 }
440 return OK;
441}
442# endif
443
444# if defined(FEAT_DIFF) || defined(PROTO)
445 void
446eval_diff(
447 char_u *origfile,
448 char_u *newfile,
449 char_u *outfile)
450{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000451 sctx_T saved_sctx = current_sctx;
452 sctx_T *ctx;
453 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200454
455 set_vim_var_string(VV_FNAME_IN, origfile, -1);
456 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
457 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000458
459 ctx = get_option_sctx("diffexpr");
460 if (ctx != NULL)
461 current_sctx = *ctx;
462
463 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100464 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000465 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000466
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200467 set_vim_var_string(VV_FNAME_IN, NULL, -1);
468 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
469 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000470 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200471}
472
473 void
474eval_patch(
475 char_u *origfile,
476 char_u *difffile,
477 char_u *outfile)
478{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000479 sctx_T saved_sctx = current_sctx;
480 sctx_T *ctx;
481 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200482
483 set_vim_var_string(VV_FNAME_IN, origfile, -1);
484 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
485 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000486
487 ctx = get_option_sctx("patchexpr");
488 if (ctx != NULL)
489 current_sctx = *ctx;
490
491 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100492 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000493 free_tv(tv);
494
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200495 set_vim_var_string(VV_FNAME_IN, NULL, -1);
496 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
497 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000498 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200499}
500# endif
501
502#if defined(FEAT_SPELL) || defined(PROTO)
503/*
504 * Evaluate an expression to a list with suggestions.
505 * For the "expr:" part of 'spellsuggest'.
506 * Returns NULL when there is an error.
507 */
508 list_T *
509eval_spell_expr(char_u *badword, char_u *expr)
510{
511 typval_T save_val;
512 typval_T rettv;
513 list_T *list = NULL;
514 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000515 sctx_T saved_sctx = current_sctx;
516 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100517 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200518
519 // Set "v:val" to the bad word.
520 prepare_vimvar(VV_VAL, &save_val);
521 set_vim_var_string(VV_VAL, badword, -1);
522 if (p_verbose == 0)
523 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000524 ctx = get_option_sctx("spellsuggest");
525 if (ctx != NULL)
526 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200527
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100528 r = may_call_simple_func(p, &rettv);
529 if (r == NOTDONE)
530 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
531 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200532 {
533 if (rettv.v_type != VAR_LIST)
534 clear_tv(&rettv);
535 else
536 list = rettv.vval.v_list;
537 }
538
539 if (p_verbose == 0)
540 --emsg_off;
541 clear_tv(get_vim_var_tv(VV_VAL));
542 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000543 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200544
545 return list;
546}
547
548/*
549 * "list" is supposed to contain two items: a word and a number. Return the
550 * word in "pp" and the number as the return value.
551 * Return -1 if anything isn't right.
552 * Used to get the good word and score from the eval_spell_expr() result.
553 */
554 int
555get_spellword(list_T *list, char_u **pp)
556{
557 listitem_T *li;
558
559 li = list->lv_first;
560 if (li == NULL)
561 return -1;
562 *pp = tv_get_string(&li->li_tv);
563
564 li = li->li_next;
565 if (li == NULL)
566 return -1;
567 return (int)tv_get_number(&li->li_tv);
568}
569#endif
570
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200571/*
572 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200573 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200574 * When not used yet add the variable to the v: hashtable.
575 */
576 void
577prepare_vimvar(int idx, typval_T *save_tv)
578{
579 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200580 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000581 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000582 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200583}
584
585/*
586 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200587 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200588 * When no longer defined, remove the variable from the v: hashtable.
589 */
590 void
591restore_vimvar(int idx, typval_T *save_tv)
592{
593 hashitem_T *hi;
594
595 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000596 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
597 return;
598
599 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
600 if (HASHITEM_EMPTY(hi))
601 internal_error("restore_vimvar()");
602 else
603 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200604}
605
606/*
607 * List Vim variables.
608 */
609 static void
610list_vim_vars(int *first)
611{
612 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
613}
614
615/*
616 * List script-local variables, if there is a script.
617 */
618 static void
619list_script_vars(int *first)
620{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200621 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200622 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
623 "s:", FALSE, first);
624}
625
626/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100627 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
628 * But only when an identifier character follows.
629 */
630 int
631is_scoped_variable(char_u *name)
632{
633 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
634 && name[1] == ':'
635 && eval_isnamec(name[2]);
636}
637
638/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100639 * Evaluate one Vim expression {expr} in string "p" and append the
640 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100641 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100642 * Return a pointer to the character after "}", NULL for an error.
643 */
644 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100645eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100646{
647 char_u *block_start = skipwhite(p + 1); // skip the opening {
648 char_u *block_end = block_start;
649 char_u *expr_val;
650
651 if (*block_start == NUL)
652 {
653 semsg(_(e_missing_close_curly_str), p);
654 return NULL;
655 }
656 if (skip_expr(&block_end, NULL) == FAIL)
657 return NULL;
658 block_end = skipwhite(block_end);
659 if (*block_end != '}')
660 {
661 semsg(_(e_missing_close_curly_str), p);
662 return NULL;
663 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100664 if (evaluate)
665 {
666 *block_end = NUL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200667 expr_val = eval_to_string(block_start, FALSE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100668 *block_end = '}';
669 if (expr_val == NULL)
670 return NULL;
671 ga_concat(gap, expr_val);
672 vim_free(expr_val);
673 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100674
675 return block_end + 1;
676}
677
678/*
679 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
680 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
681 * Used for a heredoc assignment.
682 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100683 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100684 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100685eval_all_expr_in_str(char_u *str)
686{
687 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100688 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100689
690 ga_init2(&ga, 1, 80);
691 p = str;
692
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100693 while (*p != NUL)
694 {
LemonBoy2eaef102022-05-06 13:14:50 +0100695 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100696 int escaped_brace = FALSE;
697
698 // Look for a block start.
699 lit_start = p;
700 while (*p != '{' && *p != '}' && *p != NUL)
701 ++p;
702
703 if (*p != NUL && *p == p[1])
704 {
705 // Escaped brace, unescape and continue.
706 // Include the brace in the literal string.
707 ++p;
708 escaped_brace = TRUE;
709 }
710 else if (*p == '}')
711 {
712 semsg(_(e_stray_closing_curly_str), str);
713 ga_clear(&ga);
714 return NULL;
715 }
716
717 // Append the literal part.
718 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
719
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100720 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100721 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722
LemonBoy2eaef102022-05-06 13:14:50 +0100723 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100724 {
LemonBoy2eaef102022-05-06 13:14:50 +0100725 // Skip the second brace.
726 ++p;
727 continue;
728 }
729
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100730 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100731 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100732 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100733 {
734 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100735 return NULL;
736 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100737 }
738 ga_append(&ga, NUL);
739
740 return ga.ga_data;
741}
742
743/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200744 * Get a list of lines from a HERE document. The here document is a list of
745 * lines surrounded by a marker.
746 * cmd << {marker}
747 * {line1}
748 * {line2}
749 * ....
750 * {marker}
751 *
752 * The {marker} is a string. If the optional 'trim' word is supplied before the
753 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100754 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200755 *
756 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100757 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200758 * missing, then '.' is accepted as a marker.
759 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100760 * When compiling a heredoc assignment to a variable in a Vim9 def function,
761 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
762 * string values from the heredoc, vim9 instructions are generated. On success
763 * the returned list will be empty.
764 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100765 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100768heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200769{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100770 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771 char_u *marker;
772 list_T *l;
773 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100774 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 int marker_indent_len = 0;
776 int text_indent_len = 0;
777 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200778 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200779 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100780 int evalstr = FALSE;
781 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100782 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
783 int count = 0;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200784 int heredoc_in_string = FALSE;
785 char_u *line_arg = NULL;
zeertzjq1f5175d2024-04-13 17:52:26 +0200786 char_u *nl_ptr = vim_strchr(cmd, '\n');
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200787
zeertzjq1f5175d2024-04-13 17:52:26 +0200788 if (nl_ptr != NULL)
789 {
790 heredoc_in_string = TRUE;
791 line_arg = nl_ptr + 1;
792 *nl_ptr = NUL;
793 }
794 else if (eap->ea_getline == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200795 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000796 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200797 return NULL;
798 }
799
800 // Check for the optional 'trim' word before the marker
801 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200802
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100803 while (TRUE)
804 {
805 if (STRNCMP(cmd, "trim", 4) == 0
806 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200807 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100808 cmd = skipwhite(cmd + 4);
809
810 // Trim the indentation from all the lines in the here document.
811 // The amount of indentation trimmed is the same as the indentation
812 // of the first line after the :let command line. To find the end
813 // marker the indent of the :let command line is trimmed.
814 p = *eap->cmdlinep;
815 while (VIM_ISWHITE(*p))
816 {
817 p++;
818 marker_indent_len++;
819 }
820 text_indent_len = -1;
821
822 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200823 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100824 if (STRNCMP(cmd, "eval", 4) == 0
825 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
826 {
827 cmd = skipwhite(cmd + 4);
828 evalstr = TRUE;
829 continue;
830 }
831 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832 }
833
834 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200835 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 {
837 marker = skipwhite(cmd);
zeertzjq1f5175d2024-04-13 17:52:26 +0200838 p = skiptowhite(marker);
839 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000841 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200842 return NULL;
843 }
844 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200845 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000847 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200848 return NULL;
849 }
850 }
851 else
852 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200853 // When getting lines for an embedded script, if the marker is missing,
854 // accept '.' as the marker.
855 if (script_get)
856 marker = dot;
857 else
858 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000859 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200860 return NULL;
861 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200862 }
863
864 l = list_alloc();
865 if (l == NULL)
866 return NULL;
867
868 for (;;)
869 {
870 int mi = 0;
871 int ti = 0;
872
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200873 if (heredoc_in_string)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200874 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200875 char_u *next_line;
876
877 // heredoc in a string separated by newlines. Get the next line
878 // from the string.
879
880 if (*line_arg == NUL)
881 {
882 semsg(_(e_missing_end_marker_str), marker);
883 break;
884 }
885
886 theline = line_arg;
887 next_line = vim_strchr(theline, '\n');
888 if (next_line == NULL)
889 line_arg += STRLEN(line_arg);
890 else
891 {
892 *next_line = NUL;
893 line_arg = next_line + 1;
894 }
895 }
896 else
897 {
898 vim_free(theline);
899 theline = eap->ea_getline(NUL, eap->cookie, 0, FALSE);
900 if (theline == NULL)
901 {
902 semsg(_(e_missing_end_marker_str), marker);
903 break;
904 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200905 }
906
907 // with "trim": skip the indent matching the :let line to find the
908 // marker
909 if (marker_indent_len > 0
910 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
911 mi = marker_indent_len;
912 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200913 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100915 // If expression evaluation failed in the heredoc, then skip till the
916 // end marker.
917 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100918 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100919
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200920 if (text_indent_len == -1 && *theline != NUL)
921 {
922 // set the text indent from the first line.
923 p = theline;
924 text_indent_len = 0;
925 while (VIM_ISWHITE(*p))
926 {
927 p++;
928 text_indent_len++;
929 }
930 text_indent = vim_strnsave(theline, text_indent_len);
931 }
932 // with "trim": skip the indent matching the first line
933 if (text_indent != NULL)
934 for (ti = 0; ti < text_indent_len; ++ti)
935 if (theline[ti] != text_indent[ti])
936 break;
937
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100938 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100939 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100940 {
LemonBoy2eaef102022-05-06 13:14:50 +0100941 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100942 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100943 vim_free(theline);
944 vim_free(text_indent);
945 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100946 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100947 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100948 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100949 else
950 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200951 int free_str = FALSE;
952
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100953 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100954 {
955 str = eval_all_expr_in_str(str);
956 if (str == NULL)
957 {
958 // expression evaluation failed
959 eval_failed = TRUE;
960 continue;
961 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200962 free_str = TRUE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100963 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100964
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100965 if (list_append_string(l, str, -1) == FAIL)
966 break;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200967 if (free_str)
968 vim_free(str);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100969 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200970 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200971 if (heredoc_in_string)
972 // Next command follows the heredoc in the string.
973 eap->nextcmd = line_arg;
974 else
975 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200976 vim_free(text_indent);
977
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100978 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
979 generate_NEWLIST(cctx, count, FALSE);
980
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100981 if (eval_failed)
982 {
983 // expression evaluation in the heredoc failed
984 list_free(l);
985 return NULL;
986 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987 return l;
988}
989
990/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200991 * Vim9 variable declaration:
992 * ":var name"
993 * ":var name: type"
994 * ":var name = expr"
995 * ":var name: type = expr"
996 * etc.
997 */
998 void
999ex_var(exarg_T *eap)
1000{
Bram Moolenaar330a3882022-03-05 11:05:57 +00001001 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +00001002 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +00001003
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001004 if (!in_vim9script())
1005 {
1006 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
1007 return;
1008 }
Bram Moolenaare1d12112022-03-05 11:37:48 +00001009 has_var = checkforcmd_noparen(&p, "var", 3);
1010 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00001011 {
1012 emsg(_(e_cannot_declare_variable_on_command_line));
1013 return;
1014 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001015 ex_let(eap);
1016}
1017
1018/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001019 * ":let" list all variable values
1020 * ":let var1 var2" list variable values
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 var .= expr" assignment command.
1028 * ":let var ..= expr" assignment command.
1029 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +01001031 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001032 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001033 * ":final var = expr" assignment command.
1034 * ":final [var1, var2] = expr" unpack list.
1035 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001036 * ":const" list all variable values
1037 * ":const var1 var2" list variable values
1038 * ":const var = expr" assignment command.
1039 * ":const [var1, var2] = expr" unpack list.
1040 */
1041 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001042ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001043{
1044 char_u *arg = eap->arg;
1045 char_u *expr = NULL;
1046 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 int var_count = 0;
1048 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001049 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001050 char_u *argend;
1051 int first = TRUE;
1052 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001053 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001054 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001055 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001056
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001057 if (eap->cmdidx == CMD_final && !vim9script)
1058 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001059 // In legacy Vim script ":final" is short for ":finally".
1060 ex_finally(eap);
1061 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001062 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001063 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001064 {
1065 emsg(_(e_cannot_use_let_in_vim9_script));
1066 return;
1067 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001068
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001069 if (eap->cmdidx == CMD_const)
1070 flags |= ASSIGN_CONST;
1071 else if (eap->cmdidx == CMD_final)
1072 flags |= ASSIGN_FINAL;
1073
1074 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001076 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001078 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079 if (argend == NULL)
1080 return;
1081 if (argend > arg && argend[-1] == '.') // for var.='str'
1082 --argend;
1083 expr = skipwhite(argend);
1084 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001085 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001087 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1088 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001089 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001090 {
1091 // ":let" without "=": list variables
1092 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001093 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001094 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001095 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001096 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001097 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001098 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001099 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001100 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001101 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001102 else
1103 // Vim9 declaration ":var name: type"
1104 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001105 }
1106 else
1107 {
1108 // ":let var1 var2" - list values
1109 arg = list_arg_vars(eap, arg, &first);
1110 }
1111 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001112 else if (!eap->skip)
1113 {
1114 // ":let"
1115 list_glob_vars(&first);
1116 list_buf_vars(&first);
1117 list_win_vars(&first);
1118 list_tab_vars(&first);
1119 list_script_vars(&first);
1120 list_func_vars(&first);
1121 list_vim_vars(&first);
1122 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001123 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001124 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001125 }
zeertzjq474891b2023-04-12 21:36:03 +01001126
1127 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001128 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001129 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001130 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001131
Bram Moolenaard9876422022-10-12 12:58:54 +01001132 // :let text =<< [trim] [eval] END
1133 // :var text =<< [trim] [eval] END
1134 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1135 || !IS_WHITE_OR_NUL(expr[3])))
1136 semsg(_(e_white_space_required_before_and_after_str_at_str),
1137 "=<<", expr);
1138 else
1139 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1140
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001141 if (l != NULL)
1142 {
1143 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001144 if (!eap->skip)
1145 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001146 // errors are for the assignment, not the end marker
1147 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001148 op[0] = '=';
1149 op[1] = NUL;
1150 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001152 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001153 clear_tv(&rettv);
1154 }
zeertzjq474891b2023-04-12 21:36:03 +01001155 return;
1156 }
1157
1158 evalarg_T evalarg;
1159 int len = 1;
1160
1161 CLEAR_FIELD(rettv);
1162
1163 int cur_lnum;
1164
1165 op[0] = '=';
1166 op[1] = NUL;
1167 if (*expr != '=')
1168 {
1169 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1170 {
1171 // +=, /=, etc. require an existing variable
1172 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1173 }
1174 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1175 {
1176 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1177 ++len;
1178 if (expr[0] == '.' && expr[1] == '.') // ..=
1179 {
1180 ++expr;
1181 ++len;
1182 }
1183 }
1184 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001185 }
1186 else
zeertzjq474891b2023-04-12 21:36:03 +01001187 ++expr;
1188
1189 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1190 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001191 {
zeertzjq474891b2023-04-12 21:36:03 +01001192 vim_strncpy(op, expr - len, len);
1193 semsg(_(e_white_space_required_before_and_after_str_at_str),
1194 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001195 }
zeertzjq474891b2023-04-12 21:36:03 +01001196
1197 if (eap->skip)
1198 ++emsg_skip;
1199 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1200 expr = skipwhite_and_linebreak(expr, &evalarg);
1201 cur_lnum = SOURCING_LNUM;
1202 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1203 if (eap->skip)
1204 --emsg_skip;
1205 clear_evalarg(&evalarg, eap);
1206
1207 // Restore the line number so that any type error is given for the
1208 // declaration, not the expression.
1209 SOURCING_LNUM = cur_lnum;
1210
1211 if (!eap->skip && eval_res != FAIL)
1212 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1213 flags, op);
1214 if (eval_res != FAIL)
1215 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001216}
1217
1218/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001219 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001220 * Handles both "var" with any type and "[var, var; var]" with a list type.
1221 * When "op" is not NULL it points to a string with characters that
1222 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1223 * or concatenate.
1224 * Returns OK or FAIL;
1225 */
1226 int
1227ex_let_vars(
1228 char_u *arg_start,
1229 typval_T *tv,
1230 int copy, // copy values from "tv", don't move
1231 int semicolon, // from skip_var_list()
1232 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001233 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001234 char_u *op)
1235{
1236 char_u *arg = arg_start;
1237 list_T *l;
1238 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001239 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001240 listitem_T *item;
1241 typval_T ltv;
1242
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001243 if (tv->v_type == VAR_VOID)
1244 {
1245 emsg(_(e_cannot_use_void_value));
1246 return FAIL;
1247 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001248 if (*arg != '[')
1249 {
1250 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001251 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001252 return FAIL;
1253 return OK;
1254 }
1255
1256 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1257 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1258 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001259 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260 return FAIL;
1261 }
1262
1263 i = list_len(l);
1264 if (semicolon == 0 && var_count < i)
1265 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001266 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001267 return FAIL;
1268 }
1269 if (var_count - semicolon > i)
1270 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001271 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001272 return FAIL;
1273 }
1274
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001275 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001276 item = l->lv_first;
1277 while (*arg != ']')
1278 {
1279 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001280 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001281 arg = ex_let_one(arg, &item->li_tv, TRUE,
1282 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001283 item = item->li_next;
1284 if (arg == NULL)
1285 return FAIL;
1286
1287 arg = skipwhite(arg);
1288 if (*arg == ';')
1289 {
1290 // Put the rest of the list (may be empty) in the var after ';'.
1291 // Create a new list for this.
1292 l = list_alloc();
1293 if (l == NULL)
1294 return FAIL;
1295 while (item != NULL)
1296 {
1297 list_append_tv(l, &item->li_tv);
1298 item = item->li_next;
1299 }
1300
1301 ltv.v_type = VAR_LIST;
1302 ltv.v_lock = 0;
1303 ltv.vval.v_list = l;
1304 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001305 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001307 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1308 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001309 clear_tv(&ltv);
1310 if (arg == NULL)
1311 return FAIL;
1312 break;
1313 }
1314 else if (*arg != ',' && *arg != ']')
1315 {
1316 internal_error("ex_let_vars()");
1317 return FAIL;
1318 }
1319 }
1320
1321 return OK;
1322}
1323
1324/*
1325 * Skip over assignable variable "var" or list of variables "[var, var]".
1326 * Used for ":let varvar = expr" and ":for varvar in expr".
1327 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001328 * for "[var, var; var]" set "semicolon" to 1.
1329 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001330 * Return NULL for an error.
1331 */
1332 char_u *
1333skip_var_list(
1334 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001337 int *semicolon,
1338 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001339{
1340 char_u *p, *s;
1341
1342 if (*arg == '[')
1343 {
1344 // "[var, var]": find the matching ']'.
1345 p = arg;
1346 for (;;)
1347 {
1348 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001349 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001350 if (s == p)
1351 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001352 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001353 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001354 return NULL;
1355 }
1356 ++*var_count;
1357
1358 p = skipwhite(s);
1359 if (*p == ']')
1360 break;
1361 else if (*p == ';')
1362 {
1363 if (*semicolon == 1)
1364 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001365 if (!silent)
1366 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 return NULL;
1368 }
1369 *semicolon = 1;
1370 }
1371 else if (*p != ',')
1372 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001373 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001374 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375 return NULL;
1376 }
1377 }
1378 return p + 1;
1379 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001380
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001381 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001382}
1383
1384/*
1385 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1386 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001388 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001389 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001391{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001392 char_u *end;
1393 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001395 if (*arg == '@' && arg[1] != NUL)
1396 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001397
1398 // termcap option name may have non-alpha characters
1399 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1400 return arg + 5;
1401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001404
1405 // "a: type" is declaring variable "a" with a type, not "a:".
1406 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001407 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001408 --end;
1409
Bram Moolenaar585587d2021-01-17 20:52:13 +01001410 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001412 if (*skipwhite(end) == ':')
1413 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001414 }
1415 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001416}
1417
1418/*
1419 * List variables for hashtab "ht" with prefix "prefix".
1420 * If "empty" is TRUE also list NULL strings as empty strings.
1421 */
1422 void
1423list_hashtable_vars(
1424 hashtab_T *ht,
1425 char *prefix,
1426 int empty,
1427 int *first)
1428{
1429 hashitem_T *hi;
1430 dictitem_T *di;
1431 int todo;
1432 char_u buf[IOSIZE];
1433
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001434 int save_ht_flags = ht->ht_flags;
1435 ht->ht_flags |= HTFLAGS_FROZEN;
1436
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001437 todo = (int)ht->ht_used;
1438 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1439 {
1440 if (!HASHITEM_EMPTY(hi))
1441 {
1442 --todo;
1443 di = HI2DI(hi);
1444
1445 // apply :filter /pat/ to variable name
1446 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1447 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1448 if (message_filtered(buf))
1449 continue;
1450
1451 if (empty || di->di_tv.v_type != VAR_STRING
1452 || di->di_tv.vval.v_string != NULL)
1453 list_one_var(di, prefix, first);
1454 }
1455 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001456
1457 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001458}
1459
1460/*
1461 * List global variables.
1462 */
1463 static void
1464list_glob_vars(int *first)
1465{
1466 list_hashtable_vars(&globvarht, "", TRUE, first);
1467}
1468
1469/*
1470 * List buffer variables.
1471 */
1472 static void
1473list_buf_vars(int *first)
1474{
1475 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1476}
1477
1478/*
1479 * List window variables.
1480 */
1481 static void
1482list_win_vars(int *first)
1483{
1484 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1485}
1486
1487/*
1488 * List tab page variables.
1489 */
1490 static void
1491list_tab_vars(int *first)
1492{
1493 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1494}
1495
1496/*
1497 * List variables in "arg".
1498 */
1499 static char_u *
1500list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1501{
1502 int error = FALSE;
1503 int len;
1504 char_u *name;
1505 char_u *name_start;
1506 char_u *arg_subsc;
1507 char_u *tofree;
1508 typval_T tv;
1509
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001510 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001511 {
1512 if (error || eap->skip)
1513 {
1514 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1515 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1516 {
1517 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001518 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001519 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001520 break;
1521 }
1522 }
1523 else
1524 {
1525 // get_name_len() takes care of expanding curly braces
1526 name_start = name = arg;
1527 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1528 if (len <= 0)
1529 {
1530 // This is mainly to keep test 49 working: when expanding
1531 // curly braces fails overrule the exception error message.
1532 if (len < 0 && !aborting())
1533 {
1534 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001535 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001536 break;
1537 }
1538 error = TRUE;
1539 }
1540 else
1541 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001542 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 if (tofree != NULL)
1544 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001545 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001546 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001547 error = TRUE;
1548 else
1549 {
1550 // handle d.key, l[idx], f(expr)
1551 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001552 if (handle_subscript(&arg, name_start, &tv,
1553 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554 error = TRUE;
1555 else
1556 {
1557 if (arg == arg_subsc && len == 2 && name[1] == ':')
1558 {
1559 switch (*name)
1560 {
1561 case 'g': list_glob_vars(first); break;
1562 case 'b': list_buf_vars(first); break;
1563 case 'w': list_win_vars(first); break;
1564 case 't': list_tab_vars(first); break;
1565 case 'v': list_vim_vars(first); break;
1566 case 's': list_script_vars(first); break;
1567 case 'l': list_func_vars(first); break;
1568 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001569 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001570 }
1571 }
1572 else
1573 {
1574 char_u numbuf[NUMBUFLEN];
1575 char_u *tf;
1576 int c;
1577 char_u *s;
1578
1579 s = echo_string(&tv, &tf, numbuf, 0);
1580 c = *arg;
1581 *arg = NUL;
1582 list_one_var_a("",
1583 arg == arg_subsc ? name : name_start,
1584 tv.v_type,
1585 s == NULL ? (char_u *)"" : s,
1586 first);
1587 *arg = c;
1588 vim_free(tf);
1589 }
1590 clear_tv(&tv);
1591 }
1592 }
1593 }
1594
1595 vim_free(tofree);
1596 }
1597
1598 arg = skipwhite(arg);
1599 }
1600
1601 return arg;
1602}
1603
1604/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001605 * Set an environment variable, part of ex_let_one().
1606 */
1607 static char_u *
1608ex_let_env(
1609 char_u *arg,
1610 typval_T *tv,
1611 int flags,
1612 char_u *endchars,
1613 char_u *op)
1614{
1615 char_u *arg_end = NULL;
1616 char_u *name;
1617 int len;
1618
1619 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1620 && (flags & ASSIGN_FOR_LOOP) == 0)
1621 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001622 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001623 return NULL;
1624 }
1625
1626 // Find the end of the name.
1627 ++arg;
1628 name = arg;
1629 len = get_env_len(&arg);
1630 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001631 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001632 else
1633 {
1634 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001635 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001636 else if (endchars != NULL
1637 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1638 emsg(_(e_unexpected_characters_in_let));
1639 else if (!check_secure())
1640 {
1641 char_u *tofree = NULL;
1642 int c1 = name[len];
1643 char_u *p;
1644
1645 name[len] = NUL;
1646 p = tv_get_string_chk(tv);
1647 if (p != NULL && op != NULL && *op == '.')
1648 {
1649 int mustfree = FALSE;
1650 char_u *s = vim_getenv(name, &mustfree);
1651
1652 if (s != NULL)
1653 {
1654 p = tofree = concat_str(s, p);
1655 if (mustfree)
1656 vim_free(s);
1657 }
1658 }
1659 if (p != NULL)
1660 {
1661 vim_setenv_ext(name, p);
1662 arg_end = arg;
1663 }
1664 name[len] = c1;
1665 vim_free(tofree);
1666 }
1667 }
1668 return arg_end;
1669}
1670
1671/*
1672 * Set an option, part of ex_let_one().
1673 */
1674 static char_u *
1675ex_let_option(
1676 char_u *arg,
1677 typval_T *tv,
1678 int flags,
1679 char_u *endchars,
1680 char_u *op)
1681{
1682 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001683 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001684 char_u *arg_end = NULL;
1685
1686 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1687 && (flags & ASSIGN_FOR_LOOP) == 0)
1688 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001689 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001690 return NULL;
1691 }
1692
1693 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001694 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001695 if (p == NULL || (endchars != NULL
1696 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001697 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001698 emsg(_(e_unexpected_characters_in_let));
1699 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001700 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001701
1702 int c1;
1703 long n = 0;
1704 getoption_T opt_type;
1705 long numval;
1706 char_u *stringval = NULL;
1707 char_u *s = NULL;
1708 int failed = FALSE;
1709 int opt_p_flags;
1710 char_u *tofree = NULL;
1711 char_u numbuf[NUMBUFLEN];
1712
1713 c1 = *p;
1714 *p = NUL;
1715
1716 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1717 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1718 {
1719 semsg(_(e_unknown_option_str_2), arg);
1720 goto theend;
1721 }
1722 if (op != NULL && *op != '='
1723 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1724 || (opt_type == gov_string && *op != '.')))
1725 {
1726 semsg(_(e_wrong_variable_type_for_str_equal), op);
1727 goto theend;
1728 }
1729
1730 if ((opt_type == gov_bool
1731 || opt_type == gov_number
1732 || opt_type == gov_hidden_bool
1733 || opt_type == gov_hidden_number)
1734 && (tv->v_type != VAR_STRING || !in_vim9script()))
1735 {
1736 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1737 // bool, possibly hidden
1738 n = (long)tv_get_bool_chk(tv, &failed);
1739 else
1740 // number, possibly hidden
1741 n = (long)tv_get_number_chk(tv, &failed);
1742 if (failed)
1743 goto theend;
1744 }
1745
1746 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1747 || tv->v_type == VAR_FUNC))
1748 {
1749 // If the option can be set to a function reference or a lambda
1750 // and the passed value is a function reference, then convert it to
1751 // the name (string) of the function reference.
1752 s = tv2string(tv, &tofree, numbuf, 0);
1753 if (s == NULL)
1754 goto theend;
1755 }
1756 // Avoid setting a string option to the text "v:false" or similar.
1757 // In Vim9 script also don't convert a number to string.
1758 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1759 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1760 {
1761 s = tv_get_string_chk(tv);
1762 if (s == NULL)
1763 goto theend;
1764 }
1765 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1766 {
1767 emsg(_(e_string_required));
1768 goto theend;
1769 }
1770
1771 if (op != NULL && *op != '=')
1772 {
1773 // number, in legacy script also bool
1774 if (opt_type == gov_number
1775 || (opt_type == gov_bool && !in_vim9script()))
1776 {
1777 switch (*op)
1778 {
1779 case '+': n = numval + n; break;
1780 case '-': n = numval - n; break;
1781 case '*': n = numval * n; break;
1782 case '/': n = (long)num_divide(numval, n, &failed); break;
1783 case '%': n = (long)num_modulus(numval, n, &failed); break;
1784 }
1785 s = NULL;
1786 if (failed)
1787 goto theend;
1788 }
1789 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1790 {
1791 // string
1792 s = concat_str(stringval, s);
1793 vim_free(stringval);
1794 stringval = s;
1795 }
1796 }
1797
1798 char *err = set_option_value(arg, n, s, scope);
1799 arg_end = p;
1800 if (err != NULL)
1801 emsg(_(err));
1802
1803theend:
1804 *p = c1;
1805 vim_free(stringval);
1806 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001807 return arg_end;
1808}
1809
1810/*
1811 * Set a register, part of ex_let_one().
1812 */
1813 static char_u *
1814ex_let_register(
1815 char_u *arg,
1816 typval_T *tv,
1817 int flags,
1818 char_u *endchars,
1819 char_u *op)
1820{
1821 char_u *arg_end = NULL;
1822
1823 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1824 && (flags & ASSIGN_FOR_LOOP) == 0)
1825 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001826 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001827 return NULL;
1828 }
1829 ++arg;
1830 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001831 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001832 else if (endchars != NULL
1833 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1834 emsg(_(e_unexpected_characters_in_let));
1835 else
1836 {
1837 char_u *ptofree = NULL;
1838 char_u *p;
1839
1840 p = tv_get_string_chk(tv);
1841 if (p != NULL && op != NULL && *op == '.')
1842 {
1843 char_u *s = get_reg_contents(*arg == '@'
1844 ? '"' : *arg, GREG_EXPR_SRC);
1845
1846 if (s != NULL)
1847 {
1848 p = ptofree = concat_str(s, p);
1849 vim_free(s);
1850 }
1851 }
1852 if (p != NULL)
1853 {
1854 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1855 arg_end = arg + 1;
1856 }
1857 vim_free(ptofree);
1858 }
1859 return arg_end;
1860}
1861
1862/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1864 * Returns a pointer to the char just after the var name.
1865 * Returns NULL if there is an error.
1866 */
1867 static char_u *
1868ex_let_one(
1869 char_u *arg, // points to variable name
1870 typval_T *tv, // value to assign to variable
1871 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001872 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001873 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001874 char_u *op, // "+", "-", "." or NULL
1875 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001877 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001879 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001880 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001881 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1882 {
1883 vim9_declare_error(arg);
1884 return NULL;
1885 }
1886
Ernie Rael9ed53752023-12-11 17:40:46 +01001887 if (check_typval_is_value(tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001888 return NULL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001889
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001890 if (*arg == '$')
1891 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001892 // ":let $VAR = expr": Set environment variable.
1893 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001894 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895 else if (*arg == '&')
1896 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001897 // ":let &option = expr": Set option value.
1898 // ":let &l:option = expr": Set local option value.
1899 // ":let &g:option = expr": Set global option value.
1900 // ":for &ts in range(8)": Set option value for for loop
1901 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001902 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001903 else if (*arg == '@')
1904 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001905 // ":let @r = expr": Set register contents.
1906 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001907 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001908 else if (eval_isnamec1(*arg) || *arg == '{')
1909 {
1910 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001911 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001912 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1913 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001914 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001915 if (op != NULL && *op != '=')
1916 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001917
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001918 // ":let var = expr": Set internal variable.
1919 // ":let var: type = expr": Set internal variable with type.
1920 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001921 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001922 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001923 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 if (endchars != NULL && vim_strchr(endchars,
1925 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001926 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001927 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001928 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001929 else
1930 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001931 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001932 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001933 }
1934 }
1935 clear_lval(&lv);
1936 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001938 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939
1940 return arg_end;
1941}
1942
1943/*
1944 * ":unlet[!] var1 ... " command.
1945 */
1946 void
1947ex_unlet(exarg_T *eap)
1948{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001949 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001950}
1951
1952/*
1953 * ":lockvar" and ":unlockvar" commands
1954 */
1955 void
1956ex_lockvar(exarg_T *eap)
1957{
1958 char_u *arg = eap->arg;
1959 int deep = 2;
1960
1961 if (eap->forceit)
1962 deep = -1;
1963 else if (vim_isdigit(*arg))
1964 {
1965 deep = getdigits(&arg);
1966 arg = skipwhite(arg);
1967 }
1968
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001969 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001970}
1971
1972/*
1973 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001974 * Also used for Vim9 script. "callback" is invoked as:
1975 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001976 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001977 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001978ex_unletlock(
1979 exarg_T *eap,
1980 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001981 int deep,
1982 int glv_flags,
1983 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1984 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001985{
1986 char_u *arg = argstart;
1987 char_u *name_end;
1988 int error = FALSE;
1989 lval_T lv;
1990
1991 do
1992 {
1993 if (*arg == '$')
1994 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001995 lv.ll_name = arg;
1996 lv.ll_tv = NULL;
1997 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001998 if (get_env_len(&arg) == 0)
1999 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00002000 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002001 return;
2002 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002003 if (!error && !eap->skip
2004 && callback(&lv, arg, eap, deep, cookie) == FAIL)
2005 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002006 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002007 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002008 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002009 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002010 // Parse the name and find the end.
2011 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01002012 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002013 if (lv.ll_name == NULL)
2014 error = TRUE; // error but continue parsing
2015 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
2016 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002017 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002018 if (name_end != NULL)
2019 {
2020 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00002021 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002022 }
2023 if (!(eap->skip || error))
2024 clear_lval(&lv);
2025 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002026 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002027
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002028 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002029 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002030 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002031
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002032 if (!eap->skip)
2033 clear_lval(&lv);
2034 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002035
2036 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002037 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002038
Bram Moolenaar63b91732021-08-05 20:40:03 +02002039 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002040}
2041
2042 static int
2043do_unlet_var(
2044 lval_T *lp,
2045 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002046 exarg_T *eap,
2047 int deep UNUSED,
2048 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002049{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002050 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002051 int ret = OK;
2052 int cc;
2053
2054 if (lp->ll_tv == NULL)
2055 {
2056 cc = *name_end;
2057 *name_end = NUL;
2058
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002059 // Environment variable, normal name or expanded name.
2060 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002061 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002062 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002063 ret = FAIL;
2064 *name_end = cc;
2065 }
2066 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002067 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002068 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002069 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002070 return FAIL;
2071 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002072 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2073 !lp->ll_empty2, lp->ll_n2);
2074 else if (lp->ll_list != NULL)
2075 // unlet a List item.
2076 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002077 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002078 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002079 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002080
2081 return ret;
2082}
2083
2084/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002085 * Unlet one item or a range of items from a list.
2086 * Return OK or FAIL.
2087 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002088 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002089list_unlet_range(
2090 list_T *l,
2091 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002092 long n1_arg,
2093 int has_n2,
2094 long n2)
2095{
2096 listitem_T *li = li_first;
2097 int n1 = n1_arg;
2098
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002099 // Delete a range of List items.
2100 li = li_first;
2101 n1 = n1_arg;
2102 while (li != NULL && (!has_n2 || n2 >= n1))
2103 {
2104 listitem_T *next = li->li_next;
2105
2106 listitem_remove(l, li);
2107 li = next;
2108 ++n1;
2109 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002110}
2111/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002112 * "unlet" a variable. Return OK if it existed, FAIL if not.
2113 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2114 */
2115 int
2116do_unlet(char_u *name, int forceit)
2117{
2118 hashtab_T *ht;
2119 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002120 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002121 dict_T *d;
2122 dictitem_T *di;
2123
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002124 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002125 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002126 return FAIL;
2127
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002128 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002129
2130 // can't :unlet a script variable in Vim9 script from a function
2131 if (ht == get_script_local_ht()
2132 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2133 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2134 == SCRIPT_VERSION_VIM9
2135 && check_vim9_unlet(name) == FAIL)
2136 return FAIL;
2137
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002138 if (ht != NULL && *varname != NUL)
2139 {
2140 d = get_current_funccal_dict(ht);
2141 if (d == NULL)
2142 {
2143 if (ht == &globvarht)
2144 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002145 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002146 d = &vimvardict;
2147 else
2148 {
2149 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2150 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2151 }
2152 if (d == NULL)
2153 {
2154 internal_error("do_unlet()");
2155 return FAIL;
2156 }
2157 }
2158 hi = hash_find(ht, varname);
2159 if (HASHITEM_EMPTY(hi))
2160 hi = find_hi_in_scoped_ht(name, &ht);
2161 if (hi != NULL && !HASHITEM_EMPTY(hi))
2162 {
2163 di = HI2DI(hi);
2164 if (var_check_fixed(di->di_flags, name, FALSE)
2165 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002166 || value_check_lock(d->dv_lock, name, FALSE)
2167 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002168 return FAIL;
2169
2170 delete_var(ht, hi);
2171 return OK;
2172 }
2173 }
2174 if (forceit)
2175 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002176 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002177 return FAIL;
2178}
2179
Ernie Raelee865f32023-09-29 19:53:55 +02002180 static void
2181report_lockvar_member(char *msg, lval_T *lp)
2182{
2183 int did_alloc = FALSE;
2184 char_u *vname = (char_u *)"";
2185 char_u *class_name = lp->ll_class != NULL
2186 ? lp->ll_class->class_name : (char_u *)"";
2187 if (lp->ll_name != NULL)
2188 {
2189 if (lp->ll_name_end == NULL)
2190 vname = lp->ll_name;
2191 else
2192 {
2193 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2194 if (vname == NULL)
2195 return;
2196 did_alloc = TRUE;
2197 }
2198 }
2199 semsg(_(msg), vname, class_name);
2200 if (did_alloc)
2201 vim_free(vname);
2202}
2203
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002204/*
2205 * Lock or unlock variable indicated by "lp".
2206 * "deep" is the levels to go (-1 for unlimited);
2207 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2208 */
2209 static int
2210do_lock_var(
2211 lval_T *lp,
2212 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002213 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002214 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002215 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002216{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002217 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002218 int ret = OK;
2219 int cc;
2220 dictitem_T *di;
2221
Ernie Raelee865f32023-09-29 19:53:55 +02002222#ifdef LOG_LOCKVAR
2223 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2224#endif
2225
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002226 if (lp->ll_tv == NULL)
2227 {
2228 cc = *name_end;
2229 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002230 if (*lp->ll_name == '$')
2231 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002232 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002233 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002234 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002235 else
2236 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002237 // Normal name or expanded name.
2238 di = find_var(lp->ll_name, NULL, TRUE);
2239 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002240 {
2241 if (in_vim9script())
2242 semsg(_(e_cannot_find_variable_to_unlock_str),
2243 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002244 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002245 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002246 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002247 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002248 if ((di->di_flags & DI_FLAGS_FIX)
2249 && di->di_tv.v_type != VAR_DICT
2250 && di->di_tv.v_type != VAR_LIST)
2251 {
2252 // For historic reasons this error is not given for a list
2253 // or dict. E.g., the b: dict could be locked/unlocked.
2254 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2255 ret = FAIL;
2256 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002257 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002258 {
2259 if (in_vim9script())
2260 {
2261 svar_T *sv = find_typval_in_script(&di->di_tv,
2262 0, FALSE);
2263
2264 if (sv != NULL && sv->sv_const != 0)
2265 {
2266 semsg(_(e_cannot_change_readonly_variable_str),
2267 lp->ll_name);
2268 ret = FAIL;
2269 }
2270 }
2271
2272 if (ret == OK)
2273 {
2274 if (lock)
2275 di->di_flags |= DI_FLAGS_LOCK;
2276 else
2277 di->di_flags &= ~DI_FLAGS_LOCK;
2278 if (deep != 0)
2279 item_lock(&di->di_tv, deep, lock, FALSE);
2280 }
2281 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002282 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002283 }
2284 *name_end = cc;
2285 }
Ernie Raelee865f32023-09-29 19:53:55 +02002286 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002287 {
2288 // nothing to do
2289 }
Ernie Raelee865f32023-09-29 19:53:55 +02002290 else if (lp->ll_is_root)
2291 // (un)lock the item.
2292 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002293 else if (lp->ll_range)
2294 {
2295 listitem_T *li = lp->ll_li;
2296
2297 // (un)lock a range of List items.
2298 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2299 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002300 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002301 li = li->li_next;
2302 ++lp->ll_n1;
2303 }
2304 }
2305 else if (lp->ll_list != NULL)
2306 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002307 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002308 else if (lp->ll_object != NULL) // This check must be before ll_class.
2309 {
2310 // (un)lock an object variable.
2311 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2312 ret = FAIL;
2313 }
2314 else if (lp->ll_class != NULL)
2315 {
2316 // (un)lock a class variable.
2317 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2318 ret = FAIL;
2319 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002320 else
Ernie Raelee865f32023-09-29 19:53:55 +02002321 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002322 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002323 if (lp->ll_di == NULL)
2324 {
2325 emsg(_(e_dictionary_required));
2326 ret = FAIL;
2327 }
2328 else
2329 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2330 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002331
2332 return ret;
2333}
2334
2335/*
2336 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002337 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2338 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002339 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002340 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002341item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002342{
2343 static int recurse = 0;
2344 list_T *l;
2345 listitem_T *li;
2346 dict_T *d;
2347 blob_T *b;
2348 hashitem_T *hi;
2349 int todo;
2350
Ernie Raelee865f32023-09-29 19:53:55 +02002351#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002352 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002353#endif
2354
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002355 if (recurse >= DICT_MAXNEST)
2356 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002357 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002358 return;
2359 }
2360 if (deep == 0)
2361 return;
2362 ++recurse;
2363
2364 // lock/unlock the item itself
2365 if (lock)
2366 tv->v_lock |= VAR_LOCKED;
2367 else
2368 tv->v_lock &= ~VAR_LOCKED;
2369
2370 switch (tv->v_type)
2371 {
2372 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002373 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002375 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002377 case VAR_STRING:
2378 case VAR_FUNC:
2379 case VAR_PARTIAL:
2380 case VAR_FLOAT:
2381 case VAR_SPECIAL:
2382 case VAR_JOB:
2383 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002384 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002385 case VAR_CLASS:
2386 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002387 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002388 break;
2389
2390 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002391 if ((b = tv->vval.v_blob) != NULL
2392 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002393 {
2394 if (lock)
2395 b->bv_lock |= VAR_LOCKED;
2396 else
2397 b->bv_lock &= ~VAR_LOCKED;
2398 }
2399 break;
2400 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002401 if ((l = tv->vval.v_list) != NULL
2402 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002403 {
2404 if (lock)
2405 l->lv_lock |= VAR_LOCKED;
2406 else
2407 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002408 if (deep < 0 || deep > 1)
2409 {
2410 if (l->lv_first == &range_list_item)
2411 l->lv_lock |= VAR_ITEMS_LOCKED;
2412 else
2413 {
2414 // recursive: lock/unlock the items the List contains
2415 CHECK_LIST_MATERIALIZE(l);
2416 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2417 deep - 1, lock, check_refcount);
2418 }
2419 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002420 }
2421 break;
2422 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002423 if ((d = tv->vval.v_dict) != NULL
2424 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002425 {
2426 if (lock)
2427 d->dv_lock |= VAR_LOCKED;
2428 else
2429 d->dv_lock &= ~VAR_LOCKED;
2430 if (deep < 0 || deep > 1)
2431 {
2432 // recursive: lock/unlock the items the List contains
2433 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002434 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002435 {
2436 if (!HASHITEM_EMPTY(hi))
2437 {
2438 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002439 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2440 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002441 }
2442 }
2443 }
2444 }
2445 }
2446 --recurse;
2447}
2448
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002449#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2450/*
2451 * Delete all "menutrans_" variables.
2452 */
2453 void
2454del_menutrans_vars(void)
2455{
2456 hashitem_T *hi;
2457 int todo;
2458
2459 hash_lock(&globvarht);
2460 todo = (int)globvarht.ht_used;
2461 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2462 {
2463 if (!HASHITEM_EMPTY(hi))
2464 {
2465 --todo;
2466 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2467 delete_var(&globvarht, hi);
2468 }
2469 }
2470 hash_unlock(&globvarht);
2471}
2472#endif
2473
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002474/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002475 * Local string buffer for the next two functions to store a variable name
2476 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2477 * get_user_var_name().
2478 */
2479
2480static char_u *varnamebuf = NULL;
2481static int varnamebuflen = 0;
2482
2483/*
2484 * Function to concatenate a prefix and a variable name.
2485 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002486 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002487cat_prefix_varname(int prefix, char_u *name)
2488{
2489 int len;
2490
2491 len = (int)STRLEN(name) + 3;
2492 if (len > varnamebuflen)
2493 {
2494 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002495 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002496 varnamebuf = alloc(len);
2497 if (varnamebuf == NULL)
2498 {
2499 varnamebuflen = 0;
2500 return NULL;
2501 }
2502 varnamebuflen = len;
2503 }
2504 *varnamebuf = prefix;
2505 varnamebuf[1] = ':';
2506 STRCPY(varnamebuf + 2, name);
2507 return varnamebuf;
2508}
2509
2510/*
2511 * Function given to ExpandGeneric() to obtain the list of user defined
2512 * (global/buffer/window/built-in) variable names.
2513 */
2514 char_u *
2515get_user_var_name(expand_T *xp, int idx)
2516{
2517 static long_u gdone;
2518 static long_u bdone;
2519 static long_u wdone;
2520 static long_u tdone;
2521 static int vidx;
2522 static hashitem_T *hi;
2523 hashtab_T *ht;
2524
2525 if (idx == 0)
2526 {
2527 gdone = bdone = wdone = vidx = 0;
2528 tdone = 0;
2529 }
2530
2531 // Global variables
2532 if (gdone < globvarht.ht_used)
2533 {
2534 if (gdone++ == 0)
2535 hi = globvarht.ht_array;
2536 else
2537 ++hi;
2538 while (HASHITEM_EMPTY(hi))
2539 ++hi;
2540 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2541 return cat_prefix_varname('g', hi->hi_key);
2542 return hi->hi_key;
2543 }
2544
2545 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002546 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002547 if (bdone < ht->ht_used)
2548 {
2549 if (bdone++ == 0)
2550 hi = ht->ht_array;
2551 else
2552 ++hi;
2553 while (HASHITEM_EMPTY(hi))
2554 ++hi;
2555 return cat_prefix_varname('b', hi->hi_key);
2556 }
2557
2558 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002559 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002560 if (wdone < ht->ht_used)
2561 {
2562 if (wdone++ == 0)
2563 hi = ht->ht_array;
2564 else
2565 ++hi;
2566 while (HASHITEM_EMPTY(hi))
2567 ++hi;
2568 return cat_prefix_varname('w', hi->hi_key);
2569 }
2570
2571 // t: variables
2572 ht = &curtab->tp_vars->dv_hashtab;
2573 if (tdone < ht->ht_used)
2574 {
2575 if (tdone++ == 0)
2576 hi = ht->ht_array;
2577 else
2578 ++hi;
2579 while (HASHITEM_EMPTY(hi))
2580 ++hi;
2581 return cat_prefix_varname('t', hi->hi_key);
2582 }
2583
2584 // v: variables
2585 if (vidx < VV_LEN)
2586 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2587
2588 VIM_CLEAR(varnamebuf);
2589 varnamebuflen = 0;
2590 return NULL;
2591}
2592
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002593 char *
2594get_var_special_name(int nr)
2595{
2596 switch (nr)
2597 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002598 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2599 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002600 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002601 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002602 }
2603 internal_error("get_var_special_name()");
2604 return "42";
2605}
2606
2607/*
2608 * Returns the global variable dictionary
2609 */
2610 dict_T *
2611get_globvar_dict(void)
2612{
2613 return &globvardict;
2614}
2615
2616/*
2617 * Returns the global variable hash table
2618 */
2619 hashtab_T *
2620get_globvar_ht(void)
2621{
2622 return &globvarht;
2623}
2624
2625/*
2626 * Returns the v: variable dictionary
2627 */
2628 dict_T *
2629get_vimvar_dict(void)
2630{
2631 return &vimvardict;
2632}
2633
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002634/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002636 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 */
2638 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002639find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002641 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2642 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
2644 if (di == NULL)
2645 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002646 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2648 return (int)(vv - vimvars);
2649}
2650
2651
2652/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002653 * Set type of v: variable to "type".
2654 */
2655 void
2656set_vim_var_type(int idx, vartype_T type)
2657{
Bram Moolenaard787e402021-12-24 21:36:12 +00002658 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002659}
2660
2661/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002662 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002663 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002664 */
2665 void
2666set_vim_var_nr(int idx, varnumber_T val)
2667{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002668 vimvars[idx].vv_nr = val;
2669}
2670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 char *
2672get_vim_var_name(int idx)
2673{
2674 return vimvars[idx].vv_name;
2675}
2676
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002677/*
2678 * Get typval_T v: variable value.
2679 */
2680 typval_T *
2681get_vim_var_tv(int idx)
2682{
2683 return &vimvars[idx].vv_tv;
2684}
2685
Bram Moolenaard787e402021-12-24 21:36:12 +00002686 type_T *
2687get_vim_var_type(int idx, garray_T *type_list)
2688{
2689 if (vimvars[idx].vv_type != NULL)
2690 return vimvars[idx].vv_type;
2691 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2692}
2693
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002694/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002695 * Set v: variable to "tv". Only accepts the same type.
2696 * Takes over the value of "tv".
2697 */
2698 int
2699set_vim_var_tv(int idx, typval_T *tv)
2700{
Bram Moolenaard787e402021-12-24 21:36:12 +00002701 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002702 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002703 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002704 clear_tv(tv);
2705 return FAIL;
2706 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002707 // VV_RO is also checked when compiling, but let's check here as well.
2708 if (vimvars[idx].vv_flags & VV_RO)
2709 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002710 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002711 return FAIL;
2712 }
2713 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2714 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002715 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002716 return FAIL;
2717 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002718 clear_tv(&vimvars[idx].vv_di.di_tv);
2719 vimvars[idx].vv_di.di_tv = *tv;
2720 return OK;
2721}
2722
2723/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002724 * Get number v: variable value.
2725 */
2726 varnumber_T
2727get_vim_var_nr(int idx)
2728{
2729 return vimvars[idx].vv_nr;
2730}
2731
2732/*
2733 * Get string v: variable value. Uses a static buffer, can only be used once.
2734 * If the String variable has never been set, return an empty string.
2735 * Never returns NULL;
2736 */
2737 char_u *
2738get_vim_var_str(int idx)
2739{
2740 return tv_get_string(&vimvars[idx].vv_tv);
2741}
2742
2743/*
2744 * Get List v: variable value. Caller must take care of reference count when
2745 * needed.
2746 */
2747 list_T *
2748get_vim_var_list(int idx)
2749{
2750 return vimvars[idx].vv_list;
2751}
2752
2753/*
2754 * Get Dict v: variable value. Caller must take care of reference count when
2755 * needed.
2756 */
2757 dict_T *
2758get_vim_var_dict(int idx)
2759{
2760 return vimvars[idx].vv_dict;
2761}
2762
2763/*
2764 * Set v:char to character "c".
2765 */
2766 void
2767set_vim_var_char(int c)
2768{
2769 char_u buf[MB_MAXBYTES + 1];
2770
2771 if (has_mbyte)
2772 buf[(*mb_char2bytes)(c, buf)] = NUL;
2773 else
2774 {
2775 buf[0] = c;
2776 buf[1] = NUL;
2777 }
2778 set_vim_var_string(VV_CHAR, buf, -1);
2779}
2780
2781/*
2782 * Set v:count to "count" and v:count1 to "count1".
2783 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2784 */
2785 void
2786set_vcount(
2787 long count,
2788 long count1,
2789 int set_prevcount)
2790{
2791 if (set_prevcount)
2792 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2793 vimvars[VV_COUNT].vv_nr = count;
2794 vimvars[VV_COUNT1].vv_nr = count1;
2795}
2796
2797/*
2798 * Save variables that might be changed as a side effect. Used when executing
2799 * a timer callback.
2800 */
2801 void
2802save_vimvars(vimvars_save_T *vvsave)
2803{
2804 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2805 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2806 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2807}
2808
2809/*
2810 * Restore variables saved by save_vimvars().
2811 */
2812 void
2813restore_vimvars(vimvars_save_T *vvsave)
2814{
2815 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2816 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2817 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2818}
2819
2820/*
2821 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2822 * value.
2823 */
2824 void
2825set_vim_var_string(
2826 int idx,
2827 char_u *val,
2828 int len) // length of "val" to use or -1 (whole string)
2829{
2830 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002831 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002832 if (val == NULL)
2833 vimvars[idx].vv_str = NULL;
2834 else if (len == -1)
2835 vimvars[idx].vv_str = vim_strsave(val);
2836 else
2837 vimvars[idx].vv_str = vim_strnsave(val, len);
2838}
2839
2840/*
2841 * Set List v: variable to "val".
2842 */
2843 void
2844set_vim_var_list(int idx, list_T *val)
2845{
2846 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002847 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002848 vimvars[idx].vv_list = val;
2849 if (val != NULL)
2850 ++val->lv_refcount;
2851}
2852
2853/*
2854 * Set Dictionary v: variable to "val".
2855 */
2856 void
2857set_vim_var_dict(int idx, dict_T *val)
2858{
2859 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002860 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002861 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002862 if (val == NULL)
2863 return;
2864
2865 ++val->dv_refcount;
2866 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002867}
2868
2869/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002870 * Set the v:argv list.
2871 */
2872 void
2873set_argv_var(char **argv, int argc)
2874{
2875 list_T *l = list_alloc();
2876 int i;
2877
2878 if (l == NULL)
2879 getout(1);
2880 l->lv_lock = VAR_FIXED;
2881 for (i = 0; i < argc; ++i)
2882 {
2883 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2884 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002885 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002886 }
2887 set_vim_var_list(VV_ARGV, l);
2888}
2889
2890/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002891 * Reset v:register, taking the 'clipboard' setting into account.
2892 */
2893 void
2894reset_reg_var(void)
2895{
2896 int regname = 0;
2897
2898 // Adjust the register according to 'clipboard', so that when
2899 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2900#ifdef FEAT_CLIPBOARD
2901 adjust_clip_reg(&regname);
2902#endif
2903 set_reg_var(regname);
2904}
2905
2906/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002907 * Set v:register if needed.
2908 */
2909 void
2910set_reg_var(int c)
2911{
2912 char_u regname;
2913
2914 if (c == 0 || c == ' ')
2915 regname = '"';
2916 else
2917 regname = c;
2918 // Avoid free/alloc when the value is already right.
2919 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2920 set_vim_var_string(VV_REG, &regname, 1);
2921}
2922
2923/*
2924 * Get or set v:exception. If "oldval" == NULL, return the current value.
2925 * Otherwise, restore the value to "oldval" and return NULL.
2926 * Must always be called in pairs to save and restore v:exception! Does not
2927 * take care of memory allocations.
2928 */
2929 char_u *
2930v_exception(char_u *oldval)
2931{
2932 if (oldval == NULL)
2933 return vimvars[VV_EXCEPTION].vv_str;
2934
2935 vimvars[VV_EXCEPTION].vv_str = oldval;
2936 return NULL;
2937}
2938
2939/*
2940 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2941 * Otherwise, restore the value to "oldval" and return NULL.
2942 * Must always be called in pairs to save and restore v:throwpoint! Does not
2943 * take care of memory allocations.
2944 */
2945 char_u *
2946v_throwpoint(char_u *oldval)
2947{
2948 if (oldval == NULL)
2949 return vimvars[VV_THROWPOINT].vv_str;
2950
2951 vimvars[VV_THROWPOINT].vv_str = oldval;
2952 return NULL;
2953}
2954
2955/*
2956 * Set v:cmdarg.
2957 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2958 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2959 * Must always be called in pairs!
2960 */
2961 char_u *
2962set_cmdarg(exarg_T *eap, char_u *oldarg)
2963{
2964 char_u *oldval;
2965 char_u *newval;
2966 unsigned len;
2967
2968 oldval = vimvars[VV_CMDARG].vv_str;
2969 if (eap == NULL)
2970 {
2971 vim_free(oldval);
2972 vimvars[VV_CMDARG].vv_str = oldarg;
2973 return NULL;
2974 }
2975
2976 if (eap->force_bin == FORCE_BIN)
2977 len = 6;
2978 else if (eap->force_bin == FORCE_NOBIN)
2979 len = 8;
2980 else
2981 len = 0;
2982
2983 if (eap->read_edit)
2984 len += 7;
2985
2986 if (eap->force_ff != 0)
2987 len += 10; // " ++ff=unix"
2988 if (eap->force_enc != 0)
2989 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2990 if (eap->bad_char != 0)
2991 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2992
2993 newval = alloc(len + 1);
2994 if (newval == NULL)
2995 return NULL;
2996
2997 if (eap->force_bin == FORCE_BIN)
2998 sprintf((char *)newval, " ++bin");
2999 else if (eap->force_bin == FORCE_NOBIN)
3000 sprintf((char *)newval, " ++nobin");
3001 else
3002 *newval = NUL;
3003
3004 if (eap->read_edit)
3005 STRCAT(newval, " ++edit");
3006
3007 if (eap->force_ff != 0)
3008 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
3009 eap->force_ff == 'u' ? "unix"
3010 : eap->force_ff == 'd' ? "dos"
3011 : "mac");
3012 if (eap->force_enc != 0)
3013 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
3014 eap->cmd + eap->force_enc);
3015 if (eap->bad_char == BAD_KEEP)
3016 STRCPY(newval + STRLEN(newval), " ++bad=keep");
3017 else if (eap->bad_char == BAD_DROP)
3018 STRCPY(newval + STRLEN(newval), " ++bad=drop");
3019 else if (eap->bad_char != 0)
3020 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
3021 vimvars[VV_CMDARG].vv_str = newval;
3022 return oldval;
3023}
3024
3025/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003026 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003027 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
3028 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003029 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3030 */
3031 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003032eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003033 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00003034 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003035 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003036 typval_T *rettv, // NULL when only checking existence
3037 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003038 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003039{
3040 int ret = OK;
3041 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003042 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02003043 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00003044 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003045 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003046
Bram Moolenaar94674f22023-01-06 18:42:20 +00003047 if (len > 0)
3048 {
3049 // truncate the name, so that we can use strcmp()
3050 cc = name[len];
3051 name[len] = NUL;
3052 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003053
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003054 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003055 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003056 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003057 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003058 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3059
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003060 if (v != NULL)
3061 {
3062 tv = &v->di_tv;
3063 if (dip != NULL)
3064 *dip = v;
3065 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003066 else
3067 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003068 }
3069
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003070 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003071 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003072 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003073 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3074
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003075 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003076 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077
3078 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003079 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003081 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003082 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003083 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003084 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003085 ht = &SCRIPT_VARS(sid);
3086 if (ht != NULL)
3087 {
3088 dictitem_T *v = find_var_in_ht(ht, 0, name,
3089 flags & EVAL_VAR_NOAUTOLOAD);
3090
3091 if (v != NULL)
3092 {
3093 tv = &v->di_tv;
3094 if (dip != NULL)
3095 *dip = v;
3096 }
3097 else
3098 ht = NULL;
3099 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003100 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003101 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003102 {
3103 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003104 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003105 ret = FAIL;
3106 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003107 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003108 else
3109 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003110 if (rettv != NULL)
3111 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003112 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003113 rettv->v_type = VAR_ANY;
3114 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3115 }
3116 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003117 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003119 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003120 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003121 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003122 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003123
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003124 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003125 // function name. For a global non-autoload function "g:" is
3126 // required.
3127 if (ufunc != NULL && (has_g_prefix
3128 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003129 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003130 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003131 if (rettv != NULL)
3132 {
3133 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003134 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003135 // Keep the "g:", otherwise script-local may be
3136 // assumed.
3137 rettv->vval.v_string = vim_strsave(name);
3138 else
3139 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003140 if (rettv->vval.v_string != NULL)
3141 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003142 }
3143 }
3144 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 }
3146
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003147 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003148 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003149 if (tv == NULL)
3150 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003151 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003152 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003153 ret = FAIL;
3154 }
3155 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003156 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003157 svar_T *sv = NULL;
3158 int was_assigned = FALSE;
3159
Bram Moolenaar11005b02021-07-11 20:59:00 +02003160 if (ht != NULL && ht == get_script_local_ht()
3161 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003162 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003163 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003164 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003165 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003166 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003167 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3168 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003169 }
3170
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003171 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3172 && sid != 0)
3173 {
3174 // type alias or class imported from another script. Check
3175 // whether it is exported from the other script.
3176 sv = find_typval_in_script(tv, sid, TRUE);
3177 if (sv == NULL)
3178 {
3179 ret = FAIL;
3180 goto done;
3181 }
3182 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3183 {
3184 semsg(_(e_item_not_exported_in_script_str), name);
3185 ret = FAIL;
3186 goto done;
3187 }
3188 }
3189
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003190 // If a list or dict variable wasn't initialized and has meaningful
3191 // type, do it now. Not for global variables, they are not
3192 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003193 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003194 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003195 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003196 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003197 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003198 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003199 tv->vval.v_dict = dict_alloc();
3200 if (tv->vval.v_dict != NULL)
3201 {
3202 ++tv->vval.v_dict->dv_refcount;
3203 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003204 if (sv != NULL)
3205 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003206 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003207 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003208 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003209 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003210 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003211 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003212 tv->vval.v_list = list_alloc();
3213 if (tv->vval.v_list != NULL)
3214 {
3215 ++tv->vval.v_list->lv_refcount;
3216 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003217 if (sv != NULL)
3218 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003219 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003220 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003221 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003222 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003223 || !in_vim9script()))
3224 {
3225 tv->vval.v_blob = blob_alloc();
3226 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003227 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003228 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003229 if (sv != NULL)
3230 sv->sv_flags |= SVFLAG_ASSIGNED;
3231 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003232 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003233 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003234 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003235 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003236 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003237
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003238done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003239 if (len > 0)
3240 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003241
3242 return ret;
3243}
3244
3245/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003246 * Get the value of internal variable "name", also handling "import.name".
3247 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3248 */
3249 int
3250eval_variable_import(
3251 char_u *name,
3252 typval_T *rettv)
3253{
3254 char_u *s = name;
3255 while (ASCII_ISALNUM(*s) || *s == '_')
3256 ++s;
3257 int len = (int)(s - name);
3258
3259 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3260 return FAIL;
3261 if (rettv->v_type == VAR_ANY && *s == '.')
3262 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003263 char_u *ns = s + 1;
3264 s = ns;
3265 while (ASCII_ISALNUM(*s) || *s == '_')
3266 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003267 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003268 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003269 }
3270 return OK;
3271}
3272
3273
3274/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003275 * Check if variable "name[len]" is a local variable or an argument.
3276 * If so, "*eval_lavars_used" is set to TRUE.
3277 */
3278 void
3279check_vars(char_u *name, int len)
3280{
3281 int cc;
3282 char_u *varname;
3283 hashtab_T *ht;
3284
3285 if (eval_lavars_used == NULL)
3286 return;
3287
3288 // truncate the name, so that we can use strcmp()
3289 cc = name[len];
3290 name[len] = NUL;
3291
3292 ht = find_var_ht(name, &varname);
3293 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3294 {
3295 if (find_var(name, NULL, TRUE) != NULL)
3296 *eval_lavars_used = TRUE;
3297 }
3298
3299 name[len] = cc;
3300}
3301
3302/*
3303 * Find variable "name" in the list of variables.
3304 * Return a pointer to it if found, NULL if not found.
3305 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003306 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003307 */
3308 dictitem_T *
3309find_var(char_u *name, hashtab_T **htp, int no_autoload)
3310{
3311 char_u *varname;
3312 hashtab_T *ht;
3313 dictitem_T *ret = NULL;
3314
3315 ht = find_var_ht(name, &varname);
3316 if (htp != NULL)
3317 *htp = ht;
3318 if (ht == NULL)
3319 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003320 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003321 if (ret != NULL)
3322 return ret;
3323
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003324 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003325 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003326 if (ret != NULL)
3327 return ret;
3328
3329 // in Vim9 script items without a scope can be script-local
3330 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3331 {
3332 ht = get_script_local_ht();
3333 if (ht != NULL)
3334 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003335 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003336 if (ret != NULL)
3337 {
3338 if (htp != NULL)
3339 *htp = ht;
3340 return ret;
3341 }
3342 }
3343 }
3344
Ernie Rael84f6dc72024-04-21 14:45:48 +02003345 // and finally try
3346 return find_var_autoload_prefix(name, 0, htp, NULL);
3347}
3348
3349/*
3350 * Find variable "name" with sn_autoload_prefix.
3351 * Return a pointer to it if found, NULL if not found.
3352 * When "sid" > 0, use it otherwise use "current_sctx.sc_sid".
3353 * When "htp" is not NULL set "htp" to the hashtab_T used.
3354 * When "namep" is not NULL set "namep" to the generated name, and
3355 * then the caller gets ownership and is responsible for freeing the name.
3356 */
3357 dictitem_T *
3358find_var_autoload_prefix(char_u *name, int sid, hashtab_T **htp,
3359 char_u **namep)
3360{
3361 hashtab_T *ht;
3362 dictitem_T *ret = NULL;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003363 // When using "vim9script autoload" script-local items are prefixed but can
3364 // be used with s:name.
Ernie Rael84f6dc72024-04-21 14:45:48 +02003365 int check_sid = sid > 0 ? sid : current_sctx.sc_sid;
3366 if (SCRIPT_ID_VALID(check_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003367 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003368 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003369 scriptitem_T *si = SCRIPT_ITEM(check_sid);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003370
3371 if (si->sn_autoload_prefix != NULL)
3372 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003373 char_u *base_name = (name[0] == 's' && name[1] == ':')
3374 ? name + 2 : name;
3375 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003376
3377 if (auto_name != NULL)
3378 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003379 int free_auto_name = TRUE;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003380 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003381 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003382 if (ret != NULL)
3383 {
3384 if (htp != NULL)
3385 *htp = ht;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003386 if (namep != NULL)
3387 {
3388 free_auto_name = FALSE;
3389 *namep = auto_name;
3390 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003391 }
Ernie Rael84f6dc72024-04-21 14:45:48 +02003392 if (free_auto_name)
3393 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003394 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003395 }
3396 }
3397
Ernie Rael84f6dc72024-04-21 14:45:48 +02003398 return ret;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003399}
3400
3401/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003402 * Like find_var() but if the name starts with <SNR>99_ then look in the
3403 * referenced script (used for a funcref).
3404 */
3405 dictitem_T *
3406find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3407{
Keith Thompson184f71c2024-01-04 21:19:04 +01003408 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003409 {
3410 char_u *p = name + 5;
3411 int sid = getdigits(&p);
3412
3413 if (SCRIPT_ID_VALID(sid) && *p == '_')
3414 {
3415 hashtab_T *ht = &SCRIPT_VARS(sid);
3416
3417 if (ht != NULL)
3418 {
3419 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3420
3421 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003422 {
3423 if (htp != NULL)
3424 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003425 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003426 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003427 }
3428 }
3429 }
3430
3431 return find_var(name, htp, no_autoload);
3432}
3433
3434/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003435 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003436 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003437 * Returns NULL if not found.
3438 */
3439 dictitem_T *
3440find_var_in_ht(
3441 hashtab_T *ht,
3442 int htname,
3443 char_u *varname,
3444 int no_autoload)
3445{
3446 hashitem_T *hi;
3447
3448 if (*varname == NUL)
3449 {
3450 // Must be something like "s:", otherwise "ht" would be NULL.
3451 switch (htname)
3452 {
3453 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3454 case 'g': return &globvars_var;
3455 case 'v': return &vimvars_var;
3456 case 'b': return &curbuf->b_bufvar;
3457 case 'w': return &curwin->w_winvar;
3458 case 't': return &curtab->tp_winvar;
3459 case 'l': return get_funccal_local_var();
3460 case 'a': return get_funccal_args_var();
3461 }
3462 return NULL;
3463 }
3464
3465 hi = hash_find(ht, varname);
3466 if (HASHITEM_EMPTY(hi))
3467 {
3468 // For global variables we may try auto-loading the script. If it
3469 // worked find the variable again. Don't auto-load a script if it was
3470 // loaded already, otherwise it would be loaded every time when
3471 // checking if a function name is a Funcref variable.
3472 if (ht == &globvarht && !no_autoload)
3473 {
3474 // Note: script_autoload() may make "hi" invalid. It must either
3475 // be obtained again or not used.
3476 if (!script_autoload(varname, FALSE) || aborting())
3477 return NULL;
3478 hi = hash_find(ht, varname);
3479 }
3480 if (HASHITEM_EMPTY(hi))
3481 return NULL;
3482 }
3483 return HI2DI(hi);
3484}
3485
3486/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 * Get the script-local hashtab. NULL if not in a script context.
3488 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003489 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003490get_script_local_ht(void)
3491{
3492 scid_T sid = current_sctx.sc_sid;
3493
Bram Moolenaare3d46852020-08-29 13:39:17 +02003494 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 return &SCRIPT_VARS(sid);
3496 return NULL;
3497}
3498
3499/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003500 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003501 * When "cmd" is TRUE it must look like a command, a function must be followed
3502 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003503 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003505 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003506lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003507 char_u *name,
3508 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003509 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003510 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003511{
3512 hashtab_T *ht = get_script_local_ht();
3513 char_u buffer[30];
3514 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003515 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003516 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003517 int is_global = FALSE;
3518 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003519
3520 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003521 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003522 if (len < sizeof(buffer) - 1)
3523 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003524 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 vim_strncpy(buffer, name, len);
3526 p = buffer;
3527 }
3528 else
3529 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003530 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003532 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 }
3534
3535 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003536 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537
Ernie Rael84f6dc72024-04-21 14:45:48 +02003538 // if not script-local, then perhaps autoload-exported
3539 if (res == FAIL && find_var_autoload_prefix(p, 0, NULL, NULL) != NULL)
3540 res = OK;
3541
3542 // if not script-local or autoload, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003543 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003544 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 if (p != buffer)
3546 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003547
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003548 // Find a function, so that a following "->" works.
3549 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3550 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003551 if (res != OK)
3552 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003553 p = skipwhite(name + len);
3554
3555 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003556 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003557 // Do not check for an internal function, since it might also be a
3558 // valid command, such as ":split" versus "split()".
3559 // Skip "g:" before a function name.
3560 if (name[0] == 'g' && name[1] == ':')
3561 {
3562 is_global = TRUE;
3563 fname = name + 2;
3564 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003565 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003566 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003567 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003568 }
3569
Bram Moolenaar709664c2020-12-12 14:33:41 +01003570 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571}
3572
3573/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003574 * Find the hashtab used for a variable name.
3575 * Return NULL if the name is not valid.
3576 * Set "varname" to the start of name without ':'.
3577 */
3578 hashtab_T *
3579find_var_ht(char_u *name, char_u **varname)
3580{
3581 hashitem_T *hi;
3582 hashtab_T *ht;
3583
3584 if (name[0] == NUL)
3585 return NULL;
3586 if (name[1] != ':')
3587 {
3588 // The name must not start with a colon or #.
3589 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3590 return NULL;
3591 *varname = name;
3592
3593 // "version" is "v:version" in all scopes if scriptversion < 3.
3594 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003595 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003596 {
3597 hi = hash_find(&compat_hashtab, name);
3598 if (!HASHITEM_EMPTY(hi))
3599 return &compat_hashtab;
3600 }
3601
3602 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003603 if (ht != NULL)
3604 return ht; // local variable
3605
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003606 // In Vim9 script items at the script level are script-local, except
3607 // for autoload names.
3608 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 {
3610 ht = get_script_local_ht();
3611 if (ht != NULL)
3612 return ht;
3613 }
3614
3615 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003616 }
3617 *varname = name + 2;
3618 if (*name == 'g') // global variable
3619 return &globvarht;
3620 // There must be no ':' or '#' in the rest of the name, unless g: is used
3621 if (vim_strchr(name + 2, ':') != NULL
3622 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3623 return NULL;
3624 if (*name == 'b') // buffer variable
3625 return &curbuf->b_vars->dv_hashtab;
3626 if (*name == 'w') // window variable
3627 return &curwin->w_vars->dv_hashtab;
3628 if (*name == 't') // tab page variable
3629 return &curtab->tp_vars->dv_hashtab;
3630 if (*name == 'v') // v: variable
3631 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003632 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003633 && get_current_funccal()->fc_func->uf_def_status
3634 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003636 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 if (*name == 'a') // a: function argument
3638 return get_funccal_args_ht();
3639 if (*name == 'l') // l: local function variable
3640 return get_funccal_local_ht();
3641 }
3642 if (*name == 's') // script variable
3643 {
3644 ht = get_script_local_ht();
3645 if (ht != NULL)
3646 return ht;
3647 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003648 return NULL;
3649}
3650
3651/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003652 * Get the string value of a (global/local) variable.
3653 * Note: see tv_get_string() for how long the pointer remains valid.
3654 * Returns NULL when it doesn't exist.
3655 */
3656 char_u *
3657get_var_value(char_u *name)
3658{
3659 dictitem_T *v;
3660
3661 v = find_var(name, NULL, FALSE);
3662 if (v == NULL)
3663 return NULL;
3664 return tv_get_string(&v->di_tv);
3665}
3666
3667/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003668 * Allocate a new hashtab for a sourced script. It will be used while
3669 * sourcing this script and when executing functions defined in the script.
3670 */
3671 void
3672new_script_vars(scid_T id)
3673{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003674 scriptvar_T *sv;
3675
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003676 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3677 if (sv == NULL)
3678 return;
3679 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003680 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003681}
3682
3683/*
3684 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3685 * point to it.
3686 */
3687 void
3688init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3689{
3690 hash_init(&dict->dv_hashtab);
3691 dict->dv_lock = 0;
3692 dict->dv_scope = scope;
3693 dict->dv_refcount = DO_NOT_FREE_CNT;
3694 dict->dv_copyID = 0;
3695 dict_var->di_tv.vval.v_dict = dict;
3696 dict_var->di_tv.v_type = VAR_DICT;
3697 dict_var->di_tv.v_lock = VAR_FIXED;
3698 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3699 dict_var->di_key[0] = NUL;
3700}
3701
3702/*
3703 * Unreference a dictionary initialized by init_var_dict().
3704 */
3705 void
3706unref_var_dict(dict_T *dict)
3707{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003708 // Now the dict needs to be freed if no one else is using it, go back to
3709 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003710 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3711 dict_unref(dict);
3712}
3713
3714/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003715 * Clean up a list of internal variables.
3716 * Frees all allocated variables and the value they contain.
3717 * Clears hashtab "ht", does not free it.
3718 */
3719 void
3720vars_clear(hashtab_T *ht)
3721{
3722 vars_clear_ext(ht, TRUE);
3723}
3724
3725/*
3726 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3727 */
3728 void
3729vars_clear_ext(hashtab_T *ht, int free_val)
3730{
3731 int todo;
3732 hashitem_T *hi;
3733 dictitem_T *v;
3734
3735 hash_lock(ht);
3736 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003737 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003738 {
3739 if (!HASHITEM_EMPTY(hi))
3740 {
3741 --todo;
3742
3743 // Free the variable. Don't remove it from the hashtab,
3744 // ht_array might change then. hash_clear() takes care of it
3745 // later.
3746 v = HI2DI(hi);
3747 if (free_val)
3748 clear_tv(&v->di_tv);
3749 if (v->di_flags & DI_FLAGS_ALLOC)
3750 vim_free(v);
3751 }
3752 }
3753 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003754 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003755}
3756
3757/*
3758 * Delete a variable from hashtab "ht" at item "hi".
3759 * Clear the variable value and free the dictitem.
3760 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003761 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003762delete_var(hashtab_T *ht, hashitem_T *hi)
3763{
3764 dictitem_T *di = HI2DI(hi);
3765
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003766 if (hash_remove(ht, hi, "delete variable") != OK)
3767 return;
3768
3769 clear_tv(&di->di_tv);
3770 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003771}
3772
3773/*
3774 * List the value of one internal variable.
3775 */
3776 static void
3777list_one_var(dictitem_T *v, char *prefix, int *first)
3778{
3779 char_u *tofree;
3780 char_u *s;
3781 char_u numbuf[NUMBUFLEN];
3782
3783 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3784 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3785 s == NULL ? (char_u *)"" : s, first);
3786 vim_free(tofree);
3787}
3788
3789 static void
3790list_one_var_a(
3791 char *prefix,
3792 char_u *name,
3793 int type,
3794 char_u *string,
3795 int *first) // when TRUE clear rest of screen and set to FALSE
3796{
3797 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3798 msg_start();
3799 msg_puts(prefix);
3800 if (name != NULL) // "a:" vars don't have a name stored
3801 msg_puts((char *)name);
3802 msg_putchar(' ');
3803 msg_advance(22);
3804 if (type == VAR_NUMBER)
3805 msg_putchar('#');
3806 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3807 msg_putchar('*');
3808 else if (type == VAR_LIST)
3809 {
3810 msg_putchar('[');
3811 if (*string == '[')
3812 ++string;
3813 }
3814 else if (type == VAR_DICT)
3815 {
3816 msg_putchar('{');
3817 if (*string == '{')
3818 ++string;
3819 }
3820 else
3821 msg_putchar(' ');
3822
3823 msg_outtrans(string);
3824
3825 if (type == VAR_FUNC || type == VAR_PARTIAL)
3826 msg_puts("()");
3827 if (*first)
3828 {
3829 msg_clr_eos();
3830 *first = FALSE;
3831 }
3832}
3833
3834/*
zeertzjqedcba962023-09-24 23:13:51 +02003835 * Addition handling for setting a v: variable.
3836 * Return TRUE if the variable should be set normally,
3837 * FALSE if nothing else needs to be done.
3838 */
3839 int
3840before_set_vvar(
3841 char_u *varname,
3842 dictitem_T *di,
3843 typval_T *tv,
3844 int copy,
3845 int *type_error)
3846{
3847 if (di->di_tv.v_type == VAR_STRING)
3848 {
3849 VIM_CLEAR(di->di_tv.vval.v_string);
3850 if (copy || tv->v_type != VAR_STRING)
3851 {
3852 char_u *val = tv_get_string(tv);
3853
3854 // Careful: when assigning to v:errmsg and
3855 // tv_get_string() causes an error message the variable
3856 // will already be set.
3857 if (di->di_tv.vval.v_string == NULL)
3858 di->di_tv.vval.v_string = vim_strsave(val);
3859 }
3860 else
3861 {
3862 // Take over the string to avoid an extra alloc/free.
3863 di->di_tv.vval.v_string = tv->vval.v_string;
3864 tv->vval.v_string = NULL;
3865 }
3866 return FALSE;
3867 }
3868 else if (di->di_tv.v_type == VAR_NUMBER)
3869 {
3870 di->di_tv.vval.v_number = tv_get_number(tv);
3871 if (STRCMP(varname, "searchforward") == 0)
3872 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3873#ifdef FEAT_SEARCH_EXTRA
3874 else if (STRCMP(varname, "hlsearch") == 0)
3875 {
3876 no_hlsearch = !di->di_tv.vval.v_number;
3877 redraw_all_later(UPD_SOME_VALID);
3878 }
3879#endif
3880 return FALSE;
3881 }
3882 else if (di->di_tv.v_type != tv->v_type)
3883 {
3884 *type_error = TRUE;
3885 return FALSE;
3886 }
3887 return TRUE;
3888}
3889
3890/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003891 * Set variable "name" to value in "tv".
3892 * If the variable already exists, the value is updated.
3893 * Otherwise the variable is created.
3894 */
3895 void
3896set_var(
3897 char_u *name,
3898 typval_T *tv,
3899 int copy) // make copy of value in "tv"
3900{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003901 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003902}
3903
3904/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003905 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003906 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003907 * If the variable already exists and "is_const" is FALSE the value is updated.
3908 * Otherwise the variable is created.
3909 */
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003910 int
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003911set_var_const(
3912 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003913 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003914 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003915 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003916 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003917 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003918 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003919{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003920 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003921 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003922 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003923 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003924 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003925 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003926 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003927 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003929 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003930 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003931 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003932 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003933 int free_tv_arg = !copy; // free tv_arg if not used
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003934 int rc = FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003935
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003936 if (sid != 0)
3937 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003938 varname = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003939 if (SCRIPT_ID_VALID(sid))
Ernie Rael84f6dc72024-04-21 14:45:48 +02003940 {
3941 char_u *auto_name = NULL;
3942 if (find_var_autoload_prefix(name, sid, &ht, &auto_name) != NULL)
3943 {
3944 var_in_autoload = TRUE;
3945 varname = auto_name;
3946 name_tofree = varname;
3947 }
3948 else
3949 ht = &SCRIPT_VARS(sid);
3950 }
3951 if (varname == NULL)
3952 varname = name;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003953 }
3954 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003955 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003956 scriptitem_T *si;
3957 char_u *auto_name = NULL;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003958
Ernie Rael84f6dc72024-04-21 14:45:48 +02003959 if (in_vim9script()
3960 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3961 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3962 ->sn_autoload_prefix != NULL
3963 && (is_export
3964 || find_var_autoload_prefix(name, 0, NULL, &auto_name)
3965 != NULL))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003966 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003967 // In a vim9 autoload script an exported variable is put in the
3968 // global namespace with the autoload prefix.
3969 var_in_autoload = TRUE;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003970 varname = auto_name != NULL ? auto_name
3971 : concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003972 if (varname == NULL)
3973 goto failed;
3974 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003975 ht = &globvarht;
3976 }
3977 else
3978 ht = find_var_ht(name, &varname);
3979 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003980 if (ht == NULL || *varname == NUL)
3981 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003982 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003983 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003984 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003985 is_script_local = ht == get_script_local_ht() || sid != 0
3986 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003987
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003988 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003989 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003990 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003991 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003992 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003993 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003994 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003995 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003996 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003997 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003998 // Do not make g:var, w:var, b:var or t:var final.
3999 flags &= ~ASSIGN_FINAL;
4000
Bram Moolenaare535db82021-03-31 21:07:24 +02004001 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004002 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
4003 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02004004 // For "[a, _] = list" the underscore is ignored.
4005 if ((flags & ASSIGN_UNPACK) == 0)
4006 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004007 goto failed;
4008 }
Bram Moolenaar67979662020-06-20 22:50:47 +02004009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004011
Bram Moolenaar24e93162021-07-18 20:40:33 +02004012 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004013 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004014 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004015
Bram Moolenaar24e93162021-07-18 20:40:33 +02004016 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004017 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004018 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02004019 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004020 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004021 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004022 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004024 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
4025 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004026 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00004027 if (!in_vim9script())
4028 {
4029 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
4030 name);
4031 goto failed;
4032 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004033 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
Bram Moolenaar993faa32022-02-21 15:59:11 +00004035 // Search in parent scope which is possible to reference from lambda
4036 if (di == NULL)
4037 di = find_var_in_scoped_ht(name, TRUE);
4038
4039 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
4040 && var_wrong_func_name(name, di == NULL))
4041 goto failed;
4042
4043 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004044 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004045 // Destination is a bool and the value is not, but it can be
4046 // converted.
4047 CLEAR_FIELD(bool_tv);
4048 bool_tv.v_type = VAR_BOOL;
4049 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
4050 tv = &bool_tv;
4051 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004052
Bram Moolenaar993faa32022-02-21 15:59:11 +00004053 if (di != NULL)
4054 {
4055 // Item already exists. Allowed to replace when reloading.
4056 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004057 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004058 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
4059 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004060 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004061 emsg(_(e_cannot_modify_existing_variable));
4062 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004063 }
4064
Bram Moolenaar993faa32022-02-21 15:59:11 +00004065 if (is_script_local && vim9script
4066 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02004067 {
4068 semsg(_(e_redefining_script_item_str), name);
4069 goto failed;
4070 }
4071
Ernie Raele75fde62023-12-21 17:18:54 +01004072 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004073 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004074
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004075 // List and Blob types can be modified in-place using the "+="
4076 // compound operator. For other types, this is not allowed.
4077 int type_inplace_modifiable =
4078 (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
4079
4080 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
4081 && ((flags & ASSIGN_COMPOUND_OP) == 0
4082 || !type_inplace_modifiable))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004083 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004084 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01004085 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004086
4087 if (sv != NULL)
4088 {
4089 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02004090 if (var_idx > 0)
4091 {
4092 where.wt_index = var_idx;
4093 where.wt_kind = WT_VARIABLE;
4094 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004095 if (check_script_var_type(sv, tv, name, where) == FAIL)
4096 goto failed;
4097 if (type == NULL)
4098 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004099 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004100 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004101 }
4102
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004103 // Modifying a final variable with a List value using the "+="
4104 // operator is allowed. For other types, it is not allowed.
zeertzjq6b97d7a2024-08-08 21:05:57 +02004105 if ((((flags & ASSIGN_FOR_LOOP) == 0 || (flags & ASSIGN_DECL) == 0)
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004106 && ((flags & ASSIGN_COMPOUND_OP) == 0
4107 || !type_inplace_modifiable))
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004108 ? var_check_permission(di, name) == FAIL
4109 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004110 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004111 }
4112 else
4113 {
4114 // can only redefine once
4115 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004116
Bram Moolenaar993faa32022-02-21 15:59:11 +00004117 // A Vim9 script-local variable is also present in sn_all_vars
4118 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004119 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004120 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004121 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004122 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004123 }
4124
Bram Moolenaar993faa32022-02-21 15:59:11 +00004125 // existing variable, need to clear the value
4126
zeertzjqedcba962023-09-24 23:13:51 +02004127 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004128 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004129 int type_error = FALSE;
4130 if (ht == &vimvarht
4131 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004132 {
zeertzjqedcba962023-09-24 23:13:51 +02004133 if (type_error)
4134 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4135 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004136 }
4137
4138 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004139
4140 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4141 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4142 {
4143 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4144
4145 update_script_var_block_id(name, si->sn_current_block_id);
4146 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004147 }
4148 else
4149 {
4150 // Item not found, check if a function already exists.
4151 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4152 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4153 {
4154 semsg(_(e_redefining_script_item_str), name);
4155 goto failed;
4156 }
4157
4158 // add a new variable
4159 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4160 {
4161 semsg(_(e_unknown_variable_str), name);
4162 goto failed;
4163 }
4164
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004165 if (check_hashtab_frozen(ht, "add variable"))
4166 goto failed;
4167
Bram Moolenaar993faa32022-02-21 15:59:11 +00004168 // Can't add "v:" or "a:" variable.
4169 if (ht == &vimvarht || ht == get_funccal_args_ht())
4170 {
4171 semsg(_(e_illegal_variable_name_str), name);
4172 goto failed;
4173 }
4174
4175 // Make sure the variable name is valid. In Vim9 script an
4176 // autoload variable must be prefixed with "g:" unless in an
4177 // autoload script.
4178 if (!valid_varname(varname, -1, !vim9script
4179 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4180 goto failed;
4181
zeertzjq1b438a82023-02-01 13:11:15 +00004182 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004183 if (di == NULL)
4184 goto failed;
4185 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004186 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004187 {
4188 vim_free(di);
4189 goto failed;
4190 }
4191 di->di_flags = DI_FLAGS_ALLOC;
4192 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4193 di->di_flags |= DI_FLAGS_LOCK;
4194
4195 // A Vim9 script-local variable is also added to sn_all_vars and
4196 // sn_var_vals. It may set "type" from "tv".
4197 if (var_in_vim9script || var_in_autoload)
4198 update_vim9_script_var(TRUE, di,
4199 var_in_autoload ? name : di->di_key, flags,
4200 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004201 }
4202
Bram Moolenaar993faa32022-02-21 15:59:11 +00004203 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004204 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004205 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004206 else
4207 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004208 *dest_tv = *tv;
4209 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004210 init_tv(tv);
4211 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004212 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004213
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004214 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004215 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004216
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004217 // ":const var = value" locks the value
4218 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004219 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004220 // Like :lockvar! name: lock the value and what it contains, but only
4221 // if the reference count is up to one. That locks only literal
4222 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004223 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004224
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004225 rc = OK;
4226
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004227failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004228 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004229 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004230 clear_tv(tv_arg);
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004231
4232 return rc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004233}
4234
4235/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004236 * Check in this order for backwards compatibility:
4237 * - Whether the variable is read-only
4238 * - Whether the variable value is locked
4239 * - Whether the variable is locked
Ernie Rael3f821d62024-04-24 20:07:50 +02004240 * NOTE: "name" is only used for error messages.
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004241 */
4242 int
4243var_check_permission(dictitem_T *di, char_u *name)
4244{
4245 if (var_check_ro(di->di_flags, name, FALSE)
4246 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4247 || var_check_lock(di->di_flags, name, FALSE))
4248 return FAIL;
4249 return OK;
4250}
4251
4252/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004253 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4254 * Also give an error message.
4255 */
4256 int
4257var_check_ro(int flags, char_u *name, int use_gettext)
4258{
4259 if (flags & DI_FLAGS_RO)
4260 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004261 if (name == NULL)
4262 emsg(_(e_cannot_change_readonly_variable));
4263 else
4264 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004265 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004266 return TRUE;
4267 }
4268 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4269 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004270 if (name == NULL)
4271 emsg(_(e_cannot_set_variable_in_sandbox));
4272 else
4273 semsg(_(e_cannot_set_variable_in_sandbox_str),
4274 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004275 return TRUE;
4276 }
4277 return FALSE;
4278}
4279
4280/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004281 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4282 * Also give an error message.
4283 */
4284 int
4285var_check_lock(int flags, char_u *name, int use_gettext)
4286{
4287 if (flags & DI_FLAGS_LOCK)
4288 {
4289 semsg(_(e_variable_is_locked_str),
4290 use_gettext ? (char_u *)_(name) : name);
4291 return TRUE;
4292 }
4293 return FALSE;
4294}
4295
4296/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004297 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4298 * Also give an error message.
4299 */
4300 int
4301var_check_fixed(int flags, char_u *name, int use_gettext)
4302{
4303 if (flags & DI_FLAGS_FIX)
4304 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004305 if (name == NULL)
4306 emsg(_(e_cannot_delete_variable));
4307 else
4308 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004309 use_gettext ? (char_u *)_(name) : name);
4310 return TRUE;
4311 }
4312 return FALSE;
4313}
4314
4315/*
4316 * Check if a funcref is assigned to a valid variable name.
4317 * Return TRUE and give an error if not.
4318 */
4319 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004320var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004321 char_u *name, // points to start of variable name
4322 int new_var) // TRUE when creating the variable
4323{
Bram Moolenaar32154662021-03-28 21:14:06 +02004324 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4325 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004326 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004327 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4328 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004329 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004330 ? name[2] : name[0])
4331 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004332 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004333 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004334 return TRUE;
4335 }
4336 // Don't allow hiding a function. When "v" is not NULL we might be
4337 // assigning another function to the same var, the type is checked
4338 // below.
4339 if (new_var && function_exists(name, FALSE))
4340 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004341 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004342 name);
4343 return TRUE;
4344 }
4345 return FALSE;
4346}
4347
4348/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004349 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4350 * value. Also give an error message, using "name" or _("name") when
4351 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004352 */
4353 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004354value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004355{
4356 if (lock & VAR_LOCKED)
4357 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004358 if (name == NULL)
4359 emsg(_(e_value_is_locked));
4360 else
4361 semsg(_(e_value_is_locked_str),
4362 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004363 return TRUE;
4364 }
4365 if (lock & VAR_FIXED)
4366 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004367 if (name == NULL)
4368 emsg(_(e_cannot_change_value));
4369 else
4370 semsg(_(e_cannot_change_value_of_str),
4371 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004372 return TRUE;
4373 }
4374 return FALSE;
4375}
4376
4377/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004378 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004379 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004380 * Return FALSE and give an error if not.
4381 */
4382 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004383valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004384{
4385 char_u *p;
4386
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004387 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004388 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004389 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004390 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004391 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004392 return FALSE;
4393 }
4394 return TRUE;
4395}
4396
4397/*
LemonBoy47d4e312022-05-04 18:12:55 +01004398 * Implements the logic to retrieve local variable and option values.
4399 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004400 */
4401 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004402get_var_from(
4403 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004404 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004405 typval_T *deftv, // Default value if not found.
4406 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4407 tabpage_T *tp, // can be NULL
4408 win_T *win,
4409 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004410{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004411 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004412 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004413 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004414 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004415 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004416
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004417 ++emsg_off;
4418
4419 rettv->v_type = VAR_STRING;
4420 rettv->vval.v_string = NULL;
4421
LemonBoy47d4e312022-05-04 18:12:55 +01004422 if (varname != NULL && tp != NULL && win != NULL
4423 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004424 {
4425 // Set curwin to be our win, temporarily. Also set the tabpage,
4426 // otherwise the window is not valid. Only do this when needed,
4427 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004428 // If we have a buffer reference avoid the switching, we're saving and
4429 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004430 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004431 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004432 {
LemonBoy47d4e312022-05-04 18:12:55 +01004433 // Handle options. There are no tab-local options.
4434 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004435 {
LemonBoy47d4e312022-05-04 18:12:55 +01004436 buf_T *save_curbuf = curbuf;
4437
4438 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004439 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004440 curbuf = buf;
4441
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004442 if (varname[1] == NUL)
4443 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004444 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004445 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004446
4447 if (opts != NULL)
4448 {
4449 rettv_dict_set(rettv, opts);
4450 done = TRUE;
4451 }
4452 }
LemonBoy47d4e312022-05-04 18:12:55 +01004453 else if (eval_option(&varname, rettv, TRUE) == OK)
4454 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004455 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004456
4457 curbuf = save_curbuf;
4458 }
4459 else if (*varname == NUL)
4460 {
4461 // Empty string: return a dict with all the local variables.
4462 if (htname == 'b')
4463 v = &buf->b_bufvar;
4464 else if (htname == 'w')
4465 v = &win->w_winvar;
4466 else
4467 v = &tp->tp_winvar;
4468 copy_tv(&v->di_tv, rettv);
4469 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004470 }
4471 else
4472 {
LemonBoy47d4e312022-05-04 18:12:55 +01004473 hashtab_T *ht;
4474
4475 if (htname == 'b')
4476 ht = &buf->b_vars->dv_hashtab;
4477 else if (htname == 'w')
4478 ht = &win->w_vars->dv_hashtab;
4479 else
4480 ht = &tp->tp_vars->dv_hashtab;
4481
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004482 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004483 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004484 if (v != NULL)
4485 {
4486 copy_tv(&v->di_tv, rettv);
4487 done = TRUE;
4488 }
4489 }
4490 }
4491
4492 if (need_switch_win)
4493 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004494 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004495 }
4496
LemonBoy47d4e312022-05-04 18:12:55 +01004497 if (!done && deftv->v_type != VAR_UNKNOWN)
4498 // use the default value
4499 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004500
4501 --emsg_off;
4502}
4503
4504/*
LemonBoy47d4e312022-05-04 18:12:55 +01004505 * getwinvar() and gettabwinvar()
4506 */
4507 static void
4508getwinvar(
4509 typval_T *argvars,
4510 typval_T *rettv,
4511 int off) // 1 for gettabwinvar()
4512{
4513 char_u *varname;
4514 tabpage_T *tp;
4515 win_T *win;
4516
4517 if (off == 1)
4518 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4519 else
4520 tp = curtab;
4521 win = find_win_by_nr(&argvars[off], tp);
4522 varname = tv_get_string_chk(&argvars[off + 1]);
4523
4524 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4525}
4526
4527/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004528 * Set option "varname" to the value of "varp" for the current buffer/window.
4529 */
4530 static void
4531set_option_from_tv(char_u *varname, typval_T *varp)
4532{
4533 long numval = 0;
4534 char_u *strval;
4535 char_u nbuf[NUMBUFLEN];
4536 int error = FALSE;
4537
zeertzjq4c7cb372023-06-14 16:39:54 +01004538 int opt_idx = findoption(varname);
4539 if (opt_idx < 0)
4540 {
4541 semsg(_(e_unknown_option_str_2), varname);
4542 return;
4543 }
4544 int opt_p_flags = get_option_flags(opt_idx);
4545
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004546 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004547 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004548 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004549 {
4550 emsg(_(e_string_required));
4551 return;
4552 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004553 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004554 strval = (char_u *)"0"; // avoid using "false"
4555 }
4556 else
4557 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004558 if ((opt_p_flags & (P_NUM|P_BOOL))
4559 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004560 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004561 if (!error)
4562 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004563 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004564 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004565 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004566}
4567
4568/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004569 * "setwinvar()" and "settabwinvar()" functions
4570 */
4571 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004572setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004573{
4574 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004575 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004576 int need_switch_win;
4577 char_u *varname, *winvarname;
4578 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004579 tabpage_T *tp = NULL;
4580
4581 if (check_secure())
4582 return;
4583
4584 if (off == 1)
4585 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4586 else
4587 tp = curtab;
4588 win = find_win_by_nr(&argvars[off], tp);
4589 varname = tv_get_string_chk(&argvars[off + 1]);
4590 varp = &argvars[off + 2];
4591
zeertzjqea720ae2023-01-03 10:54:09 +00004592 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004593 return;
4594
4595 need_switch_win = !(tp == curtab && win == curwin);
4596 if (!need_switch_win
4597 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004598 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004599 if (*varname == '&')
4600 set_option_from_tv(varname + 1, varp);
4601 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004602 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004603 winvarname = alloc(STRLEN(varname) + 3);
4604 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004605 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004606 STRCPY(winvarname, "w:");
4607 STRCPY(winvarname + 2, varname);
4608 set_var(winvarname, varp, TRUE);
4609 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004610 }
4611 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004612 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004613 if (need_switch_win)
4614 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004615}
4616
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004617/*
4618 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4619 * v:option_type, and v:option_command.
4620 */
4621 void
4622reset_v_option_vars(void)
4623{
4624 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4625 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4626 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4627 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4628 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4629 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4630}
4631
4632/*
4633 * Add an assert error to v:errors.
4634 */
4635 void
4636assert_error(garray_T *gap)
4637{
4638 struct vimvar *vp = &vimvars[VV_ERRORS];
4639
Bram Moolenaard787e402021-12-24 21:36:12 +00004640 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004641 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004642 set_vim_var_list(VV_ERRORS, list_alloc());
4643 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4644}
4645
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004646 int
4647var_exists(char_u *var)
4648{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004649 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004650 char_u *name;
4651 char_u *tofree;
4652 typval_T tv;
4653 int len = 0;
4654 int n = FALSE;
4655
4656 // get_name_len() takes care of expanding curly braces
4657 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004658 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004659 if (len > 0)
4660 {
4661 if (tofree != NULL)
4662 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004663 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004664 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004665 if (n)
4666 {
4667 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004668 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004669 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4670 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004671 if (n)
4672 clear_tv(&tv);
4673 }
4674 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004675 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004676 n = FALSE;
4677
4678 vim_free(tofree);
4679 return n;
4680}
4681
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004682static lval_T *redir_lval = NULL;
4683#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4684static garray_T redir_ga; // only valid when redir_lval is not NULL
4685static char_u *redir_endp = NULL;
4686static char_u *redir_varname = NULL;
4687
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004688 int
4689alloc_redir_lval(void)
4690{
4691 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4692 if (redir_lval == NULL)
4693 return FAIL;
4694 return OK;
4695}
4696
4697 void
4698clear_redir_lval(void)
4699{
4700 VIM_CLEAR(redir_lval);
4701}
4702
4703 void
4704init_redir_ga(void)
4705{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004706 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004707}
4708
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004709/*
4710 * Start recording command output to a variable
4711 * When "append" is TRUE append to an existing variable.
4712 * Returns OK if successfully completed the setup. FAIL otherwise.
4713 */
4714 int
4715var_redir_start(char_u *name, int append)
4716{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004717 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004718 typval_T tv;
4719
4720 // Catch a bad name early.
4721 if (!eval_isnamec1(*name))
4722 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004723 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004724 return FAIL;
4725 }
4726
4727 // Make a copy of the name, it is used in redir_lval until redir ends.
4728 redir_varname = vim_strsave(name);
4729 if (redir_varname == NULL)
4730 return FAIL;
4731
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004732 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004733 {
4734 var_redir_stop();
4735 return FAIL;
4736 }
4737
4738 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004739 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004740
4741 // Parse the variable name (can be a dict or list entry).
4742 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4743 FNE_CHECK_START);
4744 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4745 {
4746 clear_lval(redir_lval);
4747 if (redir_endp != NULL && *redir_endp != NUL)
4748 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004749 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004750 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004751 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004752 redir_endp = NULL; // don't store a value, only cleanup
4753 var_redir_stop();
4754 return FAIL;
4755 }
4756
4757 // check if we can write to the variable: set it to or append an empty
4758 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004759 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004760 tv.v_type = VAR_STRING;
4761 tv.vval.v_string = (char_u *)"";
4762 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004763 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004764 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004765 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004766 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004767 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004768 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004769 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004770 {
4771 redir_endp = NULL; // don't store a value, only cleanup
4772 var_redir_stop();
4773 return FAIL;
4774 }
4775
4776 return OK;
4777}
4778
4779/*
4780 * Append "value[value_len]" to the variable set by var_redir_start().
4781 * The actual appending is postponed until redirection ends, because the value
4782 * appended may in fact be the string we write to, changing it may cause freed
4783 * memory to be used:
4784 * :redir => foo
4785 * :let foo
4786 * :redir END
4787 */
4788 void
4789var_redir_str(char_u *value, int value_len)
4790{
4791 int len;
4792
4793 if (redir_lval == NULL)
4794 return;
4795
4796 if (value_len == -1)
4797 len = (int)STRLEN(value); // Append the entire string
4798 else
4799 len = value_len; // Append only "value_len" characters
4800
4801 if (ga_grow(&redir_ga, len) == OK)
4802 {
4803 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4804 redir_ga.ga_len += len;
4805 }
4806 else
4807 var_redir_stop();
4808}
4809
4810/*
4811 * Stop redirecting command output to a variable.
4812 * Frees the allocated memory.
4813 */
4814 void
4815var_redir_stop(void)
4816{
4817 typval_T tv;
4818
4819 if (EVALCMD_BUSY)
4820 {
4821 redir_lval = NULL;
4822 return;
4823 }
4824
4825 if (redir_lval != NULL)
4826 {
4827 // If there was no error: assign the text to the variable.
4828 if (redir_endp != NULL)
4829 {
4830 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4831 tv.v_type = VAR_STRING;
4832 tv.vval.v_string = redir_ga.ga_data;
4833 // Call get_lval() again, if it's inside a Dict or List it may
4834 // have changed.
4835 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4836 FALSE, FALSE, 0, FNE_CHECK_START);
4837 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004838 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004839 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004840 clear_lval(redir_lval);
4841 }
4842
4843 // free the collected output
4844 VIM_CLEAR(redir_ga.ga_data);
4845
4846 VIM_CLEAR(redir_lval);
4847 }
4848 VIM_CLEAR(redir_varname);
4849}
4850
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004851/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004852 * Get the collected redirected text and clear redir_ga.
4853 */
4854 char_u *
4855get_clear_redir_ga(void)
4856{
4857 char_u *res;
4858
4859 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4860 res = redir_ga.ga_data;
4861 redir_ga.ga_data = NULL;
4862 return res;
4863}
4864
4865/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004866 * "gettabvar()" function
4867 */
4868 void
4869f_gettabvar(typval_T *argvars, typval_T *rettv)
4870{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004871 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004872 tabpage_T *tp;
4873 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004874
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004875 if (in_vim9script()
4876 && (check_for_number_arg(argvars, 0) == FAIL
4877 || check_for_string_arg(argvars, 1) == FAIL))
4878 return;
4879
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004880 varname = tv_get_string_chk(&argvars[1]);
4881 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004882 if (tp != NULL)
4883 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4884 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004885
LemonBoy47d4e312022-05-04 18:12:55 +01004886 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004887}
4888
4889/*
4890 * "gettabwinvar()" function
4891 */
4892 void
4893f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4894{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004895 if (in_vim9script()
4896 && (check_for_number_arg(argvars, 0) == FAIL
4897 || check_for_number_arg(argvars, 1) == FAIL
4898 || check_for_string_arg(argvars, 2) == FAIL))
4899 return;
4900
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004901 getwinvar(argvars, rettv, 1);
4902}
4903
4904/*
4905 * "getwinvar()" function
4906 */
4907 void
4908f_getwinvar(typval_T *argvars, typval_T *rettv)
4909{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004910 if (in_vim9script()
4911 && (check_for_number_arg(argvars, 0) == FAIL
4912 || check_for_string_arg(argvars, 1) == FAIL))
4913 return;
4914
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004915 getwinvar(argvars, rettv, 0);
4916}
4917
4918/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004919 * "getbufvar()" function
4920 */
4921 void
4922f_getbufvar(typval_T *argvars, typval_T *rettv)
4923{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004924 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004925 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004926
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004927 if (in_vim9script()
4928 && (check_for_buffer_arg(argvars, 0) == FAIL
4929 || check_for_string_arg(argvars, 1) == FAIL))
4930 return;
4931
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004932 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004933 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004934
LemonBoy47d4e312022-05-04 18:12:55 +01004935 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004936}
4937
4938/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004939 * "settabvar()" function
4940 */
4941 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004942f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004943{
4944 tabpage_T *save_curtab;
4945 tabpage_T *tp;
zeertzjqb47fbb42024-02-12 22:50:26 +01004946 tabpage_T *save_lu_tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004947 char_u *varname, *tabvarname;
4948 typval_T *varp;
4949
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004950 if (check_secure())
4951 return;
4952
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004953 if (in_vim9script()
4954 && (check_for_number_arg(argvars, 0) == FAIL
4955 || check_for_string_arg(argvars, 1) == FAIL))
4956 return;
4957
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004958 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4959 varname = tv_get_string_chk(&argvars[1]);
4960 varp = &argvars[2];
4961
zeertzjqea720ae2023-01-03 10:54:09 +00004962 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004963 return;
4964
4965 save_curtab = curtab;
zeertzjqb47fbb42024-02-12 22:50:26 +01004966 save_lu_tp = lastused_tabpage;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004967 goto_tabpage_tp(tp, FALSE, FALSE);
4968
4969 tabvarname = alloc(STRLEN(varname) + 3);
4970 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004971 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004972 STRCPY(tabvarname, "t:");
4973 STRCPY(tabvarname + 2, varname);
4974 set_var(tabvarname, varp, TRUE);
4975 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004976 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004977
zeertzjqb47fbb42024-02-12 22:50:26 +01004978 // Restore current tabpage and last accessed tabpage.
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004979 if (valid_tabpage(save_curtab))
zeertzjqb47fbb42024-02-12 22:50:26 +01004980 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004981 goto_tabpage_tp(save_curtab, FALSE, FALSE);
zeertzjqb47fbb42024-02-12 22:50:26 +01004982 if (valid_tabpage(save_lu_tp))
4983 lastused_tabpage = save_lu_tp;
4984 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004985}
4986
4987/*
4988 * "settabwinvar()" function
4989 */
4990 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004991f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004992{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004993 if (in_vim9script()
4994 && (check_for_number_arg(argvars, 0) == FAIL
4995 || check_for_number_arg(argvars, 1) == FAIL
4996 || check_for_string_arg(argvars, 2) == FAIL))
4997 return;
4998
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004999 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005000}
5001
5002/*
5003 * "setwinvar()" function
5004 */
5005 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005006f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005007{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005008 if (in_vim9script()
5009 && (check_for_number_arg(argvars, 0) == FAIL
5010 || check_for_string_arg(argvars, 1) == FAIL))
5011 return;
5012
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005013 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005014}
5015
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005016/*
5017 * "setbufvar()" function
5018 */
5019 void
5020f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
5021{
5022 buf_T *buf;
5023 char_u *varname, *bufvarname;
5024 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005025
5026 if (check_secure())
5027 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02005028
5029 if (in_vim9script()
5030 && (check_for_buffer_arg(argvars, 0) == FAIL
5031 || check_for_string_arg(argvars, 1) == FAIL))
5032 return;
5033
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005034 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02005035 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005036 varp = &argvars[2];
5037
zeertzjqea720ae2023-01-03 10:54:09 +00005038 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005039 return;
5040
5041 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005042 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005043 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005044 // safe the current window position, it could
5045 // change because of 'scrollbind' window-local
5046 // options
5047 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005048
5049 // Set curbuf to be our buf, temporarily.
5050 aucmd_prepbuf(&aco, buf);
5051 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005052 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005053 // Only when it worked to set "curbuf".
5054 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005055
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005056 // reset notion of buffer
5057 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005058 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005059 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005060 }
5061 else
5062 {
5063 bufvarname = alloc(STRLEN(varname) + 3);
5064 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005065 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005066 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02005067
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005068 curbuf = buf;
5069 STRCPY(bufvarname, "b:");
5070 STRCPY(bufvarname + 2, varname);
5071 set_var(bufvarname, varp, TRUE);
5072 vim_free(bufvarname);
5073 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005074 }
5075 }
5076}
5077
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005078/*
5079 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005080 * When "arg" is zero "res.cb_name" is set to an empty string.
5081 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
5082 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005083 */
5084 callback_T
5085get_callback(typval_T *arg)
5086{
Bram Moolenaar14e579092020-03-07 16:59:25 +01005087 callback_T res;
5088 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005089
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005090 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005091 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
5092 {
5093 res.cb_partial = arg->vval.v_partial;
5094 ++res.cb_partial->pt_refcount;
5095 res.cb_name = partial_name(res.cb_partial);
5096 }
5097 else
5098 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01005099 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01005100 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01005101 r = FAIL;
5102 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005103 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005104 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005105 if (arg->v_type == VAR_STRING)
5106 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005107 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005108 if (name != NULL)
5109 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005110 res.cb_name = name;
5111 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005112 }
5113 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005114 func_ref(res.cb_name);
5115 }
5116 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005117 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005118 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005119 r = FAIL;
5120
5121 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005122 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005123 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005124 res.cb_name = NULL;
5125 }
5126 }
5127 return res;
5128}
5129
5130/*
5131 * Copy a callback into a typval_T.
5132 */
5133 void
5134put_callback(callback_T *cb, typval_T *tv)
5135{
5136 if (cb->cb_partial != NULL)
5137 {
5138 tv->v_type = VAR_PARTIAL;
5139 tv->vval.v_partial = cb->cb_partial;
5140 ++tv->vval.v_partial->pt_refcount;
5141 }
5142 else
5143 {
5144 tv->v_type = VAR_FUNC;
5145 tv->vval.v_string = vim_strsave(cb->cb_name);
5146 func_ref(cb->cb_name);
5147 }
5148}
5149
5150/*
5151 * Make a copy of "src" into "dest", allocating the function name if needed,
5152 * without incrementing the refcount.
5153 */
5154 void
5155set_callback(callback_T *dest, callback_T *src)
5156{
5157 if (src->cb_partial == NULL)
5158 {
5159 // just a function name, make a copy
5160 dest->cb_name = vim_strsave(src->cb_name);
5161 dest->cb_free_name = TRUE;
5162 }
5163 else
5164 {
5165 // cb_name is a pointer into cb_partial
5166 dest->cb_name = src->cb_name;
5167 dest->cb_free_name = FALSE;
5168 }
5169 dest->cb_partial = src->cb_partial;
5170}
5171
5172/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005173 * Copy callback from "src" to "dest", incrementing the refcounts.
5174 */
5175 void
5176copy_callback(callback_T *dest, callback_T *src)
5177{
5178 dest->cb_partial = src->cb_partial;
5179 if (dest->cb_partial != NULL)
5180 {
5181 dest->cb_name = src->cb_name;
5182 dest->cb_free_name = FALSE;
5183 ++dest->cb_partial->pt_refcount;
5184 }
5185 else
5186 {
5187 dest->cb_name = vim_strsave(src->cb_name);
5188 dest->cb_free_name = TRUE;
5189 func_ref(src->cb_name);
5190 }
5191}
5192
5193/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005194 * When a callback refers to an autoload import, change the function name to
5195 * the "path#name" form. Uses the current script context.
5196 * Only works when the name is allocated.
5197 */
5198 void
5199expand_autload_callback(callback_T *cb)
5200{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005201 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005202 char_u *p;
5203 imported_T *import;
5204
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005205 if (!in_vim9script() || cb->cb_name == NULL
5206 || (!cb->cb_free_name
5207 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005208 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005209 if (cb->cb_partial != NULL)
5210 name = cb->cb_partial->pt_name;
5211 else
5212 name = cb->cb_name;
5213 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005214 if (p == NULL)
5215 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005216
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005217 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005218 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5219 return;
5220
5221 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5222 if (si->sn_autoload_prefix == NULL)
5223 return;
5224
5225 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5226 if (newname == NULL)
5227 return;
5228
5229 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005230 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005231 if (cb->cb_name == cb->cb_partial->pt_name)
5232 cb->cb_name = newname;
5233 vim_free(cb->cb_partial->pt_name);
5234 cb->cb_partial->pt_name = newname;
5235 }
5236 else
5237 {
5238 vim_free(cb->cb_name);
5239 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005240 }
5241}
5242
5243/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005244 * Unref/free "callback" returned by get_callback() or set_callback().
5245 */
5246 void
5247free_callback(callback_T *callback)
5248{
5249 if (callback->cb_partial != NULL)
5250 {
5251 partial_unref(callback->cb_partial);
5252 callback->cb_partial = NULL;
5253 }
5254 else if (callback->cb_name != NULL)
5255 func_unref(callback->cb_name);
5256 if (callback->cb_free_name)
5257 {
5258 vim_free(callback->cb_name);
5259 callback->cb_free_name = FALSE;
5260 }
5261 callback->cb_name = NULL;
5262}
5263
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005264#endif // FEAT_EVAL