blob: 3be9993fdce0886df637a9fbc5b02d5828b1a0f1 [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},
114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
115 {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},
142 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
143 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
144 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
148 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
149 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
150 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
151 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
152 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
153 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
154 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
155 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
156 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000157 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200158};
159
160// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000161#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vv_nr vv_di.di_tv.vval.v_number
163#define vv_float vv_di.di_tv.vval.v_float
164#define vv_str vv_di.di_tv.vval.v_string
165#define vv_list vv_di.di_tv.vval.v_list
166#define vv_dict vv_di.di_tv.vval.v_dict
167#define vv_blob vv_di.di_tv.vval.v_blob
168#define vv_tv vv_di.di_tv
169
170static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200171static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200172#define vimvarht vimvardict.dv_hashtab
173
174// for VIM_VERSION_ defines
175#include "version.h"
176
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_glob_vars(int *first);
178static void list_buf_vars(int *first);
179static void list_win_vars(int *first);
180static void list_tab_vars(int *first);
181static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100182static 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 +0200183static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
184static 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 +0200185static void list_one_var(dictitem_T *v, char *prefix, int *first);
186static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
187
188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200189 * Initialize global and vim special variables
190 */
191 void
192evalvars_init(void)
193{
194 int i;
195 struct vimvar *p;
196
197 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
198 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
199 vimvardict.dv_lock = VAR_FIXED;
200 hash_init(&compat_hashtab);
201
202 for (i = 0; i < VV_LEN; ++i)
203 {
204 p = &vimvars[i];
205 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
206 {
207 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
208 getout(1);
209 }
210 STRCPY(p->vv_di.di_key, p->vv_name);
211 if (p->vv_flags & VV_RO)
212 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
213 else if (p->vv_flags & VV_RO_SBX)
214 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
215 else
216 p->vv_di.di_flags = DI_FLAGS_FIX;
217
218 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000219 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220 hash_add(&vimvarht, p->vv_di.di_key);
221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
223 hash_add(&compat_hashtab, p->vv_di.di_key);
224 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200225 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
226 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227
228 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
229 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100230 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
232 set_vim_var_list(VV_ERRORS, list_alloc());
233 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
234
235 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
236 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
237 set_vim_var_nr(VV_NONE, VVAL_NONE);
238 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100239 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
240 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100241 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000242 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
243 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
244 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000245 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200246
247 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
248 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
249 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
250 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
251 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
252 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
253 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
254 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
255 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
256 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
257 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
258
259 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
260
Drew Vogele30d1022021-10-24 20:35:07 +0100261 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
262
Bram Moolenaar439c0362020-06-06 15:58:03 +0200263 // Default for v:register is not 0 but '"'. This is adjusted once the
264 // clipboard has been setup by calling reset_reg_var().
265 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200266}
267
268#if defined(EXITFREE) || defined(PROTO)
269/*
270 * Free all vim variables information on exit
271 */
272 void
273evalvars_clear(void)
274{
275 int i;
276 struct vimvar *p;
277
278 for (i = 0; i < VV_LEN; ++i)
279 {
280 p = &vimvars[i];
281 if (p->vv_di.di_tv.v_type == VAR_STRING)
282 VIM_CLEAR(p->vv_str);
283 else if (p->vv_di.di_tv.v_type == VAR_LIST)
284 {
285 list_unref(p->vv_list);
286 p->vv_list = NULL;
287 }
288 }
289 hash_clear(&vimvarht);
290 hash_init(&vimvarht); // garbage_collect() will access it
291 hash_clear(&compat_hashtab);
292
293 // global variables
294 vars_clear(&globvarht);
295
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100296 // Script-local variables. Clear all the variables here.
297 // The scriptvar_T is cleared later in free_scriptnames(), because a
298 // variable in one script might hold a reference to the whole scope of
299 // another script.
300 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200301 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200302}
303#endif
304
305 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200306garbage_collect_globvars(int copyID)
307{
308 return set_ref_in_ht(&globvarht, copyID, NULL);
309}
310
311 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312garbage_collect_vimvars(int copyID)
313{
314 return set_ref_in_ht(&vimvarht, copyID, NULL);
315}
316
317 int
318garbage_collect_scriptvars(int copyID)
319{
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 int i;
321 int idx;
322 int abort = FALSE;
323 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100325 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
328
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 si = SCRIPT_ITEM(i);
330 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100334 if (sv->sv_name != NULL)
335 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 }
337 }
338
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339 return abort;
340}
341
342/*
343 * Set an internal variable to a string value. Creates the variable if it does
344 * not already exist.
345 */
346 void
347set_internal_string_var(char_u *name, char_u *value)
348{
349 char_u *val;
350 typval_T *tvp;
351
352 val = vim_strsave(value);
353 if (val != NULL)
354 {
355 tvp = alloc_string_tv(val);
356 if (tvp != NULL)
357 {
358 set_var(name, tvp, FALSE);
359 free_tv(tvp);
360 }
361 }
362}
363
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200364 int
365eval_charconvert(
366 char_u *enc_from,
367 char_u *enc_to,
368 char_u *fname_from,
369 char_u *fname_to)
370{
371 int err = FALSE;
372
373 set_vim_var_string(VV_CC_FROM, enc_from, -1);
374 set_vim_var_string(VV_CC_TO, enc_to, -1);
375 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
376 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
377 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
378 err = TRUE;
379 set_vim_var_string(VV_CC_FROM, NULL, -1);
380 set_vim_var_string(VV_CC_TO, NULL, -1);
381 set_vim_var_string(VV_FNAME_IN, NULL, -1);
382 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
383
384 if (err)
385 return FAIL;
386 return OK;
387}
388
389# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
390 int
391eval_printexpr(char_u *fname, char_u *args)
392{
393 int err = FALSE;
394
395 set_vim_var_string(VV_FNAME_IN, fname, -1);
396 set_vim_var_string(VV_CMDARG, args, -1);
397 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
398 err = TRUE;
399 set_vim_var_string(VV_FNAME_IN, NULL, -1);
400 set_vim_var_string(VV_CMDARG, NULL, -1);
401
402 if (err)
403 {
404 mch_remove(fname);
405 return FAIL;
406 }
407 return OK;
408}
409# endif
410
411# if defined(FEAT_DIFF) || defined(PROTO)
412 void
413eval_diff(
414 char_u *origfile,
415 char_u *newfile,
416 char_u *outfile)
417{
418 int err = FALSE;
419
420 set_vim_var_string(VV_FNAME_IN, origfile, -1);
421 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
422 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
423 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
424 set_vim_var_string(VV_FNAME_IN, NULL, -1);
425 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
426 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
427}
428
429 void
430eval_patch(
431 char_u *origfile,
432 char_u *difffile,
433 char_u *outfile)
434{
435 int err;
436
437 set_vim_var_string(VV_FNAME_IN, origfile, -1);
438 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
439 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
440 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
441 set_vim_var_string(VV_FNAME_IN, NULL, -1);
442 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
443 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
444}
445# endif
446
447#if defined(FEAT_SPELL) || defined(PROTO)
448/*
449 * Evaluate an expression to a list with suggestions.
450 * For the "expr:" part of 'spellsuggest'.
451 * Returns NULL when there is an error.
452 */
453 list_T *
454eval_spell_expr(char_u *badword, char_u *expr)
455{
456 typval_T save_val;
457 typval_T rettv;
458 list_T *list = NULL;
459 char_u *p = skipwhite(expr);
460
461 // Set "v:val" to the bad word.
462 prepare_vimvar(VV_VAL, &save_val);
463 set_vim_var_string(VV_VAL, badword, -1);
464 if (p_verbose == 0)
465 ++emsg_off;
466
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200467 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200468 {
469 if (rettv.v_type != VAR_LIST)
470 clear_tv(&rettv);
471 else
472 list = rettv.vval.v_list;
473 }
474
475 if (p_verbose == 0)
476 --emsg_off;
477 clear_tv(get_vim_var_tv(VV_VAL));
478 restore_vimvar(VV_VAL, &save_val);
479
480 return list;
481}
482
483/*
484 * "list" is supposed to contain two items: a word and a number. Return the
485 * word in "pp" and the number as the return value.
486 * Return -1 if anything isn't right.
487 * Used to get the good word and score from the eval_spell_expr() result.
488 */
489 int
490get_spellword(list_T *list, char_u **pp)
491{
492 listitem_T *li;
493
494 li = list->lv_first;
495 if (li == NULL)
496 return -1;
497 *pp = tv_get_string(&li->li_tv);
498
499 li = li->li_next;
500 if (li == NULL)
501 return -1;
502 return (int)tv_get_number(&li->li_tv);
503}
504#endif
505
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506/*
507 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200508 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200509 * When not used yet add the variable to the v: hashtable.
510 */
511 void
512prepare_vimvar(int idx, typval_T *save_tv)
513{
514 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200515 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000516 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200517 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
518}
519
520/*
521 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200522 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200523 * When no longer defined, remove the variable from the v: hashtable.
524 */
525 void
526restore_vimvar(int idx, typval_T *save_tv)
527{
528 hashitem_T *hi;
529
530 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000531 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200532 {
533 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
534 if (HASHITEM_EMPTY(hi))
535 internal_error("restore_vimvar()");
536 else
537 hash_remove(&vimvarht, hi);
538 }
539}
540
541/*
542 * List Vim variables.
543 */
544 static void
545list_vim_vars(int *first)
546{
547 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
548}
549
550/*
551 * List script-local variables, if there is a script.
552 */
553 static void
554list_script_vars(int *first)
555{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200556 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200557 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
558 "s:", FALSE, first);
559}
560
561/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200562 * Get a list of lines from a HERE document. The here document is a list of
563 * lines surrounded by a marker.
564 * cmd << {marker}
565 * {line1}
566 * {line2}
567 * ....
568 * {marker}
569 *
570 * The {marker} is a string. If the optional 'trim' word is supplied before the
571 * marker, then the leading indentation before the lines (matching the
572 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200573 *
574 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
575 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
576 * missing, then '.' is accepted as a marker.
577 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200578 * Returns a List with {lines} or NULL.
579 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200581heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200582{
583 char_u *theline;
584 char_u *marker;
585 list_T *l;
586 char_u *p;
587 int marker_indent_len = 0;
588 int text_indent_len = 0;
589 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200590 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200591 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200592
593 if (eap->getline == NULL)
594 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000595 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200596 return NULL;
597 }
598
599 // Check for the optional 'trim' word before the marker
600 cmd = skipwhite(cmd);
601 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
602 {
603 cmd = skipwhite(cmd + 4);
604
605 // Trim the indentation from all the lines in the here document.
606 // The amount of indentation trimmed is the same as the indentation of
607 // the first line after the :let command line. To find the end marker
608 // the indent of the :let command line is trimmed.
609 p = *eap->cmdlinep;
610 while (VIM_ISWHITE(*p))
611 {
612 p++;
613 marker_indent_len++;
614 }
615 text_indent_len = -1;
616 }
617
618 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200619 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200620 {
621 marker = skipwhite(cmd);
622 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200623 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200624 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000625 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200626 return NULL;
627 }
628 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200629 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200630 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000631 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200632 return NULL;
633 }
634 }
635 else
636 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200637 // When getting lines for an embedded script, if the marker is missing,
638 // accept '.' as the marker.
639 if (script_get)
640 marker = dot;
641 else
642 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000643 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200644 return NULL;
645 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200646 }
647
648 l = list_alloc();
649 if (l == NULL)
650 return NULL;
651
652 for (;;)
653 {
654 int mi = 0;
655 int ti = 0;
656
657 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
658 if (theline == NULL)
659 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000660 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200661 break;
662 }
663
664 // with "trim": skip the indent matching the :let line to find the
665 // marker
666 if (marker_indent_len > 0
667 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
668 mi = marker_indent_len;
669 if (STRCMP(marker, theline + mi) == 0)
670 {
671 vim_free(theline);
672 break;
673 }
674
675 if (text_indent_len == -1 && *theline != NUL)
676 {
677 // set the text indent from the first line.
678 p = theline;
679 text_indent_len = 0;
680 while (VIM_ISWHITE(*p))
681 {
682 p++;
683 text_indent_len++;
684 }
685 text_indent = vim_strnsave(theline, text_indent_len);
686 }
687 // with "trim": skip the indent matching the first line
688 if (text_indent != NULL)
689 for (ti = 0; ti < text_indent_len; ++ti)
690 if (theline[ti] != text_indent[ti])
691 break;
692
693 if (list_append_string(l, theline + ti, -1) == FAIL)
694 break;
695 vim_free(theline);
696 }
697 vim_free(text_indent);
698
699 return l;
700}
701
702/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200703 * Vim9 variable declaration:
704 * ":var name"
705 * ":var name: type"
706 * ":var name = expr"
707 * ":var name: type = expr"
708 * etc.
709 */
710 void
711ex_var(exarg_T *eap)
712{
713 if (!in_vim9script())
714 {
715 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
716 return;
717 }
718 ex_let(eap);
719}
720
721/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 * ":let" list all variable values
723 * ":let var1 var2" list variable values
724 * ":let var = expr" assignment command.
725 * ":let var += expr" assignment command.
726 * ":let var -= expr" assignment command.
727 * ":let var *= expr" assignment command.
728 * ":let var /= expr" assignment command.
729 * ":let var %= expr" assignment command.
730 * ":let var .= expr" assignment command.
731 * ":let var ..= expr" assignment command.
732 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100734 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200735 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200736 * ":final var = expr" assignment command.
737 * ":final [var1, var2] = expr" unpack list.
738 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200739 * ":const" list all variable values
740 * ":const var1 var2" list variable values
741 * ":const var = expr" assignment command.
742 * ":const [var1, var2] = expr" unpack list.
743 */
744 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200745ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200746{
747 char_u *arg = eap->arg;
748 char_u *expr = NULL;
749 typval_T rettv;
750 int i;
751 int var_count = 0;
752 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200753 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200754 char_u *argend;
755 int first = TRUE;
756 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200757 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100758 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200759 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200760
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200761 if (eap->cmdidx == CMD_final && !vim9script)
762 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100763 // In legacy Vim script ":final" is short for ":finally".
764 ex_finally(eap);
765 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200766 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200767 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200768 {
769 emsg(_(e_cannot_use_let_in_vim9_script));
770 return;
771 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200772
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100773 if (eap->cmdidx == CMD_const)
774 flags |= ASSIGN_CONST;
775 else if (eap->cmdidx == CMD_final)
776 flags |= ASSIGN_FINAL;
777
778 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200780 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200782 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200783 if (argend == NULL)
784 return;
785 if (argend > arg && argend[-1] == '.') // for var.='str'
786 --argend;
787 expr = skipwhite(argend);
788 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200789 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200790 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200791 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
792 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200793 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 {
795 // ":let" without "=": list variables
796 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000797 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200798 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000799 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200800 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200801 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200802 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200803 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100804 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000805 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100806 else
807 // Vim9 declaration ":var name: type"
808 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200809 }
810 else
811 {
812 // ":let var1 var2" - list values
813 arg = list_arg_vars(eap, arg, &first);
814 }
815 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 else if (!eap->skip)
817 {
818 // ":let"
819 list_glob_vars(&first);
820 list_buf_vars(&first);
821 list_win_vars(&first);
822 list_tab_vars(&first);
823 list_script_vars(&first);
824 list_func_vars(&first);
825 list_vim_vars(&first);
826 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200827 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200828 }
829 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
830 {
831 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200832 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833
834 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200835 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836 if (l != NULL)
837 {
838 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200839 if (!eap->skip)
840 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200841 // errors are for the assignment, not the end marker
842 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200843 op[0] = '=';
844 op[1] = NUL;
845 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100846 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200847 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200848 clear_tv(&rettv);
849 }
850 }
851 else
852 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200853 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200855
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200856 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200857 i = FAIL;
858 if (has_assign || concat)
859 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100860 int cur_lnum;
861
Bram Moolenaar32e35112020-05-14 22:41:15 +0200862 op[0] = '=';
863 op[1] = NUL;
864 if (*expr != '=')
865 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200866 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200867 {
868 // +=, /=, etc. require an existing variable
869 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
870 i = FAIL;
871 }
872 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200873 {
874 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200875 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200876 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200877 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200878 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200879 ++len;
880 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200881 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200882 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200883 }
884 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200885 ++expr;
886
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000887 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200888 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200889 {
890 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100891 semsg(_(e_white_space_required_before_and_after_str_at_str),
892 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200893 i = FAIL;
894 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200895
896 if (eap->skip)
897 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200898 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200899 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100900 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200901 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200902 if (eap->skip)
903 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200904 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100905
906 // Restore the line number so that any type error is given for the
907 // declaration, not the expression.
908 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200909 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200910 if (eap->skip)
911 {
912 if (i != FAIL)
913 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200915 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 {
917 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200918 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200919 clear_tv(&rettv);
920 }
921 }
922}
923
924/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100925 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200926 * Handles both "var" with any type and "[var, var; var]" with a list type.
927 * When "op" is not NULL it points to a string with characters that
928 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
929 * or concatenate.
930 * Returns OK or FAIL;
931 */
932 int
933ex_let_vars(
934 char_u *arg_start,
935 typval_T *tv,
936 int copy, // copy values from "tv", don't move
937 int semicolon, // from skip_var_list()
938 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100939 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200940 char_u *op)
941{
942 char_u *arg = arg_start;
943 list_T *l;
944 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100945 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200946 listitem_T *item;
947 typval_T ltv;
948
949 if (*arg != '[')
950 {
951 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100952 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200953 return FAIL;
954 return OK;
955 }
956
957 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
958 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
959 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000960 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200961 return FAIL;
962 }
963
964 i = list_len(l);
965 if (semicolon == 0 && var_count < i)
966 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000967 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 return FAIL;
969 }
970 if (var_count - semicolon > i)
971 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000972 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200973 return FAIL;
974 }
975
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200976 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977 item = l->lv_first;
978 while (*arg != ']')
979 {
980 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100981 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200982 arg = ex_let_one(arg, &item->li_tv, TRUE,
983 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984 item = item->li_next;
985 if (arg == NULL)
986 return FAIL;
987
988 arg = skipwhite(arg);
989 if (*arg == ';')
990 {
991 // Put the rest of the list (may be empty) in the var after ';'.
992 // Create a new list for this.
993 l = list_alloc();
994 if (l == NULL)
995 return FAIL;
996 while (item != NULL)
997 {
998 list_append_tv(l, &item->li_tv);
999 item = item->li_next;
1000 }
1001
1002 ltv.v_type = VAR_LIST;
1003 ltv.v_lock = 0;
1004 ltv.vval.v_list = l;
1005 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001006 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001008 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1009 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001010 clear_tv(&ltv);
1011 if (arg == NULL)
1012 return FAIL;
1013 break;
1014 }
1015 else if (*arg != ',' && *arg != ']')
1016 {
1017 internal_error("ex_let_vars()");
1018 return FAIL;
1019 }
1020 }
1021
1022 return OK;
1023}
1024
1025/*
1026 * Skip over assignable variable "var" or list of variables "[var, var]".
1027 * Used for ":let varvar = expr" and ":for varvar in expr".
1028 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001029 * for "[var, var; var]" set "semicolon" to 1.
1030 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031 * Return NULL for an error.
1032 */
1033 char_u *
1034skip_var_list(
1035 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001037 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001038 int *semicolon,
1039 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001040{
1041 char_u *p, *s;
1042
1043 if (*arg == '[')
1044 {
1045 // "[var, var]": find the matching ']'.
1046 p = arg;
1047 for (;;)
1048 {
1049 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001050 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051 if (s == p)
1052 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001053 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001054 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055 return NULL;
1056 }
1057 ++*var_count;
1058
1059 p = skipwhite(s);
1060 if (*p == ']')
1061 break;
1062 else if (*p == ';')
1063 {
1064 if (*semicolon == 1)
1065 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001066 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001067 return NULL;
1068 }
1069 *semicolon = 1;
1070 }
1071 else if (*p != ',')
1072 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001073 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001074 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001075 return NULL;
1076 }
1077 }
1078 return p + 1;
1079 }
1080 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082}
1083
1084/*
1085 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1086 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001088 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001089 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001091{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001092 char_u *end;
1093 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001095 if (*arg == '@' && arg[1] != NUL)
1096 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001098 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001099
1100 // "a: type" is declaring variable "a" with a type, not "a:".
1101 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001102 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001103 --end;
1104
Bram Moolenaar585587d2021-01-17 20:52:13 +01001105 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001107 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001108 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 }
1110 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001111}
1112
1113/*
1114 * List variables for hashtab "ht" with prefix "prefix".
1115 * If "empty" is TRUE also list NULL strings as empty strings.
1116 */
1117 void
1118list_hashtable_vars(
1119 hashtab_T *ht,
1120 char *prefix,
1121 int empty,
1122 int *first)
1123{
1124 hashitem_T *hi;
1125 dictitem_T *di;
1126 int todo;
1127 char_u buf[IOSIZE];
1128
1129 todo = (int)ht->ht_used;
1130 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1131 {
1132 if (!HASHITEM_EMPTY(hi))
1133 {
1134 --todo;
1135 di = HI2DI(hi);
1136
1137 // apply :filter /pat/ to variable name
1138 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1139 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1140 if (message_filtered(buf))
1141 continue;
1142
1143 if (empty || di->di_tv.v_type != VAR_STRING
1144 || di->di_tv.vval.v_string != NULL)
1145 list_one_var(di, prefix, first);
1146 }
1147 }
1148}
1149
1150/*
1151 * List global variables.
1152 */
1153 static void
1154list_glob_vars(int *first)
1155{
1156 list_hashtable_vars(&globvarht, "", TRUE, first);
1157}
1158
1159/*
1160 * List buffer variables.
1161 */
1162 static void
1163list_buf_vars(int *first)
1164{
1165 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1166}
1167
1168/*
1169 * List window variables.
1170 */
1171 static void
1172list_win_vars(int *first)
1173{
1174 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1175}
1176
1177/*
1178 * List tab page variables.
1179 */
1180 static void
1181list_tab_vars(int *first)
1182{
1183 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1184}
1185
1186/*
1187 * List variables in "arg".
1188 */
1189 static char_u *
1190list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1191{
1192 int error = FALSE;
1193 int len;
1194 char_u *name;
1195 char_u *name_start;
1196 char_u *arg_subsc;
1197 char_u *tofree;
1198 typval_T tv;
1199
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001200 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001201 {
1202 if (error || eap->skip)
1203 {
1204 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1205 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1206 {
1207 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001208 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001209 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 break;
1211 }
1212 }
1213 else
1214 {
1215 // get_name_len() takes care of expanding curly braces
1216 name_start = name = arg;
1217 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1218 if (len <= 0)
1219 {
1220 // This is mainly to keep test 49 working: when expanding
1221 // curly braces fails overrule the exception error message.
1222 if (len < 0 && !aborting())
1223 {
1224 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001225 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001226 break;
1227 }
1228 error = TRUE;
1229 }
1230 else
1231 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001232 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 if (tofree != NULL)
1234 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001235 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001236 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 error = TRUE;
1238 else
1239 {
1240 // handle d.key, l[idx], f(expr)
1241 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001242 if (handle_subscript(&arg, name_start, &tv,
1243 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001244 error = TRUE;
1245 else
1246 {
1247 if (arg == arg_subsc && len == 2 && name[1] == ':')
1248 {
1249 switch (*name)
1250 {
1251 case 'g': list_glob_vars(first); break;
1252 case 'b': list_buf_vars(first); break;
1253 case 'w': list_win_vars(first); break;
1254 case 't': list_tab_vars(first); break;
1255 case 'v': list_vim_vars(first); break;
1256 case 's': list_script_vars(first); break;
1257 case 'l': list_func_vars(first); break;
1258 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001259 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260 }
1261 }
1262 else
1263 {
1264 char_u numbuf[NUMBUFLEN];
1265 char_u *tf;
1266 int c;
1267 char_u *s;
1268
1269 s = echo_string(&tv, &tf, numbuf, 0);
1270 c = *arg;
1271 *arg = NUL;
1272 list_one_var_a("",
1273 arg == arg_subsc ? name : name_start,
1274 tv.v_type,
1275 s == NULL ? (char_u *)"" : s,
1276 first);
1277 *arg = c;
1278 vim_free(tf);
1279 }
1280 clear_tv(&tv);
1281 }
1282 }
1283 }
1284
1285 vim_free(tofree);
1286 }
1287
1288 arg = skipwhite(arg);
1289 }
1290
1291 return arg;
1292}
1293
1294/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001295 * Set an environment variable, part of ex_let_one().
1296 */
1297 static char_u *
1298ex_let_env(
1299 char_u *arg,
1300 typval_T *tv,
1301 int flags,
1302 char_u *endchars,
1303 char_u *op)
1304{
1305 char_u *arg_end = NULL;
1306 char_u *name;
1307 int len;
1308
1309 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1310 && (flags & ASSIGN_FOR_LOOP) == 0)
1311 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001312 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001313 return NULL;
1314 }
1315
1316 // Find the end of the name.
1317 ++arg;
1318 name = arg;
1319 len = get_env_len(&arg);
1320 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001321 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001322 else
1323 {
1324 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001325 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001326 else if (endchars != NULL
1327 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1328 emsg(_(e_unexpected_characters_in_let));
1329 else if (!check_secure())
1330 {
1331 char_u *tofree = NULL;
1332 int c1 = name[len];
1333 char_u *p;
1334
1335 name[len] = NUL;
1336 p = tv_get_string_chk(tv);
1337 if (p != NULL && op != NULL && *op == '.')
1338 {
1339 int mustfree = FALSE;
1340 char_u *s = vim_getenv(name, &mustfree);
1341
1342 if (s != NULL)
1343 {
1344 p = tofree = concat_str(s, p);
1345 if (mustfree)
1346 vim_free(s);
1347 }
1348 }
1349 if (p != NULL)
1350 {
1351 vim_setenv_ext(name, p);
1352 arg_end = arg;
1353 }
1354 name[len] = c1;
1355 vim_free(tofree);
1356 }
1357 }
1358 return arg_end;
1359}
1360
1361/*
1362 * Set an option, part of ex_let_one().
1363 */
1364 static char_u *
1365ex_let_option(
1366 char_u *arg,
1367 typval_T *tv,
1368 int flags,
1369 char_u *endchars,
1370 char_u *op)
1371{
1372 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001373 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001374 char_u *arg_end = NULL;
1375
1376 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1377 && (flags & ASSIGN_FOR_LOOP) == 0)
1378 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001379 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001380 return NULL;
1381 }
1382
1383 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001384 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001385 if (p == NULL || (endchars != NULL
1386 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1387 emsg(_(e_unexpected_characters_in_let));
1388 else
1389 {
1390 int c1;
1391 long n = 0;
1392 getoption_T opt_type;
1393 long numval;
1394 char_u *stringval = NULL;
1395 char_u *s = NULL;
1396 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001397 int opt_p_flags;
1398 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001399 char_u numbuf[NUMBUFLEN];
1400
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001401 c1 = *p;
1402 *p = NUL;
1403
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001404 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1405 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001406 if ((opt_type == gov_bool
1407 || opt_type == gov_number
1408 || opt_type == gov_hidden_bool
1409 || opt_type == gov_hidden_number)
1410 && (tv->v_type != VAR_STRING || !in_vim9script()))
1411 {
1412 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1413 // bool, possibly hidden
1414 n = (long)tv_get_bool(tv);
1415 else
1416 // number, possibly hidden
1417 n = (long)tv_get_number(tv);
1418 }
1419
Bram Moolenaaref082e12021-12-12 21:02:03 +00001420 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001421 || tv->v_type == VAR_FUNC))
1422 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001423 // If the option can be set to a function reference or a lambda
1424 // and the passed value is a function reference, then convert it to
1425 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001426 s = tv2string(tv, &tofree, numbuf, 0);
1427 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001428 // Avoid setting a string option to the text "v:false" or similar.
1429 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001430 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001431 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1432 s = tv_get_string_chk(tv);
1433
1434 if (op != NULL && *op != '=')
1435 {
1436 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1437 || (opt_type == gov_string && *op != '.'))
1438 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001439 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001440 failed = TRUE; // don't set the value
1441
1442 }
1443 else
1444 {
1445 // number, in legacy script also bool
1446 if (opt_type == gov_number
1447 || (opt_type == gov_bool && !in_vim9script()))
1448 {
1449 switch (*op)
1450 {
1451 case '+': n = numval + n; break;
1452 case '-': n = numval - n; break;
1453 case '*': n = numval * n; break;
1454 case '/': n = (long)num_divide(numval, n,
1455 &failed); break;
1456 case '%': n = (long)num_modulus(numval, n,
1457 &failed); break;
1458 }
1459 s = NULL;
1460 }
1461 else if (opt_type == gov_string
1462 && stringval != NULL && s != NULL)
1463 {
1464 // string
1465 s = concat_str(stringval, s);
1466 vim_free(stringval);
1467 stringval = s;
1468 }
1469 }
1470 }
1471
1472 if (!failed)
1473 {
1474 if (opt_type != gov_string || s != NULL)
1475 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001476 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001477 arg_end = p;
1478 }
1479 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001480 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001481 }
1482 *p = c1;
1483 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001484 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001485 }
1486 return arg_end;
1487}
1488
1489/*
1490 * Set a register, part of ex_let_one().
1491 */
1492 static char_u *
1493ex_let_register(
1494 char_u *arg,
1495 typval_T *tv,
1496 int flags,
1497 char_u *endchars,
1498 char_u *op)
1499{
1500 char_u *arg_end = NULL;
1501
1502 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1503 && (flags & ASSIGN_FOR_LOOP) == 0)
1504 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001505 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001506 return NULL;
1507 }
1508 ++arg;
1509 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001510 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001511 else if (endchars != NULL
1512 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1513 emsg(_(e_unexpected_characters_in_let));
1514 else
1515 {
1516 char_u *ptofree = NULL;
1517 char_u *p;
1518
1519 p = tv_get_string_chk(tv);
1520 if (p != NULL && op != NULL && *op == '.')
1521 {
1522 char_u *s = get_reg_contents(*arg == '@'
1523 ? '"' : *arg, GREG_EXPR_SRC);
1524
1525 if (s != NULL)
1526 {
1527 p = ptofree = concat_str(s, p);
1528 vim_free(s);
1529 }
1530 }
1531 if (p != NULL)
1532 {
1533 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1534 arg_end = arg + 1;
1535 }
1536 vim_free(ptofree);
1537 }
1538 return arg_end;
1539}
1540
1541/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001542 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1543 * Returns a pointer to the char just after the var name.
1544 * Returns NULL if there is an error.
1545 */
1546 static char_u *
1547ex_let_one(
1548 char_u *arg, // points to variable name
1549 typval_T *tv, // value to assign to variable
1550 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001551 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001553 char_u *op, // "+", "-", "." or NULL
1554 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001555{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001556 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001557
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001558 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001559 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001560 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1561 {
1562 vim9_declare_error(arg);
1563 return NULL;
1564 }
1565
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001566 if (*arg == '$')
1567 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001568 // ":let $VAR = expr": Set environment variable.
1569 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001570 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571 else if (*arg == '&')
1572 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001573 // ":let &option = expr": Set option value.
1574 // ":let &l:option = expr": Set local option value.
1575 // ":let &g:option = expr": Set global option value.
1576 // ":for &ts in range(8)": Set option value for for loop
1577 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001579 else if (*arg == '@')
1580 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001581 // ":let @r = expr": Set register contents.
1582 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584 else if (eval_isnamec1(*arg) || *arg == '{')
1585 {
1586 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001587 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001588
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001589 // ":let var = expr": Set internal variable.
1590 // ":let var: type = expr": Set internal variable with type.
1591 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001592 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001593 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1594 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001595 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 if (endchars != NULL && vim_strchr(endchars,
1598 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001599 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001600 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001601 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001602 else
1603 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001604 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001605 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 }
1607 }
1608 clear_lval(&lv);
1609 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001611 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612
1613 return arg_end;
1614}
1615
1616/*
1617 * ":unlet[!] var1 ... " command.
1618 */
1619 void
1620ex_unlet(exarg_T *eap)
1621{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001622 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623}
1624
1625/*
1626 * ":lockvar" and ":unlockvar" commands
1627 */
1628 void
1629ex_lockvar(exarg_T *eap)
1630{
1631 char_u *arg = eap->arg;
1632 int deep = 2;
1633
1634 if (eap->forceit)
1635 deep = -1;
1636 else if (vim_isdigit(*arg))
1637 {
1638 deep = getdigits(&arg);
1639 arg = skipwhite(arg);
1640 }
1641
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001642 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001643}
1644
1645/*
1646 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001647 * Also used for Vim9 script. "callback" is invoked as:
1648 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001649 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001650 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001651ex_unletlock(
1652 exarg_T *eap,
1653 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001654 int deep,
1655 int glv_flags,
1656 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1657 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001658{
1659 char_u *arg = argstart;
1660 char_u *name_end;
1661 int error = FALSE;
1662 lval_T lv;
1663
1664 do
1665 {
1666 if (*arg == '$')
1667 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001668 lv.ll_name = arg;
1669 lv.ll_tv = NULL;
1670 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001671 if (get_env_len(&arg) == 0)
1672 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001673 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001674 return;
1675 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001676 if (!error && !eap->skip
1677 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1678 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001679 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001681 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001682 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001683 // Parse the name and find the end.
1684 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001685 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001686 if (lv.ll_name == NULL)
1687 error = TRUE; // error but continue parsing
1688 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1689 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001690 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001691 if (name_end != NULL)
1692 {
1693 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001694 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001695 }
1696 if (!(eap->skip || error))
1697 clear_lval(&lv);
1698 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001699 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001700
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001701 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001702 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001703 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001704
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001705 if (!eap->skip)
1706 clear_lval(&lv);
1707 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001708
1709 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001710 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001711
Bram Moolenaar63b91732021-08-05 20:40:03 +02001712 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001713}
1714
1715 static int
1716do_unlet_var(
1717 lval_T *lp,
1718 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001719 exarg_T *eap,
1720 int deep UNUSED,
1721 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001722{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001723 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001724 int ret = OK;
1725 int cc;
1726
1727 if (lp->ll_tv == NULL)
1728 {
1729 cc = *name_end;
1730 *name_end = NUL;
1731
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001732 // Environment variable, normal name or expanded name.
1733 if (*lp->ll_name == '$')
1734 vim_unsetenv(lp->ll_name + 1);
1735 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 ret = FAIL;
1737 *name_end = cc;
1738 }
1739 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001740 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001741 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001742 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 return FAIL;
1744 else if (lp->ll_range)
1745 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001746 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1747 !lp->ll_empty2, lp->ll_n2) == FAIL)
1748 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749 }
1750 else
1751 {
1752 if (lp->ll_list != NULL)
1753 // unlet a List item.
1754 listitem_remove(lp->ll_list, lp->ll_li);
1755 else
1756 // unlet a Dictionary item.
1757 dictitem_remove(lp->ll_dict, lp->ll_di);
1758 }
1759
1760 return ret;
1761}
1762
1763/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001764 * Unlet one item or a range of items from a list.
1765 * Return OK or FAIL.
1766 */
1767 int
1768list_unlet_range(
1769 list_T *l,
1770 listitem_T *li_first,
1771 char_u *name,
1772 long n1_arg,
1773 int has_n2,
1774 long n2)
1775{
1776 listitem_T *li = li_first;
1777 int n1 = n1_arg;
1778
1779 while (li != NULL && (!has_n2 || n2 >= n1))
1780 {
1781 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1782 return FAIL;
1783 li = li->li_next;
1784 ++n1;
1785 }
1786
1787 // Delete a range of List items.
1788 li = li_first;
1789 n1 = n1_arg;
1790 while (li != NULL && (!has_n2 || n2 >= n1))
1791 {
1792 listitem_T *next = li->li_next;
1793
1794 listitem_remove(l, li);
1795 li = next;
1796 ++n1;
1797 }
1798 return OK;
1799}
1800/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801 * "unlet" a variable. Return OK if it existed, FAIL if not.
1802 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1803 */
1804 int
1805do_unlet(char_u *name, int forceit)
1806{
1807 hashtab_T *ht;
1808 hashitem_T *hi;
1809 char_u *varname;
1810 dict_T *d;
1811 dictitem_T *di;
1812
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001813 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001814 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001815 return FAIL;
1816
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001817 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001818
1819 // can't :unlet a script variable in Vim9 script from a function
1820 if (ht == get_script_local_ht()
1821 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1822 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1823 == SCRIPT_VERSION_VIM9
1824 && check_vim9_unlet(name) == FAIL)
1825 return FAIL;
1826
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 if (ht != NULL && *varname != NUL)
1828 {
1829 d = get_current_funccal_dict(ht);
1830 if (d == NULL)
1831 {
1832 if (ht == &globvarht)
1833 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001834 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001835 d = &vimvardict;
1836 else
1837 {
1838 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1839 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1840 }
1841 if (d == NULL)
1842 {
1843 internal_error("do_unlet()");
1844 return FAIL;
1845 }
1846 }
1847 hi = hash_find(ht, varname);
1848 if (HASHITEM_EMPTY(hi))
1849 hi = find_hi_in_scoped_ht(name, &ht);
1850 if (hi != NULL && !HASHITEM_EMPTY(hi))
1851 {
1852 di = HI2DI(hi);
1853 if (var_check_fixed(di->di_flags, name, FALSE)
1854 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001855 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001856 return FAIL;
1857
1858 delete_var(ht, hi);
1859 return OK;
1860 }
1861 }
1862 if (forceit)
1863 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001864 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 return FAIL;
1866}
1867
1868/*
1869 * Lock or unlock variable indicated by "lp".
1870 * "deep" is the levels to go (-1 for unlimited);
1871 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1872 */
1873 static int
1874do_lock_var(
1875 lval_T *lp,
1876 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001877 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001879 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001881 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001882 int ret = OK;
1883 int cc;
1884 dictitem_T *di;
1885
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001886 if (lp->ll_tv == NULL)
1887 {
1888 cc = *name_end;
1889 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001890 if (*lp->ll_name == '$')
1891 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001892 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001894 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895 else
1896 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001897 // Normal name or expanded name.
1898 di = find_var(lp->ll_name, NULL, TRUE);
1899 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001900 {
1901 if (in_vim9script())
1902 semsg(_(e_cannot_find_variable_to_unlock_str),
1903 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001904 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001905 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001906 else if ((di->di_flags & DI_FLAGS_FIX)
1907 && di->di_tv.v_type != VAR_DICT
1908 && di->di_tv.v_type != VAR_LIST)
1909 {
1910 // For historic reasons this error is not given for a list or
1911 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001912 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001913 ret = FAIL;
1914 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001915 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001916 {
1917 if (lock)
1918 di->di_flags |= DI_FLAGS_LOCK;
1919 else
1920 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001921 if (deep != 0)
1922 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001923 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001924 }
1925 *name_end = cc;
1926 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001927 else if (deep == 0)
1928 {
1929 // nothing to do
1930 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001931 else if (lp->ll_range)
1932 {
1933 listitem_T *li = lp->ll_li;
1934
1935 // (un)lock a range of List items.
1936 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1937 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001938 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001939 li = li->li_next;
1940 ++lp->ll_n1;
1941 }
1942 }
1943 else if (lp->ll_list != NULL)
1944 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001945 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001946 else
1947 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001948 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001949
1950 return ret;
1951}
1952
1953/*
1954 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001955 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1956 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001958 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001959item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001960{
1961 static int recurse = 0;
1962 list_T *l;
1963 listitem_T *li;
1964 dict_T *d;
1965 blob_T *b;
1966 hashitem_T *hi;
1967 int todo;
1968
1969 if (recurse >= DICT_MAXNEST)
1970 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001971 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001972 return;
1973 }
1974 if (deep == 0)
1975 return;
1976 ++recurse;
1977
1978 // lock/unlock the item itself
1979 if (lock)
1980 tv->v_lock |= VAR_LOCKED;
1981 else
1982 tv->v_lock &= ~VAR_LOCKED;
1983
1984 switch (tv->v_type)
1985 {
1986 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001987 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001989 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001991 case VAR_STRING:
1992 case VAR_FUNC:
1993 case VAR_PARTIAL:
1994 case VAR_FLOAT:
1995 case VAR_SPECIAL:
1996 case VAR_JOB:
1997 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001998 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001999 break;
2000
2001 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002002 if ((b = tv->vval.v_blob) != NULL
2003 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002004 {
2005 if (lock)
2006 b->bv_lock |= VAR_LOCKED;
2007 else
2008 b->bv_lock &= ~VAR_LOCKED;
2009 }
2010 break;
2011 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002012 if ((l = tv->vval.v_list) != NULL
2013 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002014 {
2015 if (lock)
2016 l->lv_lock |= VAR_LOCKED;
2017 else
2018 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002019 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002020 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002021 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002022 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002023 }
2024 break;
2025 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002026 if ((d = tv->vval.v_dict) != NULL
2027 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002028 {
2029 if (lock)
2030 d->dv_lock |= VAR_LOCKED;
2031 else
2032 d->dv_lock &= ~VAR_LOCKED;
2033 if (deep < 0 || deep > 1)
2034 {
2035 // recursive: lock/unlock the items the List contains
2036 todo = (int)d->dv_hashtab.ht_used;
2037 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2038 {
2039 if (!HASHITEM_EMPTY(hi))
2040 {
2041 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002042 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2043 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002044 }
2045 }
2046 }
2047 }
2048 }
2049 --recurse;
2050}
2051
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002052#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2053/*
2054 * Delete all "menutrans_" variables.
2055 */
2056 void
2057del_menutrans_vars(void)
2058{
2059 hashitem_T *hi;
2060 int todo;
2061
2062 hash_lock(&globvarht);
2063 todo = (int)globvarht.ht_used;
2064 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2065 {
2066 if (!HASHITEM_EMPTY(hi))
2067 {
2068 --todo;
2069 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2070 delete_var(&globvarht, hi);
2071 }
2072 }
2073 hash_unlock(&globvarht);
2074}
2075#endif
2076
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002077/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002078 * Local string buffer for the next two functions to store a variable name
2079 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2080 * get_user_var_name().
2081 */
2082
2083static char_u *varnamebuf = NULL;
2084static int varnamebuflen = 0;
2085
2086/*
2087 * Function to concatenate a prefix and a variable name.
2088 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002089 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002090cat_prefix_varname(int prefix, char_u *name)
2091{
2092 int len;
2093
2094 len = (int)STRLEN(name) + 3;
2095 if (len > varnamebuflen)
2096 {
2097 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002098 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002099 varnamebuf = alloc(len);
2100 if (varnamebuf == NULL)
2101 {
2102 varnamebuflen = 0;
2103 return NULL;
2104 }
2105 varnamebuflen = len;
2106 }
2107 *varnamebuf = prefix;
2108 varnamebuf[1] = ':';
2109 STRCPY(varnamebuf + 2, name);
2110 return varnamebuf;
2111}
2112
2113/*
2114 * Function given to ExpandGeneric() to obtain the list of user defined
2115 * (global/buffer/window/built-in) variable names.
2116 */
2117 char_u *
2118get_user_var_name(expand_T *xp, int idx)
2119{
2120 static long_u gdone;
2121 static long_u bdone;
2122 static long_u wdone;
2123 static long_u tdone;
2124 static int vidx;
2125 static hashitem_T *hi;
2126 hashtab_T *ht;
2127
2128 if (idx == 0)
2129 {
2130 gdone = bdone = wdone = vidx = 0;
2131 tdone = 0;
2132 }
2133
2134 // Global variables
2135 if (gdone < globvarht.ht_used)
2136 {
2137 if (gdone++ == 0)
2138 hi = globvarht.ht_array;
2139 else
2140 ++hi;
2141 while (HASHITEM_EMPTY(hi))
2142 ++hi;
2143 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2144 return cat_prefix_varname('g', hi->hi_key);
2145 return hi->hi_key;
2146 }
2147
2148 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002149 ht =
2150#ifdef FEAT_CMDWIN
2151 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002152 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002153#endif
2154 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002155 if (bdone < ht->ht_used)
2156 {
2157 if (bdone++ == 0)
2158 hi = ht->ht_array;
2159 else
2160 ++hi;
2161 while (HASHITEM_EMPTY(hi))
2162 ++hi;
2163 return cat_prefix_varname('b', hi->hi_key);
2164 }
2165
2166 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002167 ht =
2168#ifdef FEAT_CMDWIN
2169 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002170 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002171#endif
2172 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002173 if (wdone < ht->ht_used)
2174 {
2175 if (wdone++ == 0)
2176 hi = ht->ht_array;
2177 else
2178 ++hi;
2179 while (HASHITEM_EMPTY(hi))
2180 ++hi;
2181 return cat_prefix_varname('w', hi->hi_key);
2182 }
2183
2184 // t: variables
2185 ht = &curtab->tp_vars->dv_hashtab;
2186 if (tdone < ht->ht_used)
2187 {
2188 if (tdone++ == 0)
2189 hi = ht->ht_array;
2190 else
2191 ++hi;
2192 while (HASHITEM_EMPTY(hi))
2193 ++hi;
2194 return cat_prefix_varname('t', hi->hi_key);
2195 }
2196
2197 // v: variables
2198 if (vidx < VV_LEN)
2199 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2200
2201 VIM_CLEAR(varnamebuf);
2202 varnamebuflen = 0;
2203 return NULL;
2204}
2205
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002206 char *
2207get_var_special_name(int nr)
2208{
2209 switch (nr)
2210 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002211 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2212 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002213 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002214 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002215 }
2216 internal_error("get_var_special_name()");
2217 return "42";
2218}
2219
2220/*
2221 * Returns the global variable dictionary
2222 */
2223 dict_T *
2224get_globvar_dict(void)
2225{
2226 return &globvardict;
2227}
2228
2229/*
2230 * Returns the global variable hash table
2231 */
2232 hashtab_T *
2233get_globvar_ht(void)
2234{
2235 return &globvarht;
2236}
2237
2238/*
2239 * Returns the v: variable dictionary
2240 */
2241 dict_T *
2242get_vimvar_dict(void)
2243{
2244 return &vimvardict;
2245}
2246
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002247/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002249 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 */
2251 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002252find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002254 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2255 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256
2257 if (di == NULL)
2258 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002259 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002260 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2261 return (int)(vv - vimvars);
2262}
2263
2264
2265/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002266 * Set type of v: variable to "type".
2267 */
2268 void
2269set_vim_var_type(int idx, vartype_T type)
2270{
Bram Moolenaard787e402021-12-24 21:36:12 +00002271 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002272}
2273
2274/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002275 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002276 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002277 */
2278 void
2279set_vim_var_nr(int idx, varnumber_T val)
2280{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002281 vimvars[idx].vv_nr = val;
2282}
2283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 char *
2285get_vim_var_name(int idx)
2286{
2287 return vimvars[idx].vv_name;
2288}
2289
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002290/*
2291 * Get typval_T v: variable value.
2292 */
2293 typval_T *
2294get_vim_var_tv(int idx)
2295{
2296 return &vimvars[idx].vv_tv;
2297}
2298
Bram Moolenaard787e402021-12-24 21:36:12 +00002299 type_T *
2300get_vim_var_type(int idx, garray_T *type_list)
2301{
2302 if (vimvars[idx].vv_type != NULL)
2303 return vimvars[idx].vv_type;
2304 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2305}
2306
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002307/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002308 * Set v: variable to "tv". Only accepts the same type.
2309 * Takes over the value of "tv".
2310 */
2311 int
2312set_vim_var_tv(int idx, typval_T *tv)
2313{
Bram Moolenaard787e402021-12-24 21:36:12 +00002314 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002315 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002316 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002317 clear_tv(tv);
2318 return FAIL;
2319 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002320 // VV_RO is also checked when compiling, but let's check here as well.
2321 if (vimvars[idx].vv_flags & VV_RO)
2322 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002323 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002324 return FAIL;
2325 }
2326 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2327 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002328 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002329 return FAIL;
2330 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002331 clear_tv(&vimvars[idx].vv_di.di_tv);
2332 vimvars[idx].vv_di.di_tv = *tv;
2333 return OK;
2334}
2335
2336/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002337 * Get number v: variable value.
2338 */
2339 varnumber_T
2340get_vim_var_nr(int idx)
2341{
2342 return vimvars[idx].vv_nr;
2343}
2344
2345/*
2346 * Get string v: variable value. Uses a static buffer, can only be used once.
2347 * If the String variable has never been set, return an empty string.
2348 * Never returns NULL;
2349 */
2350 char_u *
2351get_vim_var_str(int idx)
2352{
2353 return tv_get_string(&vimvars[idx].vv_tv);
2354}
2355
2356/*
2357 * Get List v: variable value. Caller must take care of reference count when
2358 * needed.
2359 */
2360 list_T *
2361get_vim_var_list(int idx)
2362{
2363 return vimvars[idx].vv_list;
2364}
2365
2366/*
2367 * Get Dict v: variable value. Caller must take care of reference count when
2368 * needed.
2369 */
2370 dict_T *
2371get_vim_var_dict(int idx)
2372{
2373 return vimvars[idx].vv_dict;
2374}
2375
2376/*
2377 * Set v:char to character "c".
2378 */
2379 void
2380set_vim_var_char(int c)
2381{
2382 char_u buf[MB_MAXBYTES + 1];
2383
2384 if (has_mbyte)
2385 buf[(*mb_char2bytes)(c, buf)] = NUL;
2386 else
2387 {
2388 buf[0] = c;
2389 buf[1] = NUL;
2390 }
2391 set_vim_var_string(VV_CHAR, buf, -1);
2392}
2393
2394/*
2395 * Set v:count to "count" and v:count1 to "count1".
2396 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2397 */
2398 void
2399set_vcount(
2400 long count,
2401 long count1,
2402 int set_prevcount)
2403{
2404 if (set_prevcount)
2405 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2406 vimvars[VV_COUNT].vv_nr = count;
2407 vimvars[VV_COUNT1].vv_nr = count1;
2408}
2409
2410/*
2411 * Save variables that might be changed as a side effect. Used when executing
2412 * a timer callback.
2413 */
2414 void
2415save_vimvars(vimvars_save_T *vvsave)
2416{
2417 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2418 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2419 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2420}
2421
2422/*
2423 * Restore variables saved by save_vimvars().
2424 */
2425 void
2426restore_vimvars(vimvars_save_T *vvsave)
2427{
2428 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2429 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2430 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2431}
2432
2433/*
2434 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2435 * value.
2436 */
2437 void
2438set_vim_var_string(
2439 int idx,
2440 char_u *val,
2441 int len) // length of "val" to use or -1 (whole string)
2442{
2443 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002444 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002445 if (val == NULL)
2446 vimvars[idx].vv_str = NULL;
2447 else if (len == -1)
2448 vimvars[idx].vv_str = vim_strsave(val);
2449 else
2450 vimvars[idx].vv_str = vim_strnsave(val, len);
2451}
2452
2453/*
2454 * Set List v: variable to "val".
2455 */
2456 void
2457set_vim_var_list(int idx, list_T *val)
2458{
2459 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002460 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002461 vimvars[idx].vv_list = val;
2462 if (val != NULL)
2463 ++val->lv_refcount;
2464}
2465
2466/*
2467 * Set Dictionary v: variable to "val".
2468 */
2469 void
2470set_vim_var_dict(int idx, dict_T *val)
2471{
2472 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002473 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002474 vimvars[idx].vv_dict = val;
2475 if (val != NULL)
2476 {
2477 ++val->dv_refcount;
2478 dict_set_items_ro(val);
2479 }
2480}
2481
2482/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002483 * Set the v:argv list.
2484 */
2485 void
2486set_argv_var(char **argv, int argc)
2487{
2488 list_T *l = list_alloc();
2489 int i;
2490
2491 if (l == NULL)
2492 getout(1);
2493 l->lv_lock = VAR_FIXED;
2494 for (i = 0; i < argc; ++i)
2495 {
2496 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2497 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002498 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002499 }
2500 set_vim_var_list(VV_ARGV, l);
2501}
2502
2503/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002504 * Reset v:register, taking the 'clipboard' setting into account.
2505 */
2506 void
2507reset_reg_var(void)
2508{
2509 int regname = 0;
2510
2511 // Adjust the register according to 'clipboard', so that when
2512 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2513#ifdef FEAT_CLIPBOARD
2514 adjust_clip_reg(&regname);
2515#endif
2516 set_reg_var(regname);
2517}
2518
2519/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002520 * Set v:register if needed.
2521 */
2522 void
2523set_reg_var(int c)
2524{
2525 char_u regname;
2526
2527 if (c == 0 || c == ' ')
2528 regname = '"';
2529 else
2530 regname = c;
2531 // Avoid free/alloc when the value is already right.
2532 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2533 set_vim_var_string(VV_REG, &regname, 1);
2534}
2535
2536/*
2537 * Get or set v:exception. If "oldval" == NULL, return the current value.
2538 * Otherwise, restore the value to "oldval" and return NULL.
2539 * Must always be called in pairs to save and restore v:exception! Does not
2540 * take care of memory allocations.
2541 */
2542 char_u *
2543v_exception(char_u *oldval)
2544{
2545 if (oldval == NULL)
2546 return vimvars[VV_EXCEPTION].vv_str;
2547
2548 vimvars[VV_EXCEPTION].vv_str = oldval;
2549 return NULL;
2550}
2551
2552/*
2553 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2554 * Otherwise, restore the value to "oldval" and return NULL.
2555 * Must always be called in pairs to save and restore v:throwpoint! Does not
2556 * take care of memory allocations.
2557 */
2558 char_u *
2559v_throwpoint(char_u *oldval)
2560{
2561 if (oldval == NULL)
2562 return vimvars[VV_THROWPOINT].vv_str;
2563
2564 vimvars[VV_THROWPOINT].vv_str = oldval;
2565 return NULL;
2566}
2567
2568/*
2569 * Set v:cmdarg.
2570 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2571 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2572 * Must always be called in pairs!
2573 */
2574 char_u *
2575set_cmdarg(exarg_T *eap, char_u *oldarg)
2576{
2577 char_u *oldval;
2578 char_u *newval;
2579 unsigned len;
2580
2581 oldval = vimvars[VV_CMDARG].vv_str;
2582 if (eap == NULL)
2583 {
2584 vim_free(oldval);
2585 vimvars[VV_CMDARG].vv_str = oldarg;
2586 return NULL;
2587 }
2588
2589 if (eap->force_bin == FORCE_BIN)
2590 len = 6;
2591 else if (eap->force_bin == FORCE_NOBIN)
2592 len = 8;
2593 else
2594 len = 0;
2595
2596 if (eap->read_edit)
2597 len += 7;
2598
2599 if (eap->force_ff != 0)
2600 len += 10; // " ++ff=unix"
2601 if (eap->force_enc != 0)
2602 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2603 if (eap->bad_char != 0)
2604 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2605
2606 newval = alloc(len + 1);
2607 if (newval == NULL)
2608 return NULL;
2609
2610 if (eap->force_bin == FORCE_BIN)
2611 sprintf((char *)newval, " ++bin");
2612 else if (eap->force_bin == FORCE_NOBIN)
2613 sprintf((char *)newval, " ++nobin");
2614 else
2615 *newval = NUL;
2616
2617 if (eap->read_edit)
2618 STRCAT(newval, " ++edit");
2619
2620 if (eap->force_ff != 0)
2621 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2622 eap->force_ff == 'u' ? "unix"
2623 : eap->force_ff == 'd' ? "dos"
2624 : "mac");
2625 if (eap->force_enc != 0)
2626 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2627 eap->cmd + eap->force_enc);
2628 if (eap->bad_char == BAD_KEEP)
2629 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2630 else if (eap->bad_char == BAD_DROP)
2631 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2632 else if (eap->bad_char != 0)
2633 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2634 vimvars[VV_CMDARG].vv_str = newval;
2635 return oldval;
2636}
2637
2638/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002639 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002640 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2641 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002642 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2643 */
2644 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002645eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002646 char_u *name,
2647 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002648 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002649 typval_T *rettv, // NULL when only checking existence
2650 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002651 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002652{
2653 int ret = OK;
2654 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002655 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002656 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002657 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002658 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002659
2660 // truncate the name, so that we can use strcmp()
2661 cc = name[len];
2662 name[len] = NUL;
2663
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002664 // Check for local variable when debugging.
2665 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002666 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002667 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002668 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2669
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002670 if (v != NULL)
2671 {
2672 tv = &v->di_tv;
2673 if (dip != NULL)
2674 *dip = v;
2675 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002676 else
2677 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002678 }
2679
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002680 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002682 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002683 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2684
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002685 if (sid == 0)
2686 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687
2688 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002689 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002691 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002692 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002693 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002694 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002695 ht = &SCRIPT_VARS(sid);
2696 if (ht != NULL)
2697 {
2698 dictitem_T *v = find_var_in_ht(ht, 0, name,
2699 flags & EVAL_VAR_NOAUTOLOAD);
2700
2701 if (v != NULL)
2702 {
2703 tv = &v->di_tv;
2704 if (dip != NULL)
2705 *dip = v;
2706 }
2707 else
2708 ht = NULL;
2709 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002710 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002711 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002712 {
2713 if (flags & EVAL_VAR_VERBOSE)
2714 emsg(_(e_import_as_name_not_supported_here));
2715 ret = FAIL;
2716 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002717 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002718 else
2719 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002720 if (rettv != NULL)
2721 {
2722 rettv->v_type = VAR_ANY;
2723 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2724 }
2725 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002726 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002728 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002729 {
2730 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2731
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002732 // In Vim9 script we can get a function reference by using the
2733 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002734 if (ufunc != NULL)
2735 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002736 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002737 if (rettv != NULL)
2738 {
2739 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002740 if (STRNCMP(name, "g:", 2) == 0)
2741 // Keep the "g:", otherwise script-local may be
2742 // assumed.
2743 rettv->vval.v_string = vim_strsave(name);
2744 else
2745 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002746 if (rettv->vval.v_string != NULL)
2747 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002748 }
2749 }
2750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 }
2752
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002753 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002754 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002755 if (tv == NULL)
2756 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002757 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002758 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002759 ret = FAIL;
2760 }
2761 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002762 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002763 if (ht != NULL && ht == get_script_local_ht()
2764 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002765 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002766 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002767
Bram Moolenaarf055d452021-07-08 20:57:24 +02002768 if (sv != NULL)
2769 type = sv->sv_type;
2770 }
2771
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002772 // If a list or dict variable wasn't initialized, do it now.
2773 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2774 {
2775 tv->vval.v_dict = dict_alloc();
2776 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002777 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002778 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002779 tv->vval.v_dict->dv_type = alloc_type(type);
2780 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002781 }
2782 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2783 {
2784 tv->vval.v_list = list_alloc();
2785 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002786 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002787 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002788 tv->vval.v_list->lv_type = alloc_type(type);
2789 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002790 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002791 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2792 {
2793 tv->vval.v_blob = blob_alloc();
2794 if (tv->vval.v_blob != NULL)
2795 ++tv->vval.v_blob->bv_refcount;
2796 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002797 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002798 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002799 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002800
2801 name[len] = cc;
2802
2803 return ret;
2804}
2805
2806/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002807 * Check if variable "name[len]" is a local variable or an argument.
2808 * If so, "*eval_lavars_used" is set to TRUE.
2809 */
2810 void
2811check_vars(char_u *name, int len)
2812{
2813 int cc;
2814 char_u *varname;
2815 hashtab_T *ht;
2816
2817 if (eval_lavars_used == NULL)
2818 return;
2819
2820 // truncate the name, so that we can use strcmp()
2821 cc = name[len];
2822 name[len] = NUL;
2823
2824 ht = find_var_ht(name, &varname);
2825 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2826 {
2827 if (find_var(name, NULL, TRUE) != NULL)
2828 *eval_lavars_used = TRUE;
2829 }
2830
2831 name[len] = cc;
2832}
2833
2834/*
2835 * Find variable "name" in the list of variables.
2836 * Return a pointer to it if found, NULL if not found.
2837 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002838 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002839 */
2840 dictitem_T *
2841find_var(char_u *name, hashtab_T **htp, int no_autoload)
2842{
2843 char_u *varname;
2844 hashtab_T *ht;
2845 dictitem_T *ret = NULL;
2846
2847 ht = find_var_ht(name, &varname);
2848 if (htp != NULL)
2849 *htp = ht;
2850 if (ht == NULL)
2851 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002852 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002853 if (ret != NULL)
2854 return ret;
2855
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002856 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002857 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002858 if (ret != NULL)
2859 return ret;
2860
2861 // in Vim9 script items without a scope can be script-local
2862 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2863 {
2864 ht = get_script_local_ht();
2865 if (ht != NULL)
2866 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002867 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002868 if (ret != NULL)
2869 {
2870 if (htp != NULL)
2871 *htp = ht;
2872 return ret;
2873 }
2874 }
2875 }
2876
2877 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002878}
2879
2880/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002881 * Like find_var() but if the name starts with <SNR>99_ then look in the
2882 * referenced script (used for a funcref).
2883 */
2884 dictitem_T *
2885find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2886{
2887 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2888 {
2889 char_u *p = name + 5;
2890 int sid = getdigits(&p);
2891
2892 if (SCRIPT_ID_VALID(sid) && *p == '_')
2893 {
2894 hashtab_T *ht = &SCRIPT_VARS(sid);
2895
2896 if (ht != NULL)
2897 {
2898 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2899
2900 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002901 {
2902 if (htp != NULL)
2903 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002904 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002905 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002906 }
2907 }
2908 }
2909
2910 return find_var(name, htp, no_autoload);
2911}
2912
2913/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002914 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002915 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002916 * Returns NULL if not found.
2917 */
2918 dictitem_T *
2919find_var_in_ht(
2920 hashtab_T *ht,
2921 int htname,
2922 char_u *varname,
2923 int no_autoload)
2924{
2925 hashitem_T *hi;
2926
2927 if (*varname == NUL)
2928 {
2929 // Must be something like "s:", otherwise "ht" would be NULL.
2930 switch (htname)
2931 {
2932 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2933 case 'g': return &globvars_var;
2934 case 'v': return &vimvars_var;
2935 case 'b': return &curbuf->b_bufvar;
2936 case 'w': return &curwin->w_winvar;
2937 case 't': return &curtab->tp_winvar;
2938 case 'l': return get_funccal_local_var();
2939 case 'a': return get_funccal_args_var();
2940 }
2941 return NULL;
2942 }
2943
2944 hi = hash_find(ht, varname);
2945 if (HASHITEM_EMPTY(hi))
2946 {
2947 // For global variables we may try auto-loading the script. If it
2948 // worked find the variable again. Don't auto-load a script if it was
2949 // loaded already, otherwise it would be loaded every time when
2950 // checking if a function name is a Funcref variable.
2951 if (ht == &globvarht && !no_autoload)
2952 {
2953 // Note: script_autoload() may make "hi" invalid. It must either
2954 // be obtained again or not used.
2955 if (!script_autoload(varname, FALSE) || aborting())
2956 return NULL;
2957 hi = hash_find(ht, varname);
2958 }
2959 if (HASHITEM_EMPTY(hi))
2960 return NULL;
2961 }
2962 return HI2DI(hi);
2963}
2964
2965/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 * Get the script-local hashtab. NULL if not in a script context.
2967 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002968 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969get_script_local_ht(void)
2970{
2971 scid_T sid = current_sctx.sc_sid;
2972
Bram Moolenaare3d46852020-08-29 13:39:17 +02002973 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 return &SCRIPT_VARS(sid);
2975 return NULL;
2976}
2977
2978/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002979 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002980 * When "cmd" is TRUE it must look like a command, a function must be followed
2981 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002982 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002984 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002985lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002986 char_u *name,
2987 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002988 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002989 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990{
2991 hashtab_T *ht = get_script_local_ht();
2992 char_u buffer[30];
2993 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002994 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002996 int is_global = FALSE;
2997 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998
2999 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003000 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 if (len < sizeof(buffer) - 1)
3002 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003003 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 vim_strncpy(buffer, name, len);
3005 p = buffer;
3006 }
3007 else
3008 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003009 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003011 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 }
3013
3014 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003015 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016
3017 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01003018 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
3019 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 if (p != buffer)
3021 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003022
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003023 // Find a function, so that a following "->" works.
3024 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3025 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003026 if (res != OK)
3027 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003028 p = skipwhite(name + len);
3029
3030 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003031 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003032 // Do not check for an internal function, since it might also be a
3033 // valid command, such as ":split" versus "split()".
3034 // Skip "g:" before a function name.
3035 if (name[0] == 'g' && name[1] == ':')
3036 {
3037 is_global = TRUE;
3038 fname = name + 2;
3039 }
3040 if (find_func(fname, is_global, NULL) != NULL)
3041 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003042 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003043 }
3044
Bram Moolenaar709664c2020-12-12 14:33:41 +01003045 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046}
3047
3048/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003049 * Find the hashtab used for a variable name.
3050 * Return NULL if the name is not valid.
3051 * Set "varname" to the start of name without ':'.
3052 */
3053 hashtab_T *
3054find_var_ht(char_u *name, char_u **varname)
3055{
3056 hashitem_T *hi;
3057 hashtab_T *ht;
3058
3059 if (name[0] == NUL)
3060 return NULL;
3061 if (name[1] != ':')
3062 {
3063 // The name must not start with a colon or #.
3064 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3065 return NULL;
3066 *varname = name;
3067
3068 // "version" is "v:version" in all scopes if scriptversion < 3.
3069 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003070 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003071 {
3072 hi = hash_find(&compat_hashtab, name);
3073 if (!HASHITEM_EMPTY(hi))
3074 return &compat_hashtab;
3075 }
3076
3077 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 if (ht != NULL)
3079 return ht; // local variable
3080
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003081 // In Vim9 script items at the script level are script-local, except
3082 // for autoload names.
3083 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 {
3085 ht = get_script_local_ht();
3086 if (ht != NULL)
3087 return ht;
3088 }
3089
3090 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003091 }
3092 *varname = name + 2;
3093 if (*name == 'g') // global variable
3094 return &globvarht;
3095 // There must be no ':' or '#' in the rest of the name, unless g: is used
3096 if (vim_strchr(name + 2, ':') != NULL
3097 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3098 return NULL;
3099 if (*name == 'b') // buffer variable
3100 return &curbuf->b_vars->dv_hashtab;
3101 if (*name == 'w') // window variable
3102 return &curwin->w_vars->dv_hashtab;
3103 if (*name == 't') // tab page variable
3104 return &curtab->tp_vars->dv_hashtab;
3105 if (*name == 'v') // v: variable
3106 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003107 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003108 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003110 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 if (*name == 'a') // a: function argument
3112 return get_funccal_args_ht();
3113 if (*name == 'l') // l: local function variable
3114 return get_funccal_local_ht();
3115 }
3116 if (*name == 's') // script variable
3117 {
3118 ht = get_script_local_ht();
3119 if (ht != NULL)
3120 return ht;
3121 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003122 return NULL;
3123}
3124
3125/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003126 * Get the string value of a (global/local) variable.
3127 * Note: see tv_get_string() for how long the pointer remains valid.
3128 * Returns NULL when it doesn't exist.
3129 */
3130 char_u *
3131get_var_value(char_u *name)
3132{
3133 dictitem_T *v;
3134
3135 v = find_var(name, NULL, FALSE);
3136 if (v == NULL)
3137 return NULL;
3138 return tv_get_string(&v->di_tv);
3139}
3140
3141/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003142 * Allocate a new hashtab for a sourced script. It will be used while
3143 * sourcing this script and when executing functions defined in the script.
3144 */
3145 void
3146new_script_vars(scid_T id)
3147{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003148 scriptvar_T *sv;
3149
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003150 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3151 if (sv == NULL)
3152 return;
3153 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003154 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003155}
3156
3157/*
3158 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3159 * point to it.
3160 */
3161 void
3162init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3163{
3164 hash_init(&dict->dv_hashtab);
3165 dict->dv_lock = 0;
3166 dict->dv_scope = scope;
3167 dict->dv_refcount = DO_NOT_FREE_CNT;
3168 dict->dv_copyID = 0;
3169 dict_var->di_tv.vval.v_dict = dict;
3170 dict_var->di_tv.v_type = VAR_DICT;
3171 dict_var->di_tv.v_lock = VAR_FIXED;
3172 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3173 dict_var->di_key[0] = NUL;
3174}
3175
3176/*
3177 * Unreference a dictionary initialized by init_var_dict().
3178 */
3179 void
3180unref_var_dict(dict_T *dict)
3181{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003182 // Now the dict needs to be freed if no one else is using it, go back to
3183 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003184 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3185 dict_unref(dict);
3186}
3187
3188/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003189 * Clean up a list of internal variables.
3190 * Frees all allocated variables and the value they contain.
3191 * Clears hashtab "ht", does not free it.
3192 */
3193 void
3194vars_clear(hashtab_T *ht)
3195{
3196 vars_clear_ext(ht, TRUE);
3197}
3198
3199/*
3200 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3201 */
3202 void
3203vars_clear_ext(hashtab_T *ht, int free_val)
3204{
3205 int todo;
3206 hashitem_T *hi;
3207 dictitem_T *v;
3208
3209 hash_lock(ht);
3210 todo = (int)ht->ht_used;
3211 for (hi = ht->ht_array; todo > 0; ++hi)
3212 {
3213 if (!HASHITEM_EMPTY(hi))
3214 {
3215 --todo;
3216
3217 // Free the variable. Don't remove it from the hashtab,
3218 // ht_array might change then. hash_clear() takes care of it
3219 // later.
3220 v = HI2DI(hi);
3221 if (free_val)
3222 clear_tv(&v->di_tv);
3223 if (v->di_flags & DI_FLAGS_ALLOC)
3224 vim_free(v);
3225 }
3226 }
3227 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003228 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003229}
3230
3231/*
3232 * Delete a variable from hashtab "ht" at item "hi".
3233 * Clear the variable value and free the dictitem.
3234 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003235 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003236delete_var(hashtab_T *ht, hashitem_T *hi)
3237{
3238 dictitem_T *di = HI2DI(hi);
3239
3240 hash_remove(ht, hi);
3241 clear_tv(&di->di_tv);
3242 vim_free(di);
3243}
3244
3245/*
3246 * List the value of one internal variable.
3247 */
3248 static void
3249list_one_var(dictitem_T *v, char *prefix, int *first)
3250{
3251 char_u *tofree;
3252 char_u *s;
3253 char_u numbuf[NUMBUFLEN];
3254
3255 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3256 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3257 s == NULL ? (char_u *)"" : s, first);
3258 vim_free(tofree);
3259}
3260
3261 static void
3262list_one_var_a(
3263 char *prefix,
3264 char_u *name,
3265 int type,
3266 char_u *string,
3267 int *first) // when TRUE clear rest of screen and set to FALSE
3268{
3269 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3270 msg_start();
3271 msg_puts(prefix);
3272 if (name != NULL) // "a:" vars don't have a name stored
3273 msg_puts((char *)name);
3274 msg_putchar(' ');
3275 msg_advance(22);
3276 if (type == VAR_NUMBER)
3277 msg_putchar('#');
3278 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3279 msg_putchar('*');
3280 else if (type == VAR_LIST)
3281 {
3282 msg_putchar('[');
3283 if (*string == '[')
3284 ++string;
3285 }
3286 else if (type == VAR_DICT)
3287 {
3288 msg_putchar('{');
3289 if (*string == '{')
3290 ++string;
3291 }
3292 else
3293 msg_putchar(' ');
3294
3295 msg_outtrans(string);
3296
3297 if (type == VAR_FUNC || type == VAR_PARTIAL)
3298 msg_puts("()");
3299 if (*first)
3300 {
3301 msg_clr_eos();
3302 *first = FALSE;
3303 }
3304}
3305
3306/*
3307 * Set variable "name" to value in "tv".
3308 * If the variable already exists, the value is updated.
3309 * Otherwise the variable is created.
3310 */
3311 void
3312set_var(
3313 char_u *name,
3314 typval_T *tv,
3315 int copy) // make copy of value in "tv"
3316{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003317 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003318}
3319
3320/*
3321 * Set variable "name" to value in "tv".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003322 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003323 * If the variable already exists and "is_const" is FALSE the value is updated.
3324 * Otherwise the variable is created.
3325 */
3326 void
3327set_var_const(
3328 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003329 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003330 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003331 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003332 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003333 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003334 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003335{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003336 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003337 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003338 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003340 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003341 char_u *varname;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003342 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003344 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003345 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003346 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003347 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003348
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003349 if (sid != 0)
3350 {
3351 if (SCRIPT_ID_VALID(sid))
3352 ht = &SCRIPT_VARS(sid);
3353 varname = name;
3354 }
3355 else
3356 ht = find_var_ht(name, &varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003357 if (ht == NULL || *varname == NUL)
3358 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003359 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003360 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003361 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003362 is_script_local = ht == get_script_local_ht() || sid != 0;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003363
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003364 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003365 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003366 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003367 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003368 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003369 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003370 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003371 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003372 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003373 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3374 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3375 // Do not make g:var, w:var, b:var or t:var final.
3376 flags &= ~ASSIGN_FINAL;
3377
Bram Moolenaare535db82021-03-31 21:07:24 +02003378 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003379 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3380 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003381 // For "[a, _] = list" the underscore is ignored.
3382 if ((flags & ASSIGN_UNPACK) == 0)
3383 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003384 goto failed;
3385 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003388
Bram Moolenaar24e93162021-07-18 20:40:33 +02003389 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003390 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003391 imported_T *import = find_imported(varname, 0, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003392
Bram Moolenaar24e93162021-07-18 20:40:33 +02003393 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003394 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003395 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003396 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003398 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003399 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003401 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3402 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003403 }
3404 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405
Bram Moolenaar24e93162021-07-18 20:40:33 +02003406 if (dest_tv == NULL)
3407 {
3408 // Search in parent scope which is possible to reference from lambda
3409 if (di == NULL)
3410 di = find_var_in_scoped_ht(name, TRUE);
3411
3412 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003413 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003414 goto failed;
3415
3416 if (need_convert_to_bool(type, tv))
3417 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003418 // Destination is a bool and the value is not, but it can be
3419 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003420 CLEAR_FIELD(bool_tv);
3421 bool_tv.v_type = VAR_BOOL;
3422 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3423 tv = &bool_tv;
3424 }
3425
3426 if (di != NULL)
3427 {
3428 // Item already exists. Allowed to replace when reloading.
3429 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3430 {
3431 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003432 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003433 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003434 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar24e93162021-07-18 20:40:33 +02003435 goto failed;
3436 }
3437
3438 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003439 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003440 {
3441 semsg(_(e_redefining_script_item_str), name);
3442 goto failed;
3443 }
3444
Bram Moolenaara06758d2021-10-15 00:18:37 +01003445 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003446 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003447 where_T where = WHERE_INIT;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003448 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003449
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003450 if (sv != NULL)
3451 {
3452 // check the type and adjust to bool if needed
3453 where.wt_index = var_idx;
3454 where.wt_variable = TRUE;
3455 if (check_script_var_type(sv, tv, name, where) == FAIL)
3456 goto failed;
3457 if (type == NULL)
3458 type = sv->sv_type;
3459 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003460 }
3461
Bram Moolenaara06758d2021-10-15 00:18:37 +01003462 if ((flags & ASSIGN_FOR_LOOP) == 0
3463 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003464 goto failed;
3465 }
3466 else
3467 {
3468 // can only redefine once
3469 di->di_flags &= ~DI_FLAGS_RELOAD;
3470
Bram Moolenaarf6488542021-07-18 21:24:50 +02003471 // A Vim9 script-local variable is also present in sn_all_vars
3472 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaar24e93162021-07-18 20:40:33 +02003473 if (var_in_vim9script)
3474 update_vim9_script_var(FALSE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003475 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003476 }
3477
3478 // existing variable, need to clear the value
3479
3480 // Handle setting internal di: variables separately where needed to
3481 // prevent changing the type.
3482 if (ht == &vimvarht)
3483 {
3484 if (di->di_tv.v_type == VAR_STRING)
3485 {
3486 VIM_CLEAR(di->di_tv.vval.v_string);
3487 if (copy || tv->v_type != VAR_STRING)
3488 {
3489 char_u *val = tv_get_string(tv);
3490
Bram Moolenaarf6488542021-07-18 21:24:50 +02003491 // Careful: when assigning to v:errmsg and
3492 // tv_get_string() causes an error message the variable
3493 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003494 if (di->di_tv.vval.v_string == NULL)
3495 di->di_tv.vval.v_string = vim_strsave(val);
3496 }
3497 else
3498 {
3499 // Take over the string to avoid an extra alloc/free.
3500 di->di_tv.vval.v_string = tv->vval.v_string;
3501 tv->vval.v_string = NULL;
3502 }
3503 goto failed;
3504 }
3505 else if (di->di_tv.v_type == VAR_NUMBER)
3506 {
3507 di->di_tv.vval.v_number = tv_get_number(tv);
3508 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003509 set_search_direction(di->di_tv.vval.v_number
3510 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003511#ifdef FEAT_SEARCH_EXTRA
3512 else if (STRCMP(varname, "hlsearch") == 0)
3513 {
3514 no_hlsearch = !di->di_tv.vval.v_number;
3515 redraw_all_later(SOME_VALID);
3516 }
3517#endif
3518 goto failed;
3519 }
3520 else if (di->di_tv.v_type != tv->v_type)
3521 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003522 semsg(_(e_setting_str_to_value_with_wrong_type), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003523 goto failed;
3524 }
3525 }
3526
3527 clear_tv(&di->di_tv);
3528 }
3529 else
3530 {
3531 // Item not found, check if a function already exists.
3532 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003533 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003534 {
3535 semsg(_(e_redefining_script_item_str), name);
3536 goto failed;
3537 }
3538
Bram Moolenaar24e93162021-07-18 20:40:33 +02003539 // add a new variable
3540 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3541 {
3542 semsg(_(e_unknown_variable_str), name);
3543 goto failed;
3544 }
3545
3546 // Can't add "v:" or "a:" variable.
3547 if (ht == &vimvarht || ht == get_funccal_args_ht())
3548 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003549 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003550 goto failed;
3551 }
3552
3553 // Make sure the variable name is valid. In Vim9 script an autoload
3554 // variable must be prefixed with "g:".
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003555 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003556 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003557 goto failed;
3558
3559 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3560 if (di == NULL)
3561 goto failed;
3562 STRCPY(di->di_key, varname);
3563 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3564 {
3565 vim_free(di);
3566 goto failed;
3567 }
3568 di->di_flags = DI_FLAGS_ALLOC;
3569 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3570 di->di_flags |= DI_FLAGS_LOCK;
3571
3572 // A Vim9 script-local variable is also added to sn_all_vars and
3573 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003574 if (var_in_vim9script)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003575 update_vim9_script_var(TRUE, di, flags, tv, &type,
Bram Moolenaarf6488542021-07-18 21:24:50 +02003576 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003577 }
3578
Bram Moolenaar24e93162021-07-18 20:40:33 +02003579 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003580 }
3581
3582 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003583 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003584 else
3585 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003586 *dest_tv = *tv;
3587 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003588 init_tv(tv);
3589 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003590 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003591
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003592 if (vim9script && type != NULL)
3593 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003594 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003595 {
3596 if (dest_tv->vval.v_dict->dv_type != type)
3597 {
3598 free_type(dest_tv->vval.v_dict->dv_type);
3599 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3600 }
3601 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003602 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003603 {
3604 if (dest_tv->vval.v_list->lv_type != type)
3605 {
3606 free_type(dest_tv->vval.v_list->lv_type);
3607 dest_tv->vval.v_list->lv_type = alloc_type(type);
3608 }
3609 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003610 }
3611
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003612 // ":const var = value" locks the value
3613 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003614 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003615 // Like :lockvar! name: lock the value and what it contains, but only
3616 // if the reference count is up to one. That locks only literal
3617 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003618 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003619
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003620failed:
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003621 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003622 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003623}
3624
3625/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003626 * Check in this order for backwards compatibility:
3627 * - Whether the variable is read-only
3628 * - Whether the variable value is locked
3629 * - Whether the variable is locked
3630 */
3631 int
3632var_check_permission(dictitem_T *di, char_u *name)
3633{
3634 if (var_check_ro(di->di_flags, name, FALSE)
3635 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3636 || var_check_lock(di->di_flags, name, FALSE))
3637 return FAIL;
3638 return OK;
3639}
3640
3641/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003642 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3643 * Also give an error message.
3644 */
3645 int
3646var_check_ro(int flags, char_u *name, int use_gettext)
3647{
3648 if (flags & DI_FLAGS_RO)
3649 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003650 if (name == NULL)
3651 emsg(_(e_cannot_change_readonly_variable));
3652 else
3653 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003654 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003655 return TRUE;
3656 }
3657 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3658 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003659 if (name == NULL)
3660 emsg(_(e_cannot_set_variable_in_sandbox));
3661 else
3662 semsg(_(e_cannot_set_variable_in_sandbox_str),
3663 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003664 return TRUE;
3665 }
3666 return FALSE;
3667}
3668
3669/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003670 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3671 * Also give an error message.
3672 */
3673 int
3674var_check_lock(int flags, char_u *name, int use_gettext)
3675{
3676 if (flags & DI_FLAGS_LOCK)
3677 {
3678 semsg(_(e_variable_is_locked_str),
3679 use_gettext ? (char_u *)_(name) : name);
3680 return TRUE;
3681 }
3682 return FALSE;
3683}
3684
3685/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003686 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3687 * Also give an error message.
3688 */
3689 int
3690var_check_fixed(int flags, char_u *name, int use_gettext)
3691{
3692 if (flags & DI_FLAGS_FIX)
3693 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003694 if (name == NULL)
3695 emsg(_(e_cannot_delete_variable));
3696 else
3697 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003698 use_gettext ? (char_u *)_(name) : name);
3699 return TRUE;
3700 }
3701 return FALSE;
3702}
3703
3704/*
3705 * Check if a funcref is assigned to a valid variable name.
3706 * Return TRUE and give an error if not.
3707 */
3708 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003709var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003710 char_u *name, // points to start of variable name
3711 int new_var) // TRUE when creating the variable
3712{
Bram Moolenaar32154662021-03-28 21:14:06 +02003713 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3714 // the name can be used without the s: prefix.
3715 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3716 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003717 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3718 ? name[2] : name[0]))
3719 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003720 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003721 return TRUE;
3722 }
3723 // Don't allow hiding a function. When "v" is not NULL we might be
3724 // assigning another function to the same var, the type is checked
3725 // below.
3726 if (new_var && function_exists(name, FALSE))
3727 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003728 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003729 name);
3730 return TRUE;
3731 }
3732 return FALSE;
3733}
3734
3735/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003736 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3737 * value. Also give an error message, using "name" or _("name") when
3738 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003739 */
3740 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003741value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003742{
3743 if (lock & VAR_LOCKED)
3744 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003745 if (name == NULL)
3746 emsg(_(e_value_is_locked));
3747 else
3748 semsg(_(e_value_is_locked_str),
3749 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003750 return TRUE;
3751 }
3752 if (lock & VAR_FIXED)
3753 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003754 if (name == NULL)
3755 emsg(_(e_cannot_change_value));
3756 else
3757 semsg(_(e_cannot_change_value_of_str),
3758 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003759 return TRUE;
3760 }
3761 return FALSE;
3762}
3763
3764/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003765 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003766 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003767 * Return FALSE and give an error if not.
3768 */
3769 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003770valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003771{
3772 char_u *p;
3773
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003774 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003775 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003776 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003777 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003778 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003779 return FALSE;
3780 }
3781 return TRUE;
3782}
3783
3784/*
3785 * getwinvar() and gettabwinvar()
3786 */
3787 static void
3788getwinvar(
3789 typval_T *argvars,
3790 typval_T *rettv,
3791 int off) // 1 for gettabwinvar()
3792{
3793 win_T *win;
3794 char_u *varname;
3795 dictitem_T *v;
3796 tabpage_T *tp = NULL;
3797 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003798 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003799 int need_switch_win;
3800
3801 if (off == 1)
3802 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3803 else
3804 tp = curtab;
3805 win = find_win_by_nr(&argvars[off], tp);
3806 varname = tv_get_string_chk(&argvars[off + 1]);
3807 ++emsg_off;
3808
3809 rettv->v_type = VAR_STRING;
3810 rettv->vval.v_string = NULL;
3811
3812 if (win != NULL && varname != NULL)
3813 {
3814 // Set curwin to be our win, temporarily. Also set the tabpage,
3815 // otherwise the window is not valid. Only do this when needed,
3816 // autocommands get blocked.
3817 need_switch_win = !(tp == curtab && win == curwin);
3818 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003819 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003820 {
3821 if (*varname == '&')
3822 {
3823 if (varname[1] == NUL)
3824 {
3825 // get all window-local options in a dict
3826 dict_T *opts = get_winbuf_options(FALSE);
3827
3828 if (opts != NULL)
3829 {
3830 rettv_dict_set(rettv, opts);
3831 done = TRUE;
3832 }
3833 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003834 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003835 // window-local-option
3836 done = TRUE;
3837 }
3838 else
3839 {
3840 // Look up the variable.
3841 // Let getwinvar({nr}, "") return the "w:" dictionary.
3842 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3843 varname, FALSE);
3844 if (v != NULL)
3845 {
3846 copy_tv(&v->di_tv, rettv);
3847 done = TRUE;
3848 }
3849 }
3850 }
3851
3852 if (need_switch_win)
3853 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003854 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003855 }
3856
3857 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3858 // use the default return value
3859 copy_tv(&argvars[off + 2], rettv);
3860
3861 --emsg_off;
3862}
3863
3864/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003865 * Set option "varname" to the value of "varp" for the current buffer/window.
3866 */
3867 static void
3868set_option_from_tv(char_u *varname, typval_T *varp)
3869{
3870 long numval = 0;
3871 char_u *strval;
3872 char_u nbuf[NUMBUFLEN];
3873 int error = FALSE;
3874
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003875 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003876 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003877 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003878 strval = (char_u *)"0"; // avoid using "false"
3879 }
3880 else
3881 {
3882 if (!in_vim9script() || varp->v_type != VAR_STRING)
3883 numval = (long)tv_get_number_chk(varp, &error);
3884 strval = tv_get_string_buf_chk(varp, nbuf);
3885 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003886 if (!error && strval != NULL)
3887 set_option_value(varname, numval, strval, OPT_LOCAL);
3888}
3889
3890/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003891 * "setwinvar()" and "settabwinvar()" functions
3892 */
3893 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003894setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003895{
3896 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003897 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003898 int need_switch_win;
3899 char_u *varname, *winvarname;
3900 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003901 tabpage_T *tp = NULL;
3902
3903 if (check_secure())
3904 return;
3905
3906 if (off == 1)
3907 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3908 else
3909 tp = curtab;
3910 win = find_win_by_nr(&argvars[off], tp);
3911 varname = tv_get_string_chk(&argvars[off + 1]);
3912 varp = &argvars[off + 2];
3913
3914 if (win != NULL && varname != NULL && varp != NULL)
3915 {
3916 need_switch_win = !(tp == curtab && win == curwin);
3917 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003918 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003919 {
3920 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003921 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003922 else
3923 {
3924 winvarname = alloc(STRLEN(varname) + 3);
3925 if (winvarname != NULL)
3926 {
3927 STRCPY(winvarname, "w:");
3928 STRCPY(winvarname + 2, varname);
3929 set_var(winvarname, varp, TRUE);
3930 vim_free(winvarname);
3931 }
3932 }
3933 }
3934 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00003935 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003936 }
3937}
3938
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003939/*
3940 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3941 * v:option_type, and v:option_command.
3942 */
3943 void
3944reset_v_option_vars(void)
3945{
3946 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3947 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3948 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3949 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3950 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3951 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3952}
3953
3954/*
3955 * Add an assert error to v:errors.
3956 */
3957 void
3958assert_error(garray_T *gap)
3959{
3960 struct vimvar *vp = &vimvars[VV_ERRORS];
3961
Bram Moolenaard787e402021-12-24 21:36:12 +00003962 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003963 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003964 set_vim_var_list(VV_ERRORS, list_alloc());
3965 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3966}
3967
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003968 int
3969var_exists(char_u *var)
3970{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003971 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003972 char_u *name;
3973 char_u *tofree;
3974 typval_T tv;
3975 int len = 0;
3976 int n = FALSE;
3977
3978 // get_name_len() takes care of expanding curly braces
3979 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003980 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003981 if (len > 0)
3982 {
3983 if (tofree != NULL)
3984 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003985 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003986 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003987 if (n)
3988 {
3989 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003990 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003991 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
3992 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003993 if (n)
3994 clear_tv(&tv);
3995 }
3996 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003997 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003998 n = FALSE;
3999
4000 vim_free(tofree);
4001 return n;
4002}
4003
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004004static lval_T *redir_lval = NULL;
4005#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4006static garray_T redir_ga; // only valid when redir_lval is not NULL
4007static char_u *redir_endp = NULL;
4008static char_u *redir_varname = NULL;
4009
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004010 int
4011alloc_redir_lval(void)
4012{
4013 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4014 if (redir_lval == NULL)
4015 return FAIL;
4016 return OK;
4017}
4018
4019 void
4020clear_redir_lval(void)
4021{
4022 VIM_CLEAR(redir_lval);
4023}
4024
4025 void
4026init_redir_ga(void)
4027{
4028 ga_init2(&redir_ga, (int)sizeof(char), 500);
4029}
4030
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004031/*
4032 * Start recording command output to a variable
4033 * When "append" is TRUE append to an existing variable.
4034 * Returns OK if successfully completed the setup. FAIL otherwise.
4035 */
4036 int
4037var_redir_start(char_u *name, int append)
4038{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004039 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004040 typval_T tv;
4041
4042 // Catch a bad name early.
4043 if (!eval_isnamec1(*name))
4044 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004045 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004046 return FAIL;
4047 }
4048
4049 // Make a copy of the name, it is used in redir_lval until redir ends.
4050 redir_varname = vim_strsave(name);
4051 if (redir_varname == NULL)
4052 return FAIL;
4053
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004054 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004055 {
4056 var_redir_stop();
4057 return FAIL;
4058 }
4059
4060 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004061 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004062
4063 // Parse the variable name (can be a dict or list entry).
4064 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4065 FNE_CHECK_START);
4066 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4067 {
4068 clear_lval(redir_lval);
4069 if (redir_endp != NULL && *redir_endp != NUL)
4070 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004071 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004072 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004073 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004074 redir_endp = NULL; // don't store a value, only cleanup
4075 var_redir_stop();
4076 return FAIL;
4077 }
4078
4079 // check if we can write to the variable: set it to or append an empty
4080 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004081 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004082 tv.v_type = VAR_STRING;
4083 tv.vval.v_string = (char_u *)"";
4084 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004085 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004086 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004087 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004088 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004089 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004090 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004091 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004092 {
4093 redir_endp = NULL; // don't store a value, only cleanup
4094 var_redir_stop();
4095 return FAIL;
4096 }
4097
4098 return OK;
4099}
4100
4101/*
4102 * Append "value[value_len]" to the variable set by var_redir_start().
4103 * The actual appending is postponed until redirection ends, because the value
4104 * appended may in fact be the string we write to, changing it may cause freed
4105 * memory to be used:
4106 * :redir => foo
4107 * :let foo
4108 * :redir END
4109 */
4110 void
4111var_redir_str(char_u *value, int value_len)
4112{
4113 int len;
4114
4115 if (redir_lval == NULL)
4116 return;
4117
4118 if (value_len == -1)
4119 len = (int)STRLEN(value); // Append the entire string
4120 else
4121 len = value_len; // Append only "value_len" characters
4122
4123 if (ga_grow(&redir_ga, len) == OK)
4124 {
4125 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4126 redir_ga.ga_len += len;
4127 }
4128 else
4129 var_redir_stop();
4130}
4131
4132/*
4133 * Stop redirecting command output to a variable.
4134 * Frees the allocated memory.
4135 */
4136 void
4137var_redir_stop(void)
4138{
4139 typval_T tv;
4140
4141 if (EVALCMD_BUSY)
4142 {
4143 redir_lval = NULL;
4144 return;
4145 }
4146
4147 if (redir_lval != NULL)
4148 {
4149 // If there was no error: assign the text to the variable.
4150 if (redir_endp != NULL)
4151 {
4152 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4153 tv.v_type = VAR_STRING;
4154 tv.vval.v_string = redir_ga.ga_data;
4155 // Call get_lval() again, if it's inside a Dict or List it may
4156 // have changed.
4157 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4158 FALSE, FALSE, 0, FNE_CHECK_START);
4159 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004161 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004162 clear_lval(redir_lval);
4163 }
4164
4165 // free the collected output
4166 VIM_CLEAR(redir_ga.ga_data);
4167
4168 VIM_CLEAR(redir_lval);
4169 }
4170 VIM_CLEAR(redir_varname);
4171}
4172
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004173/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004174 * Get the collected redirected text and clear redir_ga.
4175 */
4176 char_u *
4177get_clear_redir_ga(void)
4178{
4179 char_u *res;
4180
4181 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4182 res = redir_ga.ga_data;
4183 redir_ga.ga_data = NULL;
4184 return res;
4185}
4186
4187/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004188 * "gettabvar()" function
4189 */
4190 void
4191f_gettabvar(typval_T *argvars, typval_T *rettv)
4192{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004193 switchwin_T switchwin;
4194 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004195 dictitem_T *v;
4196 char_u *varname;
4197 int done = FALSE;
4198
4199 rettv->v_type = VAR_STRING;
4200 rettv->vval.v_string = NULL;
4201
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004202 if (in_vim9script()
4203 && (check_for_number_arg(argvars, 0) == FAIL
4204 || check_for_string_arg(argvars, 1) == FAIL))
4205 return;
4206
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004207 varname = tv_get_string_chk(&argvars[1]);
4208 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4209 if (tp != NULL && varname != NULL)
4210 {
4211 // Set tp to be our tabpage, temporarily. Also set the window to the
4212 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004213 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004214 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4215 : tp->tp_firstwin, tp, TRUE) == OK)
4216 {
4217 // look up the variable
4218 // Let gettabvar({nr}, "") return the "t:" dictionary.
4219 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4220 if (v != NULL)
4221 {
4222 copy_tv(&v->di_tv, rettv);
4223 done = TRUE;
4224 }
4225 }
4226
4227 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004228 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004229 }
4230
4231 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4232 copy_tv(&argvars[2], rettv);
4233}
4234
4235/*
4236 * "gettabwinvar()" function
4237 */
4238 void
4239f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4240{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004241 if (in_vim9script()
4242 && (check_for_number_arg(argvars, 0) == FAIL
4243 || check_for_number_arg(argvars, 1) == FAIL
4244 || check_for_string_arg(argvars, 2) == FAIL))
4245 return;
4246
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004247 getwinvar(argvars, rettv, 1);
4248}
4249
4250/*
4251 * "getwinvar()" function
4252 */
4253 void
4254f_getwinvar(typval_T *argvars, typval_T *rettv)
4255{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004256 if (in_vim9script()
4257 && (check_for_number_arg(argvars, 0) == FAIL
4258 || check_for_string_arg(argvars, 1) == FAIL))
4259 return;
4260
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004261 getwinvar(argvars, rettv, 0);
4262}
4263
4264/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004265 * "getbufvar()" function
4266 */
4267 void
4268f_getbufvar(typval_T *argvars, typval_T *rettv)
4269{
4270 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004271 char_u *varname;
4272 dictitem_T *v;
4273 int done = FALSE;
4274
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004275 if (in_vim9script()
4276 && (check_for_buffer_arg(argvars, 0) == FAIL
4277 || check_for_string_arg(argvars, 1) == FAIL))
4278 return;
4279
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004280 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004281 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004282
4283 rettv->v_type = VAR_STRING;
4284 rettv->vval.v_string = NULL;
4285
4286 if (buf != NULL && varname != NULL)
4287 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004288 if (*varname == '&')
4289 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004290 buf_T *save_curbuf = curbuf;
4291
4292 // set curbuf to be our buf, temporarily
4293 curbuf = buf;
4294
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004295 if (varname[1] == NUL)
4296 {
4297 // get all buffer-local options in a dict
4298 dict_T *opts = get_winbuf_options(TRUE);
4299
4300 if (opts != NULL)
4301 {
4302 rettv_dict_set(rettv, opts);
4303 done = TRUE;
4304 }
4305 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004306 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004307 // buffer-local-option
4308 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004309
4310 // restore previous notion of curbuf
4311 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004312 }
4313 else
4314 {
4315 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004316 if (*varname == NUL)
4317 // Let getbufvar({nr}, "") return the "b:" dictionary.
4318 v = &buf->b_bufvar;
4319 else
4320 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4321 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004322 if (v != NULL)
4323 {
4324 copy_tv(&v->di_tv, rettv);
4325 done = TRUE;
4326 }
4327 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004328 }
4329
4330 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4331 // use the default value
4332 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004333}
4334
4335/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004336 * "settabvar()" function
4337 */
4338 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004339f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004340{
4341 tabpage_T *save_curtab;
4342 tabpage_T *tp;
4343 char_u *varname, *tabvarname;
4344 typval_T *varp;
4345
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004346 if (check_secure())
4347 return;
4348
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004349 if (in_vim9script()
4350 && (check_for_number_arg(argvars, 0) == FAIL
4351 || check_for_string_arg(argvars, 1) == FAIL))
4352 return;
4353
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004354 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4355 varname = tv_get_string_chk(&argvars[1]);
4356 varp = &argvars[2];
4357
4358 if (varname != NULL && varp != NULL && tp != NULL)
4359 {
4360 save_curtab = curtab;
4361 goto_tabpage_tp(tp, FALSE, FALSE);
4362
4363 tabvarname = alloc(STRLEN(varname) + 3);
4364 if (tabvarname != NULL)
4365 {
4366 STRCPY(tabvarname, "t:");
4367 STRCPY(tabvarname + 2, varname);
4368 set_var(tabvarname, varp, TRUE);
4369 vim_free(tabvarname);
4370 }
4371
4372 // Restore current tabpage
4373 if (valid_tabpage(save_curtab))
4374 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4375 }
4376}
4377
4378/*
4379 * "settabwinvar()" function
4380 */
4381 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004382f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004383{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004384 if (in_vim9script()
4385 && (check_for_number_arg(argvars, 0) == FAIL
4386 || check_for_number_arg(argvars, 1) == FAIL
4387 || check_for_string_arg(argvars, 2) == FAIL))
4388 return;
4389
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004390 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004391}
4392
4393/*
4394 * "setwinvar()" function
4395 */
4396 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004397f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004398{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004399 if (in_vim9script()
4400 && (check_for_number_arg(argvars, 0) == FAIL
4401 || check_for_string_arg(argvars, 1) == FAIL))
4402 return;
4403
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004404 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004405}
4406
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004407/*
4408 * "setbufvar()" function
4409 */
4410 void
4411f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4412{
4413 buf_T *buf;
4414 char_u *varname, *bufvarname;
4415 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004416
4417 if (check_secure())
4418 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004419
4420 if (in_vim9script()
4421 && (check_for_buffer_arg(argvars, 0) == FAIL
4422 || check_for_string_arg(argvars, 1) == FAIL))
4423 return;
4424
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004425 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004426 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004427 varp = &argvars[2];
4428
4429 if (buf != NULL && varname != NULL && varp != NULL)
4430 {
4431 if (*varname == '&')
4432 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004433 aco_save_T aco;
4434
4435 // set curbuf to be our buf, temporarily
4436 aucmd_prepbuf(&aco, buf);
4437
Bram Moolenaar191929b2020-08-19 21:20:49 +02004438 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004439
4440 // reset notion of buffer
4441 aucmd_restbuf(&aco);
4442 }
4443 else
4444 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004445 bufvarname = alloc(STRLEN(varname) + 3);
4446 if (bufvarname != NULL)
4447 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004448 buf_T *save_curbuf = curbuf;
4449
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004450 curbuf = buf;
4451 STRCPY(bufvarname, "b:");
4452 STRCPY(bufvarname + 2, varname);
4453 set_var(bufvarname, varp, TRUE);
4454 vim_free(bufvarname);
4455 curbuf = save_curbuf;
4456 }
4457 }
4458 }
4459}
4460
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004461/*
4462 * Get a callback from "arg". It can be a Funcref or a function name.
4463 * When "arg" is zero return an empty string.
4464 * "cb_name" is not allocated.
4465 * "cb_name" is set to NULL for an invalid argument.
4466 */
4467 callback_T
4468get_callback(typval_T *arg)
4469{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004470 callback_T res;
4471 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004472
4473 res.cb_free_name = FALSE;
4474 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4475 {
4476 res.cb_partial = arg->vval.v_partial;
4477 ++res.cb_partial->pt_refcount;
4478 res.cb_name = partial_name(res.cb_partial);
4479 }
4480 else
4481 {
4482 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004483 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4484 && isdigit(*arg->vval.v_string))
4485 r = FAIL;
4486 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004487 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004488 if (arg->v_type == VAR_STRING)
4489 {
4490 char_u *name;
4491
4492 name = get_scriptlocal_funcname(arg->vval.v_string);
4493 if (name != NULL)
4494 {
4495 vim_free(arg->vval.v_string);
4496 arg->vval.v_string = name;
4497 }
4498 }
4499
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004500 res.cb_name = arg->vval.v_string;
4501 func_ref(res.cb_name);
4502 }
4503 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004504 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004505 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004506 r = FAIL;
4507
4508 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004509 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004510 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004511 res.cb_name = NULL;
4512 }
4513 }
4514 return res;
4515}
4516
4517/*
4518 * Copy a callback into a typval_T.
4519 */
4520 void
4521put_callback(callback_T *cb, typval_T *tv)
4522{
4523 if (cb->cb_partial != NULL)
4524 {
4525 tv->v_type = VAR_PARTIAL;
4526 tv->vval.v_partial = cb->cb_partial;
4527 ++tv->vval.v_partial->pt_refcount;
4528 }
4529 else
4530 {
4531 tv->v_type = VAR_FUNC;
4532 tv->vval.v_string = vim_strsave(cb->cb_name);
4533 func_ref(cb->cb_name);
4534 }
4535}
4536
4537/*
4538 * Make a copy of "src" into "dest", allocating the function name if needed,
4539 * without incrementing the refcount.
4540 */
4541 void
4542set_callback(callback_T *dest, callback_T *src)
4543{
4544 if (src->cb_partial == NULL)
4545 {
4546 // just a function name, make a copy
4547 dest->cb_name = vim_strsave(src->cb_name);
4548 dest->cb_free_name = TRUE;
4549 }
4550 else
4551 {
4552 // cb_name is a pointer into cb_partial
4553 dest->cb_name = src->cb_name;
4554 dest->cb_free_name = FALSE;
4555 }
4556 dest->cb_partial = src->cb_partial;
4557}
4558
4559/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004560 * Copy callback from "src" to "dest", incrementing the refcounts.
4561 */
4562 void
4563copy_callback(callback_T *dest, callback_T *src)
4564{
4565 dest->cb_partial = src->cb_partial;
4566 if (dest->cb_partial != NULL)
4567 {
4568 dest->cb_name = src->cb_name;
4569 dest->cb_free_name = FALSE;
4570 ++dest->cb_partial->pt_refcount;
4571 }
4572 else
4573 {
4574 dest->cb_name = vim_strsave(src->cb_name);
4575 dest->cb_free_name = TRUE;
4576 func_ref(src->cb_name);
4577 }
4578}
4579
4580/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004581 * Unref/free "callback" returned by get_callback() or set_callback().
4582 */
4583 void
4584free_callback(callback_T *callback)
4585{
4586 if (callback->cb_partial != NULL)
4587 {
4588 partial_unref(callback->cb_partial);
4589 callback->cb_partial = NULL;
4590 }
4591 else if (callback->cb_name != NULL)
4592 func_unref(callback->cb_name);
4593 if (callback->cb_free_name)
4594 {
4595 vim_free(callback->cb_name);
4596 callback->cb_free_name = FALSE;
4597 }
4598 callback->cb_name = NULL;
4599}
4600
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004601#endif // FEAT_EVAL