blob: b5075d59cc69e8d47309880a1f462730b589fbba [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 Moolenaar2bb76ac2020-04-19 22:57:44 +02001431 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001432 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001433 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001434 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001435 // Parse the name and find the end.
1436 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1437 glv_flags, FNE_CHECK_START);
1438 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)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001442 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001443 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;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001452
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001453 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001454 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001455 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001457 if (!eap->skip)
1458 clear_lval(&lv);
1459 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001460
1461 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001462 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001463
1464 eap->nextcmd = check_nextcmd(arg);
1465}
1466
1467 static int
1468do_unlet_var(
1469 lval_T *lp,
1470 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001471 exarg_T *eap,
1472 int deep UNUSED,
1473 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001475 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 int ret = OK;
1477 int cc;
1478
1479 if (lp->ll_tv == NULL)
1480 {
1481 cc = *name_end;
1482 *name_end = NUL;
1483
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001484 // Environment variable, normal name or expanded name.
1485 if (*lp->ll_name == '$')
1486 vim_unsetenv(lp->ll_name + 1);
1487 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488 ret = FAIL;
1489 *name_end = cc;
1490 }
1491 else if ((lp->ll_list != NULL
1492 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1493 || (lp->ll_dict != NULL
1494 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1495 return FAIL;
1496 else if (lp->ll_range)
1497 {
1498 listitem_T *li;
1499 listitem_T *ll_li = lp->ll_li;
1500 int ll_n1 = lp->ll_n1;
1501
1502 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1503 {
1504 li = ll_li->li_next;
1505 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1506 return FAIL;
1507 ll_li = li;
1508 ++ll_n1;
1509 }
1510
1511 // Delete a range of List items.
1512 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1513 {
1514 li = lp->ll_li->li_next;
1515 listitem_remove(lp->ll_list, lp->ll_li);
1516 lp->ll_li = li;
1517 ++lp->ll_n1;
1518 }
1519 }
1520 else
1521 {
1522 if (lp->ll_list != NULL)
1523 // unlet a List item.
1524 listitem_remove(lp->ll_list, lp->ll_li);
1525 else
1526 // unlet a Dictionary item.
1527 dictitem_remove(lp->ll_dict, lp->ll_di);
1528 }
1529
1530 return ret;
1531}
1532
1533/*
1534 * "unlet" a variable. Return OK if it existed, FAIL if not.
1535 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1536 */
1537 int
1538do_unlet(char_u *name, int forceit)
1539{
1540 hashtab_T *ht;
1541 hashitem_T *hi;
1542 char_u *varname;
1543 dict_T *d;
1544 dictitem_T *di;
1545
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001546 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1547 && check_vim9_unlet(name) == FAIL)
1548 return FAIL;
1549
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001550 ht = find_var_ht(name, &varname);
1551 if (ht != NULL && *varname != NUL)
1552 {
1553 d = get_current_funccal_dict(ht);
1554 if (d == NULL)
1555 {
1556 if (ht == &globvarht)
1557 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001558 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001559 d = &vimvardict;
1560 else
1561 {
1562 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1563 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1564 }
1565 if (d == NULL)
1566 {
1567 internal_error("do_unlet()");
1568 return FAIL;
1569 }
1570 }
1571 hi = hash_find(ht, varname);
1572 if (HASHITEM_EMPTY(hi))
1573 hi = find_hi_in_scoped_ht(name, &ht);
1574 if (hi != NULL && !HASHITEM_EMPTY(hi))
1575 {
1576 di = HI2DI(hi);
1577 if (var_check_fixed(di->di_flags, name, FALSE)
1578 || var_check_ro(di->di_flags, name, FALSE)
1579 || var_check_lock(d->dv_lock, name, FALSE))
1580 return FAIL;
1581
1582 delete_var(ht, hi);
1583 return OK;
1584 }
1585 }
1586 if (forceit)
1587 return OK;
1588 semsg(_("E108: No such variable: \"%s\""), name);
1589 return FAIL;
1590}
1591
1592/*
1593 * Lock or unlock variable indicated by "lp".
1594 * "deep" is the levels to go (-1 for unlimited);
1595 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1596 */
1597 static int
1598do_lock_var(
1599 lval_T *lp,
1600 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001601 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001602 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001603 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001605 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 int ret = OK;
1607 int cc;
1608 dictitem_T *di;
1609
1610 if (deep == 0) // nothing to do
1611 return OK;
1612
1613 if (lp->ll_tv == NULL)
1614 {
1615 cc = *name_end;
1616 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001617 if (*lp->ll_name == '$')
1618 {
1619 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001620 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001621 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001622 else
1623 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001624 // Normal name or expanded name.
1625 di = find_var(lp->ll_name, NULL, TRUE);
1626 if (di == NULL)
1627 ret = FAIL;
1628 else if ((di->di_flags & DI_FLAGS_FIX)
1629 && di->di_tv.v_type != VAR_DICT
1630 && di->di_tv.v_type != VAR_LIST)
1631 {
1632 // For historic reasons this error is not given for a list or
1633 // dict. E.g., the b: dict could be locked/unlocked.
1634 semsg(_(e_lock_unlock), lp->ll_name);
1635 ret = FAIL;
1636 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001638 {
1639 if (lock)
1640 di->di_flags |= DI_FLAGS_LOCK;
1641 else
1642 di->di_flags &= ~DI_FLAGS_LOCK;
1643 item_lock(&di->di_tv, deep, lock);
1644 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001645 }
1646 *name_end = cc;
1647 }
1648 else if (lp->ll_range)
1649 {
1650 listitem_T *li = lp->ll_li;
1651
1652 // (un)lock a range of List items.
1653 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1654 {
1655 item_lock(&li->li_tv, deep, lock);
1656 li = li->li_next;
1657 ++lp->ll_n1;
1658 }
1659 }
1660 else if (lp->ll_list != NULL)
1661 // (un)lock a List item.
1662 item_lock(&lp->ll_li->li_tv, deep, lock);
1663 else
1664 // (un)lock a Dictionary item.
1665 item_lock(&lp->ll_di->di_tv, deep, lock);
1666
1667 return ret;
1668}
1669
1670/*
1671 * Lock or unlock an item. "deep" is nr of levels to go.
1672 */
1673 static void
1674item_lock(typval_T *tv, int deep, int lock)
1675{
1676 static int recurse = 0;
1677 list_T *l;
1678 listitem_T *li;
1679 dict_T *d;
1680 blob_T *b;
1681 hashitem_T *hi;
1682 int todo;
1683
1684 if (recurse >= DICT_MAXNEST)
1685 {
1686 emsg(_("E743: variable nested too deep for (un)lock"));
1687 return;
1688 }
1689 if (deep == 0)
1690 return;
1691 ++recurse;
1692
1693 // lock/unlock the item itself
1694 if (lock)
1695 tv->v_lock |= VAR_LOCKED;
1696 else
1697 tv->v_lock &= ~VAR_LOCKED;
1698
1699 switch (tv->v_type)
1700 {
1701 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001702 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001704 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001706 case VAR_STRING:
1707 case VAR_FUNC:
1708 case VAR_PARTIAL:
1709 case VAR_FLOAT:
1710 case VAR_SPECIAL:
1711 case VAR_JOB:
1712 case VAR_CHANNEL:
1713 break;
1714
1715 case VAR_BLOB:
1716 if ((b = tv->vval.v_blob) != NULL)
1717 {
1718 if (lock)
1719 b->bv_lock |= VAR_LOCKED;
1720 else
1721 b->bv_lock &= ~VAR_LOCKED;
1722 }
1723 break;
1724 case VAR_LIST:
1725 if ((l = tv->vval.v_list) != NULL)
1726 {
1727 if (lock)
1728 l->lv_lock |= VAR_LOCKED;
1729 else
1730 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001731 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001732 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001733 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 item_lock(&li->li_tv, deep - 1, lock);
1735 }
1736 break;
1737 case VAR_DICT:
1738 if ((d = tv->vval.v_dict) != NULL)
1739 {
1740 if (lock)
1741 d->dv_lock |= VAR_LOCKED;
1742 else
1743 d->dv_lock &= ~VAR_LOCKED;
1744 if (deep < 0 || deep > 1)
1745 {
1746 // recursive: lock/unlock the items the List contains
1747 todo = (int)d->dv_hashtab.ht_used;
1748 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1749 {
1750 if (!HASHITEM_EMPTY(hi))
1751 {
1752 --todo;
1753 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1754 }
1755 }
1756 }
1757 }
1758 }
1759 --recurse;
1760}
1761
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001762#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1763/*
1764 * Delete all "menutrans_" variables.
1765 */
1766 void
1767del_menutrans_vars(void)
1768{
1769 hashitem_T *hi;
1770 int todo;
1771
1772 hash_lock(&globvarht);
1773 todo = (int)globvarht.ht_used;
1774 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1775 {
1776 if (!HASHITEM_EMPTY(hi))
1777 {
1778 --todo;
1779 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1780 delete_var(&globvarht, hi);
1781 }
1782 }
1783 hash_unlock(&globvarht);
1784}
1785#endif
1786
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001788 * Local string buffer for the next two functions to store a variable name
1789 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1790 * get_user_var_name().
1791 */
1792
1793static char_u *varnamebuf = NULL;
1794static int varnamebuflen = 0;
1795
1796/*
1797 * Function to concatenate a prefix and a variable name.
1798 */
1799 static char_u *
1800cat_prefix_varname(int prefix, char_u *name)
1801{
1802 int len;
1803
1804 len = (int)STRLEN(name) + 3;
1805 if (len > varnamebuflen)
1806 {
1807 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001808 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001809 varnamebuf = alloc(len);
1810 if (varnamebuf == NULL)
1811 {
1812 varnamebuflen = 0;
1813 return NULL;
1814 }
1815 varnamebuflen = len;
1816 }
1817 *varnamebuf = prefix;
1818 varnamebuf[1] = ':';
1819 STRCPY(varnamebuf + 2, name);
1820 return varnamebuf;
1821}
1822
1823/*
1824 * Function given to ExpandGeneric() to obtain the list of user defined
1825 * (global/buffer/window/built-in) variable names.
1826 */
1827 char_u *
1828get_user_var_name(expand_T *xp, int idx)
1829{
1830 static long_u gdone;
1831 static long_u bdone;
1832 static long_u wdone;
1833 static long_u tdone;
1834 static int vidx;
1835 static hashitem_T *hi;
1836 hashtab_T *ht;
1837
1838 if (idx == 0)
1839 {
1840 gdone = bdone = wdone = vidx = 0;
1841 tdone = 0;
1842 }
1843
1844 // Global variables
1845 if (gdone < globvarht.ht_used)
1846 {
1847 if (gdone++ == 0)
1848 hi = globvarht.ht_array;
1849 else
1850 ++hi;
1851 while (HASHITEM_EMPTY(hi))
1852 ++hi;
1853 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1854 return cat_prefix_varname('g', hi->hi_key);
1855 return hi->hi_key;
1856 }
1857
1858 // b: variables
1859 ht = &curbuf->b_vars->dv_hashtab;
1860 if (bdone < ht->ht_used)
1861 {
1862 if (bdone++ == 0)
1863 hi = ht->ht_array;
1864 else
1865 ++hi;
1866 while (HASHITEM_EMPTY(hi))
1867 ++hi;
1868 return cat_prefix_varname('b', hi->hi_key);
1869 }
1870
1871 // w: variables
1872 ht = &curwin->w_vars->dv_hashtab;
1873 if (wdone < ht->ht_used)
1874 {
1875 if (wdone++ == 0)
1876 hi = ht->ht_array;
1877 else
1878 ++hi;
1879 while (HASHITEM_EMPTY(hi))
1880 ++hi;
1881 return cat_prefix_varname('w', hi->hi_key);
1882 }
1883
1884 // t: variables
1885 ht = &curtab->tp_vars->dv_hashtab;
1886 if (tdone < ht->ht_used)
1887 {
1888 if (tdone++ == 0)
1889 hi = ht->ht_array;
1890 else
1891 ++hi;
1892 while (HASHITEM_EMPTY(hi))
1893 ++hi;
1894 return cat_prefix_varname('t', hi->hi_key);
1895 }
1896
1897 // v: variables
1898 if (vidx < VV_LEN)
1899 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1900
1901 VIM_CLEAR(varnamebuf);
1902 varnamebuflen = 0;
1903 return NULL;
1904}
1905
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001906 char *
1907get_var_special_name(int nr)
1908{
1909 switch (nr)
1910 {
1911 case VVAL_FALSE: return "v:false";
1912 case VVAL_TRUE: return "v:true";
1913 case VVAL_NONE: return "v:none";
1914 case VVAL_NULL: return "v:null";
1915 }
1916 internal_error("get_var_special_name()");
1917 return "42";
1918}
1919
1920/*
1921 * Returns the global variable dictionary
1922 */
1923 dict_T *
1924get_globvar_dict(void)
1925{
1926 return &globvardict;
1927}
1928
1929/*
1930 * Returns the global variable hash table
1931 */
1932 hashtab_T *
1933get_globvar_ht(void)
1934{
1935 return &globvarht;
1936}
1937
1938/*
1939 * Returns the v: variable dictionary
1940 */
1941 dict_T *
1942get_vimvar_dict(void)
1943{
1944 return &vimvardict;
1945}
1946
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001947/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001949 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 */
1951 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001952find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001954 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1955 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956
1957 if (di == NULL)
1958 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001959 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1961 return (int)(vv - vimvars);
1962}
1963
1964
1965/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001966 * Set type of v: variable to "type".
1967 */
1968 void
1969set_vim_var_type(int idx, vartype_T type)
1970{
1971 vimvars[idx].vv_type = type;
1972}
1973
1974/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001975 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001976 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001977 */
1978 void
1979set_vim_var_nr(int idx, varnumber_T val)
1980{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001981 vimvars[idx].vv_nr = val;
1982}
1983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 char *
1985get_vim_var_name(int idx)
1986{
1987 return vimvars[idx].vv_name;
1988}
1989
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001990/*
1991 * Get typval_T v: variable value.
1992 */
1993 typval_T *
1994get_vim_var_tv(int idx)
1995{
1996 return &vimvars[idx].vv_tv;
1997}
1998
1999/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002000 * Set v: variable to "tv". Only accepts the same type.
2001 * Takes over the value of "tv".
2002 */
2003 int
2004set_vim_var_tv(int idx, typval_T *tv)
2005{
2006 if (vimvars[idx].vv_type != tv->v_type)
2007 {
2008 emsg(_("E1063: type mismatch for v: variable"));
2009 clear_tv(tv);
2010 return FAIL;
2011 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002012 // VV_RO is also checked when compiling, but let's check here as well.
2013 if (vimvars[idx].vv_flags & VV_RO)
2014 {
2015 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2016 return FAIL;
2017 }
2018 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2019 {
2020 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2021 return FAIL;
2022 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002023 clear_tv(&vimvars[idx].vv_di.di_tv);
2024 vimvars[idx].vv_di.di_tv = *tv;
2025 return OK;
2026}
2027
2028/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002029 * Get number v: variable value.
2030 */
2031 varnumber_T
2032get_vim_var_nr(int idx)
2033{
2034 return vimvars[idx].vv_nr;
2035}
2036
2037/*
2038 * Get string v: variable value. Uses a static buffer, can only be used once.
2039 * If the String variable has never been set, return an empty string.
2040 * Never returns NULL;
2041 */
2042 char_u *
2043get_vim_var_str(int idx)
2044{
2045 return tv_get_string(&vimvars[idx].vv_tv);
2046}
2047
2048/*
2049 * Get List v: variable value. Caller must take care of reference count when
2050 * needed.
2051 */
2052 list_T *
2053get_vim_var_list(int idx)
2054{
2055 return vimvars[idx].vv_list;
2056}
2057
2058/*
2059 * Get Dict v: variable value. Caller must take care of reference count when
2060 * needed.
2061 */
2062 dict_T *
2063get_vim_var_dict(int idx)
2064{
2065 return vimvars[idx].vv_dict;
2066}
2067
2068/*
2069 * Set v:char to character "c".
2070 */
2071 void
2072set_vim_var_char(int c)
2073{
2074 char_u buf[MB_MAXBYTES + 1];
2075
2076 if (has_mbyte)
2077 buf[(*mb_char2bytes)(c, buf)] = NUL;
2078 else
2079 {
2080 buf[0] = c;
2081 buf[1] = NUL;
2082 }
2083 set_vim_var_string(VV_CHAR, buf, -1);
2084}
2085
2086/*
2087 * Set v:count to "count" and v:count1 to "count1".
2088 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2089 */
2090 void
2091set_vcount(
2092 long count,
2093 long count1,
2094 int set_prevcount)
2095{
2096 if (set_prevcount)
2097 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2098 vimvars[VV_COUNT].vv_nr = count;
2099 vimvars[VV_COUNT1].vv_nr = count1;
2100}
2101
2102/*
2103 * Save variables that might be changed as a side effect. Used when executing
2104 * a timer callback.
2105 */
2106 void
2107save_vimvars(vimvars_save_T *vvsave)
2108{
2109 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2110 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2111 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2112}
2113
2114/*
2115 * Restore variables saved by save_vimvars().
2116 */
2117 void
2118restore_vimvars(vimvars_save_T *vvsave)
2119{
2120 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2121 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2122 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2123}
2124
2125/*
2126 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2127 * value.
2128 */
2129 void
2130set_vim_var_string(
2131 int idx,
2132 char_u *val,
2133 int len) // length of "val" to use or -1 (whole string)
2134{
2135 clear_tv(&vimvars[idx].vv_di.di_tv);
2136 vimvars[idx].vv_type = VAR_STRING;
2137 if (val == NULL)
2138 vimvars[idx].vv_str = NULL;
2139 else if (len == -1)
2140 vimvars[idx].vv_str = vim_strsave(val);
2141 else
2142 vimvars[idx].vv_str = vim_strnsave(val, len);
2143}
2144
2145/*
2146 * Set List v: variable to "val".
2147 */
2148 void
2149set_vim_var_list(int idx, list_T *val)
2150{
2151 clear_tv(&vimvars[idx].vv_di.di_tv);
2152 vimvars[idx].vv_type = VAR_LIST;
2153 vimvars[idx].vv_list = val;
2154 if (val != NULL)
2155 ++val->lv_refcount;
2156}
2157
2158/*
2159 * Set Dictionary v: variable to "val".
2160 */
2161 void
2162set_vim_var_dict(int idx, dict_T *val)
2163{
2164 clear_tv(&vimvars[idx].vv_di.di_tv);
2165 vimvars[idx].vv_type = VAR_DICT;
2166 vimvars[idx].vv_dict = val;
2167 if (val != NULL)
2168 {
2169 ++val->dv_refcount;
2170 dict_set_items_ro(val);
2171 }
2172}
2173
2174/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002175 * Set the v:argv list.
2176 */
2177 void
2178set_argv_var(char **argv, int argc)
2179{
2180 list_T *l = list_alloc();
2181 int i;
2182
2183 if (l == NULL)
2184 getout(1);
2185 l->lv_lock = VAR_FIXED;
2186 for (i = 0; i < argc; ++i)
2187 {
2188 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2189 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002190 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002191 }
2192 set_vim_var_list(VV_ARGV, l);
2193}
2194
2195/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002196 * Set v:register if needed.
2197 */
2198 void
2199set_reg_var(int c)
2200{
2201 char_u regname;
2202
2203 if (c == 0 || c == ' ')
2204 regname = '"';
2205 else
2206 regname = c;
2207 // Avoid free/alloc when the value is already right.
2208 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2209 set_vim_var_string(VV_REG, &regname, 1);
2210}
2211
2212/*
2213 * Get or set v:exception. If "oldval" == NULL, return the current value.
2214 * Otherwise, restore the value to "oldval" and return NULL.
2215 * Must always be called in pairs to save and restore v:exception! Does not
2216 * take care of memory allocations.
2217 */
2218 char_u *
2219v_exception(char_u *oldval)
2220{
2221 if (oldval == NULL)
2222 return vimvars[VV_EXCEPTION].vv_str;
2223
2224 vimvars[VV_EXCEPTION].vv_str = oldval;
2225 return NULL;
2226}
2227
2228/*
2229 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2230 * Otherwise, restore the value to "oldval" and return NULL.
2231 * Must always be called in pairs to save and restore v:throwpoint! Does not
2232 * take care of memory allocations.
2233 */
2234 char_u *
2235v_throwpoint(char_u *oldval)
2236{
2237 if (oldval == NULL)
2238 return vimvars[VV_THROWPOINT].vv_str;
2239
2240 vimvars[VV_THROWPOINT].vv_str = oldval;
2241 return NULL;
2242}
2243
2244/*
2245 * Set v:cmdarg.
2246 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2247 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2248 * Must always be called in pairs!
2249 */
2250 char_u *
2251set_cmdarg(exarg_T *eap, char_u *oldarg)
2252{
2253 char_u *oldval;
2254 char_u *newval;
2255 unsigned len;
2256
2257 oldval = vimvars[VV_CMDARG].vv_str;
2258 if (eap == NULL)
2259 {
2260 vim_free(oldval);
2261 vimvars[VV_CMDARG].vv_str = oldarg;
2262 return NULL;
2263 }
2264
2265 if (eap->force_bin == FORCE_BIN)
2266 len = 6;
2267 else if (eap->force_bin == FORCE_NOBIN)
2268 len = 8;
2269 else
2270 len = 0;
2271
2272 if (eap->read_edit)
2273 len += 7;
2274
2275 if (eap->force_ff != 0)
2276 len += 10; // " ++ff=unix"
2277 if (eap->force_enc != 0)
2278 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2279 if (eap->bad_char != 0)
2280 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2281
2282 newval = alloc(len + 1);
2283 if (newval == NULL)
2284 return NULL;
2285
2286 if (eap->force_bin == FORCE_BIN)
2287 sprintf((char *)newval, " ++bin");
2288 else if (eap->force_bin == FORCE_NOBIN)
2289 sprintf((char *)newval, " ++nobin");
2290 else
2291 *newval = NUL;
2292
2293 if (eap->read_edit)
2294 STRCAT(newval, " ++edit");
2295
2296 if (eap->force_ff != 0)
2297 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2298 eap->force_ff == 'u' ? "unix"
2299 : eap->force_ff == 'd' ? "dos"
2300 : "mac");
2301 if (eap->force_enc != 0)
2302 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2303 eap->cmd + eap->force_enc);
2304 if (eap->bad_char == BAD_KEEP)
2305 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2306 else if (eap->bad_char == BAD_DROP)
2307 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2308 else if (eap->bad_char != 0)
2309 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2310 vimvars[VV_CMDARG].vv_str = newval;
2311 return oldval;
2312}
2313
2314/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002315 * Get the value of internal variable "name".
2316 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2317 */
2318 int
2319get_var_tv(
2320 char_u *name,
2321 int len, // length of "name"
2322 typval_T *rettv, // NULL when only checking existence
2323 dictitem_T **dip, // non-NULL when typval's dict item is needed
2324 int verbose, // may give error message
2325 int no_autoload) // do not use script autoloading
2326{
2327 int ret = OK;
2328 typval_T *tv = NULL;
2329 dictitem_T *v;
2330 int cc;
2331
2332 // truncate the name, so that we can use strcmp()
2333 cc = name[len];
2334 name[len] = NUL;
2335
2336 // Check for user-defined variables.
2337 v = find_var(name, NULL, no_autoload);
2338 if (v != NULL)
2339 {
2340 tv = &v->di_tv;
2341 if (dip != NULL)
2342 *dip = v;
2343 }
2344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2346 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002347 imported_T *import = find_imported(name, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348
2349 // imported variable from another script
2350 if (import != NULL)
2351 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002352 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2354 + import->imp_var_vals_idx;
2355 tv = sv->sv_tv;
2356 }
2357 }
2358
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002359 if (tv == NULL)
2360 {
2361 if (rettv != NULL && verbose)
2362 semsg(_(e_undefvar), name);
2363 ret = FAIL;
2364 }
2365 else if (rettv != NULL)
2366 copy_tv(tv, rettv);
2367
2368 name[len] = cc;
2369
2370 return ret;
2371}
2372
2373/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002374 * Check if variable "name[len]" is a local variable or an argument.
2375 * If so, "*eval_lavars_used" is set to TRUE.
2376 */
2377 void
2378check_vars(char_u *name, int len)
2379{
2380 int cc;
2381 char_u *varname;
2382 hashtab_T *ht;
2383
2384 if (eval_lavars_used == NULL)
2385 return;
2386
2387 // truncate the name, so that we can use strcmp()
2388 cc = name[len];
2389 name[len] = NUL;
2390
2391 ht = find_var_ht(name, &varname);
2392 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2393 {
2394 if (find_var(name, NULL, TRUE) != NULL)
2395 *eval_lavars_used = TRUE;
2396 }
2397
2398 name[len] = cc;
2399}
2400
2401/*
2402 * Find variable "name" in the list of variables.
2403 * Return a pointer to it if found, NULL if not found.
2404 * Careful: "a:0" variables don't have a name.
2405 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2406 * hashtab_T used.
2407 */
2408 dictitem_T *
2409find_var(char_u *name, hashtab_T **htp, int no_autoload)
2410{
2411 char_u *varname;
2412 hashtab_T *ht;
2413 dictitem_T *ret = NULL;
2414
2415 ht = find_var_ht(name, &varname);
2416 if (htp != NULL)
2417 *htp = ht;
2418 if (ht == NULL)
2419 return NULL;
2420 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2421 if (ret != NULL)
2422 return ret;
2423
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002424 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002425 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2426}
2427
2428/*
2429 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002430 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002431 * Returns NULL if not found.
2432 */
2433 dictitem_T *
2434find_var_in_ht(
2435 hashtab_T *ht,
2436 int htname,
2437 char_u *varname,
2438 int no_autoload)
2439{
2440 hashitem_T *hi;
2441
2442 if (*varname == NUL)
2443 {
2444 // Must be something like "s:", otherwise "ht" would be NULL.
2445 switch (htname)
2446 {
2447 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2448 case 'g': return &globvars_var;
2449 case 'v': return &vimvars_var;
2450 case 'b': return &curbuf->b_bufvar;
2451 case 'w': return &curwin->w_winvar;
2452 case 't': return &curtab->tp_winvar;
2453 case 'l': return get_funccal_local_var();
2454 case 'a': return get_funccal_args_var();
2455 }
2456 return NULL;
2457 }
2458
2459 hi = hash_find(ht, varname);
2460 if (HASHITEM_EMPTY(hi))
2461 {
2462 // For global variables we may try auto-loading the script. If it
2463 // worked find the variable again. Don't auto-load a script if it was
2464 // loaded already, otherwise it would be loaded every time when
2465 // checking if a function name is a Funcref variable.
2466 if (ht == &globvarht && !no_autoload)
2467 {
2468 // Note: script_autoload() may make "hi" invalid. It must either
2469 // be obtained again or not used.
2470 if (!script_autoload(varname, FALSE) || aborting())
2471 return NULL;
2472 hi = hash_find(ht, varname);
2473 }
2474 if (HASHITEM_EMPTY(hi))
2475 return NULL;
2476 }
2477 return HI2DI(hi);
2478}
2479
2480/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 * Get the script-local hashtab. NULL if not in a script context.
2482 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002483 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484get_script_local_ht(void)
2485{
2486 scid_T sid = current_sctx.sc_sid;
2487
2488 if (sid > 0 && sid <= script_items.ga_len)
2489 return &SCRIPT_VARS(sid);
2490 return NULL;
2491}
2492
2493/*
2494 * Look for "name[len]" in script-local variables.
2495 * Return -1 when not found.
2496 */
2497 int
2498lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2499{
2500 hashtab_T *ht = get_script_local_ht();
2501 char_u buffer[30];
2502 char_u *p;
2503 int res;
2504 hashitem_T *hi;
2505
2506 if (ht == NULL)
2507 return -1;
2508 if (len < sizeof(buffer) - 1)
2509 {
2510 vim_strncpy(buffer, name, len);
2511 p = buffer;
2512 }
2513 else
2514 {
2515 p = vim_strnsave(name, (int)len);
2516 if (p == NULL)
2517 return -1;
2518 }
2519
2520 hi = hash_find(ht, p);
2521 res = HASHITEM_EMPTY(hi) ? -1 : 1;
2522
2523 // if not script-local, then perhaps imported
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002524 if (res == -1 && find_imported(p, 0, NULL) != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 res = 1;
2526
2527 if (p != buffer)
2528 vim_free(p);
2529 return res;
2530}
2531
2532/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002533 * Find the hashtab used for a variable name.
2534 * Return NULL if the name is not valid.
2535 * Set "varname" to the start of name without ':'.
2536 */
2537 hashtab_T *
2538find_var_ht(char_u *name, char_u **varname)
2539{
2540 hashitem_T *hi;
2541 hashtab_T *ht;
2542
2543 if (name[0] == NUL)
2544 return NULL;
2545 if (name[1] != ':')
2546 {
2547 // The name must not start with a colon or #.
2548 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2549 return NULL;
2550 *varname = name;
2551
2552 // "version" is "v:version" in all scopes if scriptversion < 3.
2553 // Same for a few other variables marked with VV_COMPAT.
2554 if (current_sctx.sc_version < 3)
2555 {
2556 hi = hash_find(&compat_hashtab, name);
2557 if (!HASHITEM_EMPTY(hi))
2558 return &compat_hashtab;
2559 }
2560
2561 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 if (ht != NULL)
2563 return ht; // local variable
2564
2565 // in Vim9 script items at the script level are script-local
2566 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2567 {
2568 ht = get_script_local_ht();
2569 if (ht != NULL)
2570 return ht;
2571 }
2572
2573 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002574 }
2575 *varname = name + 2;
2576 if (*name == 'g') // global variable
2577 return &globvarht;
2578 // There must be no ':' or '#' in the rest of the name, unless g: is used
2579 if (vim_strchr(name + 2, ':') != NULL
2580 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2581 return NULL;
2582 if (*name == 'b') // buffer variable
2583 return &curbuf->b_vars->dv_hashtab;
2584 if (*name == 'w') // window variable
2585 return &curwin->w_vars->dv_hashtab;
2586 if (*name == 't') // tab page variable
2587 return &curtab->tp_vars->dv_hashtab;
2588 if (*name == 'v') // v: variable
2589 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002590 if (get_current_funccal() != NULL
2591 && get_current_funccal()->func->uf_dfunc_idx < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002593 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 if (*name == 'a') // a: function argument
2595 return get_funccal_args_ht();
2596 if (*name == 'l') // l: local function variable
2597 return get_funccal_local_ht();
2598 }
2599 if (*name == 's') // script variable
2600 {
2601 ht = get_script_local_ht();
2602 if (ht != NULL)
2603 return ht;
2604 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002605 return NULL;
2606}
2607
2608/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002609 * Get the string value of a (global/local) variable.
2610 * Note: see tv_get_string() for how long the pointer remains valid.
2611 * Returns NULL when it doesn't exist.
2612 */
2613 char_u *
2614get_var_value(char_u *name)
2615{
2616 dictitem_T *v;
2617
2618 v = find_var(name, NULL, FALSE);
2619 if (v == NULL)
2620 return NULL;
2621 return tv_get_string(&v->di_tv);
2622}
2623
2624/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002625 * Allocate a new hashtab for a sourced script. It will be used while
2626 * sourcing this script and when executing functions defined in the script.
2627 */
2628 void
2629new_script_vars(scid_T id)
2630{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002631 scriptvar_T *sv;
2632
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002633 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2634 if (sv == NULL)
2635 return;
2636 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002637 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002638}
2639
2640/*
2641 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2642 * point to it.
2643 */
2644 void
2645init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2646{
2647 hash_init(&dict->dv_hashtab);
2648 dict->dv_lock = 0;
2649 dict->dv_scope = scope;
2650 dict->dv_refcount = DO_NOT_FREE_CNT;
2651 dict->dv_copyID = 0;
2652 dict_var->di_tv.vval.v_dict = dict;
2653 dict_var->di_tv.v_type = VAR_DICT;
2654 dict_var->di_tv.v_lock = VAR_FIXED;
2655 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2656 dict_var->di_key[0] = NUL;
2657}
2658
2659/*
2660 * Unreference a dictionary initialized by init_var_dict().
2661 */
2662 void
2663unref_var_dict(dict_T *dict)
2664{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002665 // Now the dict needs to be freed if no one else is using it, go back to
2666 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002667 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2668 dict_unref(dict);
2669}
2670
2671/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002672 * Clean up a list of internal variables.
2673 * Frees all allocated variables and the value they contain.
2674 * Clears hashtab "ht", does not free it.
2675 */
2676 void
2677vars_clear(hashtab_T *ht)
2678{
2679 vars_clear_ext(ht, TRUE);
2680}
2681
2682/*
2683 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2684 */
2685 void
2686vars_clear_ext(hashtab_T *ht, int free_val)
2687{
2688 int todo;
2689 hashitem_T *hi;
2690 dictitem_T *v;
2691
2692 hash_lock(ht);
2693 todo = (int)ht->ht_used;
2694 for (hi = ht->ht_array; todo > 0; ++hi)
2695 {
2696 if (!HASHITEM_EMPTY(hi))
2697 {
2698 --todo;
2699
2700 // Free the variable. Don't remove it from the hashtab,
2701 // ht_array might change then. hash_clear() takes care of it
2702 // later.
2703 v = HI2DI(hi);
2704 if (free_val)
2705 clear_tv(&v->di_tv);
2706 if (v->di_flags & DI_FLAGS_ALLOC)
2707 vim_free(v);
2708 }
2709 }
2710 hash_clear(ht);
2711 ht->ht_used = 0;
2712}
2713
2714/*
2715 * Delete a variable from hashtab "ht" at item "hi".
2716 * Clear the variable value and free the dictitem.
2717 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002718 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002719delete_var(hashtab_T *ht, hashitem_T *hi)
2720{
2721 dictitem_T *di = HI2DI(hi);
2722
2723 hash_remove(ht, hi);
2724 clear_tv(&di->di_tv);
2725 vim_free(di);
2726}
2727
2728/*
2729 * List the value of one internal variable.
2730 */
2731 static void
2732list_one_var(dictitem_T *v, char *prefix, int *first)
2733{
2734 char_u *tofree;
2735 char_u *s;
2736 char_u numbuf[NUMBUFLEN];
2737
2738 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2739 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2740 s == NULL ? (char_u *)"" : s, first);
2741 vim_free(tofree);
2742}
2743
2744 static void
2745list_one_var_a(
2746 char *prefix,
2747 char_u *name,
2748 int type,
2749 char_u *string,
2750 int *first) // when TRUE clear rest of screen and set to FALSE
2751{
2752 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2753 msg_start();
2754 msg_puts(prefix);
2755 if (name != NULL) // "a:" vars don't have a name stored
2756 msg_puts((char *)name);
2757 msg_putchar(' ');
2758 msg_advance(22);
2759 if (type == VAR_NUMBER)
2760 msg_putchar('#');
2761 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2762 msg_putchar('*');
2763 else if (type == VAR_LIST)
2764 {
2765 msg_putchar('[');
2766 if (*string == '[')
2767 ++string;
2768 }
2769 else if (type == VAR_DICT)
2770 {
2771 msg_putchar('{');
2772 if (*string == '{')
2773 ++string;
2774 }
2775 else
2776 msg_putchar(' ');
2777
2778 msg_outtrans(string);
2779
2780 if (type == VAR_FUNC || type == VAR_PARTIAL)
2781 msg_puts("()");
2782 if (*first)
2783 {
2784 msg_clr_eos();
2785 *first = FALSE;
2786 }
2787}
2788
2789/*
2790 * Set variable "name" to value in "tv".
2791 * If the variable already exists, the value is updated.
2792 * Otherwise the variable is created.
2793 */
2794 void
2795set_var(
2796 char_u *name,
2797 typval_T *tv,
2798 int copy) // make copy of value in "tv"
2799{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002801}
2802
2803/*
2804 * Set variable "name" to value in "tv".
2805 * If the variable already exists and "is_const" is FALSE the value is updated.
2806 * Otherwise the variable is created.
2807 */
2808 void
2809set_var_const(
2810 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002812 typval_T *tv,
2813 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002815{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002817 char_u *varname;
2818 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002820
2821 ht = find_var_ht(name, &varname);
2822 if (ht == NULL || *varname == NUL)
2823 {
2824 semsg(_(e_illvar), name);
2825 return;
2826 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 is_script_local = ht == get_script_local_ht();
2828
2829 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002830
2831 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 if (di == NULL)
2833 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002834
2835 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002837 return;
2838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002840 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002842 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 if (flags & LET_IS_CONST)
2844 {
2845 emsg(_(e_cannot_mod));
2846 return;
2847 }
2848
2849 if (var_check_ro(di->di_flags, name, FALSE)
2850 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2851 return;
2852
2853 if ((flags & LET_NO_COMMAND) == 0
2854 && is_script_local
2855 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2856 {
2857 semsg(_("E1041: Redefining script item %s"), name);
2858 return;
2859 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 else
2862 // can only redefine once
2863 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002864
2865 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002868 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002869 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002870 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002872 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002874 if (copy || tv->v_type != VAR_STRING)
2875 {
2876 char_u *val = tv_get_string(tv);
2877
2878 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002879 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 if (di->di_tv.vval.v_string == NULL)
2881 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002882 }
2883 else
2884 {
2885 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002887 tv->vval.v_string = NULL;
2888 }
2889 return;
2890 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002892 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002894 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002896#ifdef FEAT_SEARCH_EXTRA
2897 else if (STRCMP(varname, "hlsearch") == 0)
2898 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002900 redraw_all_later(SOME_VALID);
2901 }
2902#endif
2903 return;
2904 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002906 {
2907 semsg(_("E963: setting %s to value with wrong type"), name);
2908 return;
2909 }
2910 }
2911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002913 }
2914 else // add a new variable
2915 {
2916 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002917 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002918 {
2919 semsg(_(e_illvar), name);
2920 return;
2921 }
2922
2923 // Make sure the variable name is valid.
2924 if (!valid_varname(varname))
2925 return;
2926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2928 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002929 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 STRCPY(di->di_key, varname);
2931 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002932 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002934 return;
2935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 di->di_flags = DI_FLAGS_ALLOC;
2937 if (flags & LET_IS_CONST)
2938 di->di_flags |= DI_FLAGS_LOCK;
2939
2940 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2941 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002942 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943
2944 // Store a pointer to the typval_T, so that it can be found by
2945 // index instead of using a hastab lookup.
2946 if (ga_grow(&si->sn_var_vals, 1) == OK)
2947 {
2948 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2949 + si->sn_var_vals.ga_len;
2950 sv->sv_name = di->di_key;
2951 sv->sv_tv = &di->di_tv;
2952 sv->sv_type = type == NULL ? &t_any : type;
2953 sv->sv_const = (flags & LET_IS_CONST);
2954 sv->sv_export = is_export;
2955 ++si->sn_var_vals.ga_len;
2956
2957 // let ex_export() know the export worked.
2958 is_export = FALSE;
2959 }
2960 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002961 }
2962
2963 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002965 else
2966 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 di->di_tv = *tv;
2968 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002969 init_tv(tv);
2970 }
2971
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 if (flags & LET_IS_CONST)
2973 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002974}
2975
2976/*
2977 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2978 * Also give an error message.
2979 */
2980 int
2981var_check_ro(int flags, char_u *name, int use_gettext)
2982{
2983 if (flags & DI_FLAGS_RO)
2984 {
2985 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
2986 return TRUE;
2987 }
2988 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
2989 {
2990 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
2991 return TRUE;
2992 }
2993 return FALSE;
2994}
2995
2996/*
2997 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
2998 * Also give an error message.
2999 */
3000 int
3001var_check_fixed(int flags, char_u *name, int use_gettext)
3002{
3003 if (flags & DI_FLAGS_FIX)
3004 {
3005 semsg(_("E795: Cannot delete variable %s"),
3006 use_gettext ? (char_u *)_(name) : name);
3007 return TRUE;
3008 }
3009 return FALSE;
3010}
3011
3012/*
3013 * Check if a funcref is assigned to a valid variable name.
3014 * Return TRUE and give an error if not.
3015 */
3016 int
3017var_check_func_name(
3018 char_u *name, // points to start of variable name
3019 int new_var) // TRUE when creating the variable
3020{
3021 // Allow for w: b: s: and t:.
3022 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3023 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3024 ? name[2] : name[0]))
3025 {
3026 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3027 name);
3028 return TRUE;
3029 }
3030 // Don't allow hiding a function. When "v" is not NULL we might be
3031 // assigning another function to the same var, the type is checked
3032 // below.
3033 if (new_var && function_exists(name, FALSE))
3034 {
3035 semsg(_("E705: Variable name conflicts with existing function: %s"),
3036 name);
3037 return TRUE;
3038 }
3039 return FALSE;
3040}
3041
3042/*
3043 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3044 * Also give an error message, using "name" or _("name") when use_gettext is
3045 * TRUE.
3046 */
3047 int
3048var_check_lock(int lock, char_u *name, int use_gettext)
3049{
3050 if (lock & VAR_LOCKED)
3051 {
3052 semsg(_("E741: Value is locked: %s"),
3053 name == NULL ? (char_u *)_("Unknown")
3054 : use_gettext ? (char_u *)_(name)
3055 : name);
3056 return TRUE;
3057 }
3058 if (lock & VAR_FIXED)
3059 {
3060 semsg(_("E742: Cannot change value of %s"),
3061 name == NULL ? (char_u *)_("Unknown")
3062 : use_gettext ? (char_u *)_(name)
3063 : name);
3064 return TRUE;
3065 }
3066 return FALSE;
3067}
3068
3069/*
3070 * Check if a variable name is valid.
3071 * Return FALSE and give an error if not.
3072 */
3073 int
3074valid_varname(char_u *varname)
3075{
3076 char_u *p;
3077
3078 for (p = varname; *p != NUL; ++p)
3079 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3080 && *p != AUTOLOAD_CHAR)
3081 {
3082 semsg(_(e_illvar), varname);
3083 return FALSE;
3084 }
3085 return TRUE;
3086}
3087
3088/*
3089 * getwinvar() and gettabwinvar()
3090 */
3091 static void
3092getwinvar(
3093 typval_T *argvars,
3094 typval_T *rettv,
3095 int off) // 1 for gettabwinvar()
3096{
3097 win_T *win;
3098 char_u *varname;
3099 dictitem_T *v;
3100 tabpage_T *tp = NULL;
3101 int done = FALSE;
3102 win_T *oldcurwin;
3103 tabpage_T *oldtabpage;
3104 int need_switch_win;
3105
3106 if (off == 1)
3107 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3108 else
3109 tp = curtab;
3110 win = find_win_by_nr(&argvars[off], tp);
3111 varname = tv_get_string_chk(&argvars[off + 1]);
3112 ++emsg_off;
3113
3114 rettv->v_type = VAR_STRING;
3115 rettv->vval.v_string = NULL;
3116
3117 if (win != NULL && varname != NULL)
3118 {
3119 // Set curwin to be our win, temporarily. Also set the tabpage,
3120 // otherwise the window is not valid. Only do this when needed,
3121 // autocommands get blocked.
3122 need_switch_win = !(tp == curtab && win == curwin);
3123 if (!need_switch_win
3124 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3125 {
3126 if (*varname == '&')
3127 {
3128 if (varname[1] == NUL)
3129 {
3130 // get all window-local options in a dict
3131 dict_T *opts = get_winbuf_options(FALSE);
3132
3133 if (opts != NULL)
3134 {
3135 rettv_dict_set(rettv, opts);
3136 done = TRUE;
3137 }
3138 }
3139 else if (get_option_tv(&varname, rettv, 1) == OK)
3140 // window-local-option
3141 done = TRUE;
3142 }
3143 else
3144 {
3145 // Look up the variable.
3146 // Let getwinvar({nr}, "") return the "w:" dictionary.
3147 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3148 varname, FALSE);
3149 if (v != NULL)
3150 {
3151 copy_tv(&v->di_tv, rettv);
3152 done = TRUE;
3153 }
3154 }
3155 }
3156
3157 if (need_switch_win)
3158 // restore previous notion of curwin
3159 restore_win(oldcurwin, oldtabpage, TRUE);
3160 }
3161
3162 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3163 // use the default return value
3164 copy_tv(&argvars[off + 2], rettv);
3165
3166 --emsg_off;
3167}
3168
3169/*
3170 * "setwinvar()" and "settabwinvar()" functions
3171 */
3172 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003173setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174{
3175 win_T *win;
3176 win_T *save_curwin;
3177 tabpage_T *save_curtab;
3178 int need_switch_win;
3179 char_u *varname, *winvarname;
3180 typval_T *varp;
3181 char_u nbuf[NUMBUFLEN];
3182 tabpage_T *tp = NULL;
3183
3184 if (check_secure())
3185 return;
3186
3187 if (off == 1)
3188 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3189 else
3190 tp = curtab;
3191 win = find_win_by_nr(&argvars[off], tp);
3192 varname = tv_get_string_chk(&argvars[off + 1]);
3193 varp = &argvars[off + 2];
3194
3195 if (win != NULL && varname != NULL && varp != NULL)
3196 {
3197 need_switch_win = !(tp == curtab && win == curwin);
3198 if (!need_switch_win
3199 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3200 {
3201 if (*varname == '&')
3202 {
3203 long numval;
3204 char_u *strval;
3205 int error = FALSE;
3206
3207 ++varname;
3208 numval = (long)tv_get_number_chk(varp, &error);
3209 strval = tv_get_string_buf_chk(varp, nbuf);
3210 if (!error && strval != NULL)
3211 set_option_value(varname, numval, strval, OPT_LOCAL);
3212 }
3213 else
3214 {
3215 winvarname = alloc(STRLEN(varname) + 3);
3216 if (winvarname != NULL)
3217 {
3218 STRCPY(winvarname, "w:");
3219 STRCPY(winvarname + 2, varname);
3220 set_var(winvarname, varp, TRUE);
3221 vim_free(winvarname);
3222 }
3223 }
3224 }
3225 if (need_switch_win)
3226 restore_win(save_curwin, save_curtab, TRUE);
3227 }
3228}
3229
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003230/*
3231 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3232 * v:option_type, and v:option_command.
3233 */
3234 void
3235reset_v_option_vars(void)
3236{
3237 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3238 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3239 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3240 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3241 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3242 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3243}
3244
3245/*
3246 * Add an assert error to v:errors.
3247 */
3248 void
3249assert_error(garray_T *gap)
3250{
3251 struct vimvar *vp = &vimvars[VV_ERRORS];
3252
3253 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003254 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003255 set_vim_var_list(VV_ERRORS, list_alloc());
3256 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3257}
3258
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003259 int
3260var_exists(char_u *var)
3261{
3262 char_u *name;
3263 char_u *tofree;
3264 typval_T tv;
3265 int len = 0;
3266 int n = FALSE;
3267
3268 // get_name_len() takes care of expanding curly braces
3269 name = var;
3270 len = get_name_len(&var, &tofree, TRUE, FALSE);
3271 if (len > 0)
3272 {
3273 if (tofree != NULL)
3274 name = tofree;
3275 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3276 if (n)
3277 {
3278 // handle d.key, l[idx], f(expr)
3279 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
3280 if (n)
3281 clear_tv(&tv);
3282 }
3283 }
3284 if (*var != NUL)
3285 n = FALSE;
3286
3287 vim_free(tofree);
3288 return n;
3289}
3290
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003291static lval_T *redir_lval = NULL;
3292#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3293static garray_T redir_ga; // only valid when redir_lval is not NULL
3294static char_u *redir_endp = NULL;
3295static char_u *redir_varname = NULL;
3296
3297/*
3298 * Start recording command output to a variable
3299 * When "append" is TRUE append to an existing variable.
3300 * Returns OK if successfully completed the setup. FAIL otherwise.
3301 */
3302 int
3303var_redir_start(char_u *name, int append)
3304{
3305 int save_emsg;
3306 int err;
3307 typval_T tv;
3308
3309 // Catch a bad name early.
3310 if (!eval_isnamec1(*name))
3311 {
3312 emsg(_(e_invarg));
3313 return FAIL;
3314 }
3315
3316 // Make a copy of the name, it is used in redir_lval until redir ends.
3317 redir_varname = vim_strsave(name);
3318 if (redir_varname == NULL)
3319 return FAIL;
3320
3321 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3322 if (redir_lval == NULL)
3323 {
3324 var_redir_stop();
3325 return FAIL;
3326 }
3327
3328 // The output is stored in growarray "redir_ga" until redirection ends.
3329 ga_init2(&redir_ga, (int)sizeof(char), 500);
3330
3331 // Parse the variable name (can be a dict or list entry).
3332 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3333 FNE_CHECK_START);
3334 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3335 {
3336 clear_lval(redir_lval);
3337 if (redir_endp != NULL && *redir_endp != NUL)
3338 // Trailing characters are present after the variable name
3339 emsg(_(e_trailing));
3340 else
3341 emsg(_(e_invarg));
3342 redir_endp = NULL; // don't store a value, only cleanup
3343 var_redir_stop();
3344 return FAIL;
3345 }
3346
3347 // check if we can write to the variable: set it to or append an empty
3348 // string
3349 save_emsg = did_emsg;
3350 did_emsg = FALSE;
3351 tv.v_type = VAR_STRING;
3352 tv.vval.v_string = (char_u *)"";
3353 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003355 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003357 clear_lval(redir_lval);
3358 err = did_emsg;
3359 did_emsg |= save_emsg;
3360 if (err)
3361 {
3362 redir_endp = NULL; // don't store a value, only cleanup
3363 var_redir_stop();
3364 return FAIL;
3365 }
3366
3367 return OK;
3368}
3369
3370/*
3371 * Append "value[value_len]" to the variable set by var_redir_start().
3372 * The actual appending is postponed until redirection ends, because the value
3373 * appended may in fact be the string we write to, changing it may cause freed
3374 * memory to be used:
3375 * :redir => foo
3376 * :let foo
3377 * :redir END
3378 */
3379 void
3380var_redir_str(char_u *value, int value_len)
3381{
3382 int len;
3383
3384 if (redir_lval == NULL)
3385 return;
3386
3387 if (value_len == -1)
3388 len = (int)STRLEN(value); // Append the entire string
3389 else
3390 len = value_len; // Append only "value_len" characters
3391
3392 if (ga_grow(&redir_ga, len) == OK)
3393 {
3394 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3395 redir_ga.ga_len += len;
3396 }
3397 else
3398 var_redir_stop();
3399}
3400
3401/*
3402 * Stop redirecting command output to a variable.
3403 * Frees the allocated memory.
3404 */
3405 void
3406var_redir_stop(void)
3407{
3408 typval_T tv;
3409
3410 if (EVALCMD_BUSY)
3411 {
3412 redir_lval = NULL;
3413 return;
3414 }
3415
3416 if (redir_lval != NULL)
3417 {
3418 // If there was no error: assign the text to the variable.
3419 if (redir_endp != NULL)
3420 {
3421 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3422 tv.v_type = VAR_STRING;
3423 tv.vval.v_string = redir_ga.ga_data;
3424 // Call get_lval() again, if it's inside a Dict or List it may
3425 // have changed.
3426 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3427 FALSE, FALSE, 0, FNE_CHECK_START);
3428 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003430 (char_u *)".");
3431 clear_lval(redir_lval);
3432 }
3433
3434 // free the collected output
3435 VIM_CLEAR(redir_ga.ga_data);
3436
3437 VIM_CLEAR(redir_lval);
3438 }
3439 VIM_CLEAR(redir_varname);
3440}
3441
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003442/*
3443 * "gettabvar()" function
3444 */
3445 void
3446f_gettabvar(typval_T *argvars, typval_T *rettv)
3447{
3448 win_T *oldcurwin;
3449 tabpage_T *tp, *oldtabpage;
3450 dictitem_T *v;
3451 char_u *varname;
3452 int done = FALSE;
3453
3454 rettv->v_type = VAR_STRING;
3455 rettv->vval.v_string = NULL;
3456
3457 varname = tv_get_string_chk(&argvars[1]);
3458 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3459 if (tp != NULL && varname != NULL)
3460 {
3461 // Set tp to be our tabpage, temporarily. Also set the window to the
3462 // first window in the tabpage, otherwise the window is not valid.
3463 if (switch_win(&oldcurwin, &oldtabpage,
3464 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3465 : tp->tp_firstwin, tp, TRUE) == OK)
3466 {
3467 // look up the variable
3468 // Let gettabvar({nr}, "") return the "t:" dictionary.
3469 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3470 if (v != NULL)
3471 {
3472 copy_tv(&v->di_tv, rettv);
3473 done = TRUE;
3474 }
3475 }
3476
3477 // restore previous notion of curwin
3478 restore_win(oldcurwin, oldtabpage, TRUE);
3479 }
3480
3481 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3482 copy_tv(&argvars[2], rettv);
3483}
3484
3485/*
3486 * "gettabwinvar()" function
3487 */
3488 void
3489f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3490{
3491 getwinvar(argvars, rettv, 1);
3492}
3493
3494/*
3495 * "getwinvar()" function
3496 */
3497 void
3498f_getwinvar(typval_T *argvars, typval_T *rettv)
3499{
3500 getwinvar(argvars, rettv, 0);
3501}
3502
3503/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003504 * "getbufvar()" function
3505 */
3506 void
3507f_getbufvar(typval_T *argvars, typval_T *rettv)
3508{
3509 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003510 char_u *varname;
3511 dictitem_T *v;
3512 int done = FALSE;
3513
3514 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3515 varname = tv_get_string_chk(&argvars[1]);
3516 ++emsg_off;
3517 buf = tv_get_buf(&argvars[0], FALSE);
3518
3519 rettv->v_type = VAR_STRING;
3520 rettv->vval.v_string = NULL;
3521
3522 if (buf != NULL && varname != NULL)
3523 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003524 if (*varname == '&')
3525 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003526 buf_T *save_curbuf = curbuf;
3527
3528 // set curbuf to be our buf, temporarily
3529 curbuf = buf;
3530
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003531 if (varname[1] == NUL)
3532 {
3533 // get all buffer-local options in a dict
3534 dict_T *opts = get_winbuf_options(TRUE);
3535
3536 if (opts != NULL)
3537 {
3538 rettv_dict_set(rettv, opts);
3539 done = TRUE;
3540 }
3541 }
3542 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3543 // buffer-local-option
3544 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003545
3546 // restore previous notion of curbuf
3547 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003548 }
3549 else
3550 {
3551 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003552 if (*varname == NUL)
3553 // Let getbufvar({nr}, "") return the "b:" dictionary.
3554 v = &buf->b_bufvar;
3555 else
3556 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3557 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003558 if (v != NULL)
3559 {
3560 copy_tv(&v->di_tv, rettv);
3561 done = TRUE;
3562 }
3563 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003564 }
3565
3566 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3567 // use the default value
3568 copy_tv(&argvars[2], rettv);
3569
3570 --emsg_off;
3571}
3572
3573/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003574 * "settabvar()" function
3575 */
3576 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003577f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003578{
3579 tabpage_T *save_curtab;
3580 tabpage_T *tp;
3581 char_u *varname, *tabvarname;
3582 typval_T *varp;
3583
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003584 if (check_secure())
3585 return;
3586
3587 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3588 varname = tv_get_string_chk(&argvars[1]);
3589 varp = &argvars[2];
3590
3591 if (varname != NULL && varp != NULL && tp != NULL)
3592 {
3593 save_curtab = curtab;
3594 goto_tabpage_tp(tp, FALSE, FALSE);
3595
3596 tabvarname = alloc(STRLEN(varname) + 3);
3597 if (tabvarname != NULL)
3598 {
3599 STRCPY(tabvarname, "t:");
3600 STRCPY(tabvarname + 2, varname);
3601 set_var(tabvarname, varp, TRUE);
3602 vim_free(tabvarname);
3603 }
3604
3605 // Restore current tabpage
3606 if (valid_tabpage(save_curtab))
3607 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3608 }
3609}
3610
3611/*
3612 * "settabwinvar()" function
3613 */
3614 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003615f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003616{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003617 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003618}
3619
3620/*
3621 * "setwinvar()" function
3622 */
3623 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003624f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003625{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003626 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003627}
3628
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003629/*
3630 * "setbufvar()" function
3631 */
3632 void
3633f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3634{
3635 buf_T *buf;
3636 char_u *varname, *bufvarname;
3637 typval_T *varp;
3638 char_u nbuf[NUMBUFLEN];
3639
3640 if (check_secure())
3641 return;
3642 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3643 varname = tv_get_string_chk(&argvars[1]);
3644 buf = tv_get_buf(&argvars[0], FALSE);
3645 varp = &argvars[2];
3646
3647 if (buf != NULL && varname != NULL && varp != NULL)
3648 {
3649 if (*varname == '&')
3650 {
3651 long numval;
3652 char_u *strval;
3653 int error = FALSE;
3654 aco_save_T aco;
3655
3656 // set curbuf to be our buf, temporarily
3657 aucmd_prepbuf(&aco, buf);
3658
3659 ++varname;
3660 numval = (long)tv_get_number_chk(varp, &error);
3661 strval = tv_get_string_buf_chk(varp, nbuf);
3662 if (!error && strval != NULL)
3663 set_option_value(varname, numval, strval, OPT_LOCAL);
3664
3665 // reset notion of buffer
3666 aucmd_restbuf(&aco);
3667 }
3668 else
3669 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003670 bufvarname = alloc(STRLEN(varname) + 3);
3671 if (bufvarname != NULL)
3672 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003673 buf_T *save_curbuf = curbuf;
3674
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003675 curbuf = buf;
3676 STRCPY(bufvarname, "b:");
3677 STRCPY(bufvarname + 2, varname);
3678 set_var(bufvarname, varp, TRUE);
3679 vim_free(bufvarname);
3680 curbuf = save_curbuf;
3681 }
3682 }
3683 }
3684}
3685
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003686/*
3687 * Get a callback from "arg". It can be a Funcref or a function name.
3688 * When "arg" is zero return an empty string.
3689 * "cb_name" is not allocated.
3690 * "cb_name" is set to NULL for an invalid argument.
3691 */
3692 callback_T
3693get_callback(typval_T *arg)
3694{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003695 callback_T res;
3696 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003697
3698 res.cb_free_name = FALSE;
3699 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3700 {
3701 res.cb_partial = arg->vval.v_partial;
3702 ++res.cb_partial->pt_refcount;
3703 res.cb_name = partial_name(res.cb_partial);
3704 }
3705 else
3706 {
3707 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003708 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3709 && isdigit(*arg->vval.v_string))
3710 r = FAIL;
3711 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003712 {
3713 // Note that we don't make a copy of the string.
3714 res.cb_name = arg->vval.v_string;
3715 func_ref(res.cb_name);
3716 }
3717 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003718 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003719 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003720 r = FAIL;
3721
3722 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003723 {
3724 emsg(_("E921: Invalid callback argument"));
3725 res.cb_name = NULL;
3726 }
3727 }
3728 return res;
3729}
3730
3731/*
3732 * Copy a callback into a typval_T.
3733 */
3734 void
3735put_callback(callback_T *cb, typval_T *tv)
3736{
3737 if (cb->cb_partial != NULL)
3738 {
3739 tv->v_type = VAR_PARTIAL;
3740 tv->vval.v_partial = cb->cb_partial;
3741 ++tv->vval.v_partial->pt_refcount;
3742 }
3743 else
3744 {
3745 tv->v_type = VAR_FUNC;
3746 tv->vval.v_string = vim_strsave(cb->cb_name);
3747 func_ref(cb->cb_name);
3748 }
3749}
3750
3751/*
3752 * Make a copy of "src" into "dest", allocating the function name if needed,
3753 * without incrementing the refcount.
3754 */
3755 void
3756set_callback(callback_T *dest, callback_T *src)
3757{
3758 if (src->cb_partial == NULL)
3759 {
3760 // just a function name, make a copy
3761 dest->cb_name = vim_strsave(src->cb_name);
3762 dest->cb_free_name = TRUE;
3763 }
3764 else
3765 {
3766 // cb_name is a pointer into cb_partial
3767 dest->cb_name = src->cb_name;
3768 dest->cb_free_name = FALSE;
3769 }
3770 dest->cb_partial = src->cb_partial;
3771}
3772
3773/*
3774 * Unref/free "callback" returned by get_callback() or set_callback().
3775 */
3776 void
3777free_callback(callback_T *callback)
3778{
3779 if (callback->cb_partial != NULL)
3780 {
3781 partial_unref(callback->cb_partial);
3782 callback->cb_partial = NULL;
3783 }
3784 else if (callback->cb_name != NULL)
3785 func_unref(callback->cb_name);
3786 if (callback->cb_free_name)
3787 {
3788 vim_free(callback->cb_name);
3789 callback->cb_free_name = FALSE;
3790 }
3791 callback->cb_name = NULL;
3792}
3793
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003794#endif // FEAT_EVAL