blob: 12bd42fa663aa6319c8b5b1dd3bc78ce7b2aa0ac [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 Moolenaare5cdf152019-08-29 22:09:46 +0200148};
149
150// shorthand
151#define vv_type vv_di.di_tv.v_type
152#define vv_nr vv_di.di_tv.vval.v_number
153#define vv_float vv_di.di_tv.vval.v_float
154#define vv_str vv_di.di_tv.vval.v_string
155#define vv_list vv_di.di_tv.vval.v_list
156#define vv_dict vv_di.di_tv.vval.v_dict
157#define vv_blob vv_di.di_tv.vval.v_blob
158#define vv_tv vv_di.di_tv
159
160static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200161static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vimvarht vimvardict.dv_hashtab
163
164// for VIM_VERSION_ defines
165#include "version.h"
166
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200167static void ex_let_const(exarg_T *eap, int is_const);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168static char_u *skip_var_one(char_u *arg, int include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200178static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200179static void list_one_var(dictitem_T *v, char *prefix, int *first);
180static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
181
182/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200183 * Initialize global and vim special variables
184 */
185 void
186evalvars_init(void)
187{
188 int i;
189 struct vimvar *p;
190
191 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
192 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
193 vimvardict.dv_lock = VAR_FIXED;
194 hash_init(&compat_hashtab);
195
196 for (i = 0; i < VV_LEN; ++i)
197 {
198 p = &vimvars[i];
199 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
200 {
201 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
202 getout(1);
203 }
204 STRCPY(p->vv_di.di_key, p->vv_name);
205 if (p->vv_flags & VV_RO)
206 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
207 else if (p->vv_flags & VV_RO_SBX)
208 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
209 else
210 p->vv_di.di_flags = DI_FLAGS_FIX;
211
212 // add to v: scope dict, unless the value is not always available
213 if (p->vv_type != VAR_UNKNOWN)
214 hash_add(&vimvarht, p->vv_di.di_key);
215 if (p->vv_flags & VV_COMPAT)
216 // add to compat scope dict
217 hash_add(&compat_hashtab, p->vv_di.di_key);
218 }
219 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
220 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
221
222 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
223 set_vim_var_nr(VV_HLSEARCH, 1L);
224 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
225 set_vim_var_list(VV_ERRORS, list_alloc());
226 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
227
228 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
229 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
230 set_vim_var_nr(VV_NONE, VVAL_NONE);
231 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100232 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200233
234 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
235 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
236 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
237 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
238 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
239 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
240 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
241 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
242 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
243 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
244 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
245
246 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
247
248 set_reg_var(0); // default for v:register is not 0 but '"'
249}
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
437 if (eval1(&p, &rettv, TRUE) == OK)
438 {
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 Moolenaar7ebcba62020-01-12 17:42:55 +0100526 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
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 {
594 emsg(_(e_trailing));
595 return NULL;
596 }
597 *p = NUL;
598 if (vim_islower(*marker))
599 {
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/*
672 * ":let" list all variable values
673 * ":let var1 var2" list variable values
674 * ":let var = expr" assignment command.
675 * ":let var += expr" assignment command.
676 * ":let var -= expr" assignment command.
677 * ":let var *= expr" assignment command.
678 * ":let var /= expr" assignment command.
679 * ":let var %= expr" assignment command.
680 * ":let var .= expr" assignment command.
681 * ":let var ..= expr" assignment command.
682 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100684 * ":let var: string" Vim9 declaration
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200685 */
686 void
687ex_let(exarg_T *eap)
688{
689 ex_let_const(eap, FALSE);
690}
691
692/*
693 * ":const" list all variable values
694 * ":const var1 var2" list variable values
695 * ":const var = expr" assignment command.
696 * ":const [var1, var2] = expr" unpack list.
697 */
698 void
699ex_const(exarg_T *eap)
700{
701 ex_let_const(eap, TRUE);
702}
703
704 static void
705ex_let_const(exarg_T *eap, int is_const)
706{
707 char_u *arg = eap->arg;
708 char_u *expr = NULL;
709 typval_T rettv;
710 int i;
711 int var_count = 0;
712 int semicolon = 0;
713 char_u op[2];
714 char_u *argend;
715 int first = TRUE;
716 int concat;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 int flags = is_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719 // detect Vim9 assignment without ":let" or ":const"
720 if (eap->arg == eap->cmd)
721 flags |= LET_NO_COMMAND;
722
723 argend = skip_var_list(arg, TRUE, &var_count, &semicolon);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200724 if (argend == NULL)
725 return;
726 if (argend > arg && argend[-1] == '.') // for var.='str'
727 --argend;
728 expr = skipwhite(argend);
729 concat = expr[0] == '.'
730 && ((expr[1] == '=' && current_sctx.sc_version < 2)
731 || (expr[1] == '.' && expr[2] == '='));
732 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
733 && expr[1] == '=') || concat))
734 {
735 // ":let" without "=": list variables
736 if (*arg == '[')
737 emsg(_(e_invarg));
738 else if (expr[0] == '.')
739 emsg(_("E985: .= is not supported with script version 2"));
740 else if (!ends_excmd(*arg))
741 // ":let var1 var2"
742 arg = list_arg_vars(eap, arg, &first);
743 else if (!eap->skip)
744 {
745 // ":let"
746 list_glob_vars(&first);
747 list_buf_vars(&first);
748 list_win_vars(&first);
749 list_tab_vars(&first);
750 list_script_vars(&first);
751 list_func_vars(&first);
752 list_vim_vars(&first);
753 }
754 eap->nextcmd = check_nextcmd(arg);
755 }
756 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
757 {
758 list_T *l;
759
760 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200761 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 if (l != NULL)
763 {
764 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200765 if (!eap->skip)
766 {
767 op[0] = '=';
768 op[1] = NUL;
769 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200771 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200772 clear_tv(&rettv);
773 }
774 }
775 else
776 {
777 op[0] = '=';
778 op[1] = NUL;
779 if (*expr != '=')
780 {
781 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
782 {
783 op[0] = *expr; // +=, -=, *=, /=, %= or .=
784 if (expr[0] == '.' && expr[1] == '.') // ..=
785 ++expr;
786 }
787 expr = skipwhite(expr + 2);
788 }
789 else
790 expr = skipwhite(expr + 1);
791
792 if (eap->skip)
793 ++emsg_skip;
794 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
795 if (eap->skip)
796 {
797 if (i != FAIL)
798 clear_tv(&rettv);
799 --emsg_skip;
800 }
801 else if (i != FAIL)
802 {
803 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 clear_tv(&rettv);
806 }
807 }
808}
809
810/*
811 * Assign the typevalue "tv" to the variable or variables at "arg_start".
812 * Handles both "var" with any type and "[var, var; var]" with a list type.
813 * When "op" is not NULL it points to a string with characters that
814 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
815 * or concatenate.
816 * Returns OK or FAIL;
817 */
818 int
819ex_let_vars(
820 char_u *arg_start,
821 typval_T *tv,
822 int copy, // copy values from "tv", don't move
823 int semicolon, // from skip_var_list()
824 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 char_u *op)
827{
828 char_u *arg = arg_start;
829 list_T *l;
830 int i;
831 listitem_T *item;
832 typval_T ltv;
833
834 if (*arg != '[')
835 {
836 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200838 return FAIL;
839 return OK;
840 }
841
842 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
843 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
844 {
845 emsg(_(e_listreq));
846 return FAIL;
847 }
848
849 i = list_len(l);
850 if (semicolon == 0 && var_count < i)
851 {
852 emsg(_("E687: Less targets than List items"));
853 return FAIL;
854 }
855 if (var_count - semicolon > i)
856 {
857 emsg(_("E688: More targets than List items"));
858 return FAIL;
859 }
860
Bram Moolenaar50985eb2020-01-27 22:09:39 +0100861 range_list_materialize(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200862 item = l->lv_first;
863 while (*arg != ']')
864 {
865 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200867 item = item->li_next;
868 if (arg == NULL)
869 return FAIL;
870
871 arg = skipwhite(arg);
872 if (*arg == ';')
873 {
874 // Put the rest of the list (may be empty) in the var after ';'.
875 // Create a new list for this.
876 l = list_alloc();
877 if (l == NULL)
878 return FAIL;
879 while (item != NULL)
880 {
881 list_append_tv(l, &item->li_tv);
882 item = item->li_next;
883 }
884
885 ltv.v_type = VAR_LIST;
886 ltv.v_lock = 0;
887 ltv.vval.v_list = l;
888 l->lv_refcount = 1;
889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200891 (char_u *)"]", op);
892 clear_tv(&ltv);
893 if (arg == NULL)
894 return FAIL;
895 break;
896 }
897 else if (*arg != ',' && *arg != ']')
898 {
899 internal_error("ex_let_vars()");
900 return FAIL;
901 }
902 }
903
904 return OK;
905}
906
907/*
908 * Skip over assignable variable "var" or list of variables "[var, var]".
909 * Used for ":let varvar = expr" and ":for varvar in expr".
910 * For "[var, var]" increment "*var_count" for each variable.
911 * for "[var, var; var]" set "semicolon".
912 * Return NULL for an error.
913 */
914 char_u *
915skip_var_list(
916 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200918 int *var_count,
919 int *semicolon)
920{
921 char_u *p, *s;
922
923 if (*arg == '[')
924 {
925 // "[var, var]": find the matching ']'.
926 p = arg;
927 for (;;)
928 {
929 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200931 if (s == p)
932 {
933 semsg(_(e_invarg2), p);
934 return NULL;
935 }
936 ++*var_count;
937
938 p = skipwhite(s);
939 if (*p == ']')
940 break;
941 else if (*p == ';')
942 {
943 if (*semicolon == 1)
944 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200945 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200946 return NULL;
947 }
948 *semicolon = 1;
949 }
950 else if (*p != ',')
951 {
952 semsg(_(e_invarg2), p);
953 return NULL;
954 }
955 }
956 return p + 1;
957 }
958 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960}
961
962/*
963 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
964 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200966 */
967 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 char_u *end;
971
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972 if (*arg == '@' && arg[1] != NUL)
973 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
977 && *end == ':')
978 {
979 end = skip_type(skipwhite(end + 1));
980 }
981 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200982}
983
984/*
985 * List variables for hashtab "ht" with prefix "prefix".
986 * If "empty" is TRUE also list NULL strings as empty strings.
987 */
988 void
989list_hashtable_vars(
990 hashtab_T *ht,
991 char *prefix,
992 int empty,
993 int *first)
994{
995 hashitem_T *hi;
996 dictitem_T *di;
997 int todo;
998 char_u buf[IOSIZE];
999
1000 todo = (int)ht->ht_used;
1001 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1002 {
1003 if (!HASHITEM_EMPTY(hi))
1004 {
1005 --todo;
1006 di = HI2DI(hi);
1007
1008 // apply :filter /pat/ to variable name
1009 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1010 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1011 if (message_filtered(buf))
1012 continue;
1013
1014 if (empty || di->di_tv.v_type != VAR_STRING
1015 || di->di_tv.vval.v_string != NULL)
1016 list_one_var(di, prefix, first);
1017 }
1018 }
1019}
1020
1021/*
1022 * List global variables.
1023 */
1024 static void
1025list_glob_vars(int *first)
1026{
1027 list_hashtable_vars(&globvarht, "", TRUE, first);
1028}
1029
1030/*
1031 * List buffer variables.
1032 */
1033 static void
1034list_buf_vars(int *first)
1035{
1036 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1037}
1038
1039/*
1040 * List window variables.
1041 */
1042 static void
1043list_win_vars(int *first)
1044{
1045 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1046}
1047
1048/*
1049 * List tab page variables.
1050 */
1051 static void
1052list_tab_vars(int *first)
1053{
1054 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1055}
1056
1057/*
1058 * List variables in "arg".
1059 */
1060 static char_u *
1061list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1062{
1063 int error = FALSE;
1064 int len;
1065 char_u *name;
1066 char_u *name_start;
1067 char_u *arg_subsc;
1068 char_u *tofree;
1069 typval_T tv;
1070
1071 while (!ends_excmd(*arg) && !got_int)
1072 {
1073 if (error || eap->skip)
1074 {
1075 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1076 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1077 {
1078 emsg_severe = TRUE;
1079 emsg(_(e_trailing));
1080 break;
1081 }
1082 }
1083 else
1084 {
1085 // get_name_len() takes care of expanding curly braces
1086 name_start = name = arg;
1087 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1088 if (len <= 0)
1089 {
1090 // This is mainly to keep test 49 working: when expanding
1091 // curly braces fails overrule the exception error message.
1092 if (len < 0 && !aborting())
1093 {
1094 emsg_severe = TRUE;
1095 semsg(_(e_invarg2), arg);
1096 break;
1097 }
1098 error = TRUE;
1099 }
1100 else
1101 {
1102 if (tofree != NULL)
1103 name = tofree;
1104 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1105 error = TRUE;
1106 else
1107 {
1108 // handle d.key, l[idx], f(expr)
1109 arg_subsc = arg;
1110 if (handle_subscript(&arg, &tv, TRUE, TRUE,
1111 name, &name) == FAIL)
1112 error = TRUE;
1113 else
1114 {
1115 if (arg == arg_subsc && len == 2 && name[1] == ':')
1116 {
1117 switch (*name)
1118 {
1119 case 'g': list_glob_vars(first); break;
1120 case 'b': list_buf_vars(first); break;
1121 case 'w': list_win_vars(first); break;
1122 case 't': list_tab_vars(first); break;
1123 case 'v': list_vim_vars(first); break;
1124 case 's': list_script_vars(first); break;
1125 case 'l': list_func_vars(first); break;
1126 default:
1127 semsg(_("E738: Can't list variables for %s"), name);
1128 }
1129 }
1130 else
1131 {
1132 char_u numbuf[NUMBUFLEN];
1133 char_u *tf;
1134 int c;
1135 char_u *s;
1136
1137 s = echo_string(&tv, &tf, numbuf, 0);
1138 c = *arg;
1139 *arg = NUL;
1140 list_one_var_a("",
1141 arg == arg_subsc ? name : name_start,
1142 tv.v_type,
1143 s == NULL ? (char_u *)"" : s,
1144 first);
1145 *arg = c;
1146 vim_free(tf);
1147 }
1148 clear_tv(&tv);
1149 }
1150 }
1151 }
1152
1153 vim_free(tofree);
1154 }
1155
1156 arg = skipwhite(arg);
1157 }
1158
1159 return arg;
1160}
1161
1162/*
1163 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1164 * Returns a pointer to the char just after the var name.
1165 * Returns NULL if there is an error.
1166 */
1167 static char_u *
1168ex_let_one(
1169 char_u *arg, // points to variable name
1170 typval_T *tv, // value to assign to variable
1171 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001173 char_u *endchars, // valid chars after variable name or NULL
1174 char_u *op) // "+", "-", "." or NULL
1175{
1176 int c1;
1177 char_u *name;
1178 char_u *p;
1179 char_u *arg_end = NULL;
1180 int len;
1181 int opt_flags;
1182 char_u *tofree = NULL;
1183
1184 // ":let $VAR = expr": Set environment variable.
1185 if (*arg == '$')
1186 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 {
1189 emsg(_("E996: Cannot lock an environment variable"));
1190 return NULL;
1191 }
1192 // Find the end of the name.
1193 ++arg;
1194 name = arg;
1195 len = get_env_len(&arg);
1196 if (len == 0)
1197 semsg(_(e_invarg2), name - 1);
1198 else
1199 {
1200 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1201 semsg(_(e_letwrong), op);
1202 else if (endchars != NULL
1203 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1204 emsg(_(e_letunexp));
1205 else if (!check_secure())
1206 {
1207 c1 = name[len];
1208 name[len] = NUL;
1209 p = tv_get_string_chk(tv);
1210 if (p != NULL && op != NULL && *op == '.')
1211 {
1212 int mustfree = FALSE;
1213 char_u *s = vim_getenv(name, &mustfree);
1214
1215 if (s != NULL)
1216 {
1217 p = tofree = concat_str(s, p);
1218 if (mustfree)
1219 vim_free(s);
1220 }
1221 }
1222 if (p != NULL)
1223 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001224 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001225 arg_end = arg;
1226 }
1227 name[len] = c1;
1228 vim_free(tofree);
1229 }
1230 }
1231 }
1232
1233 // ":let &option = expr": Set option value.
1234 // ":let &l:option = expr": Set local option value.
1235 // ":let &g:option = expr": Set global option value.
1236 else if (*arg == '&')
1237 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001241 return NULL;
1242 }
1243 // Find the end of the name.
1244 p = find_option_end(&arg, &opt_flags);
1245 if (p == NULL || (endchars != NULL
1246 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1247 emsg(_(e_letunexp));
1248 else
1249 {
1250 long n;
1251 int opt_type;
1252 long numval;
1253 char_u *stringval = NULL;
1254 char_u *s;
1255
1256 c1 = *p;
1257 *p = NUL;
1258
1259 n = (long)tv_get_number(tv);
1260 s = tv_get_string_chk(tv); // != NULL if number or string
1261 if (s != NULL && op != NULL && *op != '=')
1262 {
1263 opt_type = get_option_value(arg, &numval,
1264 &stringval, opt_flags);
1265 if ((opt_type == 1 && *op == '.')
1266 || (opt_type == 0 && *op != '.'))
1267 {
1268 semsg(_(e_letwrong), op);
1269 s = NULL; // don't set the value
1270 }
1271 else
1272 {
1273 if (opt_type == 1) // number
1274 {
1275 switch (*op)
1276 {
1277 case '+': n = numval + n; break;
1278 case '-': n = numval - n; break;
1279 case '*': n = numval * n; break;
1280 case '/': n = (long)num_divide(numval, n); break;
1281 case '%': n = (long)num_modulus(numval, n); break;
1282 }
1283 }
1284 else if (opt_type == 0 && stringval != NULL) // string
1285 {
1286 s = concat_str(stringval, s);
1287 vim_free(stringval);
1288 stringval = s;
1289 }
1290 }
1291 }
1292 if (s != NULL)
1293 {
1294 set_option_value(arg, n, s, opt_flags);
1295 arg_end = p;
1296 }
1297 *p = c1;
1298 vim_free(stringval);
1299 }
1300 }
1301
1302 // ":let @r = expr": Set register contents.
1303 else if (*arg == '@')
1304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001306 {
1307 emsg(_("E996: Cannot lock a register"));
1308 return NULL;
1309 }
1310 ++arg;
1311 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1312 semsg(_(e_letwrong), op);
1313 else if (endchars != NULL
1314 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1315 emsg(_(e_letunexp));
1316 else
1317 {
1318 char_u *ptofree = NULL;
1319 char_u *s;
1320
1321 p = tv_get_string_chk(tv);
1322 if (p != NULL && op != NULL && *op == '.')
1323 {
1324 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1325 if (s != NULL)
1326 {
1327 p = ptofree = concat_str(s, p);
1328 vim_free(s);
1329 }
1330 }
1331 if (p != NULL)
1332 {
1333 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1334 arg_end = arg + 1;
1335 }
1336 vim_free(ptofree);
1337 }
1338 }
1339
1340 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 // ":let {expr} = expr": Idem, name made with curly braces
1343 else if (eval_isnamec1(*arg) || *arg == '{')
1344 {
1345 lval_T lv;
1346
1347 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
1348 if (p != NULL && lv.ll_name != NULL)
1349 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if (endchars != NULL && vim_strchr(endchars,
1351 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 emsg(_(e_letunexp));
1353 else
1354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001356 arg_end = p;
1357 }
1358 }
1359 clear_lval(&lv);
1360 }
1361
1362 else
1363 semsg(_(e_invarg2), arg);
1364
1365 return arg_end;
1366}
1367
1368/*
1369 * ":unlet[!] var1 ... " command.
1370 */
1371 void
1372ex_unlet(exarg_T *eap)
1373{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001374 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375}
1376
1377/*
1378 * ":lockvar" and ":unlockvar" commands
1379 */
1380 void
1381ex_lockvar(exarg_T *eap)
1382{
1383 char_u *arg = eap->arg;
1384 int deep = 2;
1385
1386 if (eap->forceit)
1387 deep = -1;
1388 else if (vim_isdigit(*arg))
1389 {
1390 deep = getdigits(&arg);
1391 arg = skipwhite(arg);
1392 }
1393
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001394 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001395}
1396
1397/*
1398 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001399 * Also used for Vim9 script. "callback" is invoked as:
1400 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001402 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403ex_unletlock(
1404 exarg_T *eap,
1405 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001406 int deep,
1407 int glv_flags,
1408 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1409 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001410{
1411 char_u *arg = argstart;
1412 char_u *name_end;
1413 int error = FALSE;
1414 lval_T lv;
1415
1416 do
1417 {
1418 if (*arg == '$')
1419 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001420 lv.ll_name = arg;
1421 lv.ll_tv = NULL;
1422 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001423 if (get_env_len(&arg) == 0)
1424 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001425 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 return;
1427 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001428 if (!error && !eap->skip
1429 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1430 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001431 arg = skipwhite(arg);
1432 continue;
1433 }
1434
1435 // Parse the name and find the end.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001436 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1437 glv_flags, FNE_CHECK_START);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001438 if (lv.ll_name == NULL)
1439 error = TRUE; // error but continue parsing
1440 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1441 && !ends_excmd(*name_end)))
1442 {
1443 if (name_end != NULL)
1444 {
1445 emsg_severe = TRUE;
1446 emsg(_(e_trailing));
1447 }
1448 if (!(eap->skip || error))
1449 clear_lval(&lv);
1450 break;
1451 }
1452
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001453 if (!error && !eap->skip
1454 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
1455 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456
1457 if (!eap->skip)
1458 clear_lval(&lv);
1459
1460 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001461 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462
1463 eap->nextcmd = check_nextcmd(arg);
1464}
1465
1466 static int
1467do_unlet_var(
1468 lval_T *lp,
1469 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001470 exarg_T *eap,
1471 int deep UNUSED,
1472 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001474 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001475 int ret = OK;
1476 int cc;
1477
1478 if (lp->ll_tv == NULL)
1479 {
1480 cc = *name_end;
1481 *name_end = NUL;
1482
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001483 // Environment variable, normal name or expanded name.
1484 if (*lp->ll_name == '$')
1485 vim_unsetenv(lp->ll_name + 1);
1486 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001487 ret = FAIL;
1488 *name_end = cc;
1489 }
1490 else if ((lp->ll_list != NULL
1491 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1492 || (lp->ll_dict != NULL
1493 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1494 return FAIL;
1495 else if (lp->ll_range)
1496 {
1497 listitem_T *li;
1498 listitem_T *ll_li = lp->ll_li;
1499 int ll_n1 = lp->ll_n1;
1500
1501 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1502 {
1503 li = ll_li->li_next;
1504 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1505 return FAIL;
1506 ll_li = li;
1507 ++ll_n1;
1508 }
1509
1510 // Delete a range of List items.
1511 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1512 {
1513 li = lp->ll_li->li_next;
1514 listitem_remove(lp->ll_list, lp->ll_li);
1515 lp->ll_li = li;
1516 ++lp->ll_n1;
1517 }
1518 }
1519 else
1520 {
1521 if (lp->ll_list != NULL)
1522 // unlet a List item.
1523 listitem_remove(lp->ll_list, lp->ll_li);
1524 else
1525 // unlet a Dictionary item.
1526 dictitem_remove(lp->ll_dict, lp->ll_di);
1527 }
1528
1529 return ret;
1530}
1531
1532/*
1533 * "unlet" a variable. Return OK if it existed, FAIL if not.
1534 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1535 */
1536 int
1537do_unlet(char_u *name, int forceit)
1538{
1539 hashtab_T *ht;
1540 hashitem_T *hi;
1541 char_u *varname;
1542 dict_T *d;
1543 dictitem_T *di;
1544
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001545 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1546 && check_vim9_unlet(name) == FAIL)
1547 return FAIL;
1548
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001549 ht = find_var_ht(name, &varname);
1550 if (ht != NULL && *varname != NUL)
1551 {
1552 d = get_current_funccal_dict(ht);
1553 if (d == NULL)
1554 {
1555 if (ht == &globvarht)
1556 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001557 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001558 d = &vimvardict;
1559 else
1560 {
1561 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1562 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1563 }
1564 if (d == NULL)
1565 {
1566 internal_error("do_unlet()");
1567 return FAIL;
1568 }
1569 }
1570 hi = hash_find(ht, varname);
1571 if (HASHITEM_EMPTY(hi))
1572 hi = find_hi_in_scoped_ht(name, &ht);
1573 if (hi != NULL && !HASHITEM_EMPTY(hi))
1574 {
1575 di = HI2DI(hi);
1576 if (var_check_fixed(di->di_flags, name, FALSE)
1577 || var_check_ro(di->di_flags, name, FALSE)
1578 || var_check_lock(d->dv_lock, name, FALSE))
1579 return FAIL;
1580
1581 delete_var(ht, hi);
1582 return OK;
1583 }
1584 }
1585 if (forceit)
1586 return OK;
1587 semsg(_("E108: No such variable: \"%s\""), name);
1588 return FAIL;
1589}
1590
1591/*
1592 * Lock or unlock variable indicated by "lp".
1593 * "deep" is the levels to go (-1 for unlimited);
1594 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1595 */
1596 static int
1597do_lock_var(
1598 lval_T *lp,
1599 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001600 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001602 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001604 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605 int ret = OK;
1606 int cc;
1607 dictitem_T *di;
1608
1609 if (deep == 0) // nothing to do
1610 return OK;
1611
1612 if (lp->ll_tv == NULL)
1613 {
1614 cc = *name_end;
1615 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001616 if (*lp->ll_name == '$')
1617 {
1618 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001620 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 else
1622 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001623 // Normal name or expanded name.
1624 di = find_var(lp->ll_name, NULL, TRUE);
1625 if (di == NULL)
1626 ret = FAIL;
1627 else if ((di->di_flags & DI_FLAGS_FIX)
1628 && di->di_tv.v_type != VAR_DICT
1629 && di->di_tv.v_type != VAR_LIST)
1630 {
1631 // For historic reasons this error is not given for a list or
1632 // dict. E.g., the b: dict could be locked/unlocked.
1633 semsg(_(e_lock_unlock), lp->ll_name);
1634 ret = FAIL;
1635 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001636 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001637 {
1638 if (lock)
1639 di->di_flags |= DI_FLAGS_LOCK;
1640 else
1641 di->di_flags &= ~DI_FLAGS_LOCK;
1642 item_lock(&di->di_tv, deep, lock);
1643 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001644 }
1645 *name_end = cc;
1646 }
1647 else if (lp->ll_range)
1648 {
1649 listitem_T *li = lp->ll_li;
1650
1651 // (un)lock a range of List items.
1652 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1653 {
1654 item_lock(&li->li_tv, deep, lock);
1655 li = li->li_next;
1656 ++lp->ll_n1;
1657 }
1658 }
1659 else if (lp->ll_list != NULL)
1660 // (un)lock a List item.
1661 item_lock(&lp->ll_li->li_tv, deep, lock);
1662 else
1663 // (un)lock a Dictionary item.
1664 item_lock(&lp->ll_di->di_tv, deep, lock);
1665
1666 return ret;
1667}
1668
1669/*
1670 * Lock or unlock an item. "deep" is nr of levels to go.
1671 */
1672 static void
1673item_lock(typval_T *tv, int deep, int lock)
1674{
1675 static int recurse = 0;
1676 list_T *l;
1677 listitem_T *li;
1678 dict_T *d;
1679 blob_T *b;
1680 hashitem_T *hi;
1681 int todo;
1682
1683 if (recurse >= DICT_MAXNEST)
1684 {
1685 emsg(_("E743: variable nested too deep for (un)lock"));
1686 return;
1687 }
1688 if (deep == 0)
1689 return;
1690 ++recurse;
1691
1692 // lock/unlock the item itself
1693 if (lock)
1694 tv->v_lock |= VAR_LOCKED;
1695 else
1696 tv->v_lock &= ~VAR_LOCKED;
1697
1698 switch (tv->v_type)
1699 {
1700 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001701 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001703 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001705 case VAR_STRING:
1706 case VAR_FUNC:
1707 case VAR_PARTIAL:
1708 case VAR_FLOAT:
1709 case VAR_SPECIAL:
1710 case VAR_JOB:
1711 case VAR_CHANNEL:
1712 break;
1713
1714 case VAR_BLOB:
1715 if ((b = tv->vval.v_blob) != NULL)
1716 {
1717 if (lock)
1718 b->bv_lock |= VAR_LOCKED;
1719 else
1720 b->bv_lock &= ~VAR_LOCKED;
1721 }
1722 break;
1723 case VAR_LIST:
1724 if ((l = tv->vval.v_list) != NULL)
1725 {
1726 if (lock)
1727 l->lv_lock |= VAR_LOCKED;
1728 else
1729 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001730 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001731 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001732 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733 item_lock(&li->li_tv, deep - 1, lock);
1734 }
1735 break;
1736 case VAR_DICT:
1737 if ((d = tv->vval.v_dict) != NULL)
1738 {
1739 if (lock)
1740 d->dv_lock |= VAR_LOCKED;
1741 else
1742 d->dv_lock &= ~VAR_LOCKED;
1743 if (deep < 0 || deep > 1)
1744 {
1745 // recursive: lock/unlock the items the List contains
1746 todo = (int)d->dv_hashtab.ht_used;
1747 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1748 {
1749 if (!HASHITEM_EMPTY(hi))
1750 {
1751 --todo;
1752 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1753 }
1754 }
1755 }
1756 }
1757 }
1758 --recurse;
1759}
1760
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001761#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1762/*
1763 * Delete all "menutrans_" variables.
1764 */
1765 void
1766del_menutrans_vars(void)
1767{
1768 hashitem_T *hi;
1769 int todo;
1770
1771 hash_lock(&globvarht);
1772 todo = (int)globvarht.ht_used;
1773 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1774 {
1775 if (!HASHITEM_EMPTY(hi))
1776 {
1777 --todo;
1778 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1779 delete_var(&globvarht, hi);
1780 }
1781 }
1782 hash_unlock(&globvarht);
1783}
1784#endif
1785
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001787 * Local string buffer for the next two functions to store a variable name
1788 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1789 * get_user_var_name().
1790 */
1791
1792static char_u *varnamebuf = NULL;
1793static int varnamebuflen = 0;
1794
1795/*
1796 * Function to concatenate a prefix and a variable name.
1797 */
1798 static char_u *
1799cat_prefix_varname(int prefix, char_u *name)
1800{
1801 int len;
1802
1803 len = (int)STRLEN(name) + 3;
1804 if (len > varnamebuflen)
1805 {
1806 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001807 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001808 varnamebuf = alloc(len);
1809 if (varnamebuf == NULL)
1810 {
1811 varnamebuflen = 0;
1812 return NULL;
1813 }
1814 varnamebuflen = len;
1815 }
1816 *varnamebuf = prefix;
1817 varnamebuf[1] = ':';
1818 STRCPY(varnamebuf + 2, name);
1819 return varnamebuf;
1820}
1821
1822/*
1823 * Function given to ExpandGeneric() to obtain the list of user defined
1824 * (global/buffer/window/built-in) variable names.
1825 */
1826 char_u *
1827get_user_var_name(expand_T *xp, int idx)
1828{
1829 static long_u gdone;
1830 static long_u bdone;
1831 static long_u wdone;
1832 static long_u tdone;
1833 static int vidx;
1834 static hashitem_T *hi;
1835 hashtab_T *ht;
1836
1837 if (idx == 0)
1838 {
1839 gdone = bdone = wdone = vidx = 0;
1840 tdone = 0;
1841 }
1842
1843 // Global variables
1844 if (gdone < globvarht.ht_used)
1845 {
1846 if (gdone++ == 0)
1847 hi = globvarht.ht_array;
1848 else
1849 ++hi;
1850 while (HASHITEM_EMPTY(hi))
1851 ++hi;
1852 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1853 return cat_prefix_varname('g', hi->hi_key);
1854 return hi->hi_key;
1855 }
1856
1857 // b: variables
1858 ht = &curbuf->b_vars->dv_hashtab;
1859 if (bdone < ht->ht_used)
1860 {
1861 if (bdone++ == 0)
1862 hi = ht->ht_array;
1863 else
1864 ++hi;
1865 while (HASHITEM_EMPTY(hi))
1866 ++hi;
1867 return cat_prefix_varname('b', hi->hi_key);
1868 }
1869
1870 // w: variables
1871 ht = &curwin->w_vars->dv_hashtab;
1872 if (wdone < ht->ht_used)
1873 {
1874 if (wdone++ == 0)
1875 hi = ht->ht_array;
1876 else
1877 ++hi;
1878 while (HASHITEM_EMPTY(hi))
1879 ++hi;
1880 return cat_prefix_varname('w', hi->hi_key);
1881 }
1882
1883 // t: variables
1884 ht = &curtab->tp_vars->dv_hashtab;
1885 if (tdone < ht->ht_used)
1886 {
1887 if (tdone++ == 0)
1888 hi = ht->ht_array;
1889 else
1890 ++hi;
1891 while (HASHITEM_EMPTY(hi))
1892 ++hi;
1893 return cat_prefix_varname('t', hi->hi_key);
1894 }
1895
1896 // v: variables
1897 if (vidx < VV_LEN)
1898 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1899
1900 VIM_CLEAR(varnamebuf);
1901 varnamebuflen = 0;
1902 return NULL;
1903}
1904
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001905 char *
1906get_var_special_name(int nr)
1907{
1908 switch (nr)
1909 {
1910 case VVAL_FALSE: return "v:false";
1911 case VVAL_TRUE: return "v:true";
1912 case VVAL_NONE: return "v:none";
1913 case VVAL_NULL: return "v:null";
1914 }
1915 internal_error("get_var_special_name()");
1916 return "42";
1917}
1918
1919/*
1920 * Returns the global variable dictionary
1921 */
1922 dict_T *
1923get_globvar_dict(void)
1924{
1925 return &globvardict;
1926}
1927
1928/*
1929 * Returns the global variable hash table
1930 */
1931 hashtab_T *
1932get_globvar_ht(void)
1933{
1934 return &globvarht;
1935}
1936
1937/*
1938 * Returns the v: variable dictionary
1939 */
1940 dict_T *
1941get_vimvar_dict(void)
1942{
1943 return &vimvardict;
1944}
1945
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001946/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001948 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 */
1950 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001951find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001953 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1954 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
1956 if (di == NULL)
1957 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001958 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1960 return (int)(vv - vimvars);
1961}
1962
1963
1964/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001965 * Set type of v: variable to "type".
1966 */
1967 void
1968set_vim_var_type(int idx, vartype_T type)
1969{
1970 vimvars[idx].vv_type = type;
1971}
1972
1973/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001974 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001975 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001976 */
1977 void
1978set_vim_var_nr(int idx, varnumber_T val)
1979{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001980 vimvars[idx].vv_nr = val;
1981}
1982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 char *
1984get_vim_var_name(int idx)
1985{
1986 return vimvars[idx].vv_name;
1987}
1988
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001989/*
1990 * Get typval_T v: variable value.
1991 */
1992 typval_T *
1993get_vim_var_tv(int idx)
1994{
1995 return &vimvars[idx].vv_tv;
1996}
1997
1998/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001999 * Set v: variable to "tv". Only accepts the same type.
2000 * Takes over the value of "tv".
2001 */
2002 int
2003set_vim_var_tv(int idx, typval_T *tv)
2004{
2005 if (vimvars[idx].vv_type != tv->v_type)
2006 {
2007 emsg(_("E1063: type mismatch for v: variable"));
2008 clear_tv(tv);
2009 return FAIL;
2010 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002011 // VV_RO is also checked when compiling, but let's check here as well.
2012 if (vimvars[idx].vv_flags & VV_RO)
2013 {
2014 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2015 return FAIL;
2016 }
2017 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2018 {
2019 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2020 return FAIL;
2021 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002022 clear_tv(&vimvars[idx].vv_di.di_tv);
2023 vimvars[idx].vv_di.di_tv = *tv;
2024 return OK;
2025}
2026
2027/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002028 * Get number v: variable value.
2029 */
2030 varnumber_T
2031get_vim_var_nr(int idx)
2032{
2033 return vimvars[idx].vv_nr;
2034}
2035
2036/*
2037 * Get string v: variable value. Uses a static buffer, can only be used once.
2038 * If the String variable has never been set, return an empty string.
2039 * Never returns NULL;
2040 */
2041 char_u *
2042get_vim_var_str(int idx)
2043{
2044 return tv_get_string(&vimvars[idx].vv_tv);
2045}
2046
2047/*
2048 * Get List v: variable value. Caller must take care of reference count when
2049 * needed.
2050 */
2051 list_T *
2052get_vim_var_list(int idx)
2053{
2054 return vimvars[idx].vv_list;
2055}
2056
2057/*
2058 * Get Dict v: variable value. Caller must take care of reference count when
2059 * needed.
2060 */
2061 dict_T *
2062get_vim_var_dict(int idx)
2063{
2064 return vimvars[idx].vv_dict;
2065}
2066
2067/*
2068 * Set v:char to character "c".
2069 */
2070 void
2071set_vim_var_char(int c)
2072{
2073 char_u buf[MB_MAXBYTES + 1];
2074
2075 if (has_mbyte)
2076 buf[(*mb_char2bytes)(c, buf)] = NUL;
2077 else
2078 {
2079 buf[0] = c;
2080 buf[1] = NUL;
2081 }
2082 set_vim_var_string(VV_CHAR, buf, -1);
2083}
2084
2085/*
2086 * Set v:count to "count" and v:count1 to "count1".
2087 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2088 */
2089 void
2090set_vcount(
2091 long count,
2092 long count1,
2093 int set_prevcount)
2094{
2095 if (set_prevcount)
2096 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2097 vimvars[VV_COUNT].vv_nr = count;
2098 vimvars[VV_COUNT1].vv_nr = count1;
2099}
2100
2101/*
2102 * Save variables that might be changed as a side effect. Used when executing
2103 * a timer callback.
2104 */
2105 void
2106save_vimvars(vimvars_save_T *vvsave)
2107{
2108 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2109 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2110 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2111}
2112
2113/*
2114 * Restore variables saved by save_vimvars().
2115 */
2116 void
2117restore_vimvars(vimvars_save_T *vvsave)
2118{
2119 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2120 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2121 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2122}
2123
2124/*
2125 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2126 * value.
2127 */
2128 void
2129set_vim_var_string(
2130 int idx,
2131 char_u *val,
2132 int len) // length of "val" to use or -1 (whole string)
2133{
2134 clear_tv(&vimvars[idx].vv_di.di_tv);
2135 vimvars[idx].vv_type = VAR_STRING;
2136 if (val == NULL)
2137 vimvars[idx].vv_str = NULL;
2138 else if (len == -1)
2139 vimvars[idx].vv_str = vim_strsave(val);
2140 else
2141 vimvars[idx].vv_str = vim_strnsave(val, len);
2142}
2143
2144/*
2145 * Set List v: variable to "val".
2146 */
2147 void
2148set_vim_var_list(int idx, list_T *val)
2149{
2150 clear_tv(&vimvars[idx].vv_di.di_tv);
2151 vimvars[idx].vv_type = VAR_LIST;
2152 vimvars[idx].vv_list = val;
2153 if (val != NULL)
2154 ++val->lv_refcount;
2155}
2156
2157/*
2158 * Set Dictionary v: variable to "val".
2159 */
2160 void
2161set_vim_var_dict(int idx, dict_T *val)
2162{
2163 clear_tv(&vimvars[idx].vv_di.di_tv);
2164 vimvars[idx].vv_type = VAR_DICT;
2165 vimvars[idx].vv_dict = val;
2166 if (val != NULL)
2167 {
2168 ++val->dv_refcount;
2169 dict_set_items_ro(val);
2170 }
2171}
2172
2173/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002174 * Set the v:argv list.
2175 */
2176 void
2177set_argv_var(char **argv, int argc)
2178{
2179 list_T *l = list_alloc();
2180 int i;
2181
2182 if (l == NULL)
2183 getout(1);
2184 l->lv_lock = VAR_FIXED;
2185 for (i = 0; i < argc; ++i)
2186 {
2187 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2188 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002189 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002190 }
2191 set_vim_var_list(VV_ARGV, l);
2192}
2193
2194/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002195 * Set v:register if needed.
2196 */
2197 void
2198set_reg_var(int c)
2199{
2200 char_u regname;
2201
2202 if (c == 0 || c == ' ')
2203 regname = '"';
2204 else
2205 regname = c;
2206 // Avoid free/alloc when the value is already right.
2207 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2208 set_vim_var_string(VV_REG, &regname, 1);
2209}
2210
2211/*
2212 * Get or set v:exception. If "oldval" == NULL, return the current value.
2213 * Otherwise, restore the value to "oldval" and return NULL.
2214 * Must always be called in pairs to save and restore v:exception! Does not
2215 * take care of memory allocations.
2216 */
2217 char_u *
2218v_exception(char_u *oldval)
2219{
2220 if (oldval == NULL)
2221 return vimvars[VV_EXCEPTION].vv_str;
2222
2223 vimvars[VV_EXCEPTION].vv_str = oldval;
2224 return NULL;
2225}
2226
2227/*
2228 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2229 * Otherwise, restore the value to "oldval" and return NULL.
2230 * Must always be called in pairs to save and restore v:throwpoint! Does not
2231 * take care of memory allocations.
2232 */
2233 char_u *
2234v_throwpoint(char_u *oldval)
2235{
2236 if (oldval == NULL)
2237 return vimvars[VV_THROWPOINT].vv_str;
2238
2239 vimvars[VV_THROWPOINT].vv_str = oldval;
2240 return NULL;
2241}
2242
2243/*
2244 * Set v:cmdarg.
2245 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2246 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2247 * Must always be called in pairs!
2248 */
2249 char_u *
2250set_cmdarg(exarg_T *eap, char_u *oldarg)
2251{
2252 char_u *oldval;
2253 char_u *newval;
2254 unsigned len;
2255
2256 oldval = vimvars[VV_CMDARG].vv_str;
2257 if (eap == NULL)
2258 {
2259 vim_free(oldval);
2260 vimvars[VV_CMDARG].vv_str = oldarg;
2261 return NULL;
2262 }
2263
2264 if (eap->force_bin == FORCE_BIN)
2265 len = 6;
2266 else if (eap->force_bin == FORCE_NOBIN)
2267 len = 8;
2268 else
2269 len = 0;
2270
2271 if (eap->read_edit)
2272 len += 7;
2273
2274 if (eap->force_ff != 0)
2275 len += 10; // " ++ff=unix"
2276 if (eap->force_enc != 0)
2277 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2278 if (eap->bad_char != 0)
2279 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2280
2281 newval = alloc(len + 1);
2282 if (newval == NULL)
2283 return NULL;
2284
2285 if (eap->force_bin == FORCE_BIN)
2286 sprintf((char *)newval, " ++bin");
2287 else if (eap->force_bin == FORCE_NOBIN)
2288 sprintf((char *)newval, " ++nobin");
2289 else
2290 *newval = NUL;
2291
2292 if (eap->read_edit)
2293 STRCAT(newval, " ++edit");
2294
2295 if (eap->force_ff != 0)
2296 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2297 eap->force_ff == 'u' ? "unix"
2298 : eap->force_ff == 'd' ? "dos"
2299 : "mac");
2300 if (eap->force_enc != 0)
2301 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2302 eap->cmd + eap->force_enc);
2303 if (eap->bad_char == BAD_KEEP)
2304 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2305 else if (eap->bad_char == BAD_DROP)
2306 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2307 else if (eap->bad_char != 0)
2308 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2309 vimvars[VV_CMDARG].vv_str = newval;
2310 return oldval;
2311}
2312
2313/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002314 * Get the value of internal variable "name".
2315 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2316 */
2317 int
2318get_var_tv(
2319 char_u *name,
2320 int len, // length of "name"
2321 typval_T *rettv, // NULL when only checking existence
2322 dictitem_T **dip, // non-NULL when typval's dict item is needed
2323 int verbose, // may give error message
2324 int no_autoload) // do not use script autoloading
2325{
2326 int ret = OK;
2327 typval_T *tv = NULL;
2328 dictitem_T *v;
2329 int cc;
2330
2331 // truncate the name, so that we can use strcmp()
2332 cc = name[len];
2333 name[len] = NUL;
2334
2335 // Check for user-defined variables.
2336 v = find_var(name, NULL, no_autoload);
2337 if (v != NULL)
2338 {
2339 tv = &v->di_tv;
2340 if (dip != NULL)
2341 *dip = v;
2342 }
2343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2345 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002346 imported_T *import = find_imported(name, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347
2348 // imported variable from another script
2349 if (import != NULL)
2350 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002351 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2353 + import->imp_var_vals_idx;
2354 tv = sv->sv_tv;
2355 }
2356 }
2357
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002358 if (tv == NULL)
2359 {
2360 if (rettv != NULL && verbose)
2361 semsg(_(e_undefvar), name);
2362 ret = FAIL;
2363 }
2364 else if (rettv != NULL)
2365 copy_tv(tv, rettv);
2366
2367 name[len] = cc;
2368
2369 return ret;
2370}
2371
2372/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002373 * Check if variable "name[len]" is a local variable or an argument.
2374 * If so, "*eval_lavars_used" is set to TRUE.
2375 */
2376 void
2377check_vars(char_u *name, int len)
2378{
2379 int cc;
2380 char_u *varname;
2381 hashtab_T *ht;
2382
2383 if (eval_lavars_used == NULL)
2384 return;
2385
2386 // truncate the name, so that we can use strcmp()
2387 cc = name[len];
2388 name[len] = NUL;
2389
2390 ht = find_var_ht(name, &varname);
2391 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2392 {
2393 if (find_var(name, NULL, TRUE) != NULL)
2394 *eval_lavars_used = TRUE;
2395 }
2396
2397 name[len] = cc;
2398}
2399
2400/*
2401 * Find variable "name" in the list of variables.
2402 * Return a pointer to it if found, NULL if not found.
2403 * Careful: "a:0" variables don't have a name.
2404 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2405 * hashtab_T used.
2406 */
2407 dictitem_T *
2408find_var(char_u *name, hashtab_T **htp, int no_autoload)
2409{
2410 char_u *varname;
2411 hashtab_T *ht;
2412 dictitem_T *ret = NULL;
2413
2414 ht = find_var_ht(name, &varname);
2415 if (htp != NULL)
2416 *htp = ht;
2417 if (ht == NULL)
2418 return NULL;
2419 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2420 if (ret != NULL)
2421 return ret;
2422
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002423 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002424 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2425}
2426
2427/*
2428 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002429 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002430 * Returns NULL if not found.
2431 */
2432 dictitem_T *
2433find_var_in_ht(
2434 hashtab_T *ht,
2435 int htname,
2436 char_u *varname,
2437 int no_autoload)
2438{
2439 hashitem_T *hi;
2440
2441 if (*varname == NUL)
2442 {
2443 // Must be something like "s:", otherwise "ht" would be NULL.
2444 switch (htname)
2445 {
2446 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2447 case 'g': return &globvars_var;
2448 case 'v': return &vimvars_var;
2449 case 'b': return &curbuf->b_bufvar;
2450 case 'w': return &curwin->w_winvar;
2451 case 't': return &curtab->tp_winvar;
2452 case 'l': return get_funccal_local_var();
2453 case 'a': return get_funccal_args_var();
2454 }
2455 return NULL;
2456 }
2457
2458 hi = hash_find(ht, varname);
2459 if (HASHITEM_EMPTY(hi))
2460 {
2461 // For global variables we may try auto-loading the script. If it
2462 // worked find the variable again. Don't auto-load a script if it was
2463 // loaded already, otherwise it would be loaded every time when
2464 // checking if a function name is a Funcref variable.
2465 if (ht == &globvarht && !no_autoload)
2466 {
2467 // Note: script_autoload() may make "hi" invalid. It must either
2468 // be obtained again or not used.
2469 if (!script_autoload(varname, FALSE) || aborting())
2470 return NULL;
2471 hi = hash_find(ht, varname);
2472 }
2473 if (HASHITEM_EMPTY(hi))
2474 return NULL;
2475 }
2476 return HI2DI(hi);
2477}
2478
2479/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 * Get the script-local hashtab. NULL if not in a script context.
2481 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002482 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483get_script_local_ht(void)
2484{
2485 scid_T sid = current_sctx.sc_sid;
2486
2487 if (sid > 0 && sid <= script_items.ga_len)
2488 return &SCRIPT_VARS(sid);
2489 return NULL;
2490}
2491
2492/*
2493 * Look for "name[len]" in script-local variables.
2494 * Return -1 when not found.
2495 */
2496 int
2497lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2498{
2499 hashtab_T *ht = get_script_local_ht();
2500 char_u buffer[30];
2501 char_u *p;
2502 int res;
2503 hashitem_T *hi;
2504
2505 if (ht == NULL)
2506 return -1;
2507 if (len < sizeof(buffer) - 1)
2508 {
2509 vim_strncpy(buffer, name, len);
2510 p = buffer;
2511 }
2512 else
2513 {
2514 p = vim_strnsave(name, (int)len);
2515 if (p == NULL)
2516 return -1;
2517 }
2518
2519 hi = hash_find(ht, p);
2520 res = HASHITEM_EMPTY(hi) ? -1 : 1;
2521
2522 // if not script-local, then perhaps imported
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002523 if (res == -1 && find_imported(p, 0, NULL) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 res = 1;
2525
2526 if (p != buffer)
2527 vim_free(p);
2528 return res;
2529}
2530
2531/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002532 * Find the hashtab used for a variable name.
2533 * Return NULL if the name is not valid.
2534 * Set "varname" to the start of name without ':'.
2535 */
2536 hashtab_T *
2537find_var_ht(char_u *name, char_u **varname)
2538{
2539 hashitem_T *hi;
2540 hashtab_T *ht;
2541
2542 if (name[0] == NUL)
2543 return NULL;
2544 if (name[1] != ':')
2545 {
2546 // The name must not start with a colon or #.
2547 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2548 return NULL;
2549 *varname = name;
2550
2551 // "version" is "v:version" in all scopes if scriptversion < 3.
2552 // Same for a few other variables marked with VV_COMPAT.
2553 if (current_sctx.sc_version < 3)
2554 {
2555 hi = hash_find(&compat_hashtab, name);
2556 if (!HASHITEM_EMPTY(hi))
2557 return &compat_hashtab;
2558 }
2559
2560 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 if (ht != NULL)
2562 return ht; // local variable
2563
2564 // in Vim9 script items at the script level are script-local
2565 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2566 {
2567 ht = get_script_local_ht();
2568 if (ht != NULL)
2569 return ht;
2570 }
2571
2572 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002573 }
2574 *varname = name + 2;
2575 if (*name == 'g') // global variable
2576 return &globvarht;
2577 // There must be no ':' or '#' in the rest of the name, unless g: is used
2578 if (vim_strchr(name + 2, ':') != NULL
2579 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2580 return NULL;
2581 if (*name == 'b') // buffer variable
2582 return &curbuf->b_vars->dv_hashtab;
2583 if (*name == 'w') // window variable
2584 return &curwin->w_vars->dv_hashtab;
2585 if (*name == 't') // tab page variable
2586 return &curtab->tp_vars->dv_hashtab;
2587 if (*name == 'v') // v: variable
2588 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002589 if (get_current_funccal() != NULL
2590 && get_current_funccal()->func->uf_dfunc_idx < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002592 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 if (*name == 'a') // a: function argument
2594 return get_funccal_args_ht();
2595 if (*name == 'l') // l: local function variable
2596 return get_funccal_local_ht();
2597 }
2598 if (*name == 's') // script variable
2599 {
2600 ht = get_script_local_ht();
2601 if (ht != NULL)
2602 return ht;
2603 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002604 return NULL;
2605}
2606
2607/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002608 * Get the string value of a (global/local) variable.
2609 * Note: see tv_get_string() for how long the pointer remains valid.
2610 * Returns NULL when it doesn't exist.
2611 */
2612 char_u *
2613get_var_value(char_u *name)
2614{
2615 dictitem_T *v;
2616
2617 v = find_var(name, NULL, FALSE);
2618 if (v == NULL)
2619 return NULL;
2620 return tv_get_string(&v->di_tv);
2621}
2622
2623/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002624 * Allocate a new hashtab for a sourced script. It will be used while
2625 * sourcing this script and when executing functions defined in the script.
2626 */
2627 void
2628new_script_vars(scid_T id)
2629{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002630 scriptvar_T *sv;
2631
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002632 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2633 if (sv == NULL)
2634 return;
2635 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002636 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002637}
2638
2639/*
2640 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2641 * point to it.
2642 */
2643 void
2644init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2645{
2646 hash_init(&dict->dv_hashtab);
2647 dict->dv_lock = 0;
2648 dict->dv_scope = scope;
2649 dict->dv_refcount = DO_NOT_FREE_CNT;
2650 dict->dv_copyID = 0;
2651 dict_var->di_tv.vval.v_dict = dict;
2652 dict_var->di_tv.v_type = VAR_DICT;
2653 dict_var->di_tv.v_lock = VAR_FIXED;
2654 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2655 dict_var->di_key[0] = NUL;
2656}
2657
2658/*
2659 * Unreference a dictionary initialized by init_var_dict().
2660 */
2661 void
2662unref_var_dict(dict_T *dict)
2663{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002664 // Now the dict needs to be freed if no one else is using it, go back to
2665 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002666 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2667 dict_unref(dict);
2668}
2669
2670/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002671 * Clean up a list of internal variables.
2672 * Frees all allocated variables and the value they contain.
2673 * Clears hashtab "ht", does not free it.
2674 */
2675 void
2676vars_clear(hashtab_T *ht)
2677{
2678 vars_clear_ext(ht, TRUE);
2679}
2680
2681/*
2682 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2683 */
2684 void
2685vars_clear_ext(hashtab_T *ht, int free_val)
2686{
2687 int todo;
2688 hashitem_T *hi;
2689 dictitem_T *v;
2690
2691 hash_lock(ht);
2692 todo = (int)ht->ht_used;
2693 for (hi = ht->ht_array; todo > 0; ++hi)
2694 {
2695 if (!HASHITEM_EMPTY(hi))
2696 {
2697 --todo;
2698
2699 // Free the variable. Don't remove it from the hashtab,
2700 // ht_array might change then. hash_clear() takes care of it
2701 // later.
2702 v = HI2DI(hi);
2703 if (free_val)
2704 clear_tv(&v->di_tv);
2705 if (v->di_flags & DI_FLAGS_ALLOC)
2706 vim_free(v);
2707 }
2708 }
2709 hash_clear(ht);
2710 ht->ht_used = 0;
2711}
2712
2713/*
2714 * Delete a variable from hashtab "ht" at item "hi".
2715 * Clear the variable value and free the dictitem.
2716 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002717 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002718delete_var(hashtab_T *ht, hashitem_T *hi)
2719{
2720 dictitem_T *di = HI2DI(hi);
2721
2722 hash_remove(ht, hi);
2723 clear_tv(&di->di_tv);
2724 vim_free(di);
2725}
2726
2727/*
2728 * List the value of one internal variable.
2729 */
2730 static void
2731list_one_var(dictitem_T *v, char *prefix, int *first)
2732{
2733 char_u *tofree;
2734 char_u *s;
2735 char_u numbuf[NUMBUFLEN];
2736
2737 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2738 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2739 s == NULL ? (char_u *)"" : s, first);
2740 vim_free(tofree);
2741}
2742
2743 static void
2744list_one_var_a(
2745 char *prefix,
2746 char_u *name,
2747 int type,
2748 char_u *string,
2749 int *first) // when TRUE clear rest of screen and set to FALSE
2750{
2751 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2752 msg_start();
2753 msg_puts(prefix);
2754 if (name != NULL) // "a:" vars don't have a name stored
2755 msg_puts((char *)name);
2756 msg_putchar(' ');
2757 msg_advance(22);
2758 if (type == VAR_NUMBER)
2759 msg_putchar('#');
2760 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2761 msg_putchar('*');
2762 else if (type == VAR_LIST)
2763 {
2764 msg_putchar('[');
2765 if (*string == '[')
2766 ++string;
2767 }
2768 else if (type == VAR_DICT)
2769 {
2770 msg_putchar('{');
2771 if (*string == '{')
2772 ++string;
2773 }
2774 else
2775 msg_putchar(' ');
2776
2777 msg_outtrans(string);
2778
2779 if (type == VAR_FUNC || type == VAR_PARTIAL)
2780 msg_puts("()");
2781 if (*first)
2782 {
2783 msg_clr_eos();
2784 *first = FALSE;
2785 }
2786}
2787
2788/*
2789 * Set variable "name" to value in "tv".
2790 * If the variable already exists, the value is updated.
2791 * Otherwise the variable is created.
2792 */
2793 void
2794set_var(
2795 char_u *name,
2796 typval_T *tv,
2797 int copy) // make copy of value in "tv"
2798{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002800}
2801
2802/*
2803 * Set variable "name" to value in "tv".
2804 * If the variable already exists and "is_const" is FALSE the value is updated.
2805 * Otherwise the variable is created.
2806 */
2807 void
2808set_var_const(
2809 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002811 typval_T *tv,
2812 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002814{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002816 char_u *varname;
2817 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002819
2820 ht = find_var_ht(name, &varname);
2821 if (ht == NULL || *varname == NUL)
2822 {
2823 semsg(_(e_illvar), name);
2824 return;
2825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 is_script_local = ht == get_script_local_ht();
2827
2828 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002829
2830 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 if (di == NULL)
2832 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002833
2834 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002835 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002836 return;
2837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002839 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002841 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 if (flags & LET_IS_CONST)
2843 {
2844 emsg(_(e_cannot_mod));
2845 return;
2846 }
2847
2848 if (var_check_ro(di->di_flags, name, FALSE)
2849 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2850 return;
2851
2852 if ((flags & LET_NO_COMMAND) == 0
2853 && is_script_local
2854 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2855 {
2856 semsg(_("E1041: Redefining script item %s"), name);
2857 return;
2858 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002859 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 else
2861 // can only redefine once
2862 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002863
2864 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002868 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002869 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002870 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002871 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002873 if (copy || tv->v_type != VAR_STRING)
2874 {
2875 char_u *val = tv_get_string(tv);
2876
2877 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002878 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 if (di->di_tv.vval.v_string == NULL)
2880 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002881 }
2882 else
2883 {
2884 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002886 tv->vval.v_string = NULL;
2887 }
2888 return;
2889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002891 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002895#ifdef FEAT_SEARCH_EXTRA
2896 else if (STRCMP(varname, "hlsearch") == 0)
2897 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002899 redraw_all_later(SOME_VALID);
2900 }
2901#endif
2902 return;
2903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002905 {
2906 semsg(_("E963: setting %s to value with wrong type"), name);
2907 return;
2908 }
2909 }
2910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002912 }
2913 else // add a new variable
2914 {
2915 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002916 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002917 {
2918 semsg(_(e_illvar), name);
2919 return;
2920 }
2921
2922 // Make sure the variable name is valid.
2923 if (!valid_varname(varname))
2924 return;
2925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2927 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002928 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 STRCPY(di->di_key, varname);
2930 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002931 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002933 return;
2934 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 di->di_flags = DI_FLAGS_ALLOC;
2936 if (flags & LET_IS_CONST)
2937 di->di_flags |= DI_FLAGS_LOCK;
2938
2939 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2940 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002941 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942
2943 // Store a pointer to the typval_T, so that it can be found by
2944 // index instead of using a hastab lookup.
2945 if (ga_grow(&si->sn_var_vals, 1) == OK)
2946 {
2947 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2948 + si->sn_var_vals.ga_len;
2949 sv->sv_name = di->di_key;
2950 sv->sv_tv = &di->di_tv;
2951 sv->sv_type = type == NULL ? &t_any : type;
2952 sv->sv_const = (flags & LET_IS_CONST);
2953 sv->sv_export = is_export;
2954 ++si->sn_var_vals.ga_len;
2955
2956 // let ex_export() know the export worked.
2957 is_export = FALSE;
2958 }
2959 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002960 }
2961
2962 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002964 else
2965 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 di->di_tv = *tv;
2967 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002968 init_tv(tv);
2969 }
2970
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 if (flags & LET_IS_CONST)
2972 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002973}
2974
2975/*
2976 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2977 * Also give an error message.
2978 */
2979 int
2980var_check_ro(int flags, char_u *name, int use_gettext)
2981{
2982 if (flags & DI_FLAGS_RO)
2983 {
2984 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
2985 return TRUE;
2986 }
2987 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
2988 {
2989 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
2990 return TRUE;
2991 }
2992 return FALSE;
2993}
2994
2995/*
2996 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
2997 * Also give an error message.
2998 */
2999 int
3000var_check_fixed(int flags, char_u *name, int use_gettext)
3001{
3002 if (flags & DI_FLAGS_FIX)
3003 {
3004 semsg(_("E795: Cannot delete variable %s"),
3005 use_gettext ? (char_u *)_(name) : name);
3006 return TRUE;
3007 }
3008 return FALSE;
3009}
3010
3011/*
3012 * Check if a funcref is assigned to a valid variable name.
3013 * Return TRUE and give an error if not.
3014 */
3015 int
3016var_check_func_name(
3017 char_u *name, // points to start of variable name
3018 int new_var) // TRUE when creating the variable
3019{
3020 // Allow for w: b: s: and t:.
3021 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3022 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3023 ? name[2] : name[0]))
3024 {
3025 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3026 name);
3027 return TRUE;
3028 }
3029 // Don't allow hiding a function. When "v" is not NULL we might be
3030 // assigning another function to the same var, the type is checked
3031 // below.
3032 if (new_var && function_exists(name, FALSE))
3033 {
3034 semsg(_("E705: Variable name conflicts with existing function: %s"),
3035 name);
3036 return TRUE;
3037 }
3038 return FALSE;
3039}
3040
3041/*
3042 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3043 * Also give an error message, using "name" or _("name") when use_gettext is
3044 * TRUE.
3045 */
3046 int
3047var_check_lock(int lock, char_u *name, int use_gettext)
3048{
3049 if (lock & VAR_LOCKED)
3050 {
3051 semsg(_("E741: Value is locked: %s"),
3052 name == NULL ? (char_u *)_("Unknown")
3053 : use_gettext ? (char_u *)_(name)
3054 : name);
3055 return TRUE;
3056 }
3057 if (lock & VAR_FIXED)
3058 {
3059 semsg(_("E742: Cannot change value of %s"),
3060 name == NULL ? (char_u *)_("Unknown")
3061 : use_gettext ? (char_u *)_(name)
3062 : name);
3063 return TRUE;
3064 }
3065 return FALSE;
3066}
3067
3068/*
3069 * Check if a variable name is valid.
3070 * Return FALSE and give an error if not.
3071 */
3072 int
3073valid_varname(char_u *varname)
3074{
3075 char_u *p;
3076
3077 for (p = varname; *p != NUL; ++p)
3078 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3079 && *p != AUTOLOAD_CHAR)
3080 {
3081 semsg(_(e_illvar), varname);
3082 return FALSE;
3083 }
3084 return TRUE;
3085}
3086
3087/*
3088 * getwinvar() and gettabwinvar()
3089 */
3090 static void
3091getwinvar(
3092 typval_T *argvars,
3093 typval_T *rettv,
3094 int off) // 1 for gettabwinvar()
3095{
3096 win_T *win;
3097 char_u *varname;
3098 dictitem_T *v;
3099 tabpage_T *tp = NULL;
3100 int done = FALSE;
3101 win_T *oldcurwin;
3102 tabpage_T *oldtabpage;
3103 int need_switch_win;
3104
3105 if (off == 1)
3106 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3107 else
3108 tp = curtab;
3109 win = find_win_by_nr(&argvars[off], tp);
3110 varname = tv_get_string_chk(&argvars[off + 1]);
3111 ++emsg_off;
3112
3113 rettv->v_type = VAR_STRING;
3114 rettv->vval.v_string = NULL;
3115
3116 if (win != NULL && varname != NULL)
3117 {
3118 // Set curwin to be our win, temporarily. Also set the tabpage,
3119 // otherwise the window is not valid. Only do this when needed,
3120 // autocommands get blocked.
3121 need_switch_win = !(tp == curtab && win == curwin);
3122 if (!need_switch_win
3123 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3124 {
3125 if (*varname == '&')
3126 {
3127 if (varname[1] == NUL)
3128 {
3129 // get all window-local options in a dict
3130 dict_T *opts = get_winbuf_options(FALSE);
3131
3132 if (opts != NULL)
3133 {
3134 rettv_dict_set(rettv, opts);
3135 done = TRUE;
3136 }
3137 }
3138 else if (get_option_tv(&varname, rettv, 1) == OK)
3139 // window-local-option
3140 done = TRUE;
3141 }
3142 else
3143 {
3144 // Look up the variable.
3145 // Let getwinvar({nr}, "") return the "w:" dictionary.
3146 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3147 varname, FALSE);
3148 if (v != NULL)
3149 {
3150 copy_tv(&v->di_tv, rettv);
3151 done = TRUE;
3152 }
3153 }
3154 }
3155
3156 if (need_switch_win)
3157 // restore previous notion of curwin
3158 restore_win(oldcurwin, oldtabpage, TRUE);
3159 }
3160
3161 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3162 // use the default return value
3163 copy_tv(&argvars[off + 2], rettv);
3164
3165 --emsg_off;
3166}
3167
3168/*
3169 * "setwinvar()" and "settabwinvar()" functions
3170 */
3171 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003172setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003173{
3174 win_T *win;
3175 win_T *save_curwin;
3176 tabpage_T *save_curtab;
3177 int need_switch_win;
3178 char_u *varname, *winvarname;
3179 typval_T *varp;
3180 char_u nbuf[NUMBUFLEN];
3181 tabpage_T *tp = NULL;
3182
3183 if (check_secure())
3184 return;
3185
3186 if (off == 1)
3187 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3188 else
3189 tp = curtab;
3190 win = find_win_by_nr(&argvars[off], tp);
3191 varname = tv_get_string_chk(&argvars[off + 1]);
3192 varp = &argvars[off + 2];
3193
3194 if (win != NULL && varname != NULL && varp != NULL)
3195 {
3196 need_switch_win = !(tp == curtab && win == curwin);
3197 if (!need_switch_win
3198 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3199 {
3200 if (*varname == '&')
3201 {
3202 long numval;
3203 char_u *strval;
3204 int error = FALSE;
3205
3206 ++varname;
3207 numval = (long)tv_get_number_chk(varp, &error);
3208 strval = tv_get_string_buf_chk(varp, nbuf);
3209 if (!error && strval != NULL)
3210 set_option_value(varname, numval, strval, OPT_LOCAL);
3211 }
3212 else
3213 {
3214 winvarname = alloc(STRLEN(varname) + 3);
3215 if (winvarname != NULL)
3216 {
3217 STRCPY(winvarname, "w:");
3218 STRCPY(winvarname + 2, varname);
3219 set_var(winvarname, varp, TRUE);
3220 vim_free(winvarname);
3221 }
3222 }
3223 }
3224 if (need_switch_win)
3225 restore_win(save_curwin, save_curtab, TRUE);
3226 }
3227}
3228
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003229/*
3230 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3231 * v:option_type, and v:option_command.
3232 */
3233 void
3234reset_v_option_vars(void)
3235{
3236 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3237 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3238 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3239 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3240 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3241 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3242}
3243
3244/*
3245 * Add an assert error to v:errors.
3246 */
3247 void
3248assert_error(garray_T *gap)
3249{
3250 struct vimvar *vp = &vimvars[VV_ERRORS];
3251
3252 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003253 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003254 set_vim_var_list(VV_ERRORS, list_alloc());
3255 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3256}
3257
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003258 int
3259var_exists(char_u *var)
3260{
3261 char_u *name;
3262 char_u *tofree;
3263 typval_T tv;
3264 int len = 0;
3265 int n = FALSE;
3266
3267 // get_name_len() takes care of expanding curly braces
3268 name = var;
3269 len = get_name_len(&var, &tofree, TRUE, FALSE);
3270 if (len > 0)
3271 {
3272 if (tofree != NULL)
3273 name = tofree;
3274 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3275 if (n)
3276 {
3277 // handle d.key, l[idx], f(expr)
3278 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
3279 if (n)
3280 clear_tv(&tv);
3281 }
3282 }
3283 if (*var != NUL)
3284 n = FALSE;
3285
3286 vim_free(tofree);
3287 return n;
3288}
3289
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003290static lval_T *redir_lval = NULL;
3291#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3292static garray_T redir_ga; // only valid when redir_lval is not NULL
3293static char_u *redir_endp = NULL;
3294static char_u *redir_varname = NULL;
3295
3296/*
3297 * Start recording command output to a variable
3298 * When "append" is TRUE append to an existing variable.
3299 * Returns OK if successfully completed the setup. FAIL otherwise.
3300 */
3301 int
3302var_redir_start(char_u *name, int append)
3303{
3304 int save_emsg;
3305 int err;
3306 typval_T tv;
3307
3308 // Catch a bad name early.
3309 if (!eval_isnamec1(*name))
3310 {
3311 emsg(_(e_invarg));
3312 return FAIL;
3313 }
3314
3315 // Make a copy of the name, it is used in redir_lval until redir ends.
3316 redir_varname = vim_strsave(name);
3317 if (redir_varname == NULL)
3318 return FAIL;
3319
3320 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3321 if (redir_lval == NULL)
3322 {
3323 var_redir_stop();
3324 return FAIL;
3325 }
3326
3327 // The output is stored in growarray "redir_ga" until redirection ends.
3328 ga_init2(&redir_ga, (int)sizeof(char), 500);
3329
3330 // Parse the variable name (can be a dict or list entry).
3331 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3332 FNE_CHECK_START);
3333 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3334 {
3335 clear_lval(redir_lval);
3336 if (redir_endp != NULL && *redir_endp != NUL)
3337 // Trailing characters are present after the variable name
3338 emsg(_(e_trailing));
3339 else
3340 emsg(_(e_invarg));
3341 redir_endp = NULL; // don't store a value, only cleanup
3342 var_redir_stop();
3343 return FAIL;
3344 }
3345
3346 // check if we can write to the variable: set it to or append an empty
3347 // string
3348 save_emsg = did_emsg;
3349 did_emsg = FALSE;
3350 tv.v_type = VAR_STRING;
3351 tv.vval.v_string = (char_u *)"";
3352 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003354 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003355 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003356 clear_lval(redir_lval);
3357 err = did_emsg;
3358 did_emsg |= save_emsg;
3359 if (err)
3360 {
3361 redir_endp = NULL; // don't store a value, only cleanup
3362 var_redir_stop();
3363 return FAIL;
3364 }
3365
3366 return OK;
3367}
3368
3369/*
3370 * Append "value[value_len]" to the variable set by var_redir_start().
3371 * The actual appending is postponed until redirection ends, because the value
3372 * appended may in fact be the string we write to, changing it may cause freed
3373 * memory to be used:
3374 * :redir => foo
3375 * :let foo
3376 * :redir END
3377 */
3378 void
3379var_redir_str(char_u *value, int value_len)
3380{
3381 int len;
3382
3383 if (redir_lval == NULL)
3384 return;
3385
3386 if (value_len == -1)
3387 len = (int)STRLEN(value); // Append the entire string
3388 else
3389 len = value_len; // Append only "value_len" characters
3390
3391 if (ga_grow(&redir_ga, len) == OK)
3392 {
3393 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3394 redir_ga.ga_len += len;
3395 }
3396 else
3397 var_redir_stop();
3398}
3399
3400/*
3401 * Stop redirecting command output to a variable.
3402 * Frees the allocated memory.
3403 */
3404 void
3405var_redir_stop(void)
3406{
3407 typval_T tv;
3408
3409 if (EVALCMD_BUSY)
3410 {
3411 redir_lval = NULL;
3412 return;
3413 }
3414
3415 if (redir_lval != NULL)
3416 {
3417 // If there was no error: assign the text to the variable.
3418 if (redir_endp != NULL)
3419 {
3420 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3421 tv.v_type = VAR_STRING;
3422 tv.vval.v_string = redir_ga.ga_data;
3423 // Call get_lval() again, if it's inside a Dict or List it may
3424 // have changed.
3425 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3426 FALSE, FALSE, 0, FNE_CHECK_START);
3427 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003429 (char_u *)".");
3430 clear_lval(redir_lval);
3431 }
3432
3433 // free the collected output
3434 VIM_CLEAR(redir_ga.ga_data);
3435
3436 VIM_CLEAR(redir_lval);
3437 }
3438 VIM_CLEAR(redir_varname);
3439}
3440
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003441/*
3442 * "gettabvar()" function
3443 */
3444 void
3445f_gettabvar(typval_T *argvars, typval_T *rettv)
3446{
3447 win_T *oldcurwin;
3448 tabpage_T *tp, *oldtabpage;
3449 dictitem_T *v;
3450 char_u *varname;
3451 int done = FALSE;
3452
3453 rettv->v_type = VAR_STRING;
3454 rettv->vval.v_string = NULL;
3455
3456 varname = tv_get_string_chk(&argvars[1]);
3457 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3458 if (tp != NULL && varname != NULL)
3459 {
3460 // Set tp to be our tabpage, temporarily. Also set the window to the
3461 // first window in the tabpage, otherwise the window is not valid.
3462 if (switch_win(&oldcurwin, &oldtabpage,
3463 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3464 : tp->tp_firstwin, tp, TRUE) == OK)
3465 {
3466 // look up the variable
3467 // Let gettabvar({nr}, "") return the "t:" dictionary.
3468 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3469 if (v != NULL)
3470 {
3471 copy_tv(&v->di_tv, rettv);
3472 done = TRUE;
3473 }
3474 }
3475
3476 // restore previous notion of curwin
3477 restore_win(oldcurwin, oldtabpage, TRUE);
3478 }
3479
3480 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3481 copy_tv(&argvars[2], rettv);
3482}
3483
3484/*
3485 * "gettabwinvar()" function
3486 */
3487 void
3488f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3489{
3490 getwinvar(argvars, rettv, 1);
3491}
3492
3493/*
3494 * "getwinvar()" function
3495 */
3496 void
3497f_getwinvar(typval_T *argvars, typval_T *rettv)
3498{
3499 getwinvar(argvars, rettv, 0);
3500}
3501
3502/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003503 * "getbufvar()" function
3504 */
3505 void
3506f_getbufvar(typval_T *argvars, typval_T *rettv)
3507{
3508 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003509 char_u *varname;
3510 dictitem_T *v;
3511 int done = FALSE;
3512
3513 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3514 varname = tv_get_string_chk(&argvars[1]);
3515 ++emsg_off;
3516 buf = tv_get_buf(&argvars[0], FALSE);
3517
3518 rettv->v_type = VAR_STRING;
3519 rettv->vval.v_string = NULL;
3520
3521 if (buf != NULL && varname != NULL)
3522 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003523 if (*varname == '&')
3524 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003525 buf_T *save_curbuf = curbuf;
3526
3527 // set curbuf to be our buf, temporarily
3528 curbuf = buf;
3529
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003530 if (varname[1] == NUL)
3531 {
3532 // get all buffer-local options in a dict
3533 dict_T *opts = get_winbuf_options(TRUE);
3534
3535 if (opts != NULL)
3536 {
3537 rettv_dict_set(rettv, opts);
3538 done = TRUE;
3539 }
3540 }
3541 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3542 // buffer-local-option
3543 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003544
3545 // restore previous notion of curbuf
3546 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003547 }
3548 else
3549 {
3550 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003551 if (*varname == NUL)
3552 // Let getbufvar({nr}, "") return the "b:" dictionary.
3553 v = &buf->b_bufvar;
3554 else
3555 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3556 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003557 if (v != NULL)
3558 {
3559 copy_tv(&v->di_tv, rettv);
3560 done = TRUE;
3561 }
3562 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003563 }
3564
3565 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3566 // use the default value
3567 copy_tv(&argvars[2], rettv);
3568
3569 --emsg_off;
3570}
3571
3572/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003573 * "settabvar()" function
3574 */
3575 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003576f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003577{
3578 tabpage_T *save_curtab;
3579 tabpage_T *tp;
3580 char_u *varname, *tabvarname;
3581 typval_T *varp;
3582
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003583 if (check_secure())
3584 return;
3585
3586 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3587 varname = tv_get_string_chk(&argvars[1]);
3588 varp = &argvars[2];
3589
3590 if (varname != NULL && varp != NULL && tp != NULL)
3591 {
3592 save_curtab = curtab;
3593 goto_tabpage_tp(tp, FALSE, FALSE);
3594
3595 tabvarname = alloc(STRLEN(varname) + 3);
3596 if (tabvarname != NULL)
3597 {
3598 STRCPY(tabvarname, "t:");
3599 STRCPY(tabvarname + 2, varname);
3600 set_var(tabvarname, varp, TRUE);
3601 vim_free(tabvarname);
3602 }
3603
3604 // Restore current tabpage
3605 if (valid_tabpage(save_curtab))
3606 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3607 }
3608}
3609
3610/*
3611 * "settabwinvar()" function
3612 */
3613 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003614f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003615{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003616 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003617}
3618
3619/*
3620 * "setwinvar()" function
3621 */
3622 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003623f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003624{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003625 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003626}
3627
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003628/*
3629 * "setbufvar()" function
3630 */
3631 void
3632f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3633{
3634 buf_T *buf;
3635 char_u *varname, *bufvarname;
3636 typval_T *varp;
3637 char_u nbuf[NUMBUFLEN];
3638
3639 if (check_secure())
3640 return;
3641 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3642 varname = tv_get_string_chk(&argvars[1]);
3643 buf = tv_get_buf(&argvars[0], FALSE);
3644 varp = &argvars[2];
3645
3646 if (buf != NULL && varname != NULL && varp != NULL)
3647 {
3648 if (*varname == '&')
3649 {
3650 long numval;
3651 char_u *strval;
3652 int error = FALSE;
3653 aco_save_T aco;
3654
3655 // set curbuf to be our buf, temporarily
3656 aucmd_prepbuf(&aco, buf);
3657
3658 ++varname;
3659 numval = (long)tv_get_number_chk(varp, &error);
3660 strval = tv_get_string_buf_chk(varp, nbuf);
3661 if (!error && strval != NULL)
3662 set_option_value(varname, numval, strval, OPT_LOCAL);
3663
3664 // reset notion of buffer
3665 aucmd_restbuf(&aco);
3666 }
3667 else
3668 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003669 bufvarname = alloc(STRLEN(varname) + 3);
3670 if (bufvarname != NULL)
3671 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003672 buf_T *save_curbuf = curbuf;
3673
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003674 curbuf = buf;
3675 STRCPY(bufvarname, "b:");
3676 STRCPY(bufvarname + 2, varname);
3677 set_var(bufvarname, varp, TRUE);
3678 vim_free(bufvarname);
3679 curbuf = save_curbuf;
3680 }
3681 }
3682 }
3683}
3684
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003685/*
3686 * Get a callback from "arg". It can be a Funcref or a function name.
3687 * When "arg" is zero return an empty string.
3688 * "cb_name" is not allocated.
3689 * "cb_name" is set to NULL for an invalid argument.
3690 */
3691 callback_T
3692get_callback(typval_T *arg)
3693{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003694 callback_T res;
3695 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003696
3697 res.cb_free_name = FALSE;
3698 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3699 {
3700 res.cb_partial = arg->vval.v_partial;
3701 ++res.cb_partial->pt_refcount;
3702 res.cb_name = partial_name(res.cb_partial);
3703 }
3704 else
3705 {
3706 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003707 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3708 && isdigit(*arg->vval.v_string))
3709 r = FAIL;
3710 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003711 {
3712 // Note that we don't make a copy of the string.
3713 res.cb_name = arg->vval.v_string;
3714 func_ref(res.cb_name);
3715 }
3716 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003717 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003718 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003719 r = FAIL;
3720
3721 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003722 {
3723 emsg(_("E921: Invalid callback argument"));
3724 res.cb_name = NULL;
3725 }
3726 }
3727 return res;
3728}
3729
3730/*
3731 * Copy a callback into a typval_T.
3732 */
3733 void
3734put_callback(callback_T *cb, typval_T *tv)
3735{
3736 if (cb->cb_partial != NULL)
3737 {
3738 tv->v_type = VAR_PARTIAL;
3739 tv->vval.v_partial = cb->cb_partial;
3740 ++tv->vval.v_partial->pt_refcount;
3741 }
3742 else
3743 {
3744 tv->v_type = VAR_FUNC;
3745 tv->vval.v_string = vim_strsave(cb->cb_name);
3746 func_ref(cb->cb_name);
3747 }
3748}
3749
3750/*
3751 * Make a copy of "src" into "dest", allocating the function name if needed,
3752 * without incrementing the refcount.
3753 */
3754 void
3755set_callback(callback_T *dest, callback_T *src)
3756{
3757 if (src->cb_partial == NULL)
3758 {
3759 // just a function name, make a copy
3760 dest->cb_name = vim_strsave(src->cb_name);
3761 dest->cb_free_name = TRUE;
3762 }
3763 else
3764 {
3765 // cb_name is a pointer into cb_partial
3766 dest->cb_name = src->cb_name;
3767 dest->cb_free_name = FALSE;
3768 }
3769 dest->cb_partial = src->cb_partial;
3770}
3771
3772/*
3773 * Unref/free "callback" returned by get_callback() or set_callback().
3774 */
3775 void
3776free_callback(callback_T *callback)
3777{
3778 if (callback->cb_partial != NULL)
3779 {
3780 partial_unref(callback->cb_partial);
3781 callback->cb_partial = NULL;
3782 }
3783 else if (callback->cb_name != NULL)
3784 func_unref(callback->cb_name);
3785 if (callback->cb_free_name)
3786 {
3787 vim_free(callback->cb_name);
3788 callback->cb_free_name = FALSE;
3789 }
3790 callback->cb_name = NULL;
3791}
3792
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003793#endif // FEAT_EVAL