blob: 1b3d1f6b1f3823c829ffd6457d9d9aca20439585 [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!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100125 {VV_NAME("numbermax", VAR_NUMBER), VV_RO},
126 {VV_NAME("numbermin", VAR_NUMBER), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100127 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
129 {VV_NAME("testing", VAR_NUMBER), 0},
130 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
139 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
140 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
141 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
142 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
143 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
144 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
145 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
146 {VV_NAME("event", VAR_DICT), VV_RO},
147 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
148 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100149 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200150 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100151 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152};
153
154// shorthand
155#define vv_type vv_di.di_tv.v_type
156#define vv_nr vv_di.di_tv.vval.v_number
157#define vv_float vv_di.di_tv.vval.v_float
158#define vv_str vv_di.di_tv.vval.v_string
159#define vv_list vv_di.di_tv.vval.v_list
160#define vv_dict vv_di.di_tv.vval.v_dict
161#define vv_blob vv_di.di_tv.vval.v_blob
162#define vv_tv vv_di.di_tv
163
164static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200165static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200166#define vimvarht vimvardict.dv_hashtab
167
168// for VIM_VERSION_ defines
169#include "version.h"
170
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200171static void list_glob_vars(int *first);
172static void list_buf_vars(int *first);
173static void list_win_vars(int *first);
174static void list_tab_vars(int *first);
175static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100176static 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 +0200177static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
178static 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 +0200179static void list_one_var(dictitem_T *v, char *prefix, int *first);
180static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
181
182/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200183 * Initialize global and vim special variables
184 */
185 void
186evalvars_init(void)
187{
188 int i;
189 struct vimvar *p;
190
191 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
192 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
193 vimvardict.dv_lock = VAR_FIXED;
194 hash_init(&compat_hashtab);
195
196 for (i = 0; i < VV_LEN; ++i)
197 {
198 p = &vimvars[i];
199 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
200 {
201 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
202 getout(1);
203 }
204 STRCPY(p->vv_di.di_key, p->vv_name);
205 if (p->vv_flags & VV_RO)
206 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
207 else if (p->vv_flags & VV_RO_SBX)
208 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
209 else
210 p->vv_di.di_flags = DI_FLAGS_FIX;
211
212 // add to v: scope dict, unless the value is not always available
213 if (p->vv_type != VAR_UNKNOWN)
214 hash_add(&vimvarht, p->vv_di.di_key);
215 if (p->vv_flags & VV_COMPAT)
216 // add to compat scope dict
217 hash_add(&compat_hashtab, p->vv_di.di_key);
218 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200219 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
220 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200221
222 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
223 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100224 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200225 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
226 set_vim_var_list(VV_ERRORS, list_alloc());
227 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
228
229 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
230 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
231 set_vim_var_nr(VV_NONE, VVAL_NONE);
232 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100233 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
234 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100235 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200236
237 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
238 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
239 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
240 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
241 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
242 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
243 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
244 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
245 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
246 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
247 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
248
249 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
250
Bram Moolenaar439c0362020-06-06 15:58:03 +0200251 // Default for v:register is not 0 but '"'. This is adjusted once the
252 // clipboard has been setup by calling reset_reg_var().
253 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200254}
255
256#if defined(EXITFREE) || defined(PROTO)
257/*
258 * Free all vim variables information on exit
259 */
260 void
261evalvars_clear(void)
262{
263 int i;
264 struct vimvar *p;
265
266 for (i = 0; i < VV_LEN; ++i)
267 {
268 p = &vimvars[i];
269 if (p->vv_di.di_tv.v_type == VAR_STRING)
270 VIM_CLEAR(p->vv_str);
271 else if (p->vv_di.di_tv.v_type == VAR_LIST)
272 {
273 list_unref(p->vv_list);
274 p->vv_list = NULL;
275 }
276 }
277 hash_clear(&vimvarht);
278 hash_init(&vimvarht); // garbage_collect() will access it
279 hash_clear(&compat_hashtab);
280
281 // global variables
282 vars_clear(&globvarht);
283
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100284 // Script-local variables. Clear all the variables here.
285 // The scriptvar_T is cleared later in free_scriptnames(), because a
286 // variable in one script might hold a reference to the whole scope of
287 // another script.
288 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200289 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200290}
291#endif
292
293 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200294garbage_collect_globvars(int copyID)
295{
296 return set_ref_in_ht(&globvarht, copyID, NULL);
297}
298
299 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200300garbage_collect_vimvars(int copyID)
301{
302 return set_ref_in_ht(&vimvarht, copyID, NULL);
303}
304
305 int
306garbage_collect_scriptvars(int copyID)
307{
Bram Moolenaared234f22020-10-15 20:42:20 +0200308 int i;
309 int idx;
310 int abort = FALSE;
311 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100313 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200314 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200315 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
316
Bram Moolenaared234f22020-10-15 20:42:20 +0200317 si = SCRIPT_ITEM(i);
318 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
319 {
320 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
321
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100322 if (sv->sv_name != NULL)
323 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200324 }
325 }
326
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 return abort;
328}
329
330/*
331 * Set an internal variable to a string value. Creates the variable if it does
332 * not already exist.
333 */
334 void
335set_internal_string_var(char_u *name, char_u *value)
336{
337 char_u *val;
338 typval_T *tvp;
339
340 val = vim_strsave(value);
341 if (val != NULL)
342 {
343 tvp = alloc_string_tv(val);
344 if (tvp != NULL)
345 {
346 set_var(name, tvp, FALSE);
347 free_tv(tvp);
348 }
349 }
350}
351
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200352 int
353eval_charconvert(
354 char_u *enc_from,
355 char_u *enc_to,
356 char_u *fname_from,
357 char_u *fname_to)
358{
359 int err = FALSE;
360
361 set_vim_var_string(VV_CC_FROM, enc_from, -1);
362 set_vim_var_string(VV_CC_TO, enc_to, -1);
363 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
364 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
365 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
366 err = TRUE;
367 set_vim_var_string(VV_CC_FROM, NULL, -1);
368 set_vim_var_string(VV_CC_TO, NULL, -1);
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
371
372 if (err)
373 return FAIL;
374 return OK;
375}
376
377# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
378 int
379eval_printexpr(char_u *fname, char_u *args)
380{
381 int err = FALSE;
382
383 set_vim_var_string(VV_FNAME_IN, fname, -1);
384 set_vim_var_string(VV_CMDARG, args, -1);
385 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
386 err = TRUE;
387 set_vim_var_string(VV_FNAME_IN, NULL, -1);
388 set_vim_var_string(VV_CMDARG, NULL, -1);
389
390 if (err)
391 {
392 mch_remove(fname);
393 return FAIL;
394 }
395 return OK;
396}
397# endif
398
399# if defined(FEAT_DIFF) || defined(PROTO)
400 void
401eval_diff(
402 char_u *origfile,
403 char_u *newfile,
404 char_u *outfile)
405{
406 int err = FALSE;
407
408 set_vim_var_string(VV_FNAME_IN, origfile, -1);
409 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
410 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
411 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
412 set_vim_var_string(VV_FNAME_IN, NULL, -1);
413 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
414 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
415}
416
417 void
418eval_patch(
419 char_u *origfile,
420 char_u *difffile,
421 char_u *outfile)
422{
423 int err;
424
425 set_vim_var_string(VV_FNAME_IN, origfile, -1);
426 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
427 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
428 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
429 set_vim_var_string(VV_FNAME_IN, NULL, -1);
430 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
431 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
432}
433# endif
434
435#if defined(FEAT_SPELL) || defined(PROTO)
436/*
437 * Evaluate an expression to a list with suggestions.
438 * For the "expr:" part of 'spellsuggest'.
439 * Returns NULL when there is an error.
440 */
441 list_T *
442eval_spell_expr(char_u *badword, char_u *expr)
443{
444 typval_T save_val;
445 typval_T rettv;
446 list_T *list = NULL;
447 char_u *p = skipwhite(expr);
448
449 // Set "v:val" to the bad word.
450 prepare_vimvar(VV_VAL, &save_val);
451 set_vim_var_string(VV_VAL, badword, -1);
452 if (p_verbose == 0)
453 ++emsg_off;
454
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200455 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200456 {
457 if (rettv.v_type != VAR_LIST)
458 clear_tv(&rettv);
459 else
460 list = rettv.vval.v_list;
461 }
462
463 if (p_verbose == 0)
464 --emsg_off;
465 clear_tv(get_vim_var_tv(VV_VAL));
466 restore_vimvar(VV_VAL, &save_val);
467
468 return list;
469}
470
471/*
472 * "list" is supposed to contain two items: a word and a number. Return the
473 * word in "pp" and the number as the return value.
474 * Return -1 if anything isn't right.
475 * Used to get the good word and score from the eval_spell_expr() result.
476 */
477 int
478get_spellword(list_T *list, char_u **pp)
479{
480 listitem_T *li;
481
482 li = list->lv_first;
483 if (li == NULL)
484 return -1;
485 *pp = tv_get_string(&li->li_tv);
486
487 li = li->li_next;
488 if (li == NULL)
489 return -1;
490 return (int)tv_get_number(&li->li_tv);
491}
492#endif
493
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494/*
495 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200496 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200497 * When not used yet add the variable to the v: hashtable.
498 */
499 void
500prepare_vimvar(int idx, typval_T *save_tv)
501{
502 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200503 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200504 if (vimvars[idx].vv_type == VAR_UNKNOWN)
505 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
506}
507
508/*
509 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200510 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200511 * When no longer defined, remove the variable from the v: hashtable.
512 */
513 void
514restore_vimvar(int idx, typval_T *save_tv)
515{
516 hashitem_T *hi;
517
518 vimvars[idx].vv_tv = *save_tv;
519 if (vimvars[idx].vv_type == VAR_UNKNOWN)
520 {
521 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
522 if (HASHITEM_EMPTY(hi))
523 internal_error("restore_vimvar()");
524 else
525 hash_remove(&vimvarht, hi);
526 }
527}
528
529/*
530 * List Vim variables.
531 */
532 static void
533list_vim_vars(int *first)
534{
535 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
536}
537
538/*
539 * List script-local variables, if there is a script.
540 */
541 static void
542list_script_vars(int *first)
543{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200544 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200545 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
546 "s:", FALSE, first);
547}
548
549/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200550 * Get a list of lines from a HERE document. The here document is a list of
551 * lines surrounded by a marker.
552 * cmd << {marker}
553 * {line1}
554 * {line2}
555 * ....
556 * {marker}
557 *
558 * The {marker} is a string. If the optional 'trim' word is supplied before the
559 * marker, then the leading indentation before the lines (matching the
560 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200561 *
562 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
563 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
564 * missing, then '.' is accepted as a marker.
565 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200566 * Returns a List with {lines} or NULL.
567 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200569heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200570{
571 char_u *theline;
572 char_u *marker;
573 list_T *l;
574 char_u *p;
575 int marker_indent_len = 0;
576 int text_indent_len = 0;
577 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200578 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200579 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200580
581 if (eap->getline == NULL)
582 {
583 emsg(_("E991: cannot use =<< here"));
584 return NULL;
585 }
586
587 // Check for the optional 'trim' word before the marker
588 cmd = skipwhite(cmd);
589 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
590 {
591 cmd = skipwhite(cmd + 4);
592
593 // Trim the indentation from all the lines in the here document.
594 // The amount of indentation trimmed is the same as the indentation of
595 // the first line after the :let command line. To find the end marker
596 // the indent of the :let command line is trimmed.
597 p = *eap->cmdlinep;
598 while (VIM_ISWHITE(*p))
599 {
600 p++;
601 marker_indent_len++;
602 }
603 text_indent_len = -1;
604 }
605
606 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200607 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200608 {
609 marker = skipwhite(cmd);
610 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200611 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200612 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200613 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200614 return NULL;
615 }
616 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200617 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200618 {
619 emsg(_("E221: Marker cannot start with lower case letter"));
620 return NULL;
621 }
622 }
623 else
624 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200625 // When getting lines for an embedded script, if the marker is missing,
626 // accept '.' as the marker.
627 if (script_get)
628 marker = dot;
629 else
630 {
631 emsg(_("E172: Missing marker"));
632 return NULL;
633 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200634 }
635
636 l = list_alloc();
637 if (l == NULL)
638 return NULL;
639
640 for (;;)
641 {
642 int mi = 0;
643 int ti = 0;
644
645 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
646 if (theline == NULL)
647 {
648 semsg(_("E990: Missing end marker '%s'"), marker);
649 break;
650 }
651
652 // with "trim": skip the indent matching the :let line to find the
653 // marker
654 if (marker_indent_len > 0
655 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
656 mi = marker_indent_len;
657 if (STRCMP(marker, theline + mi) == 0)
658 {
659 vim_free(theline);
660 break;
661 }
662
663 if (text_indent_len == -1 && *theline != NUL)
664 {
665 // set the text indent from the first line.
666 p = theline;
667 text_indent_len = 0;
668 while (VIM_ISWHITE(*p))
669 {
670 p++;
671 text_indent_len++;
672 }
673 text_indent = vim_strnsave(theline, text_indent_len);
674 }
675 // with "trim": skip the indent matching the first line
676 if (text_indent != NULL)
677 for (ti = 0; ti < text_indent_len; ++ti)
678 if (theline[ti] != text_indent[ti])
679 break;
680
681 if (list_append_string(l, theline + ti, -1) == FAIL)
682 break;
683 vim_free(theline);
684 }
685 vim_free(text_indent);
686
687 return l;
688}
689
690/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200691 * Vim9 variable declaration:
692 * ":var name"
693 * ":var name: type"
694 * ":var name = expr"
695 * ":var name: type = expr"
696 * etc.
697 */
698 void
699ex_var(exarg_T *eap)
700{
701 if (!in_vim9script())
702 {
703 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
704 return;
705 }
706 ex_let(eap);
707}
708
709/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200710 * ":let" list all variable values
711 * ":let var1 var2" list variable values
712 * ":let var = expr" assignment command.
713 * ":let var += expr" assignment command.
714 * ":let var -= expr" assignment command.
715 * ":let var *= expr" assignment command.
716 * ":let var /= expr" assignment command.
717 * ":let var %= expr" assignment command.
718 * ":let var .= expr" assignment command.
719 * ":let var ..= expr" assignment command.
720 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100722 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200723 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200724 * ":final var = expr" assignment command.
725 * ":final [var1, var2] = expr" unpack list.
726 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200727 * ":const" list all variable values
728 * ":const var1 var2" list variable values
729 * ":const var = expr" assignment command.
730 * ":const [var1, var2] = expr" unpack list.
731 */
732 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200733ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200734{
735 char_u *arg = eap->arg;
736 char_u *expr = NULL;
737 typval_T rettv;
738 int i;
739 int var_count = 0;
740 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200741 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200742 char_u *argend;
743 int first = TRUE;
744 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200745 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100746 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200747 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200748
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200749 if (eap->cmdidx == CMD_final && !vim9script)
750 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100751 // In legacy Vim script ":final" is short for ":finally".
752 ex_finally(eap);
753 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200754 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200755 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200756 {
757 emsg(_(e_cannot_use_let_in_vim9_script));
758 return;
759 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200760
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100761 if (eap->cmdidx == CMD_const)
762 flags |= ASSIGN_CONST;
763 else if (eap->cmdidx == CMD_final)
764 flags |= ASSIGN_FINAL;
765
766 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200768 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200770 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771 if (argend == NULL)
772 return;
773 if (argend > arg && argend[-1] == '.') // for var.='str'
774 --argend;
775 expr = skipwhite(argend);
776 concat = expr[0] == '.'
777 && ((expr[1] == '=' && current_sctx.sc_version < 2)
778 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
780 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200781 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200782 {
783 // ":let" without "=": list variables
784 if (*arg == '[')
785 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200786 else if (expr[0] == '.' && expr[1] == '=')
787 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200788 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200789 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200790 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200791 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100792 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
793 semsg(_(e_trailing_arg), argend);
794 else
795 // Vim9 declaration ":var name: type"
796 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200797 }
798 else
799 {
800 // ":let var1 var2" - list values
801 arg = list_arg_vars(eap, arg, &first);
802 }
803 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804 else if (!eap->skip)
805 {
806 // ":let"
807 list_glob_vars(&first);
808 list_buf_vars(&first);
809 list_win_vars(&first);
810 list_tab_vars(&first);
811 list_script_vars(&first);
812 list_func_vars(&first);
813 list_vim_vars(&first);
814 }
815 eap->nextcmd = check_nextcmd(arg);
816 }
817 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
818 {
819 list_T *l;
820
821 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200822 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200823 if (l != NULL)
824 {
825 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200826 if (!eap->skip)
827 {
828 op[0] = '=';
829 op[1] = NUL;
830 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200832 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833 clear_tv(&rettv);
834 }
835 }
836 else
837 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200838 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200839 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200840
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200841 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200842 i = FAIL;
843 if (has_assign || concat)
844 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100845 int cur_lnum;
846
Bram Moolenaar32e35112020-05-14 22:41:15 +0200847 op[0] = '=';
848 op[1] = NUL;
849 if (*expr != '=')
850 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200851 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200852 {
853 // +=, /=, etc. require an existing variable
854 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
855 i = FAIL;
856 }
857 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858 {
859 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200861 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200862 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200863 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 ++len;
865 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200866 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200867 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200868 }
869 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200870 ++expr;
871
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200872 if (vim9script && (!VIM_ISWHITE(*argend)
873 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200874 {
875 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100876 semsg(_(e_white_space_required_before_and_after_str_at_str),
877 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200878 i = FAIL;
879 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200880
881 if (eap->skip)
882 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200883 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200884 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200885 if (getline_equal(eap->getline, eap->cookie, getsourceline))
886 {
887 evalarg.eval_getline = eap->getline;
888 evalarg.eval_cookie = eap->cookie;
889 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200890 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100891 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200892 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200893 if (eap->skip)
894 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200895 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100896
897 // Restore the line number so that any type error is given for the
898 // declaration, not the expression.
899 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200900 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200901 if (eap->skip)
902 {
903 if (i != FAIL)
904 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200905 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200906 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200907 {
908 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200909 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200910 clear_tv(&rettv);
911 }
912 }
913}
914
915/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100916 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200917 * Handles both "var" with any type and "[var, var; var]" with a list type.
918 * When "op" is not NULL it points to a string with characters that
919 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
920 * or concatenate.
921 * Returns OK or FAIL;
922 */
923 int
924ex_let_vars(
925 char_u *arg_start,
926 typval_T *tv,
927 int copy, // copy values from "tv", don't move
928 int semicolon, // from skip_var_list()
929 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100930 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200931 char_u *op)
932{
933 char_u *arg = arg_start;
934 list_T *l;
935 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100936 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200937 listitem_T *item;
938 typval_T ltv;
939
940 if (*arg != '[')
941 {
942 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100943 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 return FAIL;
945 return OK;
946 }
947
948 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
949 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
950 {
951 emsg(_(e_listreq));
952 return FAIL;
953 }
954
955 i = list_len(l);
956 if (semicolon == 0 && var_count < i)
957 {
958 emsg(_("E687: Less targets than List items"));
959 return FAIL;
960 }
961 if (var_count - semicolon > i)
962 {
963 emsg(_("E688: More targets than List items"));
964 return FAIL;
965 }
966
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200967 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 item = l->lv_first;
969 while (*arg != ']')
970 {
971 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100972 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200973 arg = ex_let_one(arg, &item->li_tv, TRUE,
974 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 item = item->li_next;
976 if (arg == NULL)
977 return FAIL;
978
979 arg = skipwhite(arg);
980 if (*arg == ';')
981 {
982 // Put the rest of the list (may be empty) in the var after ';'.
983 // Create a new list for this.
984 l = list_alloc();
985 if (l == NULL)
986 return FAIL;
987 while (item != NULL)
988 {
989 list_append_tv(l, &item->li_tv);
990 item = item->li_next;
991 }
992
993 ltv.v_type = VAR_LIST;
994 ltv.v_lock = 0;
995 ltv.vval.v_list = l;
996 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100997 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200999 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1000 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001 clear_tv(&ltv);
1002 if (arg == NULL)
1003 return FAIL;
1004 break;
1005 }
1006 else if (*arg != ',' && *arg != ']')
1007 {
1008 internal_error("ex_let_vars()");
1009 return FAIL;
1010 }
1011 }
1012
1013 return OK;
1014}
1015
1016/*
1017 * Skip over assignable variable "var" or list of variables "[var, var]".
1018 * Used for ":let varvar = expr" and ":for varvar in expr".
1019 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001020 * for "[var, var; var]" set "semicolon" to 1.
1021 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 * Return NULL for an error.
1023 */
1024 char_u *
1025skip_var_list(
1026 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001029 int *semicolon,
1030 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031{
1032 char_u *p, *s;
1033
1034 if (*arg == '[')
1035 {
1036 // "[var, var]": find the matching ']'.
1037 p = arg;
1038 for (;;)
1039 {
1040 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001041 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 if (s == p)
1043 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001044 if (!silent)
1045 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 return NULL;
1047 }
1048 ++*var_count;
1049
1050 p = skipwhite(s);
1051 if (*p == ']')
1052 break;
1053 else if (*p == ';')
1054 {
1055 if (*semicolon == 1)
1056 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001057 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058 return NULL;
1059 }
1060 *semicolon = 1;
1061 }
1062 else if (*p != ',')
1063 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001064 if (!silent)
1065 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 return NULL;
1067 }
1068 }
1069 return p + 1;
1070 }
1071 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001073}
1074
1075/*
1076 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1077 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001080 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001082{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001083 char_u *end;
1084 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 if (*arg == '@' && arg[1] != NUL)
1087 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001090
1091 // "a: type" is declaring variable "a" with a type, not "a:".
1092 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001093 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001094 --end;
1095
Bram Moolenaar585587d2021-01-17 20:52:13 +01001096 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001098 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001099 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 }
1101 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001102}
1103
1104/*
1105 * List variables for hashtab "ht" with prefix "prefix".
1106 * If "empty" is TRUE also list NULL strings as empty strings.
1107 */
1108 void
1109list_hashtable_vars(
1110 hashtab_T *ht,
1111 char *prefix,
1112 int empty,
1113 int *first)
1114{
1115 hashitem_T *hi;
1116 dictitem_T *di;
1117 int todo;
1118 char_u buf[IOSIZE];
1119
1120 todo = (int)ht->ht_used;
1121 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1122 {
1123 if (!HASHITEM_EMPTY(hi))
1124 {
1125 --todo;
1126 di = HI2DI(hi);
1127
1128 // apply :filter /pat/ to variable name
1129 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1130 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1131 if (message_filtered(buf))
1132 continue;
1133
1134 if (empty || di->di_tv.v_type != VAR_STRING
1135 || di->di_tv.vval.v_string != NULL)
1136 list_one_var(di, prefix, first);
1137 }
1138 }
1139}
1140
1141/*
1142 * List global variables.
1143 */
1144 static void
1145list_glob_vars(int *first)
1146{
1147 list_hashtable_vars(&globvarht, "", TRUE, first);
1148}
1149
1150/*
1151 * List buffer variables.
1152 */
1153 static void
1154list_buf_vars(int *first)
1155{
1156 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1157}
1158
1159/*
1160 * List window variables.
1161 */
1162 static void
1163list_win_vars(int *first)
1164{
1165 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1166}
1167
1168/*
1169 * List tab page variables.
1170 */
1171 static void
1172list_tab_vars(int *first)
1173{
1174 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1175}
1176
1177/*
1178 * List variables in "arg".
1179 */
1180 static char_u *
1181list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1182{
1183 int error = FALSE;
1184 int len;
1185 char_u *name;
1186 char_u *name_start;
1187 char_u *arg_subsc;
1188 char_u *tofree;
1189 typval_T tv;
1190
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001191 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001192 {
1193 if (error || eap->skip)
1194 {
1195 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1196 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1197 {
1198 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001199 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 break;
1201 }
1202 }
1203 else
1204 {
1205 // get_name_len() takes care of expanding curly braces
1206 name_start = name = arg;
1207 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1208 if (len <= 0)
1209 {
1210 // This is mainly to keep test 49 working: when expanding
1211 // curly braces fails overrule the exception error message.
1212 if (len < 0 && !aborting())
1213 {
1214 emsg_severe = TRUE;
1215 semsg(_(e_invarg2), arg);
1216 break;
1217 }
1218 error = TRUE;
1219 }
1220 else
1221 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001222 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001223 if (tofree != NULL)
1224 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001225 if (eval_variable(name, len, &tv, NULL,
1226 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001227 error = TRUE;
1228 else
1229 {
1230 // handle d.key, l[idx], f(expr)
1231 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001232 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001233 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001234 error = TRUE;
1235 else
1236 {
1237 if (arg == arg_subsc && len == 2 && name[1] == ':')
1238 {
1239 switch (*name)
1240 {
1241 case 'g': list_glob_vars(first); break;
1242 case 'b': list_buf_vars(first); break;
1243 case 'w': list_win_vars(first); break;
1244 case 't': list_tab_vars(first); break;
1245 case 'v': list_vim_vars(first); break;
1246 case 's': list_script_vars(first); break;
1247 case 'l': list_func_vars(first); break;
1248 default:
1249 semsg(_("E738: Can't list variables for %s"), name);
1250 }
1251 }
1252 else
1253 {
1254 char_u numbuf[NUMBUFLEN];
1255 char_u *tf;
1256 int c;
1257 char_u *s;
1258
1259 s = echo_string(&tv, &tf, numbuf, 0);
1260 c = *arg;
1261 *arg = NUL;
1262 list_one_var_a("",
1263 arg == arg_subsc ? name : name_start,
1264 tv.v_type,
1265 s == NULL ? (char_u *)"" : s,
1266 first);
1267 *arg = c;
1268 vim_free(tf);
1269 }
1270 clear_tv(&tv);
1271 }
1272 }
1273 }
1274
1275 vim_free(tofree);
1276 }
1277
1278 arg = skipwhite(arg);
1279 }
1280
1281 return arg;
1282}
1283
1284/*
1285 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1286 * Returns a pointer to the char just after the var name.
1287 * Returns NULL if there is an error.
1288 */
1289 static char_u *
1290ex_let_one(
1291 char_u *arg, // points to variable name
1292 typval_T *tv, // value to assign to variable
1293 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001294 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001296 char_u *op, // "+", "-", "." or NULL
1297 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298{
1299 int c1;
1300 char_u *name;
1301 char_u *p;
1302 char_u *arg_end = NULL;
1303 int len;
1304 int opt_flags;
1305 char_u *tofree = NULL;
1306
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001307 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001308 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001309 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1310 {
1311 vim9_declare_error(arg);
1312 return NULL;
1313 }
1314
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001315 // ":let $VAR = expr": Set environment variable.
1316 if (*arg == '$')
1317 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001318 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1319 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001320 {
1321 emsg(_("E996: Cannot lock an environment variable"));
1322 return NULL;
1323 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001324
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001325 // Find the end of the name.
1326 ++arg;
1327 name = arg;
1328 len = get_env_len(&arg);
1329 if (len == 0)
1330 semsg(_(e_invarg2), name - 1);
1331 else
1332 {
1333 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1334 semsg(_(e_letwrong), op);
1335 else if (endchars != NULL
1336 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001337 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001338 else if (!check_secure())
1339 {
1340 c1 = name[len];
1341 name[len] = NUL;
1342 p = tv_get_string_chk(tv);
1343 if (p != NULL && op != NULL && *op == '.')
1344 {
1345 int mustfree = FALSE;
1346 char_u *s = vim_getenv(name, &mustfree);
1347
1348 if (s != NULL)
1349 {
1350 p = tofree = concat_str(s, p);
1351 if (mustfree)
1352 vim_free(s);
1353 }
1354 }
1355 if (p != NULL)
1356 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001358 arg_end = arg;
1359 }
1360 name[len] = c1;
1361 vim_free(tofree);
1362 }
1363 }
1364 }
1365
1366 // ":let &option = expr": Set option value.
1367 // ":let &l:option = expr": Set local option value.
1368 // ":let &g:option = expr": Set global option value.
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001369 // ":for &ts in range(8)": Set option value for for loop
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001370 else if (*arg == '&')
1371 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001372 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1373 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001374 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 return NULL;
1377 }
1378 // Find the end of the name.
1379 p = find_option_end(&arg, &opt_flags);
1380 if (p == NULL || (endchars != NULL
1381 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar108010a2021-06-27 22:03:33 +02001382 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383 else
1384 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001385 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001386 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001387 long numval;
1388 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001389 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001390 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001391
1392 c1 = *p;
1393 *p = NUL;
1394
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001395 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001396 if ((opt_type == gov_bool
1397 || opt_type == gov_number
1398 || opt_type == gov_hidden_bool
1399 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001400 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001401 {
1402 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1403 // bool, possibly hidden
1404 n = (long)tv_get_bool(tv);
1405 else
1406 // number, possibly hidden
1407 n = (long)tv_get_number(tv);
1408 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001409
1410 // Avoid setting a string option to the text "v:false" or similar.
1411 // In Vim9 script also don't convert a number to string.
1412 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1413 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1414 s = tv_get_string_chk(tv);
1415
1416 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001417 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001418 if (((opt_type == gov_bool || opt_type == gov_number)
1419 && *op == '.')
1420 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001421 {
1422 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001423 failed = TRUE; // don't set the value
1424
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001425 }
1426 else
1427 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001428 // number, in legacy script also bool
1429 if (opt_type == gov_number
1430 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001431 {
1432 switch (*op)
1433 {
1434 case '+': n = numval + n; break;
1435 case '-': n = numval - n; break;
1436 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001437 case '/': n = (long)num_divide(numval, n,
1438 &failed); break;
1439 case '%': n = (long)num_modulus(numval, n,
1440 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001441 }
Bram Moolenaara42e6e02021-06-10 18:43:25 +02001442 s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001444 else if (opt_type == gov_string
1445 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001446 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001447 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001448 s = concat_str(stringval, s);
1449 vim_free(stringval);
1450 stringval = s;
1451 }
1452 }
1453 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001454
1455 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001457 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001458 {
1459 set_option_value(arg, n, s, opt_flags);
1460 arg_end = p;
1461 }
1462 else
1463 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001464 }
1465 *p = c1;
1466 vim_free(stringval);
1467 }
1468 }
1469
1470 // ":let @r = expr": Set register contents.
1471 else if (*arg == '@')
1472 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001473 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1474 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001475 {
1476 emsg(_("E996: Cannot lock a register"));
1477 return NULL;
1478 }
1479 ++arg;
1480 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1481 semsg(_(e_letwrong), op);
1482 else if (endchars != NULL
1483 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001484 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001485 else
1486 {
1487 char_u *ptofree = NULL;
1488 char_u *s;
1489
1490 p = tv_get_string_chk(tv);
1491 if (p != NULL && op != NULL && *op == '.')
1492 {
1493 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1494 if (s != NULL)
1495 {
1496 p = ptofree = concat_str(s, p);
1497 vim_free(s);
1498 }
1499 }
1500 if (p != NULL)
1501 {
1502 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1503 arg_end = arg + 1;
1504 }
1505 vim_free(ptofree);
1506 }
1507 }
1508
1509 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001511 // ":let {expr} = expr": Idem, name made with curly braces
1512 else if (eval_isnamec1(*arg) || *arg == '{')
1513 {
1514 lval_T lv;
1515
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001516 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001517 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1518 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001519 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001520 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 if (endchars != NULL && vim_strchr(endchars,
1522 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02001523 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524 else
1525 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001526 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001527 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001528 }
1529 }
1530 clear_lval(&lv);
1531 }
1532
1533 else
1534 semsg(_(e_invarg2), arg);
1535
1536 return arg_end;
1537}
1538
1539/*
1540 * ":unlet[!] var1 ... " command.
1541 */
1542 void
1543ex_unlet(exarg_T *eap)
1544{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001545 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546}
1547
1548/*
1549 * ":lockvar" and ":unlockvar" commands
1550 */
1551 void
1552ex_lockvar(exarg_T *eap)
1553{
1554 char_u *arg = eap->arg;
1555 int deep = 2;
1556
1557 if (eap->forceit)
1558 deep = -1;
1559 else if (vim_isdigit(*arg))
1560 {
1561 deep = getdigits(&arg);
1562 arg = skipwhite(arg);
1563 }
1564
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001565 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001566}
1567
1568/*
1569 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001570 * Also used for Vim9 script. "callback" is invoked as:
1571 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001573 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001574ex_unletlock(
1575 exarg_T *eap,
1576 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001577 int deep,
1578 int glv_flags,
1579 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1580 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581{
1582 char_u *arg = argstart;
1583 char_u *name_end;
1584 int error = FALSE;
1585 lval_T lv;
1586
1587 do
1588 {
1589 if (*arg == '$')
1590 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001591 lv.ll_name = arg;
1592 lv.ll_tv = NULL;
1593 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 if (get_env_len(&arg) == 0)
1595 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001596 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597 return;
1598 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001599 if (!error && !eap->skip
1600 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1601 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001602 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001604 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001606 // Parse the name and find the end.
1607 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001608 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001609 if (lv.ll_name == NULL)
1610 error = TRUE; // error but continue parsing
1611 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1612 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001614 if (name_end != NULL)
1615 {
1616 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001617 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001618 }
1619 if (!(eap->skip || error))
1620 clear_lval(&lv);
1621 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001622 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001624 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001625 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001626 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001627
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001628 if (!eap->skip)
1629 clear_lval(&lv);
1630 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001631
1632 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001633 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001634
1635 eap->nextcmd = check_nextcmd(arg);
1636}
1637
1638 static int
1639do_unlet_var(
1640 lval_T *lp,
1641 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001642 exarg_T *eap,
1643 int deep UNUSED,
1644 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001645{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001646 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001647 int ret = OK;
1648 int cc;
1649
1650 if (lp->ll_tv == NULL)
1651 {
1652 cc = *name_end;
1653 *name_end = NUL;
1654
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001655 // Environment variable, normal name or expanded name.
1656 if (*lp->ll_name == '$')
1657 vim_unsetenv(lp->ll_name + 1);
1658 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001659 ret = FAIL;
1660 *name_end = cc;
1661 }
1662 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001663 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001664 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001665 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001666 return FAIL;
1667 else if (lp->ll_range)
1668 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001669 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1670 !lp->ll_empty2, lp->ll_n2) == FAIL)
1671 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001672 }
1673 else
1674 {
1675 if (lp->ll_list != NULL)
1676 // unlet a List item.
1677 listitem_remove(lp->ll_list, lp->ll_li);
1678 else
1679 // unlet a Dictionary item.
1680 dictitem_remove(lp->ll_dict, lp->ll_di);
1681 }
1682
1683 return ret;
1684}
1685
1686/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001687 * Unlet one item or a range of items from a list.
1688 * Return OK or FAIL.
1689 */
1690 int
1691list_unlet_range(
1692 list_T *l,
1693 listitem_T *li_first,
1694 char_u *name,
1695 long n1_arg,
1696 int has_n2,
1697 long n2)
1698{
1699 listitem_T *li = li_first;
1700 int n1 = n1_arg;
1701
1702 while (li != NULL && (!has_n2 || n2 >= n1))
1703 {
1704 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1705 return FAIL;
1706 li = li->li_next;
1707 ++n1;
1708 }
1709
1710 // Delete a range of List items.
1711 li = li_first;
1712 n1 = n1_arg;
1713 while (li != NULL && (!has_n2 || n2 >= n1))
1714 {
1715 listitem_T *next = li->li_next;
1716
1717 listitem_remove(l, li);
1718 li = next;
1719 ++n1;
1720 }
1721 return OK;
1722}
1723/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001724 * "unlet" a variable. Return OK if it existed, FAIL if not.
1725 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1726 */
1727 int
1728do_unlet(char_u *name, int forceit)
1729{
1730 hashtab_T *ht;
1731 hashitem_T *hi;
1732 char_u *varname;
1733 dict_T *d;
1734 dictitem_T *di;
1735
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001736 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001737 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001738 return FAIL;
1739
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001741
1742 // can't :unlet a script variable in Vim9 script from a function
1743 if (ht == get_script_local_ht()
1744 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1745 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1746 == SCRIPT_VERSION_VIM9
1747 && check_vim9_unlet(name) == FAIL)
1748 return FAIL;
1749
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001750 if (ht != NULL && *varname != NUL)
1751 {
1752 d = get_current_funccal_dict(ht);
1753 if (d == NULL)
1754 {
1755 if (ht == &globvarht)
1756 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001757 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001758 d = &vimvardict;
1759 else
1760 {
1761 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1762 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1763 }
1764 if (d == NULL)
1765 {
1766 internal_error("do_unlet()");
1767 return FAIL;
1768 }
1769 }
1770 hi = hash_find(ht, varname);
1771 if (HASHITEM_EMPTY(hi))
1772 hi = find_hi_in_scoped_ht(name, &ht);
1773 if (hi != NULL && !HASHITEM_EMPTY(hi))
1774 {
1775 di = HI2DI(hi);
1776 if (var_check_fixed(di->di_flags, name, FALSE)
1777 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001778 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779 return FAIL;
1780
1781 delete_var(ht, hi);
1782 return OK;
1783 }
1784 }
1785 if (forceit)
1786 return OK;
1787 semsg(_("E108: No such variable: \"%s\""), name);
1788 return FAIL;
1789}
1790
1791/*
1792 * Lock or unlock variable indicated by "lp".
1793 * "deep" is the levels to go (-1 for unlimited);
1794 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1795 */
1796 static int
1797do_lock_var(
1798 lval_T *lp,
1799 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001800 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001802 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001803{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001804 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001805 int ret = OK;
1806 int cc;
1807 dictitem_T *di;
1808
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809 if (lp->ll_tv == NULL)
1810 {
1811 cc = *name_end;
1812 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001813 if (*lp->ll_name == '$')
1814 {
1815 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001816 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001817 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001818 else
1819 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001820 // Normal name or expanded name.
1821 di = find_var(lp->ll_name, NULL, TRUE);
1822 if (di == NULL)
1823 ret = FAIL;
1824 else if ((di->di_flags & DI_FLAGS_FIX)
1825 && di->di_tv.v_type != VAR_DICT
1826 && di->di_tv.v_type != VAR_LIST)
1827 {
1828 // For historic reasons this error is not given for a list or
1829 // dict. E.g., the b: dict could be locked/unlocked.
1830 semsg(_(e_lock_unlock), lp->ll_name);
1831 ret = FAIL;
1832 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001834 {
1835 if (lock)
1836 di->di_flags |= DI_FLAGS_LOCK;
1837 else
1838 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001839 if (deep != 0)
1840 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001841 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 }
1843 *name_end = cc;
1844 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001845 else if (deep == 0)
1846 {
1847 // nothing to do
1848 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 else if (lp->ll_range)
1850 {
1851 listitem_T *li = lp->ll_li;
1852
1853 // (un)lock a range of List items.
1854 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1855 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001856 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001857 li = li->li_next;
1858 ++lp->ll_n1;
1859 }
1860 }
1861 else if (lp->ll_list != NULL)
1862 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001863 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001864 else
1865 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001866 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001867
1868 return ret;
1869}
1870
1871/*
1872 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001873 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1874 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001876 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001877item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001878{
1879 static int recurse = 0;
1880 list_T *l;
1881 listitem_T *li;
1882 dict_T *d;
1883 blob_T *b;
1884 hashitem_T *hi;
1885 int todo;
1886
1887 if (recurse >= DICT_MAXNEST)
1888 {
1889 emsg(_("E743: variable nested too deep for (un)lock"));
1890 return;
1891 }
1892 if (deep == 0)
1893 return;
1894 ++recurse;
1895
1896 // lock/unlock the item itself
1897 if (lock)
1898 tv->v_lock |= VAR_LOCKED;
1899 else
1900 tv->v_lock &= ~VAR_LOCKED;
1901
1902 switch (tv->v_type)
1903 {
1904 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001905 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001907 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001909 case VAR_STRING:
1910 case VAR_FUNC:
1911 case VAR_PARTIAL:
1912 case VAR_FLOAT:
1913 case VAR_SPECIAL:
1914 case VAR_JOB:
1915 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001916 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001917 break;
1918
1919 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001920 if ((b = tv->vval.v_blob) != NULL
1921 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001922 {
1923 if (lock)
1924 b->bv_lock |= VAR_LOCKED;
1925 else
1926 b->bv_lock &= ~VAR_LOCKED;
1927 }
1928 break;
1929 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001930 if ((l = tv->vval.v_list) != NULL
1931 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001932 {
1933 if (lock)
1934 l->lv_lock |= VAR_LOCKED;
1935 else
1936 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001937 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001938 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001939 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001940 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001941 }
1942 break;
1943 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001944 if ((d = tv->vval.v_dict) != NULL
1945 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001946 {
1947 if (lock)
1948 d->dv_lock |= VAR_LOCKED;
1949 else
1950 d->dv_lock &= ~VAR_LOCKED;
1951 if (deep < 0 || deep > 1)
1952 {
1953 // recursive: lock/unlock the items the List contains
1954 todo = (int)d->dv_hashtab.ht_used;
1955 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1956 {
1957 if (!HASHITEM_EMPTY(hi))
1958 {
1959 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001960 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1961 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001962 }
1963 }
1964 }
1965 }
1966 }
1967 --recurse;
1968}
1969
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001970#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1971/*
1972 * Delete all "menutrans_" variables.
1973 */
1974 void
1975del_menutrans_vars(void)
1976{
1977 hashitem_T *hi;
1978 int todo;
1979
1980 hash_lock(&globvarht);
1981 todo = (int)globvarht.ht_used;
1982 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1983 {
1984 if (!HASHITEM_EMPTY(hi))
1985 {
1986 --todo;
1987 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1988 delete_var(&globvarht, hi);
1989 }
1990 }
1991 hash_unlock(&globvarht);
1992}
1993#endif
1994
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001995/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001996 * Local string buffer for the next two functions to store a variable name
1997 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1998 * get_user_var_name().
1999 */
2000
2001static char_u *varnamebuf = NULL;
2002static int varnamebuflen = 0;
2003
2004/*
2005 * Function to concatenate a prefix and a variable name.
2006 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002007 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002008cat_prefix_varname(int prefix, char_u *name)
2009{
2010 int len;
2011
2012 len = (int)STRLEN(name) + 3;
2013 if (len > varnamebuflen)
2014 {
2015 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002016 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002017 varnamebuf = alloc(len);
2018 if (varnamebuf == NULL)
2019 {
2020 varnamebuflen = 0;
2021 return NULL;
2022 }
2023 varnamebuflen = len;
2024 }
2025 *varnamebuf = prefix;
2026 varnamebuf[1] = ':';
2027 STRCPY(varnamebuf + 2, name);
2028 return varnamebuf;
2029}
2030
2031/*
2032 * Function given to ExpandGeneric() to obtain the list of user defined
2033 * (global/buffer/window/built-in) variable names.
2034 */
2035 char_u *
2036get_user_var_name(expand_T *xp, int idx)
2037{
2038 static long_u gdone;
2039 static long_u bdone;
2040 static long_u wdone;
2041 static long_u tdone;
2042 static int vidx;
2043 static hashitem_T *hi;
2044 hashtab_T *ht;
2045
2046 if (idx == 0)
2047 {
2048 gdone = bdone = wdone = vidx = 0;
2049 tdone = 0;
2050 }
2051
2052 // Global variables
2053 if (gdone < globvarht.ht_used)
2054 {
2055 if (gdone++ == 0)
2056 hi = globvarht.ht_array;
2057 else
2058 ++hi;
2059 while (HASHITEM_EMPTY(hi))
2060 ++hi;
2061 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2062 return cat_prefix_varname('g', hi->hi_key);
2063 return hi->hi_key;
2064 }
2065
2066 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002067 ht =
2068#ifdef FEAT_CMDWIN
2069 // In cmdwin, the alternative buffer should be used.
2070 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2071 &prevwin->w_buffer->b_vars->dv_hashtab :
2072#endif
2073 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002074 if (bdone < ht->ht_used)
2075 {
2076 if (bdone++ == 0)
2077 hi = ht->ht_array;
2078 else
2079 ++hi;
2080 while (HASHITEM_EMPTY(hi))
2081 ++hi;
2082 return cat_prefix_varname('b', hi->hi_key);
2083 }
2084
2085 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002086 ht =
2087#ifdef FEAT_CMDWIN
2088 // In cmdwin, the alternative window should be used.
2089 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2090 &prevwin->w_vars->dv_hashtab :
2091#endif
2092 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002093 if (wdone < ht->ht_used)
2094 {
2095 if (wdone++ == 0)
2096 hi = ht->ht_array;
2097 else
2098 ++hi;
2099 while (HASHITEM_EMPTY(hi))
2100 ++hi;
2101 return cat_prefix_varname('w', hi->hi_key);
2102 }
2103
2104 // t: variables
2105 ht = &curtab->tp_vars->dv_hashtab;
2106 if (tdone < ht->ht_used)
2107 {
2108 if (tdone++ == 0)
2109 hi = ht->ht_array;
2110 else
2111 ++hi;
2112 while (HASHITEM_EMPTY(hi))
2113 ++hi;
2114 return cat_prefix_varname('t', hi->hi_key);
2115 }
2116
2117 // v: variables
2118 if (vidx < VV_LEN)
2119 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2120
2121 VIM_CLEAR(varnamebuf);
2122 varnamebuflen = 0;
2123 return NULL;
2124}
2125
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002126 char *
2127get_var_special_name(int nr)
2128{
2129 switch (nr)
2130 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002131 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2132 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002133 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002134 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002135 }
2136 internal_error("get_var_special_name()");
2137 return "42";
2138}
2139
2140/*
2141 * Returns the global variable dictionary
2142 */
2143 dict_T *
2144get_globvar_dict(void)
2145{
2146 return &globvardict;
2147}
2148
2149/*
2150 * Returns the global variable hash table
2151 */
2152 hashtab_T *
2153get_globvar_ht(void)
2154{
2155 return &globvarht;
2156}
2157
2158/*
2159 * Returns the v: variable dictionary
2160 */
2161 dict_T *
2162get_vimvar_dict(void)
2163{
2164 return &vimvardict;
2165}
2166
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002167/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002169 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 */
2171 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002172find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002174 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2175 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176
2177 if (di == NULL)
2178 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002179 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2181 return (int)(vv - vimvars);
2182}
2183
2184
2185/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002186 * Set type of v: variable to "type".
2187 */
2188 void
2189set_vim_var_type(int idx, vartype_T type)
2190{
2191 vimvars[idx].vv_type = type;
2192}
2193
2194/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002195 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002196 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002197 */
2198 void
2199set_vim_var_nr(int idx, varnumber_T val)
2200{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002201 vimvars[idx].vv_nr = val;
2202}
2203
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 char *
2205get_vim_var_name(int idx)
2206{
2207 return vimvars[idx].vv_name;
2208}
2209
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002210/*
2211 * Get typval_T v: variable value.
2212 */
2213 typval_T *
2214get_vim_var_tv(int idx)
2215{
2216 return &vimvars[idx].vv_tv;
2217}
2218
2219/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002220 * Set v: variable to "tv". Only accepts the same type.
2221 * Takes over the value of "tv".
2222 */
2223 int
2224set_vim_var_tv(int idx, typval_T *tv)
2225{
2226 if (vimvars[idx].vv_type != tv->v_type)
2227 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002228 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002229 clear_tv(tv);
2230 return FAIL;
2231 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002232 // VV_RO is also checked when compiling, but let's check here as well.
2233 if (vimvars[idx].vv_flags & VV_RO)
2234 {
2235 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2236 return FAIL;
2237 }
2238 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2239 {
2240 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2241 return FAIL;
2242 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002243 clear_tv(&vimvars[idx].vv_di.di_tv);
2244 vimvars[idx].vv_di.di_tv = *tv;
2245 return OK;
2246}
2247
2248/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002249 * Get number v: variable value.
2250 */
2251 varnumber_T
2252get_vim_var_nr(int idx)
2253{
2254 return vimvars[idx].vv_nr;
2255}
2256
2257/*
2258 * Get string v: variable value. Uses a static buffer, can only be used once.
2259 * If the String variable has never been set, return an empty string.
2260 * Never returns NULL;
2261 */
2262 char_u *
2263get_vim_var_str(int idx)
2264{
2265 return tv_get_string(&vimvars[idx].vv_tv);
2266}
2267
2268/*
2269 * Get List v: variable value. Caller must take care of reference count when
2270 * needed.
2271 */
2272 list_T *
2273get_vim_var_list(int idx)
2274{
2275 return vimvars[idx].vv_list;
2276}
2277
2278/*
2279 * Get Dict v: variable value. Caller must take care of reference count when
2280 * needed.
2281 */
2282 dict_T *
2283get_vim_var_dict(int idx)
2284{
2285 return vimvars[idx].vv_dict;
2286}
2287
2288/*
2289 * Set v:char to character "c".
2290 */
2291 void
2292set_vim_var_char(int c)
2293{
2294 char_u buf[MB_MAXBYTES + 1];
2295
2296 if (has_mbyte)
2297 buf[(*mb_char2bytes)(c, buf)] = NUL;
2298 else
2299 {
2300 buf[0] = c;
2301 buf[1] = NUL;
2302 }
2303 set_vim_var_string(VV_CHAR, buf, -1);
2304}
2305
2306/*
2307 * Set v:count to "count" and v:count1 to "count1".
2308 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2309 */
2310 void
2311set_vcount(
2312 long count,
2313 long count1,
2314 int set_prevcount)
2315{
2316 if (set_prevcount)
2317 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2318 vimvars[VV_COUNT].vv_nr = count;
2319 vimvars[VV_COUNT1].vv_nr = count1;
2320}
2321
2322/*
2323 * Save variables that might be changed as a side effect. Used when executing
2324 * a timer callback.
2325 */
2326 void
2327save_vimvars(vimvars_save_T *vvsave)
2328{
2329 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2330 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2331 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2332}
2333
2334/*
2335 * Restore variables saved by save_vimvars().
2336 */
2337 void
2338restore_vimvars(vimvars_save_T *vvsave)
2339{
2340 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2341 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2342 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2343}
2344
2345/*
2346 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2347 * value.
2348 */
2349 void
2350set_vim_var_string(
2351 int idx,
2352 char_u *val,
2353 int len) // length of "val" to use or -1 (whole string)
2354{
2355 clear_tv(&vimvars[idx].vv_di.di_tv);
2356 vimvars[idx].vv_type = VAR_STRING;
2357 if (val == NULL)
2358 vimvars[idx].vv_str = NULL;
2359 else if (len == -1)
2360 vimvars[idx].vv_str = vim_strsave(val);
2361 else
2362 vimvars[idx].vv_str = vim_strnsave(val, len);
2363}
2364
2365/*
2366 * Set List v: variable to "val".
2367 */
2368 void
2369set_vim_var_list(int idx, list_T *val)
2370{
2371 clear_tv(&vimvars[idx].vv_di.di_tv);
2372 vimvars[idx].vv_type = VAR_LIST;
2373 vimvars[idx].vv_list = val;
2374 if (val != NULL)
2375 ++val->lv_refcount;
2376}
2377
2378/*
2379 * Set Dictionary v: variable to "val".
2380 */
2381 void
2382set_vim_var_dict(int idx, dict_T *val)
2383{
2384 clear_tv(&vimvars[idx].vv_di.di_tv);
2385 vimvars[idx].vv_type = VAR_DICT;
2386 vimvars[idx].vv_dict = val;
2387 if (val != NULL)
2388 {
2389 ++val->dv_refcount;
2390 dict_set_items_ro(val);
2391 }
2392}
2393
2394/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002395 * Set the v:argv list.
2396 */
2397 void
2398set_argv_var(char **argv, int argc)
2399{
2400 list_T *l = list_alloc();
2401 int i;
2402
2403 if (l == NULL)
2404 getout(1);
2405 l->lv_lock = VAR_FIXED;
2406 for (i = 0; i < argc; ++i)
2407 {
2408 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2409 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002410 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002411 }
2412 set_vim_var_list(VV_ARGV, l);
2413}
2414
2415/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002416 * Reset v:register, taking the 'clipboard' setting into account.
2417 */
2418 void
2419reset_reg_var(void)
2420{
2421 int regname = 0;
2422
2423 // Adjust the register according to 'clipboard', so that when
2424 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2425#ifdef FEAT_CLIPBOARD
2426 adjust_clip_reg(&regname);
2427#endif
2428 set_reg_var(regname);
2429}
2430
2431/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002432 * Set v:register if needed.
2433 */
2434 void
2435set_reg_var(int c)
2436{
2437 char_u regname;
2438
2439 if (c == 0 || c == ' ')
2440 regname = '"';
2441 else
2442 regname = c;
2443 // Avoid free/alloc when the value is already right.
2444 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2445 set_vim_var_string(VV_REG, &regname, 1);
2446}
2447
2448/*
2449 * Get or set v:exception. If "oldval" == NULL, return the current value.
2450 * Otherwise, restore the value to "oldval" and return NULL.
2451 * Must always be called in pairs to save and restore v:exception! Does not
2452 * take care of memory allocations.
2453 */
2454 char_u *
2455v_exception(char_u *oldval)
2456{
2457 if (oldval == NULL)
2458 return vimvars[VV_EXCEPTION].vv_str;
2459
2460 vimvars[VV_EXCEPTION].vv_str = oldval;
2461 return NULL;
2462}
2463
2464/*
2465 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2466 * Otherwise, restore the value to "oldval" and return NULL.
2467 * Must always be called in pairs to save and restore v:throwpoint! Does not
2468 * take care of memory allocations.
2469 */
2470 char_u *
2471v_throwpoint(char_u *oldval)
2472{
2473 if (oldval == NULL)
2474 return vimvars[VV_THROWPOINT].vv_str;
2475
2476 vimvars[VV_THROWPOINT].vv_str = oldval;
2477 return NULL;
2478}
2479
2480/*
2481 * Set v:cmdarg.
2482 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2483 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2484 * Must always be called in pairs!
2485 */
2486 char_u *
2487set_cmdarg(exarg_T *eap, char_u *oldarg)
2488{
2489 char_u *oldval;
2490 char_u *newval;
2491 unsigned len;
2492
2493 oldval = vimvars[VV_CMDARG].vv_str;
2494 if (eap == NULL)
2495 {
2496 vim_free(oldval);
2497 vimvars[VV_CMDARG].vv_str = oldarg;
2498 return NULL;
2499 }
2500
2501 if (eap->force_bin == FORCE_BIN)
2502 len = 6;
2503 else if (eap->force_bin == FORCE_NOBIN)
2504 len = 8;
2505 else
2506 len = 0;
2507
2508 if (eap->read_edit)
2509 len += 7;
2510
2511 if (eap->force_ff != 0)
2512 len += 10; // " ++ff=unix"
2513 if (eap->force_enc != 0)
2514 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2515 if (eap->bad_char != 0)
2516 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2517
2518 newval = alloc(len + 1);
2519 if (newval == NULL)
2520 return NULL;
2521
2522 if (eap->force_bin == FORCE_BIN)
2523 sprintf((char *)newval, " ++bin");
2524 else if (eap->force_bin == FORCE_NOBIN)
2525 sprintf((char *)newval, " ++nobin");
2526 else
2527 *newval = NUL;
2528
2529 if (eap->read_edit)
2530 STRCAT(newval, " ++edit");
2531
2532 if (eap->force_ff != 0)
2533 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2534 eap->force_ff == 'u' ? "unix"
2535 : eap->force_ff == 'd' ? "dos"
2536 : "mac");
2537 if (eap->force_enc != 0)
2538 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2539 eap->cmd + eap->force_enc);
2540 if (eap->bad_char == BAD_KEEP)
2541 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2542 else if (eap->bad_char == BAD_DROP)
2543 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2544 else if (eap->bad_char != 0)
2545 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2546 vimvars[VV_CMDARG].vv_str = newval;
2547 return oldval;
2548}
2549
2550/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002551 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002552 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2553 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002554 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2555 */
2556 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002557eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002558 char_u *name,
2559 int len, // length of "name"
2560 typval_T *rettv, // NULL when only checking existence
2561 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002562 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002563{
2564 int ret = OK;
2565 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002566 int found = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002567 dictitem_T *v;
2568 int cc;
2569
2570 // truncate the name, so that we can use strcmp()
2571 cc = name[len];
2572 name[len] = NUL;
2573
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002574 // Check for local variable when debugging.
2575 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002576 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002577 // Check for user-defined variables.
2578 v = find_var(name, NULL, flags & EVAL_VAR_NOAUTOLOAD);
2579 if (v != NULL)
2580 {
2581 tv = &v->di_tv;
2582 if (dip != NULL)
2583 *dip = v;
2584 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002585 }
2586
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002587 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002589 imported_T *import;
2590 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2591
2592 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593
2594 // imported variable from another script
2595 if (import != NULL)
2596 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002597 if (import->imp_funcname != NULL)
2598 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002599 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002600 if (rettv != NULL)
2601 {
2602 rettv->v_type = VAR_FUNC;
2603 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2604 }
2605 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002606 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002607 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002608 if ((flags & EVAL_VAR_IMPORT) == 0)
2609 {
2610 if (flags & EVAL_VAR_VERBOSE)
2611 emsg(_(e_import_as_name_not_supported_here));
2612 ret = FAIL;
2613 }
2614 else
2615 {
2616 if (rettv != NULL)
2617 {
2618 rettv->v_type = VAR_ANY;
2619 rettv->vval.v_number = import->imp_sid;
2620 }
2621 found = TRUE;
2622 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002623 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002624 else
2625 {
2626 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002627 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002629 tv = sv->sv_tv;
2630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002632 else if (in_vim9script())
2633 {
2634 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2635
2636 if (ufunc != NULL)
2637 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002638 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002639 if (rettv != NULL)
2640 {
2641 rettv->v_type = VAR_FUNC;
2642 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2643 }
2644 }
2645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 }
2647
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002648 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002649 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002650 if (tv == NULL)
2651 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002652 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002653 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002654 ret = FAIL;
2655 }
2656 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002657 {
2658 // If a list or dict variable wasn't initialized, do it now.
2659 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2660 {
2661 tv->vval.v_dict = dict_alloc();
2662 if (tv->vval.v_dict != NULL)
2663 ++tv->vval.v_dict->dv_refcount;
2664 }
2665 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2666 {
2667 tv->vval.v_list = list_alloc();
2668 if (tv->vval.v_list != NULL)
2669 ++tv->vval.v_list->lv_refcount;
2670 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002671 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2672 {
2673 tv->vval.v_blob = blob_alloc();
2674 if (tv->vval.v_blob != NULL)
2675 ++tv->vval.v_blob->bv_refcount;
2676 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002677 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002678 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002679 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002680
2681 name[len] = cc;
2682
2683 return ret;
2684}
2685
2686/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002687 * Check if variable "name[len]" is a local variable or an argument.
2688 * If so, "*eval_lavars_used" is set to TRUE.
2689 */
2690 void
2691check_vars(char_u *name, int len)
2692{
2693 int cc;
2694 char_u *varname;
2695 hashtab_T *ht;
2696
2697 if (eval_lavars_used == NULL)
2698 return;
2699
2700 // truncate the name, so that we can use strcmp()
2701 cc = name[len];
2702 name[len] = NUL;
2703
2704 ht = find_var_ht(name, &varname);
2705 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2706 {
2707 if (find_var(name, NULL, TRUE) != NULL)
2708 *eval_lavars_used = TRUE;
2709 }
2710
2711 name[len] = cc;
2712}
2713
2714/*
2715 * Find variable "name" in the list of variables.
2716 * Return a pointer to it if found, NULL if not found.
2717 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002718 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002719 */
2720 dictitem_T *
2721find_var(char_u *name, hashtab_T **htp, int no_autoload)
2722{
2723 char_u *varname;
2724 hashtab_T *ht;
2725 dictitem_T *ret = NULL;
2726
2727 ht = find_var_ht(name, &varname);
2728 if (htp != NULL)
2729 *htp = ht;
2730 if (ht == NULL)
2731 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002732 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002733 if (ret != NULL)
2734 return ret;
2735
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002736 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002737 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002738 if (ret != NULL)
2739 return ret;
2740
2741 // in Vim9 script items without a scope can be script-local
2742 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2743 {
2744 ht = get_script_local_ht();
2745 if (ht != NULL)
2746 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002747 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002748 if (ret != NULL)
2749 {
2750 if (htp != NULL)
2751 *htp = ht;
2752 return ret;
2753 }
2754 }
2755 }
2756
2757 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002758}
2759
2760/*
2761 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002762 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002763 * Returns NULL if not found.
2764 */
2765 dictitem_T *
2766find_var_in_ht(
2767 hashtab_T *ht,
2768 int htname,
2769 char_u *varname,
2770 int no_autoload)
2771{
2772 hashitem_T *hi;
2773
2774 if (*varname == NUL)
2775 {
2776 // Must be something like "s:", otherwise "ht" would be NULL.
2777 switch (htname)
2778 {
2779 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2780 case 'g': return &globvars_var;
2781 case 'v': return &vimvars_var;
2782 case 'b': return &curbuf->b_bufvar;
2783 case 'w': return &curwin->w_winvar;
2784 case 't': return &curtab->tp_winvar;
2785 case 'l': return get_funccal_local_var();
2786 case 'a': return get_funccal_args_var();
2787 }
2788 return NULL;
2789 }
2790
2791 hi = hash_find(ht, varname);
2792 if (HASHITEM_EMPTY(hi))
2793 {
2794 // For global variables we may try auto-loading the script. If it
2795 // worked find the variable again. Don't auto-load a script if it was
2796 // loaded already, otherwise it would be loaded every time when
2797 // checking if a function name is a Funcref variable.
2798 if (ht == &globvarht && !no_autoload)
2799 {
2800 // Note: script_autoload() may make "hi" invalid. It must either
2801 // be obtained again or not used.
2802 if (!script_autoload(varname, FALSE) || aborting())
2803 return NULL;
2804 hi = hash_find(ht, varname);
2805 }
2806 if (HASHITEM_EMPTY(hi))
2807 return NULL;
2808 }
2809 return HI2DI(hi);
2810}
2811
2812/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 * Get the script-local hashtab. NULL if not in a script context.
2814 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002815 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816get_script_local_ht(void)
2817{
2818 scid_T sid = current_sctx.sc_sid;
2819
Bram Moolenaare3d46852020-08-29 13:39:17 +02002820 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 return &SCRIPT_VARS(sid);
2822 return NULL;
2823}
2824
2825/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002826 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002827 * When "cmd" is TRUE it must look like a command, a function must be followed
2828 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01002829 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002831 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002832lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01002833 char_u *name,
2834 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002835 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01002836 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837{
2838 hashtab_T *ht = get_script_local_ht();
2839 char_u buffer[30];
2840 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002841 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002843 int is_global = FALSE;
2844 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845
2846 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002847 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 if (len < sizeof(buffer) - 1)
2849 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002850 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 vim_strncpy(buffer, name, len);
2852 p = buffer;
2853 }
2854 else
2855 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002856 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002857 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002858 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 }
2860
2861 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002862 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863
2864 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002865 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2866 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 if (p != buffer)
2868 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002869
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002870 // Find a function, so that a following "->" works.
2871 // When used as a command require "(" or "->" to follow, "Cmd" is a user
2872 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002873 if (res != OK)
2874 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002875 p = skipwhite(name + len);
2876
2877 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002878 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01002879 // Do not check for an internal function, since it might also be a
2880 // valid command, such as ":split" versus "split()".
2881 // Skip "g:" before a function name.
2882 if (name[0] == 'g' && name[1] == ':')
2883 {
2884 is_global = TRUE;
2885 fname = name + 2;
2886 }
2887 if (find_func(fname, is_global, NULL) != NULL)
2888 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002889 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01002890 }
2891
Bram Moolenaar709664c2020-12-12 14:33:41 +01002892 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893}
2894
2895/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002896 * Find the hashtab used for a variable name.
2897 * Return NULL if the name is not valid.
2898 * Set "varname" to the start of name without ':'.
2899 */
2900 hashtab_T *
2901find_var_ht(char_u *name, char_u **varname)
2902{
2903 hashitem_T *hi;
2904 hashtab_T *ht;
2905
2906 if (name[0] == NUL)
2907 return NULL;
2908 if (name[1] != ':')
2909 {
2910 // The name must not start with a colon or #.
2911 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2912 return NULL;
2913 *varname = name;
2914
2915 // "version" is "v:version" in all scopes if scriptversion < 3.
2916 // Same for a few other variables marked with VV_COMPAT.
2917 if (current_sctx.sc_version < 3)
2918 {
2919 hi = hash_find(&compat_hashtab, name);
2920 if (!HASHITEM_EMPTY(hi))
2921 return &compat_hashtab;
2922 }
2923
2924 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 if (ht != NULL)
2926 return ht; // local variable
2927
Bram Moolenaarf0a40692021-06-11 22:05:47 +02002928 // In Vim9 script items at the script level are script-local, except
2929 // for autoload names.
2930 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 {
2932 ht = get_script_local_ht();
2933 if (ht != NULL)
2934 return ht;
2935 }
2936
2937 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002938 }
2939 *varname = name + 2;
2940 if (*name == 'g') // global variable
2941 return &globvarht;
2942 // There must be no ':' or '#' in the rest of the name, unless g: is used
2943 if (vim_strchr(name + 2, ':') != NULL
2944 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2945 return NULL;
2946 if (*name == 'b') // buffer variable
2947 return &curbuf->b_vars->dv_hashtab;
2948 if (*name == 'w') // window variable
2949 return &curwin->w_vars->dv_hashtab;
2950 if (*name == 't') // tab page variable
2951 return &curtab->tp_vars->dv_hashtab;
2952 if (*name == 'v') // v: variable
2953 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002954 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002955 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002957 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 if (*name == 'a') // a: function argument
2959 return get_funccal_args_ht();
2960 if (*name == 'l') // l: local function variable
2961 return get_funccal_local_ht();
2962 }
2963 if (*name == 's') // script variable
2964 {
2965 ht = get_script_local_ht();
2966 if (ht != NULL)
2967 return ht;
2968 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002969 return NULL;
2970}
2971
2972/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002973 * Get the string value of a (global/local) variable.
2974 * Note: see tv_get_string() for how long the pointer remains valid.
2975 * Returns NULL when it doesn't exist.
2976 */
2977 char_u *
2978get_var_value(char_u *name)
2979{
2980 dictitem_T *v;
2981
2982 v = find_var(name, NULL, FALSE);
2983 if (v == NULL)
2984 return NULL;
2985 return tv_get_string(&v->di_tv);
2986}
2987
2988/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002989 * Allocate a new hashtab for a sourced script. It will be used while
2990 * sourcing this script and when executing functions defined in the script.
2991 */
2992 void
2993new_script_vars(scid_T id)
2994{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002995 scriptvar_T *sv;
2996
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002997 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2998 if (sv == NULL)
2999 return;
3000 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003001 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003002}
3003
3004/*
3005 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3006 * point to it.
3007 */
3008 void
3009init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3010{
3011 hash_init(&dict->dv_hashtab);
3012 dict->dv_lock = 0;
3013 dict->dv_scope = scope;
3014 dict->dv_refcount = DO_NOT_FREE_CNT;
3015 dict->dv_copyID = 0;
3016 dict_var->di_tv.vval.v_dict = dict;
3017 dict_var->di_tv.v_type = VAR_DICT;
3018 dict_var->di_tv.v_lock = VAR_FIXED;
3019 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3020 dict_var->di_key[0] = NUL;
3021}
3022
3023/*
3024 * Unreference a dictionary initialized by init_var_dict().
3025 */
3026 void
3027unref_var_dict(dict_T *dict)
3028{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003029 // Now the dict needs to be freed if no one else is using it, go back to
3030 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003031 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3032 dict_unref(dict);
3033}
3034
3035/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003036 * Clean up a list of internal variables.
3037 * Frees all allocated variables and the value they contain.
3038 * Clears hashtab "ht", does not free it.
3039 */
3040 void
3041vars_clear(hashtab_T *ht)
3042{
3043 vars_clear_ext(ht, TRUE);
3044}
3045
3046/*
3047 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3048 */
3049 void
3050vars_clear_ext(hashtab_T *ht, int free_val)
3051{
3052 int todo;
3053 hashitem_T *hi;
3054 dictitem_T *v;
3055
3056 hash_lock(ht);
3057 todo = (int)ht->ht_used;
3058 for (hi = ht->ht_array; todo > 0; ++hi)
3059 {
3060 if (!HASHITEM_EMPTY(hi))
3061 {
3062 --todo;
3063
3064 // Free the variable. Don't remove it from the hashtab,
3065 // ht_array might change then. hash_clear() takes care of it
3066 // later.
3067 v = HI2DI(hi);
3068 if (free_val)
3069 clear_tv(&v->di_tv);
3070 if (v->di_flags & DI_FLAGS_ALLOC)
3071 vim_free(v);
3072 }
3073 }
3074 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003075 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003076}
3077
3078/*
3079 * Delete a variable from hashtab "ht" at item "hi".
3080 * Clear the variable value and free the dictitem.
3081 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003082 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003083delete_var(hashtab_T *ht, hashitem_T *hi)
3084{
3085 dictitem_T *di = HI2DI(hi);
3086
3087 hash_remove(ht, hi);
3088 clear_tv(&di->di_tv);
3089 vim_free(di);
3090}
3091
3092/*
3093 * List the value of one internal variable.
3094 */
3095 static void
3096list_one_var(dictitem_T *v, char *prefix, int *first)
3097{
3098 char_u *tofree;
3099 char_u *s;
3100 char_u numbuf[NUMBUFLEN];
3101
3102 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3103 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3104 s == NULL ? (char_u *)"" : s, first);
3105 vim_free(tofree);
3106}
3107
3108 static void
3109list_one_var_a(
3110 char *prefix,
3111 char_u *name,
3112 int type,
3113 char_u *string,
3114 int *first) // when TRUE clear rest of screen and set to FALSE
3115{
3116 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3117 msg_start();
3118 msg_puts(prefix);
3119 if (name != NULL) // "a:" vars don't have a name stored
3120 msg_puts((char *)name);
3121 msg_putchar(' ');
3122 msg_advance(22);
3123 if (type == VAR_NUMBER)
3124 msg_putchar('#');
3125 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3126 msg_putchar('*');
3127 else if (type == VAR_LIST)
3128 {
3129 msg_putchar('[');
3130 if (*string == '[')
3131 ++string;
3132 }
3133 else if (type == VAR_DICT)
3134 {
3135 msg_putchar('{');
3136 if (*string == '{')
3137 ++string;
3138 }
3139 else
3140 msg_putchar(' ');
3141
3142 msg_outtrans(string);
3143
3144 if (type == VAR_FUNC || type == VAR_PARTIAL)
3145 msg_puts("()");
3146 if (*first)
3147 {
3148 msg_clr_eos();
3149 *first = FALSE;
3150 }
3151}
3152
3153/*
3154 * Set variable "name" to value in "tv".
3155 * If the variable already exists, the value is updated.
3156 * Otherwise the variable is created.
3157 */
3158 void
3159set_var(
3160 char_u *name,
3161 typval_T *tv,
3162 int copy) // make copy of value in "tv"
3163{
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003164 set_var_const(name, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003165}
3166
3167/*
3168 * Set variable "name" to value in "tv".
3169 * If the variable already exists and "is_const" is FALSE the value is updated.
3170 * Otherwise the variable is created.
3171 */
3172 void
3173set_var_const(
3174 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003176 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003177 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003178 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003179 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003180{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003181 typval_T *tv = tv_arg;
3182 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003184 char_u *varname;
3185 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003187 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003188 int var_in_vim9script;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003189 int flags = flags_arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190
3191 ht = find_var_ht(name, &varname);
3192 if (ht == NULL || *varname == NUL)
3193 {
3194 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003195 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003196 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003197 is_script_local = ht == get_script_local_ht();
3198
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003199 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003200 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003201 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003202 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003203 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003204 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003205 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003206 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003207 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003208 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3209 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3210 // Do not make g:var, w:var, b:var or t:var final.
3211 flags &= ~ASSIGN_FINAL;
3212
Bram Moolenaare535db82021-03-31 21:07:24 +02003213 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003214 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3215 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003216 // For "[a, _] = list" the underscore is ignored.
3217 if ((flags & ASSIGN_UNPACK) == 0)
3218 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003219 goto failed;
3220 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003223
3224 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 if (di == NULL)
3226 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227
3228 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003229 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003230 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003231
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003232 if (need_convert_to_bool(type, tv))
3233 {
3234 // Destination is a bool and the value is not, but it can be converted.
3235 CLEAR_FIELD(bool_tv);
3236 bool_tv.v_type = VAR_BOOL;
3237 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3238 tv = &bool_tv;
3239 }
3240
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003242 {
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01003243 // Item already exists. Allowed to replace when reloading.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003245 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003246 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
3247 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 {
3249 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003250 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 }
3252
Bram Moolenaar12be7342021-03-31 21:47:33 +02003253 if (is_script_local && vim9script
3254 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
3255 {
3256 semsg(_(e_redefining_script_item_str), name);
3257 goto failed;
3258 }
3259
Bram Moolenaare535db82021-03-31 21:07:24 +02003260 if (var_in_vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003262 where_T where;
3263
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003264 // check the type and adjust to bool if needed
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003265 where.wt_index = var_idx;
3266 where.wt_variable = TRUE;
3267 if (check_script_var_type(&di->di_tv, tv, name, where) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003268 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003270
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003271 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003272 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003275 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 // can only redefine once
3277 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003278
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003279 // A Vim9 script-local variable is also present in sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003280 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003281 if (var_in_vim9script)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003282 update_vim9_script_var(FALSE, di, flags, tv, &type,
3283 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003284 }
3285
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003286 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003289 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003290 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003291 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003293 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003295 if (copy || tv->v_type != VAR_STRING)
3296 {
3297 char_u *val = tv_get_string(tv);
3298
3299 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003300 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 if (di->di_tv.vval.v_string == NULL)
3302 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003303 }
3304 else
3305 {
3306 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003307 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003308 tv->vval.v_string = NULL;
3309 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003310 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003311 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003313 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003315 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003317#ifdef FEAT_SEARCH_EXTRA
3318 else if (STRCMP(varname, "hlsearch") == 0)
3319 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003321 redraw_all_later(SOME_VALID);
3322 }
3323#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003324 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003325 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003327 {
3328 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003329 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003330 }
3331 }
3332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003334 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003335 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003336 {
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01003337 // Item not found, check if a function already exists.
3338 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003339 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01003340 {
3341 semsg(_(e_redefining_script_item_str), name);
3342 goto failed;
3343 }
3344
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003345 // add a new variable
Bram Moolenaare535db82021-03-31 21:07:24 +02003346 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003347 {
3348 semsg(_(e_unknown_variable_str), name);
3349 goto failed;
3350 }
3351
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003352 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003353 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003354 {
3355 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003356 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003357 }
3358
Bram Moolenaar03290b82020-12-19 16:30:44 +01003359 // Make sure the variable name is valid. In Vim9 script an autoload
3360 // variable must be prefixed with "g:".
3361 if (!valid_varname(varname, !vim9script
3362 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003363 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3366 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003367 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 STRCPY(di->di_key, varname);
3369 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003370 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003372 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003373 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003374 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003375 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 di->di_flags |= DI_FLAGS_LOCK;
3377
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003378 // A Vim9 script-local variable is also added to sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003379 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaare535db82021-03-31 21:07:24 +02003380 if (var_in_vim9script)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003381 update_vim9_script_var(TRUE, di, flags, tv, &type,
3382 (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003383 }
3384
3385 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003387 else
3388 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389 di->di_tv = *tv;
3390 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003391 init_tv(tv);
3392 }
3393
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003394 if (vim9script && type != NULL)
3395 {
3396 if (type->tt_type == VAR_DICT && di->di_tv.vval.v_dict != NULL)
3397 di->di_tv.vval.v_dict->dv_type = alloc_type(type);
3398 else if (type->tt_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
3399 di->di_tv.vval.v_list->lv_type = alloc_type(type);
3400 }
3401
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003402 // ":const var = value" locks the value
3403 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003404 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003405 // Like :lockvar! name: lock the value and what it contains, but only
3406 // if the reference count is up to one. That locks only literal
3407 // values.
3408 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003409 return;
3410failed:
3411 if (!copy)
3412 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003413}
3414
3415/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003416 * Check in this order for backwards compatibility:
3417 * - Whether the variable is read-only
3418 * - Whether the variable value is locked
3419 * - Whether the variable is locked
3420 */
3421 int
3422var_check_permission(dictitem_T *di, char_u *name)
3423{
3424 if (var_check_ro(di->di_flags, name, FALSE)
3425 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3426 || var_check_lock(di->di_flags, name, FALSE))
3427 return FAIL;
3428 return OK;
3429}
3430
3431/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003432 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3433 * Also give an error message.
3434 */
3435 int
3436var_check_ro(int flags, char_u *name, int use_gettext)
3437{
3438 if (flags & DI_FLAGS_RO)
3439 {
3440 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3441 return TRUE;
3442 }
3443 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3444 {
3445 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3446 return TRUE;
3447 }
3448 return FALSE;
3449}
3450
3451/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003452 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3453 * Also give an error message.
3454 */
3455 int
3456var_check_lock(int flags, char_u *name, int use_gettext)
3457{
3458 if (flags & DI_FLAGS_LOCK)
3459 {
3460 semsg(_(e_variable_is_locked_str),
3461 use_gettext ? (char_u *)_(name) : name);
3462 return TRUE;
3463 }
3464 return FALSE;
3465}
3466
3467/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003468 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3469 * Also give an error message.
3470 */
3471 int
3472var_check_fixed(int flags, char_u *name, int use_gettext)
3473{
3474 if (flags & DI_FLAGS_FIX)
3475 {
3476 semsg(_("E795: Cannot delete variable %s"),
3477 use_gettext ? (char_u *)_(name) : name);
3478 return TRUE;
3479 }
3480 return FALSE;
3481}
3482
3483/*
3484 * Check if a funcref is assigned to a valid variable name.
3485 * Return TRUE and give an error if not.
3486 */
3487 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003488var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003489 char_u *name, // points to start of variable name
3490 int new_var) // TRUE when creating the variable
3491{
Bram Moolenaar32154662021-03-28 21:14:06 +02003492 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3493 // the name can be used without the s: prefix.
3494 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3495 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003496 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3497 ? name[2] : name[0]))
3498 {
3499 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3500 name);
3501 return TRUE;
3502 }
3503 // Don't allow hiding a function. When "v" is not NULL we might be
3504 // assigning another function to the same var, the type is checked
3505 // below.
3506 if (new_var && function_exists(name, FALSE))
3507 {
3508 semsg(_("E705: Variable name conflicts with existing function: %s"),
3509 name);
3510 return TRUE;
3511 }
3512 return FALSE;
3513}
3514
3515/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003516 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3517 * value. Also give an error message, using "name" or _("name") when
3518 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003519 */
3520 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003521value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003522{
3523 if (lock & VAR_LOCKED)
3524 {
3525 semsg(_("E741: Value is locked: %s"),
3526 name == NULL ? (char_u *)_("Unknown")
3527 : use_gettext ? (char_u *)_(name)
3528 : name);
3529 return TRUE;
3530 }
3531 if (lock & VAR_FIXED)
3532 {
3533 semsg(_("E742: Cannot change value of %s"),
3534 name == NULL ? (char_u *)_("Unknown")
3535 : use_gettext ? (char_u *)_(name)
3536 : name);
3537 return TRUE;
3538 }
3539 return FALSE;
3540}
3541
3542/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003543 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003544 * Return FALSE and give an error if not.
3545 */
3546 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003547valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003548{
3549 char_u *p;
3550
3551 for (p = varname; *p != NUL; ++p)
3552 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003553 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003554 {
3555 semsg(_(e_illvar), varname);
3556 return FALSE;
3557 }
3558 return TRUE;
3559}
3560
3561/*
3562 * getwinvar() and gettabwinvar()
3563 */
3564 static void
3565getwinvar(
3566 typval_T *argvars,
3567 typval_T *rettv,
3568 int off) // 1 for gettabwinvar()
3569{
3570 win_T *win;
3571 char_u *varname;
3572 dictitem_T *v;
3573 tabpage_T *tp = NULL;
3574 int done = FALSE;
3575 win_T *oldcurwin;
3576 tabpage_T *oldtabpage;
3577 int need_switch_win;
3578
3579 if (off == 1)
3580 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3581 else
3582 tp = curtab;
3583 win = find_win_by_nr(&argvars[off], tp);
3584 varname = tv_get_string_chk(&argvars[off + 1]);
3585 ++emsg_off;
3586
3587 rettv->v_type = VAR_STRING;
3588 rettv->vval.v_string = NULL;
3589
3590 if (win != NULL && varname != NULL)
3591 {
3592 // Set curwin to be our win, temporarily. Also set the tabpage,
3593 // otherwise the window is not valid. Only do this when needed,
3594 // autocommands get blocked.
3595 need_switch_win = !(tp == curtab && win == curwin);
3596 if (!need_switch_win
3597 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3598 {
3599 if (*varname == '&')
3600 {
3601 if (varname[1] == NUL)
3602 {
3603 // get all window-local options in a dict
3604 dict_T *opts = get_winbuf_options(FALSE);
3605
3606 if (opts != NULL)
3607 {
3608 rettv_dict_set(rettv, opts);
3609 done = TRUE;
3610 }
3611 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003612 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003613 // window-local-option
3614 done = TRUE;
3615 }
3616 else
3617 {
3618 // Look up the variable.
3619 // Let getwinvar({nr}, "") return the "w:" dictionary.
3620 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3621 varname, FALSE);
3622 if (v != NULL)
3623 {
3624 copy_tv(&v->di_tv, rettv);
3625 done = TRUE;
3626 }
3627 }
3628 }
3629
3630 if (need_switch_win)
3631 // restore previous notion of curwin
3632 restore_win(oldcurwin, oldtabpage, TRUE);
3633 }
3634
3635 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3636 // use the default return value
3637 copy_tv(&argvars[off + 2], rettv);
3638
3639 --emsg_off;
3640}
3641
3642/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003643 * Set option "varname" to the value of "varp" for the current buffer/window.
3644 */
3645 static void
3646set_option_from_tv(char_u *varname, typval_T *varp)
3647{
3648 long numval = 0;
3649 char_u *strval;
3650 char_u nbuf[NUMBUFLEN];
3651 int error = FALSE;
3652
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003653 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003654 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003655 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003656 strval = (char_u *)"0"; // avoid using "false"
3657 }
3658 else
3659 {
3660 if (!in_vim9script() || varp->v_type != VAR_STRING)
3661 numval = (long)tv_get_number_chk(varp, &error);
3662 strval = tv_get_string_buf_chk(varp, nbuf);
3663 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003664 if (!error && strval != NULL)
3665 set_option_value(varname, numval, strval, OPT_LOCAL);
3666}
3667
3668/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003669 * "setwinvar()" and "settabwinvar()" functions
3670 */
3671 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003672setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003673{
3674 win_T *win;
3675 win_T *save_curwin;
3676 tabpage_T *save_curtab;
3677 int need_switch_win;
3678 char_u *varname, *winvarname;
3679 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003680 tabpage_T *tp = NULL;
3681
3682 if (check_secure())
3683 return;
3684
3685 if (off == 1)
3686 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3687 else
3688 tp = curtab;
3689 win = find_win_by_nr(&argvars[off], tp);
3690 varname = tv_get_string_chk(&argvars[off + 1]);
3691 varp = &argvars[off + 2];
3692
3693 if (win != NULL && varname != NULL && varp != NULL)
3694 {
3695 need_switch_win = !(tp == curtab && win == curwin);
3696 if (!need_switch_win
3697 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3698 {
3699 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003700 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003701 else
3702 {
3703 winvarname = alloc(STRLEN(varname) + 3);
3704 if (winvarname != NULL)
3705 {
3706 STRCPY(winvarname, "w:");
3707 STRCPY(winvarname + 2, varname);
3708 set_var(winvarname, varp, TRUE);
3709 vim_free(winvarname);
3710 }
3711 }
3712 }
3713 if (need_switch_win)
3714 restore_win(save_curwin, save_curtab, TRUE);
3715 }
3716}
3717
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003718/*
3719 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3720 * v:option_type, and v:option_command.
3721 */
3722 void
3723reset_v_option_vars(void)
3724{
3725 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3726 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3727 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3728 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3729 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3730 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3731}
3732
3733/*
3734 * Add an assert error to v:errors.
3735 */
3736 void
3737assert_error(garray_T *gap)
3738{
3739 struct vimvar *vp = &vimvars[VV_ERRORS];
3740
3741 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003742 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003743 set_vim_var_list(VV_ERRORS, list_alloc());
3744 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3745}
3746
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003747 int
3748var_exists(char_u *var)
3749{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003750 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003751 char_u *name;
3752 char_u *tofree;
3753 typval_T tv;
3754 int len = 0;
3755 int n = FALSE;
3756
3757 // get_name_len() takes care of expanding curly braces
3758 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003759 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003760 if (len > 0)
3761 {
3762 if (tofree != NULL)
3763 name = tofree;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003764 n = (eval_variable(name, len, &tv, NULL,
3765 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003766 if (n)
3767 {
3768 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003769 arg = skipwhite(arg);
3770 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003771 if (n)
3772 clear_tv(&tv);
3773 }
3774 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003775 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003776 n = FALSE;
3777
3778 vim_free(tofree);
3779 return n;
3780}
3781
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003782static lval_T *redir_lval = NULL;
3783#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3784static garray_T redir_ga; // only valid when redir_lval is not NULL
3785static char_u *redir_endp = NULL;
3786static char_u *redir_varname = NULL;
3787
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003788 int
3789alloc_redir_lval(void)
3790{
3791 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3792 if (redir_lval == NULL)
3793 return FAIL;
3794 return OK;
3795}
3796
3797 void
3798clear_redir_lval(void)
3799{
3800 VIM_CLEAR(redir_lval);
3801}
3802
3803 void
3804init_redir_ga(void)
3805{
3806 ga_init2(&redir_ga, (int)sizeof(char), 500);
3807}
3808
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003809/*
3810 * Start recording command output to a variable
3811 * When "append" is TRUE append to an existing variable.
3812 * Returns OK if successfully completed the setup. FAIL otherwise.
3813 */
3814 int
3815var_redir_start(char_u *name, int append)
3816{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003817 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003818 typval_T tv;
3819
3820 // Catch a bad name early.
3821 if (!eval_isnamec1(*name))
3822 {
3823 emsg(_(e_invarg));
3824 return FAIL;
3825 }
3826
3827 // Make a copy of the name, it is used in redir_lval until redir ends.
3828 redir_varname = vim_strsave(name);
3829 if (redir_varname == NULL)
3830 return FAIL;
3831
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003832 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003833 {
3834 var_redir_stop();
3835 return FAIL;
3836 }
3837
3838 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003839 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003840
3841 // Parse the variable name (can be a dict or list entry).
3842 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3843 FNE_CHECK_START);
3844 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3845 {
3846 clear_lval(redir_lval);
3847 if (redir_endp != NULL && *redir_endp != NUL)
3848 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003849 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003850 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003851 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003852 redir_endp = NULL; // don't store a value, only cleanup
3853 var_redir_stop();
3854 return FAIL;
3855 }
3856
3857 // check if we can write to the variable: set it to or append an empty
3858 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003859 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003860 tv.v_type = VAR_STRING;
3861 tv.vval.v_string = (char_u *)"";
3862 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003863 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003864 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003865 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003866 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003867 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003868 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003869 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003870 {
3871 redir_endp = NULL; // don't store a value, only cleanup
3872 var_redir_stop();
3873 return FAIL;
3874 }
3875
3876 return OK;
3877}
3878
3879/*
3880 * Append "value[value_len]" to the variable set by var_redir_start().
3881 * The actual appending is postponed until redirection ends, because the value
3882 * appended may in fact be the string we write to, changing it may cause freed
3883 * memory to be used:
3884 * :redir => foo
3885 * :let foo
3886 * :redir END
3887 */
3888 void
3889var_redir_str(char_u *value, int value_len)
3890{
3891 int len;
3892
3893 if (redir_lval == NULL)
3894 return;
3895
3896 if (value_len == -1)
3897 len = (int)STRLEN(value); // Append the entire string
3898 else
3899 len = value_len; // Append only "value_len" characters
3900
3901 if (ga_grow(&redir_ga, len) == OK)
3902 {
3903 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3904 redir_ga.ga_len += len;
3905 }
3906 else
3907 var_redir_stop();
3908}
3909
3910/*
3911 * Stop redirecting command output to a variable.
3912 * Frees the allocated memory.
3913 */
3914 void
3915var_redir_stop(void)
3916{
3917 typval_T tv;
3918
3919 if (EVALCMD_BUSY)
3920 {
3921 redir_lval = NULL;
3922 return;
3923 }
3924
3925 if (redir_lval != NULL)
3926 {
3927 // If there was no error: assign the text to the variable.
3928 if (redir_endp != NULL)
3929 {
3930 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3931 tv.v_type = VAR_STRING;
3932 tv.vval.v_string = redir_ga.ga_data;
3933 // Call get_lval() again, if it's inside a Dict or List it may
3934 // have changed.
3935 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3936 FALSE, FALSE, 0, FNE_CHECK_START);
3937 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003939 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003940 clear_lval(redir_lval);
3941 }
3942
3943 // free the collected output
3944 VIM_CLEAR(redir_ga.ga_data);
3945
3946 VIM_CLEAR(redir_lval);
3947 }
3948 VIM_CLEAR(redir_varname);
3949}
3950
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003951/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02003952 * Get the collected redirected text and clear redir_ga.
3953 */
3954 char_u *
3955get_clear_redir_ga(void)
3956{
3957 char_u *res;
3958
3959 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3960 res = redir_ga.ga_data;
3961 redir_ga.ga_data = NULL;
3962 return res;
3963}
3964
3965/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003966 * "gettabvar()" function
3967 */
3968 void
3969f_gettabvar(typval_T *argvars, typval_T *rettv)
3970{
3971 win_T *oldcurwin;
3972 tabpage_T *tp, *oldtabpage;
3973 dictitem_T *v;
3974 char_u *varname;
3975 int done = FALSE;
3976
3977 rettv->v_type = VAR_STRING;
3978 rettv->vval.v_string = NULL;
3979
3980 varname = tv_get_string_chk(&argvars[1]);
3981 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3982 if (tp != NULL && varname != NULL)
3983 {
3984 // Set tp to be our tabpage, temporarily. Also set the window to the
3985 // first window in the tabpage, otherwise the window is not valid.
3986 if (switch_win(&oldcurwin, &oldtabpage,
3987 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3988 : tp->tp_firstwin, tp, TRUE) == OK)
3989 {
3990 // look up the variable
3991 // Let gettabvar({nr}, "") return the "t:" dictionary.
3992 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3993 if (v != NULL)
3994 {
3995 copy_tv(&v->di_tv, rettv);
3996 done = TRUE;
3997 }
3998 }
3999
4000 // restore previous notion of curwin
4001 restore_win(oldcurwin, oldtabpage, TRUE);
4002 }
4003
4004 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4005 copy_tv(&argvars[2], rettv);
4006}
4007
4008/*
4009 * "gettabwinvar()" function
4010 */
4011 void
4012f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4013{
4014 getwinvar(argvars, rettv, 1);
4015}
4016
4017/*
4018 * "getwinvar()" function
4019 */
4020 void
4021f_getwinvar(typval_T *argvars, typval_T *rettv)
4022{
4023 getwinvar(argvars, rettv, 0);
4024}
4025
4026/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004027 * "getbufvar()" function
4028 */
4029 void
4030f_getbufvar(typval_T *argvars, typval_T *rettv)
4031{
4032 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004033 char_u *varname;
4034 dictitem_T *v;
4035 int done = FALSE;
4036
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004037 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004038 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004039
4040 rettv->v_type = VAR_STRING;
4041 rettv->vval.v_string = NULL;
4042
4043 if (buf != NULL && varname != NULL)
4044 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004045 if (*varname == '&')
4046 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004047 buf_T *save_curbuf = curbuf;
4048
4049 // set curbuf to be our buf, temporarily
4050 curbuf = buf;
4051
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004052 if (varname[1] == NUL)
4053 {
4054 // get all buffer-local options in a dict
4055 dict_T *opts = get_winbuf_options(TRUE);
4056
4057 if (opts != NULL)
4058 {
4059 rettv_dict_set(rettv, opts);
4060 done = TRUE;
4061 }
4062 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004063 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004064 // buffer-local-option
4065 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004066
4067 // restore previous notion of curbuf
4068 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004069 }
4070 else
4071 {
4072 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004073 if (*varname == NUL)
4074 // Let getbufvar({nr}, "") return the "b:" dictionary.
4075 v = &buf->b_bufvar;
4076 else
4077 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4078 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004079 if (v != NULL)
4080 {
4081 copy_tv(&v->di_tv, rettv);
4082 done = TRUE;
4083 }
4084 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004085 }
4086
4087 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4088 // use the default value
4089 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004090}
4091
4092/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004093 * "settabvar()" function
4094 */
4095 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004096f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004097{
4098 tabpage_T *save_curtab;
4099 tabpage_T *tp;
4100 char_u *varname, *tabvarname;
4101 typval_T *varp;
4102
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004103 if (check_secure())
4104 return;
4105
4106 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4107 varname = tv_get_string_chk(&argvars[1]);
4108 varp = &argvars[2];
4109
4110 if (varname != NULL && varp != NULL && tp != NULL)
4111 {
4112 save_curtab = curtab;
4113 goto_tabpage_tp(tp, FALSE, FALSE);
4114
4115 tabvarname = alloc(STRLEN(varname) + 3);
4116 if (tabvarname != NULL)
4117 {
4118 STRCPY(tabvarname, "t:");
4119 STRCPY(tabvarname + 2, varname);
4120 set_var(tabvarname, varp, TRUE);
4121 vim_free(tabvarname);
4122 }
4123
4124 // Restore current tabpage
4125 if (valid_tabpage(save_curtab))
4126 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4127 }
4128}
4129
4130/*
4131 * "settabwinvar()" function
4132 */
4133 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004134f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004135{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004136 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004137}
4138
4139/*
4140 * "setwinvar()" function
4141 */
4142 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004143f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004144{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004145 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004146}
4147
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004148/*
4149 * "setbufvar()" function
4150 */
4151 void
4152f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4153{
4154 buf_T *buf;
4155 char_u *varname, *bufvarname;
4156 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004157
4158 if (check_secure())
4159 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004160 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004161 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004162 varp = &argvars[2];
4163
4164 if (buf != NULL && varname != NULL && varp != NULL)
4165 {
4166 if (*varname == '&')
4167 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004168 aco_save_T aco;
4169
4170 // set curbuf to be our buf, temporarily
4171 aucmd_prepbuf(&aco, buf);
4172
Bram Moolenaar191929b2020-08-19 21:20:49 +02004173 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004174
4175 // reset notion of buffer
4176 aucmd_restbuf(&aco);
4177 }
4178 else
4179 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004180 bufvarname = alloc(STRLEN(varname) + 3);
4181 if (bufvarname != NULL)
4182 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004183 buf_T *save_curbuf = curbuf;
4184
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004185 curbuf = buf;
4186 STRCPY(bufvarname, "b:");
4187 STRCPY(bufvarname + 2, varname);
4188 set_var(bufvarname, varp, TRUE);
4189 vim_free(bufvarname);
4190 curbuf = save_curbuf;
4191 }
4192 }
4193 }
4194}
4195
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004196/*
4197 * Get a callback from "arg". It can be a Funcref or a function name.
4198 * When "arg" is zero return an empty string.
4199 * "cb_name" is not allocated.
4200 * "cb_name" is set to NULL for an invalid argument.
4201 */
4202 callback_T
4203get_callback(typval_T *arg)
4204{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004205 callback_T res;
4206 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004207
4208 res.cb_free_name = FALSE;
4209 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4210 {
4211 res.cb_partial = arg->vval.v_partial;
4212 ++res.cb_partial->pt_refcount;
4213 res.cb_name = partial_name(res.cb_partial);
4214 }
4215 else
4216 {
4217 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004218 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4219 && isdigit(*arg->vval.v_string))
4220 r = FAIL;
4221 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004222 {
4223 // Note that we don't make a copy of the string.
4224 res.cb_name = arg->vval.v_string;
4225 func_ref(res.cb_name);
4226 }
4227 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004228 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004229 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004230 r = FAIL;
4231
4232 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004233 {
4234 emsg(_("E921: Invalid callback argument"));
4235 res.cb_name = NULL;
4236 }
4237 }
4238 return res;
4239}
4240
4241/*
4242 * Copy a callback into a typval_T.
4243 */
4244 void
4245put_callback(callback_T *cb, typval_T *tv)
4246{
4247 if (cb->cb_partial != NULL)
4248 {
4249 tv->v_type = VAR_PARTIAL;
4250 tv->vval.v_partial = cb->cb_partial;
4251 ++tv->vval.v_partial->pt_refcount;
4252 }
4253 else
4254 {
4255 tv->v_type = VAR_FUNC;
4256 tv->vval.v_string = vim_strsave(cb->cb_name);
4257 func_ref(cb->cb_name);
4258 }
4259}
4260
4261/*
4262 * Make a copy of "src" into "dest", allocating the function name if needed,
4263 * without incrementing the refcount.
4264 */
4265 void
4266set_callback(callback_T *dest, callback_T *src)
4267{
4268 if (src->cb_partial == NULL)
4269 {
4270 // just a function name, make a copy
4271 dest->cb_name = vim_strsave(src->cb_name);
4272 dest->cb_free_name = TRUE;
4273 }
4274 else
4275 {
4276 // cb_name is a pointer into cb_partial
4277 dest->cb_name = src->cb_name;
4278 dest->cb_free_name = FALSE;
4279 }
4280 dest->cb_partial = src->cb_partial;
4281}
4282
4283/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004284 * Copy callback from "src" to "dest", incrementing the refcounts.
4285 */
4286 void
4287copy_callback(callback_T *dest, callback_T *src)
4288{
4289 dest->cb_partial = src->cb_partial;
4290 if (dest->cb_partial != NULL)
4291 {
4292 dest->cb_name = src->cb_name;
4293 dest->cb_free_name = FALSE;
4294 ++dest->cb_partial->pt_refcount;
4295 }
4296 else
4297 {
4298 dest->cb_name = vim_strsave(src->cb_name);
4299 dest->cb_free_name = TRUE;
4300 func_ref(src->cb_name);
4301 }
4302}
4303
4304/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004305 * Unref/free "callback" returned by get_callback() or set_callback().
4306 */
4307 void
4308free_callback(callback_T *callback)
4309{
4310 if (callback->cb_partial != NULL)
4311 {
4312 partial_unref(callback->cb_partial);
4313 callback->cb_partial = NULL;
4314 }
4315 else if (callback->cb_name != NULL)
4316 func_unref(callback->cb_name);
4317 if (callback->cb_free_name)
4318 {
4319 vim_free(callback->cb_name);
4320 callback->cb_free_name = FALSE;
4321 }
4322 callback->cb_name = NULL;
4323}
4324
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004325#endif // FEAT_EVAL