blob: f7ee58d531d64b3a24f00e1f2f842d95988d2366 [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 Moolenaarcfcd0112020-09-27 15:19:27 +0200149 {VV_NAME("disallow_let", VAR_NUMBER), 0}, // TODO: remove
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200150};
151
152// shorthand
153#define vv_type vv_di.di_tv.v_type
154#define vv_nr vv_di.di_tv.vval.v_number
155#define vv_float vv_di.di_tv.vval.v_float
156#define vv_str vv_di.di_tv.vval.v_string
157#define vv_list vv_di.di_tv.vval.v_list
158#define vv_dict vv_di.di_tv.vval.v_dict
159#define vv_blob vv_di.di_tv.vval.v_blob
160#define vv_tv vv_di.di_tv
161
162static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200163static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vimvarht vimvardict.dv_hashtab
165
166// for VIM_VERSION_ defines
167#include "version.h"
168
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static 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 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void list_one_var(dictitem_T *v, char *prefix, int *first);
179static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
180
181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200182 * Initialize global and vim special variables
183 */
184 void
185evalvars_init(void)
186{
187 int i;
188 struct vimvar *p;
189
190 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
191 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
192 vimvardict.dv_lock = VAR_FIXED;
193 hash_init(&compat_hashtab);
194
195 for (i = 0; i < VV_LEN; ++i)
196 {
197 p = &vimvars[i];
198 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
199 {
200 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
201 getout(1);
202 }
203 STRCPY(p->vv_di.di_key, p->vv_name);
204 if (p->vv_flags & VV_RO)
205 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
206 else if (p->vv_flags & VV_RO_SBX)
207 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
208 else
209 p->vv_di.di_flags = DI_FLAGS_FIX;
210
211 // add to v: scope dict, unless the value is not always available
212 if (p->vv_type != VAR_UNKNOWN)
213 hash_add(&vimvarht, p->vv_di.di_key);
214 if (p->vv_flags & VV_COMPAT)
215 // add to compat scope dict
216 hash_add(&compat_hashtab, p->vv_di.di_key);
217 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200218 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
219 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220
221 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
222 set_vim_var_nr(VV_HLSEARCH, 1L);
223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200247 // TODO: remove later
248 set_vim_var_nr(VV_DISALLOW_LET, 1);
249
Bram Moolenaar439c0362020-06-06 15:58:03 +0200250 // Default for v:register is not 0 but '"'. This is adjusted once the
251 // clipboard has been setup by calling reset_reg_var().
252 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200253}
254
255#if defined(EXITFREE) || defined(PROTO)
256/*
257 * Free all vim variables information on exit
258 */
259 void
260evalvars_clear(void)
261{
262 int i;
263 struct vimvar *p;
264
265 for (i = 0; i < VV_LEN; ++i)
266 {
267 p = &vimvars[i];
268 if (p->vv_di.di_tv.v_type == VAR_STRING)
269 VIM_CLEAR(p->vv_str);
270 else if (p->vv_di.di_tv.v_type == VAR_LIST)
271 {
272 list_unref(p->vv_list);
273 p->vv_list = NULL;
274 }
275 }
276 hash_clear(&vimvarht);
277 hash_init(&vimvarht); // garbage_collect() will access it
278 hash_clear(&compat_hashtab);
279
280 // global variables
281 vars_clear(&globvarht);
282
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100283 // Script-local variables. Clear all the variables here.
284 // The scriptvar_T is cleared later in free_scriptnames(), because a
285 // variable in one script might hold a reference to the whole scope of
286 // another script.
287 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200288 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200289}
290#endif
291
292 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200293garbage_collect_globvars(int copyID)
294{
295 return set_ref_in_ht(&globvarht, copyID, NULL);
296}
297
298 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200299garbage_collect_vimvars(int copyID)
300{
301 return set_ref_in_ht(&vimvarht, copyID, NULL);
302}
303
304 int
305garbage_collect_scriptvars(int copyID)
306{
307 int i;
308 int abort = FALSE;
309
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100310 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200311 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
312
313 return abort;
314}
315
316/*
317 * Set an internal variable to a string value. Creates the variable if it does
318 * not already exist.
319 */
320 void
321set_internal_string_var(char_u *name, char_u *value)
322{
323 char_u *val;
324 typval_T *tvp;
325
326 val = vim_strsave(value);
327 if (val != NULL)
328 {
329 tvp = alloc_string_tv(val);
330 if (tvp != NULL)
331 {
332 set_var(name, tvp, FALSE);
333 free_tv(tvp);
334 }
335 }
336}
337
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200338 int
339eval_charconvert(
340 char_u *enc_from,
341 char_u *enc_to,
342 char_u *fname_from,
343 char_u *fname_to)
344{
345 int err = FALSE;
346
347 set_vim_var_string(VV_CC_FROM, enc_from, -1);
348 set_vim_var_string(VV_CC_TO, enc_to, -1);
349 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
350 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
351 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
352 err = TRUE;
353 set_vim_var_string(VV_CC_FROM, NULL, -1);
354 set_vim_var_string(VV_CC_TO, NULL, -1);
355 set_vim_var_string(VV_FNAME_IN, NULL, -1);
356 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
357
358 if (err)
359 return FAIL;
360 return OK;
361}
362
363# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
364 int
365eval_printexpr(char_u *fname, char_u *args)
366{
367 int err = FALSE;
368
369 set_vim_var_string(VV_FNAME_IN, fname, -1);
370 set_vim_var_string(VV_CMDARG, args, -1);
371 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
372 err = TRUE;
373 set_vim_var_string(VV_FNAME_IN, NULL, -1);
374 set_vim_var_string(VV_CMDARG, NULL, -1);
375
376 if (err)
377 {
378 mch_remove(fname);
379 return FAIL;
380 }
381 return OK;
382}
383# endif
384
385# if defined(FEAT_DIFF) || defined(PROTO)
386 void
387eval_diff(
388 char_u *origfile,
389 char_u *newfile,
390 char_u *outfile)
391{
392 int err = FALSE;
393
394 set_vim_var_string(VV_FNAME_IN, origfile, -1);
395 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
396 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
397 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
398 set_vim_var_string(VV_FNAME_IN, NULL, -1);
399 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
400 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
401}
402
403 void
404eval_patch(
405 char_u *origfile,
406 char_u *difffile,
407 char_u *outfile)
408{
409 int err;
410
411 set_vim_var_string(VV_FNAME_IN, origfile, -1);
412 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
413 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
414 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
415 set_vim_var_string(VV_FNAME_IN, NULL, -1);
416 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
417 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
418}
419# endif
420
421#if defined(FEAT_SPELL) || defined(PROTO)
422/*
423 * Evaluate an expression to a list with suggestions.
424 * For the "expr:" part of 'spellsuggest'.
425 * Returns NULL when there is an error.
426 */
427 list_T *
428eval_spell_expr(char_u *badword, char_u *expr)
429{
430 typval_T save_val;
431 typval_T rettv;
432 list_T *list = NULL;
433 char_u *p = skipwhite(expr);
434
435 // Set "v:val" to the bad word.
436 prepare_vimvar(VV_VAL, &save_val);
437 set_vim_var_string(VV_VAL, badword, -1);
438 if (p_verbose == 0)
439 ++emsg_off;
440
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200441 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200442 {
443 if (rettv.v_type != VAR_LIST)
444 clear_tv(&rettv);
445 else
446 list = rettv.vval.v_list;
447 }
448
449 if (p_verbose == 0)
450 --emsg_off;
451 clear_tv(get_vim_var_tv(VV_VAL));
452 restore_vimvar(VV_VAL, &save_val);
453
454 return list;
455}
456
457/*
458 * "list" is supposed to contain two items: a word and a number. Return the
459 * word in "pp" and the number as the return value.
460 * Return -1 if anything isn't right.
461 * Used to get the good word and score from the eval_spell_expr() result.
462 */
463 int
464get_spellword(list_T *list, char_u **pp)
465{
466 listitem_T *li;
467
468 li = list->lv_first;
469 if (li == NULL)
470 return -1;
471 *pp = tv_get_string(&li->li_tv);
472
473 li = li->li_next;
474 if (li == NULL)
475 return -1;
476 return (int)tv_get_number(&li->li_tv);
477}
478#endif
479
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200480/*
481 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200482 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200483 * When not used yet add the variable to the v: hashtable.
484 */
485 void
486prepare_vimvar(int idx, typval_T *save_tv)
487{
488 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200489 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200490 if (vimvars[idx].vv_type == VAR_UNKNOWN)
491 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
492}
493
494/*
495 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200496 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200497 * When no longer defined, remove the variable from the v: hashtable.
498 */
499 void
500restore_vimvar(int idx, typval_T *save_tv)
501{
502 hashitem_T *hi;
503
504 vimvars[idx].vv_tv = *save_tv;
505 if (vimvars[idx].vv_type == VAR_UNKNOWN)
506 {
507 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
508 if (HASHITEM_EMPTY(hi))
509 internal_error("restore_vimvar()");
510 else
511 hash_remove(&vimvarht, hi);
512 }
513}
514
515/*
516 * List Vim variables.
517 */
518 static void
519list_vim_vars(int *first)
520{
521 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
522}
523
524/*
525 * List script-local variables, if there is a script.
526 */
527 static void
528list_script_vars(int *first)
529{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200530 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200531 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
532 "s:", FALSE, first);
533}
534
535/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200536 * Get a list of lines from a HERE document. The here document is a list of
537 * lines surrounded by a marker.
538 * cmd << {marker}
539 * {line1}
540 * {line2}
541 * ....
542 * {marker}
543 *
544 * The {marker} is a string. If the optional 'trim' word is supplied before the
545 * marker, then the leading indentation before the lines (matching the
546 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200547 *
548 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
549 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
550 * missing, then '.' is accepted as a marker.
551 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200552 * Returns a List with {lines} or NULL.
553 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200555heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200556{
557 char_u *theline;
558 char_u *marker;
559 list_T *l;
560 char_u *p;
561 int marker_indent_len = 0;
562 int text_indent_len = 0;
563 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200564 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200565 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200566
567 if (eap->getline == NULL)
568 {
569 emsg(_("E991: cannot use =<< here"));
570 return NULL;
571 }
572
573 // Check for the optional 'trim' word before the marker
574 cmd = skipwhite(cmd);
575 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
576 {
577 cmd = skipwhite(cmd + 4);
578
579 // Trim the indentation from all the lines in the here document.
580 // The amount of indentation trimmed is the same as the indentation of
581 // the first line after the :let command line. To find the end marker
582 // the indent of the :let command line is trimmed.
583 p = *eap->cmdlinep;
584 while (VIM_ISWHITE(*p))
585 {
586 p++;
587 marker_indent_len++;
588 }
589 text_indent_len = -1;
590 }
591
592 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200593 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200594 {
595 marker = skipwhite(cmd);
596 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200597 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200598 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200599 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 return NULL;
601 }
602 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200603 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200604 {
605 emsg(_("E221: Marker cannot start with lower case letter"));
606 return NULL;
607 }
608 }
609 else
610 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200611 // When getting lines for an embedded script, if the marker is missing,
612 // accept '.' as the marker.
613 if (script_get)
614 marker = dot;
615 else
616 {
617 emsg(_("E172: Missing marker"));
618 return NULL;
619 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200620 }
621
622 l = list_alloc();
623 if (l == NULL)
624 return NULL;
625
626 for (;;)
627 {
628 int mi = 0;
629 int ti = 0;
630
631 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
632 if (theline == NULL)
633 {
634 semsg(_("E990: Missing end marker '%s'"), marker);
635 break;
636 }
637
638 // with "trim": skip the indent matching the :let line to find the
639 // marker
640 if (marker_indent_len > 0
641 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
642 mi = marker_indent_len;
643 if (STRCMP(marker, theline + mi) == 0)
644 {
645 vim_free(theline);
646 break;
647 }
648
649 if (text_indent_len == -1 && *theline != NUL)
650 {
651 // set the text indent from the first line.
652 p = theline;
653 text_indent_len = 0;
654 while (VIM_ISWHITE(*p))
655 {
656 p++;
657 text_indent_len++;
658 }
659 text_indent = vim_strnsave(theline, text_indent_len);
660 }
661 // with "trim": skip the indent matching the first line
662 if (text_indent != NULL)
663 for (ti = 0; ti < text_indent_len; ++ti)
664 if (theline[ti] != text_indent[ti])
665 break;
666
667 if (list_append_string(l, theline + ti, -1) == FAIL)
668 break;
669 vim_free(theline);
670 }
671 vim_free(text_indent);
672
673 return l;
674}
675
676/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200677 * Vim9 variable declaration:
678 * ":var name"
679 * ":var name: type"
680 * ":var name = expr"
681 * ":var name: type = expr"
682 * etc.
683 */
684 void
685ex_var(exarg_T *eap)
686{
687 if (!in_vim9script())
688 {
689 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
690 return;
691 }
692 ex_let(eap);
693}
694
695/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200696 * ":let" list all variable values
697 * ":let var1 var2" list variable values
698 * ":let var = expr" assignment command.
699 * ":let var += expr" assignment command.
700 * ":let var -= expr" assignment command.
701 * ":let var *= expr" assignment command.
702 * ":let var /= expr" assignment command.
703 * ":let var %= expr" assignment command.
704 * ":let var .= expr" assignment command.
705 * ":let var ..= expr" assignment command.
706 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100708 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200709 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200710 * ":final var = expr" assignment command.
711 * ":final [var1, var2] = expr" unpack list.
712 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200713 * ":const" list all variable values
714 * ":const var1 var2" list variable values
715 * ":const var = expr" assignment command.
716 * ":const [var1, var2] = expr" unpack list.
717 */
718 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200719ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200720{
721 char_u *arg = eap->arg;
722 char_u *expr = NULL;
723 typval_T rettv;
724 int i;
725 int var_count = 0;
726 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200727 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200728 char_u *argend;
729 int first = TRUE;
730 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200731 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200732 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200733 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200734
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200735 if (eap->cmdidx == CMD_final && !vim9script)
736 {
737 // In legacy Vim script ":final" is short for ":finally".
738 ex_finally(eap);
739 return;
740 }
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200741 if (get_vim_var_nr(VV_DISALLOW_LET)
742 && eap->cmdidx == CMD_let && vim9script)
743 {
744 emsg(_(e_cannot_use_let_in_vim9_script));
745 return;
746 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200747 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
748 // In legacy Vim script ":const" works like ":final".
749 eap->cmdidx = CMD_final;
750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 // detect Vim9 assignment without ":let" or ":const"
752 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200753 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200755 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200756 if (argend == NULL)
757 return;
758 if (argend > arg && argend[-1] == '.') // for var.='str'
759 --argend;
760 expr = skipwhite(argend);
761 concat = expr[0] == '.'
762 && ((expr[1] == '=' && current_sctx.sc_version < 2)
763 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200764 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
765 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200766 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767 {
768 // ":let" without "=": list variables
769 if (*arg == '[')
770 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200771 else if (expr[0] == '.' && expr[1] == '=')
772 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200773 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200774 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200775 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200776 {
777 // Vim9 declaration ":let var: type"
778 arg = vim9_declare_scriptvar(eap, arg);
779 }
780 else
781 {
782 // ":let var1 var2" - list values
783 arg = list_arg_vars(eap, arg, &first);
784 }
785 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200786 else if (!eap->skip)
787 {
788 // ":let"
789 list_glob_vars(&first);
790 list_buf_vars(&first);
791 list_win_vars(&first);
792 list_tab_vars(&first);
793 list_script_vars(&first);
794 list_func_vars(&first);
795 list_vim_vars(&first);
796 }
797 eap->nextcmd = check_nextcmd(arg);
798 }
799 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
800 {
801 list_T *l;
802
803 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200804 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 if (l != NULL)
806 {
807 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200808 if (!eap->skip)
809 {
810 op[0] = '=';
811 op[1] = NUL;
812 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100813 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200814 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 clear_tv(&rettv);
816 }
817 }
818 else
819 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200820 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200821 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200822
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200823 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200824 i = FAIL;
825 if (has_assign || concat)
826 {
827 op[0] = '=';
828 op[1] = NUL;
829 if (*expr != '=')
830 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200831 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200832 {
833 // +=, /=, etc. require an existing variable
834 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
835 i = FAIL;
836 }
837 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200838 {
839 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200840 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200841 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200842 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200843 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200844 ++len;
845 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200846 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200847 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848 }
849 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++expr;
851
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200852 if (vim9script && (!VIM_ISWHITE(*argend)
853 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 {
855 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200856 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200857 i = FAIL;
858 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200859
860 if (eap->skip)
861 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200862 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200863 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200864 if (getline_equal(eap->getline, eap->cookie, getsourceline))
865 {
866 evalarg.eval_getline = eap->getline;
867 evalarg.eval_cookie = eap->cookie;
868 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200869 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200870 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200871 if (eap->skip)
872 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200873 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200874 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200875 if (eap->skip)
876 {
877 if (i != FAIL)
878 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200879 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200880 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200881 {
882 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200883 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200884 clear_tv(&rettv);
885 }
886 }
887}
888
889/*
890 * Assign the typevalue "tv" to the variable or variables at "arg_start".
891 * Handles both "var" with any type and "[var, var; var]" with a list type.
892 * When "op" is not NULL it points to a string with characters that
893 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
894 * or concatenate.
895 * Returns OK or FAIL;
896 */
897 int
898ex_let_vars(
899 char_u *arg_start,
900 typval_T *tv,
901 int copy, // copy values from "tv", don't move
902 int semicolon, // from skip_var_list()
903 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200904 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200905 char_u *op)
906{
907 char_u *arg = arg_start;
908 list_T *l;
909 int i;
910 listitem_T *item;
911 typval_T ltv;
912
913 if (*arg != '[')
914 {
915 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200917 return FAIL;
918 return OK;
919 }
920
921 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
922 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
923 {
924 emsg(_(e_listreq));
925 return FAIL;
926 }
927
928 i = list_len(l);
929 if (semicolon == 0 && var_count < i)
930 {
931 emsg(_("E687: Less targets than List items"));
932 return FAIL;
933 }
934 if (var_count - semicolon > i)
935 {
936 emsg(_("E688: More targets than List items"));
937 return FAIL;
938 }
939
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200940 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200941 item = l->lv_first;
942 while (*arg != ']')
943 {
944 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200946 item = item->li_next;
947 if (arg == NULL)
948 return FAIL;
949
950 arg = skipwhite(arg);
951 if (*arg == ';')
952 {
953 // Put the rest of the list (may be empty) in the var after ';'.
954 // Create a new list for this.
955 l = list_alloc();
956 if (l == NULL)
957 return FAIL;
958 while (item != NULL)
959 {
960 list_append_tv(l, &item->li_tv);
961 item = item->li_next;
962 }
963
964 ltv.v_type = VAR_LIST;
965 ltv.v_lock = 0;
966 ltv.vval.v_list = l;
967 l->lv_refcount = 1;
968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200970 (char_u *)"]", op);
971 clear_tv(&ltv);
972 if (arg == NULL)
973 return FAIL;
974 break;
975 }
976 else if (*arg != ',' && *arg != ']')
977 {
978 internal_error("ex_let_vars()");
979 return FAIL;
980 }
981 }
982
983 return OK;
984}
985
986/*
987 * Skip over assignable variable "var" or list of variables "[var, var]".
988 * Used for ":let varvar = expr" and ":for varvar in expr".
989 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200990 * for "[var, var; var]" set "semicolon" to 1.
991 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992 * Return NULL for an error.
993 */
994 char_u *
995skip_var_list(
996 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200999 int *semicolon,
1000 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001{
1002 char_u *p, *s;
1003
1004 if (*arg == '[')
1005 {
1006 // "[var, var]": find the matching ']'.
1007 p = arg;
1008 for (;;)
1009 {
1010 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001011 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012 if (s == p)
1013 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001014 if (!silent)
1015 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001016 return NULL;
1017 }
1018 ++*var_count;
1019
1020 p = skipwhite(s);
1021 if (*p == ']')
1022 break;
1023 else if (*p == ';')
1024 {
1025 if (*semicolon == 1)
1026 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001027 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001028 return NULL;
1029 }
1030 *semicolon = 1;
1031 }
1032 else if (*p != ',')
1033 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001034 if (!silent)
1035 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001036 return NULL;
1037 }
1038 }
1039 return p + 1;
1040 }
1041 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001043}
1044
1045/*
1046 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1047 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001050 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001052{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 char_u *end;
1054
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055 if (*arg == '@' && arg[1] != NUL)
1056 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001059 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001061 // "a: type" is declaring variable "a" with a type, not "a:".
1062 if (end == arg + 2 && end[-1] == ':')
1063 --end;
1064 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001065 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 }
1067 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001068}
1069
1070/*
1071 * List variables for hashtab "ht" with prefix "prefix".
1072 * If "empty" is TRUE also list NULL strings as empty strings.
1073 */
1074 void
1075list_hashtable_vars(
1076 hashtab_T *ht,
1077 char *prefix,
1078 int empty,
1079 int *first)
1080{
1081 hashitem_T *hi;
1082 dictitem_T *di;
1083 int todo;
1084 char_u buf[IOSIZE];
1085
1086 todo = (int)ht->ht_used;
1087 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1088 {
1089 if (!HASHITEM_EMPTY(hi))
1090 {
1091 --todo;
1092 di = HI2DI(hi);
1093
1094 // apply :filter /pat/ to variable name
1095 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1096 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1097 if (message_filtered(buf))
1098 continue;
1099
1100 if (empty || di->di_tv.v_type != VAR_STRING
1101 || di->di_tv.vval.v_string != NULL)
1102 list_one_var(di, prefix, first);
1103 }
1104 }
1105}
1106
1107/*
1108 * List global variables.
1109 */
1110 static void
1111list_glob_vars(int *first)
1112{
1113 list_hashtable_vars(&globvarht, "", TRUE, first);
1114}
1115
1116/*
1117 * List buffer variables.
1118 */
1119 static void
1120list_buf_vars(int *first)
1121{
1122 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1123}
1124
1125/*
1126 * List window variables.
1127 */
1128 static void
1129list_win_vars(int *first)
1130{
1131 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1132}
1133
1134/*
1135 * List tab page variables.
1136 */
1137 static void
1138list_tab_vars(int *first)
1139{
1140 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1141}
1142
1143/*
1144 * List variables in "arg".
1145 */
1146 static char_u *
1147list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1148{
1149 int error = FALSE;
1150 int len;
1151 char_u *name;
1152 char_u *name_start;
1153 char_u *arg_subsc;
1154 char_u *tofree;
1155 typval_T tv;
1156
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001157 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001158 {
1159 if (error || eap->skip)
1160 {
1161 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1162 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1163 {
1164 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001165 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001166 break;
1167 }
1168 }
1169 else
1170 {
1171 // get_name_len() takes care of expanding curly braces
1172 name_start = name = arg;
1173 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1174 if (len <= 0)
1175 {
1176 // This is mainly to keep test 49 working: when expanding
1177 // curly braces fails overrule the exception error message.
1178 if (len < 0 && !aborting())
1179 {
1180 emsg_severe = TRUE;
1181 semsg(_(e_invarg2), arg);
1182 break;
1183 }
1184 error = TRUE;
1185 }
1186 else
1187 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001188 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001189 if (tofree != NULL)
1190 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001191 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001192 error = TRUE;
1193 else
1194 {
1195 // handle d.key, l[idx], f(expr)
1196 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001197 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001198 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001199 error = TRUE;
1200 else
1201 {
1202 if (arg == arg_subsc && len == 2 && name[1] == ':')
1203 {
1204 switch (*name)
1205 {
1206 case 'g': list_glob_vars(first); break;
1207 case 'b': list_buf_vars(first); break;
1208 case 'w': list_win_vars(first); break;
1209 case 't': list_tab_vars(first); break;
1210 case 'v': list_vim_vars(first); break;
1211 case 's': list_script_vars(first); break;
1212 case 'l': list_func_vars(first); break;
1213 default:
1214 semsg(_("E738: Can't list variables for %s"), name);
1215 }
1216 }
1217 else
1218 {
1219 char_u numbuf[NUMBUFLEN];
1220 char_u *tf;
1221 int c;
1222 char_u *s;
1223
1224 s = echo_string(&tv, &tf, numbuf, 0);
1225 c = *arg;
1226 *arg = NUL;
1227 list_one_var_a("",
1228 arg == arg_subsc ? name : name_start,
1229 tv.v_type,
1230 s == NULL ? (char_u *)"" : s,
1231 first);
1232 *arg = c;
1233 vim_free(tf);
1234 }
1235 clear_tv(&tv);
1236 }
1237 }
1238 }
1239
1240 vim_free(tofree);
1241 }
1242
1243 arg = skipwhite(arg);
1244 }
1245
1246 return arg;
1247}
1248
1249/*
1250 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1251 * Returns a pointer to the char just after the var name.
1252 * Returns NULL if there is an error.
1253 */
1254 static char_u *
1255ex_let_one(
1256 char_u *arg, // points to variable name
1257 typval_T *tv, // value to assign to variable
1258 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001259 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260 char_u *endchars, // valid chars after variable name or NULL
1261 char_u *op) // "+", "-", "." or NULL
1262{
1263 int c1;
1264 char_u *name;
1265 char_u *p;
1266 char_u *arg_end = NULL;
1267 int len;
1268 int opt_flags;
1269 char_u *tofree = NULL;
1270
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001271 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001272 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1273 {
1274 vim9_declare_error(arg);
1275 return NULL;
1276 }
1277
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001278 // ":let $VAR = expr": Set environment variable.
1279 if (*arg == '$')
1280 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001281 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 {
1283 emsg(_("E996: Cannot lock an environment variable"));
1284 return NULL;
1285 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001286
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001287 // Find the end of the name.
1288 ++arg;
1289 name = arg;
1290 len = get_env_len(&arg);
1291 if (len == 0)
1292 semsg(_(e_invarg2), name - 1);
1293 else
1294 {
1295 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1296 semsg(_(e_letwrong), op);
1297 else if (endchars != NULL
1298 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1299 emsg(_(e_letunexp));
1300 else if (!check_secure())
1301 {
1302 c1 = name[len];
1303 name[len] = NUL;
1304 p = tv_get_string_chk(tv);
1305 if (p != NULL && op != NULL && *op == '.')
1306 {
1307 int mustfree = FALSE;
1308 char_u *s = vim_getenv(name, &mustfree);
1309
1310 if (s != NULL)
1311 {
1312 p = tofree = concat_str(s, p);
1313 if (mustfree)
1314 vim_free(s);
1315 }
1316 }
1317 if (p != NULL)
1318 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001319 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001320 arg_end = arg;
1321 }
1322 name[len] = c1;
1323 vim_free(tofree);
1324 }
1325 }
1326 }
1327
1328 // ":let &option = expr": Set option value.
1329 // ":let &l:option = expr": Set local option value.
1330 // ":let &g:option = expr": Set global option value.
1331 else if (*arg == '&')
1332 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001333 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001334 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336 return NULL;
1337 }
1338 // Find the end of the name.
1339 p = find_option_end(&arg, &opt_flags);
1340 if (p == NULL || (endchars != NULL
1341 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1342 emsg(_(e_letunexp));
1343 else
1344 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001345 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001346 int opt_type;
1347 long numval;
1348 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001349 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001350 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001351
1352 c1 = *p;
1353 *p = NUL;
1354
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001355 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1356 if ((opt_type == 1 || opt_type == -1)
1357 && (tv->v_type != VAR_STRING || !in_vim9script()))
1358 // number, possibly hidden
1359 n = (long)tv_get_number(tv);
1360
1361 // Avoid setting a string option to the text "v:false" or similar.
1362 // In Vim9 script also don't convert a number to string.
1363 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1364 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1365 s = tv_get_string_chk(tv);
1366
1367 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001368 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001369 if ((opt_type == 1 && *op == '.')
1370 || (opt_type == 0 && *op != '.'))
1371 {
1372 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001373 failed = TRUE; // don't set the value
1374
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375 }
1376 else
1377 {
1378 if (opt_type == 1) // number
1379 {
1380 switch (*op)
1381 {
1382 case '+': n = numval + n; break;
1383 case '-': n = numval - n; break;
1384 case '*': n = numval * n; break;
1385 case '/': n = (long)num_divide(numval, n); break;
1386 case '%': n = (long)num_modulus(numval, n); break;
1387 }
1388 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001389 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001390 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001391 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001392 s = concat_str(stringval, s);
1393 vim_free(stringval);
1394 stringval = s;
1395 }
1396 }
1397 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001398
1399 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001400 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001401 if (opt_type != 0 || s != NULL)
1402 {
1403 set_option_value(arg, n, s, opt_flags);
1404 arg_end = p;
1405 }
1406 else
1407 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408 }
1409 *p = c1;
1410 vim_free(stringval);
1411 }
1412 }
1413
1414 // ":let @r = expr": Set register contents.
1415 else if (*arg == '@')
1416 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001417 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001418 {
1419 emsg(_("E996: Cannot lock a register"));
1420 return NULL;
1421 }
1422 ++arg;
1423 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1424 semsg(_(e_letwrong), op);
1425 else if (endchars != NULL
1426 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1427 emsg(_(e_letunexp));
1428 else
1429 {
1430 char_u *ptofree = NULL;
1431 char_u *s;
1432
1433 p = tv_get_string_chk(tv);
1434 if (p != NULL && op != NULL && *op == '.')
1435 {
1436 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1437 if (s != NULL)
1438 {
1439 p = ptofree = concat_str(s, p);
1440 vim_free(s);
1441 }
1442 }
1443 if (p != NULL)
1444 {
1445 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1446 arg_end = arg + 1;
1447 }
1448 vim_free(ptofree);
1449 }
1450 }
1451
1452 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001454 // ":let {expr} = expr": Idem, name made with curly braces
1455 else if (eval_isnamec1(*arg) || *arg == '{')
1456 {
1457 lval_T lv;
1458
1459 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001460 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 if (endchars != NULL && vim_strchr(endchars,
1463 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001464 emsg(_(e_letunexp));
1465 else
1466 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001468 arg_end = p;
1469 }
1470 }
1471 clear_lval(&lv);
1472 }
1473
1474 else
1475 semsg(_(e_invarg2), arg);
1476
1477 return arg_end;
1478}
1479
1480/*
1481 * ":unlet[!] var1 ... " command.
1482 */
1483 void
1484ex_unlet(exarg_T *eap)
1485{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001486 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001487}
1488
1489/*
1490 * ":lockvar" and ":unlockvar" commands
1491 */
1492 void
1493ex_lockvar(exarg_T *eap)
1494{
1495 char_u *arg = eap->arg;
1496 int deep = 2;
1497
1498 if (eap->forceit)
1499 deep = -1;
1500 else if (vim_isdigit(*arg))
1501 {
1502 deep = getdigits(&arg);
1503 arg = skipwhite(arg);
1504 }
1505
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001506 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001507}
1508
1509/*
1510 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001511 * Also used for Vim9 script. "callback" is invoked as:
1512 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001513 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001514 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001515ex_unletlock(
1516 exarg_T *eap,
1517 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001518 int deep,
1519 int glv_flags,
1520 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1521 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522{
1523 char_u *arg = argstart;
1524 char_u *name_end;
1525 int error = FALSE;
1526 lval_T lv;
1527
1528 do
1529 {
1530 if (*arg == '$')
1531 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001532 lv.ll_name = arg;
1533 lv.ll_tv = NULL;
1534 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001535 if (get_env_len(&arg) == 0)
1536 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001537 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001538 return;
1539 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001540 if (!error && !eap->skip
1541 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1542 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001543 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001545 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001547 // Parse the name and find the end.
1548 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1549 glv_flags, FNE_CHECK_START);
1550 if (lv.ll_name == NULL)
1551 error = TRUE; // error but continue parsing
1552 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1553 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001555 if (name_end != NULL)
1556 {
1557 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001558 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001559 }
1560 if (!(eap->skip || error))
1561 clear_lval(&lv);
1562 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001565 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001566 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001567 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001569 if (!eap->skip)
1570 clear_lval(&lv);
1571 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572
1573 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001574 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575
1576 eap->nextcmd = check_nextcmd(arg);
1577}
1578
1579 static int
1580do_unlet_var(
1581 lval_T *lp,
1582 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001583 exarg_T *eap,
1584 int deep UNUSED,
1585 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001586{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001587 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001588 int ret = OK;
1589 int cc;
1590
1591 if (lp->ll_tv == NULL)
1592 {
1593 cc = *name_end;
1594 *name_end = NUL;
1595
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001596 // Environment variable, normal name or expanded name.
1597 if (*lp->ll_name == '$')
1598 vim_unsetenv(lp->ll_name + 1);
1599 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 ret = FAIL;
1601 *name_end = cc;
1602 }
1603 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001604 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001606 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 return FAIL;
1608 else if (lp->ll_range)
1609 {
1610 listitem_T *li;
1611 listitem_T *ll_li = lp->ll_li;
1612 int ll_n1 = lp->ll_n1;
1613
1614 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1615 {
1616 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001617 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001618 return FAIL;
1619 ll_li = li;
1620 ++ll_n1;
1621 }
1622
1623 // Delete a range of List items.
1624 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1625 {
1626 li = lp->ll_li->li_next;
1627 listitem_remove(lp->ll_list, lp->ll_li);
1628 lp->ll_li = li;
1629 ++lp->ll_n1;
1630 }
1631 }
1632 else
1633 {
1634 if (lp->ll_list != NULL)
1635 // unlet a List item.
1636 listitem_remove(lp->ll_list, lp->ll_li);
1637 else
1638 // unlet a Dictionary item.
1639 dictitem_remove(lp->ll_dict, lp->ll_di);
1640 }
1641
1642 return ret;
1643}
1644
1645/*
1646 * "unlet" a variable. Return OK if it existed, FAIL if not.
1647 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1648 */
1649 int
1650do_unlet(char_u *name, int forceit)
1651{
1652 hashtab_T *ht;
1653 hashitem_T *hi;
1654 char_u *varname;
1655 dict_T *d;
1656 dictitem_T *di;
1657
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001658 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001659 return FAIL;
1660
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001661 ht = find_var_ht(name, &varname);
1662 if (ht != NULL && *varname != NUL)
1663 {
1664 d = get_current_funccal_dict(ht);
1665 if (d == NULL)
1666 {
1667 if (ht == &globvarht)
1668 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001669 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001670 d = &vimvardict;
1671 else
1672 {
1673 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1674 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1675 }
1676 if (d == NULL)
1677 {
1678 internal_error("do_unlet()");
1679 return FAIL;
1680 }
1681 }
1682 hi = hash_find(ht, varname);
1683 if (HASHITEM_EMPTY(hi))
1684 hi = find_hi_in_scoped_ht(name, &ht);
1685 if (hi != NULL && !HASHITEM_EMPTY(hi))
1686 {
1687 di = HI2DI(hi);
1688 if (var_check_fixed(di->di_flags, name, FALSE)
1689 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001690 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001691 return FAIL;
1692
1693 delete_var(ht, hi);
1694 return OK;
1695 }
1696 }
1697 if (forceit)
1698 return OK;
1699 semsg(_("E108: No such variable: \"%s\""), name);
1700 return FAIL;
1701}
1702
1703/*
1704 * Lock or unlock variable indicated by "lp".
1705 * "deep" is the levels to go (-1 for unlimited);
1706 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1707 */
1708 static int
1709do_lock_var(
1710 lval_T *lp,
1711 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001712 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001713 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001714 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001715{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001716 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001717 int ret = OK;
1718 int cc;
1719 dictitem_T *di;
1720
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 if (lp->ll_tv == NULL)
1722 {
1723 cc = *name_end;
1724 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001725 if (*lp->ll_name == '$')
1726 {
1727 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001728 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001729 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001730 else
1731 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001732 // Normal name or expanded name.
1733 di = find_var(lp->ll_name, NULL, TRUE);
1734 if (di == NULL)
1735 ret = FAIL;
1736 else if ((di->di_flags & DI_FLAGS_FIX)
1737 && di->di_tv.v_type != VAR_DICT
1738 && di->di_tv.v_type != VAR_LIST)
1739 {
1740 // For historic reasons this error is not given for a list or
1741 // dict. E.g., the b: dict could be locked/unlocked.
1742 semsg(_(e_lock_unlock), lp->ll_name);
1743 ret = FAIL;
1744 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001745 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001746 {
1747 if (lock)
1748 di->di_flags |= DI_FLAGS_LOCK;
1749 else
1750 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001751 if (deep != 0)
1752 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001753 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001754 }
1755 *name_end = cc;
1756 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001757 else if (deep == 0)
1758 {
1759 // nothing to do
1760 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001761 else if (lp->ll_range)
1762 {
1763 listitem_T *li = lp->ll_li;
1764
1765 // (un)lock a range of List items.
1766 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1767 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001768 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769 li = li->li_next;
1770 ++lp->ll_n1;
1771 }
1772 }
1773 else if (lp->ll_list != NULL)
1774 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001775 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776 else
1777 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001778 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779
1780 return ret;
1781}
1782
1783/*
1784 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001785 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1786 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001788 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001789item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001790{
1791 static int recurse = 0;
1792 list_T *l;
1793 listitem_T *li;
1794 dict_T *d;
1795 blob_T *b;
1796 hashitem_T *hi;
1797 int todo;
1798
1799 if (recurse >= DICT_MAXNEST)
1800 {
1801 emsg(_("E743: variable nested too deep for (un)lock"));
1802 return;
1803 }
1804 if (deep == 0)
1805 return;
1806 ++recurse;
1807
1808 // lock/unlock the item itself
1809 if (lock)
1810 tv->v_lock |= VAR_LOCKED;
1811 else
1812 tv->v_lock &= ~VAR_LOCKED;
1813
1814 switch (tv->v_type)
1815 {
1816 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001817 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001819 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001821 case VAR_STRING:
1822 case VAR_FUNC:
1823 case VAR_PARTIAL:
1824 case VAR_FLOAT:
1825 case VAR_SPECIAL:
1826 case VAR_JOB:
1827 case VAR_CHANNEL:
1828 break;
1829
1830 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001831 if ((b = tv->vval.v_blob) != NULL
1832 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833 {
1834 if (lock)
1835 b->bv_lock |= VAR_LOCKED;
1836 else
1837 b->bv_lock &= ~VAR_LOCKED;
1838 }
1839 break;
1840 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001841 if ((l = tv->vval.v_list) != NULL
1842 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001843 {
1844 if (lock)
1845 l->lv_lock |= VAR_LOCKED;
1846 else
1847 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001848 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001850 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001851 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001852 }
1853 break;
1854 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001855 if ((d = tv->vval.v_dict) != NULL
1856 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001857 {
1858 if (lock)
1859 d->dv_lock |= VAR_LOCKED;
1860 else
1861 d->dv_lock &= ~VAR_LOCKED;
1862 if (deep < 0 || deep > 1)
1863 {
1864 // recursive: lock/unlock the items the List contains
1865 todo = (int)d->dv_hashtab.ht_used;
1866 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1867 {
1868 if (!HASHITEM_EMPTY(hi))
1869 {
1870 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001871 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1872 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001873 }
1874 }
1875 }
1876 }
1877 }
1878 --recurse;
1879}
1880
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001881#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1882/*
1883 * Delete all "menutrans_" variables.
1884 */
1885 void
1886del_menutrans_vars(void)
1887{
1888 hashitem_T *hi;
1889 int todo;
1890
1891 hash_lock(&globvarht);
1892 todo = (int)globvarht.ht_used;
1893 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1894 {
1895 if (!HASHITEM_EMPTY(hi))
1896 {
1897 --todo;
1898 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1899 delete_var(&globvarht, hi);
1900 }
1901 }
1902 hash_unlock(&globvarht);
1903}
1904#endif
1905
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001906/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001907 * Local string buffer for the next two functions to store a variable name
1908 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1909 * get_user_var_name().
1910 */
1911
1912static char_u *varnamebuf = NULL;
1913static int varnamebuflen = 0;
1914
1915/*
1916 * Function to concatenate a prefix and a variable name.
1917 */
1918 static char_u *
1919cat_prefix_varname(int prefix, char_u *name)
1920{
1921 int len;
1922
1923 len = (int)STRLEN(name) + 3;
1924 if (len > varnamebuflen)
1925 {
1926 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001927 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001928 varnamebuf = alloc(len);
1929 if (varnamebuf == NULL)
1930 {
1931 varnamebuflen = 0;
1932 return NULL;
1933 }
1934 varnamebuflen = len;
1935 }
1936 *varnamebuf = prefix;
1937 varnamebuf[1] = ':';
1938 STRCPY(varnamebuf + 2, name);
1939 return varnamebuf;
1940}
1941
1942/*
1943 * Function given to ExpandGeneric() to obtain the list of user defined
1944 * (global/buffer/window/built-in) variable names.
1945 */
1946 char_u *
1947get_user_var_name(expand_T *xp, int idx)
1948{
1949 static long_u gdone;
1950 static long_u bdone;
1951 static long_u wdone;
1952 static long_u tdone;
1953 static int vidx;
1954 static hashitem_T *hi;
1955 hashtab_T *ht;
1956
1957 if (idx == 0)
1958 {
1959 gdone = bdone = wdone = vidx = 0;
1960 tdone = 0;
1961 }
1962
1963 // Global variables
1964 if (gdone < globvarht.ht_used)
1965 {
1966 if (gdone++ == 0)
1967 hi = globvarht.ht_array;
1968 else
1969 ++hi;
1970 while (HASHITEM_EMPTY(hi))
1971 ++hi;
1972 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1973 return cat_prefix_varname('g', hi->hi_key);
1974 return hi->hi_key;
1975 }
1976
1977 // b: variables
1978 ht = &curbuf->b_vars->dv_hashtab;
1979 if (bdone < ht->ht_used)
1980 {
1981 if (bdone++ == 0)
1982 hi = ht->ht_array;
1983 else
1984 ++hi;
1985 while (HASHITEM_EMPTY(hi))
1986 ++hi;
1987 return cat_prefix_varname('b', hi->hi_key);
1988 }
1989
1990 // w: variables
1991 ht = &curwin->w_vars->dv_hashtab;
1992 if (wdone < ht->ht_used)
1993 {
1994 if (wdone++ == 0)
1995 hi = ht->ht_array;
1996 else
1997 ++hi;
1998 while (HASHITEM_EMPTY(hi))
1999 ++hi;
2000 return cat_prefix_varname('w', hi->hi_key);
2001 }
2002
2003 // t: variables
2004 ht = &curtab->tp_vars->dv_hashtab;
2005 if (tdone < ht->ht_used)
2006 {
2007 if (tdone++ == 0)
2008 hi = ht->ht_array;
2009 else
2010 ++hi;
2011 while (HASHITEM_EMPTY(hi))
2012 ++hi;
2013 return cat_prefix_varname('t', hi->hi_key);
2014 }
2015
2016 // v: variables
2017 if (vidx < VV_LEN)
2018 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2019
2020 VIM_CLEAR(varnamebuf);
2021 varnamebuflen = 0;
2022 return NULL;
2023}
2024
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002025 char *
2026get_var_special_name(int nr)
2027{
2028 switch (nr)
2029 {
2030 case VVAL_FALSE: return "v:false";
2031 case VVAL_TRUE: return "v:true";
2032 case VVAL_NONE: return "v:none";
2033 case VVAL_NULL: return "v:null";
2034 }
2035 internal_error("get_var_special_name()");
2036 return "42";
2037}
2038
2039/*
2040 * Returns the global variable dictionary
2041 */
2042 dict_T *
2043get_globvar_dict(void)
2044{
2045 return &globvardict;
2046}
2047
2048/*
2049 * Returns the global variable hash table
2050 */
2051 hashtab_T *
2052get_globvar_ht(void)
2053{
2054 return &globvarht;
2055}
2056
2057/*
2058 * Returns the v: variable dictionary
2059 */
2060 dict_T *
2061get_vimvar_dict(void)
2062{
2063 return &vimvardict;
2064}
2065
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002066/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002067 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002068 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 */
2070 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002071find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002073 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2074 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075
2076 if (di == NULL)
2077 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002078 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2080 return (int)(vv - vimvars);
2081}
2082
2083
2084/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002085 * Set type of v: variable to "type".
2086 */
2087 void
2088set_vim_var_type(int idx, vartype_T type)
2089{
2090 vimvars[idx].vv_type = type;
2091}
2092
2093/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002094 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002095 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002096 */
2097 void
2098set_vim_var_nr(int idx, varnumber_T val)
2099{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002100 vimvars[idx].vv_nr = val;
2101}
2102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 char *
2104get_vim_var_name(int idx)
2105{
2106 return vimvars[idx].vv_name;
2107}
2108
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002109/*
2110 * Get typval_T v: variable value.
2111 */
2112 typval_T *
2113get_vim_var_tv(int idx)
2114{
2115 return &vimvars[idx].vv_tv;
2116}
2117
2118/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002119 * Set v: variable to "tv". Only accepts the same type.
2120 * Takes over the value of "tv".
2121 */
2122 int
2123set_vim_var_tv(int idx, typval_T *tv)
2124{
2125 if (vimvars[idx].vv_type != tv->v_type)
2126 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002127 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002128 clear_tv(tv);
2129 return FAIL;
2130 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002131 // VV_RO is also checked when compiling, but let's check here as well.
2132 if (vimvars[idx].vv_flags & VV_RO)
2133 {
2134 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2135 return FAIL;
2136 }
2137 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2138 {
2139 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2140 return FAIL;
2141 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002142 clear_tv(&vimvars[idx].vv_di.di_tv);
2143 vimvars[idx].vv_di.di_tv = *tv;
2144 return OK;
2145}
2146
2147/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002148 * Get number v: variable value.
2149 */
2150 varnumber_T
2151get_vim_var_nr(int idx)
2152{
2153 return vimvars[idx].vv_nr;
2154}
2155
2156/*
2157 * Get string v: variable value. Uses a static buffer, can only be used once.
2158 * If the String variable has never been set, return an empty string.
2159 * Never returns NULL;
2160 */
2161 char_u *
2162get_vim_var_str(int idx)
2163{
2164 return tv_get_string(&vimvars[idx].vv_tv);
2165}
2166
2167/*
2168 * Get List v: variable value. Caller must take care of reference count when
2169 * needed.
2170 */
2171 list_T *
2172get_vim_var_list(int idx)
2173{
2174 return vimvars[idx].vv_list;
2175}
2176
2177/*
2178 * Get Dict v: variable value. Caller must take care of reference count when
2179 * needed.
2180 */
2181 dict_T *
2182get_vim_var_dict(int idx)
2183{
2184 return vimvars[idx].vv_dict;
2185}
2186
2187/*
2188 * Set v:char to character "c".
2189 */
2190 void
2191set_vim_var_char(int c)
2192{
2193 char_u buf[MB_MAXBYTES + 1];
2194
2195 if (has_mbyte)
2196 buf[(*mb_char2bytes)(c, buf)] = NUL;
2197 else
2198 {
2199 buf[0] = c;
2200 buf[1] = NUL;
2201 }
2202 set_vim_var_string(VV_CHAR, buf, -1);
2203}
2204
2205/*
2206 * Set v:count to "count" and v:count1 to "count1".
2207 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2208 */
2209 void
2210set_vcount(
2211 long count,
2212 long count1,
2213 int set_prevcount)
2214{
2215 if (set_prevcount)
2216 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2217 vimvars[VV_COUNT].vv_nr = count;
2218 vimvars[VV_COUNT1].vv_nr = count1;
2219}
2220
2221/*
2222 * Save variables that might be changed as a side effect. Used when executing
2223 * a timer callback.
2224 */
2225 void
2226save_vimvars(vimvars_save_T *vvsave)
2227{
2228 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2229 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2230 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2231}
2232
2233/*
2234 * Restore variables saved by save_vimvars().
2235 */
2236 void
2237restore_vimvars(vimvars_save_T *vvsave)
2238{
2239 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2240 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2241 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2242}
2243
2244/*
2245 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2246 * value.
2247 */
2248 void
2249set_vim_var_string(
2250 int idx,
2251 char_u *val,
2252 int len) // length of "val" to use or -1 (whole string)
2253{
2254 clear_tv(&vimvars[idx].vv_di.di_tv);
2255 vimvars[idx].vv_type = VAR_STRING;
2256 if (val == NULL)
2257 vimvars[idx].vv_str = NULL;
2258 else if (len == -1)
2259 vimvars[idx].vv_str = vim_strsave(val);
2260 else
2261 vimvars[idx].vv_str = vim_strnsave(val, len);
2262}
2263
2264/*
2265 * Set List v: variable to "val".
2266 */
2267 void
2268set_vim_var_list(int idx, list_T *val)
2269{
2270 clear_tv(&vimvars[idx].vv_di.di_tv);
2271 vimvars[idx].vv_type = VAR_LIST;
2272 vimvars[idx].vv_list = val;
2273 if (val != NULL)
2274 ++val->lv_refcount;
2275}
2276
2277/*
2278 * Set Dictionary v: variable to "val".
2279 */
2280 void
2281set_vim_var_dict(int idx, dict_T *val)
2282{
2283 clear_tv(&vimvars[idx].vv_di.di_tv);
2284 vimvars[idx].vv_type = VAR_DICT;
2285 vimvars[idx].vv_dict = val;
2286 if (val != NULL)
2287 {
2288 ++val->dv_refcount;
2289 dict_set_items_ro(val);
2290 }
2291}
2292
2293/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002294 * Set the v:argv list.
2295 */
2296 void
2297set_argv_var(char **argv, int argc)
2298{
2299 list_T *l = list_alloc();
2300 int i;
2301
2302 if (l == NULL)
2303 getout(1);
2304 l->lv_lock = VAR_FIXED;
2305 for (i = 0; i < argc; ++i)
2306 {
2307 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2308 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002309 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002310 }
2311 set_vim_var_list(VV_ARGV, l);
2312}
2313
2314/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002315 * Reset v:register, taking the 'clipboard' setting into account.
2316 */
2317 void
2318reset_reg_var(void)
2319{
2320 int regname = 0;
2321
2322 // Adjust the register according to 'clipboard', so that when
2323 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2324#ifdef FEAT_CLIPBOARD
2325 adjust_clip_reg(&regname);
2326#endif
2327 set_reg_var(regname);
2328}
2329
2330/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002331 * Set v:register if needed.
2332 */
2333 void
2334set_reg_var(int c)
2335{
2336 char_u regname;
2337
2338 if (c == 0 || c == ' ')
2339 regname = '"';
2340 else
2341 regname = c;
2342 // Avoid free/alloc when the value is already right.
2343 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2344 set_vim_var_string(VV_REG, &regname, 1);
2345}
2346
2347/*
2348 * Get or set v:exception. If "oldval" == NULL, return the current value.
2349 * Otherwise, restore the value to "oldval" and return NULL.
2350 * Must always be called in pairs to save and restore v:exception! Does not
2351 * take care of memory allocations.
2352 */
2353 char_u *
2354v_exception(char_u *oldval)
2355{
2356 if (oldval == NULL)
2357 return vimvars[VV_EXCEPTION].vv_str;
2358
2359 vimvars[VV_EXCEPTION].vv_str = oldval;
2360 return NULL;
2361}
2362
2363/*
2364 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2365 * Otherwise, restore the value to "oldval" and return NULL.
2366 * Must always be called in pairs to save and restore v:throwpoint! Does not
2367 * take care of memory allocations.
2368 */
2369 char_u *
2370v_throwpoint(char_u *oldval)
2371{
2372 if (oldval == NULL)
2373 return vimvars[VV_THROWPOINT].vv_str;
2374
2375 vimvars[VV_THROWPOINT].vv_str = oldval;
2376 return NULL;
2377}
2378
2379/*
2380 * Set v:cmdarg.
2381 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2382 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2383 * Must always be called in pairs!
2384 */
2385 char_u *
2386set_cmdarg(exarg_T *eap, char_u *oldarg)
2387{
2388 char_u *oldval;
2389 char_u *newval;
2390 unsigned len;
2391
2392 oldval = vimvars[VV_CMDARG].vv_str;
2393 if (eap == NULL)
2394 {
2395 vim_free(oldval);
2396 vimvars[VV_CMDARG].vv_str = oldarg;
2397 return NULL;
2398 }
2399
2400 if (eap->force_bin == FORCE_BIN)
2401 len = 6;
2402 else if (eap->force_bin == FORCE_NOBIN)
2403 len = 8;
2404 else
2405 len = 0;
2406
2407 if (eap->read_edit)
2408 len += 7;
2409
2410 if (eap->force_ff != 0)
2411 len += 10; // " ++ff=unix"
2412 if (eap->force_enc != 0)
2413 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2414 if (eap->bad_char != 0)
2415 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2416
2417 newval = alloc(len + 1);
2418 if (newval == NULL)
2419 return NULL;
2420
2421 if (eap->force_bin == FORCE_BIN)
2422 sprintf((char *)newval, " ++bin");
2423 else if (eap->force_bin == FORCE_NOBIN)
2424 sprintf((char *)newval, " ++nobin");
2425 else
2426 *newval = NUL;
2427
2428 if (eap->read_edit)
2429 STRCAT(newval, " ++edit");
2430
2431 if (eap->force_ff != 0)
2432 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2433 eap->force_ff == 'u' ? "unix"
2434 : eap->force_ff == 'd' ? "dos"
2435 : "mac");
2436 if (eap->force_enc != 0)
2437 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2438 eap->cmd + eap->force_enc);
2439 if (eap->bad_char == BAD_KEEP)
2440 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2441 else if (eap->bad_char == BAD_DROP)
2442 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2443 else if (eap->bad_char != 0)
2444 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2445 vimvars[VV_CMDARG].vv_str = newval;
2446 return oldval;
2447}
2448
2449/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002450 * Get the value of internal variable "name".
2451 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2452 */
2453 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002454eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002455 char_u *name,
2456 int len, // length of "name"
2457 typval_T *rettv, // NULL when only checking existence
2458 dictitem_T **dip, // non-NULL when typval's dict item is needed
2459 int verbose, // may give error message
2460 int no_autoload) // do not use script autoloading
2461{
2462 int ret = OK;
2463 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002464 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002465 dictitem_T *v;
2466 int cc;
2467
2468 // truncate the name, so that we can use strcmp()
2469 cc = name[len];
2470 name[len] = NUL;
2471
2472 // Check for user-defined variables.
2473 v = find_var(name, NULL, no_autoload);
2474 if (v != NULL)
2475 {
2476 tv = &v->di_tv;
2477 if (dip != NULL)
2478 *dip = v;
2479 }
2480
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002481 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002483 imported_T *import;
2484 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2485
2486 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487
2488 // imported variable from another script
2489 if (import != NULL)
2490 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002491 if (import->imp_funcname != NULL)
2492 {
2493 foundFunc = TRUE;
2494 if (rettv != NULL)
2495 {
2496 rettv->v_type = VAR_FUNC;
2497 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2498 }
2499 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002500 else if (import->imp_all)
2501 {
2502 emsg("Sorry, 'import * as X' not implemented yet");
2503 ret = FAIL;
2504 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002505 else
2506 {
2507 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002508 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002510 tv = sv->sv_tv;
2511 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002513 else if (in_vim9script())
2514 {
2515 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2516
2517 if (ufunc != NULL)
2518 {
2519 foundFunc = TRUE;
2520 if (rettv != NULL)
2521 {
2522 rettv->v_type = VAR_FUNC;
2523 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2524 }
2525 }
2526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 }
2528
Bram Moolenaarc620c052020-07-08 15:16:19 +02002529 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002530 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002531 if (tv == NULL)
2532 {
2533 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002534 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002535 ret = FAIL;
2536 }
2537 else if (rettv != NULL)
2538 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002539 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002540
2541 name[len] = cc;
2542
2543 return ret;
2544}
2545
2546/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002547 * Check if variable "name[len]" is a local variable or an argument.
2548 * If so, "*eval_lavars_used" is set to TRUE.
2549 */
2550 void
2551check_vars(char_u *name, int len)
2552{
2553 int cc;
2554 char_u *varname;
2555 hashtab_T *ht;
2556
2557 if (eval_lavars_used == NULL)
2558 return;
2559
2560 // truncate the name, so that we can use strcmp()
2561 cc = name[len];
2562 name[len] = NUL;
2563
2564 ht = find_var_ht(name, &varname);
2565 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2566 {
2567 if (find_var(name, NULL, TRUE) != NULL)
2568 *eval_lavars_used = TRUE;
2569 }
2570
2571 name[len] = cc;
2572}
2573
2574/*
2575 * Find variable "name" in the list of variables.
2576 * Return a pointer to it if found, NULL if not found.
2577 * Careful: "a:0" variables don't have a name.
2578 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2579 * hashtab_T used.
2580 */
2581 dictitem_T *
2582find_var(char_u *name, hashtab_T **htp, int no_autoload)
2583{
2584 char_u *varname;
2585 hashtab_T *ht;
2586 dictitem_T *ret = NULL;
2587
2588 ht = find_var_ht(name, &varname);
2589 if (htp != NULL)
2590 *htp = ht;
2591 if (ht == NULL)
2592 return NULL;
2593 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2594 if (ret != NULL)
2595 return ret;
2596
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002597 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002598 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2599}
2600
2601/*
2602 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002603 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002604 * Returns NULL if not found.
2605 */
2606 dictitem_T *
2607find_var_in_ht(
2608 hashtab_T *ht,
2609 int htname,
2610 char_u *varname,
2611 int no_autoload)
2612{
2613 hashitem_T *hi;
2614
2615 if (*varname == NUL)
2616 {
2617 // Must be something like "s:", otherwise "ht" would be NULL.
2618 switch (htname)
2619 {
2620 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2621 case 'g': return &globvars_var;
2622 case 'v': return &vimvars_var;
2623 case 'b': return &curbuf->b_bufvar;
2624 case 'w': return &curwin->w_winvar;
2625 case 't': return &curtab->tp_winvar;
2626 case 'l': return get_funccal_local_var();
2627 case 'a': return get_funccal_args_var();
2628 }
2629 return NULL;
2630 }
2631
2632 hi = hash_find(ht, varname);
2633 if (HASHITEM_EMPTY(hi))
2634 {
2635 // For global variables we may try auto-loading the script. If it
2636 // worked find the variable again. Don't auto-load a script if it was
2637 // loaded already, otherwise it would be loaded every time when
2638 // checking if a function name is a Funcref variable.
2639 if (ht == &globvarht && !no_autoload)
2640 {
2641 // Note: script_autoload() may make "hi" invalid. It must either
2642 // be obtained again or not used.
2643 if (!script_autoload(varname, FALSE) || aborting())
2644 return NULL;
2645 hi = hash_find(ht, varname);
2646 }
2647 if (HASHITEM_EMPTY(hi))
2648 return NULL;
2649 }
2650 return HI2DI(hi);
2651}
2652
2653/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 * Get the script-local hashtab. NULL if not in a script context.
2655 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002656 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657get_script_local_ht(void)
2658{
2659 scid_T sid = current_sctx.sc_sid;
2660
Bram Moolenaare3d46852020-08-29 13:39:17 +02002661 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 return &SCRIPT_VARS(sid);
2663 return NULL;
2664}
2665
2666/*
2667 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002668 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002669 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002670 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2672{
2673 hashtab_T *ht = get_script_local_ht();
2674 char_u buffer[30];
2675 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002676 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677 hashitem_T *hi;
2678
2679 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002680 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 if (len < sizeof(buffer) - 1)
2682 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002683 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 vim_strncpy(buffer, name, len);
2685 p = buffer;
2686 }
2687 else
2688 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002689 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002691 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 }
2693
2694 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002695 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696
2697 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002698 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2699 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002700
2701 if (p != buffer)
2702 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002703 // Don't return "buffer", gcc complains.
2704 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705}
2706
2707/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002708 * Find the hashtab used for a variable name.
2709 * Return NULL if the name is not valid.
2710 * Set "varname" to the start of name without ':'.
2711 */
2712 hashtab_T *
2713find_var_ht(char_u *name, char_u **varname)
2714{
2715 hashitem_T *hi;
2716 hashtab_T *ht;
2717
2718 if (name[0] == NUL)
2719 return NULL;
2720 if (name[1] != ':')
2721 {
2722 // The name must not start with a colon or #.
2723 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2724 return NULL;
2725 *varname = name;
2726
2727 // "version" is "v:version" in all scopes if scriptversion < 3.
2728 // Same for a few other variables marked with VV_COMPAT.
2729 if (current_sctx.sc_version < 3)
2730 {
2731 hi = hash_find(&compat_hashtab, name);
2732 if (!HASHITEM_EMPTY(hi))
2733 return &compat_hashtab;
2734 }
2735
2736 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 if (ht != NULL)
2738 return ht; // local variable
2739
2740 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002741 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 {
2743 ht = get_script_local_ht();
2744 if (ht != NULL)
2745 return ht;
2746 }
2747
2748 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002749 }
2750 *varname = name + 2;
2751 if (*name == 'g') // global variable
2752 return &globvarht;
2753 // There must be no ':' or '#' in the rest of the name, unless g: is used
2754 if (vim_strchr(name + 2, ':') != NULL
2755 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2756 return NULL;
2757 if (*name == 'b') // buffer variable
2758 return &curbuf->b_vars->dv_hashtab;
2759 if (*name == 'w') // window variable
2760 return &curwin->w_vars->dv_hashtab;
2761 if (*name == 't') // tab page variable
2762 return &curtab->tp_vars->dv_hashtab;
2763 if (*name == 'v') // v: variable
2764 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002765 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002766 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002768 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 if (*name == 'a') // a: function argument
2770 return get_funccal_args_ht();
2771 if (*name == 'l') // l: local function variable
2772 return get_funccal_local_ht();
2773 }
2774 if (*name == 's') // script variable
2775 {
2776 ht = get_script_local_ht();
2777 if (ht != NULL)
2778 return ht;
2779 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002780 return NULL;
2781}
2782
2783/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002784 * Get the string value of a (global/local) variable.
2785 * Note: see tv_get_string() for how long the pointer remains valid.
2786 * Returns NULL when it doesn't exist.
2787 */
2788 char_u *
2789get_var_value(char_u *name)
2790{
2791 dictitem_T *v;
2792
2793 v = find_var(name, NULL, FALSE);
2794 if (v == NULL)
2795 return NULL;
2796 return tv_get_string(&v->di_tv);
2797}
2798
2799/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002800 * Allocate a new hashtab for a sourced script. It will be used while
2801 * sourcing this script and when executing functions defined in the script.
2802 */
2803 void
2804new_script_vars(scid_T id)
2805{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002806 scriptvar_T *sv;
2807
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002808 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2809 if (sv == NULL)
2810 return;
2811 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002812 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002813}
2814
2815/*
2816 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2817 * point to it.
2818 */
2819 void
2820init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2821{
2822 hash_init(&dict->dv_hashtab);
2823 dict->dv_lock = 0;
2824 dict->dv_scope = scope;
2825 dict->dv_refcount = DO_NOT_FREE_CNT;
2826 dict->dv_copyID = 0;
2827 dict_var->di_tv.vval.v_dict = dict;
2828 dict_var->di_tv.v_type = VAR_DICT;
2829 dict_var->di_tv.v_lock = VAR_FIXED;
2830 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2831 dict_var->di_key[0] = NUL;
2832}
2833
2834/*
2835 * Unreference a dictionary initialized by init_var_dict().
2836 */
2837 void
2838unref_var_dict(dict_T *dict)
2839{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002840 // Now the dict needs to be freed if no one else is using it, go back to
2841 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002842 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2843 dict_unref(dict);
2844}
2845
2846/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002847 * Clean up a list of internal variables.
2848 * Frees all allocated variables and the value they contain.
2849 * Clears hashtab "ht", does not free it.
2850 */
2851 void
2852vars_clear(hashtab_T *ht)
2853{
2854 vars_clear_ext(ht, TRUE);
2855}
2856
2857/*
2858 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2859 */
2860 void
2861vars_clear_ext(hashtab_T *ht, int free_val)
2862{
2863 int todo;
2864 hashitem_T *hi;
2865 dictitem_T *v;
2866
2867 hash_lock(ht);
2868 todo = (int)ht->ht_used;
2869 for (hi = ht->ht_array; todo > 0; ++hi)
2870 {
2871 if (!HASHITEM_EMPTY(hi))
2872 {
2873 --todo;
2874
2875 // Free the variable. Don't remove it from the hashtab,
2876 // ht_array might change then. hash_clear() takes care of it
2877 // later.
2878 v = HI2DI(hi);
2879 if (free_val)
2880 clear_tv(&v->di_tv);
2881 if (v->di_flags & DI_FLAGS_ALLOC)
2882 vim_free(v);
2883 }
2884 }
2885 hash_clear(ht);
2886 ht->ht_used = 0;
2887}
2888
2889/*
2890 * Delete a variable from hashtab "ht" at item "hi".
2891 * Clear the variable value and free the dictitem.
2892 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002893 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002894delete_var(hashtab_T *ht, hashitem_T *hi)
2895{
2896 dictitem_T *di = HI2DI(hi);
2897
2898 hash_remove(ht, hi);
2899 clear_tv(&di->di_tv);
2900 vim_free(di);
2901}
2902
2903/*
2904 * List the value of one internal variable.
2905 */
2906 static void
2907list_one_var(dictitem_T *v, char *prefix, int *first)
2908{
2909 char_u *tofree;
2910 char_u *s;
2911 char_u numbuf[NUMBUFLEN];
2912
2913 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2914 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2915 s == NULL ? (char_u *)"" : s, first);
2916 vim_free(tofree);
2917}
2918
2919 static void
2920list_one_var_a(
2921 char *prefix,
2922 char_u *name,
2923 int type,
2924 char_u *string,
2925 int *first) // when TRUE clear rest of screen and set to FALSE
2926{
2927 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2928 msg_start();
2929 msg_puts(prefix);
2930 if (name != NULL) // "a:" vars don't have a name stored
2931 msg_puts((char *)name);
2932 msg_putchar(' ');
2933 msg_advance(22);
2934 if (type == VAR_NUMBER)
2935 msg_putchar('#');
2936 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2937 msg_putchar('*');
2938 else if (type == VAR_LIST)
2939 {
2940 msg_putchar('[');
2941 if (*string == '[')
2942 ++string;
2943 }
2944 else if (type == VAR_DICT)
2945 {
2946 msg_putchar('{');
2947 if (*string == '{')
2948 ++string;
2949 }
2950 else
2951 msg_putchar(' ');
2952
2953 msg_outtrans(string);
2954
2955 if (type == VAR_FUNC || type == VAR_PARTIAL)
2956 msg_puts("()");
2957 if (*first)
2958 {
2959 msg_clr_eos();
2960 *first = FALSE;
2961 }
2962}
2963
2964/*
2965 * Set variable "name" to value in "tv".
2966 * If the variable already exists, the value is updated.
2967 * Otherwise the variable is created.
2968 */
2969 void
2970set_var(
2971 char_u *name,
2972 typval_T *tv,
2973 int copy) // make copy of value in "tv"
2974{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002975 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002976}
2977
2978/*
2979 * Set variable "name" to value in "tv".
2980 * If the variable already exists and "is_const" is FALSE the value is updated.
2981 * Otherwise the variable is created.
2982 */
2983 void
2984set_var_const(
2985 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002987 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002989 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002991 typval_T *tv = tv_arg;
2992 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994 char_u *varname;
2995 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002997 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002998
2999 ht = find_var_ht(name, &varname);
3000 if (ht == NULL || *varname == NUL)
3001 {
3002 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003003 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003004 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003005 is_script_local = ht == get_script_local_ht();
3006
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003007 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003008 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003009 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003010 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003011 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003012 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003013 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003014 }
3015
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003017
3018 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 if (di == NULL)
3020 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003021
3022 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003023 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003024 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003026 if (need_convert_to_bool(type, tv))
3027 {
3028 // Destination is a bool and the value is not, but it can be converted.
3029 CLEAR_FIELD(bool_tv);
3030 bool_tv.v_type = VAR_BOOL;
3031 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3032 tv = &bool_tv;
3033 }
3034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003036 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003038 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003039 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 {
3041 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003042 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 }
3044
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003045 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003047 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003048 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003049 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003050 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003051 }
3052
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003053 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003054 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003055 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003057
Bram Moolenaara187c432020-09-16 21:08:28 +02003058 // Check in this order for backwards compatibility:
3059 // - Whether the variable is read-only
3060 // - Whether the variable value is locked
3061 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003062 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003063 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3064 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003065 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003066 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 else
3068 // can only redefine once
3069 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003070
3071 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003074 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003075 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003076 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003078 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003080 if (copy || tv->v_type != VAR_STRING)
3081 {
3082 char_u *val = tv_get_string(tv);
3083
3084 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003085 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 if (di->di_tv.vval.v_string == NULL)
3087 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003088 }
3089 else
3090 {
3091 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003093 tv->vval.v_string = NULL;
3094 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003095 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003096 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003098 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003102#ifdef FEAT_SEARCH_EXTRA
3103 else if (STRCMP(varname, "hlsearch") == 0)
3104 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003106 redraw_all_later(SOME_VALID);
3107 }
3108#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003109 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003112 {
3113 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003114 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003115 }
3116 }
3117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003119 }
3120 else // add a new variable
3121 {
3122 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003123 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003124 {
3125 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003126 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003127 }
3128
3129 // Make sure the variable name is valid.
3130 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003131 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003132
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3134 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003135 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 STRCPY(di->di_key, varname);
3137 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003140 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003141 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003143 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 di->di_flags |= DI_FLAGS_LOCK;
3145
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003146 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003148 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149
3150 // Store a pointer to the typval_T, so that it can be found by
3151 // index instead of using a hastab lookup.
3152 if (ga_grow(&si->sn_var_vals, 1) == OK)
3153 {
3154 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3155 + si->sn_var_vals.ga_len;
3156 sv->sv_name = di->di_key;
3157 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003158 if (type == NULL)
3159 sv->sv_type = typval2type(tv, &si->sn_type_list);
3160 else
3161 sv->sv_type = type;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003162 sv->sv_const = (flags & ASSIGN_CONST);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 sv->sv_export = is_export;
3164 ++si->sn_var_vals.ga_len;
3165
3166 // let ex_export() know the export worked.
3167 is_export = FALSE;
3168 }
3169 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003170 }
3171
3172 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174 else
3175 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 di->di_tv = *tv;
3177 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003178 init_tv(tv);
3179 }
3180
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003181 // ":const var = val" locks the value
3182 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003183 // Like :lockvar! name: lock the value and what it contains, but only
3184 // if the reference count is up to one. That locks only literal
3185 // values.
3186 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003187 return;
3188failed:
3189 if (!copy)
3190 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191}
3192
3193/*
3194 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3195 * Also give an error message.
3196 */
3197 int
3198var_check_ro(int flags, char_u *name, int use_gettext)
3199{
3200 if (flags & DI_FLAGS_RO)
3201 {
3202 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3203 return TRUE;
3204 }
3205 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3206 {
3207 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3208 return TRUE;
3209 }
3210 return FALSE;
3211}
3212
3213/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003214 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3215 * Also give an error message.
3216 */
3217 int
3218var_check_lock(int flags, char_u *name, int use_gettext)
3219{
3220 if (flags & DI_FLAGS_LOCK)
3221 {
3222 semsg(_(e_variable_is_locked_str),
3223 use_gettext ? (char_u *)_(name) : name);
3224 return TRUE;
3225 }
3226 return FALSE;
3227}
3228
3229/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003230 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3231 * Also give an error message.
3232 */
3233 int
3234var_check_fixed(int flags, char_u *name, int use_gettext)
3235{
3236 if (flags & DI_FLAGS_FIX)
3237 {
3238 semsg(_("E795: Cannot delete variable %s"),
3239 use_gettext ? (char_u *)_(name) : name);
3240 return TRUE;
3241 }
3242 return FALSE;
3243}
3244
3245/*
3246 * Check if a funcref is assigned to a valid variable name.
3247 * Return TRUE and give an error if not.
3248 */
3249 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003250var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003251 char_u *name, // points to start of variable name
3252 int new_var) // TRUE when creating the variable
3253{
3254 // Allow for w: b: s: and t:.
3255 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3256 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3257 ? name[2] : name[0]))
3258 {
3259 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3260 name);
3261 return TRUE;
3262 }
3263 // Don't allow hiding a function. When "v" is not NULL we might be
3264 // assigning another function to the same var, the type is checked
3265 // below.
3266 if (new_var && function_exists(name, FALSE))
3267 {
3268 semsg(_("E705: Variable name conflicts with existing function: %s"),
3269 name);
3270 return TRUE;
3271 }
3272 return FALSE;
3273}
3274
3275/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003276 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3277 * value. Also give an error message, using "name" or _("name") when
3278 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003279 */
3280 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003281value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003282{
3283 if (lock & VAR_LOCKED)
3284 {
3285 semsg(_("E741: Value is locked: %s"),
3286 name == NULL ? (char_u *)_("Unknown")
3287 : use_gettext ? (char_u *)_(name)
3288 : name);
3289 return TRUE;
3290 }
3291 if (lock & VAR_FIXED)
3292 {
3293 semsg(_("E742: Cannot change value of %s"),
3294 name == NULL ? (char_u *)_("Unknown")
3295 : use_gettext ? (char_u *)_(name)
3296 : name);
3297 return TRUE;
3298 }
3299 return FALSE;
3300}
3301
3302/*
3303 * Check if a variable name is valid.
3304 * Return FALSE and give an error if not.
3305 */
3306 int
3307valid_varname(char_u *varname)
3308{
3309 char_u *p;
3310
3311 for (p = varname; *p != NUL; ++p)
3312 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3313 && *p != AUTOLOAD_CHAR)
3314 {
3315 semsg(_(e_illvar), varname);
3316 return FALSE;
3317 }
3318 return TRUE;
3319}
3320
3321/*
3322 * getwinvar() and gettabwinvar()
3323 */
3324 static void
3325getwinvar(
3326 typval_T *argvars,
3327 typval_T *rettv,
3328 int off) // 1 for gettabwinvar()
3329{
3330 win_T *win;
3331 char_u *varname;
3332 dictitem_T *v;
3333 tabpage_T *tp = NULL;
3334 int done = FALSE;
3335 win_T *oldcurwin;
3336 tabpage_T *oldtabpage;
3337 int need_switch_win;
3338
3339 if (off == 1)
3340 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3341 else
3342 tp = curtab;
3343 win = find_win_by_nr(&argvars[off], tp);
3344 varname = tv_get_string_chk(&argvars[off + 1]);
3345 ++emsg_off;
3346
3347 rettv->v_type = VAR_STRING;
3348 rettv->vval.v_string = NULL;
3349
3350 if (win != NULL && varname != NULL)
3351 {
3352 // Set curwin to be our win, temporarily. Also set the tabpage,
3353 // otherwise the window is not valid. Only do this when needed,
3354 // autocommands get blocked.
3355 need_switch_win = !(tp == curtab && win == curwin);
3356 if (!need_switch_win
3357 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3358 {
3359 if (*varname == '&')
3360 {
3361 if (varname[1] == NUL)
3362 {
3363 // get all window-local options in a dict
3364 dict_T *opts = get_winbuf_options(FALSE);
3365
3366 if (opts != NULL)
3367 {
3368 rettv_dict_set(rettv, opts);
3369 done = TRUE;
3370 }
3371 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003372 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003373 // window-local-option
3374 done = TRUE;
3375 }
3376 else
3377 {
3378 // Look up the variable.
3379 // Let getwinvar({nr}, "") return the "w:" dictionary.
3380 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3381 varname, FALSE);
3382 if (v != NULL)
3383 {
3384 copy_tv(&v->di_tv, rettv);
3385 done = TRUE;
3386 }
3387 }
3388 }
3389
3390 if (need_switch_win)
3391 // restore previous notion of curwin
3392 restore_win(oldcurwin, oldtabpage, TRUE);
3393 }
3394
3395 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3396 // use the default return value
3397 copy_tv(&argvars[off + 2], rettv);
3398
3399 --emsg_off;
3400}
3401
3402/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003403 * Set option "varname" to the value of "varp" for the current buffer/window.
3404 */
3405 static void
3406set_option_from_tv(char_u *varname, typval_T *varp)
3407{
3408 long numval = 0;
3409 char_u *strval;
3410 char_u nbuf[NUMBUFLEN];
3411 int error = FALSE;
3412
3413 if (!in_vim9script() || varp->v_type != VAR_STRING)
3414 numval = (long)tv_get_number_chk(varp, &error);
3415 strval = tv_get_string_buf_chk(varp, nbuf);
3416 if (!error && strval != NULL)
3417 set_option_value(varname, numval, strval, OPT_LOCAL);
3418}
3419
3420/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003421 * "setwinvar()" and "settabwinvar()" functions
3422 */
3423 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003424setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003425{
3426 win_T *win;
3427 win_T *save_curwin;
3428 tabpage_T *save_curtab;
3429 int need_switch_win;
3430 char_u *varname, *winvarname;
3431 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003432 tabpage_T *tp = NULL;
3433
3434 if (check_secure())
3435 return;
3436
3437 if (off == 1)
3438 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3439 else
3440 tp = curtab;
3441 win = find_win_by_nr(&argvars[off], tp);
3442 varname = tv_get_string_chk(&argvars[off + 1]);
3443 varp = &argvars[off + 2];
3444
3445 if (win != NULL && varname != NULL && varp != NULL)
3446 {
3447 need_switch_win = !(tp == curtab && win == curwin);
3448 if (!need_switch_win
3449 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3450 {
3451 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003452 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003453 else
3454 {
3455 winvarname = alloc(STRLEN(varname) + 3);
3456 if (winvarname != NULL)
3457 {
3458 STRCPY(winvarname, "w:");
3459 STRCPY(winvarname + 2, varname);
3460 set_var(winvarname, varp, TRUE);
3461 vim_free(winvarname);
3462 }
3463 }
3464 }
3465 if (need_switch_win)
3466 restore_win(save_curwin, save_curtab, TRUE);
3467 }
3468}
3469
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003470/*
3471 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3472 * v:option_type, and v:option_command.
3473 */
3474 void
3475reset_v_option_vars(void)
3476{
3477 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3478 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3479 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3480 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3481 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3482 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3483}
3484
3485/*
3486 * Add an assert error to v:errors.
3487 */
3488 void
3489assert_error(garray_T *gap)
3490{
3491 struct vimvar *vp = &vimvars[VV_ERRORS];
3492
3493 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003494 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003495 set_vim_var_list(VV_ERRORS, list_alloc());
3496 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3497}
3498
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003499 int
3500var_exists(char_u *var)
3501{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003502 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003503 char_u *name;
3504 char_u *tofree;
3505 typval_T tv;
3506 int len = 0;
3507 int n = FALSE;
3508
3509 // get_name_len() takes care of expanding curly braces
3510 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003511 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003512 if (len > 0)
3513 {
3514 if (tofree != NULL)
3515 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003516 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003517 if (n)
3518 {
3519 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003520 arg = skipwhite(arg);
3521 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003522 if (n)
3523 clear_tv(&tv);
3524 }
3525 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003526 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003527 n = FALSE;
3528
3529 vim_free(tofree);
3530 return n;
3531}
3532
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003533static lval_T *redir_lval = NULL;
3534#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3535static garray_T redir_ga; // only valid when redir_lval is not NULL
3536static char_u *redir_endp = NULL;
3537static char_u *redir_varname = NULL;
3538
3539/*
3540 * Start recording command output to a variable
3541 * When "append" is TRUE append to an existing variable.
3542 * Returns OK if successfully completed the setup. FAIL otherwise.
3543 */
3544 int
3545var_redir_start(char_u *name, int append)
3546{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003547 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003548 typval_T tv;
3549
3550 // Catch a bad name early.
3551 if (!eval_isnamec1(*name))
3552 {
3553 emsg(_(e_invarg));
3554 return FAIL;
3555 }
3556
3557 // Make a copy of the name, it is used in redir_lval until redir ends.
3558 redir_varname = vim_strsave(name);
3559 if (redir_varname == NULL)
3560 return FAIL;
3561
3562 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3563 if (redir_lval == NULL)
3564 {
3565 var_redir_stop();
3566 return FAIL;
3567 }
3568
3569 // The output is stored in growarray "redir_ga" until redirection ends.
3570 ga_init2(&redir_ga, (int)sizeof(char), 500);
3571
3572 // Parse the variable name (can be a dict or list entry).
3573 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3574 FNE_CHECK_START);
3575 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3576 {
3577 clear_lval(redir_lval);
3578 if (redir_endp != NULL && *redir_endp != NUL)
3579 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003580 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003581 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003582 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003583 redir_endp = NULL; // don't store a value, only cleanup
3584 var_redir_stop();
3585 return FAIL;
3586 }
3587
3588 // check if we can write to the variable: set it to or append an empty
3589 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003590 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003591 tv.v_type = VAR_STRING;
3592 tv.vval.v_string = (char_u *)"";
3593 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003595 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003596 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003597 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003598 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003599 {
3600 redir_endp = NULL; // don't store a value, only cleanup
3601 var_redir_stop();
3602 return FAIL;
3603 }
3604
3605 return OK;
3606}
3607
3608/*
3609 * Append "value[value_len]" to the variable set by var_redir_start().
3610 * The actual appending is postponed until redirection ends, because the value
3611 * appended may in fact be the string we write to, changing it may cause freed
3612 * memory to be used:
3613 * :redir => foo
3614 * :let foo
3615 * :redir END
3616 */
3617 void
3618var_redir_str(char_u *value, int value_len)
3619{
3620 int len;
3621
3622 if (redir_lval == NULL)
3623 return;
3624
3625 if (value_len == -1)
3626 len = (int)STRLEN(value); // Append the entire string
3627 else
3628 len = value_len; // Append only "value_len" characters
3629
3630 if (ga_grow(&redir_ga, len) == OK)
3631 {
3632 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3633 redir_ga.ga_len += len;
3634 }
3635 else
3636 var_redir_stop();
3637}
3638
3639/*
3640 * Stop redirecting command output to a variable.
3641 * Frees the allocated memory.
3642 */
3643 void
3644var_redir_stop(void)
3645{
3646 typval_T tv;
3647
3648 if (EVALCMD_BUSY)
3649 {
3650 redir_lval = NULL;
3651 return;
3652 }
3653
3654 if (redir_lval != NULL)
3655 {
3656 // If there was no error: assign the text to the variable.
3657 if (redir_endp != NULL)
3658 {
3659 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3660 tv.v_type = VAR_STRING;
3661 tv.vval.v_string = redir_ga.ga_data;
3662 // Call get_lval() again, if it's inside a Dict or List it may
3663 // have changed.
3664 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3665 FALSE, FALSE, 0, FNE_CHECK_START);
3666 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003667 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003668 (char_u *)".");
3669 clear_lval(redir_lval);
3670 }
3671
3672 // free the collected output
3673 VIM_CLEAR(redir_ga.ga_data);
3674
3675 VIM_CLEAR(redir_lval);
3676 }
3677 VIM_CLEAR(redir_varname);
3678}
3679
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003680/*
3681 * "gettabvar()" function
3682 */
3683 void
3684f_gettabvar(typval_T *argvars, typval_T *rettv)
3685{
3686 win_T *oldcurwin;
3687 tabpage_T *tp, *oldtabpage;
3688 dictitem_T *v;
3689 char_u *varname;
3690 int done = FALSE;
3691
3692 rettv->v_type = VAR_STRING;
3693 rettv->vval.v_string = NULL;
3694
3695 varname = tv_get_string_chk(&argvars[1]);
3696 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3697 if (tp != NULL && varname != NULL)
3698 {
3699 // Set tp to be our tabpage, temporarily. Also set the window to the
3700 // first window in the tabpage, otherwise the window is not valid.
3701 if (switch_win(&oldcurwin, &oldtabpage,
3702 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3703 : tp->tp_firstwin, tp, TRUE) == OK)
3704 {
3705 // look up the variable
3706 // Let gettabvar({nr}, "") return the "t:" dictionary.
3707 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3708 if (v != NULL)
3709 {
3710 copy_tv(&v->di_tv, rettv);
3711 done = TRUE;
3712 }
3713 }
3714
3715 // restore previous notion of curwin
3716 restore_win(oldcurwin, oldtabpage, TRUE);
3717 }
3718
3719 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3720 copy_tv(&argvars[2], rettv);
3721}
3722
3723/*
3724 * "gettabwinvar()" function
3725 */
3726 void
3727f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3728{
3729 getwinvar(argvars, rettv, 1);
3730}
3731
3732/*
3733 * "getwinvar()" function
3734 */
3735 void
3736f_getwinvar(typval_T *argvars, typval_T *rettv)
3737{
3738 getwinvar(argvars, rettv, 0);
3739}
3740
3741/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003742 * "getbufvar()" function
3743 */
3744 void
3745f_getbufvar(typval_T *argvars, typval_T *rettv)
3746{
3747 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003748 char_u *varname;
3749 dictitem_T *v;
3750 int done = FALSE;
3751
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003752 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003753 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003754
3755 rettv->v_type = VAR_STRING;
3756 rettv->vval.v_string = NULL;
3757
3758 if (buf != NULL && varname != NULL)
3759 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003760 if (*varname == '&')
3761 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003762 buf_T *save_curbuf = curbuf;
3763
3764 // set curbuf to be our buf, temporarily
3765 curbuf = buf;
3766
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003767 if (varname[1] == NUL)
3768 {
3769 // get all buffer-local options in a dict
3770 dict_T *opts = get_winbuf_options(TRUE);
3771
3772 if (opts != NULL)
3773 {
3774 rettv_dict_set(rettv, opts);
3775 done = TRUE;
3776 }
3777 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003778 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003779 // buffer-local-option
3780 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003781
3782 // restore previous notion of curbuf
3783 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003784 }
3785 else
3786 {
3787 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003788 if (*varname == NUL)
3789 // Let getbufvar({nr}, "") return the "b:" dictionary.
3790 v = &buf->b_bufvar;
3791 else
3792 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3793 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003794 if (v != NULL)
3795 {
3796 copy_tv(&v->di_tv, rettv);
3797 done = TRUE;
3798 }
3799 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003800 }
3801
3802 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3803 // use the default value
3804 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003805}
3806
3807/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003808 * "settabvar()" function
3809 */
3810 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003811f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003812{
3813 tabpage_T *save_curtab;
3814 tabpage_T *tp;
3815 char_u *varname, *tabvarname;
3816 typval_T *varp;
3817
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003818 if (check_secure())
3819 return;
3820
3821 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3822 varname = tv_get_string_chk(&argvars[1]);
3823 varp = &argvars[2];
3824
3825 if (varname != NULL && varp != NULL && tp != NULL)
3826 {
3827 save_curtab = curtab;
3828 goto_tabpage_tp(tp, FALSE, FALSE);
3829
3830 tabvarname = alloc(STRLEN(varname) + 3);
3831 if (tabvarname != NULL)
3832 {
3833 STRCPY(tabvarname, "t:");
3834 STRCPY(tabvarname + 2, varname);
3835 set_var(tabvarname, varp, TRUE);
3836 vim_free(tabvarname);
3837 }
3838
3839 // Restore current tabpage
3840 if (valid_tabpage(save_curtab))
3841 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3842 }
3843}
3844
3845/*
3846 * "settabwinvar()" function
3847 */
3848 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003849f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003850{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003851 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003852}
3853
3854/*
3855 * "setwinvar()" function
3856 */
3857 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003858f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003859{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003860 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003861}
3862
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003863/*
3864 * "setbufvar()" function
3865 */
3866 void
3867f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3868{
3869 buf_T *buf;
3870 char_u *varname, *bufvarname;
3871 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003872
3873 if (check_secure())
3874 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003875 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003876 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003877 varp = &argvars[2];
3878
3879 if (buf != NULL && varname != NULL && varp != NULL)
3880 {
3881 if (*varname == '&')
3882 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003883 aco_save_T aco;
3884
3885 // set curbuf to be our buf, temporarily
3886 aucmd_prepbuf(&aco, buf);
3887
Bram Moolenaar191929b2020-08-19 21:20:49 +02003888 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003889
3890 // reset notion of buffer
3891 aucmd_restbuf(&aco);
3892 }
3893 else
3894 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003895 bufvarname = alloc(STRLEN(varname) + 3);
3896 if (bufvarname != NULL)
3897 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003898 buf_T *save_curbuf = curbuf;
3899
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003900 curbuf = buf;
3901 STRCPY(bufvarname, "b:");
3902 STRCPY(bufvarname + 2, varname);
3903 set_var(bufvarname, varp, TRUE);
3904 vim_free(bufvarname);
3905 curbuf = save_curbuf;
3906 }
3907 }
3908 }
3909}
3910
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003911/*
3912 * Get a callback from "arg". It can be a Funcref or a function name.
3913 * When "arg" is zero return an empty string.
3914 * "cb_name" is not allocated.
3915 * "cb_name" is set to NULL for an invalid argument.
3916 */
3917 callback_T
3918get_callback(typval_T *arg)
3919{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003920 callback_T res;
3921 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003922
3923 res.cb_free_name = FALSE;
3924 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3925 {
3926 res.cb_partial = arg->vval.v_partial;
3927 ++res.cb_partial->pt_refcount;
3928 res.cb_name = partial_name(res.cb_partial);
3929 }
3930 else
3931 {
3932 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003933 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3934 && isdigit(*arg->vval.v_string))
3935 r = FAIL;
3936 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003937 {
3938 // Note that we don't make a copy of the string.
3939 res.cb_name = arg->vval.v_string;
3940 func_ref(res.cb_name);
3941 }
3942 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003943 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003944 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003945 r = FAIL;
3946
3947 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003948 {
3949 emsg(_("E921: Invalid callback argument"));
3950 res.cb_name = NULL;
3951 }
3952 }
3953 return res;
3954}
3955
3956/*
3957 * Copy a callback into a typval_T.
3958 */
3959 void
3960put_callback(callback_T *cb, typval_T *tv)
3961{
3962 if (cb->cb_partial != NULL)
3963 {
3964 tv->v_type = VAR_PARTIAL;
3965 tv->vval.v_partial = cb->cb_partial;
3966 ++tv->vval.v_partial->pt_refcount;
3967 }
3968 else
3969 {
3970 tv->v_type = VAR_FUNC;
3971 tv->vval.v_string = vim_strsave(cb->cb_name);
3972 func_ref(cb->cb_name);
3973 }
3974}
3975
3976/*
3977 * Make a copy of "src" into "dest", allocating the function name if needed,
3978 * without incrementing the refcount.
3979 */
3980 void
3981set_callback(callback_T *dest, callback_T *src)
3982{
3983 if (src->cb_partial == NULL)
3984 {
3985 // just a function name, make a copy
3986 dest->cb_name = vim_strsave(src->cb_name);
3987 dest->cb_free_name = TRUE;
3988 }
3989 else
3990 {
3991 // cb_name is a pointer into cb_partial
3992 dest->cb_name = src->cb_name;
3993 dest->cb_free_name = FALSE;
3994 }
3995 dest->cb_partial = src->cb_partial;
3996}
3997
3998/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003999 * Copy callback from "src" to "dest", incrementing the refcounts.
4000 */
4001 void
4002copy_callback(callback_T *dest, callback_T *src)
4003{
4004 dest->cb_partial = src->cb_partial;
4005 if (dest->cb_partial != NULL)
4006 {
4007 dest->cb_name = src->cb_name;
4008 dest->cb_free_name = FALSE;
4009 ++dest->cb_partial->pt_refcount;
4010 }
4011 else
4012 {
4013 dest->cb_name = vim_strsave(src->cb_name);
4014 dest->cb_free_name = TRUE;
4015 func_ref(src->cb_name);
4016 }
4017}
4018
4019/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004020 * Unref/free "callback" returned by get_callback() or set_callback().
4021 */
4022 void
4023free_callback(callback_T *callback)
4024{
4025 if (callback->cb_partial != NULL)
4026 {
4027 partial_unref(callback->cb_partial);
4028 callback->cb_partial = NULL;
4029 }
4030 else if (callback->cb_name != NULL)
4031 func_unref(callback->cb_name);
4032 if (callback->cb_free_name)
4033 {
4034 vim_free(callback->cb_name);
4035 callback->cb_free_name = FALSE;
4036 }
4037 callback->cb_name = NULL;
4038}
4039
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004040#endif // FEAT_EVAL