blob: 3571169370c8f389a0687dc4985ac698d99cbaf9 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200176static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
217 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
218 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
222 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
223 set_vim_var_list(VV_ERRORS, list_alloc());
224 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
225
226 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
227 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
228 set_vim_var_nr(VV_NONE, VVAL_NONE);
229 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100230 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
233 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
234 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
235 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
236 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
237 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
238 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
239 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
240 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
241 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
242 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
243
244 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
245
Bram Moolenaar439c0362020-06-06 15:58:03 +0200246 // Default for v:register is not 0 but '"'. This is adjusted once the
247 // clipboard has been setup by calling reset_reg_var().
248 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200249}
250
251#if defined(EXITFREE) || defined(PROTO)
252/*
253 * Free all vim variables information on exit
254 */
255 void
256evalvars_clear(void)
257{
258 int i;
259 struct vimvar *p;
260
261 for (i = 0; i < VV_LEN; ++i)
262 {
263 p = &vimvars[i];
264 if (p->vv_di.di_tv.v_type == VAR_STRING)
265 VIM_CLEAR(p->vv_str);
266 else if (p->vv_di.di_tv.v_type == VAR_LIST)
267 {
268 list_unref(p->vv_list);
269 p->vv_list = NULL;
270 }
271 }
272 hash_clear(&vimvarht);
273 hash_init(&vimvarht); // garbage_collect() will access it
274 hash_clear(&compat_hashtab);
275
276 // global variables
277 vars_clear(&globvarht);
278
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100279 // Script-local variables. Clear all the variables here.
280 // The scriptvar_T is cleared later in free_scriptnames(), because a
281 // variable in one script might hold a reference to the whole scope of
282 // another script.
283 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285}
286#endif
287
288 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200289garbage_collect_globvars(int copyID)
290{
291 return set_ref_in_ht(&globvarht, copyID, NULL);
292}
293
294 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200295garbage_collect_vimvars(int copyID)
296{
297 return set_ref_in_ht(&vimvarht, copyID, NULL);
298}
299
300 int
301garbage_collect_scriptvars(int copyID)
302{
303 int i;
304 int abort = FALSE;
305
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100306 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200307 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
308
309 return abort;
310}
311
312/*
313 * Set an internal variable to a string value. Creates the variable if it does
314 * not already exist.
315 */
316 void
317set_internal_string_var(char_u *name, char_u *value)
318{
319 char_u *val;
320 typval_T *tvp;
321
322 val = vim_strsave(value);
323 if (val != NULL)
324 {
325 tvp = alloc_string_tv(val);
326 if (tvp != NULL)
327 {
328 set_var(name, tvp, FALSE);
329 free_tv(tvp);
330 }
331 }
332}
333
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200334 int
335eval_charconvert(
336 char_u *enc_from,
337 char_u *enc_to,
338 char_u *fname_from,
339 char_u *fname_to)
340{
341 int err = FALSE;
342
343 set_vim_var_string(VV_CC_FROM, enc_from, -1);
344 set_vim_var_string(VV_CC_TO, enc_to, -1);
345 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
346 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
347 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
348 err = TRUE;
349 set_vim_var_string(VV_CC_FROM, NULL, -1);
350 set_vim_var_string(VV_CC_TO, NULL, -1);
351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
352 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
353
354 if (err)
355 return FAIL;
356 return OK;
357}
358
359# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
360 int
361eval_printexpr(char_u *fname, char_u *args)
362{
363 int err = FALSE;
364
365 set_vim_var_string(VV_FNAME_IN, fname, -1);
366 set_vim_var_string(VV_CMDARG, args, -1);
367 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
368 err = TRUE;
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_CMDARG, NULL, -1);
371
372 if (err)
373 {
374 mch_remove(fname);
375 return FAIL;
376 }
377 return OK;
378}
379# endif
380
381# if defined(FEAT_DIFF) || defined(PROTO)
382 void
383eval_diff(
384 char_u *origfile,
385 char_u *newfile,
386 char_u *outfile)
387{
388 int err = FALSE;
389
390 set_vim_var_string(VV_FNAME_IN, origfile, -1);
391 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
392 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
393 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
394 set_vim_var_string(VV_FNAME_IN, NULL, -1);
395 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
396 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
397}
398
399 void
400eval_patch(
401 char_u *origfile,
402 char_u *difffile,
403 char_u *outfile)
404{
405 int err;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415# endif
416
417#if defined(FEAT_SPELL) || defined(PROTO)
418/*
419 * Evaluate an expression to a list with suggestions.
420 * For the "expr:" part of 'spellsuggest'.
421 * Returns NULL when there is an error.
422 */
423 list_T *
424eval_spell_expr(char_u *badword, char_u *expr)
425{
426 typval_T save_val;
427 typval_T rettv;
428 list_T *list = NULL;
429 char_u *p = skipwhite(expr);
430
431 // Set "v:val" to the bad word.
432 prepare_vimvar(VV_VAL, &save_val);
433 set_vim_var_string(VV_VAL, badword, -1);
434 if (p_verbose == 0)
435 ++emsg_off;
436
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200437 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438 {
439 if (rettv.v_type != VAR_LIST)
440 clear_tv(&rettv);
441 else
442 list = rettv.vval.v_list;
443 }
444
445 if (p_verbose == 0)
446 --emsg_off;
447 clear_tv(get_vim_var_tv(VV_VAL));
448 restore_vimvar(VV_VAL, &save_val);
449
450 return list;
451}
452
453/*
454 * "list" is supposed to contain two items: a word and a number. Return the
455 * word in "pp" and the number as the return value.
456 * Return -1 if anything isn't right.
457 * Used to get the good word and score from the eval_spell_expr() result.
458 */
459 int
460get_spellword(list_T *list, char_u **pp)
461{
462 listitem_T *li;
463
464 li = list->lv_first;
465 if (li == NULL)
466 return -1;
467 *pp = tv_get_string(&li->li_tv);
468
469 li = li->li_next;
470 if (li == NULL)
471 return -1;
472 return (int)tv_get_number(&li->li_tv);
473}
474#endif
475
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200476/*
477 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200478 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200479 * When not used yet add the variable to the v: hashtable.
480 */
481 void
482prepare_vimvar(int idx, typval_T *save_tv)
483{
484 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200485 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
488}
489
490/*
491 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200492 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493 * When no longer defined, remove the variable from the v: hashtable.
494 */
495 void
496restore_vimvar(int idx, typval_T *save_tv)
497{
498 hashitem_T *hi;
499
500 vimvars[idx].vv_tv = *save_tv;
501 if (vimvars[idx].vv_type == VAR_UNKNOWN)
502 {
503 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
504 if (HASHITEM_EMPTY(hi))
505 internal_error("restore_vimvar()");
506 else
507 hash_remove(&vimvarht, hi);
508 }
509}
510
511/*
512 * List Vim variables.
513 */
514 static void
515list_vim_vars(int *first)
516{
517 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
518}
519
520/*
521 * List script-local variables, if there is a script.
522 */
523 static void
524list_script_vars(int *first)
525{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200526 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200527 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
528 "s:", FALSE, first);
529}
530
531/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200532 * Get a list of lines from a HERE document. The here document is a list of
533 * lines surrounded by a marker.
534 * cmd << {marker}
535 * {line1}
536 * {line2}
537 * ....
538 * {marker}
539 *
540 * The {marker} is a string. If the optional 'trim' word is supplied before the
541 * marker, then the leading indentation before the lines (matching the
542 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200543 *
544 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
545 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
546 * missing, then '.' is accepted as a marker.
547 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200548 * Returns a List with {lines} or NULL.
549 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200551heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200552{
553 char_u *theline;
554 char_u *marker;
555 list_T *l;
556 char_u *p;
557 int marker_indent_len = 0;
558 int text_indent_len = 0;
559 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200560 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561
562 if (eap->getline == NULL)
563 {
564 emsg(_("E991: cannot use =<< here"));
565 return NULL;
566 }
567
568 // Check for the optional 'trim' word before the marker
569 cmd = skipwhite(cmd);
570 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
571 {
572 cmd = skipwhite(cmd + 4);
573
574 // Trim the indentation from all the lines in the here document.
575 // The amount of indentation trimmed is the same as the indentation of
576 // the first line after the :let command line. To find the end marker
577 // the indent of the :let command line is trimmed.
578 p = *eap->cmdlinep;
579 while (VIM_ISWHITE(*p))
580 {
581 p++;
582 marker_indent_len++;
583 }
584 text_indent_len = -1;
585 }
586
587 // The marker is the next word.
588 if (*cmd != NUL && *cmd != '"')
589 {
590 marker = skipwhite(cmd);
591 p = skiptowhite(marker);
592 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
593 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200594 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200595 return NULL;
596 }
597 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200598 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200599 {
600 emsg(_("E221: Marker cannot start with lower case letter"));
601 return NULL;
602 }
603 }
604 else
605 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200606 // When getting lines for an embedded script, if the marker is missing,
607 // accept '.' as the marker.
608 if (script_get)
609 marker = dot;
610 else
611 {
612 emsg(_("E172: Missing marker"));
613 return NULL;
614 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200615 }
616
617 l = list_alloc();
618 if (l == NULL)
619 return NULL;
620
621 for (;;)
622 {
623 int mi = 0;
624 int ti = 0;
625
626 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
627 if (theline == NULL)
628 {
629 semsg(_("E990: Missing end marker '%s'"), marker);
630 break;
631 }
632
633 // with "trim": skip the indent matching the :let line to find the
634 // marker
635 if (marker_indent_len > 0
636 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
637 mi = marker_indent_len;
638 if (STRCMP(marker, theline + mi) == 0)
639 {
640 vim_free(theline);
641 break;
642 }
643
644 if (text_indent_len == -1 && *theline != NUL)
645 {
646 // set the text indent from the first line.
647 p = theline;
648 text_indent_len = 0;
649 while (VIM_ISWHITE(*p))
650 {
651 p++;
652 text_indent_len++;
653 }
654 text_indent = vim_strnsave(theline, text_indent_len);
655 }
656 // with "trim": skip the indent matching the first line
657 if (text_indent != NULL)
658 for (ti = 0; ti < text_indent_len; ++ti)
659 if (theline[ti] != text_indent[ti])
660 break;
661
662 if (list_append_string(l, theline + ti, -1) == FAIL)
663 break;
664 vim_free(theline);
665 }
666 vim_free(text_indent);
667
668 return l;
669}
670
671/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200672 * Vim9 variable declaration:
673 * ":var name"
674 * ":var name: type"
675 * ":var name = expr"
676 * ":var name: type = expr"
677 * etc.
678 */
679 void
680ex_var(exarg_T *eap)
681{
682 if (!in_vim9script())
683 {
684 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
685 return;
686 }
687 ex_let(eap);
688}
689
690/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200691 * ":let" list all variable values
692 * ":let var1 var2" list variable values
693 * ":let var = expr" assignment command.
694 * ":let var += expr" assignment command.
695 * ":let var -= expr" assignment command.
696 * ":let var *= expr" assignment command.
697 * ":let var /= expr" assignment command.
698 * ":let var %= expr" assignment command.
699 * ":let var .= expr" assignment command.
700 * ":let var ..= expr" assignment command.
701 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100703 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200704 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200705 * ":final var = expr" assignment command.
706 * ":final [var1, var2] = expr" unpack list.
707 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200708 * ":const" list all variable values
709 * ":const var1 var2" list variable values
710 * ":const var = expr" assignment command.
711 * ":const [var1, var2] = expr" unpack list.
712 */
713 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200714ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200715{
716 char_u *arg = eap->arg;
717 char_u *expr = NULL;
718 typval_T rettv;
719 int i;
720 int var_count = 0;
721 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200722 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200723 char_u *argend;
724 int first = TRUE;
725 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200726 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200727 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200728 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200730 if (eap->cmdidx == CMD_final && !vim9script)
731 {
732 // In legacy Vim script ":final" is short for ":finally".
733 ex_finally(eap);
734 return;
735 }
736 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
737 // In legacy Vim script ":const" works like ":final".
738 eap->cmdidx = CMD_final;
739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 // detect Vim9 assignment without ":let" or ":const"
741 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200742 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200744 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200745 if (argend == NULL)
746 return;
747 if (argend > arg && argend[-1] == '.') // for var.='str'
748 --argend;
749 expr = skipwhite(argend);
750 concat = expr[0] == '.'
751 && ((expr[1] == '=' && current_sctx.sc_version < 2)
752 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200753 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
754 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200755 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200756 {
757 // ":let" without "=": list variables
758 if (*arg == '[')
759 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200760 else if (expr[0] == '.' && expr[1] == '=')
761 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200762 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200763 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200764 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200765 {
766 // Vim9 declaration ":let var: type"
767 arg = vim9_declare_scriptvar(eap, arg);
768 }
769 else
770 {
771 // ":let var1 var2" - list values
772 arg = list_arg_vars(eap, arg, &first);
773 }
774 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 else if (!eap->skip)
776 {
777 // ":let"
778 list_glob_vars(&first);
779 list_buf_vars(&first);
780 list_win_vars(&first);
781 list_tab_vars(&first);
782 list_script_vars(&first);
783 list_func_vars(&first);
784 list_vim_vars(&first);
785 }
786 eap->nextcmd = check_nextcmd(arg);
787 }
788 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
789 {
790 list_T *l;
791
792 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200793 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 if (l != NULL)
795 {
796 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200797 if (!eap->skip)
798 {
799 op[0] = '=';
800 op[1] = NUL;
801 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200803 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804 clear_tv(&rettv);
805 }
806 }
807 else
808 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200809 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200810 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200811
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200812 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200813 i = FAIL;
814 if (has_assign || concat)
815 {
816 op[0] = '=';
817 op[1] = NUL;
818 if (*expr != '=')
819 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200820 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200821 {
822 // +=, /=, etc. require an existing variable
823 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
824 i = FAIL;
825 }
826 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200827 {
828 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200829 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200830 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200831 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200832 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200833 ++len;
834 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200835 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200836 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200837 }
838 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200839 ++expr;
840
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200841 if (vim9script && (!VIM_ISWHITE(*argend)
842 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200843 {
844 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200845 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200846 i = FAIL;
847 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848
849 if (eap->skip)
850 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200851 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200852 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200853 if (getline_equal(eap->getline, eap->cookie, getsourceline))
854 {
855 evalarg.eval_getline = eap->getline;
856 evalarg.eval_cookie = eap->cookie;
857 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200858 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200859 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200860 if (eap->skip)
861 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200862 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200863 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200864 if (eap->skip)
865 {
866 if (i != FAIL)
867 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200868 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200869 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200870 {
871 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200872 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200873 clear_tv(&rettv);
874 }
875 }
876}
877
878/*
879 * Assign the typevalue "tv" to the variable or variables at "arg_start".
880 * Handles both "var" with any type and "[var, var; var]" with a list type.
881 * When "op" is not NULL it points to a string with characters that
882 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
883 * or concatenate.
884 * Returns OK or FAIL;
885 */
886 int
887ex_let_vars(
888 char_u *arg_start,
889 typval_T *tv,
890 int copy, // copy values from "tv", don't move
891 int semicolon, // from skip_var_list()
892 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200893 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200894 char_u *op)
895{
896 char_u *arg = arg_start;
897 list_T *l;
898 int i;
899 listitem_T *item;
900 typval_T ltv;
901
902 if (*arg != '[')
903 {
904 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200906 return FAIL;
907 return OK;
908 }
909
910 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
911 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
912 {
913 emsg(_(e_listreq));
914 return FAIL;
915 }
916
917 i = list_len(l);
918 if (semicolon == 0 && var_count < i)
919 {
920 emsg(_("E687: Less targets than List items"));
921 return FAIL;
922 }
923 if (var_count - semicolon > i)
924 {
925 emsg(_("E688: More targets than List items"));
926 return FAIL;
927 }
928
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200929 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200930 item = l->lv_first;
931 while (*arg != ']')
932 {
933 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200935 item = item->li_next;
936 if (arg == NULL)
937 return FAIL;
938
939 arg = skipwhite(arg);
940 if (*arg == ';')
941 {
942 // Put the rest of the list (may be empty) in the var after ';'.
943 // Create a new list for this.
944 l = list_alloc();
945 if (l == NULL)
946 return FAIL;
947 while (item != NULL)
948 {
949 list_append_tv(l, &item->li_tv);
950 item = item->li_next;
951 }
952
953 ltv.v_type = VAR_LIST;
954 ltv.v_lock = 0;
955 ltv.vval.v_list = l;
956 l->lv_refcount = 1;
957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200959 (char_u *)"]", op);
960 clear_tv(&ltv);
961 if (arg == NULL)
962 return FAIL;
963 break;
964 }
965 else if (*arg != ',' && *arg != ']')
966 {
967 internal_error("ex_let_vars()");
968 return FAIL;
969 }
970 }
971
972 return OK;
973}
974
975/*
976 * Skip over assignable variable "var" or list of variables "[var, var]".
977 * Used for ":let varvar = expr" and ":for varvar in expr".
978 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200979 * for "[var, var; var]" set "semicolon" to 1.
980 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 * Return NULL for an error.
982 */
983 char_u *
984skip_var_list(
985 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200988 int *semicolon,
989 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200990{
991 char_u *p, *s;
992
993 if (*arg == '[')
994 {
995 // "[var, var]": find the matching ']'.
996 p = arg;
997 for (;;)
998 {
999 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001000 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001 if (s == p)
1002 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001003 if (!silent)
1004 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005 return NULL;
1006 }
1007 ++*var_count;
1008
1009 p = skipwhite(s);
1010 if (*p == ']')
1011 break;
1012 else if (*p == ';')
1013 {
1014 if (*semicolon == 1)
1015 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001016 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 return NULL;
1018 }
1019 *semicolon = 1;
1020 }
1021 else if (*p != ',')
1022 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001023 if (!silent)
1024 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 return NULL;
1026 }
1027 }
1028 return p + 1;
1029 }
1030 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001032}
1033
1034/*
1035 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1036 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001039 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001041{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 char_u *end;
1043
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001044 if (*arg == '@' && arg[1] != NUL)
1045 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001048 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001050 // "a: type" is declaring variable "a" with a type, not "a:".
1051 if (end == arg + 2 && end[-1] == ':')
1052 --end;
1053 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001054 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 }
1056 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057}
1058
1059/*
1060 * List variables for hashtab "ht" with prefix "prefix".
1061 * If "empty" is TRUE also list NULL strings as empty strings.
1062 */
1063 void
1064list_hashtable_vars(
1065 hashtab_T *ht,
1066 char *prefix,
1067 int empty,
1068 int *first)
1069{
1070 hashitem_T *hi;
1071 dictitem_T *di;
1072 int todo;
1073 char_u buf[IOSIZE];
1074
1075 todo = (int)ht->ht_used;
1076 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1077 {
1078 if (!HASHITEM_EMPTY(hi))
1079 {
1080 --todo;
1081 di = HI2DI(hi);
1082
1083 // apply :filter /pat/ to variable name
1084 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1085 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1086 if (message_filtered(buf))
1087 continue;
1088
1089 if (empty || di->di_tv.v_type != VAR_STRING
1090 || di->di_tv.vval.v_string != NULL)
1091 list_one_var(di, prefix, first);
1092 }
1093 }
1094}
1095
1096/*
1097 * List global variables.
1098 */
1099 static void
1100list_glob_vars(int *first)
1101{
1102 list_hashtable_vars(&globvarht, "", TRUE, first);
1103}
1104
1105/*
1106 * List buffer variables.
1107 */
1108 static void
1109list_buf_vars(int *first)
1110{
1111 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1112}
1113
1114/*
1115 * List window variables.
1116 */
1117 static void
1118list_win_vars(int *first)
1119{
1120 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1121}
1122
1123/*
1124 * List tab page variables.
1125 */
1126 static void
1127list_tab_vars(int *first)
1128{
1129 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1130}
1131
1132/*
1133 * List variables in "arg".
1134 */
1135 static char_u *
1136list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1137{
1138 int error = FALSE;
1139 int len;
1140 char_u *name;
1141 char_u *name_start;
1142 char_u *arg_subsc;
1143 char_u *tofree;
1144 typval_T tv;
1145
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001146 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001147 {
1148 if (error || eap->skip)
1149 {
1150 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1151 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1152 {
1153 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001154 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001155 break;
1156 }
1157 }
1158 else
1159 {
1160 // get_name_len() takes care of expanding curly braces
1161 name_start = name = arg;
1162 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1163 if (len <= 0)
1164 {
1165 // This is mainly to keep test 49 working: when expanding
1166 // curly braces fails overrule the exception error message.
1167 if (len < 0 && !aborting())
1168 {
1169 emsg_severe = TRUE;
1170 semsg(_(e_invarg2), arg);
1171 break;
1172 }
1173 error = TRUE;
1174 }
1175 else
1176 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001177 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001178 if (tofree != NULL)
1179 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001180 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001181 error = TRUE;
1182 else
1183 {
1184 // handle d.key, l[idx], f(expr)
1185 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001186 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001187 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 error = TRUE;
1189 else
1190 {
1191 if (arg == arg_subsc && len == 2 && name[1] == ':')
1192 {
1193 switch (*name)
1194 {
1195 case 'g': list_glob_vars(first); break;
1196 case 'b': list_buf_vars(first); break;
1197 case 'w': list_win_vars(first); break;
1198 case 't': list_tab_vars(first); break;
1199 case 'v': list_vim_vars(first); break;
1200 case 's': list_script_vars(first); break;
1201 case 'l': list_func_vars(first); break;
1202 default:
1203 semsg(_("E738: Can't list variables for %s"), name);
1204 }
1205 }
1206 else
1207 {
1208 char_u numbuf[NUMBUFLEN];
1209 char_u *tf;
1210 int c;
1211 char_u *s;
1212
1213 s = echo_string(&tv, &tf, numbuf, 0);
1214 c = *arg;
1215 *arg = NUL;
1216 list_one_var_a("",
1217 arg == arg_subsc ? name : name_start,
1218 tv.v_type,
1219 s == NULL ? (char_u *)"" : s,
1220 first);
1221 *arg = c;
1222 vim_free(tf);
1223 }
1224 clear_tv(&tv);
1225 }
1226 }
1227 }
1228
1229 vim_free(tofree);
1230 }
1231
1232 arg = skipwhite(arg);
1233 }
1234
1235 return arg;
1236}
1237
1238/*
1239 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1240 * Returns a pointer to the char just after the var name.
1241 * Returns NULL if there is an error.
1242 */
1243 static char_u *
1244ex_let_one(
1245 char_u *arg, // points to variable name
1246 typval_T *tv, // value to assign to variable
1247 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001248 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001249 char_u *endchars, // valid chars after variable name or NULL
1250 char_u *op) // "+", "-", "." or NULL
1251{
1252 int c1;
1253 char_u *name;
1254 char_u *p;
1255 char_u *arg_end = NULL;
1256 int len;
1257 int opt_flags;
1258 char_u *tofree = NULL;
1259
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001260 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001261 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1262 {
1263 vim9_declare_error(arg);
1264 return NULL;
1265 }
1266
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001267 // ":let $VAR = expr": Set environment variable.
1268 if (*arg == '$')
1269 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001270 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 {
1272 emsg(_("E996: Cannot lock an environment variable"));
1273 return NULL;
1274 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001275
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001276 // Find the end of the name.
1277 ++arg;
1278 name = arg;
1279 len = get_env_len(&arg);
1280 if (len == 0)
1281 semsg(_(e_invarg2), name - 1);
1282 else
1283 {
1284 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1285 semsg(_(e_letwrong), op);
1286 else if (endchars != NULL
1287 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1288 emsg(_(e_letunexp));
1289 else if (!check_secure())
1290 {
1291 c1 = name[len];
1292 name[len] = NUL;
1293 p = tv_get_string_chk(tv);
1294 if (p != NULL && op != NULL && *op == '.')
1295 {
1296 int mustfree = FALSE;
1297 char_u *s = vim_getenv(name, &mustfree);
1298
1299 if (s != NULL)
1300 {
1301 p = tofree = concat_str(s, p);
1302 if (mustfree)
1303 vim_free(s);
1304 }
1305 }
1306 if (p != NULL)
1307 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001308 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001309 arg_end = arg;
1310 }
1311 name[len] = c1;
1312 vim_free(tofree);
1313 }
1314 }
1315 }
1316
1317 // ":let &option = expr": Set option value.
1318 // ":let &l:option = expr": Set local option value.
1319 // ":let &g:option = expr": Set global option value.
1320 else if (*arg == '&')
1321 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001322 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001323 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001325 return NULL;
1326 }
1327 // Find the end of the name.
1328 p = find_option_end(&arg, &opt_flags);
1329 if (p == NULL || (endchars != NULL
1330 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1331 emsg(_(e_letunexp));
1332 else
1333 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001334 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001335 int opt_type;
1336 long numval;
1337 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001338 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001339 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340
1341 c1 = *p;
1342 *p = NUL;
1343
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001344 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1345 if ((opt_type == 1 || opt_type == -1)
1346 && (tv->v_type != VAR_STRING || !in_vim9script()))
1347 // number, possibly hidden
1348 n = (long)tv_get_number(tv);
1349
1350 // Avoid setting a string option to the text "v:false" or similar.
1351 // In Vim9 script also don't convert a number to string.
1352 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1353 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1354 s = tv_get_string_chk(tv);
1355
1356 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001358 if ((opt_type == 1 && *op == '.')
1359 || (opt_type == 0 && *op != '.'))
1360 {
1361 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001362 failed = TRUE; // don't set the value
1363
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001364 }
1365 else
1366 {
1367 if (opt_type == 1) // number
1368 {
1369 switch (*op)
1370 {
1371 case '+': n = numval + n; break;
1372 case '-': n = numval - n; break;
1373 case '*': n = numval * n; break;
1374 case '/': n = (long)num_divide(numval, n); break;
1375 case '%': n = (long)num_modulus(numval, n); break;
1376 }
1377 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001378 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001379 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001380 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381 s = concat_str(stringval, s);
1382 vim_free(stringval);
1383 stringval = s;
1384 }
1385 }
1386 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001387
1388 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001390 if (opt_type != 0 || s != NULL)
1391 {
1392 set_option_value(arg, n, s, opt_flags);
1393 arg_end = p;
1394 }
1395 else
1396 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001397 }
1398 *p = c1;
1399 vim_free(stringval);
1400 }
1401 }
1402
1403 // ":let @r = expr": Set register contents.
1404 else if (*arg == '@')
1405 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001406 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001407 {
1408 emsg(_("E996: Cannot lock a register"));
1409 return NULL;
1410 }
1411 ++arg;
1412 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1413 semsg(_(e_letwrong), op);
1414 else if (endchars != NULL
1415 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1416 emsg(_(e_letunexp));
1417 else
1418 {
1419 char_u *ptofree = NULL;
1420 char_u *s;
1421
1422 p = tv_get_string_chk(tv);
1423 if (p != NULL && op != NULL && *op == '.')
1424 {
1425 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1426 if (s != NULL)
1427 {
1428 p = ptofree = concat_str(s, p);
1429 vim_free(s);
1430 }
1431 }
1432 if (p != NULL)
1433 {
1434 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1435 arg_end = arg + 1;
1436 }
1437 vim_free(ptofree);
1438 }
1439 }
1440
1441 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 // ":let {expr} = expr": Idem, name made with curly braces
1444 else if (eval_isnamec1(*arg) || *arg == '{')
1445 {
1446 lval_T lv;
1447
1448 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001449 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if (endchars != NULL && vim_strchr(endchars,
1452 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 emsg(_(e_letunexp));
1454 else
1455 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001457 arg_end = p;
1458 }
1459 }
1460 clear_lval(&lv);
1461 }
1462
1463 else
1464 semsg(_(e_invarg2), arg);
1465
1466 return arg_end;
1467}
1468
1469/*
1470 * ":unlet[!] var1 ... " command.
1471 */
1472 void
1473ex_unlet(exarg_T *eap)
1474{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001475 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476}
1477
1478/*
1479 * ":lockvar" and ":unlockvar" commands
1480 */
1481 void
1482ex_lockvar(exarg_T *eap)
1483{
1484 char_u *arg = eap->arg;
1485 int deep = 2;
1486
1487 if (eap->forceit)
1488 deep = -1;
1489 else if (vim_isdigit(*arg))
1490 {
1491 deep = getdigits(&arg);
1492 arg = skipwhite(arg);
1493 }
1494
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001495 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001496}
1497
1498/*
1499 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001500 * Also used for Vim9 script. "callback" is invoked as:
1501 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001502 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001503 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504ex_unletlock(
1505 exarg_T *eap,
1506 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001507 int deep,
1508 int glv_flags,
1509 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1510 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001511{
1512 char_u *arg = argstart;
1513 char_u *name_end;
1514 int error = FALSE;
1515 lval_T lv;
1516
1517 do
1518 {
1519 if (*arg == '$')
1520 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001521 lv.ll_name = arg;
1522 lv.ll_tv = NULL;
1523 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524 if (get_env_len(&arg) == 0)
1525 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001526 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001527 return;
1528 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001529 if (!error && !eap->skip
1530 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1531 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001532 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001533 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001534 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001535 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001536 // Parse the name and find the end.
1537 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1538 glv_flags, FNE_CHECK_START);
1539 if (lv.ll_name == NULL)
1540 error = TRUE; // error but continue parsing
1541 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1542 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001544 if (name_end != NULL)
1545 {
1546 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001547 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001548 }
1549 if (!(eap->skip || error))
1550 clear_lval(&lv);
1551 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001554 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001555 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001556 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001557
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001558 if (!eap->skip)
1559 clear_lval(&lv);
1560 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001561
1562 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001563 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564
1565 eap->nextcmd = check_nextcmd(arg);
1566}
1567
1568 static int
1569do_unlet_var(
1570 lval_T *lp,
1571 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001572 exarg_T *eap,
1573 int deep UNUSED,
1574 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001576 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577 int ret = OK;
1578 int cc;
1579
1580 if (lp->ll_tv == NULL)
1581 {
1582 cc = *name_end;
1583 *name_end = NUL;
1584
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001585 // Environment variable, normal name or expanded name.
1586 if (*lp->ll_name == '$')
1587 vim_unsetenv(lp->ll_name + 1);
1588 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001589 ret = FAIL;
1590 *name_end = cc;
1591 }
1592 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001593 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001595 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 return FAIL;
1597 else if (lp->ll_range)
1598 {
1599 listitem_T *li;
1600 listitem_T *ll_li = lp->ll_li;
1601 int ll_n1 = lp->ll_n1;
1602
1603 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1604 {
1605 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001606 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 return FAIL;
1608 ll_li = li;
1609 ++ll_n1;
1610 }
1611
1612 // Delete a range of List items.
1613 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1614 {
1615 li = lp->ll_li->li_next;
1616 listitem_remove(lp->ll_list, lp->ll_li);
1617 lp->ll_li = li;
1618 ++lp->ll_n1;
1619 }
1620 }
1621 else
1622 {
1623 if (lp->ll_list != NULL)
1624 // unlet a List item.
1625 listitem_remove(lp->ll_list, lp->ll_li);
1626 else
1627 // unlet a Dictionary item.
1628 dictitem_remove(lp->ll_dict, lp->ll_di);
1629 }
1630
1631 return ret;
1632}
1633
1634/*
1635 * "unlet" a variable. Return OK if it existed, FAIL if not.
1636 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1637 */
1638 int
1639do_unlet(char_u *name, int forceit)
1640{
1641 hashtab_T *ht;
1642 hashitem_T *hi;
1643 char_u *varname;
1644 dict_T *d;
1645 dictitem_T *di;
1646
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001647 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001648 return FAIL;
1649
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650 ht = find_var_ht(name, &varname);
1651 if (ht != NULL && *varname != NUL)
1652 {
1653 d = get_current_funccal_dict(ht);
1654 if (d == NULL)
1655 {
1656 if (ht == &globvarht)
1657 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001658 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001659 d = &vimvardict;
1660 else
1661 {
1662 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1663 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1664 }
1665 if (d == NULL)
1666 {
1667 internal_error("do_unlet()");
1668 return FAIL;
1669 }
1670 }
1671 hi = hash_find(ht, varname);
1672 if (HASHITEM_EMPTY(hi))
1673 hi = find_hi_in_scoped_ht(name, &ht);
1674 if (hi != NULL && !HASHITEM_EMPTY(hi))
1675 {
1676 di = HI2DI(hi);
1677 if (var_check_fixed(di->di_flags, name, FALSE)
1678 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001679 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680 return FAIL;
1681
1682 delete_var(ht, hi);
1683 return OK;
1684 }
1685 }
1686 if (forceit)
1687 return OK;
1688 semsg(_("E108: No such variable: \"%s\""), name);
1689 return FAIL;
1690}
1691
1692/*
1693 * Lock or unlock variable indicated by "lp".
1694 * "deep" is the levels to go (-1 for unlimited);
1695 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1696 */
1697 static int
1698do_lock_var(
1699 lval_T *lp,
1700 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001701 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001702 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001703 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001704{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001705 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001706 int ret = OK;
1707 int cc;
1708 dictitem_T *di;
1709
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001710 if (lp->ll_tv == NULL)
1711 {
1712 cc = *name_end;
1713 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001714 if (*lp->ll_name == '$')
1715 {
1716 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001717 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001718 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719 else
1720 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001721 // Normal name or expanded name.
1722 di = find_var(lp->ll_name, NULL, TRUE);
1723 if (di == NULL)
1724 ret = FAIL;
1725 else if ((di->di_flags & DI_FLAGS_FIX)
1726 && di->di_tv.v_type != VAR_DICT
1727 && di->di_tv.v_type != VAR_LIST)
1728 {
1729 // For historic reasons this error is not given for a list or
1730 // dict. E.g., the b: dict could be locked/unlocked.
1731 semsg(_(e_lock_unlock), lp->ll_name);
1732 ret = FAIL;
1733 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001735 {
1736 if (lock)
1737 di->di_flags |= DI_FLAGS_LOCK;
1738 else
1739 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001740 if (deep != 0)
1741 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001742 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 }
1744 *name_end = cc;
1745 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001746 else if (deep == 0)
1747 {
1748 // nothing to do
1749 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001750 else if (lp->ll_range)
1751 {
1752 listitem_T *li = lp->ll_li;
1753
1754 // (un)lock a range of List items.
1755 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1756 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001757 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001758 li = li->li_next;
1759 ++lp->ll_n1;
1760 }
1761 }
1762 else if (lp->ll_list != NULL)
1763 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001764 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001765 else
1766 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001767 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001768
1769 return ret;
1770}
1771
1772/*
1773 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001774 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1775 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001777 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001778item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779{
1780 static int recurse = 0;
1781 list_T *l;
1782 listitem_T *li;
1783 dict_T *d;
1784 blob_T *b;
1785 hashitem_T *hi;
1786 int todo;
1787
1788 if (recurse >= DICT_MAXNEST)
1789 {
1790 emsg(_("E743: variable nested too deep for (un)lock"));
1791 return;
1792 }
1793 if (deep == 0)
1794 return;
1795 ++recurse;
1796
1797 // lock/unlock the item itself
1798 if (lock)
1799 tv->v_lock |= VAR_LOCKED;
1800 else
1801 tv->v_lock &= ~VAR_LOCKED;
1802
1803 switch (tv->v_type)
1804 {
1805 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001806 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810 case VAR_STRING:
1811 case VAR_FUNC:
1812 case VAR_PARTIAL:
1813 case VAR_FLOAT:
1814 case VAR_SPECIAL:
1815 case VAR_JOB:
1816 case VAR_CHANNEL:
1817 break;
1818
1819 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001820 if ((b = tv->vval.v_blob) != NULL
1821 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001822 {
1823 if (lock)
1824 b->bv_lock |= VAR_LOCKED;
1825 else
1826 b->bv_lock &= ~VAR_LOCKED;
1827 }
1828 break;
1829 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001830 if ((l = tv->vval.v_list) != NULL
1831 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 {
1833 if (lock)
1834 l->lv_lock |= VAR_LOCKED;
1835 else
1836 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001837 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001838 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001839 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001840 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 }
1842 break;
1843 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001844 if ((d = tv->vval.v_dict) != NULL
1845 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 {
1847 if (lock)
1848 d->dv_lock |= VAR_LOCKED;
1849 else
1850 d->dv_lock &= ~VAR_LOCKED;
1851 if (deep < 0 || deep > 1)
1852 {
1853 // recursive: lock/unlock the items the List contains
1854 todo = (int)d->dv_hashtab.ht_used;
1855 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1856 {
1857 if (!HASHITEM_EMPTY(hi))
1858 {
1859 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001860 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1861 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 }
1863 }
1864 }
1865 }
1866 }
1867 --recurse;
1868}
1869
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001870#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1871/*
1872 * Delete all "menutrans_" variables.
1873 */
1874 void
1875del_menutrans_vars(void)
1876{
1877 hashitem_T *hi;
1878 int todo;
1879
1880 hash_lock(&globvarht);
1881 todo = (int)globvarht.ht_used;
1882 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1883 {
1884 if (!HASHITEM_EMPTY(hi))
1885 {
1886 --todo;
1887 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1888 delete_var(&globvarht, hi);
1889 }
1890 }
1891 hash_unlock(&globvarht);
1892}
1893#endif
1894
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001896 * Local string buffer for the next two functions to store a variable name
1897 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1898 * get_user_var_name().
1899 */
1900
1901static char_u *varnamebuf = NULL;
1902static int varnamebuflen = 0;
1903
1904/*
1905 * Function to concatenate a prefix and a variable name.
1906 */
1907 static char_u *
1908cat_prefix_varname(int prefix, char_u *name)
1909{
1910 int len;
1911
1912 len = (int)STRLEN(name) + 3;
1913 if (len > varnamebuflen)
1914 {
1915 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001916 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001917 varnamebuf = alloc(len);
1918 if (varnamebuf == NULL)
1919 {
1920 varnamebuflen = 0;
1921 return NULL;
1922 }
1923 varnamebuflen = len;
1924 }
1925 *varnamebuf = prefix;
1926 varnamebuf[1] = ':';
1927 STRCPY(varnamebuf + 2, name);
1928 return varnamebuf;
1929}
1930
1931/*
1932 * Function given to ExpandGeneric() to obtain the list of user defined
1933 * (global/buffer/window/built-in) variable names.
1934 */
1935 char_u *
1936get_user_var_name(expand_T *xp, int idx)
1937{
1938 static long_u gdone;
1939 static long_u bdone;
1940 static long_u wdone;
1941 static long_u tdone;
1942 static int vidx;
1943 static hashitem_T *hi;
1944 hashtab_T *ht;
1945
1946 if (idx == 0)
1947 {
1948 gdone = bdone = wdone = vidx = 0;
1949 tdone = 0;
1950 }
1951
1952 // Global variables
1953 if (gdone < globvarht.ht_used)
1954 {
1955 if (gdone++ == 0)
1956 hi = globvarht.ht_array;
1957 else
1958 ++hi;
1959 while (HASHITEM_EMPTY(hi))
1960 ++hi;
1961 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1962 return cat_prefix_varname('g', hi->hi_key);
1963 return hi->hi_key;
1964 }
1965
1966 // b: variables
1967 ht = &curbuf->b_vars->dv_hashtab;
1968 if (bdone < ht->ht_used)
1969 {
1970 if (bdone++ == 0)
1971 hi = ht->ht_array;
1972 else
1973 ++hi;
1974 while (HASHITEM_EMPTY(hi))
1975 ++hi;
1976 return cat_prefix_varname('b', hi->hi_key);
1977 }
1978
1979 // w: variables
1980 ht = &curwin->w_vars->dv_hashtab;
1981 if (wdone < ht->ht_used)
1982 {
1983 if (wdone++ == 0)
1984 hi = ht->ht_array;
1985 else
1986 ++hi;
1987 while (HASHITEM_EMPTY(hi))
1988 ++hi;
1989 return cat_prefix_varname('w', hi->hi_key);
1990 }
1991
1992 // t: variables
1993 ht = &curtab->tp_vars->dv_hashtab;
1994 if (tdone < ht->ht_used)
1995 {
1996 if (tdone++ == 0)
1997 hi = ht->ht_array;
1998 else
1999 ++hi;
2000 while (HASHITEM_EMPTY(hi))
2001 ++hi;
2002 return cat_prefix_varname('t', hi->hi_key);
2003 }
2004
2005 // v: variables
2006 if (vidx < VV_LEN)
2007 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2008
2009 VIM_CLEAR(varnamebuf);
2010 varnamebuflen = 0;
2011 return NULL;
2012}
2013
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002014 char *
2015get_var_special_name(int nr)
2016{
2017 switch (nr)
2018 {
2019 case VVAL_FALSE: return "v:false";
2020 case VVAL_TRUE: return "v:true";
2021 case VVAL_NONE: return "v:none";
2022 case VVAL_NULL: return "v:null";
2023 }
2024 internal_error("get_var_special_name()");
2025 return "42";
2026}
2027
2028/*
2029 * Returns the global variable dictionary
2030 */
2031 dict_T *
2032get_globvar_dict(void)
2033{
2034 return &globvardict;
2035}
2036
2037/*
2038 * Returns the global variable hash table
2039 */
2040 hashtab_T *
2041get_globvar_ht(void)
2042{
2043 return &globvarht;
2044}
2045
2046/*
2047 * Returns the v: variable dictionary
2048 */
2049 dict_T *
2050get_vimvar_dict(void)
2051{
2052 return &vimvardict;
2053}
2054
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002055/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002057 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 */
2059 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002060find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002062 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2063 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064
2065 if (di == NULL)
2066 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002067 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2069 return (int)(vv - vimvars);
2070}
2071
2072
2073/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002074 * Set type of v: variable to "type".
2075 */
2076 void
2077set_vim_var_type(int idx, vartype_T type)
2078{
2079 vimvars[idx].vv_type = type;
2080}
2081
2082/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002083 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002084 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002085 */
2086 void
2087set_vim_var_nr(int idx, varnumber_T val)
2088{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002089 vimvars[idx].vv_nr = val;
2090}
2091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 char *
2093get_vim_var_name(int idx)
2094{
2095 return vimvars[idx].vv_name;
2096}
2097
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002098/*
2099 * Get typval_T v: variable value.
2100 */
2101 typval_T *
2102get_vim_var_tv(int idx)
2103{
2104 return &vimvars[idx].vv_tv;
2105}
2106
2107/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002108 * Set v: variable to "tv". Only accepts the same type.
2109 * Takes over the value of "tv".
2110 */
2111 int
2112set_vim_var_tv(int idx, typval_T *tv)
2113{
2114 if (vimvars[idx].vv_type != tv->v_type)
2115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002116 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002117 clear_tv(tv);
2118 return FAIL;
2119 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002120 // VV_RO is also checked when compiling, but let's check here as well.
2121 if (vimvars[idx].vv_flags & VV_RO)
2122 {
2123 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2124 return FAIL;
2125 }
2126 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2127 {
2128 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2129 return FAIL;
2130 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002131 clear_tv(&vimvars[idx].vv_di.di_tv);
2132 vimvars[idx].vv_di.di_tv = *tv;
2133 return OK;
2134}
2135
2136/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002137 * Get number v: variable value.
2138 */
2139 varnumber_T
2140get_vim_var_nr(int idx)
2141{
2142 return vimvars[idx].vv_nr;
2143}
2144
2145/*
2146 * Get string v: variable value. Uses a static buffer, can only be used once.
2147 * If the String variable has never been set, return an empty string.
2148 * Never returns NULL;
2149 */
2150 char_u *
2151get_vim_var_str(int idx)
2152{
2153 return tv_get_string(&vimvars[idx].vv_tv);
2154}
2155
2156/*
2157 * Get List v: variable value. Caller must take care of reference count when
2158 * needed.
2159 */
2160 list_T *
2161get_vim_var_list(int idx)
2162{
2163 return vimvars[idx].vv_list;
2164}
2165
2166/*
2167 * Get Dict v: variable value. Caller must take care of reference count when
2168 * needed.
2169 */
2170 dict_T *
2171get_vim_var_dict(int idx)
2172{
2173 return vimvars[idx].vv_dict;
2174}
2175
2176/*
2177 * Set v:char to character "c".
2178 */
2179 void
2180set_vim_var_char(int c)
2181{
2182 char_u buf[MB_MAXBYTES + 1];
2183
2184 if (has_mbyte)
2185 buf[(*mb_char2bytes)(c, buf)] = NUL;
2186 else
2187 {
2188 buf[0] = c;
2189 buf[1] = NUL;
2190 }
2191 set_vim_var_string(VV_CHAR, buf, -1);
2192}
2193
2194/*
2195 * Set v:count to "count" and v:count1 to "count1".
2196 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2197 */
2198 void
2199set_vcount(
2200 long count,
2201 long count1,
2202 int set_prevcount)
2203{
2204 if (set_prevcount)
2205 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2206 vimvars[VV_COUNT].vv_nr = count;
2207 vimvars[VV_COUNT1].vv_nr = count1;
2208}
2209
2210/*
2211 * Save variables that might be changed as a side effect. Used when executing
2212 * a timer callback.
2213 */
2214 void
2215save_vimvars(vimvars_save_T *vvsave)
2216{
2217 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2218 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2219 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2220}
2221
2222/*
2223 * Restore variables saved by save_vimvars().
2224 */
2225 void
2226restore_vimvars(vimvars_save_T *vvsave)
2227{
2228 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2229 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2230 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2231}
2232
2233/*
2234 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2235 * value.
2236 */
2237 void
2238set_vim_var_string(
2239 int idx,
2240 char_u *val,
2241 int len) // length of "val" to use or -1 (whole string)
2242{
2243 clear_tv(&vimvars[idx].vv_di.di_tv);
2244 vimvars[idx].vv_type = VAR_STRING;
2245 if (val == NULL)
2246 vimvars[idx].vv_str = NULL;
2247 else if (len == -1)
2248 vimvars[idx].vv_str = vim_strsave(val);
2249 else
2250 vimvars[idx].vv_str = vim_strnsave(val, len);
2251}
2252
2253/*
2254 * Set List v: variable to "val".
2255 */
2256 void
2257set_vim_var_list(int idx, list_T *val)
2258{
2259 clear_tv(&vimvars[idx].vv_di.di_tv);
2260 vimvars[idx].vv_type = VAR_LIST;
2261 vimvars[idx].vv_list = val;
2262 if (val != NULL)
2263 ++val->lv_refcount;
2264}
2265
2266/*
2267 * Set Dictionary v: variable to "val".
2268 */
2269 void
2270set_vim_var_dict(int idx, dict_T *val)
2271{
2272 clear_tv(&vimvars[idx].vv_di.di_tv);
2273 vimvars[idx].vv_type = VAR_DICT;
2274 vimvars[idx].vv_dict = val;
2275 if (val != NULL)
2276 {
2277 ++val->dv_refcount;
2278 dict_set_items_ro(val);
2279 }
2280}
2281
2282/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002283 * Set the v:argv list.
2284 */
2285 void
2286set_argv_var(char **argv, int argc)
2287{
2288 list_T *l = list_alloc();
2289 int i;
2290
2291 if (l == NULL)
2292 getout(1);
2293 l->lv_lock = VAR_FIXED;
2294 for (i = 0; i < argc; ++i)
2295 {
2296 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2297 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002298 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002299 }
2300 set_vim_var_list(VV_ARGV, l);
2301}
2302
2303/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002304 * Reset v:register, taking the 'clipboard' setting into account.
2305 */
2306 void
2307reset_reg_var(void)
2308{
2309 int regname = 0;
2310
2311 // Adjust the register according to 'clipboard', so that when
2312 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2313#ifdef FEAT_CLIPBOARD
2314 adjust_clip_reg(&regname);
2315#endif
2316 set_reg_var(regname);
2317}
2318
2319/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002320 * Set v:register if needed.
2321 */
2322 void
2323set_reg_var(int c)
2324{
2325 char_u regname;
2326
2327 if (c == 0 || c == ' ')
2328 regname = '"';
2329 else
2330 regname = c;
2331 // Avoid free/alloc when the value is already right.
2332 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2333 set_vim_var_string(VV_REG, &regname, 1);
2334}
2335
2336/*
2337 * Get or set v:exception. If "oldval" == NULL, return the current value.
2338 * Otherwise, restore the value to "oldval" and return NULL.
2339 * Must always be called in pairs to save and restore v:exception! Does not
2340 * take care of memory allocations.
2341 */
2342 char_u *
2343v_exception(char_u *oldval)
2344{
2345 if (oldval == NULL)
2346 return vimvars[VV_EXCEPTION].vv_str;
2347
2348 vimvars[VV_EXCEPTION].vv_str = oldval;
2349 return NULL;
2350}
2351
2352/*
2353 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2354 * Otherwise, restore the value to "oldval" and return NULL.
2355 * Must always be called in pairs to save and restore v:throwpoint! Does not
2356 * take care of memory allocations.
2357 */
2358 char_u *
2359v_throwpoint(char_u *oldval)
2360{
2361 if (oldval == NULL)
2362 return vimvars[VV_THROWPOINT].vv_str;
2363
2364 vimvars[VV_THROWPOINT].vv_str = oldval;
2365 return NULL;
2366}
2367
2368/*
2369 * Set v:cmdarg.
2370 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2371 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2372 * Must always be called in pairs!
2373 */
2374 char_u *
2375set_cmdarg(exarg_T *eap, char_u *oldarg)
2376{
2377 char_u *oldval;
2378 char_u *newval;
2379 unsigned len;
2380
2381 oldval = vimvars[VV_CMDARG].vv_str;
2382 if (eap == NULL)
2383 {
2384 vim_free(oldval);
2385 vimvars[VV_CMDARG].vv_str = oldarg;
2386 return NULL;
2387 }
2388
2389 if (eap->force_bin == FORCE_BIN)
2390 len = 6;
2391 else if (eap->force_bin == FORCE_NOBIN)
2392 len = 8;
2393 else
2394 len = 0;
2395
2396 if (eap->read_edit)
2397 len += 7;
2398
2399 if (eap->force_ff != 0)
2400 len += 10; // " ++ff=unix"
2401 if (eap->force_enc != 0)
2402 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2403 if (eap->bad_char != 0)
2404 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2405
2406 newval = alloc(len + 1);
2407 if (newval == NULL)
2408 return NULL;
2409
2410 if (eap->force_bin == FORCE_BIN)
2411 sprintf((char *)newval, " ++bin");
2412 else if (eap->force_bin == FORCE_NOBIN)
2413 sprintf((char *)newval, " ++nobin");
2414 else
2415 *newval = NUL;
2416
2417 if (eap->read_edit)
2418 STRCAT(newval, " ++edit");
2419
2420 if (eap->force_ff != 0)
2421 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2422 eap->force_ff == 'u' ? "unix"
2423 : eap->force_ff == 'd' ? "dos"
2424 : "mac");
2425 if (eap->force_enc != 0)
2426 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2427 eap->cmd + eap->force_enc);
2428 if (eap->bad_char == BAD_KEEP)
2429 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2430 else if (eap->bad_char == BAD_DROP)
2431 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2432 else if (eap->bad_char != 0)
2433 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2434 vimvars[VV_CMDARG].vv_str = newval;
2435 return oldval;
2436}
2437
2438/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002439 * Get the value of internal variable "name".
2440 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2441 */
2442 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002443eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002444 char_u *name,
2445 int len, // length of "name"
2446 typval_T *rettv, // NULL when only checking existence
2447 dictitem_T **dip, // non-NULL when typval's dict item is needed
2448 int verbose, // may give error message
2449 int no_autoload) // do not use script autoloading
2450{
2451 int ret = OK;
2452 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002453 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002454 dictitem_T *v;
2455 int cc;
2456
2457 // truncate the name, so that we can use strcmp()
2458 cc = name[len];
2459 name[len] = NUL;
2460
2461 // Check for user-defined variables.
2462 v = find_var(name, NULL, no_autoload);
2463 if (v != NULL)
2464 {
2465 tv = &v->di_tv;
2466 if (dip != NULL)
2467 *dip = v;
2468 }
2469
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002470 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002472 imported_T *import;
2473 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2474
2475 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476
2477 // imported variable from another script
2478 if (import != NULL)
2479 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002480 if (import->imp_funcname != NULL)
2481 {
2482 foundFunc = TRUE;
2483 if (rettv != NULL)
2484 {
2485 rettv->v_type = VAR_FUNC;
2486 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2487 }
2488 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002489 else if (import->imp_all)
2490 {
2491 emsg("Sorry, 'import * as X' not implemented yet");
2492 ret = FAIL;
2493 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002494 else
2495 {
2496 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002497 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002499 tv = sv->sv_tv;
2500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002502 else if (in_vim9script())
2503 {
2504 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2505
2506 if (ufunc != NULL)
2507 {
2508 foundFunc = TRUE;
2509 if (rettv != NULL)
2510 {
2511 rettv->v_type = VAR_FUNC;
2512 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2513 }
2514 }
2515 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 }
2517
Bram Moolenaarc620c052020-07-08 15:16:19 +02002518 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002519 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002520 if (tv == NULL)
2521 {
2522 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002523 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002524 ret = FAIL;
2525 }
2526 else if (rettv != NULL)
2527 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002528 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002529
2530 name[len] = cc;
2531
2532 return ret;
2533}
2534
2535/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002536 * Check if variable "name[len]" is a local variable or an argument.
2537 * If so, "*eval_lavars_used" is set to TRUE.
2538 */
2539 void
2540check_vars(char_u *name, int len)
2541{
2542 int cc;
2543 char_u *varname;
2544 hashtab_T *ht;
2545
2546 if (eval_lavars_used == NULL)
2547 return;
2548
2549 // truncate the name, so that we can use strcmp()
2550 cc = name[len];
2551 name[len] = NUL;
2552
2553 ht = find_var_ht(name, &varname);
2554 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2555 {
2556 if (find_var(name, NULL, TRUE) != NULL)
2557 *eval_lavars_used = TRUE;
2558 }
2559
2560 name[len] = cc;
2561}
2562
2563/*
2564 * Find variable "name" in the list of variables.
2565 * Return a pointer to it if found, NULL if not found.
2566 * Careful: "a:0" variables don't have a name.
2567 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2568 * hashtab_T used.
2569 */
2570 dictitem_T *
2571find_var(char_u *name, hashtab_T **htp, int no_autoload)
2572{
2573 char_u *varname;
2574 hashtab_T *ht;
2575 dictitem_T *ret = NULL;
2576
2577 ht = find_var_ht(name, &varname);
2578 if (htp != NULL)
2579 *htp = ht;
2580 if (ht == NULL)
2581 return NULL;
2582 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2583 if (ret != NULL)
2584 return ret;
2585
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002586 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002587 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2588}
2589
2590/*
2591 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002592 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002593 * Returns NULL if not found.
2594 */
2595 dictitem_T *
2596find_var_in_ht(
2597 hashtab_T *ht,
2598 int htname,
2599 char_u *varname,
2600 int no_autoload)
2601{
2602 hashitem_T *hi;
2603
2604 if (*varname == NUL)
2605 {
2606 // Must be something like "s:", otherwise "ht" would be NULL.
2607 switch (htname)
2608 {
2609 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2610 case 'g': return &globvars_var;
2611 case 'v': return &vimvars_var;
2612 case 'b': return &curbuf->b_bufvar;
2613 case 'w': return &curwin->w_winvar;
2614 case 't': return &curtab->tp_winvar;
2615 case 'l': return get_funccal_local_var();
2616 case 'a': return get_funccal_args_var();
2617 }
2618 return NULL;
2619 }
2620
2621 hi = hash_find(ht, varname);
2622 if (HASHITEM_EMPTY(hi))
2623 {
2624 // For global variables we may try auto-loading the script. If it
2625 // worked find the variable again. Don't auto-load a script if it was
2626 // loaded already, otherwise it would be loaded every time when
2627 // checking if a function name is a Funcref variable.
2628 if (ht == &globvarht && !no_autoload)
2629 {
2630 // Note: script_autoload() may make "hi" invalid. It must either
2631 // be obtained again or not used.
2632 if (!script_autoload(varname, FALSE) || aborting())
2633 return NULL;
2634 hi = hash_find(ht, varname);
2635 }
2636 if (HASHITEM_EMPTY(hi))
2637 return NULL;
2638 }
2639 return HI2DI(hi);
2640}
2641
2642/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 * Get the script-local hashtab. NULL if not in a script context.
2644 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002645 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646get_script_local_ht(void)
2647{
2648 scid_T sid = current_sctx.sc_sid;
2649
Bram Moolenaare3d46852020-08-29 13:39:17 +02002650 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 return &SCRIPT_VARS(sid);
2652 return NULL;
2653}
2654
2655/*
2656 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002657 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002659 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2661{
2662 hashtab_T *ht = get_script_local_ht();
2663 char_u buffer[30];
2664 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002665 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 hashitem_T *hi;
2667
2668 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002669 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 if (len < sizeof(buffer) - 1)
2671 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002672 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 vim_strncpy(buffer, name, len);
2674 p = buffer;
2675 }
2676 else
2677 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002678 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002680 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 }
2682
2683 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002684 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685
2686 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002687 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2688 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689
2690 if (p != buffer)
2691 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002692 // Don't return "buffer", gcc complains.
2693 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694}
2695
2696/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002697 * Find the hashtab used for a variable name.
2698 * Return NULL if the name is not valid.
2699 * Set "varname" to the start of name without ':'.
2700 */
2701 hashtab_T *
2702find_var_ht(char_u *name, char_u **varname)
2703{
2704 hashitem_T *hi;
2705 hashtab_T *ht;
2706
2707 if (name[0] == NUL)
2708 return NULL;
2709 if (name[1] != ':')
2710 {
2711 // The name must not start with a colon or #.
2712 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2713 return NULL;
2714 *varname = name;
2715
2716 // "version" is "v:version" in all scopes if scriptversion < 3.
2717 // Same for a few other variables marked with VV_COMPAT.
2718 if (current_sctx.sc_version < 3)
2719 {
2720 hi = hash_find(&compat_hashtab, name);
2721 if (!HASHITEM_EMPTY(hi))
2722 return &compat_hashtab;
2723 }
2724
2725 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 if (ht != NULL)
2727 return ht; // local variable
2728
2729 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002730 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 {
2732 ht = get_script_local_ht();
2733 if (ht != NULL)
2734 return ht;
2735 }
2736
2737 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002738 }
2739 *varname = name + 2;
2740 if (*name == 'g') // global variable
2741 return &globvarht;
2742 // There must be no ':' or '#' in the rest of the name, unless g: is used
2743 if (vim_strchr(name + 2, ':') != NULL
2744 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2745 return NULL;
2746 if (*name == 'b') // buffer variable
2747 return &curbuf->b_vars->dv_hashtab;
2748 if (*name == 'w') // window variable
2749 return &curwin->w_vars->dv_hashtab;
2750 if (*name == 't') // tab page variable
2751 return &curtab->tp_vars->dv_hashtab;
2752 if (*name == 'v') // v: variable
2753 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002754 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002755 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002757 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 if (*name == 'a') // a: function argument
2759 return get_funccal_args_ht();
2760 if (*name == 'l') // l: local function variable
2761 return get_funccal_local_ht();
2762 }
2763 if (*name == 's') // script variable
2764 {
2765 ht = get_script_local_ht();
2766 if (ht != NULL)
2767 return ht;
2768 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002769 return NULL;
2770}
2771
2772/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002773 * Get the string value of a (global/local) variable.
2774 * Note: see tv_get_string() for how long the pointer remains valid.
2775 * Returns NULL when it doesn't exist.
2776 */
2777 char_u *
2778get_var_value(char_u *name)
2779{
2780 dictitem_T *v;
2781
2782 v = find_var(name, NULL, FALSE);
2783 if (v == NULL)
2784 return NULL;
2785 return tv_get_string(&v->di_tv);
2786}
2787
2788/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002789 * Allocate a new hashtab for a sourced script. It will be used while
2790 * sourcing this script and when executing functions defined in the script.
2791 */
2792 void
2793new_script_vars(scid_T id)
2794{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002795 scriptvar_T *sv;
2796
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002797 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2798 if (sv == NULL)
2799 return;
2800 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002801 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002802}
2803
2804/*
2805 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2806 * point to it.
2807 */
2808 void
2809init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2810{
2811 hash_init(&dict->dv_hashtab);
2812 dict->dv_lock = 0;
2813 dict->dv_scope = scope;
2814 dict->dv_refcount = DO_NOT_FREE_CNT;
2815 dict->dv_copyID = 0;
2816 dict_var->di_tv.vval.v_dict = dict;
2817 dict_var->di_tv.v_type = VAR_DICT;
2818 dict_var->di_tv.v_lock = VAR_FIXED;
2819 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2820 dict_var->di_key[0] = NUL;
2821}
2822
2823/*
2824 * Unreference a dictionary initialized by init_var_dict().
2825 */
2826 void
2827unref_var_dict(dict_T *dict)
2828{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002829 // Now the dict needs to be freed if no one else is using it, go back to
2830 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002831 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2832 dict_unref(dict);
2833}
2834
2835/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002836 * Clean up a list of internal variables.
2837 * Frees all allocated variables and the value they contain.
2838 * Clears hashtab "ht", does not free it.
2839 */
2840 void
2841vars_clear(hashtab_T *ht)
2842{
2843 vars_clear_ext(ht, TRUE);
2844}
2845
2846/*
2847 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2848 */
2849 void
2850vars_clear_ext(hashtab_T *ht, int free_val)
2851{
2852 int todo;
2853 hashitem_T *hi;
2854 dictitem_T *v;
2855
2856 hash_lock(ht);
2857 todo = (int)ht->ht_used;
2858 for (hi = ht->ht_array; todo > 0; ++hi)
2859 {
2860 if (!HASHITEM_EMPTY(hi))
2861 {
2862 --todo;
2863
2864 // Free the variable. Don't remove it from the hashtab,
2865 // ht_array might change then. hash_clear() takes care of it
2866 // later.
2867 v = HI2DI(hi);
2868 if (free_val)
2869 clear_tv(&v->di_tv);
2870 if (v->di_flags & DI_FLAGS_ALLOC)
2871 vim_free(v);
2872 }
2873 }
2874 hash_clear(ht);
2875 ht->ht_used = 0;
2876}
2877
2878/*
2879 * Delete a variable from hashtab "ht" at item "hi".
2880 * Clear the variable value and free the dictitem.
2881 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002882 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002883delete_var(hashtab_T *ht, hashitem_T *hi)
2884{
2885 dictitem_T *di = HI2DI(hi);
2886
2887 hash_remove(ht, hi);
2888 clear_tv(&di->di_tv);
2889 vim_free(di);
2890}
2891
2892/*
2893 * List the value of one internal variable.
2894 */
2895 static void
2896list_one_var(dictitem_T *v, char *prefix, int *first)
2897{
2898 char_u *tofree;
2899 char_u *s;
2900 char_u numbuf[NUMBUFLEN];
2901
2902 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2903 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2904 s == NULL ? (char_u *)"" : s, first);
2905 vim_free(tofree);
2906}
2907
2908 static void
2909list_one_var_a(
2910 char *prefix,
2911 char_u *name,
2912 int type,
2913 char_u *string,
2914 int *first) // when TRUE clear rest of screen and set to FALSE
2915{
2916 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2917 msg_start();
2918 msg_puts(prefix);
2919 if (name != NULL) // "a:" vars don't have a name stored
2920 msg_puts((char *)name);
2921 msg_putchar(' ');
2922 msg_advance(22);
2923 if (type == VAR_NUMBER)
2924 msg_putchar('#');
2925 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2926 msg_putchar('*');
2927 else if (type == VAR_LIST)
2928 {
2929 msg_putchar('[');
2930 if (*string == '[')
2931 ++string;
2932 }
2933 else if (type == VAR_DICT)
2934 {
2935 msg_putchar('{');
2936 if (*string == '{')
2937 ++string;
2938 }
2939 else
2940 msg_putchar(' ');
2941
2942 msg_outtrans(string);
2943
2944 if (type == VAR_FUNC || type == VAR_PARTIAL)
2945 msg_puts("()");
2946 if (*first)
2947 {
2948 msg_clr_eos();
2949 *first = FALSE;
2950 }
2951}
2952
2953/*
2954 * Set variable "name" to value in "tv".
2955 * If the variable already exists, the value is updated.
2956 * Otherwise the variable is created.
2957 */
2958 void
2959set_var(
2960 char_u *name,
2961 typval_T *tv,
2962 int copy) // make copy of value in "tv"
2963{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002964 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002965}
2966
2967/*
2968 * Set variable "name" to value in "tv".
2969 * If the variable already exists and "is_const" is FALSE the value is updated.
2970 * Otherwise the variable is created.
2971 */
2972 void
2973set_var_const(
2974 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002976 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002977 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002978 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002979{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002980 typval_T *tv = tv_arg;
2981 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002983 char_u *varname;
2984 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002986 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002987
2988 ht = find_var_ht(name, &varname);
2989 if (ht == NULL || *varname == NUL)
2990 {
2991 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002992 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002993 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002994 is_script_local = ht == get_script_local_ht();
2995
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002996 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002997 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002998 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002999 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003000 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003001 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003002 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003003 }
3004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003006
3007 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 if (di == NULL)
3009 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003010
3011 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003012 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003013 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003014
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003015 if (need_convert_to_bool(type, tv))
3016 {
3017 // Destination is a bool and the value is not, but it can be converted.
3018 CLEAR_FIELD(bool_tv);
3019 bool_tv.v_type = VAR_BOOL;
3020 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3021 tv = &bool_tv;
3022 }
3023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003027 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003028 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 {
3030 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003031 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 }
3033
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003034 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003036 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003037 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003038 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003039 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003040 }
3041
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003042 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003043 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003044 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003046
Bram Moolenaara187c432020-09-16 21:08:28 +02003047 // Check in this order for backwards compatibility:
3048 // - Whether the variable is read-only
3049 // - Whether the variable value is locked
3050 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003051 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003052 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3053 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003054 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 else
3057 // can only redefine once
3058 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003059
3060 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003061
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003063 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003064 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003065 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003067 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003068 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003069 if (copy || tv->v_type != VAR_STRING)
3070 {
3071 char_u *val = tv_get_string(tv);
3072
3073 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003074 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 if (di->di_tv.vval.v_string == NULL)
3076 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077 }
3078 else
3079 {
3080 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003082 tv->vval.v_string = NULL;
3083 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003084 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003085 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003087 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003089 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003091#ifdef FEAT_SEARCH_EXTRA
3092 else if (STRCMP(varname, "hlsearch") == 0)
3093 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095 redraw_all_later(SOME_VALID);
3096 }
3097#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003098 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003101 {
3102 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003103 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003104 }
3105 }
3106
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003108 }
3109 else // add a new variable
3110 {
3111 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003112 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003113 {
3114 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003115 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003116 }
3117
3118 // Make sure the variable name is valid.
3119 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003120 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3123 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003124 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125 STRCPY(di->di_key, varname);
3126 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003127 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003129 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003132 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 di->di_flags |= DI_FLAGS_LOCK;
3134
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003135 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003137 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138
3139 // Store a pointer to the typval_T, so that it can be found by
3140 // index instead of using a hastab lookup.
3141 if (ga_grow(&si->sn_var_vals, 1) == OK)
3142 {
3143 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3144 + si->sn_var_vals.ga_len;
3145 sv->sv_name = di->di_key;
3146 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003147 if (type == NULL)
3148 sv->sv_type = typval2type(tv, &si->sn_type_list);
3149 else
3150 sv->sv_type = type;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003151 sv->sv_const = (flags & ASSIGN_CONST);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 sv->sv_export = is_export;
3153 ++si->sn_var_vals.ga_len;
3154
3155 // let ex_export() know the export worked.
3156 is_export = FALSE;
3157 }
3158 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003159 }
3160
3161 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003163 else
3164 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 di->di_tv = *tv;
3166 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003167 init_tv(tv);
3168 }
3169
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003170 // ":const var = val" locks the value
3171 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003172 // Like :lockvar! name: lock the value and what it contains, but only
3173 // if the reference count is up to one. That locks only literal
3174 // values.
3175 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003176 return;
3177failed:
3178 if (!copy)
3179 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003180}
3181
3182/*
3183 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3184 * Also give an error message.
3185 */
3186 int
3187var_check_ro(int flags, char_u *name, int use_gettext)
3188{
3189 if (flags & DI_FLAGS_RO)
3190 {
3191 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3192 return TRUE;
3193 }
3194 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3195 {
3196 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3197 return TRUE;
3198 }
3199 return FALSE;
3200}
3201
3202/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003203 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3204 * Also give an error message.
3205 */
3206 int
3207var_check_lock(int flags, char_u *name, int use_gettext)
3208{
3209 if (flags & DI_FLAGS_LOCK)
3210 {
3211 semsg(_(e_variable_is_locked_str),
3212 use_gettext ? (char_u *)_(name) : name);
3213 return TRUE;
3214 }
3215 return FALSE;
3216}
3217
3218/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003219 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3220 * Also give an error message.
3221 */
3222 int
3223var_check_fixed(int flags, char_u *name, int use_gettext)
3224{
3225 if (flags & DI_FLAGS_FIX)
3226 {
3227 semsg(_("E795: Cannot delete variable %s"),
3228 use_gettext ? (char_u *)_(name) : name);
3229 return TRUE;
3230 }
3231 return FALSE;
3232}
3233
3234/*
3235 * Check if a funcref is assigned to a valid variable name.
3236 * Return TRUE and give an error if not.
3237 */
3238 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003239var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003240 char_u *name, // points to start of variable name
3241 int new_var) // TRUE when creating the variable
3242{
3243 // Allow for w: b: s: and t:.
3244 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3245 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3246 ? name[2] : name[0]))
3247 {
3248 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3249 name);
3250 return TRUE;
3251 }
3252 // Don't allow hiding a function. When "v" is not NULL we might be
3253 // assigning another function to the same var, the type is checked
3254 // below.
3255 if (new_var && function_exists(name, FALSE))
3256 {
3257 semsg(_("E705: Variable name conflicts with existing function: %s"),
3258 name);
3259 return TRUE;
3260 }
3261 return FALSE;
3262}
3263
3264/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003265 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3266 * value. Also give an error message, using "name" or _("name") when
3267 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003268 */
3269 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003270value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003271{
3272 if (lock & VAR_LOCKED)
3273 {
3274 semsg(_("E741: Value is locked: %s"),
3275 name == NULL ? (char_u *)_("Unknown")
3276 : use_gettext ? (char_u *)_(name)
3277 : name);
3278 return TRUE;
3279 }
3280 if (lock & VAR_FIXED)
3281 {
3282 semsg(_("E742: Cannot change value of %s"),
3283 name == NULL ? (char_u *)_("Unknown")
3284 : use_gettext ? (char_u *)_(name)
3285 : name);
3286 return TRUE;
3287 }
3288 return FALSE;
3289}
3290
3291/*
3292 * Check if a variable name is valid.
3293 * Return FALSE and give an error if not.
3294 */
3295 int
3296valid_varname(char_u *varname)
3297{
3298 char_u *p;
3299
3300 for (p = varname; *p != NUL; ++p)
3301 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3302 && *p != AUTOLOAD_CHAR)
3303 {
3304 semsg(_(e_illvar), varname);
3305 return FALSE;
3306 }
3307 return TRUE;
3308}
3309
3310/*
3311 * getwinvar() and gettabwinvar()
3312 */
3313 static void
3314getwinvar(
3315 typval_T *argvars,
3316 typval_T *rettv,
3317 int off) // 1 for gettabwinvar()
3318{
3319 win_T *win;
3320 char_u *varname;
3321 dictitem_T *v;
3322 tabpage_T *tp = NULL;
3323 int done = FALSE;
3324 win_T *oldcurwin;
3325 tabpage_T *oldtabpage;
3326 int need_switch_win;
3327
3328 if (off == 1)
3329 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3330 else
3331 tp = curtab;
3332 win = find_win_by_nr(&argvars[off], tp);
3333 varname = tv_get_string_chk(&argvars[off + 1]);
3334 ++emsg_off;
3335
3336 rettv->v_type = VAR_STRING;
3337 rettv->vval.v_string = NULL;
3338
3339 if (win != NULL && varname != NULL)
3340 {
3341 // Set curwin to be our win, temporarily. Also set the tabpage,
3342 // otherwise the window is not valid. Only do this when needed,
3343 // autocommands get blocked.
3344 need_switch_win = !(tp == curtab && win == curwin);
3345 if (!need_switch_win
3346 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3347 {
3348 if (*varname == '&')
3349 {
3350 if (varname[1] == NUL)
3351 {
3352 // get all window-local options in a dict
3353 dict_T *opts = get_winbuf_options(FALSE);
3354
3355 if (opts != NULL)
3356 {
3357 rettv_dict_set(rettv, opts);
3358 done = TRUE;
3359 }
3360 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003361 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003362 // window-local-option
3363 done = TRUE;
3364 }
3365 else
3366 {
3367 // Look up the variable.
3368 // Let getwinvar({nr}, "") return the "w:" dictionary.
3369 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3370 varname, FALSE);
3371 if (v != NULL)
3372 {
3373 copy_tv(&v->di_tv, rettv);
3374 done = TRUE;
3375 }
3376 }
3377 }
3378
3379 if (need_switch_win)
3380 // restore previous notion of curwin
3381 restore_win(oldcurwin, oldtabpage, TRUE);
3382 }
3383
3384 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3385 // use the default return value
3386 copy_tv(&argvars[off + 2], rettv);
3387
3388 --emsg_off;
3389}
3390
3391/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003392 * Set option "varname" to the value of "varp" for the current buffer/window.
3393 */
3394 static void
3395set_option_from_tv(char_u *varname, typval_T *varp)
3396{
3397 long numval = 0;
3398 char_u *strval;
3399 char_u nbuf[NUMBUFLEN];
3400 int error = FALSE;
3401
3402 if (!in_vim9script() || varp->v_type != VAR_STRING)
3403 numval = (long)tv_get_number_chk(varp, &error);
3404 strval = tv_get_string_buf_chk(varp, nbuf);
3405 if (!error && strval != NULL)
3406 set_option_value(varname, numval, strval, OPT_LOCAL);
3407}
3408
3409/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003410 * "setwinvar()" and "settabwinvar()" functions
3411 */
3412 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003413setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003414{
3415 win_T *win;
3416 win_T *save_curwin;
3417 tabpage_T *save_curtab;
3418 int need_switch_win;
3419 char_u *varname, *winvarname;
3420 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003421 tabpage_T *tp = NULL;
3422
3423 if (check_secure())
3424 return;
3425
3426 if (off == 1)
3427 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3428 else
3429 tp = curtab;
3430 win = find_win_by_nr(&argvars[off], tp);
3431 varname = tv_get_string_chk(&argvars[off + 1]);
3432 varp = &argvars[off + 2];
3433
3434 if (win != NULL && varname != NULL && varp != NULL)
3435 {
3436 need_switch_win = !(tp == curtab && win == curwin);
3437 if (!need_switch_win
3438 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3439 {
3440 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003441 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003442 else
3443 {
3444 winvarname = alloc(STRLEN(varname) + 3);
3445 if (winvarname != NULL)
3446 {
3447 STRCPY(winvarname, "w:");
3448 STRCPY(winvarname + 2, varname);
3449 set_var(winvarname, varp, TRUE);
3450 vim_free(winvarname);
3451 }
3452 }
3453 }
3454 if (need_switch_win)
3455 restore_win(save_curwin, save_curtab, TRUE);
3456 }
3457}
3458
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003459/*
3460 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3461 * v:option_type, and v:option_command.
3462 */
3463 void
3464reset_v_option_vars(void)
3465{
3466 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3467 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3468 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3469 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3470 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3471 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3472}
3473
3474/*
3475 * Add an assert error to v:errors.
3476 */
3477 void
3478assert_error(garray_T *gap)
3479{
3480 struct vimvar *vp = &vimvars[VV_ERRORS];
3481
3482 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003483 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003484 set_vim_var_list(VV_ERRORS, list_alloc());
3485 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3486}
3487
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003488 int
3489var_exists(char_u *var)
3490{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003491 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003492 char_u *name;
3493 char_u *tofree;
3494 typval_T tv;
3495 int len = 0;
3496 int n = FALSE;
3497
3498 // get_name_len() takes care of expanding curly braces
3499 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003500 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003501 if (len > 0)
3502 {
3503 if (tofree != NULL)
3504 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003505 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003506 if (n)
3507 {
3508 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003509 arg = skipwhite(arg);
3510 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003511 if (n)
3512 clear_tv(&tv);
3513 }
3514 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003515 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003516 n = FALSE;
3517
3518 vim_free(tofree);
3519 return n;
3520}
3521
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003522static lval_T *redir_lval = NULL;
3523#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3524static garray_T redir_ga; // only valid when redir_lval is not NULL
3525static char_u *redir_endp = NULL;
3526static char_u *redir_varname = NULL;
3527
3528/*
3529 * Start recording command output to a variable
3530 * When "append" is TRUE append to an existing variable.
3531 * Returns OK if successfully completed the setup. FAIL otherwise.
3532 */
3533 int
3534var_redir_start(char_u *name, int append)
3535{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003536 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003537 typval_T tv;
3538
3539 // Catch a bad name early.
3540 if (!eval_isnamec1(*name))
3541 {
3542 emsg(_(e_invarg));
3543 return FAIL;
3544 }
3545
3546 // Make a copy of the name, it is used in redir_lval until redir ends.
3547 redir_varname = vim_strsave(name);
3548 if (redir_varname == NULL)
3549 return FAIL;
3550
3551 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3552 if (redir_lval == NULL)
3553 {
3554 var_redir_stop();
3555 return FAIL;
3556 }
3557
3558 // The output is stored in growarray "redir_ga" until redirection ends.
3559 ga_init2(&redir_ga, (int)sizeof(char), 500);
3560
3561 // Parse the variable name (can be a dict or list entry).
3562 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3563 FNE_CHECK_START);
3564 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3565 {
3566 clear_lval(redir_lval);
3567 if (redir_endp != NULL && *redir_endp != NUL)
3568 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003569 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003570 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003571 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003572 redir_endp = NULL; // don't store a value, only cleanup
3573 var_redir_stop();
3574 return FAIL;
3575 }
3576
3577 // check if we can write to the variable: set it to or append an empty
3578 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003579 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003580 tv.v_type = VAR_STRING;
3581 tv.vval.v_string = (char_u *)"";
3582 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003584 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003586 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003587 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003588 {
3589 redir_endp = NULL; // don't store a value, only cleanup
3590 var_redir_stop();
3591 return FAIL;
3592 }
3593
3594 return OK;
3595}
3596
3597/*
3598 * Append "value[value_len]" to the variable set by var_redir_start().
3599 * The actual appending is postponed until redirection ends, because the value
3600 * appended may in fact be the string we write to, changing it may cause freed
3601 * memory to be used:
3602 * :redir => foo
3603 * :let foo
3604 * :redir END
3605 */
3606 void
3607var_redir_str(char_u *value, int value_len)
3608{
3609 int len;
3610
3611 if (redir_lval == NULL)
3612 return;
3613
3614 if (value_len == -1)
3615 len = (int)STRLEN(value); // Append the entire string
3616 else
3617 len = value_len; // Append only "value_len" characters
3618
3619 if (ga_grow(&redir_ga, len) == OK)
3620 {
3621 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3622 redir_ga.ga_len += len;
3623 }
3624 else
3625 var_redir_stop();
3626}
3627
3628/*
3629 * Stop redirecting command output to a variable.
3630 * Frees the allocated memory.
3631 */
3632 void
3633var_redir_stop(void)
3634{
3635 typval_T tv;
3636
3637 if (EVALCMD_BUSY)
3638 {
3639 redir_lval = NULL;
3640 return;
3641 }
3642
3643 if (redir_lval != NULL)
3644 {
3645 // If there was no error: assign the text to the variable.
3646 if (redir_endp != NULL)
3647 {
3648 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3649 tv.v_type = VAR_STRING;
3650 tv.vval.v_string = redir_ga.ga_data;
3651 // Call get_lval() again, if it's inside a Dict or List it may
3652 // have changed.
3653 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3654 FALSE, FALSE, 0, FNE_CHECK_START);
3655 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003657 (char_u *)".");
3658 clear_lval(redir_lval);
3659 }
3660
3661 // free the collected output
3662 VIM_CLEAR(redir_ga.ga_data);
3663
3664 VIM_CLEAR(redir_lval);
3665 }
3666 VIM_CLEAR(redir_varname);
3667}
3668
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003669/*
3670 * "gettabvar()" function
3671 */
3672 void
3673f_gettabvar(typval_T *argvars, typval_T *rettv)
3674{
3675 win_T *oldcurwin;
3676 tabpage_T *tp, *oldtabpage;
3677 dictitem_T *v;
3678 char_u *varname;
3679 int done = FALSE;
3680
3681 rettv->v_type = VAR_STRING;
3682 rettv->vval.v_string = NULL;
3683
3684 varname = tv_get_string_chk(&argvars[1]);
3685 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3686 if (tp != NULL && varname != NULL)
3687 {
3688 // Set tp to be our tabpage, temporarily. Also set the window to the
3689 // first window in the tabpage, otherwise the window is not valid.
3690 if (switch_win(&oldcurwin, &oldtabpage,
3691 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3692 : tp->tp_firstwin, tp, TRUE) == OK)
3693 {
3694 // look up the variable
3695 // Let gettabvar({nr}, "") return the "t:" dictionary.
3696 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3697 if (v != NULL)
3698 {
3699 copy_tv(&v->di_tv, rettv);
3700 done = TRUE;
3701 }
3702 }
3703
3704 // restore previous notion of curwin
3705 restore_win(oldcurwin, oldtabpage, TRUE);
3706 }
3707
3708 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3709 copy_tv(&argvars[2], rettv);
3710}
3711
3712/*
3713 * "gettabwinvar()" function
3714 */
3715 void
3716f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3717{
3718 getwinvar(argvars, rettv, 1);
3719}
3720
3721/*
3722 * "getwinvar()" function
3723 */
3724 void
3725f_getwinvar(typval_T *argvars, typval_T *rettv)
3726{
3727 getwinvar(argvars, rettv, 0);
3728}
3729
3730/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003731 * "getbufvar()" function
3732 */
3733 void
3734f_getbufvar(typval_T *argvars, typval_T *rettv)
3735{
3736 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003737 char_u *varname;
3738 dictitem_T *v;
3739 int done = FALSE;
3740
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003741 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003742 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003743
3744 rettv->v_type = VAR_STRING;
3745 rettv->vval.v_string = NULL;
3746
3747 if (buf != NULL && varname != NULL)
3748 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003749 if (*varname == '&')
3750 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003751 buf_T *save_curbuf = curbuf;
3752
3753 // set curbuf to be our buf, temporarily
3754 curbuf = buf;
3755
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003756 if (varname[1] == NUL)
3757 {
3758 // get all buffer-local options in a dict
3759 dict_T *opts = get_winbuf_options(TRUE);
3760
3761 if (opts != NULL)
3762 {
3763 rettv_dict_set(rettv, opts);
3764 done = TRUE;
3765 }
3766 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003767 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003768 // buffer-local-option
3769 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003770
3771 // restore previous notion of curbuf
3772 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003773 }
3774 else
3775 {
3776 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003777 if (*varname == NUL)
3778 // Let getbufvar({nr}, "") return the "b:" dictionary.
3779 v = &buf->b_bufvar;
3780 else
3781 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3782 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003783 if (v != NULL)
3784 {
3785 copy_tv(&v->di_tv, rettv);
3786 done = TRUE;
3787 }
3788 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003789 }
3790
3791 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3792 // use the default value
3793 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003794}
3795
3796/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003797 * "settabvar()" function
3798 */
3799 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003800f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003801{
3802 tabpage_T *save_curtab;
3803 tabpage_T *tp;
3804 char_u *varname, *tabvarname;
3805 typval_T *varp;
3806
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003807 if (check_secure())
3808 return;
3809
3810 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3811 varname = tv_get_string_chk(&argvars[1]);
3812 varp = &argvars[2];
3813
3814 if (varname != NULL && varp != NULL && tp != NULL)
3815 {
3816 save_curtab = curtab;
3817 goto_tabpage_tp(tp, FALSE, FALSE);
3818
3819 tabvarname = alloc(STRLEN(varname) + 3);
3820 if (tabvarname != NULL)
3821 {
3822 STRCPY(tabvarname, "t:");
3823 STRCPY(tabvarname + 2, varname);
3824 set_var(tabvarname, varp, TRUE);
3825 vim_free(tabvarname);
3826 }
3827
3828 // Restore current tabpage
3829 if (valid_tabpage(save_curtab))
3830 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3831 }
3832}
3833
3834/*
3835 * "settabwinvar()" function
3836 */
3837 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003838f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003839{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003840 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003841}
3842
3843/*
3844 * "setwinvar()" function
3845 */
3846 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003847f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003848{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003849 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003850}
3851
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003852/*
3853 * "setbufvar()" function
3854 */
3855 void
3856f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3857{
3858 buf_T *buf;
3859 char_u *varname, *bufvarname;
3860 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003861
3862 if (check_secure())
3863 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003864 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003865 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003866 varp = &argvars[2];
3867
3868 if (buf != NULL && varname != NULL && varp != NULL)
3869 {
3870 if (*varname == '&')
3871 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003872 aco_save_T aco;
3873
3874 // set curbuf to be our buf, temporarily
3875 aucmd_prepbuf(&aco, buf);
3876
Bram Moolenaar191929b2020-08-19 21:20:49 +02003877 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003878
3879 // reset notion of buffer
3880 aucmd_restbuf(&aco);
3881 }
3882 else
3883 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003884 bufvarname = alloc(STRLEN(varname) + 3);
3885 if (bufvarname != NULL)
3886 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003887 buf_T *save_curbuf = curbuf;
3888
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003889 curbuf = buf;
3890 STRCPY(bufvarname, "b:");
3891 STRCPY(bufvarname + 2, varname);
3892 set_var(bufvarname, varp, TRUE);
3893 vim_free(bufvarname);
3894 curbuf = save_curbuf;
3895 }
3896 }
3897 }
3898}
3899
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003900/*
3901 * Get a callback from "arg". It can be a Funcref or a function name.
3902 * When "arg" is zero return an empty string.
3903 * "cb_name" is not allocated.
3904 * "cb_name" is set to NULL for an invalid argument.
3905 */
3906 callback_T
3907get_callback(typval_T *arg)
3908{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003909 callback_T res;
3910 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003911
3912 res.cb_free_name = FALSE;
3913 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3914 {
3915 res.cb_partial = arg->vval.v_partial;
3916 ++res.cb_partial->pt_refcount;
3917 res.cb_name = partial_name(res.cb_partial);
3918 }
3919 else
3920 {
3921 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003922 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3923 && isdigit(*arg->vval.v_string))
3924 r = FAIL;
3925 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003926 {
3927 // Note that we don't make a copy of the string.
3928 res.cb_name = arg->vval.v_string;
3929 func_ref(res.cb_name);
3930 }
3931 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003932 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003933 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003934 r = FAIL;
3935
3936 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003937 {
3938 emsg(_("E921: Invalid callback argument"));
3939 res.cb_name = NULL;
3940 }
3941 }
3942 return res;
3943}
3944
3945/*
3946 * Copy a callback into a typval_T.
3947 */
3948 void
3949put_callback(callback_T *cb, typval_T *tv)
3950{
3951 if (cb->cb_partial != NULL)
3952 {
3953 tv->v_type = VAR_PARTIAL;
3954 tv->vval.v_partial = cb->cb_partial;
3955 ++tv->vval.v_partial->pt_refcount;
3956 }
3957 else
3958 {
3959 tv->v_type = VAR_FUNC;
3960 tv->vval.v_string = vim_strsave(cb->cb_name);
3961 func_ref(cb->cb_name);
3962 }
3963}
3964
3965/*
3966 * Make a copy of "src" into "dest", allocating the function name if needed,
3967 * without incrementing the refcount.
3968 */
3969 void
3970set_callback(callback_T *dest, callback_T *src)
3971{
3972 if (src->cb_partial == NULL)
3973 {
3974 // just a function name, make a copy
3975 dest->cb_name = vim_strsave(src->cb_name);
3976 dest->cb_free_name = TRUE;
3977 }
3978 else
3979 {
3980 // cb_name is a pointer into cb_partial
3981 dest->cb_name = src->cb_name;
3982 dest->cb_free_name = FALSE;
3983 }
3984 dest->cb_partial = src->cb_partial;
3985}
3986
3987/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003988 * Copy callback from "src" to "dest", incrementing the refcounts.
3989 */
3990 void
3991copy_callback(callback_T *dest, callback_T *src)
3992{
3993 dest->cb_partial = src->cb_partial;
3994 if (dest->cb_partial != NULL)
3995 {
3996 dest->cb_name = src->cb_name;
3997 dest->cb_free_name = FALSE;
3998 ++dest->cb_partial->pt_refcount;
3999 }
4000 else
4001 {
4002 dest->cb_name = vim_strsave(src->cb_name);
4003 dest->cb_free_name = TRUE;
4004 func_ref(src->cb_name);
4005 }
4006}
4007
4008/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004009 * Unref/free "callback" returned by get_callback() or set_callback().
4010 */
4011 void
4012free_callback(callback_T *callback)
4013{
4014 if (callback->cb_partial != NULL)
4015 {
4016 partial_unref(callback->cb_partial);
4017 callback->cb_partial = NULL;
4018 }
4019 else if (callback->cb_name != NULL)
4020 func_unref(callback->cb_name);
4021 if (callback->cb_free_name)
4022 {
4023 vim_free(callback->cb_name);
4024 callback->cb_free_name = FALSE;
4025 }
4026 callback->cb_name = NULL;
4027}
4028
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004029#endif // FEAT_EVAL