blob: 9c05c57a01054682806dedb5ca12b06752aa3a09 [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},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static 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 +0200176static void list_one_var(dictitem_T *v, char *prefix, int *first);
177static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
178
179/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200180 * Initialize global and vim special variables
181 */
182 void
183evalvars_init(void)
184{
185 int i;
186 struct vimvar *p;
187
188 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
189 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
190 vimvardict.dv_lock = VAR_FIXED;
191 hash_init(&compat_hashtab);
192
193 for (i = 0; i < VV_LEN; ++i)
194 {
195 p = &vimvars[i];
196 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
197 {
198 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
199 getout(1);
200 }
201 STRCPY(p->vv_di.di_key, p->vv_name);
202 if (p->vv_flags & VV_RO)
203 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
204 else if (p->vv_flags & VV_RO_SBX)
205 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
206 else
207 p->vv_di.di_flags = DI_FLAGS_FIX;
208
209 // add to v: scope dict, unless the value is not always available
210 if (p->vv_type != VAR_UNKNOWN)
211 hash_add(&vimvarht, p->vv_di.di_key);
212 if (p->vv_flags & VV_COMPAT)
213 // add to compat scope dict
214 hash_add(&compat_hashtab, p->vv_di.di_key);
215 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200216 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
217 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200218
219 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
220 set_vim_var_nr(VV_HLSEARCH, 1L);
221 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
222 set_vim_var_list(VV_ERRORS, list_alloc());
223 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
224
225 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
226 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
227 set_vim_var_nr(VV_NONE, VVAL_NONE);
228 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100229 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200230
231 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
232 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
233 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
234 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
235 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
236 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
237 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
238 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
239 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
240 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
241 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
242
243 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
244
Bram Moolenaar439c0362020-06-06 15:58:03 +0200245 // Default for v:register is not 0 but '"'. This is adjusted once the
246 // clipboard has been setup by calling reset_reg_var().
247 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200248}
249
250#if defined(EXITFREE) || defined(PROTO)
251/*
252 * Free all vim variables information on exit
253 */
254 void
255evalvars_clear(void)
256{
257 int i;
258 struct vimvar *p;
259
260 for (i = 0; i < VV_LEN; ++i)
261 {
262 p = &vimvars[i];
263 if (p->vv_di.di_tv.v_type == VAR_STRING)
264 VIM_CLEAR(p->vv_str);
265 else if (p->vv_di.di_tv.v_type == VAR_LIST)
266 {
267 list_unref(p->vv_list);
268 p->vv_list = NULL;
269 }
270 }
271 hash_clear(&vimvarht);
272 hash_init(&vimvarht); // garbage_collect() will access it
273 hash_clear(&compat_hashtab);
274
275 // global variables
276 vars_clear(&globvarht);
277
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100278 // Script-local variables. Clear all the variables here.
279 // The scriptvar_T is cleared later in free_scriptnames(), because a
280 // variable in one script might hold a reference to the whole scope of
281 // another script.
282 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200283 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284}
285#endif
286
287 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200288garbage_collect_globvars(int copyID)
289{
290 return set_ref_in_ht(&globvarht, copyID, NULL);
291}
292
293 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200294garbage_collect_vimvars(int copyID)
295{
296 return set_ref_in_ht(&vimvarht, copyID, NULL);
297}
298
299 int
300garbage_collect_scriptvars(int copyID)
301{
Bram Moolenaared234f22020-10-15 20:42:20 +0200302 int i;
303 int idx;
304 int abort = FALSE;
305 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200308 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200309 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
310
Bram Moolenaared234f22020-10-15 20:42:20 +0200311 si = SCRIPT_ITEM(i);
312 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
313 {
314 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
315
316 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
317 }
318 }
319
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200320 return abort;
321}
322
323/*
324 * Set an internal variable to a string value. Creates the variable if it does
325 * not already exist.
326 */
327 void
328set_internal_string_var(char_u *name, char_u *value)
329{
330 char_u *val;
331 typval_T *tvp;
332
333 val = vim_strsave(value);
334 if (val != NULL)
335 {
336 tvp = alloc_string_tv(val);
337 if (tvp != NULL)
338 {
339 set_var(name, tvp, FALSE);
340 free_tv(tvp);
341 }
342 }
343}
344
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200345 int
346eval_charconvert(
347 char_u *enc_from,
348 char_u *enc_to,
349 char_u *fname_from,
350 char_u *fname_to)
351{
352 int err = FALSE;
353
354 set_vim_var_string(VV_CC_FROM, enc_from, -1);
355 set_vim_var_string(VV_CC_TO, enc_to, -1);
356 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
357 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
358 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
359 err = TRUE;
360 set_vim_var_string(VV_CC_FROM, NULL, -1);
361 set_vim_var_string(VV_CC_TO, NULL, -1);
362 set_vim_var_string(VV_FNAME_IN, NULL, -1);
363 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
364
365 if (err)
366 return FAIL;
367 return OK;
368}
369
370# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
371 int
372eval_printexpr(char_u *fname, char_u *args)
373{
374 int err = FALSE;
375
376 set_vim_var_string(VV_FNAME_IN, fname, -1);
377 set_vim_var_string(VV_CMDARG, args, -1);
378 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
379 err = TRUE;
380 set_vim_var_string(VV_FNAME_IN, NULL, -1);
381 set_vim_var_string(VV_CMDARG, NULL, -1);
382
383 if (err)
384 {
385 mch_remove(fname);
386 return FAIL;
387 }
388 return OK;
389}
390# endif
391
392# if defined(FEAT_DIFF) || defined(PROTO)
393 void
394eval_diff(
395 char_u *origfile,
396 char_u *newfile,
397 char_u *outfile)
398{
399 int err = FALSE;
400
401 set_vim_var_string(VV_FNAME_IN, origfile, -1);
402 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
403 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
404 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
405 set_vim_var_string(VV_FNAME_IN, NULL, -1);
406 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
407 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
408}
409
410 void
411eval_patch(
412 char_u *origfile,
413 char_u *difffile,
414 char_u *outfile)
415{
416 int err;
417
418 set_vim_var_string(VV_FNAME_IN, origfile, -1);
419 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
420 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
421 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
422 set_vim_var_string(VV_FNAME_IN, NULL, -1);
423 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
424 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
425}
426# endif
427
428#if defined(FEAT_SPELL) || defined(PROTO)
429/*
430 * Evaluate an expression to a list with suggestions.
431 * For the "expr:" part of 'spellsuggest'.
432 * Returns NULL when there is an error.
433 */
434 list_T *
435eval_spell_expr(char_u *badword, char_u *expr)
436{
437 typval_T save_val;
438 typval_T rettv;
439 list_T *list = NULL;
440 char_u *p = skipwhite(expr);
441
442 // Set "v:val" to the bad word.
443 prepare_vimvar(VV_VAL, &save_val);
444 set_vim_var_string(VV_VAL, badword, -1);
445 if (p_verbose == 0)
446 ++emsg_off;
447
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200448 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200449 {
450 if (rettv.v_type != VAR_LIST)
451 clear_tv(&rettv);
452 else
453 list = rettv.vval.v_list;
454 }
455
456 if (p_verbose == 0)
457 --emsg_off;
458 clear_tv(get_vim_var_tv(VV_VAL));
459 restore_vimvar(VV_VAL, &save_val);
460
461 return list;
462}
463
464/*
465 * "list" is supposed to contain two items: a word and a number. Return the
466 * word in "pp" and the number as the return value.
467 * Return -1 if anything isn't right.
468 * Used to get the good word and score from the eval_spell_expr() result.
469 */
470 int
471get_spellword(list_T *list, char_u **pp)
472{
473 listitem_T *li;
474
475 li = list->lv_first;
476 if (li == NULL)
477 return -1;
478 *pp = tv_get_string(&li->li_tv);
479
480 li = li->li_next;
481 if (li == NULL)
482 return -1;
483 return (int)tv_get_number(&li->li_tv);
484}
485#endif
486
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200487/*
488 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200489 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200490 * When not used yet add the variable to the v: hashtable.
491 */
492 void
493prepare_vimvar(int idx, typval_T *save_tv)
494{
495 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200496 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200497 if (vimvars[idx].vv_type == VAR_UNKNOWN)
498 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
499}
500
501/*
502 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200503 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200504 * When no longer defined, remove the variable from the v: hashtable.
505 */
506 void
507restore_vimvar(int idx, typval_T *save_tv)
508{
509 hashitem_T *hi;
510
511 vimvars[idx].vv_tv = *save_tv;
512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
513 {
514 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
515 if (HASHITEM_EMPTY(hi))
516 internal_error("restore_vimvar()");
517 else
518 hash_remove(&vimvarht, hi);
519 }
520}
521
522/*
523 * List Vim variables.
524 */
525 static void
526list_vim_vars(int *first)
527{
528 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
529}
530
531/*
532 * List script-local variables, if there is a script.
533 */
534 static void
535list_script_vars(int *first)
536{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200537 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200538 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
539 "s:", FALSE, first);
540}
541
542/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200543 * Get a list of lines from a HERE document. The here document is a list of
544 * lines surrounded by a marker.
545 * cmd << {marker}
546 * {line1}
547 * {line2}
548 * ....
549 * {marker}
550 *
551 * The {marker} is a string. If the optional 'trim' word is supplied before the
552 * marker, then the leading indentation before the lines (matching the
553 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200554 *
555 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
556 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
557 * missing, then '.' is accepted as a marker.
558 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200559 * Returns a List with {lines} or NULL.
560 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200562heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200563{
564 char_u *theline;
565 char_u *marker;
566 list_T *l;
567 char_u *p;
568 int marker_indent_len = 0;
569 int text_indent_len = 0;
570 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200571 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200572 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200573
574 if (eap->getline == NULL)
575 {
576 emsg(_("E991: cannot use =<< here"));
577 return NULL;
578 }
579
580 // Check for the optional 'trim' word before the marker
581 cmd = skipwhite(cmd);
582 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
583 {
584 cmd = skipwhite(cmd + 4);
585
586 // Trim the indentation from all the lines in the here document.
587 // The amount of indentation trimmed is the same as the indentation of
588 // the first line after the :let command line. To find the end marker
589 // the indent of the :let command line is trimmed.
590 p = *eap->cmdlinep;
591 while (VIM_ISWHITE(*p))
592 {
593 p++;
594 marker_indent_len++;
595 }
596 text_indent_len = -1;
597 }
598
599 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200600 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200601 {
602 marker = skipwhite(cmd);
603 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200604 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200605 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200606 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 return NULL;
608 }
609 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200610 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200611 {
612 emsg(_("E221: Marker cannot start with lower case letter"));
613 return NULL;
614 }
615 }
616 else
617 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200618 // When getting lines for an embedded script, if the marker is missing,
619 // accept '.' as the marker.
620 if (script_get)
621 marker = dot;
622 else
623 {
624 emsg(_("E172: Missing marker"));
625 return NULL;
626 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200627 }
628
629 l = list_alloc();
630 if (l == NULL)
631 return NULL;
632
633 for (;;)
634 {
635 int mi = 0;
636 int ti = 0;
637
638 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
639 if (theline == NULL)
640 {
641 semsg(_("E990: Missing end marker '%s'"), marker);
642 break;
643 }
644
645 // with "trim": skip the indent matching the :let line to find the
646 // marker
647 if (marker_indent_len > 0
648 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
649 mi = marker_indent_len;
650 if (STRCMP(marker, theline + mi) == 0)
651 {
652 vim_free(theline);
653 break;
654 }
655
656 if (text_indent_len == -1 && *theline != NUL)
657 {
658 // set the text indent from the first line.
659 p = theline;
660 text_indent_len = 0;
661 while (VIM_ISWHITE(*p))
662 {
663 p++;
664 text_indent_len++;
665 }
666 text_indent = vim_strnsave(theline, text_indent_len);
667 }
668 // with "trim": skip the indent matching the first line
669 if (text_indent != NULL)
670 for (ti = 0; ti < text_indent_len; ++ti)
671 if (theline[ti] != text_indent[ti])
672 break;
673
674 if (list_append_string(l, theline + ti, -1) == FAIL)
675 break;
676 vim_free(theline);
677 }
678 vim_free(text_indent);
679
680 return l;
681}
682
683/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200684 * Vim9 variable declaration:
685 * ":var name"
686 * ":var name: type"
687 * ":var name = expr"
688 * ":var name: type = expr"
689 * etc.
690 */
691 void
692ex_var(exarg_T *eap)
693{
694 if (!in_vim9script())
695 {
696 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
697 return;
698 }
699 ex_let(eap);
700}
701
702/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200703 * ":let" list all variable values
704 * ":let var1 var2" list variable values
705 * ":let var = expr" assignment command.
706 * ":let var += expr" assignment command.
707 * ":let var -= expr" assignment command.
708 * ":let var *= expr" assignment command.
709 * ":let var /= expr" assignment command.
710 * ":let var %= expr" assignment command.
711 * ":let var .= expr" assignment command.
712 * ":let var ..= expr" assignment command.
713 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100715 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200716 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200717 * ":final var = expr" assignment command.
718 * ":final [var1, var2] = expr" unpack list.
719 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200720 * ":const" list all variable values
721 * ":const var1 var2" list variable values
722 * ":const var = expr" assignment command.
723 * ":const [var1, var2] = expr" unpack list.
724 */
725 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200726ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200727{
728 char_u *arg = eap->arg;
729 char_u *expr = NULL;
730 typval_T rettv;
731 int i;
732 int var_count = 0;
733 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200734 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200735 char_u *argend;
736 int first = TRUE;
737 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200738 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200739 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200740 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200741
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200742 if (eap->cmdidx == CMD_final && !vim9script)
743 {
744 // In legacy Vim script ":final" is short for ":finally".
745 ex_finally(eap);
746 return;
747 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200748 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200749 {
750 emsg(_(e_cannot_use_let_in_vim9_script));
751 return;
752 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200753 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
754 // In legacy Vim script ":const" works like ":final".
755 eap->cmdidx = CMD_final;
756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 // detect Vim9 assignment without ":let" or ":const"
758 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200759 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200761 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 if (argend == NULL)
763 return;
764 if (argend > arg && argend[-1] == '.') // for var.='str'
765 --argend;
766 expr = skipwhite(argend);
767 concat = expr[0] == '.'
768 && ((expr[1] == '=' && current_sctx.sc_version < 2)
769 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200770 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
771 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200772 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200773 {
774 // ":let" without "=": list variables
775 if (*arg == '[')
776 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200777 else if (expr[0] == '.' && expr[1] == '=')
778 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200779 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200780 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200782 {
783 // Vim9 declaration ":let var: type"
784 arg = vim9_declare_scriptvar(eap, arg);
785 }
786 else
787 {
788 // ":let var1 var2" - list values
789 arg = list_arg_vars(eap, arg, &first);
790 }
791 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200792 else if (!eap->skip)
793 {
794 // ":let"
795 list_glob_vars(&first);
796 list_buf_vars(&first);
797 list_win_vars(&first);
798 list_tab_vars(&first);
799 list_script_vars(&first);
800 list_func_vars(&first);
801 list_vim_vars(&first);
802 }
803 eap->nextcmd = check_nextcmd(arg);
804 }
805 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
806 {
807 list_T *l;
808
809 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200810 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200811 if (l != NULL)
812 {
813 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200814 if (!eap->skip)
815 {
816 op[0] = '=';
817 op[1] = NUL;
818 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200820 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 clear_tv(&rettv);
822 }
823 }
824 else
825 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200826 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200827 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200828
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200829 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200830 i = FAIL;
831 if (has_assign || concat)
832 {
833 op[0] = '=';
834 op[1] = NUL;
835 if (*expr != '=')
836 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200837 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200838 {
839 // +=, /=, etc. require an existing variable
840 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
841 i = FAIL;
842 }
843 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200844 {
845 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200846 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200847 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200848 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200849 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++len;
851 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200852 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200853 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 }
855 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200856 ++expr;
857
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200858 if (vim9script && (!VIM_ISWHITE(*argend)
859 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 {
861 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200862 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200863 i = FAIL;
864 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200865
866 if (eap->skip)
867 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200868 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200869 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200870 if (getline_equal(eap->getline, eap->cookie, getsourceline))
871 {
872 evalarg.eval_getline = eap->getline;
873 evalarg.eval_cookie = eap->cookie;
874 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200875 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200876 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200877 if (eap->skip)
878 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200879 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200880 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200881 if (eap->skip)
882 {
883 if (i != FAIL)
884 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200885 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200886 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200887 {
888 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200889 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200890 clear_tv(&rettv);
891 }
892 }
893}
894
895/*
896 * Assign the typevalue "tv" to the variable or variables at "arg_start".
897 * Handles both "var" with any type and "[var, var; var]" with a list type.
898 * When "op" is not NULL it points to a string with characters that
899 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
900 * or concatenate.
901 * Returns OK or FAIL;
902 */
903 int
904ex_let_vars(
905 char_u *arg_start,
906 typval_T *tv,
907 int copy, // copy values from "tv", don't move
908 int semicolon, // from skip_var_list()
909 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200910 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200911 char_u *op)
912{
913 char_u *arg = arg_start;
914 list_T *l;
915 int i;
916 listitem_T *item;
917 typval_T ltv;
918
919 if (*arg != '[')
920 {
921 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200923 return FAIL;
924 return OK;
925 }
926
927 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
928 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
929 {
930 emsg(_(e_listreq));
931 return FAIL;
932 }
933
934 i = list_len(l);
935 if (semicolon == 0 && var_count < i)
936 {
937 emsg(_("E687: Less targets than List items"));
938 return FAIL;
939 }
940 if (var_count - semicolon > i)
941 {
942 emsg(_("E688: More targets than List items"));
943 return FAIL;
944 }
945
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200946 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200947 item = l->lv_first;
948 while (*arg != ']')
949 {
950 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952 item = item->li_next;
953 if (arg == NULL)
954 return FAIL;
955
956 arg = skipwhite(arg);
957 if (*arg == ';')
958 {
959 // Put the rest of the list (may be empty) in the var after ';'.
960 // Create a new list for this.
961 l = list_alloc();
962 if (l == NULL)
963 return FAIL;
964 while (item != NULL)
965 {
966 list_append_tv(l, &item->li_tv);
967 item = item->li_next;
968 }
969
970 ltv.v_type = VAR_LIST;
971 ltv.v_lock = 0;
972 ltv.vval.v_list = l;
973 l->lv_refcount = 1;
974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200976 (char_u *)"]", op);
977 clear_tv(&ltv);
978 if (arg == NULL)
979 return FAIL;
980 break;
981 }
982 else if (*arg != ',' && *arg != ']')
983 {
984 internal_error("ex_let_vars()");
985 return FAIL;
986 }
987 }
988
989 return OK;
990}
991
992/*
993 * Skip over assignable variable "var" or list of variables "[var, var]".
994 * Used for ":let varvar = expr" and ":for varvar in expr".
995 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200996 * for "[var, var; var]" set "semicolon" to 1.
997 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998 * Return NULL for an error.
999 */
1000 char_u *
1001skip_var_list(
1002 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001004 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001005 int *semicolon,
1006 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007{
1008 char_u *p, *s;
1009
1010 if (*arg == '[')
1011 {
1012 // "[var, var]": find the matching ']'.
1013 p = arg;
1014 for (;;)
1015 {
1016 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001017 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001018 if (s == p)
1019 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001020 if (!silent)
1021 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 return NULL;
1023 }
1024 ++*var_count;
1025
1026 p = skipwhite(s);
1027 if (*p == ']')
1028 break;
1029 else if (*p == ';')
1030 {
1031 if (*semicolon == 1)
1032 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001033 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001034 return NULL;
1035 }
1036 *semicolon = 1;
1037 }
1038 else if (*p != ',')
1039 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001040 if (!silent)
1041 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 return NULL;
1043 }
1044 }
1045 return p + 1;
1046 }
1047 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049}
1050
1051/*
1052 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1053 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001056 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 char_u *end;
1060
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001061 if (*arg == '@' && arg[1] != NUL)
1062 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001064 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001065 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001067 // "a: type" is declaring variable "a" with a type, not "a:".
1068 if (end == arg + 2 && end[-1] == ':')
1069 --end;
1070 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001071 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 }
1073 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001074}
1075
1076/*
1077 * List variables for hashtab "ht" with prefix "prefix".
1078 * If "empty" is TRUE also list NULL strings as empty strings.
1079 */
1080 void
1081list_hashtable_vars(
1082 hashtab_T *ht,
1083 char *prefix,
1084 int empty,
1085 int *first)
1086{
1087 hashitem_T *hi;
1088 dictitem_T *di;
1089 int todo;
1090 char_u buf[IOSIZE];
1091
1092 todo = (int)ht->ht_used;
1093 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1094 {
1095 if (!HASHITEM_EMPTY(hi))
1096 {
1097 --todo;
1098 di = HI2DI(hi);
1099
1100 // apply :filter /pat/ to variable name
1101 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1102 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1103 if (message_filtered(buf))
1104 continue;
1105
1106 if (empty || di->di_tv.v_type != VAR_STRING
1107 || di->di_tv.vval.v_string != NULL)
1108 list_one_var(di, prefix, first);
1109 }
1110 }
1111}
1112
1113/*
1114 * List global variables.
1115 */
1116 static void
1117list_glob_vars(int *first)
1118{
1119 list_hashtable_vars(&globvarht, "", TRUE, first);
1120}
1121
1122/*
1123 * List buffer variables.
1124 */
1125 static void
1126list_buf_vars(int *first)
1127{
1128 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1129}
1130
1131/*
1132 * List window variables.
1133 */
1134 static void
1135list_win_vars(int *first)
1136{
1137 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1138}
1139
1140/*
1141 * List tab page variables.
1142 */
1143 static void
1144list_tab_vars(int *first)
1145{
1146 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1147}
1148
1149/*
1150 * List variables in "arg".
1151 */
1152 static char_u *
1153list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1154{
1155 int error = FALSE;
1156 int len;
1157 char_u *name;
1158 char_u *name_start;
1159 char_u *arg_subsc;
1160 char_u *tofree;
1161 typval_T tv;
1162
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001163 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001164 {
1165 if (error || eap->skip)
1166 {
1167 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1168 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1169 {
1170 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001171 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172 break;
1173 }
1174 }
1175 else
1176 {
1177 // get_name_len() takes care of expanding curly braces
1178 name_start = name = arg;
1179 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1180 if (len <= 0)
1181 {
1182 // This is mainly to keep test 49 working: when expanding
1183 // curly braces fails overrule the exception error message.
1184 if (len < 0 && !aborting())
1185 {
1186 emsg_severe = TRUE;
1187 semsg(_(e_invarg2), arg);
1188 break;
1189 }
1190 error = TRUE;
1191 }
1192 else
1193 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001194 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001195 if (tofree != NULL)
1196 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001197 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 error = TRUE;
1199 else
1200 {
1201 // handle d.key, l[idx], f(expr)
1202 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001203 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001204 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001205 error = TRUE;
1206 else
1207 {
1208 if (arg == arg_subsc && len == 2 && name[1] == ':')
1209 {
1210 switch (*name)
1211 {
1212 case 'g': list_glob_vars(first); break;
1213 case 'b': list_buf_vars(first); break;
1214 case 'w': list_win_vars(first); break;
1215 case 't': list_tab_vars(first); break;
1216 case 'v': list_vim_vars(first); break;
1217 case 's': list_script_vars(first); break;
1218 case 'l': list_func_vars(first); break;
1219 default:
1220 semsg(_("E738: Can't list variables for %s"), name);
1221 }
1222 }
1223 else
1224 {
1225 char_u numbuf[NUMBUFLEN];
1226 char_u *tf;
1227 int c;
1228 char_u *s;
1229
1230 s = echo_string(&tv, &tf, numbuf, 0);
1231 c = *arg;
1232 *arg = NUL;
1233 list_one_var_a("",
1234 arg == arg_subsc ? name : name_start,
1235 tv.v_type,
1236 s == NULL ? (char_u *)"" : s,
1237 first);
1238 *arg = c;
1239 vim_free(tf);
1240 }
1241 clear_tv(&tv);
1242 }
1243 }
1244 }
1245
1246 vim_free(tofree);
1247 }
1248
1249 arg = skipwhite(arg);
1250 }
1251
1252 return arg;
1253}
1254
1255/*
1256 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1257 * Returns a pointer to the char just after the var name.
1258 * Returns NULL if there is an error.
1259 */
1260 static char_u *
1261ex_let_one(
1262 char_u *arg, // points to variable name
1263 typval_T *tv, // value to assign to variable
1264 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001265 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266 char_u *endchars, // valid chars after variable name or NULL
1267 char_u *op) // "+", "-", "." or NULL
1268{
1269 int c1;
1270 char_u *name;
1271 char_u *p;
1272 char_u *arg_end = NULL;
1273 int len;
1274 int opt_flags;
1275 char_u *tofree = NULL;
1276
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001277 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001278 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1279 {
1280 vim9_declare_error(arg);
1281 return NULL;
1282 }
1283
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001284 // ":let $VAR = expr": Set environment variable.
1285 if (*arg == '$')
1286 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001287 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 {
1289 emsg(_("E996: Cannot lock an environment variable"));
1290 return NULL;
1291 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001292
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 // Find the end of the name.
1294 ++arg;
1295 name = arg;
1296 len = get_env_len(&arg);
1297 if (len == 0)
1298 semsg(_(e_invarg2), name - 1);
1299 else
1300 {
1301 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1302 semsg(_(e_letwrong), op);
1303 else if (endchars != NULL
1304 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1305 emsg(_(e_letunexp));
1306 else if (!check_secure())
1307 {
1308 c1 = name[len];
1309 name[len] = NUL;
1310 p = tv_get_string_chk(tv);
1311 if (p != NULL && op != NULL && *op == '.')
1312 {
1313 int mustfree = FALSE;
1314 char_u *s = vim_getenv(name, &mustfree);
1315
1316 if (s != NULL)
1317 {
1318 p = tofree = concat_str(s, p);
1319 if (mustfree)
1320 vim_free(s);
1321 }
1322 }
1323 if (p != NULL)
1324 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001325 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 arg_end = arg;
1327 }
1328 name[len] = c1;
1329 vim_free(tofree);
1330 }
1331 }
1332 }
1333
1334 // ":let &option = expr": Set option value.
1335 // ":let &l:option = expr": Set local option value.
1336 // ":let &g:option = expr": Set global option value.
1337 else if (*arg == '&')
1338 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001339 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 return NULL;
1343 }
1344 // Find the end of the name.
1345 p = find_option_end(&arg, &opt_flags);
1346 if (p == NULL || (endchars != NULL
1347 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1348 emsg(_(e_letunexp));
1349 else
1350 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001351 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 int opt_type;
1353 long numval;
1354 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001355 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001356 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357
1358 c1 = *p;
1359 *p = NUL;
1360
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001361 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1362 if ((opt_type == 1 || opt_type == -1)
1363 && (tv->v_type != VAR_STRING || !in_vim9script()))
1364 // number, possibly hidden
1365 n = (long)tv_get_number(tv);
1366
1367 // Avoid setting a string option to the text "v:false" or similar.
1368 // In Vim9 script also don't convert a number to string.
1369 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1370 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1371 s = tv_get_string_chk(tv);
1372
1373 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001374 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375 if ((opt_type == 1 && *op == '.')
1376 || (opt_type == 0 && *op != '.'))
1377 {
1378 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001379 failed = TRUE; // don't set the value
1380
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381 }
1382 else
1383 {
1384 if (opt_type == 1) // number
1385 {
1386 switch (*op)
1387 {
1388 case '+': n = numval + n; break;
1389 case '-': n = numval - n; break;
1390 case '*': n = numval * n; break;
1391 case '/': n = (long)num_divide(numval, n); break;
1392 case '%': n = (long)num_modulus(numval, n); break;
1393 }
1394 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001395 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001396 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001397 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001398 s = concat_str(stringval, s);
1399 vim_free(stringval);
1400 stringval = s;
1401 }
1402 }
1403 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001404
1405 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001406 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001407 if (opt_type != 0 || s != NULL)
1408 {
1409 set_option_value(arg, n, s, opt_flags);
1410 arg_end = p;
1411 }
1412 else
1413 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414 }
1415 *p = c1;
1416 vim_free(stringval);
1417 }
1418 }
1419
1420 // ":let @r = expr": Set register contents.
1421 else if (*arg == '@')
1422 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001423 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001424 {
1425 emsg(_("E996: Cannot lock a register"));
1426 return NULL;
1427 }
1428 ++arg;
1429 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1430 semsg(_(e_letwrong), op);
1431 else if (endchars != NULL
1432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1433 emsg(_(e_letunexp));
1434 else
1435 {
1436 char_u *ptofree = NULL;
1437 char_u *s;
1438
1439 p = tv_get_string_chk(tv);
1440 if (p != NULL && op != NULL && *op == '.')
1441 {
1442 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1443 if (s != NULL)
1444 {
1445 p = ptofree = concat_str(s, p);
1446 vim_free(s);
1447 }
1448 }
1449 if (p != NULL)
1450 {
1451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1452 arg_end = arg + 1;
1453 }
1454 vim_free(ptofree);
1455 }
1456 }
1457
1458 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001460 // ":let {expr} = expr": Idem, name made with curly braces
1461 else if (eval_isnamec1(*arg) || *arg == '{')
1462 {
1463 lval_T lv;
1464
1465 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001466 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 if (endchars != NULL && vim_strchr(endchars,
1469 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470 emsg(_(e_letunexp));
1471 else
1472 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474 arg_end = p;
1475 }
1476 }
1477 clear_lval(&lv);
1478 }
1479
1480 else
1481 semsg(_(e_invarg2), arg);
1482
1483 return arg_end;
1484}
1485
1486/*
1487 * ":unlet[!] var1 ... " command.
1488 */
1489 void
1490ex_unlet(exarg_T *eap)
1491{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001492 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001493}
1494
1495/*
1496 * ":lockvar" and ":unlockvar" commands
1497 */
1498 void
1499ex_lockvar(exarg_T *eap)
1500{
1501 char_u *arg = eap->arg;
1502 int deep = 2;
1503
1504 if (eap->forceit)
1505 deep = -1;
1506 else if (vim_isdigit(*arg))
1507 {
1508 deep = getdigits(&arg);
1509 arg = skipwhite(arg);
1510 }
1511
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001512 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001513}
1514
1515/*
1516 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001517 * Also used for Vim9 script. "callback" is invoked as:
1518 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001519 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001520 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521ex_unletlock(
1522 exarg_T *eap,
1523 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001524 int deep,
1525 int glv_flags,
1526 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1527 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001528{
1529 char_u *arg = argstart;
1530 char_u *name_end;
1531 int error = FALSE;
1532 lval_T lv;
1533
1534 do
1535 {
1536 if (*arg == '$')
1537 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001538 lv.ll_name = arg;
1539 lv.ll_tv = NULL;
1540 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001541 if (get_env_len(&arg) == 0)
1542 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001543 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544 return;
1545 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001546 if (!error && !eap->skip
1547 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1548 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001549 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001550 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001551 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001553 // Parse the name and find the end.
1554 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1555 glv_flags, FNE_CHECK_START);
1556 if (lv.ll_name == NULL)
1557 error = TRUE; // error but continue parsing
1558 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1559 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001560 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001561 if (name_end != NULL)
1562 {
1563 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001564 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001565 }
1566 if (!(eap->skip || error))
1567 clear_lval(&lv);
1568 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001569 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001570
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001571 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001572 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001573 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001574
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001575 if (!eap->skip)
1576 clear_lval(&lv);
1577 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578
1579 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001580 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581
1582 eap->nextcmd = check_nextcmd(arg);
1583}
1584
1585 static int
1586do_unlet_var(
1587 lval_T *lp,
1588 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001589 exarg_T *eap,
1590 int deep UNUSED,
1591 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001592{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001593 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 int ret = OK;
1595 int cc;
1596
1597 if (lp->ll_tv == NULL)
1598 {
1599 cc = *name_end;
1600 *name_end = NUL;
1601
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001602 // Environment variable, normal name or expanded name.
1603 if (*lp->ll_name == '$')
1604 vim_unsetenv(lp->ll_name + 1);
1605 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 ret = FAIL;
1607 *name_end = cc;
1608 }
1609 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001610 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001611 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001612 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 return FAIL;
1614 else if (lp->ll_range)
1615 {
1616 listitem_T *li;
1617 listitem_T *ll_li = lp->ll_li;
1618 int ll_n1 = lp->ll_n1;
1619
1620 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1621 {
1622 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001623 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624 return FAIL;
1625 ll_li = li;
1626 ++ll_n1;
1627 }
1628
1629 // Delete a range of List items.
1630 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1631 {
1632 li = lp->ll_li->li_next;
1633 listitem_remove(lp->ll_list, lp->ll_li);
1634 lp->ll_li = li;
1635 ++lp->ll_n1;
1636 }
1637 }
1638 else
1639 {
1640 if (lp->ll_list != NULL)
1641 // unlet a List item.
1642 listitem_remove(lp->ll_list, lp->ll_li);
1643 else
1644 // unlet a Dictionary item.
1645 dictitem_remove(lp->ll_dict, lp->ll_di);
1646 }
1647
1648 return ret;
1649}
1650
1651/*
1652 * "unlet" a variable. Return OK if it existed, FAIL if not.
1653 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1654 */
1655 int
1656do_unlet(char_u *name, int forceit)
1657{
1658 hashtab_T *ht;
1659 hashitem_T *hi;
1660 char_u *varname;
1661 dict_T *d;
1662 dictitem_T *di;
1663
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001664 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001665 return FAIL;
1666
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667 ht = find_var_ht(name, &varname);
1668 if (ht != NULL && *varname != NUL)
1669 {
1670 d = get_current_funccal_dict(ht);
1671 if (d == NULL)
1672 {
1673 if (ht == &globvarht)
1674 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001675 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001676 d = &vimvardict;
1677 else
1678 {
1679 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1680 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1681 }
1682 if (d == NULL)
1683 {
1684 internal_error("do_unlet()");
1685 return FAIL;
1686 }
1687 }
1688 hi = hash_find(ht, varname);
1689 if (HASHITEM_EMPTY(hi))
1690 hi = find_hi_in_scoped_ht(name, &ht);
1691 if (hi != NULL && !HASHITEM_EMPTY(hi))
1692 {
1693 di = HI2DI(hi);
1694 if (var_check_fixed(di->di_flags, name, FALSE)
1695 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001696 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001697 return FAIL;
1698
1699 delete_var(ht, hi);
1700 return OK;
1701 }
1702 }
1703 if (forceit)
1704 return OK;
1705 semsg(_("E108: No such variable: \"%s\""), name);
1706 return FAIL;
1707}
1708
1709/*
1710 * Lock or unlock variable indicated by "lp".
1711 * "deep" is the levels to go (-1 for unlimited);
1712 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1713 */
1714 static int
1715do_lock_var(
1716 lval_T *lp,
1717 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001718 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001720 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001722 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001723 int ret = OK;
1724 int cc;
1725 dictitem_T *di;
1726
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001727 if (lp->ll_tv == NULL)
1728 {
1729 cc = *name_end;
1730 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001731 if (*lp->ll_name == '$')
1732 {
1733 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001735 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 else
1737 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001738 // Normal name or expanded name.
1739 di = find_var(lp->ll_name, NULL, TRUE);
1740 if (di == NULL)
1741 ret = FAIL;
1742 else if ((di->di_flags & DI_FLAGS_FIX)
1743 && di->di_tv.v_type != VAR_DICT
1744 && di->di_tv.v_type != VAR_LIST)
1745 {
1746 // For historic reasons this error is not given for a list or
1747 // dict. E.g., the b: dict could be locked/unlocked.
1748 semsg(_(e_lock_unlock), lp->ll_name);
1749 ret = FAIL;
1750 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001752 {
1753 if (lock)
1754 di->di_flags |= DI_FLAGS_LOCK;
1755 else
1756 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001757 if (deep != 0)
1758 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001759 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760 }
1761 *name_end = cc;
1762 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001763 else if (deep == 0)
1764 {
1765 // nothing to do
1766 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001767 else if (lp->ll_range)
1768 {
1769 listitem_T *li = lp->ll_li;
1770
1771 // (un)lock a range of List items.
1772 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1773 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001774 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001775 li = li->li_next;
1776 ++lp->ll_n1;
1777 }
1778 }
1779 else if (lp->ll_list != NULL)
1780 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001781 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001782 else
1783 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001784 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001785
1786 return ret;
1787}
1788
1789/*
1790 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001791 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1792 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001794 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001795item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001796{
1797 static int recurse = 0;
1798 list_T *l;
1799 listitem_T *li;
1800 dict_T *d;
1801 blob_T *b;
1802 hashitem_T *hi;
1803 int todo;
1804
1805 if (recurse >= DICT_MAXNEST)
1806 {
1807 emsg(_("E743: variable nested too deep for (un)lock"));
1808 return;
1809 }
1810 if (deep == 0)
1811 return;
1812 ++recurse;
1813
1814 // lock/unlock the item itself
1815 if (lock)
1816 tv->v_lock |= VAR_LOCKED;
1817 else
1818 tv->v_lock &= ~VAR_LOCKED;
1819
1820 switch (tv->v_type)
1821 {
1822 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001823 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001825 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 case VAR_STRING:
1828 case VAR_FUNC:
1829 case VAR_PARTIAL:
1830 case VAR_FLOAT:
1831 case VAR_SPECIAL:
1832 case VAR_JOB:
1833 case VAR_CHANNEL:
1834 break;
1835
1836 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001837 if ((b = tv->vval.v_blob) != NULL
1838 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 {
1840 if (lock)
1841 b->bv_lock |= VAR_LOCKED;
1842 else
1843 b->bv_lock &= ~VAR_LOCKED;
1844 }
1845 break;
1846 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001847 if ((l = tv->vval.v_list) != NULL
1848 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 {
1850 if (lock)
1851 l->lv_lock |= VAR_LOCKED;
1852 else
1853 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001854 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001856 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001857 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 }
1859 break;
1860 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001861 if ((d = tv->vval.v_dict) != NULL
1862 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 {
1864 if (lock)
1865 d->dv_lock |= VAR_LOCKED;
1866 else
1867 d->dv_lock &= ~VAR_LOCKED;
1868 if (deep < 0 || deep > 1)
1869 {
1870 // recursive: lock/unlock the items the List contains
1871 todo = (int)d->dv_hashtab.ht_used;
1872 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1873 {
1874 if (!HASHITEM_EMPTY(hi))
1875 {
1876 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001877 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1878 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 }
1880 }
1881 }
1882 }
1883 }
1884 --recurse;
1885}
1886
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001887#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1888/*
1889 * Delete all "menutrans_" variables.
1890 */
1891 void
1892del_menutrans_vars(void)
1893{
1894 hashitem_T *hi;
1895 int todo;
1896
1897 hash_lock(&globvarht);
1898 todo = (int)globvarht.ht_used;
1899 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1900 {
1901 if (!HASHITEM_EMPTY(hi))
1902 {
1903 --todo;
1904 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1905 delete_var(&globvarht, hi);
1906 }
1907 }
1908 hash_unlock(&globvarht);
1909}
1910#endif
1911
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001912/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001913 * Local string buffer for the next two functions to store a variable name
1914 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1915 * get_user_var_name().
1916 */
1917
1918static char_u *varnamebuf = NULL;
1919static int varnamebuflen = 0;
1920
1921/*
1922 * Function to concatenate a prefix and a variable name.
1923 */
1924 static char_u *
1925cat_prefix_varname(int prefix, char_u *name)
1926{
1927 int len;
1928
1929 len = (int)STRLEN(name) + 3;
1930 if (len > varnamebuflen)
1931 {
1932 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001933 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001934 varnamebuf = alloc(len);
1935 if (varnamebuf == NULL)
1936 {
1937 varnamebuflen = 0;
1938 return NULL;
1939 }
1940 varnamebuflen = len;
1941 }
1942 *varnamebuf = prefix;
1943 varnamebuf[1] = ':';
1944 STRCPY(varnamebuf + 2, name);
1945 return varnamebuf;
1946}
1947
1948/*
1949 * Function given to ExpandGeneric() to obtain the list of user defined
1950 * (global/buffer/window/built-in) variable names.
1951 */
1952 char_u *
1953get_user_var_name(expand_T *xp, int idx)
1954{
1955 static long_u gdone;
1956 static long_u bdone;
1957 static long_u wdone;
1958 static long_u tdone;
1959 static int vidx;
1960 static hashitem_T *hi;
1961 hashtab_T *ht;
1962
1963 if (idx == 0)
1964 {
1965 gdone = bdone = wdone = vidx = 0;
1966 tdone = 0;
1967 }
1968
1969 // Global variables
1970 if (gdone < globvarht.ht_used)
1971 {
1972 if (gdone++ == 0)
1973 hi = globvarht.ht_array;
1974 else
1975 ++hi;
1976 while (HASHITEM_EMPTY(hi))
1977 ++hi;
1978 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1979 return cat_prefix_varname('g', hi->hi_key);
1980 return hi->hi_key;
1981 }
1982
1983 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01001984 ht =
1985#ifdef FEAT_CMDWIN
1986 // In cmdwin, the alternative buffer should be used.
1987 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
1988 &prevwin->w_buffer->b_vars->dv_hashtab :
1989#endif
1990 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001991 if (bdone < ht->ht_used)
1992 {
1993 if (bdone++ == 0)
1994 hi = ht->ht_array;
1995 else
1996 ++hi;
1997 while (HASHITEM_EMPTY(hi))
1998 ++hi;
1999 return cat_prefix_varname('b', hi->hi_key);
2000 }
2001
2002 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002003 ht =
2004#ifdef FEAT_CMDWIN
2005 // In cmdwin, the alternative window should be used.
2006 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2007 &prevwin->w_vars->dv_hashtab :
2008#endif
2009 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002010 if (wdone < ht->ht_used)
2011 {
2012 if (wdone++ == 0)
2013 hi = ht->ht_array;
2014 else
2015 ++hi;
2016 while (HASHITEM_EMPTY(hi))
2017 ++hi;
2018 return cat_prefix_varname('w', hi->hi_key);
2019 }
2020
2021 // t: variables
2022 ht = &curtab->tp_vars->dv_hashtab;
2023 if (tdone < ht->ht_used)
2024 {
2025 if (tdone++ == 0)
2026 hi = ht->ht_array;
2027 else
2028 ++hi;
2029 while (HASHITEM_EMPTY(hi))
2030 ++hi;
2031 return cat_prefix_varname('t', hi->hi_key);
2032 }
2033
2034 // v: variables
2035 if (vidx < VV_LEN)
2036 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2037
2038 VIM_CLEAR(varnamebuf);
2039 varnamebuflen = 0;
2040 return NULL;
2041}
2042
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002043 char *
2044get_var_special_name(int nr)
2045{
2046 switch (nr)
2047 {
2048 case VVAL_FALSE: return "v:false";
2049 case VVAL_TRUE: return "v:true";
2050 case VVAL_NONE: return "v:none";
2051 case VVAL_NULL: return "v:null";
2052 }
2053 internal_error("get_var_special_name()");
2054 return "42";
2055}
2056
2057/*
2058 * Returns the global variable dictionary
2059 */
2060 dict_T *
2061get_globvar_dict(void)
2062{
2063 return &globvardict;
2064}
2065
2066/*
2067 * Returns the global variable hash table
2068 */
2069 hashtab_T *
2070get_globvar_ht(void)
2071{
2072 return &globvarht;
2073}
2074
2075/*
2076 * Returns the v: variable dictionary
2077 */
2078 dict_T *
2079get_vimvar_dict(void)
2080{
2081 return &vimvardict;
2082}
2083
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002084/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002086 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 */
2088 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002089find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002091 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2092 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093
2094 if (di == NULL)
2095 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002096 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002097 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2098 return (int)(vv - vimvars);
2099}
2100
2101
2102/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002103 * Set type of v: variable to "type".
2104 */
2105 void
2106set_vim_var_type(int idx, vartype_T type)
2107{
2108 vimvars[idx].vv_type = type;
2109}
2110
2111/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002112 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002113 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002114 */
2115 void
2116set_vim_var_nr(int idx, varnumber_T val)
2117{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002118 vimvars[idx].vv_nr = val;
2119}
2120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 char *
2122get_vim_var_name(int idx)
2123{
2124 return vimvars[idx].vv_name;
2125}
2126
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002127/*
2128 * Get typval_T v: variable value.
2129 */
2130 typval_T *
2131get_vim_var_tv(int idx)
2132{
2133 return &vimvars[idx].vv_tv;
2134}
2135
2136/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002137 * Set v: variable to "tv". Only accepts the same type.
2138 * Takes over the value of "tv".
2139 */
2140 int
2141set_vim_var_tv(int idx, typval_T *tv)
2142{
2143 if (vimvars[idx].vv_type != tv->v_type)
2144 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002145 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002146 clear_tv(tv);
2147 return FAIL;
2148 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002149 // VV_RO is also checked when compiling, but let's check here as well.
2150 if (vimvars[idx].vv_flags & VV_RO)
2151 {
2152 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2153 return FAIL;
2154 }
2155 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2156 {
2157 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2158 return FAIL;
2159 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002160 clear_tv(&vimvars[idx].vv_di.di_tv);
2161 vimvars[idx].vv_di.di_tv = *tv;
2162 return OK;
2163}
2164
2165/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002166 * Get number v: variable value.
2167 */
2168 varnumber_T
2169get_vim_var_nr(int idx)
2170{
2171 return vimvars[idx].vv_nr;
2172}
2173
2174/*
2175 * Get string v: variable value. Uses a static buffer, can only be used once.
2176 * If the String variable has never been set, return an empty string.
2177 * Never returns NULL;
2178 */
2179 char_u *
2180get_vim_var_str(int idx)
2181{
2182 return tv_get_string(&vimvars[idx].vv_tv);
2183}
2184
2185/*
2186 * Get List v: variable value. Caller must take care of reference count when
2187 * needed.
2188 */
2189 list_T *
2190get_vim_var_list(int idx)
2191{
2192 return vimvars[idx].vv_list;
2193}
2194
2195/*
2196 * Get Dict v: variable value. Caller must take care of reference count when
2197 * needed.
2198 */
2199 dict_T *
2200get_vim_var_dict(int idx)
2201{
2202 return vimvars[idx].vv_dict;
2203}
2204
2205/*
2206 * Set v:char to character "c".
2207 */
2208 void
2209set_vim_var_char(int c)
2210{
2211 char_u buf[MB_MAXBYTES + 1];
2212
2213 if (has_mbyte)
2214 buf[(*mb_char2bytes)(c, buf)] = NUL;
2215 else
2216 {
2217 buf[0] = c;
2218 buf[1] = NUL;
2219 }
2220 set_vim_var_string(VV_CHAR, buf, -1);
2221}
2222
2223/*
2224 * Set v:count to "count" and v:count1 to "count1".
2225 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2226 */
2227 void
2228set_vcount(
2229 long count,
2230 long count1,
2231 int set_prevcount)
2232{
2233 if (set_prevcount)
2234 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2235 vimvars[VV_COUNT].vv_nr = count;
2236 vimvars[VV_COUNT1].vv_nr = count1;
2237}
2238
2239/*
2240 * Save variables that might be changed as a side effect. Used when executing
2241 * a timer callback.
2242 */
2243 void
2244save_vimvars(vimvars_save_T *vvsave)
2245{
2246 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2247 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2248 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2249}
2250
2251/*
2252 * Restore variables saved by save_vimvars().
2253 */
2254 void
2255restore_vimvars(vimvars_save_T *vvsave)
2256{
2257 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2258 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2259 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2260}
2261
2262/*
2263 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2264 * value.
2265 */
2266 void
2267set_vim_var_string(
2268 int idx,
2269 char_u *val,
2270 int len) // length of "val" to use or -1 (whole string)
2271{
2272 clear_tv(&vimvars[idx].vv_di.di_tv);
2273 vimvars[idx].vv_type = VAR_STRING;
2274 if (val == NULL)
2275 vimvars[idx].vv_str = NULL;
2276 else if (len == -1)
2277 vimvars[idx].vv_str = vim_strsave(val);
2278 else
2279 vimvars[idx].vv_str = vim_strnsave(val, len);
2280}
2281
2282/*
2283 * Set List v: variable to "val".
2284 */
2285 void
2286set_vim_var_list(int idx, list_T *val)
2287{
2288 clear_tv(&vimvars[idx].vv_di.di_tv);
2289 vimvars[idx].vv_type = VAR_LIST;
2290 vimvars[idx].vv_list = val;
2291 if (val != NULL)
2292 ++val->lv_refcount;
2293}
2294
2295/*
2296 * Set Dictionary v: variable to "val".
2297 */
2298 void
2299set_vim_var_dict(int idx, dict_T *val)
2300{
2301 clear_tv(&vimvars[idx].vv_di.di_tv);
2302 vimvars[idx].vv_type = VAR_DICT;
2303 vimvars[idx].vv_dict = val;
2304 if (val != NULL)
2305 {
2306 ++val->dv_refcount;
2307 dict_set_items_ro(val);
2308 }
2309}
2310
2311/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002312 * Set the v:argv list.
2313 */
2314 void
2315set_argv_var(char **argv, int argc)
2316{
2317 list_T *l = list_alloc();
2318 int i;
2319
2320 if (l == NULL)
2321 getout(1);
2322 l->lv_lock = VAR_FIXED;
2323 for (i = 0; i < argc; ++i)
2324 {
2325 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2326 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002327 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002328 }
2329 set_vim_var_list(VV_ARGV, l);
2330}
2331
2332/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002333 * Reset v:register, taking the 'clipboard' setting into account.
2334 */
2335 void
2336reset_reg_var(void)
2337{
2338 int regname = 0;
2339
2340 // Adjust the register according to 'clipboard', so that when
2341 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2342#ifdef FEAT_CLIPBOARD
2343 adjust_clip_reg(&regname);
2344#endif
2345 set_reg_var(regname);
2346}
2347
2348/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002349 * Set v:register if needed.
2350 */
2351 void
2352set_reg_var(int c)
2353{
2354 char_u regname;
2355
2356 if (c == 0 || c == ' ')
2357 regname = '"';
2358 else
2359 regname = c;
2360 // Avoid free/alloc when the value is already right.
2361 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2362 set_vim_var_string(VV_REG, &regname, 1);
2363}
2364
2365/*
2366 * Get or set v:exception. If "oldval" == NULL, return the current value.
2367 * Otherwise, restore the value to "oldval" and return NULL.
2368 * Must always be called in pairs to save and restore v:exception! Does not
2369 * take care of memory allocations.
2370 */
2371 char_u *
2372v_exception(char_u *oldval)
2373{
2374 if (oldval == NULL)
2375 return vimvars[VV_EXCEPTION].vv_str;
2376
2377 vimvars[VV_EXCEPTION].vv_str = oldval;
2378 return NULL;
2379}
2380
2381/*
2382 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2383 * Otherwise, restore the value to "oldval" and return NULL.
2384 * Must always be called in pairs to save and restore v:throwpoint! Does not
2385 * take care of memory allocations.
2386 */
2387 char_u *
2388v_throwpoint(char_u *oldval)
2389{
2390 if (oldval == NULL)
2391 return vimvars[VV_THROWPOINT].vv_str;
2392
2393 vimvars[VV_THROWPOINT].vv_str = oldval;
2394 return NULL;
2395}
2396
2397/*
2398 * Set v:cmdarg.
2399 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2400 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2401 * Must always be called in pairs!
2402 */
2403 char_u *
2404set_cmdarg(exarg_T *eap, char_u *oldarg)
2405{
2406 char_u *oldval;
2407 char_u *newval;
2408 unsigned len;
2409
2410 oldval = vimvars[VV_CMDARG].vv_str;
2411 if (eap == NULL)
2412 {
2413 vim_free(oldval);
2414 vimvars[VV_CMDARG].vv_str = oldarg;
2415 return NULL;
2416 }
2417
2418 if (eap->force_bin == FORCE_BIN)
2419 len = 6;
2420 else if (eap->force_bin == FORCE_NOBIN)
2421 len = 8;
2422 else
2423 len = 0;
2424
2425 if (eap->read_edit)
2426 len += 7;
2427
2428 if (eap->force_ff != 0)
2429 len += 10; // " ++ff=unix"
2430 if (eap->force_enc != 0)
2431 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2432 if (eap->bad_char != 0)
2433 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2434
2435 newval = alloc(len + 1);
2436 if (newval == NULL)
2437 return NULL;
2438
2439 if (eap->force_bin == FORCE_BIN)
2440 sprintf((char *)newval, " ++bin");
2441 else if (eap->force_bin == FORCE_NOBIN)
2442 sprintf((char *)newval, " ++nobin");
2443 else
2444 *newval = NUL;
2445
2446 if (eap->read_edit)
2447 STRCAT(newval, " ++edit");
2448
2449 if (eap->force_ff != 0)
2450 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2451 eap->force_ff == 'u' ? "unix"
2452 : eap->force_ff == 'd' ? "dos"
2453 : "mac");
2454 if (eap->force_enc != 0)
2455 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2456 eap->cmd + eap->force_enc);
2457 if (eap->bad_char == BAD_KEEP)
2458 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2459 else if (eap->bad_char == BAD_DROP)
2460 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2461 else if (eap->bad_char != 0)
2462 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2463 vimvars[VV_CMDARG].vv_str = newval;
2464 return oldval;
2465}
2466
2467/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002468 * Get the value of internal variable "name".
2469 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2470 */
2471 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002472eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002473 char_u *name,
2474 int len, // length of "name"
2475 typval_T *rettv, // NULL when only checking existence
2476 dictitem_T **dip, // non-NULL when typval's dict item is needed
2477 int verbose, // may give error message
2478 int no_autoload) // do not use script autoloading
2479{
2480 int ret = OK;
2481 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002482 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002483 dictitem_T *v;
2484 int cc;
2485
2486 // truncate the name, so that we can use strcmp()
2487 cc = name[len];
2488 name[len] = NUL;
2489
2490 // Check for user-defined variables.
2491 v = find_var(name, NULL, no_autoload);
2492 if (v != NULL)
2493 {
2494 tv = &v->di_tv;
2495 if (dip != NULL)
2496 *dip = v;
2497 }
2498
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002499 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002501 imported_T *import;
2502 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2503
2504 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505
2506 // imported variable from another script
2507 if (import != NULL)
2508 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002509 if (import->imp_funcname != NULL)
2510 {
2511 foundFunc = TRUE;
2512 if (rettv != NULL)
2513 {
2514 rettv->v_type = VAR_FUNC;
2515 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2516 }
2517 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002518 else if (import->imp_all)
2519 {
2520 emsg("Sorry, 'import * as X' not implemented yet");
2521 ret = FAIL;
2522 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002523 else
2524 {
2525 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002526 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002528 tv = sv->sv_tv;
2529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002531 else if (in_vim9script())
2532 {
2533 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2534
2535 if (ufunc != NULL)
2536 {
2537 foundFunc = TRUE;
2538 if (rettv != NULL)
2539 {
2540 rettv->v_type = VAR_FUNC;
2541 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2542 }
2543 }
2544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 }
2546
Bram Moolenaarc620c052020-07-08 15:16:19 +02002547 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002548 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002549 if (tv == NULL)
2550 {
2551 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002552 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002553 ret = FAIL;
2554 }
2555 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002556 {
2557 // If a list or dict variable wasn't initialized, do it now.
2558 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2559 {
2560 tv->vval.v_dict = dict_alloc();
2561 if (tv->vval.v_dict != NULL)
2562 ++tv->vval.v_dict->dv_refcount;
2563 }
2564 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2565 {
2566 tv->vval.v_list = list_alloc();
2567 if (tv->vval.v_list != NULL)
2568 ++tv->vval.v_list->lv_refcount;
2569 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002570 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002571 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002572 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002573
2574 name[len] = cc;
2575
2576 return ret;
2577}
2578
2579/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002580 * Check if variable "name[len]" is a local variable or an argument.
2581 * If so, "*eval_lavars_used" is set to TRUE.
2582 */
2583 void
2584check_vars(char_u *name, int len)
2585{
2586 int cc;
2587 char_u *varname;
2588 hashtab_T *ht;
2589
2590 if (eval_lavars_used == NULL)
2591 return;
2592
2593 // truncate the name, so that we can use strcmp()
2594 cc = name[len];
2595 name[len] = NUL;
2596
2597 ht = find_var_ht(name, &varname);
2598 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2599 {
2600 if (find_var(name, NULL, TRUE) != NULL)
2601 *eval_lavars_used = TRUE;
2602 }
2603
2604 name[len] = cc;
2605}
2606
2607/*
2608 * Find variable "name" in the list of variables.
2609 * Return a pointer to it if found, NULL if not found.
2610 * Careful: "a:0" variables don't have a name.
2611 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2612 * hashtab_T used.
2613 */
2614 dictitem_T *
2615find_var(char_u *name, hashtab_T **htp, int no_autoload)
2616{
2617 char_u *varname;
2618 hashtab_T *ht;
2619 dictitem_T *ret = NULL;
2620
2621 ht = find_var_ht(name, &varname);
2622 if (htp != NULL)
2623 *htp = ht;
2624 if (ht == NULL)
2625 return NULL;
2626 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2627 if (ret != NULL)
2628 return ret;
2629
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002630 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002631 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2632}
2633
2634/*
2635 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002636 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002637 * Returns NULL if not found.
2638 */
2639 dictitem_T *
2640find_var_in_ht(
2641 hashtab_T *ht,
2642 int htname,
2643 char_u *varname,
2644 int no_autoload)
2645{
2646 hashitem_T *hi;
2647
2648 if (*varname == NUL)
2649 {
2650 // Must be something like "s:", otherwise "ht" would be NULL.
2651 switch (htname)
2652 {
2653 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2654 case 'g': return &globvars_var;
2655 case 'v': return &vimvars_var;
2656 case 'b': return &curbuf->b_bufvar;
2657 case 'w': return &curwin->w_winvar;
2658 case 't': return &curtab->tp_winvar;
2659 case 'l': return get_funccal_local_var();
2660 case 'a': return get_funccal_args_var();
2661 }
2662 return NULL;
2663 }
2664
2665 hi = hash_find(ht, varname);
2666 if (HASHITEM_EMPTY(hi))
2667 {
2668 // For global variables we may try auto-loading the script. If it
2669 // worked find the variable again. Don't auto-load a script if it was
2670 // loaded already, otherwise it would be loaded every time when
2671 // checking if a function name is a Funcref variable.
2672 if (ht == &globvarht && !no_autoload)
2673 {
2674 // Note: script_autoload() may make "hi" invalid. It must either
2675 // be obtained again or not used.
2676 if (!script_autoload(varname, FALSE) || aborting())
2677 return NULL;
2678 hi = hash_find(ht, varname);
2679 }
2680 if (HASHITEM_EMPTY(hi))
2681 return NULL;
2682 }
2683 return HI2DI(hi);
2684}
2685
2686/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 * Get the script-local hashtab. NULL if not in a script context.
2688 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002689 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690get_script_local_ht(void)
2691{
2692 scid_T sid = current_sctx.sc_sid;
2693
Bram Moolenaare3d46852020-08-29 13:39:17 +02002694 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 return &SCRIPT_VARS(sid);
2696 return NULL;
2697}
2698
2699/*
2700 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002701 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002703 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2705{
2706 hashtab_T *ht = get_script_local_ht();
2707 char_u buffer[30];
2708 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002709 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 hashitem_T *hi;
2711
2712 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002713 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 if (len < sizeof(buffer) - 1)
2715 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002716 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 vim_strncpy(buffer, name, len);
2718 p = buffer;
2719 }
2720 else
2721 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002722 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002724 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 }
2726
2727 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002728 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729
2730 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002731 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2732 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733
2734 if (p != buffer)
2735 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002736 // Don't return "buffer", gcc complains.
2737 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738}
2739
2740/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002741 * Find the hashtab used for a variable name.
2742 * Return NULL if the name is not valid.
2743 * Set "varname" to the start of name without ':'.
2744 */
2745 hashtab_T *
2746find_var_ht(char_u *name, char_u **varname)
2747{
2748 hashitem_T *hi;
2749 hashtab_T *ht;
2750
2751 if (name[0] == NUL)
2752 return NULL;
2753 if (name[1] != ':')
2754 {
2755 // The name must not start with a colon or #.
2756 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2757 return NULL;
2758 *varname = name;
2759
2760 // "version" is "v:version" in all scopes if scriptversion < 3.
2761 // Same for a few other variables marked with VV_COMPAT.
2762 if (current_sctx.sc_version < 3)
2763 {
2764 hi = hash_find(&compat_hashtab, name);
2765 if (!HASHITEM_EMPTY(hi))
2766 return &compat_hashtab;
2767 }
2768
2769 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 if (ht != NULL)
2771 return ht; // local variable
2772
2773 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002774 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 {
2776 ht = get_script_local_ht();
2777 if (ht != NULL)
2778 return ht;
2779 }
2780
2781 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002782 }
2783 *varname = name + 2;
2784 if (*name == 'g') // global variable
2785 return &globvarht;
2786 // There must be no ':' or '#' in the rest of the name, unless g: is used
2787 if (vim_strchr(name + 2, ':') != NULL
2788 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2789 return NULL;
2790 if (*name == 'b') // buffer variable
2791 return &curbuf->b_vars->dv_hashtab;
2792 if (*name == 'w') // window variable
2793 return &curwin->w_vars->dv_hashtab;
2794 if (*name == 't') // tab page variable
2795 return &curtab->tp_vars->dv_hashtab;
2796 if (*name == 'v') // v: variable
2797 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002798 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002799 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002801 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 if (*name == 'a') // a: function argument
2803 return get_funccal_args_ht();
2804 if (*name == 'l') // l: local function variable
2805 return get_funccal_local_ht();
2806 }
2807 if (*name == 's') // script variable
2808 {
2809 ht = get_script_local_ht();
2810 if (ht != NULL)
2811 return ht;
2812 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002813 return NULL;
2814}
2815
2816/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002817 * Get the string value of a (global/local) variable.
2818 * Note: see tv_get_string() for how long the pointer remains valid.
2819 * Returns NULL when it doesn't exist.
2820 */
2821 char_u *
2822get_var_value(char_u *name)
2823{
2824 dictitem_T *v;
2825
2826 v = find_var(name, NULL, FALSE);
2827 if (v == NULL)
2828 return NULL;
2829 return tv_get_string(&v->di_tv);
2830}
2831
2832/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002833 * Allocate a new hashtab for a sourced script. It will be used while
2834 * sourcing this script and when executing functions defined in the script.
2835 */
2836 void
2837new_script_vars(scid_T id)
2838{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002839 scriptvar_T *sv;
2840
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002841 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2842 if (sv == NULL)
2843 return;
2844 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002845 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002846}
2847
2848/*
2849 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2850 * point to it.
2851 */
2852 void
2853init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2854{
2855 hash_init(&dict->dv_hashtab);
2856 dict->dv_lock = 0;
2857 dict->dv_scope = scope;
2858 dict->dv_refcount = DO_NOT_FREE_CNT;
2859 dict->dv_copyID = 0;
2860 dict_var->di_tv.vval.v_dict = dict;
2861 dict_var->di_tv.v_type = VAR_DICT;
2862 dict_var->di_tv.v_lock = VAR_FIXED;
2863 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2864 dict_var->di_key[0] = NUL;
2865}
2866
2867/*
2868 * Unreference a dictionary initialized by init_var_dict().
2869 */
2870 void
2871unref_var_dict(dict_T *dict)
2872{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002873 // Now the dict needs to be freed if no one else is using it, go back to
2874 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002875 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2876 dict_unref(dict);
2877}
2878
2879/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002880 * Clean up a list of internal variables.
2881 * Frees all allocated variables and the value they contain.
2882 * Clears hashtab "ht", does not free it.
2883 */
2884 void
2885vars_clear(hashtab_T *ht)
2886{
2887 vars_clear_ext(ht, TRUE);
2888}
2889
2890/*
2891 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2892 */
2893 void
2894vars_clear_ext(hashtab_T *ht, int free_val)
2895{
2896 int todo;
2897 hashitem_T *hi;
2898 dictitem_T *v;
2899
2900 hash_lock(ht);
2901 todo = (int)ht->ht_used;
2902 for (hi = ht->ht_array; todo > 0; ++hi)
2903 {
2904 if (!HASHITEM_EMPTY(hi))
2905 {
2906 --todo;
2907
2908 // Free the variable. Don't remove it from the hashtab,
2909 // ht_array might change then. hash_clear() takes care of it
2910 // later.
2911 v = HI2DI(hi);
2912 if (free_val)
2913 clear_tv(&v->di_tv);
2914 if (v->di_flags & DI_FLAGS_ALLOC)
2915 vim_free(v);
2916 }
2917 }
2918 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002919 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002920}
2921
2922/*
2923 * Delete a variable from hashtab "ht" at item "hi".
2924 * Clear the variable value and free the dictitem.
2925 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002926 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002927delete_var(hashtab_T *ht, hashitem_T *hi)
2928{
2929 dictitem_T *di = HI2DI(hi);
2930
2931 hash_remove(ht, hi);
2932 clear_tv(&di->di_tv);
2933 vim_free(di);
2934}
2935
2936/*
2937 * List the value of one internal variable.
2938 */
2939 static void
2940list_one_var(dictitem_T *v, char *prefix, int *first)
2941{
2942 char_u *tofree;
2943 char_u *s;
2944 char_u numbuf[NUMBUFLEN];
2945
2946 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2947 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2948 s == NULL ? (char_u *)"" : s, first);
2949 vim_free(tofree);
2950}
2951
2952 static void
2953list_one_var_a(
2954 char *prefix,
2955 char_u *name,
2956 int type,
2957 char_u *string,
2958 int *first) // when TRUE clear rest of screen and set to FALSE
2959{
2960 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2961 msg_start();
2962 msg_puts(prefix);
2963 if (name != NULL) // "a:" vars don't have a name stored
2964 msg_puts((char *)name);
2965 msg_putchar(' ');
2966 msg_advance(22);
2967 if (type == VAR_NUMBER)
2968 msg_putchar('#');
2969 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2970 msg_putchar('*');
2971 else if (type == VAR_LIST)
2972 {
2973 msg_putchar('[');
2974 if (*string == '[')
2975 ++string;
2976 }
2977 else if (type == VAR_DICT)
2978 {
2979 msg_putchar('{');
2980 if (*string == '{')
2981 ++string;
2982 }
2983 else
2984 msg_putchar(' ');
2985
2986 msg_outtrans(string);
2987
2988 if (type == VAR_FUNC || type == VAR_PARTIAL)
2989 msg_puts("()");
2990 if (*first)
2991 {
2992 msg_clr_eos();
2993 *first = FALSE;
2994 }
2995}
2996
2997/*
2998 * Set variable "name" to value in "tv".
2999 * If the variable already exists, the value is updated.
3000 * Otherwise the variable is created.
3001 */
3002 void
3003set_var(
3004 char_u *name,
3005 typval_T *tv,
3006 int copy) // make copy of value in "tv"
3007{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003008 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003009}
3010
3011/*
3012 * Set variable "name" to value in "tv".
3013 * If the variable already exists and "is_const" is FALSE the value is updated.
3014 * Otherwise the variable is created.
3015 */
3016 void
3017set_var_const(
3018 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003020 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003021 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003022 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003023{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003024 typval_T *tv = tv_arg;
3025 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003027 char_u *varname;
3028 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003030 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003031
3032 ht = find_var_ht(name, &varname);
3033 if (ht == NULL || *varname == NUL)
3034 {
3035 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003036 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003037 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003038 is_script_local = ht == get_script_local_ht();
3039
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003040 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003041 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003042 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003043 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003044 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003045 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003046 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003047 }
3048
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003050
3051 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 if (di == NULL)
3053 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003054
3055 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003056 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003057 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003058
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003059 if (need_convert_to_bool(type, tv))
3060 {
3061 // Destination is a bool and the value is not, but it can be converted.
3062 CLEAR_FIELD(bool_tv);
3063 bool_tv.v_type = VAR_BOOL;
3064 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3065 tv = &bool_tv;
3066 }
3067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003069 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003071 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003072 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 {
3074 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003075 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 }
3077
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003078 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003080 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003081 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003082 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003083 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003084 }
3085
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003086 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003087 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003088 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003090
Bram Moolenaara187c432020-09-16 21:08:28 +02003091 // Check in this order for backwards compatibility:
3092 // - Whether the variable is read-only
3093 // - Whether the variable value is locked
3094 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003095 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003096 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3097 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003098 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 else
3101 // can only redefine once
3102 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103
3104 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003107 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003108 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003109 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003111 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003113 if (copy || tv->v_type != VAR_STRING)
3114 {
3115 char_u *val = tv_get_string(tv);
3116
3117 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003118 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 if (di->di_tv.vval.v_string == NULL)
3120 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003121 }
3122 else
3123 {
3124 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003126 tv->vval.v_string = NULL;
3127 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003128 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003131 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003133 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003135#ifdef FEAT_SEARCH_EXTRA
3136 else if (STRCMP(varname, "hlsearch") == 0)
3137 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003139 redraw_all_later(SOME_VALID);
3140 }
3141#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003142 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003145 {
3146 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003147 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003148 }
3149 }
3150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003151 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003152 }
3153 else // add a new variable
3154 {
3155 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003156 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003157 {
3158 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003159 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003160 }
3161
3162 // Make sure the variable name is valid.
3163 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003164 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003165
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3167 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003168 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 STRCPY(di->di_key, varname);
3170 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003173 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003176 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 di->di_flags |= DI_FLAGS_LOCK;
3178
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003179 // A Vim9 script-local variable is also added to sn_all_vars and
3180 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003181 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003182 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003183 }
3184
3185 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003187 else
3188 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 di->di_tv = *tv;
3190 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191 init_tv(tv);
3192 }
3193
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003194 // ":const var = val" locks the value
3195 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003196 // Like :lockvar! name: lock the value and what it contains, but only
3197 // if the reference count is up to one. That locks only literal
3198 // values.
3199 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003200 return;
3201failed:
3202 if (!copy)
3203 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003204}
3205
3206/*
3207 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3208 * Also give an error message.
3209 */
3210 int
3211var_check_ro(int flags, char_u *name, int use_gettext)
3212{
3213 if (flags & DI_FLAGS_RO)
3214 {
3215 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3216 return TRUE;
3217 }
3218 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3219 {
3220 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3221 return TRUE;
3222 }
3223 return FALSE;
3224}
3225
3226/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003227 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3228 * Also give an error message.
3229 */
3230 int
3231var_check_lock(int flags, char_u *name, int use_gettext)
3232{
3233 if (flags & DI_FLAGS_LOCK)
3234 {
3235 semsg(_(e_variable_is_locked_str),
3236 use_gettext ? (char_u *)_(name) : name);
3237 return TRUE;
3238 }
3239 return FALSE;
3240}
3241
3242/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003243 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3244 * Also give an error message.
3245 */
3246 int
3247var_check_fixed(int flags, char_u *name, int use_gettext)
3248{
3249 if (flags & DI_FLAGS_FIX)
3250 {
3251 semsg(_("E795: Cannot delete variable %s"),
3252 use_gettext ? (char_u *)_(name) : name);
3253 return TRUE;
3254 }
3255 return FALSE;
3256}
3257
3258/*
3259 * Check if a funcref is assigned to a valid variable name.
3260 * Return TRUE and give an error if not.
3261 */
3262 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003263var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003264 char_u *name, // points to start of variable name
3265 int new_var) // TRUE when creating the variable
3266{
3267 // Allow for w: b: s: and t:.
3268 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3269 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3270 ? name[2] : name[0]))
3271 {
3272 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3273 name);
3274 return TRUE;
3275 }
3276 // Don't allow hiding a function. When "v" is not NULL we might be
3277 // assigning another function to the same var, the type is checked
3278 // below.
3279 if (new_var && function_exists(name, FALSE))
3280 {
3281 semsg(_("E705: Variable name conflicts with existing function: %s"),
3282 name);
3283 return TRUE;
3284 }
3285 return FALSE;
3286}
3287
3288/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003289 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3290 * value. Also give an error message, using "name" or _("name") when
3291 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003292 */
3293 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003294value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003295{
3296 if (lock & VAR_LOCKED)
3297 {
3298 semsg(_("E741: Value is locked: %s"),
3299 name == NULL ? (char_u *)_("Unknown")
3300 : use_gettext ? (char_u *)_(name)
3301 : name);
3302 return TRUE;
3303 }
3304 if (lock & VAR_FIXED)
3305 {
3306 semsg(_("E742: Cannot change value of %s"),
3307 name == NULL ? (char_u *)_("Unknown")
3308 : use_gettext ? (char_u *)_(name)
3309 : name);
3310 return TRUE;
3311 }
3312 return FALSE;
3313}
3314
3315/*
3316 * Check if a variable name is valid.
3317 * Return FALSE and give an error if not.
3318 */
3319 int
3320valid_varname(char_u *varname)
3321{
3322 char_u *p;
3323
3324 for (p = varname; *p != NUL; ++p)
3325 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3326 && *p != AUTOLOAD_CHAR)
3327 {
3328 semsg(_(e_illvar), varname);
3329 return FALSE;
3330 }
3331 return TRUE;
3332}
3333
3334/*
3335 * getwinvar() and gettabwinvar()
3336 */
3337 static void
3338getwinvar(
3339 typval_T *argvars,
3340 typval_T *rettv,
3341 int off) // 1 for gettabwinvar()
3342{
3343 win_T *win;
3344 char_u *varname;
3345 dictitem_T *v;
3346 tabpage_T *tp = NULL;
3347 int done = FALSE;
3348 win_T *oldcurwin;
3349 tabpage_T *oldtabpage;
3350 int need_switch_win;
3351
3352 if (off == 1)
3353 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3354 else
3355 tp = curtab;
3356 win = find_win_by_nr(&argvars[off], tp);
3357 varname = tv_get_string_chk(&argvars[off + 1]);
3358 ++emsg_off;
3359
3360 rettv->v_type = VAR_STRING;
3361 rettv->vval.v_string = NULL;
3362
3363 if (win != NULL && varname != NULL)
3364 {
3365 // Set curwin to be our win, temporarily. Also set the tabpage,
3366 // otherwise the window is not valid. Only do this when needed,
3367 // autocommands get blocked.
3368 need_switch_win = !(tp == curtab && win == curwin);
3369 if (!need_switch_win
3370 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3371 {
3372 if (*varname == '&')
3373 {
3374 if (varname[1] == NUL)
3375 {
3376 // get all window-local options in a dict
3377 dict_T *opts = get_winbuf_options(FALSE);
3378
3379 if (opts != NULL)
3380 {
3381 rettv_dict_set(rettv, opts);
3382 done = TRUE;
3383 }
3384 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003385 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003386 // window-local-option
3387 done = TRUE;
3388 }
3389 else
3390 {
3391 // Look up the variable.
3392 // Let getwinvar({nr}, "") return the "w:" dictionary.
3393 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3394 varname, FALSE);
3395 if (v != NULL)
3396 {
3397 copy_tv(&v->di_tv, rettv);
3398 done = TRUE;
3399 }
3400 }
3401 }
3402
3403 if (need_switch_win)
3404 // restore previous notion of curwin
3405 restore_win(oldcurwin, oldtabpage, TRUE);
3406 }
3407
3408 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3409 // use the default return value
3410 copy_tv(&argvars[off + 2], rettv);
3411
3412 --emsg_off;
3413}
3414
3415/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003416 * Set option "varname" to the value of "varp" for the current buffer/window.
3417 */
3418 static void
3419set_option_from_tv(char_u *varname, typval_T *varp)
3420{
3421 long numval = 0;
3422 char_u *strval;
3423 char_u nbuf[NUMBUFLEN];
3424 int error = FALSE;
3425
3426 if (!in_vim9script() || varp->v_type != VAR_STRING)
3427 numval = (long)tv_get_number_chk(varp, &error);
3428 strval = tv_get_string_buf_chk(varp, nbuf);
3429 if (!error && strval != NULL)
3430 set_option_value(varname, numval, strval, OPT_LOCAL);
3431}
3432
3433/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003434 * "setwinvar()" and "settabwinvar()" functions
3435 */
3436 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003437setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003438{
3439 win_T *win;
3440 win_T *save_curwin;
3441 tabpage_T *save_curtab;
3442 int need_switch_win;
3443 char_u *varname, *winvarname;
3444 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003445 tabpage_T *tp = NULL;
3446
3447 if (check_secure())
3448 return;
3449
3450 if (off == 1)
3451 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3452 else
3453 tp = curtab;
3454 win = find_win_by_nr(&argvars[off], tp);
3455 varname = tv_get_string_chk(&argvars[off + 1]);
3456 varp = &argvars[off + 2];
3457
3458 if (win != NULL && varname != NULL && varp != NULL)
3459 {
3460 need_switch_win = !(tp == curtab && win == curwin);
3461 if (!need_switch_win
3462 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3463 {
3464 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003465 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003466 else
3467 {
3468 winvarname = alloc(STRLEN(varname) + 3);
3469 if (winvarname != NULL)
3470 {
3471 STRCPY(winvarname, "w:");
3472 STRCPY(winvarname + 2, varname);
3473 set_var(winvarname, varp, TRUE);
3474 vim_free(winvarname);
3475 }
3476 }
3477 }
3478 if (need_switch_win)
3479 restore_win(save_curwin, save_curtab, TRUE);
3480 }
3481}
3482
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003483/*
3484 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3485 * v:option_type, and v:option_command.
3486 */
3487 void
3488reset_v_option_vars(void)
3489{
3490 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3491 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3492 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3493 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3494 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3495 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3496}
3497
3498/*
3499 * Add an assert error to v:errors.
3500 */
3501 void
3502assert_error(garray_T *gap)
3503{
3504 struct vimvar *vp = &vimvars[VV_ERRORS];
3505
3506 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003507 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003508 set_vim_var_list(VV_ERRORS, list_alloc());
3509 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3510}
3511
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003512 int
3513var_exists(char_u *var)
3514{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003515 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003516 char_u *name;
3517 char_u *tofree;
3518 typval_T tv;
3519 int len = 0;
3520 int n = FALSE;
3521
3522 // get_name_len() takes care of expanding curly braces
3523 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003524 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003525 if (len > 0)
3526 {
3527 if (tofree != NULL)
3528 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003529 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003530 if (n)
3531 {
3532 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003533 arg = skipwhite(arg);
3534 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003535 if (n)
3536 clear_tv(&tv);
3537 }
3538 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003539 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003540 n = FALSE;
3541
3542 vim_free(tofree);
3543 return n;
3544}
3545
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003546static lval_T *redir_lval = NULL;
3547#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3548static garray_T redir_ga; // only valid when redir_lval is not NULL
3549static char_u *redir_endp = NULL;
3550static char_u *redir_varname = NULL;
3551
3552/*
3553 * Start recording command output to a variable
3554 * When "append" is TRUE append to an existing variable.
3555 * Returns OK if successfully completed the setup. FAIL otherwise.
3556 */
3557 int
3558var_redir_start(char_u *name, int append)
3559{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003560 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003561 typval_T tv;
3562
3563 // Catch a bad name early.
3564 if (!eval_isnamec1(*name))
3565 {
3566 emsg(_(e_invarg));
3567 return FAIL;
3568 }
3569
3570 // Make a copy of the name, it is used in redir_lval until redir ends.
3571 redir_varname = vim_strsave(name);
3572 if (redir_varname == NULL)
3573 return FAIL;
3574
3575 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3576 if (redir_lval == NULL)
3577 {
3578 var_redir_stop();
3579 return FAIL;
3580 }
3581
3582 // The output is stored in growarray "redir_ga" until redirection ends.
3583 ga_init2(&redir_ga, (int)sizeof(char), 500);
3584
3585 // Parse the variable name (can be a dict or list entry).
3586 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3587 FNE_CHECK_START);
3588 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3589 {
3590 clear_lval(redir_lval);
3591 if (redir_endp != NULL && *redir_endp != NUL)
3592 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003593 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003594 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003595 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003596 redir_endp = NULL; // don't store a value, only cleanup
3597 var_redir_stop();
3598 return FAIL;
3599 }
3600
3601 // check if we can write to the variable: set it to or append an empty
3602 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003603 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003604 tv.v_type = VAR_STRING;
3605 tv.vval.v_string = (char_u *)"";
3606 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003607 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3608 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003609 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003610 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3611 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003612 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003613 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003614 {
3615 redir_endp = NULL; // don't store a value, only cleanup
3616 var_redir_stop();
3617 return FAIL;
3618 }
3619
3620 return OK;
3621}
3622
3623/*
3624 * Append "value[value_len]" to the variable set by var_redir_start().
3625 * The actual appending is postponed until redirection ends, because the value
3626 * appended may in fact be the string we write to, changing it may cause freed
3627 * memory to be used:
3628 * :redir => foo
3629 * :let foo
3630 * :redir END
3631 */
3632 void
3633var_redir_str(char_u *value, int value_len)
3634{
3635 int len;
3636
3637 if (redir_lval == NULL)
3638 return;
3639
3640 if (value_len == -1)
3641 len = (int)STRLEN(value); // Append the entire string
3642 else
3643 len = value_len; // Append only "value_len" characters
3644
3645 if (ga_grow(&redir_ga, len) == OK)
3646 {
3647 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3648 redir_ga.ga_len += len;
3649 }
3650 else
3651 var_redir_stop();
3652}
3653
3654/*
3655 * Stop redirecting command output to a variable.
3656 * Frees the allocated memory.
3657 */
3658 void
3659var_redir_stop(void)
3660{
3661 typval_T tv;
3662
3663 if (EVALCMD_BUSY)
3664 {
3665 redir_lval = NULL;
3666 return;
3667 }
3668
3669 if (redir_lval != NULL)
3670 {
3671 // If there was no error: assign the text to the variable.
3672 if (redir_endp != NULL)
3673 {
3674 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3675 tv.v_type = VAR_STRING;
3676 tv.vval.v_string = redir_ga.ga_data;
3677 // Call get_lval() again, if it's inside a Dict or List it may
3678 // have changed.
3679 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3680 FALSE, FALSE, 0, FNE_CHECK_START);
3681 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003683 (char_u *)".");
3684 clear_lval(redir_lval);
3685 }
3686
3687 // free the collected output
3688 VIM_CLEAR(redir_ga.ga_data);
3689
3690 VIM_CLEAR(redir_lval);
3691 }
3692 VIM_CLEAR(redir_varname);
3693}
3694
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003695/*
3696 * "gettabvar()" function
3697 */
3698 void
3699f_gettabvar(typval_T *argvars, typval_T *rettv)
3700{
3701 win_T *oldcurwin;
3702 tabpage_T *tp, *oldtabpage;
3703 dictitem_T *v;
3704 char_u *varname;
3705 int done = FALSE;
3706
3707 rettv->v_type = VAR_STRING;
3708 rettv->vval.v_string = NULL;
3709
3710 varname = tv_get_string_chk(&argvars[1]);
3711 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3712 if (tp != NULL && varname != NULL)
3713 {
3714 // Set tp to be our tabpage, temporarily. Also set the window to the
3715 // first window in the tabpage, otherwise the window is not valid.
3716 if (switch_win(&oldcurwin, &oldtabpage,
3717 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3718 : tp->tp_firstwin, tp, TRUE) == OK)
3719 {
3720 // look up the variable
3721 // Let gettabvar({nr}, "") return the "t:" dictionary.
3722 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3723 if (v != NULL)
3724 {
3725 copy_tv(&v->di_tv, rettv);
3726 done = TRUE;
3727 }
3728 }
3729
3730 // restore previous notion of curwin
3731 restore_win(oldcurwin, oldtabpage, TRUE);
3732 }
3733
3734 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3735 copy_tv(&argvars[2], rettv);
3736}
3737
3738/*
3739 * "gettabwinvar()" function
3740 */
3741 void
3742f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3743{
3744 getwinvar(argvars, rettv, 1);
3745}
3746
3747/*
3748 * "getwinvar()" function
3749 */
3750 void
3751f_getwinvar(typval_T *argvars, typval_T *rettv)
3752{
3753 getwinvar(argvars, rettv, 0);
3754}
3755
3756/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003757 * "getbufvar()" function
3758 */
3759 void
3760f_getbufvar(typval_T *argvars, typval_T *rettv)
3761{
3762 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003763 char_u *varname;
3764 dictitem_T *v;
3765 int done = FALSE;
3766
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003767 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003768 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003769
3770 rettv->v_type = VAR_STRING;
3771 rettv->vval.v_string = NULL;
3772
3773 if (buf != NULL && varname != NULL)
3774 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003775 if (*varname == '&')
3776 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003777 buf_T *save_curbuf = curbuf;
3778
3779 // set curbuf to be our buf, temporarily
3780 curbuf = buf;
3781
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003782 if (varname[1] == NUL)
3783 {
3784 // get all buffer-local options in a dict
3785 dict_T *opts = get_winbuf_options(TRUE);
3786
3787 if (opts != NULL)
3788 {
3789 rettv_dict_set(rettv, opts);
3790 done = TRUE;
3791 }
3792 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003793 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003794 // buffer-local-option
3795 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003796
3797 // restore previous notion of curbuf
3798 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003799 }
3800 else
3801 {
3802 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003803 if (*varname == NUL)
3804 // Let getbufvar({nr}, "") return the "b:" dictionary.
3805 v = &buf->b_bufvar;
3806 else
3807 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3808 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003809 if (v != NULL)
3810 {
3811 copy_tv(&v->di_tv, rettv);
3812 done = TRUE;
3813 }
3814 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003815 }
3816
3817 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3818 // use the default value
3819 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003820}
3821
3822/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003823 * "settabvar()" function
3824 */
3825 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003826f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003827{
3828 tabpage_T *save_curtab;
3829 tabpage_T *tp;
3830 char_u *varname, *tabvarname;
3831 typval_T *varp;
3832
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003833 if (check_secure())
3834 return;
3835
3836 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3837 varname = tv_get_string_chk(&argvars[1]);
3838 varp = &argvars[2];
3839
3840 if (varname != NULL && varp != NULL && tp != NULL)
3841 {
3842 save_curtab = curtab;
3843 goto_tabpage_tp(tp, FALSE, FALSE);
3844
3845 tabvarname = alloc(STRLEN(varname) + 3);
3846 if (tabvarname != NULL)
3847 {
3848 STRCPY(tabvarname, "t:");
3849 STRCPY(tabvarname + 2, varname);
3850 set_var(tabvarname, varp, TRUE);
3851 vim_free(tabvarname);
3852 }
3853
3854 // Restore current tabpage
3855 if (valid_tabpage(save_curtab))
3856 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3857 }
3858}
3859
3860/*
3861 * "settabwinvar()" function
3862 */
3863 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003864f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003865{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003866 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003867}
3868
3869/*
3870 * "setwinvar()" function
3871 */
3872 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003873f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003874{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003875 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003876}
3877
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003878/*
3879 * "setbufvar()" function
3880 */
3881 void
3882f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3883{
3884 buf_T *buf;
3885 char_u *varname, *bufvarname;
3886 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003887
3888 if (check_secure())
3889 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003890 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003891 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003892 varp = &argvars[2];
3893
3894 if (buf != NULL && varname != NULL && varp != NULL)
3895 {
3896 if (*varname == '&')
3897 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003898 aco_save_T aco;
3899
3900 // set curbuf to be our buf, temporarily
3901 aucmd_prepbuf(&aco, buf);
3902
Bram Moolenaar191929b2020-08-19 21:20:49 +02003903 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003904
3905 // reset notion of buffer
3906 aucmd_restbuf(&aco);
3907 }
3908 else
3909 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003910 bufvarname = alloc(STRLEN(varname) + 3);
3911 if (bufvarname != NULL)
3912 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003913 buf_T *save_curbuf = curbuf;
3914
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003915 curbuf = buf;
3916 STRCPY(bufvarname, "b:");
3917 STRCPY(bufvarname + 2, varname);
3918 set_var(bufvarname, varp, TRUE);
3919 vim_free(bufvarname);
3920 curbuf = save_curbuf;
3921 }
3922 }
3923 }
3924}
3925
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003926/*
3927 * Get a callback from "arg". It can be a Funcref or a function name.
3928 * When "arg" is zero return an empty string.
3929 * "cb_name" is not allocated.
3930 * "cb_name" is set to NULL for an invalid argument.
3931 */
3932 callback_T
3933get_callback(typval_T *arg)
3934{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003935 callback_T res;
3936 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003937
3938 res.cb_free_name = FALSE;
3939 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3940 {
3941 res.cb_partial = arg->vval.v_partial;
3942 ++res.cb_partial->pt_refcount;
3943 res.cb_name = partial_name(res.cb_partial);
3944 }
3945 else
3946 {
3947 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003948 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3949 && isdigit(*arg->vval.v_string))
3950 r = FAIL;
3951 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003952 {
3953 // Note that we don't make a copy of the string.
3954 res.cb_name = arg->vval.v_string;
3955 func_ref(res.cb_name);
3956 }
3957 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003958 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003959 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003960 r = FAIL;
3961
3962 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003963 {
3964 emsg(_("E921: Invalid callback argument"));
3965 res.cb_name = NULL;
3966 }
3967 }
3968 return res;
3969}
3970
3971/*
3972 * Copy a callback into a typval_T.
3973 */
3974 void
3975put_callback(callback_T *cb, typval_T *tv)
3976{
3977 if (cb->cb_partial != NULL)
3978 {
3979 tv->v_type = VAR_PARTIAL;
3980 tv->vval.v_partial = cb->cb_partial;
3981 ++tv->vval.v_partial->pt_refcount;
3982 }
3983 else
3984 {
3985 tv->v_type = VAR_FUNC;
3986 tv->vval.v_string = vim_strsave(cb->cb_name);
3987 func_ref(cb->cb_name);
3988 }
3989}
3990
3991/*
3992 * Make a copy of "src" into "dest", allocating the function name if needed,
3993 * without incrementing the refcount.
3994 */
3995 void
3996set_callback(callback_T *dest, callback_T *src)
3997{
3998 if (src->cb_partial == NULL)
3999 {
4000 // just a function name, make a copy
4001 dest->cb_name = vim_strsave(src->cb_name);
4002 dest->cb_free_name = TRUE;
4003 }
4004 else
4005 {
4006 // cb_name is a pointer into cb_partial
4007 dest->cb_name = src->cb_name;
4008 dest->cb_free_name = FALSE;
4009 }
4010 dest->cb_partial = src->cb_partial;
4011}
4012
4013/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004014 * Copy callback from "src" to "dest", incrementing the refcounts.
4015 */
4016 void
4017copy_callback(callback_T *dest, callback_T *src)
4018{
4019 dest->cb_partial = src->cb_partial;
4020 if (dest->cb_partial != NULL)
4021 {
4022 dest->cb_name = src->cb_name;
4023 dest->cb_free_name = FALSE;
4024 ++dest->cb_partial->pt_refcount;
4025 }
4026 else
4027 {
4028 dest->cb_name = vim_strsave(src->cb_name);
4029 dest->cb_free_name = TRUE;
4030 func_ref(src->cb_name);
4031 }
4032}
4033
4034/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004035 * Unref/free "callback" returned by get_callback() or set_callback().
4036 */
4037 void
4038free_callback(callback_T *callback)
4039{
4040 if (callback->cb_partial != NULL)
4041 {
4042 partial_unref(callback->cb_partial);
4043 callback->cb_partial = NULL;
4044 }
4045 else if (callback->cb_name != NULL)
4046 func_unref(callback->cb_name);
4047 if (callback->cb_free_name)
4048 {
4049 vim_free(callback->cb_name);
4050 callback->cb_free_name = FALSE;
4051 }
4052 callback->cb_name = NULL;
4053}
4054
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004055#endif // FEAT_EVAL