blob: 038d7ed5a13da1d38815aa8d7b5316bc4f1b838a [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},
Yegappan Lakshmanana13f3a42024-11-02 18:40:10 +0100163 {VV_NAME("t_enumvalue", VAR_NUMBER), NULL, VV_RO}
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164};
165
166// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000167#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200168#define vv_nr vv_di.di_tv.vval.v_number
169#define vv_float vv_di.di_tv.vval.v_float
170#define vv_str vv_di.di_tv.vval.v_string
171#define vv_list vv_di.di_tv.vval.v_list
172#define vv_dict vv_di.di_tv.vval.v_dict
173#define vv_blob vv_di.di_tv.vval.v_blob
174#define vv_tv vv_di.di_tv
175
176static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200178#define vimvarht vimvardict.dv_hashtab
179
180// for VIM_VERSION_ defines
181#include "version.h"
182
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200183static void list_glob_vars(int *first);
184static void list_buf_vars(int *first);
185static void list_win_vars(int *first);
186static void list_tab_vars(int *first);
187static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100188static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200189static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
190static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200191static void list_one_var(dictitem_T *v, char *prefix, int *first);
192static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
193
194/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200195 * Initialize global and vim special variables
196 */
197 void
198evalvars_init(void)
199{
200 int i;
201 struct vimvar *p;
202
203 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
204 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
205 vimvardict.dv_lock = VAR_FIXED;
206 hash_init(&compat_hashtab);
207
208 for (i = 0; i < VV_LEN; ++i)
209 {
210 p = &vimvars[i];
211 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
212 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100213 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200214 getout(1);
215 }
216 STRCPY(p->vv_di.di_key, p->vv_name);
217 if (p->vv_flags & VV_RO)
218 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
219 else if (p->vv_flags & VV_RO_SBX)
220 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
221 else
222 p->vv_di.di_flags = DI_FLAGS_FIX;
223
224 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000225 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000226 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227 if (p->vv_flags & VV_COMPAT)
228 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000229 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200230 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200231 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
232 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233
234 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
235 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100236 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200237 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
238 set_vim_var_list(VV_ERRORS, list_alloc());
239 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
240
241 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
242 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
243 set_vim_var_nr(VV_NONE, VVAL_NONE);
244 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100245 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
246 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100247 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000248 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
249 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
250 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000251 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200252
253 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
254 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
255 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
256 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
257 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
258 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
259 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
260 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
261 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
262 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
263 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000264 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
265 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200266 set_vim_var_nr(VV_TYPE_TYPEALIAS, VAR_TYPE_TYPEALIAS);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100267 set_vim_var_nr(VV_TYPE_ENUM, VAR_TYPE_ENUM);
268 set_vim_var_nr(VV_TYPE_ENUMVALUE, VAR_TYPE_ENUMVALUE);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200269
270 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
271
Drew Vogele30d1022021-10-24 20:35:07 +0100272 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
273
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200274#ifdef FEAT_PYTHON3
275 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
276#endif
277
Bram Moolenaar439c0362020-06-06 15:58:03 +0200278 // Default for v:register is not 0 but '"'. This is adjusted once the
279 // clipboard has been setup by calling reset_reg_var().
280 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200281}
282
283#if defined(EXITFREE) || defined(PROTO)
284/*
285 * Free all vim variables information on exit
286 */
287 void
288evalvars_clear(void)
289{
290 int i;
291 struct vimvar *p;
292
293 for (i = 0; i < VV_LEN; ++i)
294 {
295 p = &vimvars[i];
296 if (p->vv_di.di_tv.v_type == VAR_STRING)
297 VIM_CLEAR(p->vv_str);
298 else if (p->vv_di.di_tv.v_type == VAR_LIST)
299 {
300 list_unref(p->vv_list);
301 p->vv_list = NULL;
302 }
303 }
304 hash_clear(&vimvarht);
305 hash_init(&vimvarht); // garbage_collect() will access it
306 hash_clear(&compat_hashtab);
307
308 // global variables
309 vars_clear(&globvarht);
310
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100311 // Script-local variables. Clear all the variables here.
312 // The scriptvar_T is cleared later in free_scriptnames(), because a
313 // variable in one script might hold a reference to the whole scope of
314 // another script.
315 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200316 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200317}
318#endif
319
320 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200321garbage_collect_globvars(int copyID)
322{
323 return set_ref_in_ht(&globvarht, copyID, NULL);
324}
325
326 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327garbage_collect_vimvars(int copyID)
328{
329 return set_ref_in_ht(&vimvarht, copyID, NULL);
330}
331
332 int
333garbage_collect_scriptvars(int copyID)
334{
Bram Moolenaared234f22020-10-15 20:42:20 +0200335 int i;
336 int idx;
337 int abort = FALSE;
338 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100340 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200341 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200342 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
343
Bram Moolenaared234f22020-10-15 20:42:20 +0200344 si = SCRIPT_ITEM(i);
345 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
346 {
347 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
348
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100349 if (sv->sv_name != NULL)
350 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200351 }
352 }
353
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200354 return abort;
355}
356
357/*
358 * Set an internal variable to a string value. Creates the variable if it does
359 * not already exist.
360 */
361 void
362set_internal_string_var(char_u *name, char_u *value)
363{
364 char_u *val;
365 typval_T *tvp;
366
367 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000368 if (val == NULL)
369 return;
370
371 tvp = alloc_string_tv(val);
372 if (tvp == NULL)
373 return;
374
375 set_var(name, tvp, FALSE);
376 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200377}
378
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200379 int
380eval_charconvert(
381 char_u *enc_from,
382 char_u *enc_to,
383 char_u *fname_from,
384 char_u *fname_to)
385{
386 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000387 sctx_T saved_sctx = current_sctx;
388 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200389
390 set_vim_var_string(VV_CC_FROM, enc_from, -1);
391 set_vim_var_string(VV_CC_TO, enc_to, -1);
392 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
393 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000394 ctx = get_option_sctx("charconvert");
395 if (ctx != NULL)
396 current_sctx = *ctx;
397
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100398 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200399 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000400
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200401 set_vim_var_string(VV_CC_FROM, NULL, -1);
402 set_vim_var_string(VV_CC_TO, NULL, -1);
403 set_vim_var_string(VV_FNAME_IN, NULL, -1);
404 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000405 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200406
407 if (err)
408 return FAIL;
409 return OK;
410}
411
412# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
413 int
414eval_printexpr(char_u *fname, char_u *args)
415{
416 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000417 sctx_T saved_sctx = current_sctx;
418 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200419
420 set_vim_var_string(VV_FNAME_IN, fname, -1);
421 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000422 ctx = get_option_sctx("printexpr");
423 if (ctx != NULL)
424 current_sctx = *ctx;
425
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100426 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200427 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000428
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200429 set_vim_var_string(VV_FNAME_IN, NULL, -1);
430 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000431 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200432
433 if (err)
434 {
435 mch_remove(fname);
436 return FAIL;
437 }
438 return OK;
439}
440# endif
441
442# if defined(FEAT_DIFF) || defined(PROTO)
443 void
444eval_diff(
445 char_u *origfile,
446 char_u *newfile,
447 char_u *outfile)
448{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000449 sctx_T saved_sctx = current_sctx;
450 sctx_T *ctx;
451 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200452
453 set_vim_var_string(VV_FNAME_IN, origfile, -1);
454 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
455 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000456
457 ctx = get_option_sctx("diffexpr");
458 if (ctx != NULL)
459 current_sctx = *ctx;
460
461 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100462 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000463 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000464
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200465 set_vim_var_string(VV_FNAME_IN, NULL, -1);
466 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
467 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000468 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200469}
470
471 void
472eval_patch(
473 char_u *origfile,
474 char_u *difffile,
475 char_u *outfile)
476{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000477 sctx_T saved_sctx = current_sctx;
478 sctx_T *ctx;
479 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200480
481 set_vim_var_string(VV_FNAME_IN, origfile, -1);
482 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
483 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000484
485 ctx = get_option_sctx("patchexpr");
486 if (ctx != NULL)
487 current_sctx = *ctx;
488
489 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100490 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000491 free_tv(tv);
492
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200493 set_vim_var_string(VV_FNAME_IN, NULL, -1);
494 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
495 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000496 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200497}
498# endif
499
500#if defined(FEAT_SPELL) || defined(PROTO)
501/*
502 * Evaluate an expression to a list with suggestions.
503 * For the "expr:" part of 'spellsuggest'.
504 * Returns NULL when there is an error.
505 */
506 list_T *
507eval_spell_expr(char_u *badword, char_u *expr)
508{
509 typval_T save_val;
510 typval_T rettv;
511 list_T *list = NULL;
512 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000513 sctx_T saved_sctx = current_sctx;
514 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100515 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200516
517 // Set "v:val" to the bad word.
518 prepare_vimvar(VV_VAL, &save_val);
519 set_vim_var_string(VV_VAL, badword, -1);
520 if (p_verbose == 0)
521 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000522 ctx = get_option_sctx("spellsuggest");
523 if (ctx != NULL)
524 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200525
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100526 r = may_call_simple_func(p, &rettv);
527 if (r == NOTDONE)
528 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
529 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200530 {
531 if (rettv.v_type != VAR_LIST)
532 clear_tv(&rettv);
533 else
534 list = rettv.vval.v_list;
535 }
536
537 if (p_verbose == 0)
538 --emsg_off;
539 clear_tv(get_vim_var_tv(VV_VAL));
540 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000541 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200542
543 return list;
544}
545
546/*
547 * "list" is supposed to contain two items: a word and a number. Return the
548 * word in "pp" and the number as the return value.
549 * Return -1 if anything isn't right.
550 * Used to get the good word and score from the eval_spell_expr() result.
551 */
552 int
553get_spellword(list_T *list, char_u **pp)
554{
555 listitem_T *li;
556
557 li = list->lv_first;
558 if (li == NULL)
559 return -1;
560 *pp = tv_get_string(&li->li_tv);
561
562 li = li->li_next;
563 if (li == NULL)
564 return -1;
565 return (int)tv_get_number(&li->li_tv);
566}
567#endif
568
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200569/*
570 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200571 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200572 * When not used yet add the variable to the v: hashtable.
573 */
574 void
575prepare_vimvar(int idx, typval_T *save_tv)
576{
577 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200578 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000579 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000580 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200581}
582
583/*
584 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200585 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200586 * When no longer defined, remove the variable from the v: hashtable.
587 */
588 void
589restore_vimvar(int idx, typval_T *save_tv)
590{
591 hashitem_T *hi;
592
593 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000594 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
595 return;
596
597 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
598 if (HASHITEM_EMPTY(hi))
599 internal_error("restore_vimvar()");
600 else
601 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200602}
603
604/*
605 * List Vim variables.
606 */
607 static void
608list_vim_vars(int *first)
609{
610 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
611}
612
613/*
614 * List script-local variables, if there is a script.
615 */
616 static void
617list_script_vars(int *first)
618{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200619 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200620 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
621 "s:", FALSE, first);
622}
623
624/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100625 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
626 * But only when an identifier character follows.
627 */
628 int
629is_scoped_variable(char_u *name)
630{
631 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
632 && name[1] == ':'
633 && eval_isnamec(name[2]);
634}
635
636/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100637 * Evaluate one Vim expression {expr} in string "p" and append the
638 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100639 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100640 * Return a pointer to the character after "}", NULL for an error.
641 */
642 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100643eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100644{
645 char_u *block_start = skipwhite(p + 1); // skip the opening {
646 char_u *block_end = block_start;
647 char_u *expr_val;
648
649 if (*block_start == NUL)
650 {
651 semsg(_(e_missing_close_curly_str), p);
652 return NULL;
653 }
654 if (skip_expr(&block_end, NULL) == FAIL)
655 return NULL;
656 block_end = skipwhite(block_end);
657 if (*block_end != '}')
658 {
659 semsg(_(e_missing_close_curly_str), p);
660 return NULL;
661 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100662 if (evaluate)
663 {
664 *block_end = NUL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200665 expr_val = eval_to_string(block_start, FALSE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100666 *block_end = '}';
667 if (expr_val == NULL)
668 return NULL;
669 ga_concat(gap, expr_val);
670 vim_free(expr_val);
671 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100672
673 return block_end + 1;
674}
675
676/*
677 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
678 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
679 * Used for a heredoc assignment.
680 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100681 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100682 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100683eval_all_expr_in_str(char_u *str)
684{
685 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100686 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100687
688 ga_init2(&ga, 1, 80);
689 p = str;
690
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100691 while (*p != NUL)
692 {
LemonBoy2eaef102022-05-06 13:14:50 +0100693 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100694 int escaped_brace = FALSE;
695
696 // Look for a block start.
697 lit_start = p;
698 while (*p != '{' && *p != '}' && *p != NUL)
699 ++p;
700
701 if (*p != NUL && *p == p[1])
702 {
703 // Escaped brace, unescape and continue.
704 // Include the brace in the literal string.
705 ++p;
706 escaped_brace = TRUE;
707 }
708 else if (*p == '}')
709 {
710 semsg(_(e_stray_closing_curly_str), str);
711 ga_clear(&ga);
712 return NULL;
713 }
714
715 // Append the literal part.
716 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
717
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100718 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100719 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100720
LemonBoy2eaef102022-05-06 13:14:50 +0100721 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100722 {
LemonBoy2eaef102022-05-06 13:14:50 +0100723 // Skip the second brace.
724 ++p;
725 continue;
726 }
727
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100728 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100729 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100730 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100731 {
732 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100733 return NULL;
734 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100735 }
736 ga_append(&ga, NUL);
737
738 return ga.ga_data;
739}
740
741/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 * Get a list of lines from a HERE document. The here document is a list of
743 * lines surrounded by a marker.
744 * cmd << {marker}
745 * {line1}
746 * {line2}
747 * ....
748 * {marker}
749 *
750 * The {marker} is a string. If the optional 'trim' word is supplied before the
751 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100752 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200753 *
754 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100755 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200756 * missing, then '.' is accepted as a marker.
757 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100758 * When compiling a heredoc assignment to a variable in a Vim9 def function,
759 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
760 * string values from the heredoc, vim9 instructions are generated. On success
761 * the returned list will be empty.
762 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100763 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100766heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100768 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200769 char_u *marker;
770 list_T *l;
771 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100772 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200773 int marker_indent_len = 0;
774 int text_indent_len = 0;
775 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200776 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200777 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100778 int evalstr = FALSE;
779 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100780 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
781 int count = 0;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200782 int heredoc_in_string = FALSE;
783 char_u *line_arg = NULL;
zeertzjq1f5175d2024-04-13 17:52:26 +0200784 char_u *nl_ptr = vim_strchr(cmd, '\n');
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200785
zeertzjq1f5175d2024-04-13 17:52:26 +0200786 if (nl_ptr != NULL)
787 {
788 heredoc_in_string = TRUE;
789 line_arg = nl_ptr + 1;
790 *nl_ptr = NUL;
791 }
792 else if (eap->ea_getline == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200793 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000794 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200795 return NULL;
796 }
797
798 // Check for the optional 'trim' word before the marker
799 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200800
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100801 while (TRUE)
802 {
803 if (STRNCMP(cmd, "trim", 4) == 0
804 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100806 cmd = skipwhite(cmd + 4);
807
808 // Trim the indentation from all the lines in the here document.
809 // The amount of indentation trimmed is the same as the indentation
810 // of the first line after the :let command line. To find the end
811 // marker the indent of the :let command line is trimmed.
812 p = *eap->cmdlinep;
813 while (VIM_ISWHITE(*p))
814 {
815 p++;
816 marker_indent_len++;
817 }
818 text_indent_len = -1;
819
820 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100822 if (STRNCMP(cmd, "eval", 4) == 0
823 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
824 {
825 cmd = skipwhite(cmd + 4);
826 evalstr = TRUE;
827 continue;
828 }
829 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200830 }
831
832 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200833 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 {
835 marker = skipwhite(cmd);
zeertzjq1f5175d2024-04-13 17:52:26 +0200836 p = skiptowhite(marker);
837 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000839 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840 return NULL;
841 }
842 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200843 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200844 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000845 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846 return NULL;
847 }
848 }
849 else
850 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200851 // When getting lines for an embedded script, if the marker is missing,
852 // accept '.' as the marker.
853 if (script_get)
854 marker = dot;
855 else
856 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000857 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200858 return NULL;
859 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200860 }
861
862 l = list_alloc();
863 if (l == NULL)
864 return NULL;
865
866 for (;;)
867 {
868 int mi = 0;
869 int ti = 0;
870
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200871 if (heredoc_in_string)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200873 char_u *next_line;
874
875 // heredoc in a string separated by newlines. Get the next line
876 // from the string.
877
878 if (*line_arg == NUL)
879 {
880 semsg(_(e_missing_end_marker_str), marker);
881 break;
882 }
883
884 theline = line_arg;
885 next_line = vim_strchr(theline, '\n');
886 if (next_line == NULL)
887 line_arg += STRLEN(line_arg);
888 else
889 {
890 *next_line = NUL;
891 line_arg = next_line + 1;
892 }
893 }
894 else
895 {
896 vim_free(theline);
897 theline = eap->ea_getline(NUL, eap->cookie, 0, FALSE);
898 if (theline == NULL)
899 {
900 semsg(_(e_missing_end_marker_str), marker);
901 break;
902 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200903 }
904
905 // with "trim": skip the indent matching the :let line to find the
906 // marker
907 if (marker_indent_len > 0
908 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
909 mi = marker_indent_len;
910 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200911 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200912
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100913 // If expression evaluation failed in the heredoc, then skip till the
914 // end marker.
915 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100916 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100917
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200918 if (text_indent_len == -1 && *theline != NUL)
919 {
920 // set the text indent from the first line.
921 p = theline;
922 text_indent_len = 0;
923 while (VIM_ISWHITE(*p))
924 {
925 p++;
926 text_indent_len++;
927 }
928 text_indent = vim_strnsave(theline, text_indent_len);
929 }
930 // with "trim": skip the indent matching the first line
931 if (text_indent != NULL)
932 for (ti = 0; ti < text_indent_len; ++ti)
933 if (theline[ti] != text_indent[ti])
934 break;
935
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100936 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100937 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100938 {
LemonBoy2eaef102022-05-06 13:14:50 +0100939 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100940 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100941 vim_free(theline);
942 vim_free(text_indent);
943 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100944 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100945 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100946 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100947 else
948 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200949 int free_str = FALSE;
950
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100951 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100952 {
953 str = eval_all_expr_in_str(str);
954 if (str == NULL)
955 {
956 // expression evaluation failed
957 eval_failed = TRUE;
958 continue;
959 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200960 free_str = TRUE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100961 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100962
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100963 if (list_append_string(l, str, -1) == FAIL)
964 break;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200965 if (free_str)
966 vim_free(str);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100967 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200969 if (heredoc_in_string)
970 // Next command follows the heredoc in the string.
971 eap->nextcmd = line_arg;
972 else
973 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200974 vim_free(text_indent);
975
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100976 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
977 generate_NEWLIST(cctx, count, FALSE);
978
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100979 if (eval_failed)
980 {
981 // expression evaluation in the heredoc failed
982 list_free(l);
983 return NULL;
984 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200985 return l;
986}
987
988/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200989 * Vim9 variable declaration:
990 * ":var name"
991 * ":var name: type"
992 * ":var name = expr"
993 * ":var name: type = expr"
994 * etc.
995 */
996 void
997ex_var(exarg_T *eap)
998{
Bram Moolenaar330a3882022-03-05 11:05:57 +0000999 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +00001000 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +00001001
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001002 if (!in_vim9script())
1003 {
1004 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
1005 return;
1006 }
Bram Moolenaare1d12112022-03-05 11:37:48 +00001007 has_var = checkforcmd_noparen(&p, "var", 3);
1008 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00001009 {
1010 emsg(_(e_cannot_declare_variable_on_command_line));
1011 return;
1012 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001013 ex_let(eap);
1014}
1015
1016/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 * ":let" list all variable values
1018 * ":let var1 var2" list variable values
1019 * ":let var = expr" assignment command.
1020 * ":let var += expr" assignment command.
1021 * ":let var -= expr" assignment command.
1022 * ":let var *= expr" assignment command.
1023 * ":let var /= expr" assignment command.
1024 * ":let var %= expr" assignment command.
1025 * ":let var .= expr" assignment command.
1026 * ":let var ..= expr" assignment command.
1027 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +01001029 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001030 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001031 * ":final var = expr" assignment command.
1032 * ":final [var1, var2] = expr" unpack list.
1033 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001034 * ":const" list all variable values
1035 * ":const var1 var2" list variable values
1036 * ":const var = expr" assignment command.
1037 * ":const [var1, var2] = expr" unpack list.
1038 */
1039 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001040ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001041{
1042 char_u *arg = eap->arg;
1043 char_u *expr = NULL;
1044 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001045 int var_count = 0;
1046 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001047 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 char_u *argend;
1049 int first = TRUE;
1050 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001051 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001052 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001053 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001055 if (eap->cmdidx == CMD_final && !vim9script)
1056 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001057 // In legacy Vim script ":final" is short for ":finally".
1058 ex_finally(eap);
1059 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001060 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001061 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001062 {
1063 emsg(_(e_cannot_use_let_in_vim9_script));
1064 return;
1065 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001066
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001067 if (eap->cmdidx == CMD_const)
1068 flags |= ASSIGN_CONST;
1069 else if (eap->cmdidx == CMD_final)
1070 flags |= ASSIGN_FINAL;
1071
1072 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001074 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001076 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001077 if (argend == NULL)
1078 return;
1079 if (argend > arg && argend[-1] == '.') // for var.='str'
1080 --argend;
1081 expr = skipwhite(argend);
1082 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001083 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001084 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001085 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1086 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001087 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001088 {
1089 // ":let" without "=": list variables
1090 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001091 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001092 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001093 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001094 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001095 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001096 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001097 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001098 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001099 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001100 else
1101 // Vim9 declaration ":var name: type"
1102 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001103 }
1104 else
1105 {
1106 // ":let var1 var2" - list values
1107 arg = list_arg_vars(eap, arg, &first);
1108 }
1109 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001110 else if (!eap->skip)
1111 {
1112 // ":let"
1113 list_glob_vars(&first);
1114 list_buf_vars(&first);
1115 list_win_vars(&first);
1116 list_tab_vars(&first);
1117 list_script_vars(&first);
1118 list_func_vars(&first);
1119 list_vim_vars(&first);
1120 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001121 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001122 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001123 }
zeertzjq474891b2023-04-12 21:36:03 +01001124
1125 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001127 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001128 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001129
Bram Moolenaard9876422022-10-12 12:58:54 +01001130 // :let text =<< [trim] [eval] END
1131 // :var text =<< [trim] [eval] END
1132 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1133 || !IS_WHITE_OR_NUL(expr[3])))
1134 semsg(_(e_white_space_required_before_and_after_str_at_str),
1135 "=<<", expr);
1136 else
1137 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1138
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001139 if (l != NULL)
1140 {
1141 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001142 if (!eap->skip)
1143 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001144 // errors are for the assignment, not the end marker
1145 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001146 op[0] = '=';
1147 op[1] = NUL;
1148 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001150 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001151 clear_tv(&rettv);
1152 }
zeertzjq474891b2023-04-12 21:36:03 +01001153 return;
1154 }
1155
1156 evalarg_T evalarg;
1157 int len = 1;
1158
1159 CLEAR_FIELD(rettv);
1160
1161 int cur_lnum;
1162
1163 op[0] = '=';
1164 op[1] = NUL;
1165 if (*expr != '=')
1166 {
1167 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1168 {
1169 // +=, /=, etc. require an existing variable
1170 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1171 }
1172 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1173 {
1174 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1175 ++len;
1176 if (expr[0] == '.' && expr[1] == '.') // ..=
1177 {
1178 ++expr;
1179 ++len;
1180 }
1181 }
1182 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001183 }
1184 else
zeertzjq474891b2023-04-12 21:36:03 +01001185 ++expr;
1186
1187 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1188 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001189 {
zeertzjq474891b2023-04-12 21:36:03 +01001190 vim_strncpy(op, expr - len, len);
1191 semsg(_(e_white_space_required_before_and_after_str_at_str),
1192 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001193 }
zeertzjq474891b2023-04-12 21:36:03 +01001194
1195 if (eap->skip)
1196 ++emsg_skip;
1197 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1198 expr = skipwhite_and_linebreak(expr, &evalarg);
1199 cur_lnum = SOURCING_LNUM;
1200 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1201 if (eap->skip)
1202 --emsg_skip;
1203 clear_evalarg(&evalarg, eap);
1204
1205 // Restore the line number so that any type error is given for the
1206 // declaration, not the expression.
1207 SOURCING_LNUM = cur_lnum;
1208
1209 if (!eap->skip && eval_res != FAIL)
1210 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1211 flags, op);
1212 if (eval_res != FAIL)
1213 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001214}
1215
1216/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001217 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001218 * Handles both "var" with any type and "[var, var; var]" with a list type.
1219 * When "op" is not NULL it points to a string with characters that
1220 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1221 * or concatenate.
1222 * Returns OK or FAIL;
1223 */
1224 int
1225ex_let_vars(
1226 char_u *arg_start,
1227 typval_T *tv,
1228 int copy, // copy values from "tv", don't move
1229 int semicolon, // from skip_var_list()
1230 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001231 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001232 char_u *op)
1233{
1234 char_u *arg = arg_start;
1235 list_T *l;
1236 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001237 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001238 listitem_T *item;
1239 typval_T ltv;
1240
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001241 if (tv->v_type == VAR_VOID)
1242 {
1243 emsg(_(e_cannot_use_void_value));
1244 return FAIL;
1245 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001246 if (*arg != '[')
1247 {
1248 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001249 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001250 return FAIL;
1251 return OK;
1252 }
1253
1254 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1255 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1256 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001257 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001258 return FAIL;
1259 }
1260
1261 i = list_len(l);
1262 if (semicolon == 0 && var_count < i)
1263 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001264 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001265 return FAIL;
1266 }
1267 if (var_count - semicolon > i)
1268 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001269 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001270 return FAIL;
1271 }
1272
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001273 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001274 item = l->lv_first;
1275 while (*arg != ']')
1276 {
1277 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001278 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001279 arg = ex_let_one(arg, &item->li_tv, TRUE,
1280 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001281 item = item->li_next;
1282 if (arg == NULL)
1283 return FAIL;
1284
1285 arg = skipwhite(arg);
1286 if (*arg == ';')
1287 {
1288 // Put the rest of the list (may be empty) in the var after ';'.
1289 // Create a new list for this.
1290 l = list_alloc();
1291 if (l == NULL)
1292 return FAIL;
1293 while (item != NULL)
1294 {
1295 list_append_tv(l, &item->li_tv);
1296 item = item->li_next;
1297 }
1298
1299 ltv.v_type = VAR_LIST;
1300 ltv.v_lock = 0;
1301 ltv.vval.v_list = l;
1302 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001303 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001304
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001305 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1306 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001307 clear_tv(&ltv);
1308 if (arg == NULL)
1309 return FAIL;
1310 break;
1311 }
1312 else if (*arg != ',' && *arg != ']')
1313 {
1314 internal_error("ex_let_vars()");
1315 return FAIL;
1316 }
1317 }
1318
1319 return OK;
1320}
1321
1322/*
1323 * Skip over assignable variable "var" or list of variables "[var, var]".
1324 * Used for ":let varvar = expr" and ":for varvar in expr".
1325 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001326 * for "[var, var; var]" set "semicolon" to 1.
1327 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001328 * Return NULL for an error.
1329 */
1330 char_u *
1331skip_var_list(
1332 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001334 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001335 int *semicolon,
1336 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001337{
1338 char_u *p, *s;
1339
1340 if (*arg == '[')
1341 {
1342 // "[var, var]": find the matching ']'.
1343 p = arg;
1344 for (;;)
1345 {
1346 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001347 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 if (s == p)
1349 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001350 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001351 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 return NULL;
1353 }
1354 ++*var_count;
1355
1356 p = skipwhite(s);
1357 if (*p == ']')
1358 break;
1359 else if (*p == ';')
1360 {
1361 if (*semicolon == 1)
1362 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001363 if (!silent)
1364 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365 return NULL;
1366 }
1367 *semicolon = 1;
1368 }
1369 else if (*p != ',')
1370 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001371 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001372 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001373 return NULL;
1374 }
1375 }
1376 return p + 1;
1377 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001378
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001379 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001380}
1381
1382/*
1383 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1384 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001386 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001387 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001390 char_u *end;
1391 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001393 if (*arg == '@' && arg[1] != NUL)
1394 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001395
1396 // termcap option name may have non-alpha characters
1397 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1398 return arg + 5;
1399
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001402
1403 // "a: type" is declaring variable "a" with a type, not "a:".
1404 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001405 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001406 --end;
1407
Bram Moolenaar585587d2021-01-17 20:52:13 +01001408 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001410 if (*skipwhite(end) == ':')
1411 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 }
1413 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414}
1415
1416/*
1417 * List variables for hashtab "ht" with prefix "prefix".
1418 * If "empty" is TRUE also list NULL strings as empty strings.
1419 */
1420 void
1421list_hashtable_vars(
1422 hashtab_T *ht,
1423 char *prefix,
1424 int empty,
1425 int *first)
1426{
1427 hashitem_T *hi;
1428 dictitem_T *di;
1429 int todo;
1430 char_u buf[IOSIZE];
1431
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001432 int save_ht_flags = ht->ht_flags;
1433 ht->ht_flags |= HTFLAGS_FROZEN;
1434
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 todo = (int)ht->ht_used;
1436 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1437 {
1438 if (!HASHITEM_EMPTY(hi))
1439 {
1440 --todo;
1441 di = HI2DI(hi);
1442
1443 // apply :filter /pat/ to variable name
1444 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1445 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1446 if (message_filtered(buf))
1447 continue;
1448
1449 if (empty || di->di_tv.v_type != VAR_STRING
1450 || di->di_tv.vval.v_string != NULL)
1451 list_one_var(di, prefix, first);
1452 }
1453 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001454
1455 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456}
1457
1458/*
1459 * List global variables.
1460 */
1461 static void
1462list_glob_vars(int *first)
1463{
1464 list_hashtable_vars(&globvarht, "", TRUE, first);
1465}
1466
1467/*
1468 * List buffer variables.
1469 */
1470 static void
1471list_buf_vars(int *first)
1472{
1473 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1474}
1475
1476/*
1477 * List window variables.
1478 */
1479 static void
1480list_win_vars(int *first)
1481{
1482 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1483}
1484
1485/*
1486 * List tab page variables.
1487 */
1488 static void
1489list_tab_vars(int *first)
1490{
1491 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1492}
1493
1494/*
1495 * List variables in "arg".
1496 */
1497 static char_u *
1498list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1499{
1500 int error = FALSE;
1501 int len;
1502 char_u *name;
1503 char_u *name_start;
1504 char_u *arg_subsc;
1505 char_u *tofree;
1506 typval_T tv;
1507
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001508 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001509 {
1510 if (error || eap->skip)
1511 {
1512 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1513 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1514 {
1515 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001516 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001517 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001518 break;
1519 }
1520 }
1521 else
1522 {
1523 // get_name_len() takes care of expanding curly braces
1524 name_start = name = arg;
1525 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1526 if (len <= 0)
1527 {
1528 // This is mainly to keep test 49 working: when expanding
1529 // curly braces fails overrule the exception error message.
1530 if (len < 0 && !aborting())
1531 {
1532 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001533 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534 break;
1535 }
1536 error = TRUE;
1537 }
1538 else
1539 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001540 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001541 if (tofree != NULL)
1542 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001543 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001544 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001545 error = TRUE;
1546 else
1547 {
1548 // handle d.key, l[idx], f(expr)
1549 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001550 if (handle_subscript(&arg, name_start, &tv,
1551 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 error = TRUE;
1553 else
1554 {
1555 if (arg == arg_subsc && len == 2 && name[1] == ':')
1556 {
1557 switch (*name)
1558 {
1559 case 'g': list_glob_vars(first); break;
1560 case 'b': list_buf_vars(first); break;
1561 case 'w': list_win_vars(first); break;
1562 case 't': list_tab_vars(first); break;
1563 case 'v': list_vim_vars(first); break;
1564 case 's': list_script_vars(first); break;
1565 case 'l': list_func_vars(first); break;
1566 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001567 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568 }
1569 }
1570 else
1571 {
1572 char_u numbuf[NUMBUFLEN];
1573 char_u *tf;
1574 int c;
1575 char_u *s;
1576
1577 s = echo_string(&tv, &tf, numbuf, 0);
1578 c = *arg;
1579 *arg = NUL;
1580 list_one_var_a("",
1581 arg == arg_subsc ? name : name_start,
1582 tv.v_type,
1583 s == NULL ? (char_u *)"" : s,
1584 first);
1585 *arg = c;
1586 vim_free(tf);
1587 }
1588 clear_tv(&tv);
1589 }
1590 }
1591 }
1592
1593 vim_free(tofree);
1594 }
1595
1596 arg = skipwhite(arg);
1597 }
1598
1599 return arg;
1600}
1601
1602/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001603 * Set an environment variable, part of ex_let_one().
1604 */
1605 static char_u *
1606ex_let_env(
1607 char_u *arg,
1608 typval_T *tv,
1609 int flags,
1610 char_u *endchars,
1611 char_u *op)
1612{
1613 char_u *arg_end = NULL;
1614 char_u *name;
1615 int len;
1616
1617 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1618 && (flags & ASSIGN_FOR_LOOP) == 0)
1619 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001620 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001621 return NULL;
1622 }
1623
1624 // Find the end of the name.
1625 ++arg;
1626 name = arg;
1627 len = get_env_len(&arg);
1628 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001629 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001630 else
1631 {
1632 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001633 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001634 else if (endchars != NULL
1635 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1636 emsg(_(e_unexpected_characters_in_let));
1637 else if (!check_secure())
1638 {
1639 char_u *tofree = NULL;
1640 int c1 = name[len];
1641 char_u *p;
1642
1643 name[len] = NUL;
1644 p = tv_get_string_chk(tv);
1645 if (p != NULL && op != NULL && *op == '.')
1646 {
1647 int mustfree = FALSE;
1648 char_u *s = vim_getenv(name, &mustfree);
1649
1650 if (s != NULL)
1651 {
1652 p = tofree = concat_str(s, p);
1653 if (mustfree)
1654 vim_free(s);
1655 }
1656 }
1657 if (p != NULL)
1658 {
1659 vim_setenv_ext(name, p);
1660 arg_end = arg;
1661 }
1662 name[len] = c1;
1663 vim_free(tofree);
1664 }
1665 }
1666 return arg_end;
1667}
1668
1669/*
1670 * Set an option, part of ex_let_one().
1671 */
1672 static char_u *
1673ex_let_option(
1674 char_u *arg,
1675 typval_T *tv,
1676 int flags,
1677 char_u *endchars,
1678 char_u *op)
1679{
1680 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001681 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001682 char_u *arg_end = NULL;
1683
1684 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1685 && (flags & ASSIGN_FOR_LOOP) == 0)
1686 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001687 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001688 return NULL;
1689 }
1690
1691 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001692 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001693 if (p == NULL || (endchars != NULL
1694 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001695 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001696 emsg(_(e_unexpected_characters_in_let));
1697 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001698 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001699
1700 int c1;
1701 long n = 0;
1702 getoption_T opt_type;
1703 long numval;
1704 char_u *stringval = NULL;
1705 char_u *s = NULL;
1706 int failed = FALSE;
1707 int opt_p_flags;
1708 char_u *tofree = NULL;
1709 char_u numbuf[NUMBUFLEN];
1710
1711 c1 = *p;
1712 *p = NUL;
1713
1714 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1715 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1716 {
1717 semsg(_(e_unknown_option_str_2), arg);
1718 goto theend;
1719 }
1720 if (op != NULL && *op != '='
1721 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1722 || (opt_type == gov_string && *op != '.')))
1723 {
1724 semsg(_(e_wrong_variable_type_for_str_equal), op);
1725 goto theend;
1726 }
1727
1728 if ((opt_type == gov_bool
1729 || opt_type == gov_number
1730 || opt_type == gov_hidden_bool
1731 || opt_type == gov_hidden_number)
1732 && (tv->v_type != VAR_STRING || !in_vim9script()))
1733 {
1734 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1735 // bool, possibly hidden
1736 n = (long)tv_get_bool_chk(tv, &failed);
1737 else
1738 // number, possibly hidden
1739 n = (long)tv_get_number_chk(tv, &failed);
1740 if (failed)
1741 goto theend;
1742 }
1743
1744 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1745 || tv->v_type == VAR_FUNC))
1746 {
1747 // If the option can be set to a function reference or a lambda
1748 // and the passed value is a function reference, then convert it to
1749 // the name (string) of the function reference.
1750 s = tv2string(tv, &tofree, numbuf, 0);
1751 if (s == NULL)
1752 goto theend;
1753 }
1754 // Avoid setting a string option to the text "v:false" or similar.
1755 // In Vim9 script also don't convert a number to string.
1756 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1757 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1758 {
1759 s = tv_get_string_chk(tv);
1760 if (s == NULL)
1761 goto theend;
1762 }
1763 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1764 {
1765 emsg(_(e_string_required));
1766 goto theend;
1767 }
1768
1769 if (op != NULL && *op != '=')
1770 {
1771 // number, in legacy script also bool
1772 if (opt_type == gov_number
1773 || (opt_type == gov_bool && !in_vim9script()))
1774 {
1775 switch (*op)
1776 {
1777 case '+': n = numval + n; break;
1778 case '-': n = numval - n; break;
1779 case '*': n = numval * n; break;
1780 case '/': n = (long)num_divide(numval, n, &failed); break;
1781 case '%': n = (long)num_modulus(numval, n, &failed); break;
1782 }
1783 s = NULL;
1784 if (failed)
1785 goto theend;
1786 }
1787 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1788 {
1789 // string
1790 s = concat_str(stringval, s);
1791 vim_free(stringval);
1792 stringval = s;
1793 }
1794 }
1795
1796 char *err = set_option_value(arg, n, s, scope);
1797 arg_end = p;
1798 if (err != NULL)
1799 emsg(_(err));
1800
1801theend:
1802 *p = c1;
1803 vim_free(stringval);
1804 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001805 return arg_end;
1806}
1807
1808/*
1809 * Set a register, part of ex_let_one().
1810 */
1811 static char_u *
1812ex_let_register(
1813 char_u *arg,
1814 typval_T *tv,
1815 int flags,
1816 char_u *endchars,
1817 char_u *op)
1818{
1819 char_u *arg_end = NULL;
1820
1821 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1822 && (flags & ASSIGN_FOR_LOOP) == 0)
1823 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001824 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001825 return NULL;
1826 }
1827 ++arg;
1828 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001829 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001830 else if (endchars != NULL
1831 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1832 emsg(_(e_unexpected_characters_in_let));
1833 else
1834 {
1835 char_u *ptofree = NULL;
1836 char_u *p;
1837
1838 p = tv_get_string_chk(tv);
1839 if (p != NULL && op != NULL && *op == '.')
1840 {
1841 char_u *s = get_reg_contents(*arg == '@'
1842 ? '"' : *arg, GREG_EXPR_SRC);
1843
1844 if (s != NULL)
1845 {
1846 p = ptofree = concat_str(s, p);
1847 vim_free(s);
1848 }
1849 }
1850 if (p != NULL)
1851 {
1852 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1853 arg_end = arg + 1;
1854 }
1855 vim_free(ptofree);
1856 }
1857 return arg_end;
1858}
1859
1860/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001861 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1862 * Returns a pointer to the char just after the var name.
1863 * Returns NULL if there is an error.
1864 */
1865 static char_u *
1866ex_let_one(
1867 char_u *arg, // points to variable name
1868 typval_T *tv, // value to assign to variable
1869 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001870 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001872 char_u *op, // "+", "-", "." or NULL
1873 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001874{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001877 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001878 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001879 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1880 {
1881 vim9_declare_error(arg);
1882 return NULL;
1883 }
1884
Ernie Rael9ed53752023-12-11 17:40:46 +01001885 if (check_typval_is_value(tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001886 return NULL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001887
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001888 if (*arg == '$')
1889 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001890 // ":let $VAR = expr": Set environment variable.
1891 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 else if (*arg == '&')
1894 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001895 // ":let &option = expr": Set option value.
1896 // ":let &l:option = expr": Set local option value.
1897 // ":let &g:option = expr": Set global option value.
1898 // ":for &ts in range(8)": Set option value for for loop
1899 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001900 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001901 else if (*arg == '@')
1902 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001903 // ":let @r = expr": Set register contents.
1904 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001905 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001906 else if (eval_isnamec1(*arg) || *arg == '{')
1907 {
1908 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001909 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001910 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1911 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001912 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001913 if (op != NULL && *op != '=')
1914 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001915
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001916 // ":let var = expr": Set internal variable.
1917 // ":let var: type = expr": Set internal variable with type.
1918 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001919 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001920 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001921 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 if (endchars != NULL && vim_strchr(endchars,
1923 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001924 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001925 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001926 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001927 else
1928 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001929 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001930 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 }
1932 }
1933 clear_lval(&lv);
1934 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001936 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001937
1938 return arg_end;
1939}
1940
1941/*
1942 * ":unlet[!] var1 ... " command.
1943 */
1944 void
1945ex_unlet(exarg_T *eap)
1946{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001947 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001948}
1949
1950/*
1951 * ":lockvar" and ":unlockvar" commands
1952 */
1953 void
1954ex_lockvar(exarg_T *eap)
1955{
1956 char_u *arg = eap->arg;
1957 int deep = 2;
1958
1959 if (eap->forceit)
1960 deep = -1;
1961 else if (vim_isdigit(*arg))
1962 {
1963 deep = getdigits(&arg);
1964 arg = skipwhite(arg);
1965 }
1966
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001967 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968}
1969
1970/*
1971 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001972 * Also used for Vim9 script. "callback" is invoked as:
1973 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001974 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001975 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001976ex_unletlock(
1977 exarg_T *eap,
1978 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001979 int deep,
1980 int glv_flags,
1981 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1982 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001983{
1984 char_u *arg = argstart;
1985 char_u *name_end;
1986 int error = FALSE;
1987 lval_T lv;
1988
1989 do
1990 {
1991 if (*arg == '$')
1992 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001993 lv.ll_name = arg;
1994 lv.ll_tv = NULL;
1995 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001996 if (get_env_len(&arg) == 0)
1997 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001998 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001999 return;
2000 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002001 if (!error && !eap->skip
2002 && callback(&lv, arg, eap, deep, cookie) == FAIL)
2003 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002004 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002005 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002006 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002007 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002008 // Parse the name and find the end.
2009 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01002010 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002011 if (lv.ll_name == NULL)
2012 error = TRUE; // error but continue parsing
2013 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
2014 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002015 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002016 if (name_end != NULL)
2017 {
2018 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00002019 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002020 }
2021 if (!(eap->skip || error))
2022 clear_lval(&lv);
2023 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002024 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002025
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002026 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002027 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002028 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002029
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002030 if (!eap->skip)
2031 clear_lval(&lv);
2032 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002033
2034 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002035 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002036
Bram Moolenaar63b91732021-08-05 20:40:03 +02002037 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002038}
2039
2040 static int
2041do_unlet_var(
2042 lval_T *lp,
2043 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002044 exarg_T *eap,
2045 int deep UNUSED,
2046 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002047{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002048 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002049 int ret = OK;
2050 int cc;
2051
2052 if (lp->ll_tv == NULL)
2053 {
2054 cc = *name_end;
2055 *name_end = NUL;
2056
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002057 // Environment variable, normal name or expanded name.
2058 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002059 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002060 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002061 ret = FAIL;
2062 *name_end = cc;
2063 }
2064 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002065 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002066 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002067 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002068 return FAIL;
2069 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002070 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2071 !lp->ll_empty2, lp->ll_n2);
2072 else if (lp->ll_list != NULL)
2073 // unlet a List item.
2074 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002075 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002076 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002077 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002078
2079 return ret;
2080}
2081
2082/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002083 * Unlet one item or a range of items from a list.
2084 * Return OK or FAIL.
2085 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002086 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002087list_unlet_range(
2088 list_T *l,
2089 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002090 long n1_arg,
2091 int has_n2,
2092 long n2)
2093{
2094 listitem_T *li = li_first;
2095 int n1 = n1_arg;
2096
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002097 // Delete a range of List items.
2098 li = li_first;
2099 n1 = n1_arg;
2100 while (li != NULL && (!has_n2 || n2 >= n1))
2101 {
2102 listitem_T *next = li->li_next;
2103
2104 listitem_remove(l, li);
2105 li = next;
2106 ++n1;
2107 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002108}
2109/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002110 * "unlet" a variable. Return OK if it existed, FAIL if not.
2111 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2112 */
2113 int
2114do_unlet(char_u *name, int forceit)
2115{
2116 hashtab_T *ht;
2117 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002118 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002119 dict_T *d;
2120 dictitem_T *di;
2121
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002122 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002123 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002124 return FAIL;
2125
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002126 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002127
2128 // can't :unlet a script variable in Vim9 script from a function
2129 if (ht == get_script_local_ht()
2130 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2131 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2132 == SCRIPT_VERSION_VIM9
2133 && check_vim9_unlet(name) == FAIL)
2134 return FAIL;
2135
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002136 if (ht != NULL && *varname != NUL)
2137 {
2138 d = get_current_funccal_dict(ht);
2139 if (d == NULL)
2140 {
2141 if (ht == &globvarht)
2142 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002143 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002144 d = &vimvardict;
2145 else
2146 {
2147 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2148 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2149 }
2150 if (d == NULL)
2151 {
2152 internal_error("do_unlet()");
2153 return FAIL;
2154 }
2155 }
2156 hi = hash_find(ht, varname);
2157 if (HASHITEM_EMPTY(hi))
2158 hi = find_hi_in_scoped_ht(name, &ht);
2159 if (hi != NULL && !HASHITEM_EMPTY(hi))
2160 {
2161 di = HI2DI(hi);
2162 if (var_check_fixed(di->di_flags, name, FALSE)
2163 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002164 || value_check_lock(d->dv_lock, name, FALSE)
2165 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002166 return FAIL;
2167
2168 delete_var(ht, hi);
2169 return OK;
2170 }
2171 }
2172 if (forceit)
2173 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002174 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002175 return FAIL;
2176}
2177
Ernie Raelee865f32023-09-29 19:53:55 +02002178 static void
2179report_lockvar_member(char *msg, lval_T *lp)
2180{
2181 int did_alloc = FALSE;
2182 char_u *vname = (char_u *)"";
2183 char_u *class_name = lp->ll_class != NULL
2184 ? lp->ll_class->class_name : (char_u *)"";
2185 if (lp->ll_name != NULL)
2186 {
2187 if (lp->ll_name_end == NULL)
2188 vname = lp->ll_name;
2189 else
2190 {
2191 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2192 if (vname == NULL)
2193 return;
2194 did_alloc = TRUE;
2195 }
2196 }
2197 semsg(_(msg), vname, class_name);
2198 if (did_alloc)
2199 vim_free(vname);
2200}
2201
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002202/*
2203 * Lock or unlock variable indicated by "lp".
2204 * "deep" is the levels to go (-1 for unlimited);
2205 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2206 */
2207 static int
2208do_lock_var(
2209 lval_T *lp,
2210 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002211 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002212 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002213 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002214{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002215 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002216 int ret = OK;
2217 int cc;
2218 dictitem_T *di;
2219
Ernie Raelee865f32023-09-29 19:53:55 +02002220#ifdef LOG_LOCKVAR
2221 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2222#endif
2223
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002224 if (lp->ll_tv == NULL)
2225 {
2226 cc = *name_end;
2227 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002228 if (*lp->ll_name == '$')
2229 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002230 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002231 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002232 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002233 else
2234 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002235 // Normal name or expanded name.
2236 di = find_var(lp->ll_name, NULL, TRUE);
2237 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002238 {
2239 if (in_vim9script())
2240 semsg(_(e_cannot_find_variable_to_unlock_str),
2241 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002242 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002243 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002244 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002245 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002246 if ((di->di_flags & DI_FLAGS_FIX)
2247 && di->di_tv.v_type != VAR_DICT
2248 && di->di_tv.v_type != VAR_LIST)
2249 {
2250 // For historic reasons this error is not given for a list
2251 // or dict. E.g., the b: dict could be locked/unlocked.
2252 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2253 ret = FAIL;
2254 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002255 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002256 {
2257 if (in_vim9script())
2258 {
2259 svar_T *sv = find_typval_in_script(&di->di_tv,
2260 0, FALSE);
2261
2262 if (sv != NULL && sv->sv_const != 0)
2263 {
2264 semsg(_(e_cannot_change_readonly_variable_str),
2265 lp->ll_name);
2266 ret = FAIL;
2267 }
2268 }
2269
2270 if (ret == OK)
2271 {
2272 if (lock)
2273 di->di_flags |= DI_FLAGS_LOCK;
2274 else
2275 di->di_flags &= ~DI_FLAGS_LOCK;
2276 if (deep != 0)
2277 item_lock(&di->di_tv, deep, lock, FALSE);
2278 }
2279 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002280 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002281 }
2282 *name_end = cc;
2283 }
Ernie Raelee865f32023-09-29 19:53:55 +02002284 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002285 {
2286 // nothing to do
2287 }
Ernie Raelee865f32023-09-29 19:53:55 +02002288 else if (lp->ll_is_root)
2289 // (un)lock the item.
2290 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002291 else if (lp->ll_range)
2292 {
2293 listitem_T *li = lp->ll_li;
2294
2295 // (un)lock a range of List items.
2296 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2297 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002298 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002299 li = li->li_next;
2300 ++lp->ll_n1;
2301 }
2302 }
2303 else if (lp->ll_list != NULL)
2304 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002305 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002306 else if (lp->ll_object != NULL) // This check must be before ll_class.
2307 {
2308 // (un)lock an object variable.
2309 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2310 ret = FAIL;
2311 }
2312 else if (lp->ll_class != NULL)
2313 {
2314 // (un)lock a class variable.
2315 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2316 ret = FAIL;
2317 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002318 else
Ernie Raelee865f32023-09-29 19:53:55 +02002319 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002320 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002321 if (lp->ll_di == NULL)
2322 {
2323 emsg(_(e_dictionary_required));
2324 ret = FAIL;
2325 }
2326 else
2327 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2328 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002329
2330 return ret;
2331}
2332
2333/*
2334 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002335 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2336 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002337 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002338 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002339item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002340{
2341 static int recurse = 0;
2342 list_T *l;
2343 listitem_T *li;
2344 dict_T *d;
2345 blob_T *b;
2346 hashitem_T *hi;
2347 int todo;
2348
Ernie Raelee865f32023-09-29 19:53:55 +02002349#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002350 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002351#endif
2352
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002353 if (recurse >= DICT_MAXNEST)
2354 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002355 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002356 return;
2357 }
2358 if (deep == 0)
2359 return;
2360 ++recurse;
2361
2362 // lock/unlock the item itself
2363 if (lock)
2364 tv->v_lock |= VAR_LOCKED;
2365 else
2366 tv->v_lock &= ~VAR_LOCKED;
2367
2368 switch (tv->v_type)
2369 {
2370 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002371 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002373 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002375 case VAR_STRING:
2376 case VAR_FUNC:
2377 case VAR_PARTIAL:
2378 case VAR_FLOAT:
2379 case VAR_SPECIAL:
2380 case VAR_JOB:
2381 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002382 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002383 case VAR_CLASS:
2384 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002385 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002386 break;
2387
2388 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002389 if ((b = tv->vval.v_blob) != NULL
2390 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002391 {
2392 if (lock)
2393 b->bv_lock |= VAR_LOCKED;
2394 else
2395 b->bv_lock &= ~VAR_LOCKED;
2396 }
2397 break;
2398 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002399 if ((l = tv->vval.v_list) != NULL
2400 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002401 {
2402 if (lock)
2403 l->lv_lock |= VAR_LOCKED;
2404 else
2405 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002406 if (deep < 0 || deep > 1)
2407 {
2408 if (l->lv_first == &range_list_item)
2409 l->lv_lock |= VAR_ITEMS_LOCKED;
2410 else
2411 {
2412 // recursive: lock/unlock the items the List contains
2413 CHECK_LIST_MATERIALIZE(l);
2414 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2415 deep - 1, lock, check_refcount);
2416 }
2417 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002418 }
2419 break;
2420 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002421 if ((d = tv->vval.v_dict) != NULL
2422 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002423 {
2424 if (lock)
2425 d->dv_lock |= VAR_LOCKED;
2426 else
2427 d->dv_lock &= ~VAR_LOCKED;
2428 if (deep < 0 || deep > 1)
2429 {
2430 // recursive: lock/unlock the items the List contains
2431 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002432 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002433 {
2434 if (!HASHITEM_EMPTY(hi))
2435 {
2436 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002437 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2438 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002439 }
2440 }
2441 }
2442 }
2443 }
2444 --recurse;
2445}
2446
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002447#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2448/*
2449 * Delete all "menutrans_" variables.
2450 */
2451 void
2452del_menutrans_vars(void)
2453{
2454 hashitem_T *hi;
2455 int todo;
2456
2457 hash_lock(&globvarht);
2458 todo = (int)globvarht.ht_used;
2459 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2460 {
2461 if (!HASHITEM_EMPTY(hi))
2462 {
2463 --todo;
2464 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2465 delete_var(&globvarht, hi);
2466 }
2467 }
2468 hash_unlock(&globvarht);
2469}
2470#endif
2471
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002472/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002473 * Local string buffer for the next two functions to store a variable name
2474 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2475 * get_user_var_name().
2476 */
2477
2478static char_u *varnamebuf = NULL;
2479static int varnamebuflen = 0;
2480
2481/*
2482 * Function to concatenate a prefix and a variable name.
2483 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002484 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002485cat_prefix_varname(int prefix, char_u *name)
2486{
2487 int len;
2488
2489 len = (int)STRLEN(name) + 3;
2490 if (len > varnamebuflen)
2491 {
2492 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002493 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002494 varnamebuf = alloc(len);
2495 if (varnamebuf == NULL)
2496 {
2497 varnamebuflen = 0;
2498 return NULL;
2499 }
2500 varnamebuflen = len;
2501 }
2502 *varnamebuf = prefix;
2503 varnamebuf[1] = ':';
2504 STRCPY(varnamebuf + 2, name);
2505 return varnamebuf;
2506}
2507
2508/*
2509 * Function given to ExpandGeneric() to obtain the list of user defined
2510 * (global/buffer/window/built-in) variable names.
2511 */
2512 char_u *
2513get_user_var_name(expand_T *xp, int idx)
2514{
2515 static long_u gdone;
2516 static long_u bdone;
2517 static long_u wdone;
2518 static long_u tdone;
2519 static int vidx;
2520 static hashitem_T *hi;
2521 hashtab_T *ht;
2522
2523 if (idx == 0)
2524 {
2525 gdone = bdone = wdone = vidx = 0;
2526 tdone = 0;
2527 }
2528
2529 // Global variables
2530 if (gdone < globvarht.ht_used)
2531 {
2532 if (gdone++ == 0)
2533 hi = globvarht.ht_array;
2534 else
2535 ++hi;
2536 while (HASHITEM_EMPTY(hi))
2537 ++hi;
2538 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2539 return cat_prefix_varname('g', hi->hi_key);
2540 return hi->hi_key;
2541 }
2542
2543 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002544 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002545 if (bdone < ht->ht_used)
2546 {
2547 if (bdone++ == 0)
2548 hi = ht->ht_array;
2549 else
2550 ++hi;
2551 while (HASHITEM_EMPTY(hi))
2552 ++hi;
2553 return cat_prefix_varname('b', hi->hi_key);
2554 }
2555
2556 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002557 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002558 if (wdone < ht->ht_used)
2559 {
2560 if (wdone++ == 0)
2561 hi = ht->ht_array;
2562 else
2563 ++hi;
2564 while (HASHITEM_EMPTY(hi))
2565 ++hi;
2566 return cat_prefix_varname('w', hi->hi_key);
2567 }
2568
2569 // t: variables
2570 ht = &curtab->tp_vars->dv_hashtab;
2571 if (tdone < ht->ht_used)
2572 {
2573 if (tdone++ == 0)
2574 hi = ht->ht_array;
2575 else
2576 ++hi;
2577 while (HASHITEM_EMPTY(hi))
2578 ++hi;
2579 return cat_prefix_varname('t', hi->hi_key);
2580 }
2581
2582 // v: variables
2583 if (vidx < VV_LEN)
2584 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2585
2586 VIM_CLEAR(varnamebuf);
2587 varnamebuflen = 0;
2588 return NULL;
2589}
2590
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002591 char *
2592get_var_special_name(int nr)
2593{
2594 switch (nr)
2595 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002596 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2597 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002598 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002599 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002600 }
2601 internal_error("get_var_special_name()");
2602 return "42";
2603}
2604
2605/*
2606 * Returns the global variable dictionary
2607 */
2608 dict_T *
2609get_globvar_dict(void)
2610{
2611 return &globvardict;
2612}
2613
2614/*
2615 * Returns the global variable hash table
2616 */
2617 hashtab_T *
2618get_globvar_ht(void)
2619{
2620 return &globvarht;
2621}
2622
2623/*
2624 * Returns the v: variable dictionary
2625 */
2626 dict_T *
2627get_vimvar_dict(void)
2628{
2629 return &vimvardict;
2630}
2631
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002632/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002634 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 */
2636 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002637find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002639 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2640 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641
2642 if (di == NULL)
2643 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002644 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2646 return (int)(vv - vimvars);
2647}
2648
2649
2650/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002651 * Set type of v: variable to "type".
2652 */
2653 void
2654set_vim_var_type(int idx, vartype_T type)
2655{
Bram Moolenaard787e402021-12-24 21:36:12 +00002656 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002657}
2658
2659/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002660 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002661 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002662 */
2663 void
2664set_vim_var_nr(int idx, varnumber_T val)
2665{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002666 vimvars[idx].vv_nr = val;
2667}
2668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 char *
2670get_vim_var_name(int idx)
2671{
2672 return vimvars[idx].vv_name;
2673}
2674
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002675/*
2676 * Get typval_T v: variable value.
2677 */
2678 typval_T *
2679get_vim_var_tv(int idx)
2680{
2681 return &vimvars[idx].vv_tv;
2682}
2683
Bram Moolenaard787e402021-12-24 21:36:12 +00002684 type_T *
2685get_vim_var_type(int idx, garray_T *type_list)
2686{
2687 if (vimvars[idx].vv_type != NULL)
2688 return vimvars[idx].vv_type;
2689 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2690}
2691
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002692/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002693 * Set v: variable to "tv". Only accepts the same type.
2694 * Takes over the value of "tv".
2695 */
2696 int
2697set_vim_var_tv(int idx, typval_T *tv)
2698{
Bram Moolenaard787e402021-12-24 21:36:12 +00002699 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002700 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002701 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002702 clear_tv(tv);
2703 return FAIL;
2704 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002705 // VV_RO is also checked when compiling, but let's check here as well.
2706 if (vimvars[idx].vv_flags & VV_RO)
2707 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002708 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002709 return FAIL;
2710 }
2711 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2712 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002713 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002714 return FAIL;
2715 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002716 clear_tv(&vimvars[idx].vv_di.di_tv);
2717 vimvars[idx].vv_di.di_tv = *tv;
2718 return OK;
2719}
2720
2721/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002722 * Get number v: variable value.
2723 */
2724 varnumber_T
2725get_vim_var_nr(int idx)
2726{
2727 return vimvars[idx].vv_nr;
2728}
2729
2730/*
2731 * Get string v: variable value. Uses a static buffer, can only be used once.
2732 * If the String variable has never been set, return an empty string.
2733 * Never returns NULL;
2734 */
2735 char_u *
2736get_vim_var_str(int idx)
2737{
2738 return tv_get_string(&vimvars[idx].vv_tv);
2739}
2740
2741/*
2742 * Get List v: variable value. Caller must take care of reference count when
2743 * needed.
2744 */
2745 list_T *
2746get_vim_var_list(int idx)
2747{
2748 return vimvars[idx].vv_list;
2749}
2750
2751/*
2752 * Get Dict v: variable value. Caller must take care of reference count when
2753 * needed.
2754 */
2755 dict_T *
2756get_vim_var_dict(int idx)
2757{
2758 return vimvars[idx].vv_dict;
2759}
2760
2761/*
2762 * Set v:char to character "c".
2763 */
2764 void
2765set_vim_var_char(int c)
2766{
2767 char_u buf[MB_MAXBYTES + 1];
2768
2769 if (has_mbyte)
2770 buf[(*mb_char2bytes)(c, buf)] = NUL;
2771 else
2772 {
2773 buf[0] = c;
2774 buf[1] = NUL;
2775 }
2776 set_vim_var_string(VV_CHAR, buf, -1);
2777}
2778
2779/*
2780 * Set v:count to "count" and v:count1 to "count1".
2781 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2782 */
2783 void
2784set_vcount(
2785 long count,
2786 long count1,
2787 int set_prevcount)
2788{
2789 if (set_prevcount)
2790 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2791 vimvars[VV_COUNT].vv_nr = count;
2792 vimvars[VV_COUNT1].vv_nr = count1;
2793}
2794
2795/*
2796 * Save variables that might be changed as a side effect. Used when executing
2797 * a timer callback.
2798 */
2799 void
2800save_vimvars(vimvars_save_T *vvsave)
2801{
2802 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2803 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2804 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2805}
2806
2807/*
2808 * Restore variables saved by save_vimvars().
2809 */
2810 void
2811restore_vimvars(vimvars_save_T *vvsave)
2812{
2813 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2814 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2815 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2816}
2817
2818/*
2819 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2820 * value.
2821 */
2822 void
2823set_vim_var_string(
2824 int idx,
2825 char_u *val,
2826 int len) // length of "val" to use or -1 (whole string)
2827{
2828 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002829 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002830 if (val == NULL)
2831 vimvars[idx].vv_str = NULL;
2832 else if (len == -1)
2833 vimvars[idx].vv_str = vim_strsave(val);
2834 else
2835 vimvars[idx].vv_str = vim_strnsave(val, len);
2836}
2837
2838/*
2839 * Set List v: variable to "val".
2840 */
2841 void
2842set_vim_var_list(int idx, list_T *val)
2843{
2844 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002845 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002846 vimvars[idx].vv_list = val;
2847 if (val != NULL)
2848 ++val->lv_refcount;
2849}
2850
2851/*
2852 * Set Dictionary v: variable to "val".
2853 */
2854 void
2855set_vim_var_dict(int idx, dict_T *val)
2856{
2857 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002858 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002859 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002860 if (val == NULL)
2861 return;
2862
2863 ++val->dv_refcount;
2864 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002865}
2866
2867/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002868 * Set the v:argv list.
2869 */
2870 void
2871set_argv_var(char **argv, int argc)
2872{
2873 list_T *l = list_alloc();
2874 int i;
2875
2876 if (l == NULL)
2877 getout(1);
2878 l->lv_lock = VAR_FIXED;
2879 for (i = 0; i < argc; ++i)
2880 {
2881 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2882 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002883 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002884 }
2885 set_vim_var_list(VV_ARGV, l);
2886}
2887
2888/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002889 * Reset v:register, taking the 'clipboard' setting into account.
2890 */
2891 void
2892reset_reg_var(void)
2893{
2894 int regname = 0;
2895
2896 // Adjust the register according to 'clipboard', so that when
2897 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2898#ifdef FEAT_CLIPBOARD
2899 adjust_clip_reg(&regname);
2900#endif
2901 set_reg_var(regname);
2902}
2903
2904/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002905 * Set v:register if needed.
2906 */
2907 void
2908set_reg_var(int c)
2909{
2910 char_u regname;
2911
2912 if (c == 0 || c == ' ')
2913 regname = '"';
2914 else
2915 regname = c;
2916 // Avoid free/alloc when the value is already right.
2917 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2918 set_vim_var_string(VV_REG, &regname, 1);
2919}
2920
2921/*
2922 * Get or set v:exception. If "oldval" == NULL, return the current value.
2923 * Otherwise, restore the value to "oldval" and return NULL.
2924 * Must always be called in pairs to save and restore v:exception! Does not
2925 * take care of memory allocations.
2926 */
2927 char_u *
2928v_exception(char_u *oldval)
2929{
2930 if (oldval == NULL)
2931 return vimvars[VV_EXCEPTION].vv_str;
2932
2933 vimvars[VV_EXCEPTION].vv_str = oldval;
2934 return NULL;
2935}
2936
2937/*
2938 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2939 * Otherwise, restore the value to "oldval" and return NULL.
2940 * Must always be called in pairs to save and restore v:throwpoint! Does not
2941 * take care of memory allocations.
2942 */
2943 char_u *
2944v_throwpoint(char_u *oldval)
2945{
2946 if (oldval == NULL)
2947 return vimvars[VV_THROWPOINT].vv_str;
2948
2949 vimvars[VV_THROWPOINT].vv_str = oldval;
2950 return NULL;
2951}
2952
2953/*
2954 * Set v:cmdarg.
2955 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2956 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2957 * Must always be called in pairs!
2958 */
2959 char_u *
2960set_cmdarg(exarg_T *eap, char_u *oldarg)
2961{
2962 char_u *oldval;
2963 char_u *newval;
2964 unsigned len;
2965
2966 oldval = vimvars[VV_CMDARG].vv_str;
2967 if (eap == NULL)
2968 {
2969 vim_free(oldval);
2970 vimvars[VV_CMDARG].vv_str = oldarg;
2971 return NULL;
2972 }
2973
2974 if (eap->force_bin == FORCE_BIN)
2975 len = 6;
2976 else if (eap->force_bin == FORCE_NOBIN)
2977 len = 8;
2978 else
2979 len = 0;
2980
2981 if (eap->read_edit)
2982 len += 7;
2983
2984 if (eap->force_ff != 0)
2985 len += 10; // " ++ff=unix"
2986 if (eap->force_enc != 0)
2987 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2988 if (eap->bad_char != 0)
2989 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2990
2991 newval = alloc(len + 1);
2992 if (newval == NULL)
2993 return NULL;
2994
2995 if (eap->force_bin == FORCE_BIN)
2996 sprintf((char *)newval, " ++bin");
2997 else if (eap->force_bin == FORCE_NOBIN)
2998 sprintf((char *)newval, " ++nobin");
2999 else
3000 *newval = NUL;
3001
3002 if (eap->read_edit)
3003 STRCAT(newval, " ++edit");
3004
3005 if (eap->force_ff != 0)
3006 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
3007 eap->force_ff == 'u' ? "unix"
3008 : eap->force_ff == 'd' ? "dos"
3009 : "mac");
3010 if (eap->force_enc != 0)
3011 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
3012 eap->cmd + eap->force_enc);
3013 if (eap->bad_char == BAD_KEEP)
3014 STRCPY(newval + STRLEN(newval), " ++bad=keep");
3015 else if (eap->bad_char == BAD_DROP)
3016 STRCPY(newval + STRLEN(newval), " ++bad=drop");
3017 else if (eap->bad_char != 0)
3018 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
3019 vimvars[VV_CMDARG].vv_str = newval;
3020 return oldval;
3021}
3022
3023/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003024 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003025 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
3026 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003027 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3028 */
3029 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003030eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003031 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00003032 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003033 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003034 typval_T *rettv, // NULL when only checking existence
3035 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003036 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003037{
3038 int ret = OK;
3039 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003040 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02003041 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00003042 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003043 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003044
Bram Moolenaar94674f22023-01-06 18:42:20 +00003045 if (len > 0)
3046 {
3047 // truncate the name, so that we can use strcmp()
3048 cc = name[len];
3049 name[len] = NUL;
3050 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003051
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003052 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003053 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003054 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003055 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003056 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3057
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003058 if (v != NULL)
3059 {
3060 tv = &v->di_tv;
3061 if (dip != NULL)
3062 *dip = v;
3063 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003064 else
3065 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003066 }
3067
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003068 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003070 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003071 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3072
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003073 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003074 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075
3076 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003077 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003079 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003080 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003081 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003082 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003083 ht = &SCRIPT_VARS(sid);
3084 if (ht != NULL)
3085 {
3086 dictitem_T *v = find_var_in_ht(ht, 0, name,
3087 flags & EVAL_VAR_NOAUTOLOAD);
3088
3089 if (v != NULL)
3090 {
3091 tv = &v->di_tv;
3092 if (dip != NULL)
3093 *dip = v;
3094 }
3095 else
3096 ht = NULL;
3097 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003098 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003099 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003100 {
3101 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003102 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003103 ret = FAIL;
3104 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003105 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003106 else
3107 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003108 if (rettv != NULL)
3109 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003110 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003111 rettv->v_type = VAR_ANY;
3112 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3113 }
3114 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003115 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003117 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003118 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003119 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003120 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003121
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003122 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003123 // function name. For a global non-autoload function "g:" is
3124 // required.
3125 if (ufunc != NULL && (has_g_prefix
3126 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003127 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003128 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003129 if (rettv != NULL)
3130 {
3131 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003132 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003133 // Keep the "g:", otherwise script-local may be
3134 // assumed.
3135 rettv->vval.v_string = vim_strsave(name);
3136 else
3137 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003138 if (rettv->vval.v_string != NULL)
3139 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003140 }
3141 }
3142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 }
3144
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003145 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003146 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003147 if (tv == NULL)
3148 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003149 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003150 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003151 ret = FAIL;
3152 }
3153 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003154 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003155 svar_T *sv = NULL;
3156 int was_assigned = FALSE;
3157
Bram Moolenaar11005b02021-07-11 20:59:00 +02003158 if (ht != NULL && ht == get_script_local_ht()
3159 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003160 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003161 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003162 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003163 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003164 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003165 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3166 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003167 }
3168
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003169 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3170 && sid != 0)
3171 {
3172 // type alias or class imported from another script. Check
3173 // whether it is exported from the other script.
3174 sv = find_typval_in_script(tv, sid, TRUE);
3175 if (sv == NULL)
3176 {
3177 ret = FAIL;
3178 goto done;
3179 }
3180 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3181 {
3182 semsg(_(e_item_not_exported_in_script_str), name);
3183 ret = FAIL;
3184 goto done;
3185 }
3186 }
3187
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003188 // If a list or dict variable wasn't initialized and has meaningful
3189 // type, do it now. Not for global variables, they are not
3190 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003191 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003192 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003193 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003194 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003195 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003196 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003197 tv->vval.v_dict = dict_alloc();
3198 if (tv->vval.v_dict != NULL)
3199 {
3200 ++tv->vval.v_dict->dv_refcount;
3201 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003202 if (sv != NULL)
3203 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003204 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003205 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003206 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003207 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003208 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003209 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003210 tv->vval.v_list = list_alloc();
3211 if (tv->vval.v_list != NULL)
3212 {
3213 ++tv->vval.v_list->lv_refcount;
3214 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003215 if (sv != NULL)
3216 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003217 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003218 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003219 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003220 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003221 || !in_vim9script()))
3222 {
3223 tv->vval.v_blob = blob_alloc();
3224 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003225 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003226 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003227 if (sv != NULL)
3228 sv->sv_flags |= SVFLAG_ASSIGNED;
3229 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003230 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003231 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003232 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003233 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003234 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003235
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003236done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003237 if (len > 0)
3238 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003239
3240 return ret;
3241}
3242
3243/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003244 * Get the value of internal variable "name", also handling "import.name".
3245 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3246 */
3247 int
3248eval_variable_import(
3249 char_u *name,
3250 typval_T *rettv)
3251{
3252 char_u *s = name;
3253 while (ASCII_ISALNUM(*s) || *s == '_')
3254 ++s;
3255 int len = (int)(s - name);
3256
3257 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3258 return FAIL;
3259 if (rettv->v_type == VAR_ANY && *s == '.')
3260 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003261 char_u *ns = s + 1;
3262 s = ns;
3263 while (ASCII_ISALNUM(*s) || *s == '_')
3264 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003265 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003266 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003267 }
3268 return OK;
3269}
3270
3271
3272/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003273 * Check if variable "name[len]" is a local variable or an argument.
3274 * If so, "*eval_lavars_used" is set to TRUE.
3275 */
3276 void
3277check_vars(char_u *name, int len)
3278{
3279 int cc;
3280 char_u *varname;
3281 hashtab_T *ht;
3282
3283 if (eval_lavars_used == NULL)
3284 return;
3285
3286 // truncate the name, so that we can use strcmp()
3287 cc = name[len];
3288 name[len] = NUL;
3289
3290 ht = find_var_ht(name, &varname);
3291 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3292 {
3293 if (find_var(name, NULL, TRUE) != NULL)
3294 *eval_lavars_used = TRUE;
3295 }
3296
3297 name[len] = cc;
3298}
3299
3300/*
3301 * Find variable "name" in the list of variables.
3302 * Return a pointer to it if found, NULL if not found.
3303 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003304 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003305 */
3306 dictitem_T *
3307find_var(char_u *name, hashtab_T **htp, int no_autoload)
3308{
3309 char_u *varname;
3310 hashtab_T *ht;
3311 dictitem_T *ret = NULL;
3312
3313 ht = find_var_ht(name, &varname);
3314 if (htp != NULL)
3315 *htp = ht;
3316 if (ht == NULL)
3317 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003318 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003319 if (ret != NULL)
3320 return ret;
3321
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003322 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003323 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003324 if (ret != NULL)
3325 return ret;
3326
3327 // in Vim9 script items without a scope can be script-local
3328 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3329 {
3330 ht = get_script_local_ht();
3331 if (ht != NULL)
3332 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003333 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003334 if (ret != NULL)
3335 {
3336 if (htp != NULL)
3337 *htp = ht;
3338 return ret;
3339 }
3340 }
3341 }
3342
Ernie Rael84f6dc72024-04-21 14:45:48 +02003343 // and finally try
3344 return find_var_autoload_prefix(name, 0, htp, NULL);
3345}
3346
3347/*
3348 * Find variable "name" with sn_autoload_prefix.
3349 * Return a pointer to it if found, NULL if not found.
3350 * When "sid" > 0, use it otherwise use "current_sctx.sc_sid".
3351 * When "htp" is not NULL set "htp" to the hashtab_T used.
3352 * When "namep" is not NULL set "namep" to the generated name, and
3353 * then the caller gets ownership and is responsible for freeing the name.
3354 */
3355 dictitem_T *
3356find_var_autoload_prefix(char_u *name, int sid, hashtab_T **htp,
3357 char_u **namep)
3358{
3359 hashtab_T *ht;
3360 dictitem_T *ret = NULL;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003361 // When using "vim9script autoload" script-local items are prefixed but can
3362 // be used with s:name.
Ernie Rael84f6dc72024-04-21 14:45:48 +02003363 int check_sid = sid > 0 ? sid : current_sctx.sc_sid;
3364 if (SCRIPT_ID_VALID(check_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003365 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003366 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003367 scriptitem_T *si = SCRIPT_ITEM(check_sid);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003368
3369 if (si->sn_autoload_prefix != NULL)
3370 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003371 char_u *base_name = (name[0] == 's' && name[1] == ':')
3372 ? name + 2 : name;
3373 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003374
3375 if (auto_name != NULL)
3376 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003377 int free_auto_name = TRUE;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003378 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003379 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003380 if (ret != NULL)
3381 {
3382 if (htp != NULL)
3383 *htp = ht;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003384 if (namep != NULL)
3385 {
3386 free_auto_name = FALSE;
3387 *namep = auto_name;
3388 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003389 }
Ernie Rael84f6dc72024-04-21 14:45:48 +02003390 if (free_auto_name)
3391 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003392 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003393 }
3394 }
3395
Ernie Rael84f6dc72024-04-21 14:45:48 +02003396 return ret;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003397}
3398
3399/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003400 * Like find_var() but if the name starts with <SNR>99_ then look in the
3401 * referenced script (used for a funcref).
3402 */
3403 dictitem_T *
3404find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3405{
Keith Thompson184f71c2024-01-04 21:19:04 +01003406 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003407 {
3408 char_u *p = name + 5;
3409 int sid = getdigits(&p);
3410
3411 if (SCRIPT_ID_VALID(sid) && *p == '_')
3412 {
3413 hashtab_T *ht = &SCRIPT_VARS(sid);
3414
3415 if (ht != NULL)
3416 {
3417 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3418
3419 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003420 {
3421 if (htp != NULL)
3422 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003423 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003424 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003425 }
3426 }
3427 }
3428
3429 return find_var(name, htp, no_autoload);
3430}
3431
3432/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003433 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003434 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003435 * Returns NULL if not found.
3436 */
3437 dictitem_T *
3438find_var_in_ht(
3439 hashtab_T *ht,
3440 int htname,
3441 char_u *varname,
3442 int no_autoload)
3443{
3444 hashitem_T *hi;
3445
3446 if (*varname == NUL)
3447 {
3448 // Must be something like "s:", otherwise "ht" would be NULL.
3449 switch (htname)
3450 {
3451 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3452 case 'g': return &globvars_var;
3453 case 'v': return &vimvars_var;
3454 case 'b': return &curbuf->b_bufvar;
3455 case 'w': return &curwin->w_winvar;
3456 case 't': return &curtab->tp_winvar;
3457 case 'l': return get_funccal_local_var();
3458 case 'a': return get_funccal_args_var();
3459 }
3460 return NULL;
3461 }
3462
3463 hi = hash_find(ht, varname);
3464 if (HASHITEM_EMPTY(hi))
3465 {
3466 // For global variables we may try auto-loading the script. If it
3467 // worked find the variable again. Don't auto-load a script if it was
3468 // loaded already, otherwise it would be loaded every time when
3469 // checking if a function name is a Funcref variable.
3470 if (ht == &globvarht && !no_autoload)
3471 {
3472 // Note: script_autoload() may make "hi" invalid. It must either
3473 // be obtained again or not used.
3474 if (!script_autoload(varname, FALSE) || aborting())
3475 return NULL;
3476 hi = hash_find(ht, varname);
3477 }
3478 if (HASHITEM_EMPTY(hi))
3479 return NULL;
3480 }
3481 return HI2DI(hi);
3482}
3483
3484/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 * Get the script-local hashtab. NULL if not in a script context.
3486 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003487 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488get_script_local_ht(void)
3489{
3490 scid_T sid = current_sctx.sc_sid;
3491
Bram Moolenaare3d46852020-08-29 13:39:17 +02003492 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 return &SCRIPT_VARS(sid);
3494 return NULL;
3495}
3496
3497/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003498 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003499 * When "cmd" is TRUE it must look like a command, a function must be followed
3500 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003501 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003502 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003503 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003504lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003505 char_u *name,
3506 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003507 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003508 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509{
3510 hashtab_T *ht = get_script_local_ht();
3511 char_u buffer[30];
3512 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003513 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003515 int is_global = FALSE;
3516 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517
3518 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003519 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 if (len < sizeof(buffer) - 1)
3521 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003522 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 vim_strncpy(buffer, name, len);
3524 p = buffer;
3525 }
3526 else
3527 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003528 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003530 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 }
3532
3533 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003534 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535
Ernie Rael84f6dc72024-04-21 14:45:48 +02003536 // if not script-local, then perhaps autoload-exported
3537 if (res == FAIL && find_var_autoload_prefix(p, 0, NULL, NULL) != NULL)
3538 res = OK;
3539
3540 // if not script-local or autoload, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003541 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003542 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 if (p != buffer)
3544 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003545
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003546 // Find a function, so that a following "->" works.
3547 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3548 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003549 if (res != OK)
3550 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003551 p = skipwhite(name + len);
3552
3553 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003554 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003555 // Do not check for an internal function, since it might also be a
3556 // valid command, such as ":split" versus "split()".
3557 // Skip "g:" before a function name.
3558 if (name[0] == 'g' && name[1] == ':')
3559 {
3560 is_global = TRUE;
3561 fname = name + 2;
3562 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003563 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003564 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003565 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003566 }
3567
Bram Moolenaar709664c2020-12-12 14:33:41 +01003568 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003569}
3570
3571/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003572 * Find the hashtab used for a variable name.
3573 * Return NULL if the name is not valid.
3574 * Set "varname" to the start of name without ':'.
3575 */
3576 hashtab_T *
3577find_var_ht(char_u *name, char_u **varname)
3578{
3579 hashitem_T *hi;
3580 hashtab_T *ht;
3581
3582 if (name[0] == NUL)
3583 return NULL;
3584 if (name[1] != ':')
3585 {
3586 // The name must not start with a colon or #.
3587 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3588 return NULL;
3589 *varname = name;
3590
3591 // "version" is "v:version" in all scopes if scriptversion < 3.
3592 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003593 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003594 {
3595 hi = hash_find(&compat_hashtab, name);
3596 if (!HASHITEM_EMPTY(hi))
3597 return &compat_hashtab;
3598 }
3599
3600 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 if (ht != NULL)
3602 return ht; // local variable
3603
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003604 // In Vim9 script items at the script level are script-local, except
3605 // for autoload names.
3606 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 {
3608 ht = get_script_local_ht();
3609 if (ht != NULL)
3610 return ht;
3611 }
3612
3613 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003614 }
3615 *varname = name + 2;
3616 if (*name == 'g') // global variable
3617 return &globvarht;
3618 // There must be no ':' or '#' in the rest of the name, unless g: is used
3619 if (vim_strchr(name + 2, ':') != NULL
3620 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3621 return NULL;
3622 if (*name == 'b') // buffer variable
3623 return &curbuf->b_vars->dv_hashtab;
3624 if (*name == 'w') // window variable
3625 return &curwin->w_vars->dv_hashtab;
3626 if (*name == 't') // tab page variable
3627 return &curtab->tp_vars->dv_hashtab;
3628 if (*name == 'v') // v: variable
3629 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003630 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003631 && get_current_funccal()->fc_func->uf_def_status
3632 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003634 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 if (*name == 'a') // a: function argument
3636 return get_funccal_args_ht();
3637 if (*name == 'l') // l: local function variable
3638 return get_funccal_local_ht();
3639 }
3640 if (*name == 's') // script variable
3641 {
3642 ht = get_script_local_ht();
3643 if (ht != NULL)
3644 return ht;
3645 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003646 return NULL;
3647}
3648
3649/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003650 * Get the string value of a (global/local) variable.
3651 * Note: see tv_get_string() for how long the pointer remains valid.
3652 * Returns NULL when it doesn't exist.
3653 */
3654 char_u *
3655get_var_value(char_u *name)
3656{
3657 dictitem_T *v;
3658
3659 v = find_var(name, NULL, FALSE);
3660 if (v == NULL)
3661 return NULL;
3662 return tv_get_string(&v->di_tv);
3663}
3664
3665/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003666 * Allocate a new hashtab for a sourced script. It will be used while
3667 * sourcing this script and when executing functions defined in the script.
3668 */
3669 void
3670new_script_vars(scid_T id)
3671{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003672 scriptvar_T *sv;
3673
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003674 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3675 if (sv == NULL)
3676 return;
3677 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003678 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003679}
3680
3681/*
3682 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3683 * point to it.
3684 */
3685 void
3686init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3687{
3688 hash_init(&dict->dv_hashtab);
3689 dict->dv_lock = 0;
3690 dict->dv_scope = scope;
3691 dict->dv_refcount = DO_NOT_FREE_CNT;
3692 dict->dv_copyID = 0;
3693 dict_var->di_tv.vval.v_dict = dict;
3694 dict_var->di_tv.v_type = VAR_DICT;
3695 dict_var->di_tv.v_lock = VAR_FIXED;
3696 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3697 dict_var->di_key[0] = NUL;
3698}
3699
3700/*
3701 * Unreference a dictionary initialized by init_var_dict().
3702 */
3703 void
3704unref_var_dict(dict_T *dict)
3705{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003706 // Now the dict needs to be freed if no one else is using it, go back to
3707 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003708 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3709 dict_unref(dict);
3710}
3711
3712/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003713 * Clean up a list of internal variables.
3714 * Frees all allocated variables and the value they contain.
3715 * Clears hashtab "ht", does not free it.
3716 */
3717 void
3718vars_clear(hashtab_T *ht)
3719{
3720 vars_clear_ext(ht, TRUE);
3721}
3722
3723/*
3724 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3725 */
3726 void
3727vars_clear_ext(hashtab_T *ht, int free_val)
3728{
3729 int todo;
3730 hashitem_T *hi;
3731 dictitem_T *v;
3732
3733 hash_lock(ht);
3734 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003735 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003736 {
3737 if (!HASHITEM_EMPTY(hi))
3738 {
3739 --todo;
3740
3741 // Free the variable. Don't remove it from the hashtab,
3742 // ht_array might change then. hash_clear() takes care of it
3743 // later.
3744 v = HI2DI(hi);
3745 if (free_val)
3746 clear_tv(&v->di_tv);
3747 if (v->di_flags & DI_FLAGS_ALLOC)
3748 vim_free(v);
3749 }
3750 }
3751 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003752 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003753}
3754
3755/*
3756 * Delete a variable from hashtab "ht" at item "hi".
3757 * Clear the variable value and free the dictitem.
3758 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003759 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003760delete_var(hashtab_T *ht, hashitem_T *hi)
3761{
3762 dictitem_T *di = HI2DI(hi);
3763
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003764 if (hash_remove(ht, hi, "delete variable") != OK)
3765 return;
3766
3767 clear_tv(&di->di_tv);
3768 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003769}
3770
3771/*
3772 * List the value of one internal variable.
3773 */
3774 static void
3775list_one_var(dictitem_T *v, char *prefix, int *first)
3776{
3777 char_u *tofree;
3778 char_u *s;
3779 char_u numbuf[NUMBUFLEN];
3780
3781 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3782 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3783 s == NULL ? (char_u *)"" : s, first);
3784 vim_free(tofree);
3785}
3786
3787 static void
3788list_one_var_a(
3789 char *prefix,
3790 char_u *name,
3791 int type,
3792 char_u *string,
3793 int *first) // when TRUE clear rest of screen and set to FALSE
3794{
3795 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3796 msg_start();
3797 msg_puts(prefix);
3798 if (name != NULL) // "a:" vars don't have a name stored
3799 msg_puts((char *)name);
3800 msg_putchar(' ');
3801 msg_advance(22);
3802 if (type == VAR_NUMBER)
3803 msg_putchar('#');
3804 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3805 msg_putchar('*');
3806 else if (type == VAR_LIST)
3807 {
3808 msg_putchar('[');
3809 if (*string == '[')
3810 ++string;
3811 }
3812 else if (type == VAR_DICT)
3813 {
3814 msg_putchar('{');
3815 if (*string == '{')
3816 ++string;
3817 }
3818 else
3819 msg_putchar(' ');
3820
3821 msg_outtrans(string);
3822
3823 if (type == VAR_FUNC || type == VAR_PARTIAL)
3824 msg_puts("()");
3825 if (*first)
3826 {
3827 msg_clr_eos();
3828 *first = FALSE;
3829 }
3830}
3831
3832/*
zeertzjqedcba962023-09-24 23:13:51 +02003833 * Addition handling for setting a v: variable.
3834 * Return TRUE if the variable should be set normally,
3835 * FALSE if nothing else needs to be done.
3836 */
3837 int
3838before_set_vvar(
3839 char_u *varname,
3840 dictitem_T *di,
3841 typval_T *tv,
3842 int copy,
3843 int *type_error)
3844{
3845 if (di->di_tv.v_type == VAR_STRING)
3846 {
3847 VIM_CLEAR(di->di_tv.vval.v_string);
3848 if (copy || tv->v_type != VAR_STRING)
3849 {
3850 char_u *val = tv_get_string(tv);
3851
3852 // Careful: when assigning to v:errmsg and
3853 // tv_get_string() causes an error message the variable
3854 // will already be set.
3855 if (di->di_tv.vval.v_string == NULL)
3856 di->di_tv.vval.v_string = vim_strsave(val);
3857 }
3858 else
3859 {
3860 // Take over the string to avoid an extra alloc/free.
3861 di->di_tv.vval.v_string = tv->vval.v_string;
3862 tv->vval.v_string = NULL;
3863 }
3864 return FALSE;
3865 }
3866 else if (di->di_tv.v_type == VAR_NUMBER)
3867 {
3868 di->di_tv.vval.v_number = tv_get_number(tv);
3869 if (STRCMP(varname, "searchforward") == 0)
3870 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3871#ifdef FEAT_SEARCH_EXTRA
3872 else if (STRCMP(varname, "hlsearch") == 0)
3873 {
3874 no_hlsearch = !di->di_tv.vval.v_number;
3875 redraw_all_later(UPD_SOME_VALID);
3876 }
3877#endif
3878 return FALSE;
3879 }
3880 else if (di->di_tv.v_type != tv->v_type)
3881 {
3882 *type_error = TRUE;
3883 return FALSE;
3884 }
3885 return TRUE;
3886}
3887
3888/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003889 * Set variable "name" to value in "tv".
3890 * If the variable already exists, the value is updated.
3891 * Otherwise the variable is created.
3892 */
3893 void
3894set_var(
3895 char_u *name,
3896 typval_T *tv,
3897 int copy) // make copy of value in "tv"
3898{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003899 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003900}
3901
3902/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003903 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003904 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003905 * If the variable already exists and "is_const" is FALSE the value is updated.
3906 * Otherwise the variable is created.
3907 */
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003908 int
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003909set_var_const(
3910 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003911 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003912 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003913 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003914 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003915 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003916 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003917{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003918 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003919 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003920 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003922 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003923 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003924 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003925 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003927 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003928 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003929 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003930 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003931 int free_tv_arg = !copy; // free tv_arg if not used
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003932 int rc = FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003933
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003934 if (sid != 0)
3935 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003936 varname = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003937 if (SCRIPT_ID_VALID(sid))
Ernie Rael84f6dc72024-04-21 14:45:48 +02003938 {
3939 char_u *auto_name = NULL;
3940 if (find_var_autoload_prefix(name, sid, &ht, &auto_name) != NULL)
3941 {
3942 var_in_autoload = TRUE;
3943 varname = auto_name;
3944 name_tofree = varname;
3945 }
3946 else
3947 ht = &SCRIPT_VARS(sid);
3948 }
3949 if (varname == NULL)
3950 varname = name;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003951 }
3952 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003953 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003954 scriptitem_T *si;
3955 char_u *auto_name = NULL;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003956
Ernie Rael84f6dc72024-04-21 14:45:48 +02003957 if (in_vim9script()
3958 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3959 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3960 ->sn_autoload_prefix != NULL
3961 && (is_export
3962 || find_var_autoload_prefix(name, 0, NULL, &auto_name)
3963 != NULL))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003964 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003965 // In a vim9 autoload script an exported variable is put in the
3966 // global namespace with the autoload prefix.
3967 var_in_autoload = TRUE;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003968 varname = auto_name != NULL ? auto_name
3969 : concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003970 if (varname == NULL)
3971 goto failed;
3972 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003973 ht = &globvarht;
3974 }
3975 else
3976 ht = find_var_ht(name, &varname);
3977 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003978 if (ht == NULL || *varname == NUL)
3979 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003980 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003981 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003982 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003983 is_script_local = ht == get_script_local_ht() || sid != 0
3984 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003985
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003986 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003987 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003988 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003989 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003990 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003991 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003992 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003993 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003994 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003995 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003996 // Do not make g:var, w:var, b:var or t:var final.
3997 flags &= ~ASSIGN_FINAL;
3998
Bram Moolenaare535db82021-03-31 21:07:24 +02003999 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004000 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
4001 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02004002 // For "[a, _] = list" the underscore is ignored.
4003 if ((flags & ASSIGN_UNPACK) == 0)
4004 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004005 goto failed;
4006 }
Bram Moolenaar67979662020-06-20 22:50:47 +02004007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004009
Bram Moolenaar24e93162021-07-18 20:40:33 +02004010 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004011 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004012 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004013
Bram Moolenaar24e93162021-07-18 20:40:33 +02004014 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004015 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004016 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02004017 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004018 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004019 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004020 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004022 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
4023 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004024 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00004025 if (!in_vim9script())
4026 {
4027 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
4028 name);
4029 goto failed;
4030 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004031 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032
Bram Moolenaar993faa32022-02-21 15:59:11 +00004033 // Search in parent scope which is possible to reference from lambda
4034 if (di == NULL)
4035 di = find_var_in_scoped_ht(name, TRUE);
4036
4037 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
4038 && var_wrong_func_name(name, di == NULL))
4039 goto failed;
4040
4041 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004042 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004043 // Destination is a bool and the value is not, but it can be
4044 // converted.
4045 CLEAR_FIELD(bool_tv);
4046 bool_tv.v_type = VAR_BOOL;
4047 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
4048 tv = &bool_tv;
4049 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004050
Bram Moolenaar993faa32022-02-21 15:59:11 +00004051 if (di != NULL)
4052 {
4053 // Item already exists. Allowed to replace when reloading.
4054 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004055 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004056 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
4057 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004058 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004059 emsg(_(e_cannot_modify_existing_variable));
4060 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004061 }
4062
Bram Moolenaar993faa32022-02-21 15:59:11 +00004063 if (is_script_local && vim9script
4064 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02004065 {
4066 semsg(_(e_redefining_script_item_str), name);
4067 goto failed;
4068 }
4069
Ernie Raele75fde62023-12-21 17:18:54 +01004070 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004071 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004072
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004073 // List and Blob types can be modified in-place using the "+="
4074 // compound operator. For other types, this is not allowed.
4075 int type_inplace_modifiable =
4076 (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
4077
4078 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
4079 && ((flags & ASSIGN_COMPOUND_OP) == 0
4080 || !type_inplace_modifiable))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004081 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004082 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01004083 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004084
4085 if (sv != NULL)
4086 {
4087 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02004088 if (var_idx > 0)
4089 {
4090 where.wt_index = var_idx;
4091 where.wt_kind = WT_VARIABLE;
4092 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004093 if (check_script_var_type(sv, tv, name, where) == FAIL)
4094 goto failed;
4095 if (type == NULL)
4096 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004097 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004098 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004099 }
4100
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004101 // Modifying a final variable with a List value using the "+="
4102 // operator is allowed. For other types, it is not allowed.
zeertzjq6b97d7a2024-08-08 21:05:57 +02004103 if ((((flags & ASSIGN_FOR_LOOP) == 0 || (flags & ASSIGN_DECL) == 0)
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004104 && ((flags & ASSIGN_COMPOUND_OP) == 0
4105 || !type_inplace_modifiable))
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004106 ? var_check_permission(di, name) == FAIL
4107 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004108 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004109 }
4110 else
4111 {
4112 // can only redefine once
4113 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004114
Bram Moolenaar993faa32022-02-21 15:59:11 +00004115 // A Vim9 script-local variable is also present in sn_all_vars
4116 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004117 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004118 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004119 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004120 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004121 }
4122
Bram Moolenaar993faa32022-02-21 15:59:11 +00004123 // existing variable, need to clear the value
4124
zeertzjqedcba962023-09-24 23:13:51 +02004125 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004126 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004127 int type_error = FALSE;
4128 if (ht == &vimvarht
4129 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004130 {
zeertzjqedcba962023-09-24 23:13:51 +02004131 if (type_error)
4132 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4133 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004134 }
4135
4136 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004137
4138 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4139 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4140 {
4141 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4142
4143 update_script_var_block_id(name, si->sn_current_block_id);
4144 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004145 }
4146 else
4147 {
4148 // Item not found, check if a function already exists.
4149 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4150 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4151 {
4152 semsg(_(e_redefining_script_item_str), name);
4153 goto failed;
4154 }
4155
4156 // add a new variable
4157 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4158 {
4159 semsg(_(e_unknown_variable_str), name);
4160 goto failed;
4161 }
4162
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004163 if (check_hashtab_frozen(ht, "add variable"))
4164 goto failed;
4165
Bram Moolenaar993faa32022-02-21 15:59:11 +00004166 // Can't add "v:" or "a:" variable.
4167 if (ht == &vimvarht || ht == get_funccal_args_ht())
4168 {
4169 semsg(_(e_illegal_variable_name_str), name);
4170 goto failed;
4171 }
4172
4173 // Make sure the variable name is valid. In Vim9 script an
4174 // autoload variable must be prefixed with "g:" unless in an
4175 // autoload script.
4176 if (!valid_varname(varname, -1, !vim9script
4177 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4178 goto failed;
4179
zeertzjq1b438a82023-02-01 13:11:15 +00004180 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004181 if (di == NULL)
4182 goto failed;
4183 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004184 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004185 {
4186 vim_free(di);
4187 goto failed;
4188 }
4189 di->di_flags = DI_FLAGS_ALLOC;
4190 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4191 di->di_flags |= DI_FLAGS_LOCK;
4192
4193 // A Vim9 script-local variable is also added to sn_all_vars and
4194 // sn_var_vals. It may set "type" from "tv".
4195 if (var_in_vim9script || var_in_autoload)
4196 update_vim9_script_var(TRUE, di,
4197 var_in_autoload ? name : di->di_key, flags,
4198 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004199 }
4200
Bram Moolenaar993faa32022-02-21 15:59:11 +00004201 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004202 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004203 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004204 else
4205 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004206 *dest_tv = *tv;
4207 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004208 init_tv(tv);
4209 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004210 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004211
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004212 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004213 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004214
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004215 // ":const var = value" locks the value
4216 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004217 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004218 // Like :lockvar! name: lock the value and what it contains, but only
4219 // if the reference count is up to one. That locks only literal
4220 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004221 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004222
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004223 rc = OK;
4224
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004225failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004226 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004227 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004228 clear_tv(tv_arg);
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004229
4230 return rc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004231}
4232
4233/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004234 * Check in this order for backwards compatibility:
4235 * - Whether the variable is read-only
4236 * - Whether the variable value is locked
4237 * - Whether the variable is locked
Ernie Rael3f821d62024-04-24 20:07:50 +02004238 * NOTE: "name" is only used for error messages.
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004239 */
4240 int
4241var_check_permission(dictitem_T *di, char_u *name)
4242{
4243 if (var_check_ro(di->di_flags, name, FALSE)
4244 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4245 || var_check_lock(di->di_flags, name, FALSE))
4246 return FAIL;
4247 return OK;
4248}
4249
4250/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004251 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4252 * Also give an error message.
4253 */
4254 int
4255var_check_ro(int flags, char_u *name, int use_gettext)
4256{
4257 if (flags & DI_FLAGS_RO)
4258 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004259 if (name == NULL)
4260 emsg(_(e_cannot_change_readonly_variable));
4261 else
4262 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004263 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004264 return TRUE;
4265 }
4266 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4267 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004268 if (name == NULL)
4269 emsg(_(e_cannot_set_variable_in_sandbox));
4270 else
4271 semsg(_(e_cannot_set_variable_in_sandbox_str),
4272 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004273 return TRUE;
4274 }
4275 return FALSE;
4276}
4277
4278/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004279 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4280 * Also give an error message.
4281 */
4282 int
4283var_check_lock(int flags, char_u *name, int use_gettext)
4284{
4285 if (flags & DI_FLAGS_LOCK)
4286 {
4287 semsg(_(e_variable_is_locked_str),
4288 use_gettext ? (char_u *)_(name) : name);
4289 return TRUE;
4290 }
4291 return FALSE;
4292}
4293
4294/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004295 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4296 * Also give an error message.
4297 */
4298 int
4299var_check_fixed(int flags, char_u *name, int use_gettext)
4300{
4301 if (flags & DI_FLAGS_FIX)
4302 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004303 if (name == NULL)
4304 emsg(_(e_cannot_delete_variable));
4305 else
4306 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004307 use_gettext ? (char_u *)_(name) : name);
4308 return TRUE;
4309 }
4310 return FALSE;
4311}
4312
4313/*
4314 * Check if a funcref is assigned to a valid variable name.
4315 * Return TRUE and give an error if not.
4316 */
4317 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004318var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004319 char_u *name, // points to start of variable name
4320 int new_var) // TRUE when creating the variable
4321{
Bram Moolenaar32154662021-03-28 21:14:06 +02004322 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4323 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004324 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004325 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4326 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004327 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004328 ? name[2] : name[0])
4329 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004330 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004331 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004332 return TRUE;
4333 }
4334 // Don't allow hiding a function. When "v" is not NULL we might be
4335 // assigning another function to the same var, the type is checked
4336 // below.
4337 if (new_var && function_exists(name, FALSE))
4338 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004339 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004340 name);
4341 return TRUE;
4342 }
4343 return FALSE;
4344}
4345
4346/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004347 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4348 * value. Also give an error message, using "name" or _("name") when
4349 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004350 */
4351 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004352value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004353{
4354 if (lock & VAR_LOCKED)
4355 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004356 if (name == NULL)
4357 emsg(_(e_value_is_locked));
4358 else
4359 semsg(_(e_value_is_locked_str),
4360 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004361 return TRUE;
4362 }
4363 if (lock & VAR_FIXED)
4364 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004365 if (name == NULL)
4366 emsg(_(e_cannot_change_value));
4367 else
4368 semsg(_(e_cannot_change_value_of_str),
4369 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004370 return TRUE;
4371 }
4372 return FALSE;
4373}
4374
4375/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004376 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004377 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004378 * Return FALSE and give an error if not.
4379 */
4380 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004381valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004382{
4383 char_u *p;
4384
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004385 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004386 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004387 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004388 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004389 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004390 return FALSE;
4391 }
4392 return TRUE;
4393}
4394
4395/*
LemonBoy47d4e312022-05-04 18:12:55 +01004396 * Implements the logic to retrieve local variable and option values.
4397 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004398 */
4399 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004400get_var_from(
4401 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004402 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004403 typval_T *deftv, // Default value if not found.
4404 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4405 tabpage_T *tp, // can be NULL
4406 win_T *win,
4407 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004408{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004409 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004410 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004411 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004412 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004413 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004414
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004415 ++emsg_off;
4416
4417 rettv->v_type = VAR_STRING;
4418 rettv->vval.v_string = NULL;
4419
LemonBoy47d4e312022-05-04 18:12:55 +01004420 if (varname != NULL && tp != NULL && win != NULL
4421 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004422 {
4423 // Set curwin to be our win, temporarily. Also set the tabpage,
4424 // otherwise the window is not valid. Only do this when needed,
4425 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004426 // If we have a buffer reference avoid the switching, we're saving and
4427 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004428 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004429 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004430 {
LemonBoy47d4e312022-05-04 18:12:55 +01004431 // Handle options. There are no tab-local options.
4432 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004433 {
LemonBoy47d4e312022-05-04 18:12:55 +01004434 buf_T *save_curbuf = curbuf;
4435
4436 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004437 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004438 curbuf = buf;
4439
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004440 if (varname[1] == NUL)
4441 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004442 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004443 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004444
4445 if (opts != NULL)
4446 {
4447 rettv_dict_set(rettv, opts);
4448 done = TRUE;
4449 }
4450 }
LemonBoy47d4e312022-05-04 18:12:55 +01004451 else if (eval_option(&varname, rettv, TRUE) == OK)
4452 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004453 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004454
4455 curbuf = save_curbuf;
4456 }
4457 else if (*varname == NUL)
4458 {
4459 // Empty string: return a dict with all the local variables.
4460 if (htname == 'b')
4461 v = &buf->b_bufvar;
4462 else if (htname == 'w')
4463 v = &win->w_winvar;
4464 else
4465 v = &tp->tp_winvar;
4466 copy_tv(&v->di_tv, rettv);
4467 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004468 }
4469 else
4470 {
LemonBoy47d4e312022-05-04 18:12:55 +01004471 hashtab_T *ht;
4472
4473 if (htname == 'b')
4474 ht = &buf->b_vars->dv_hashtab;
4475 else if (htname == 'w')
4476 ht = &win->w_vars->dv_hashtab;
4477 else
4478 ht = &tp->tp_vars->dv_hashtab;
4479
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004480 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004481 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004482 if (v != NULL)
4483 {
4484 copy_tv(&v->di_tv, rettv);
4485 done = TRUE;
4486 }
4487 }
4488 }
4489
4490 if (need_switch_win)
4491 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004492 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004493 }
4494
LemonBoy47d4e312022-05-04 18:12:55 +01004495 if (!done && deftv->v_type != VAR_UNKNOWN)
4496 // use the default value
4497 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004498
4499 --emsg_off;
4500}
4501
4502/*
LemonBoy47d4e312022-05-04 18:12:55 +01004503 * getwinvar() and gettabwinvar()
4504 */
4505 static void
4506getwinvar(
4507 typval_T *argvars,
4508 typval_T *rettv,
4509 int off) // 1 for gettabwinvar()
4510{
4511 char_u *varname;
4512 tabpage_T *tp;
4513 win_T *win;
4514
4515 if (off == 1)
4516 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4517 else
4518 tp = curtab;
4519 win = find_win_by_nr(&argvars[off], tp);
4520 varname = tv_get_string_chk(&argvars[off + 1]);
4521
4522 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4523}
4524
4525/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004526 * Set option "varname" to the value of "varp" for the current buffer/window.
4527 */
4528 static void
4529set_option_from_tv(char_u *varname, typval_T *varp)
4530{
4531 long numval = 0;
4532 char_u *strval;
4533 char_u nbuf[NUMBUFLEN];
4534 int error = FALSE;
4535
zeertzjq4c7cb372023-06-14 16:39:54 +01004536 int opt_idx = findoption(varname);
4537 if (opt_idx < 0)
4538 {
4539 semsg(_(e_unknown_option_str_2), varname);
4540 return;
4541 }
4542 int opt_p_flags = get_option_flags(opt_idx);
4543
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004544 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004545 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004546 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004547 {
4548 emsg(_(e_string_required));
4549 return;
4550 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004551 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004552 strval = (char_u *)"0"; // avoid using "false"
4553 }
4554 else
4555 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004556 if ((opt_p_flags & (P_NUM|P_BOOL))
4557 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004558 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004559 if (!error)
4560 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004561 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004562 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004563 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004564}
4565
4566/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004567 * "setwinvar()" and "settabwinvar()" functions
4568 */
4569 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004570setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004571{
4572 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004573 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004574 int need_switch_win;
4575 char_u *varname, *winvarname;
4576 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004577 tabpage_T *tp = NULL;
4578
4579 if (check_secure())
4580 return;
4581
4582 if (off == 1)
4583 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4584 else
4585 tp = curtab;
4586 win = find_win_by_nr(&argvars[off], tp);
4587 varname = tv_get_string_chk(&argvars[off + 1]);
4588 varp = &argvars[off + 2];
4589
zeertzjqea720ae2023-01-03 10:54:09 +00004590 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004591 return;
4592
4593 need_switch_win = !(tp == curtab && win == curwin);
4594 if (!need_switch_win
4595 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004596 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004597 if (*varname == '&')
4598 set_option_from_tv(varname + 1, varp);
4599 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004600 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004601 winvarname = alloc(STRLEN(varname) + 3);
4602 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004603 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004604 STRCPY(winvarname, "w:");
4605 STRCPY(winvarname + 2, varname);
4606 set_var(winvarname, varp, TRUE);
4607 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004608 }
4609 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004610 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004611 if (need_switch_win)
4612 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004613}
4614
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004615/*
4616 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4617 * v:option_type, and v:option_command.
4618 */
4619 void
4620reset_v_option_vars(void)
4621{
4622 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4623 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4624 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4625 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4626 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4627 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4628}
4629
4630/*
4631 * Add an assert error to v:errors.
4632 */
4633 void
4634assert_error(garray_T *gap)
4635{
4636 struct vimvar *vp = &vimvars[VV_ERRORS];
4637
Bram Moolenaard787e402021-12-24 21:36:12 +00004638 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004639 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004640 set_vim_var_list(VV_ERRORS, list_alloc());
4641 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4642}
4643
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004644 int
4645var_exists(char_u *var)
4646{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004647 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004648 char_u *name;
4649 char_u *tofree;
4650 typval_T tv;
4651 int len = 0;
4652 int n = FALSE;
4653
4654 // get_name_len() takes care of expanding curly braces
4655 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004656 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004657 if (len > 0)
4658 {
4659 if (tofree != NULL)
4660 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004661 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004662 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004663 if (n)
4664 {
4665 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004666 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004667 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4668 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004669 if (n)
4670 clear_tv(&tv);
4671 }
4672 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004673 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004674 n = FALSE;
4675
4676 vim_free(tofree);
4677 return n;
4678}
4679
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004680static lval_T *redir_lval = NULL;
4681#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4682static garray_T redir_ga; // only valid when redir_lval is not NULL
4683static char_u *redir_endp = NULL;
4684static char_u *redir_varname = NULL;
4685
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004686 int
4687alloc_redir_lval(void)
4688{
4689 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4690 if (redir_lval == NULL)
4691 return FAIL;
4692 return OK;
4693}
4694
4695 void
4696clear_redir_lval(void)
4697{
4698 VIM_CLEAR(redir_lval);
4699}
4700
4701 void
4702init_redir_ga(void)
4703{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004704 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004705}
4706
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004707/*
4708 * Start recording command output to a variable
4709 * When "append" is TRUE append to an existing variable.
4710 * Returns OK if successfully completed the setup. FAIL otherwise.
4711 */
4712 int
4713var_redir_start(char_u *name, int append)
4714{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004715 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004716 typval_T tv;
4717
4718 // Catch a bad name early.
4719 if (!eval_isnamec1(*name))
4720 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004721 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004722 return FAIL;
4723 }
4724
4725 // Make a copy of the name, it is used in redir_lval until redir ends.
4726 redir_varname = vim_strsave(name);
4727 if (redir_varname == NULL)
4728 return FAIL;
4729
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004730 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004731 {
4732 var_redir_stop();
4733 return FAIL;
4734 }
4735
4736 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004737 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004738
4739 // Parse the variable name (can be a dict or list entry).
4740 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4741 FNE_CHECK_START);
4742 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4743 {
4744 clear_lval(redir_lval);
4745 if (redir_endp != NULL && *redir_endp != NUL)
4746 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004747 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004748 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004749 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004750 redir_endp = NULL; // don't store a value, only cleanup
4751 var_redir_stop();
4752 return FAIL;
4753 }
4754
4755 // check if we can write to the variable: set it to or append an empty
4756 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004757 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004758 tv.v_type = VAR_STRING;
4759 tv.vval.v_string = (char_u *)"";
4760 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004761 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004762 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004763 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004764 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004765 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004766 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004767 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004768 {
4769 redir_endp = NULL; // don't store a value, only cleanup
4770 var_redir_stop();
4771 return FAIL;
4772 }
4773
4774 return OK;
4775}
4776
4777/*
4778 * Append "value[value_len]" to the variable set by var_redir_start().
4779 * The actual appending is postponed until redirection ends, because the value
4780 * appended may in fact be the string we write to, changing it may cause freed
4781 * memory to be used:
4782 * :redir => foo
4783 * :let foo
4784 * :redir END
4785 */
4786 void
4787var_redir_str(char_u *value, int value_len)
4788{
4789 int len;
4790
4791 if (redir_lval == NULL)
4792 return;
4793
4794 if (value_len == -1)
4795 len = (int)STRLEN(value); // Append the entire string
4796 else
4797 len = value_len; // Append only "value_len" characters
4798
4799 if (ga_grow(&redir_ga, len) == OK)
4800 {
4801 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4802 redir_ga.ga_len += len;
4803 }
4804 else
4805 var_redir_stop();
4806}
4807
4808/*
4809 * Stop redirecting command output to a variable.
4810 * Frees the allocated memory.
4811 */
4812 void
4813var_redir_stop(void)
4814{
4815 typval_T tv;
4816
4817 if (EVALCMD_BUSY)
4818 {
4819 redir_lval = NULL;
4820 return;
4821 }
4822
4823 if (redir_lval != NULL)
4824 {
4825 // If there was no error: assign the text to the variable.
4826 if (redir_endp != NULL)
4827 {
4828 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4829 tv.v_type = VAR_STRING;
4830 tv.vval.v_string = redir_ga.ga_data;
4831 // Call get_lval() again, if it's inside a Dict or List it may
4832 // have changed.
4833 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4834 FALSE, FALSE, 0, FNE_CHECK_START);
4835 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004836 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004837 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004838 clear_lval(redir_lval);
4839 }
4840
4841 // free the collected output
4842 VIM_CLEAR(redir_ga.ga_data);
4843
4844 VIM_CLEAR(redir_lval);
4845 }
4846 VIM_CLEAR(redir_varname);
4847}
4848
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004849/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004850 * Get the collected redirected text and clear redir_ga.
4851 */
4852 char_u *
4853get_clear_redir_ga(void)
4854{
4855 char_u *res;
4856
4857 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4858 res = redir_ga.ga_data;
4859 redir_ga.ga_data = NULL;
4860 return res;
4861}
4862
4863/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004864 * "gettabvar()" function
4865 */
4866 void
4867f_gettabvar(typval_T *argvars, typval_T *rettv)
4868{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004869 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004870 tabpage_T *tp;
4871 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004872
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004873 if (in_vim9script()
4874 && (check_for_number_arg(argvars, 0) == FAIL
4875 || check_for_string_arg(argvars, 1) == FAIL))
4876 return;
4877
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004878 varname = tv_get_string_chk(&argvars[1]);
4879 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004880 if (tp != NULL)
4881 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4882 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004883
LemonBoy47d4e312022-05-04 18:12:55 +01004884 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004885}
4886
4887/*
4888 * "gettabwinvar()" function
4889 */
4890 void
4891f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4892{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004893 if (in_vim9script()
4894 && (check_for_number_arg(argvars, 0) == FAIL
4895 || check_for_number_arg(argvars, 1) == FAIL
4896 || check_for_string_arg(argvars, 2) == FAIL))
4897 return;
4898
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004899 getwinvar(argvars, rettv, 1);
4900}
4901
4902/*
4903 * "getwinvar()" function
4904 */
4905 void
4906f_getwinvar(typval_T *argvars, typval_T *rettv)
4907{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004908 if (in_vim9script()
4909 && (check_for_number_arg(argvars, 0) == FAIL
4910 || check_for_string_arg(argvars, 1) == FAIL))
4911 return;
4912
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004913 getwinvar(argvars, rettv, 0);
4914}
4915
4916/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004917 * "getbufvar()" function
4918 */
4919 void
4920f_getbufvar(typval_T *argvars, typval_T *rettv)
4921{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004922 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004923 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004924
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004925 if (in_vim9script()
4926 && (check_for_buffer_arg(argvars, 0) == FAIL
4927 || check_for_string_arg(argvars, 1) == FAIL))
4928 return;
4929
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004930 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004931 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004932
LemonBoy47d4e312022-05-04 18:12:55 +01004933 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004934}
4935
4936/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004937 * "settabvar()" function
4938 */
4939 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004940f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004941{
4942 tabpage_T *save_curtab;
4943 tabpage_T *tp;
zeertzjqb47fbb42024-02-12 22:50:26 +01004944 tabpage_T *save_lu_tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004945 char_u *varname, *tabvarname;
4946 typval_T *varp;
4947
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004948 if (check_secure())
4949 return;
4950
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004951 if (in_vim9script()
4952 && (check_for_number_arg(argvars, 0) == FAIL
4953 || check_for_string_arg(argvars, 1) == FAIL))
4954 return;
4955
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004956 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4957 varname = tv_get_string_chk(&argvars[1]);
4958 varp = &argvars[2];
4959
zeertzjqea720ae2023-01-03 10:54:09 +00004960 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004961 return;
4962
4963 save_curtab = curtab;
zeertzjqb47fbb42024-02-12 22:50:26 +01004964 save_lu_tp = lastused_tabpage;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004965 goto_tabpage_tp(tp, FALSE, FALSE);
4966
4967 tabvarname = alloc(STRLEN(varname) + 3);
4968 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004969 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004970 STRCPY(tabvarname, "t:");
4971 STRCPY(tabvarname + 2, varname);
4972 set_var(tabvarname, varp, TRUE);
4973 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004974 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004975
zeertzjqb47fbb42024-02-12 22:50:26 +01004976 // Restore current tabpage and last accessed tabpage.
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004977 if (valid_tabpage(save_curtab))
zeertzjqb47fbb42024-02-12 22:50:26 +01004978 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004979 goto_tabpage_tp(save_curtab, FALSE, FALSE);
zeertzjqb47fbb42024-02-12 22:50:26 +01004980 if (valid_tabpage(save_lu_tp))
4981 lastused_tabpage = save_lu_tp;
4982 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004983}
4984
4985/*
4986 * "settabwinvar()" function
4987 */
4988 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004989f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004990{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004991 if (in_vim9script()
4992 && (check_for_number_arg(argvars, 0) == FAIL
4993 || check_for_number_arg(argvars, 1) == FAIL
4994 || check_for_string_arg(argvars, 2) == FAIL))
4995 return;
4996
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004997 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004998}
4999
5000/*
5001 * "setwinvar()" function
5002 */
5003 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005004f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005005{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005006 if (in_vim9script()
5007 && (check_for_number_arg(argvars, 0) == FAIL
5008 || check_for_string_arg(argvars, 1) == FAIL))
5009 return;
5010
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005011 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005012}
5013
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005014/*
5015 * "setbufvar()" function
5016 */
5017 void
5018f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
5019{
5020 buf_T *buf;
5021 char_u *varname, *bufvarname;
5022 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005023
5024 if (check_secure())
5025 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02005026
5027 if (in_vim9script()
5028 && (check_for_buffer_arg(argvars, 0) == FAIL
5029 || check_for_string_arg(argvars, 1) == FAIL))
5030 return;
5031
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005032 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02005033 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005034 varp = &argvars[2];
5035
zeertzjqea720ae2023-01-03 10:54:09 +00005036 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005037 return;
5038
5039 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005040 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005041 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005042 // safe the current window position, it could
5043 // change because of 'scrollbind' window-local
5044 // options
5045 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005046
5047 // Set curbuf to be our buf, temporarily.
5048 aucmd_prepbuf(&aco, buf);
5049 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005050 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005051 // Only when it worked to set "curbuf".
5052 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005053
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005054 // reset notion of buffer
5055 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005056 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005057 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005058 }
5059 else
5060 {
5061 bufvarname = alloc(STRLEN(varname) + 3);
5062 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005063 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005064 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02005065
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005066 curbuf = buf;
5067 STRCPY(bufvarname, "b:");
5068 STRCPY(bufvarname + 2, varname);
5069 set_var(bufvarname, varp, TRUE);
5070 vim_free(bufvarname);
5071 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005072 }
5073 }
5074}
5075
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005076/*
5077 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005078 * When "arg" is zero "res.cb_name" is set to an empty string.
5079 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
5080 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005081 */
5082 callback_T
5083get_callback(typval_T *arg)
5084{
Bram Moolenaar14e579092020-03-07 16:59:25 +01005085 callback_T res;
5086 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005087
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005088 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005089 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
5090 {
5091 res.cb_partial = arg->vval.v_partial;
5092 ++res.cb_partial->pt_refcount;
5093 res.cb_name = partial_name(res.cb_partial);
5094 }
5095 else
5096 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01005097 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01005098 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01005099 r = FAIL;
5100 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005101 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005102 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005103 if (arg->v_type == VAR_STRING)
5104 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005105 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005106 if (name != NULL)
5107 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005108 res.cb_name = name;
5109 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005110 }
5111 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005112 func_ref(res.cb_name);
5113 }
5114 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005115 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005116 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005117 r = FAIL;
5118
5119 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005120 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005121 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005122 res.cb_name = NULL;
5123 }
5124 }
5125 return res;
5126}
5127
5128/*
5129 * Copy a callback into a typval_T.
5130 */
5131 void
5132put_callback(callback_T *cb, typval_T *tv)
5133{
5134 if (cb->cb_partial != NULL)
5135 {
5136 tv->v_type = VAR_PARTIAL;
5137 tv->vval.v_partial = cb->cb_partial;
5138 ++tv->vval.v_partial->pt_refcount;
5139 }
5140 else
5141 {
5142 tv->v_type = VAR_FUNC;
5143 tv->vval.v_string = vim_strsave(cb->cb_name);
5144 func_ref(cb->cb_name);
5145 }
5146}
5147
5148/*
5149 * Make a copy of "src" into "dest", allocating the function name if needed,
5150 * without incrementing the refcount.
5151 */
5152 void
5153set_callback(callback_T *dest, callback_T *src)
5154{
5155 if (src->cb_partial == NULL)
5156 {
5157 // just a function name, make a copy
5158 dest->cb_name = vim_strsave(src->cb_name);
5159 dest->cb_free_name = TRUE;
5160 }
5161 else
5162 {
5163 // cb_name is a pointer into cb_partial
5164 dest->cb_name = src->cb_name;
5165 dest->cb_free_name = FALSE;
5166 }
5167 dest->cb_partial = src->cb_partial;
5168}
5169
5170/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005171 * Copy callback from "src" to "dest", incrementing the refcounts.
5172 */
5173 void
5174copy_callback(callback_T *dest, callback_T *src)
5175{
5176 dest->cb_partial = src->cb_partial;
5177 if (dest->cb_partial != NULL)
5178 {
5179 dest->cb_name = src->cb_name;
5180 dest->cb_free_name = FALSE;
5181 ++dest->cb_partial->pt_refcount;
5182 }
5183 else
5184 {
5185 dest->cb_name = vim_strsave(src->cb_name);
5186 dest->cb_free_name = TRUE;
5187 func_ref(src->cb_name);
5188 }
5189}
5190
5191/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005192 * When a callback refers to an autoload import, change the function name to
5193 * the "path#name" form. Uses the current script context.
5194 * Only works when the name is allocated.
5195 */
5196 void
5197expand_autload_callback(callback_T *cb)
5198{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005199 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005200 char_u *p;
5201 imported_T *import;
5202
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005203 if (!in_vim9script() || cb->cb_name == NULL
5204 || (!cb->cb_free_name
5205 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005206 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005207 if (cb->cb_partial != NULL)
5208 name = cb->cb_partial->pt_name;
5209 else
5210 name = cb->cb_name;
5211 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005212 if (p == NULL)
5213 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005214
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005215 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005216 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5217 return;
5218
5219 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5220 if (si->sn_autoload_prefix == NULL)
5221 return;
5222
5223 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5224 if (newname == NULL)
5225 return;
5226
5227 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005228 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005229 if (cb->cb_name == cb->cb_partial->pt_name)
5230 cb->cb_name = newname;
5231 vim_free(cb->cb_partial->pt_name);
5232 cb->cb_partial->pt_name = newname;
5233 }
5234 else
5235 {
5236 vim_free(cb->cb_name);
5237 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005238 }
5239}
5240
5241/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005242 * Unref/free "callback" returned by get_callback() or set_callback().
5243 */
5244 void
5245free_callback(callback_T *callback)
5246{
5247 if (callback->cb_partial != NULL)
5248 {
5249 partial_unref(callback->cb_partial);
5250 callback->cb_partial = NULL;
5251 }
5252 else if (callback->cb_name != NULL)
5253 func_unref(callback->cb_name);
5254 if (callback->cb_free_name)
5255 {
5256 vim_free(callback->cb_name);
5257 callback->cb_free_name = FALSE;
5258 }
5259 callback->cb_name = NULL;
5260}
5261
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005262#endif // FEAT_EVAL