blob: 9aa6d6554a6f621fe113520240dee73e9ffd5933 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200176static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
217 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
218 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
222 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
223 set_vim_var_list(VV_ERRORS, list_alloc());
224 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
225
226 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
227 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
228 set_vim_var_nr(VV_NONE, VVAL_NONE);
229 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100230 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
233 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
234 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
235 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
236 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
237 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
238 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
239 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
240 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
241 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
242 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
243
244 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
245
Bram Moolenaar439c0362020-06-06 15:58:03 +0200246 // Default for v:register is not 0 but '"'. This is adjusted once the
247 // clipboard has been setup by calling reset_reg_var().
248 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200249}
250
251#if defined(EXITFREE) || defined(PROTO)
252/*
253 * Free all vim variables information on exit
254 */
255 void
256evalvars_clear(void)
257{
258 int i;
259 struct vimvar *p;
260
261 for (i = 0; i < VV_LEN; ++i)
262 {
263 p = &vimvars[i];
264 if (p->vv_di.di_tv.v_type == VAR_STRING)
265 VIM_CLEAR(p->vv_str);
266 else if (p->vv_di.di_tv.v_type == VAR_LIST)
267 {
268 list_unref(p->vv_list);
269 p->vv_list = NULL;
270 }
271 }
272 hash_clear(&vimvarht);
273 hash_init(&vimvarht); // garbage_collect() will access it
274 hash_clear(&compat_hashtab);
275
276 // global variables
277 vars_clear(&globvarht);
278
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100279 // Script-local variables. Clear all the variables here.
280 // The scriptvar_T is cleared later in free_scriptnames(), because a
281 // variable in one script might hold a reference to the whole scope of
282 // another script.
283 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285}
286#endif
287
288 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200289garbage_collect_globvars(int copyID)
290{
291 return set_ref_in_ht(&globvarht, copyID, NULL);
292}
293
294 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200295garbage_collect_vimvars(int copyID)
296{
297 return set_ref_in_ht(&vimvarht, copyID, NULL);
298}
299
300 int
301garbage_collect_scriptvars(int copyID)
302{
303 int i;
304 int abort = FALSE;
305
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100306 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200307 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
308
309 return abort;
310}
311
312/*
313 * Set an internal variable to a string value. Creates the variable if it does
314 * not already exist.
315 */
316 void
317set_internal_string_var(char_u *name, char_u *value)
318{
319 char_u *val;
320 typval_T *tvp;
321
322 val = vim_strsave(value);
323 if (val != NULL)
324 {
325 tvp = alloc_string_tv(val);
326 if (tvp != NULL)
327 {
328 set_var(name, tvp, FALSE);
329 free_tv(tvp);
330 }
331 }
332}
333
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200334 int
335eval_charconvert(
336 char_u *enc_from,
337 char_u *enc_to,
338 char_u *fname_from,
339 char_u *fname_to)
340{
341 int err = FALSE;
342
343 set_vim_var_string(VV_CC_FROM, enc_from, -1);
344 set_vim_var_string(VV_CC_TO, enc_to, -1);
345 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
346 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
347 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
348 err = TRUE;
349 set_vim_var_string(VV_CC_FROM, NULL, -1);
350 set_vim_var_string(VV_CC_TO, NULL, -1);
351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
352 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
353
354 if (err)
355 return FAIL;
356 return OK;
357}
358
359# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
360 int
361eval_printexpr(char_u *fname, char_u *args)
362{
363 int err = FALSE;
364
365 set_vim_var_string(VV_FNAME_IN, fname, -1);
366 set_vim_var_string(VV_CMDARG, args, -1);
367 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
368 err = TRUE;
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_CMDARG, NULL, -1);
371
372 if (err)
373 {
374 mch_remove(fname);
375 return FAIL;
376 }
377 return OK;
378}
379# endif
380
381# if defined(FEAT_DIFF) || defined(PROTO)
382 void
383eval_diff(
384 char_u *origfile,
385 char_u *newfile,
386 char_u *outfile)
387{
388 int err = FALSE;
389
390 set_vim_var_string(VV_FNAME_IN, origfile, -1);
391 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
392 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
393 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
394 set_vim_var_string(VV_FNAME_IN, NULL, -1);
395 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
396 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
397}
398
399 void
400eval_patch(
401 char_u *origfile,
402 char_u *difffile,
403 char_u *outfile)
404{
405 int err;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415# endif
416
417#if defined(FEAT_SPELL) || defined(PROTO)
418/*
419 * Evaluate an expression to a list with suggestions.
420 * For the "expr:" part of 'spellsuggest'.
421 * Returns NULL when there is an error.
422 */
423 list_T *
424eval_spell_expr(char_u *badword, char_u *expr)
425{
426 typval_T save_val;
427 typval_T rettv;
428 list_T *list = NULL;
429 char_u *p = skipwhite(expr);
430
431 // Set "v:val" to the bad word.
432 prepare_vimvar(VV_VAL, &save_val);
433 set_vim_var_string(VV_VAL, badword, -1);
434 if (p_verbose == 0)
435 ++emsg_off;
436
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200437 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438 {
439 if (rettv.v_type != VAR_LIST)
440 clear_tv(&rettv);
441 else
442 list = rettv.vval.v_list;
443 }
444
445 if (p_verbose == 0)
446 --emsg_off;
447 clear_tv(get_vim_var_tv(VV_VAL));
448 restore_vimvar(VV_VAL, &save_val);
449
450 return list;
451}
452
453/*
454 * "list" is supposed to contain two items: a word and a number. Return the
455 * word in "pp" and the number as the return value.
456 * Return -1 if anything isn't right.
457 * Used to get the good word and score from the eval_spell_expr() result.
458 */
459 int
460get_spellword(list_T *list, char_u **pp)
461{
462 listitem_T *li;
463
464 li = list->lv_first;
465 if (li == NULL)
466 return -1;
467 *pp = tv_get_string(&li->li_tv);
468
469 li = li->li_next;
470 if (li == NULL)
471 return -1;
472 return (int)tv_get_number(&li->li_tv);
473}
474#endif
475
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200476/*
477 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200478 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200479 * When not used yet add the variable to the v: hashtable.
480 */
481 void
482prepare_vimvar(int idx, typval_T *save_tv)
483{
484 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200485 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
488}
489
490/*
491 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200492 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493 * When no longer defined, remove the variable from the v: hashtable.
494 */
495 void
496restore_vimvar(int idx, typval_T *save_tv)
497{
498 hashitem_T *hi;
499
500 vimvars[idx].vv_tv = *save_tv;
501 if (vimvars[idx].vv_type == VAR_UNKNOWN)
502 {
503 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
504 if (HASHITEM_EMPTY(hi))
505 internal_error("restore_vimvar()");
506 else
507 hash_remove(&vimvarht, hi);
508 }
509}
510
511/*
512 * List Vim variables.
513 */
514 static void
515list_vim_vars(int *first)
516{
517 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
518}
519
520/*
521 * List script-local variables, if there is a script.
522 */
523 static void
524list_script_vars(int *first)
525{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200526 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200527 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
528 "s:", FALSE, first);
529}
530
531/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200532 * Get a list of lines from a HERE document. The here document is a list of
533 * lines surrounded by a marker.
534 * cmd << {marker}
535 * {line1}
536 * {line2}
537 * ....
538 * {marker}
539 *
540 * The {marker} is a string. If the optional 'trim' word is supplied before the
541 * marker, then the leading indentation before the lines (matching the
542 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200543 *
544 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
545 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
546 * missing, then '.' is accepted as a marker.
547 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200548 * Returns a List with {lines} or NULL.
549 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200551heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200552{
553 char_u *theline;
554 char_u *marker;
555 list_T *l;
556 char_u *p;
557 int marker_indent_len = 0;
558 int text_indent_len = 0;
559 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200560 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200561 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200562
563 if (eap->getline == NULL)
564 {
565 emsg(_("E991: cannot use =<< here"));
566 return NULL;
567 }
568
569 // Check for the optional 'trim' word before the marker
570 cmd = skipwhite(cmd);
571 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
572 {
573 cmd = skipwhite(cmd + 4);
574
575 // Trim the indentation from all the lines in the here document.
576 // The amount of indentation trimmed is the same as the indentation of
577 // the first line after the :let command line. To find the end marker
578 // the indent of the :let command line is trimmed.
579 p = *eap->cmdlinep;
580 while (VIM_ISWHITE(*p))
581 {
582 p++;
583 marker_indent_len++;
584 }
585 text_indent_len = -1;
586 }
587
588 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200589 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200590 {
591 marker = skipwhite(cmd);
592 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200593 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200594 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200595 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200596 return NULL;
597 }
598 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200599 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 {
601 emsg(_("E221: Marker cannot start with lower case letter"));
602 return NULL;
603 }
604 }
605 else
606 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200607 // When getting lines for an embedded script, if the marker is missing,
608 // accept '.' as the marker.
609 if (script_get)
610 marker = dot;
611 else
612 {
613 emsg(_("E172: Missing marker"));
614 return NULL;
615 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200616 }
617
618 l = list_alloc();
619 if (l == NULL)
620 return NULL;
621
622 for (;;)
623 {
624 int mi = 0;
625 int ti = 0;
626
627 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
628 if (theline == NULL)
629 {
630 semsg(_("E990: Missing end marker '%s'"), marker);
631 break;
632 }
633
634 // with "trim": skip the indent matching the :let line to find the
635 // marker
636 if (marker_indent_len > 0
637 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
638 mi = marker_indent_len;
639 if (STRCMP(marker, theline + mi) == 0)
640 {
641 vim_free(theline);
642 break;
643 }
644
645 if (text_indent_len == -1 && *theline != NUL)
646 {
647 // set the text indent from the first line.
648 p = theline;
649 text_indent_len = 0;
650 while (VIM_ISWHITE(*p))
651 {
652 p++;
653 text_indent_len++;
654 }
655 text_indent = vim_strnsave(theline, text_indent_len);
656 }
657 // with "trim": skip the indent matching the first line
658 if (text_indent != NULL)
659 for (ti = 0; ti < text_indent_len; ++ti)
660 if (theline[ti] != text_indent[ti])
661 break;
662
663 if (list_append_string(l, theline + ti, -1) == FAIL)
664 break;
665 vim_free(theline);
666 }
667 vim_free(text_indent);
668
669 return l;
670}
671
672/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200673 * Vim9 variable declaration:
674 * ":var name"
675 * ":var name: type"
676 * ":var name = expr"
677 * ":var name: type = expr"
678 * etc.
679 */
680 void
681ex_var(exarg_T *eap)
682{
683 if (!in_vim9script())
684 {
685 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
686 return;
687 }
688 ex_let(eap);
689}
690
691/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200692 * ":let" list all variable values
693 * ":let var1 var2" list variable values
694 * ":let var = expr" assignment command.
695 * ":let var += expr" assignment command.
696 * ":let var -= expr" assignment command.
697 * ":let var *= expr" assignment command.
698 * ":let var /= expr" assignment command.
699 * ":let var %= expr" assignment command.
700 * ":let var .= expr" assignment command.
701 * ":let var ..= expr" assignment command.
702 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100704 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200705 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200706 * ":final var = expr" assignment command.
707 * ":final [var1, var2] = expr" unpack list.
708 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200709 * ":const" list all variable values
710 * ":const var1 var2" list variable values
711 * ":const var = expr" assignment command.
712 * ":const [var1, var2] = expr" unpack list.
713 */
714 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200715ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200716{
717 char_u *arg = eap->arg;
718 char_u *expr = NULL;
719 typval_T rettv;
720 int i;
721 int var_count = 0;
722 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200723 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200724 char_u *argend;
725 int first = TRUE;
726 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200727 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200728 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200729 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200730
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200731 if (eap->cmdidx == CMD_final && !vim9script)
732 {
733 // In legacy Vim script ":final" is short for ":finally".
734 ex_finally(eap);
735 return;
736 }
737 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
738 // In legacy Vim script ":const" works like ":final".
739 eap->cmdidx = CMD_final;
740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 // detect Vim9 assignment without ":let" or ":const"
742 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200743 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200745 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200746 if (argend == NULL)
747 return;
748 if (argend > arg && argend[-1] == '.') // for var.='str'
749 --argend;
750 expr = skipwhite(argend);
751 concat = expr[0] == '.'
752 && ((expr[1] == '=' && current_sctx.sc_version < 2)
753 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200754 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
755 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200756 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200757 {
758 // ":let" without "=": list variables
759 if (*arg == '[')
760 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200761 else if (expr[0] == '.' && expr[1] == '=')
762 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200763 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200764 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200765 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200766 {
767 // Vim9 declaration ":let var: type"
768 arg = vim9_declare_scriptvar(eap, arg);
769 }
770 else
771 {
772 // ":let var1 var2" - list values
773 arg = list_arg_vars(eap, arg, &first);
774 }
775 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200776 else if (!eap->skip)
777 {
778 // ":let"
779 list_glob_vars(&first);
780 list_buf_vars(&first);
781 list_win_vars(&first);
782 list_tab_vars(&first);
783 list_script_vars(&first);
784 list_func_vars(&first);
785 list_vim_vars(&first);
786 }
787 eap->nextcmd = check_nextcmd(arg);
788 }
789 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
790 {
791 list_T *l;
792
793 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200794 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200795 if (l != NULL)
796 {
797 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200798 if (!eap->skip)
799 {
800 op[0] = '=';
801 op[1] = NUL;
802 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200804 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 clear_tv(&rettv);
806 }
807 }
808 else
809 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200810 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200811 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200813 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200814 i = FAIL;
815 if (has_assign || concat)
816 {
817 op[0] = '=';
818 op[1] = NUL;
819 if (*expr != '=')
820 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200821 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200822 {
823 // +=, /=, etc. require an existing variable
824 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
825 i = FAIL;
826 }
827 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200828 {
829 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200830 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200831 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200832 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200833 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200834 ++len;
835 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200836 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200837 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200838 }
839 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200840 ++expr;
841
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200842 if (vim9script && (!VIM_ISWHITE(*argend)
843 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200844 {
845 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200846 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200847 i = FAIL;
848 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200849
850 if (eap->skip)
851 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200852 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200853 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200854 if (getline_equal(eap->getline, eap->cookie, getsourceline))
855 {
856 evalarg.eval_getline = eap->getline;
857 evalarg.eval_cookie = eap->cookie;
858 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200859 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200860 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200861 if (eap->skip)
862 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200863 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200864 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200865 if (eap->skip)
866 {
867 if (i != FAIL)
868 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200869 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200870 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200871 {
872 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200873 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200874 clear_tv(&rettv);
875 }
876 }
877}
878
879/*
880 * Assign the typevalue "tv" to the variable or variables at "arg_start".
881 * Handles both "var" with any type and "[var, var; var]" with a list type.
882 * When "op" is not NULL it points to a string with characters that
883 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
884 * or concatenate.
885 * Returns OK or FAIL;
886 */
887 int
888ex_let_vars(
889 char_u *arg_start,
890 typval_T *tv,
891 int copy, // copy values from "tv", don't move
892 int semicolon, // from skip_var_list()
893 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200894 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200895 char_u *op)
896{
897 char_u *arg = arg_start;
898 list_T *l;
899 int i;
900 listitem_T *item;
901 typval_T ltv;
902
903 if (*arg != '[')
904 {
905 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200907 return FAIL;
908 return OK;
909 }
910
911 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
912 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
913 {
914 emsg(_(e_listreq));
915 return FAIL;
916 }
917
918 i = list_len(l);
919 if (semicolon == 0 && var_count < i)
920 {
921 emsg(_("E687: Less targets than List items"));
922 return FAIL;
923 }
924 if (var_count - semicolon > i)
925 {
926 emsg(_("E688: More targets than List items"));
927 return FAIL;
928 }
929
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200930 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200931 item = l->lv_first;
932 while (*arg != ']')
933 {
934 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200936 item = item->li_next;
937 if (arg == NULL)
938 return FAIL;
939
940 arg = skipwhite(arg);
941 if (*arg == ';')
942 {
943 // Put the rest of the list (may be empty) in the var after ';'.
944 // Create a new list for this.
945 l = list_alloc();
946 if (l == NULL)
947 return FAIL;
948 while (item != NULL)
949 {
950 list_append_tv(l, &item->li_tv);
951 item = item->li_next;
952 }
953
954 ltv.v_type = VAR_LIST;
955 ltv.v_lock = 0;
956 ltv.vval.v_list = l;
957 l->lv_refcount = 1;
958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960 (char_u *)"]", op);
961 clear_tv(&ltv);
962 if (arg == NULL)
963 return FAIL;
964 break;
965 }
966 else if (*arg != ',' && *arg != ']')
967 {
968 internal_error("ex_let_vars()");
969 return FAIL;
970 }
971 }
972
973 return OK;
974}
975
976/*
977 * Skip over assignable variable "var" or list of variables "[var, var]".
978 * Used for ":let varvar = expr" and ":for varvar in expr".
979 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200980 * for "[var, var; var]" set "semicolon" to 1.
981 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200982 * Return NULL for an error.
983 */
984 char_u *
985skip_var_list(
986 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200989 int *semicolon,
990 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200991{
992 char_u *p, *s;
993
994 if (*arg == '[')
995 {
996 // "[var, var]": find the matching ']'.
997 p = arg;
998 for (;;)
999 {
1000 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001001 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001002 if (s == p)
1003 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001004 if (!silent)
1005 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 return NULL;
1007 }
1008 ++*var_count;
1009
1010 p = skipwhite(s);
1011 if (*p == ']')
1012 break;
1013 else if (*p == ';')
1014 {
1015 if (*semicolon == 1)
1016 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001017 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001018 return NULL;
1019 }
1020 *semicolon = 1;
1021 }
1022 else if (*p != ',')
1023 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001024 if (!silent)
1025 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001026 return NULL;
1027 }
1028 }
1029 return p + 1;
1030 }
1031 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001033}
1034
1035/*
1036 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1037 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001039 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001040 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 char_u *end;
1044
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001045 if (*arg == '@' && arg[1] != NUL)
1046 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001049 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001051 // "a: type" is declaring variable "a" with a type, not "a:".
1052 if (end == arg + 2 && end[-1] == ':')
1053 --end;
1054 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001055 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 }
1057 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058}
1059
1060/*
1061 * List variables for hashtab "ht" with prefix "prefix".
1062 * If "empty" is TRUE also list NULL strings as empty strings.
1063 */
1064 void
1065list_hashtable_vars(
1066 hashtab_T *ht,
1067 char *prefix,
1068 int empty,
1069 int *first)
1070{
1071 hashitem_T *hi;
1072 dictitem_T *di;
1073 int todo;
1074 char_u buf[IOSIZE];
1075
1076 todo = (int)ht->ht_used;
1077 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1078 {
1079 if (!HASHITEM_EMPTY(hi))
1080 {
1081 --todo;
1082 di = HI2DI(hi);
1083
1084 // apply :filter /pat/ to variable name
1085 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1086 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1087 if (message_filtered(buf))
1088 continue;
1089
1090 if (empty || di->di_tv.v_type != VAR_STRING
1091 || di->di_tv.vval.v_string != NULL)
1092 list_one_var(di, prefix, first);
1093 }
1094 }
1095}
1096
1097/*
1098 * List global variables.
1099 */
1100 static void
1101list_glob_vars(int *first)
1102{
1103 list_hashtable_vars(&globvarht, "", TRUE, first);
1104}
1105
1106/*
1107 * List buffer variables.
1108 */
1109 static void
1110list_buf_vars(int *first)
1111{
1112 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1113}
1114
1115/*
1116 * List window variables.
1117 */
1118 static void
1119list_win_vars(int *first)
1120{
1121 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1122}
1123
1124/*
1125 * List tab page variables.
1126 */
1127 static void
1128list_tab_vars(int *first)
1129{
1130 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1131}
1132
1133/*
1134 * List variables in "arg".
1135 */
1136 static char_u *
1137list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1138{
1139 int error = FALSE;
1140 int len;
1141 char_u *name;
1142 char_u *name_start;
1143 char_u *arg_subsc;
1144 char_u *tofree;
1145 typval_T tv;
1146
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001147 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001148 {
1149 if (error || eap->skip)
1150 {
1151 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1152 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1153 {
1154 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001155 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001156 break;
1157 }
1158 }
1159 else
1160 {
1161 // get_name_len() takes care of expanding curly braces
1162 name_start = name = arg;
1163 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1164 if (len <= 0)
1165 {
1166 // This is mainly to keep test 49 working: when expanding
1167 // curly braces fails overrule the exception error message.
1168 if (len < 0 && !aborting())
1169 {
1170 emsg_severe = TRUE;
1171 semsg(_(e_invarg2), arg);
1172 break;
1173 }
1174 error = TRUE;
1175 }
1176 else
1177 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001178 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001179 if (tofree != NULL)
1180 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001181 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001182 error = TRUE;
1183 else
1184 {
1185 // handle d.key, l[idx], f(expr)
1186 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001187 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001188 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001189 error = TRUE;
1190 else
1191 {
1192 if (arg == arg_subsc && len == 2 && name[1] == ':')
1193 {
1194 switch (*name)
1195 {
1196 case 'g': list_glob_vars(first); break;
1197 case 'b': list_buf_vars(first); break;
1198 case 'w': list_win_vars(first); break;
1199 case 't': list_tab_vars(first); break;
1200 case 'v': list_vim_vars(first); break;
1201 case 's': list_script_vars(first); break;
1202 case 'l': list_func_vars(first); break;
1203 default:
1204 semsg(_("E738: Can't list variables for %s"), name);
1205 }
1206 }
1207 else
1208 {
1209 char_u numbuf[NUMBUFLEN];
1210 char_u *tf;
1211 int c;
1212 char_u *s;
1213
1214 s = echo_string(&tv, &tf, numbuf, 0);
1215 c = *arg;
1216 *arg = NUL;
1217 list_one_var_a("",
1218 arg == arg_subsc ? name : name_start,
1219 tv.v_type,
1220 s == NULL ? (char_u *)"" : s,
1221 first);
1222 *arg = c;
1223 vim_free(tf);
1224 }
1225 clear_tv(&tv);
1226 }
1227 }
1228 }
1229
1230 vim_free(tofree);
1231 }
1232
1233 arg = skipwhite(arg);
1234 }
1235
1236 return arg;
1237}
1238
1239/*
1240 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1241 * Returns a pointer to the char just after the var name.
1242 * Returns NULL if there is an error.
1243 */
1244 static char_u *
1245ex_let_one(
1246 char_u *arg, // points to variable name
1247 typval_T *tv, // value to assign to variable
1248 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001249 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001250 char_u *endchars, // valid chars after variable name or NULL
1251 char_u *op) // "+", "-", "." or NULL
1252{
1253 int c1;
1254 char_u *name;
1255 char_u *p;
1256 char_u *arg_end = NULL;
1257 int len;
1258 int opt_flags;
1259 char_u *tofree = NULL;
1260
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001261 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001262 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1263 {
1264 vim9_declare_error(arg);
1265 return NULL;
1266 }
1267
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001268 // ":let $VAR = expr": Set environment variable.
1269 if (*arg == '$')
1270 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001271 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001272 {
1273 emsg(_("E996: Cannot lock an environment variable"));
1274 return NULL;
1275 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001276
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001277 // Find the end of the name.
1278 ++arg;
1279 name = arg;
1280 len = get_env_len(&arg);
1281 if (len == 0)
1282 semsg(_(e_invarg2), name - 1);
1283 else
1284 {
1285 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1286 semsg(_(e_letwrong), op);
1287 else if (endchars != NULL
1288 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1289 emsg(_(e_letunexp));
1290 else if (!check_secure())
1291 {
1292 c1 = name[len];
1293 name[len] = NUL;
1294 p = tv_get_string_chk(tv);
1295 if (p != NULL && op != NULL && *op == '.')
1296 {
1297 int mustfree = FALSE;
1298 char_u *s = vim_getenv(name, &mustfree);
1299
1300 if (s != NULL)
1301 {
1302 p = tofree = concat_str(s, p);
1303 if (mustfree)
1304 vim_free(s);
1305 }
1306 }
1307 if (p != NULL)
1308 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001309 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001310 arg_end = arg;
1311 }
1312 name[len] = c1;
1313 vim_free(tofree);
1314 }
1315 }
1316 }
1317
1318 // ":let &option = expr": Set option value.
1319 // ":let &l:option = expr": Set local option value.
1320 // ":let &g:option = expr": Set global option value.
1321 else if (*arg == '&')
1322 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001323 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001324 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 return NULL;
1327 }
1328 // Find the end of the name.
1329 p = find_option_end(&arg, &opt_flags);
1330 if (p == NULL || (endchars != NULL
1331 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1332 emsg(_(e_letunexp));
1333 else
1334 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001335 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001336 int opt_type;
1337 long numval;
1338 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001339 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001340 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001341
1342 c1 = *p;
1343 *p = NUL;
1344
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001345 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1346 if ((opt_type == 1 || opt_type == -1)
1347 && (tv->v_type != VAR_STRING || !in_vim9script()))
1348 // number, possibly hidden
1349 n = (long)tv_get_number(tv);
1350
1351 // Avoid setting a string option to the text "v:false" or similar.
1352 // In Vim9 script also don't convert a number to string.
1353 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1354 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1355 s = tv_get_string_chk(tv);
1356
1357 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001358 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359 if ((opt_type == 1 && *op == '.')
1360 || (opt_type == 0 && *op != '.'))
1361 {
1362 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001363 failed = TRUE; // don't set the value
1364
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365 }
1366 else
1367 {
1368 if (opt_type == 1) // number
1369 {
1370 switch (*op)
1371 {
1372 case '+': n = numval + n; break;
1373 case '-': n = numval - n; break;
1374 case '*': n = numval * n; break;
1375 case '/': n = (long)num_divide(numval, n); break;
1376 case '%': n = (long)num_modulus(numval, n); break;
1377 }
1378 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001379 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001380 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001381 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001382 s = concat_str(stringval, s);
1383 vim_free(stringval);
1384 stringval = s;
1385 }
1386 }
1387 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001388
1389 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001390 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001391 if (opt_type != 0 || s != NULL)
1392 {
1393 set_option_value(arg, n, s, opt_flags);
1394 arg_end = p;
1395 }
1396 else
1397 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001398 }
1399 *p = c1;
1400 vim_free(stringval);
1401 }
1402 }
1403
1404 // ":let @r = expr": Set register contents.
1405 else if (*arg == '@')
1406 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001407 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408 {
1409 emsg(_("E996: Cannot lock a register"));
1410 return NULL;
1411 }
1412 ++arg;
1413 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1414 semsg(_(e_letwrong), op);
1415 else if (endchars != NULL
1416 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1417 emsg(_(e_letunexp));
1418 else
1419 {
1420 char_u *ptofree = NULL;
1421 char_u *s;
1422
1423 p = tv_get_string_chk(tv);
1424 if (p != NULL && op != NULL && *op == '.')
1425 {
1426 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1427 if (s != NULL)
1428 {
1429 p = ptofree = concat_str(s, p);
1430 vim_free(s);
1431 }
1432 }
1433 if (p != NULL)
1434 {
1435 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1436 arg_end = arg + 1;
1437 }
1438 vim_free(ptofree);
1439 }
1440 }
1441
1442 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001444 // ":let {expr} = expr": Idem, name made with curly braces
1445 else if (eval_isnamec1(*arg) || *arg == '{')
1446 {
1447 lval_T lv;
1448
1449 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001450 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 if (endchars != NULL && vim_strchr(endchars,
1453 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001454 emsg(_(e_letunexp));
1455 else
1456 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001458 arg_end = p;
1459 }
1460 }
1461 clear_lval(&lv);
1462 }
1463
1464 else
1465 semsg(_(e_invarg2), arg);
1466
1467 return arg_end;
1468}
1469
1470/*
1471 * ":unlet[!] var1 ... " command.
1472 */
1473 void
1474ex_unlet(exarg_T *eap)
1475{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001476 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001477}
1478
1479/*
1480 * ":lockvar" and ":unlockvar" commands
1481 */
1482 void
1483ex_lockvar(exarg_T *eap)
1484{
1485 char_u *arg = eap->arg;
1486 int deep = 2;
1487
1488 if (eap->forceit)
1489 deep = -1;
1490 else if (vim_isdigit(*arg))
1491 {
1492 deep = getdigits(&arg);
1493 arg = skipwhite(arg);
1494 }
1495
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001496 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001497}
1498
1499/*
1500 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001501 * Also used for Vim9 script. "callback" is invoked as:
1502 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001503 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001504 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001505ex_unletlock(
1506 exarg_T *eap,
1507 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001508 int deep,
1509 int glv_flags,
1510 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1511 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001512{
1513 char_u *arg = argstart;
1514 char_u *name_end;
1515 int error = FALSE;
1516 lval_T lv;
1517
1518 do
1519 {
1520 if (*arg == '$')
1521 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001522 lv.ll_name = arg;
1523 lv.ll_tv = NULL;
1524 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001525 if (get_env_len(&arg) == 0)
1526 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001527 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001528 return;
1529 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001530 if (!error && !eap->skip
1531 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1532 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001533 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001535 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001536 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001537 // Parse the name and find the end.
1538 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1539 glv_flags, FNE_CHECK_START);
1540 if (lv.ll_name == NULL)
1541 error = TRUE; // error but continue parsing
1542 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1543 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001545 if (name_end != NULL)
1546 {
1547 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001548 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001549 }
1550 if (!(eap->skip || error))
1551 clear_lval(&lv);
1552 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001555 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001556 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001557 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001558
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001559 if (!eap->skip)
1560 clear_lval(&lv);
1561 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001562
1563 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001564 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001565
1566 eap->nextcmd = check_nextcmd(arg);
1567}
1568
1569 static int
1570do_unlet_var(
1571 lval_T *lp,
1572 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001573 exarg_T *eap,
1574 int deep UNUSED,
1575 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001577 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578 int ret = OK;
1579 int cc;
1580
1581 if (lp->ll_tv == NULL)
1582 {
1583 cc = *name_end;
1584 *name_end = NUL;
1585
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001586 // Environment variable, normal name or expanded name.
1587 if (*lp->ll_name == '$')
1588 vim_unsetenv(lp->ll_name + 1);
1589 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001590 ret = FAIL;
1591 *name_end = cc;
1592 }
1593 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001594 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001595 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001596 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597 return FAIL;
1598 else if (lp->ll_range)
1599 {
1600 listitem_T *li;
1601 listitem_T *ll_li = lp->ll_li;
1602 int ll_n1 = lp->ll_n1;
1603
1604 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1605 {
1606 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001607 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001608 return FAIL;
1609 ll_li = li;
1610 ++ll_n1;
1611 }
1612
1613 // Delete a range of List items.
1614 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1615 {
1616 li = lp->ll_li->li_next;
1617 listitem_remove(lp->ll_list, lp->ll_li);
1618 lp->ll_li = li;
1619 ++lp->ll_n1;
1620 }
1621 }
1622 else
1623 {
1624 if (lp->ll_list != NULL)
1625 // unlet a List item.
1626 listitem_remove(lp->ll_list, lp->ll_li);
1627 else
1628 // unlet a Dictionary item.
1629 dictitem_remove(lp->ll_dict, lp->ll_di);
1630 }
1631
1632 return ret;
1633}
1634
1635/*
1636 * "unlet" a variable. Return OK if it existed, FAIL if not.
1637 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1638 */
1639 int
1640do_unlet(char_u *name, int forceit)
1641{
1642 hashtab_T *ht;
1643 hashitem_T *hi;
1644 char_u *varname;
1645 dict_T *d;
1646 dictitem_T *di;
1647
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001648 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001649 return FAIL;
1650
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001651 ht = find_var_ht(name, &varname);
1652 if (ht != NULL && *varname != NUL)
1653 {
1654 d = get_current_funccal_dict(ht);
1655 if (d == NULL)
1656 {
1657 if (ht == &globvarht)
1658 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001659 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001660 d = &vimvardict;
1661 else
1662 {
1663 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1664 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1665 }
1666 if (d == NULL)
1667 {
1668 internal_error("do_unlet()");
1669 return FAIL;
1670 }
1671 }
1672 hi = hash_find(ht, varname);
1673 if (HASHITEM_EMPTY(hi))
1674 hi = find_hi_in_scoped_ht(name, &ht);
1675 if (hi != NULL && !HASHITEM_EMPTY(hi))
1676 {
1677 di = HI2DI(hi);
1678 if (var_check_fixed(di->di_flags, name, FALSE)
1679 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001680 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001681 return FAIL;
1682
1683 delete_var(ht, hi);
1684 return OK;
1685 }
1686 }
1687 if (forceit)
1688 return OK;
1689 semsg(_("E108: No such variable: \"%s\""), name);
1690 return FAIL;
1691}
1692
1693/*
1694 * Lock or unlock variable indicated by "lp".
1695 * "deep" is the levels to go (-1 for unlimited);
1696 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1697 */
1698 static int
1699do_lock_var(
1700 lval_T *lp,
1701 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001702 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001703 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001704 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001705{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001706 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001707 int ret = OK;
1708 int cc;
1709 dictitem_T *di;
1710
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001711 if (lp->ll_tv == NULL)
1712 {
1713 cc = *name_end;
1714 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001715 if (*lp->ll_name == '$')
1716 {
1717 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001718 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001719 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001720 else
1721 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001722 // Normal name or expanded name.
1723 di = find_var(lp->ll_name, NULL, TRUE);
1724 if (di == NULL)
1725 ret = FAIL;
1726 else if ((di->di_flags & DI_FLAGS_FIX)
1727 && di->di_tv.v_type != VAR_DICT
1728 && di->di_tv.v_type != VAR_LIST)
1729 {
1730 // For historic reasons this error is not given for a list or
1731 // dict. E.g., the b: dict could be locked/unlocked.
1732 semsg(_(e_lock_unlock), lp->ll_name);
1733 ret = FAIL;
1734 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001736 {
1737 if (lock)
1738 di->di_flags |= DI_FLAGS_LOCK;
1739 else
1740 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001741 if (deep != 0)
1742 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001743 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001744 }
1745 *name_end = cc;
1746 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001747 else if (deep == 0)
1748 {
1749 // nothing to do
1750 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751 else if (lp->ll_range)
1752 {
1753 listitem_T *li = lp->ll_li;
1754
1755 // (un)lock a range of List items.
1756 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1757 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001758 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001759 li = li->li_next;
1760 ++lp->ll_n1;
1761 }
1762 }
1763 else if (lp->ll_list != NULL)
1764 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001765 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001766 else
1767 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001768 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769
1770 return ret;
1771}
1772
1773/*
1774 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001775 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1776 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001777 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001778 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001779item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001780{
1781 static int recurse = 0;
1782 list_T *l;
1783 listitem_T *li;
1784 dict_T *d;
1785 blob_T *b;
1786 hashitem_T *hi;
1787 int todo;
1788
1789 if (recurse >= DICT_MAXNEST)
1790 {
1791 emsg(_("E743: variable nested too deep for (un)lock"));
1792 return;
1793 }
1794 if (deep == 0)
1795 return;
1796 ++recurse;
1797
1798 // lock/unlock the item itself
1799 if (lock)
1800 tv->v_lock |= VAR_LOCKED;
1801 else
1802 tv->v_lock &= ~VAR_LOCKED;
1803
1804 switch (tv->v_type)
1805 {
1806 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001807 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001811 case VAR_STRING:
1812 case VAR_FUNC:
1813 case VAR_PARTIAL:
1814 case VAR_FLOAT:
1815 case VAR_SPECIAL:
1816 case VAR_JOB:
1817 case VAR_CHANNEL:
1818 break;
1819
1820 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001821 if ((b = tv->vval.v_blob) != NULL
1822 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 {
1824 if (lock)
1825 b->bv_lock |= VAR_LOCKED;
1826 else
1827 b->bv_lock &= ~VAR_LOCKED;
1828 }
1829 break;
1830 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001831 if ((l = tv->vval.v_list) != NULL
1832 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001833 {
1834 if (lock)
1835 l->lv_lock |= VAR_LOCKED;
1836 else
1837 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001838 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001840 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001841 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001842 }
1843 break;
1844 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001845 if ((d = tv->vval.v_dict) != NULL
1846 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001847 {
1848 if (lock)
1849 d->dv_lock |= VAR_LOCKED;
1850 else
1851 d->dv_lock &= ~VAR_LOCKED;
1852 if (deep < 0 || deep > 1)
1853 {
1854 // recursive: lock/unlock the items the List contains
1855 todo = (int)d->dv_hashtab.ht_used;
1856 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1857 {
1858 if (!HASHITEM_EMPTY(hi))
1859 {
1860 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001861 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1862 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 }
1864 }
1865 }
1866 }
1867 }
1868 --recurse;
1869}
1870
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001871#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1872/*
1873 * Delete all "menutrans_" variables.
1874 */
1875 void
1876del_menutrans_vars(void)
1877{
1878 hashitem_T *hi;
1879 int todo;
1880
1881 hash_lock(&globvarht);
1882 todo = (int)globvarht.ht_used;
1883 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1884 {
1885 if (!HASHITEM_EMPTY(hi))
1886 {
1887 --todo;
1888 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1889 delete_var(&globvarht, hi);
1890 }
1891 }
1892 hash_unlock(&globvarht);
1893}
1894#endif
1895
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001896/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001897 * Local string buffer for the next two functions to store a variable name
1898 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1899 * get_user_var_name().
1900 */
1901
1902static char_u *varnamebuf = NULL;
1903static int varnamebuflen = 0;
1904
1905/*
1906 * Function to concatenate a prefix and a variable name.
1907 */
1908 static char_u *
1909cat_prefix_varname(int prefix, char_u *name)
1910{
1911 int len;
1912
1913 len = (int)STRLEN(name) + 3;
1914 if (len > varnamebuflen)
1915 {
1916 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001917 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001918 varnamebuf = alloc(len);
1919 if (varnamebuf == NULL)
1920 {
1921 varnamebuflen = 0;
1922 return NULL;
1923 }
1924 varnamebuflen = len;
1925 }
1926 *varnamebuf = prefix;
1927 varnamebuf[1] = ':';
1928 STRCPY(varnamebuf + 2, name);
1929 return varnamebuf;
1930}
1931
1932/*
1933 * Function given to ExpandGeneric() to obtain the list of user defined
1934 * (global/buffer/window/built-in) variable names.
1935 */
1936 char_u *
1937get_user_var_name(expand_T *xp, int idx)
1938{
1939 static long_u gdone;
1940 static long_u bdone;
1941 static long_u wdone;
1942 static long_u tdone;
1943 static int vidx;
1944 static hashitem_T *hi;
1945 hashtab_T *ht;
1946
1947 if (idx == 0)
1948 {
1949 gdone = bdone = wdone = vidx = 0;
1950 tdone = 0;
1951 }
1952
1953 // Global variables
1954 if (gdone < globvarht.ht_used)
1955 {
1956 if (gdone++ == 0)
1957 hi = globvarht.ht_array;
1958 else
1959 ++hi;
1960 while (HASHITEM_EMPTY(hi))
1961 ++hi;
1962 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1963 return cat_prefix_varname('g', hi->hi_key);
1964 return hi->hi_key;
1965 }
1966
1967 // b: variables
1968 ht = &curbuf->b_vars->dv_hashtab;
1969 if (bdone < ht->ht_used)
1970 {
1971 if (bdone++ == 0)
1972 hi = ht->ht_array;
1973 else
1974 ++hi;
1975 while (HASHITEM_EMPTY(hi))
1976 ++hi;
1977 return cat_prefix_varname('b', hi->hi_key);
1978 }
1979
1980 // w: variables
1981 ht = &curwin->w_vars->dv_hashtab;
1982 if (wdone < ht->ht_used)
1983 {
1984 if (wdone++ == 0)
1985 hi = ht->ht_array;
1986 else
1987 ++hi;
1988 while (HASHITEM_EMPTY(hi))
1989 ++hi;
1990 return cat_prefix_varname('w', hi->hi_key);
1991 }
1992
1993 // t: variables
1994 ht = &curtab->tp_vars->dv_hashtab;
1995 if (tdone < ht->ht_used)
1996 {
1997 if (tdone++ == 0)
1998 hi = ht->ht_array;
1999 else
2000 ++hi;
2001 while (HASHITEM_EMPTY(hi))
2002 ++hi;
2003 return cat_prefix_varname('t', hi->hi_key);
2004 }
2005
2006 // v: variables
2007 if (vidx < VV_LEN)
2008 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2009
2010 VIM_CLEAR(varnamebuf);
2011 varnamebuflen = 0;
2012 return NULL;
2013}
2014
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002015 char *
2016get_var_special_name(int nr)
2017{
2018 switch (nr)
2019 {
2020 case VVAL_FALSE: return "v:false";
2021 case VVAL_TRUE: return "v:true";
2022 case VVAL_NONE: return "v:none";
2023 case VVAL_NULL: return "v:null";
2024 }
2025 internal_error("get_var_special_name()");
2026 return "42";
2027}
2028
2029/*
2030 * Returns the global variable dictionary
2031 */
2032 dict_T *
2033get_globvar_dict(void)
2034{
2035 return &globvardict;
2036}
2037
2038/*
2039 * Returns the global variable hash table
2040 */
2041 hashtab_T *
2042get_globvar_ht(void)
2043{
2044 return &globvarht;
2045}
2046
2047/*
2048 * Returns the v: variable dictionary
2049 */
2050 dict_T *
2051get_vimvar_dict(void)
2052{
2053 return &vimvardict;
2054}
2055
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002056/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002058 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 */
2060 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002061find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002063 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2064 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065
2066 if (di == NULL)
2067 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002068 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2070 return (int)(vv - vimvars);
2071}
2072
2073
2074/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002075 * Set type of v: variable to "type".
2076 */
2077 void
2078set_vim_var_type(int idx, vartype_T type)
2079{
2080 vimvars[idx].vv_type = type;
2081}
2082
2083/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002084 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002085 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002086 */
2087 void
2088set_vim_var_nr(int idx, varnumber_T val)
2089{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002090 vimvars[idx].vv_nr = val;
2091}
2092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 char *
2094get_vim_var_name(int idx)
2095{
2096 return vimvars[idx].vv_name;
2097}
2098
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002099/*
2100 * Get typval_T v: variable value.
2101 */
2102 typval_T *
2103get_vim_var_tv(int idx)
2104{
2105 return &vimvars[idx].vv_tv;
2106}
2107
2108/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002109 * Set v: variable to "tv". Only accepts the same type.
2110 * Takes over the value of "tv".
2111 */
2112 int
2113set_vim_var_tv(int idx, typval_T *tv)
2114{
2115 if (vimvars[idx].vv_type != tv->v_type)
2116 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002117 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002118 clear_tv(tv);
2119 return FAIL;
2120 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002121 // VV_RO is also checked when compiling, but let's check here as well.
2122 if (vimvars[idx].vv_flags & VV_RO)
2123 {
2124 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2125 return FAIL;
2126 }
2127 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2128 {
2129 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2130 return FAIL;
2131 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002132 clear_tv(&vimvars[idx].vv_di.di_tv);
2133 vimvars[idx].vv_di.di_tv = *tv;
2134 return OK;
2135}
2136
2137/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002138 * Get number v: variable value.
2139 */
2140 varnumber_T
2141get_vim_var_nr(int idx)
2142{
2143 return vimvars[idx].vv_nr;
2144}
2145
2146/*
2147 * Get string v: variable value. Uses a static buffer, can only be used once.
2148 * If the String variable has never been set, return an empty string.
2149 * Never returns NULL;
2150 */
2151 char_u *
2152get_vim_var_str(int idx)
2153{
2154 return tv_get_string(&vimvars[idx].vv_tv);
2155}
2156
2157/*
2158 * Get List v: variable value. Caller must take care of reference count when
2159 * needed.
2160 */
2161 list_T *
2162get_vim_var_list(int idx)
2163{
2164 return vimvars[idx].vv_list;
2165}
2166
2167/*
2168 * Get Dict v: variable value. Caller must take care of reference count when
2169 * needed.
2170 */
2171 dict_T *
2172get_vim_var_dict(int idx)
2173{
2174 return vimvars[idx].vv_dict;
2175}
2176
2177/*
2178 * Set v:char to character "c".
2179 */
2180 void
2181set_vim_var_char(int c)
2182{
2183 char_u buf[MB_MAXBYTES + 1];
2184
2185 if (has_mbyte)
2186 buf[(*mb_char2bytes)(c, buf)] = NUL;
2187 else
2188 {
2189 buf[0] = c;
2190 buf[1] = NUL;
2191 }
2192 set_vim_var_string(VV_CHAR, buf, -1);
2193}
2194
2195/*
2196 * Set v:count to "count" and v:count1 to "count1".
2197 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2198 */
2199 void
2200set_vcount(
2201 long count,
2202 long count1,
2203 int set_prevcount)
2204{
2205 if (set_prevcount)
2206 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2207 vimvars[VV_COUNT].vv_nr = count;
2208 vimvars[VV_COUNT1].vv_nr = count1;
2209}
2210
2211/*
2212 * Save variables that might be changed as a side effect. Used when executing
2213 * a timer callback.
2214 */
2215 void
2216save_vimvars(vimvars_save_T *vvsave)
2217{
2218 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2219 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2220 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2221}
2222
2223/*
2224 * Restore variables saved by save_vimvars().
2225 */
2226 void
2227restore_vimvars(vimvars_save_T *vvsave)
2228{
2229 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2230 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2231 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2232}
2233
2234/*
2235 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2236 * value.
2237 */
2238 void
2239set_vim_var_string(
2240 int idx,
2241 char_u *val,
2242 int len) // length of "val" to use or -1 (whole string)
2243{
2244 clear_tv(&vimvars[idx].vv_di.di_tv);
2245 vimvars[idx].vv_type = VAR_STRING;
2246 if (val == NULL)
2247 vimvars[idx].vv_str = NULL;
2248 else if (len == -1)
2249 vimvars[idx].vv_str = vim_strsave(val);
2250 else
2251 vimvars[idx].vv_str = vim_strnsave(val, len);
2252}
2253
2254/*
2255 * Set List v: variable to "val".
2256 */
2257 void
2258set_vim_var_list(int idx, list_T *val)
2259{
2260 clear_tv(&vimvars[idx].vv_di.di_tv);
2261 vimvars[idx].vv_type = VAR_LIST;
2262 vimvars[idx].vv_list = val;
2263 if (val != NULL)
2264 ++val->lv_refcount;
2265}
2266
2267/*
2268 * Set Dictionary v: variable to "val".
2269 */
2270 void
2271set_vim_var_dict(int idx, dict_T *val)
2272{
2273 clear_tv(&vimvars[idx].vv_di.di_tv);
2274 vimvars[idx].vv_type = VAR_DICT;
2275 vimvars[idx].vv_dict = val;
2276 if (val != NULL)
2277 {
2278 ++val->dv_refcount;
2279 dict_set_items_ro(val);
2280 }
2281}
2282
2283/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002284 * Set the v:argv list.
2285 */
2286 void
2287set_argv_var(char **argv, int argc)
2288{
2289 list_T *l = list_alloc();
2290 int i;
2291
2292 if (l == NULL)
2293 getout(1);
2294 l->lv_lock = VAR_FIXED;
2295 for (i = 0; i < argc; ++i)
2296 {
2297 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2298 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002299 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002300 }
2301 set_vim_var_list(VV_ARGV, l);
2302}
2303
2304/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002305 * Reset v:register, taking the 'clipboard' setting into account.
2306 */
2307 void
2308reset_reg_var(void)
2309{
2310 int regname = 0;
2311
2312 // Adjust the register according to 'clipboard', so that when
2313 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2314#ifdef FEAT_CLIPBOARD
2315 adjust_clip_reg(&regname);
2316#endif
2317 set_reg_var(regname);
2318}
2319
2320/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002321 * Set v:register if needed.
2322 */
2323 void
2324set_reg_var(int c)
2325{
2326 char_u regname;
2327
2328 if (c == 0 || c == ' ')
2329 regname = '"';
2330 else
2331 regname = c;
2332 // Avoid free/alloc when the value is already right.
2333 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2334 set_vim_var_string(VV_REG, &regname, 1);
2335}
2336
2337/*
2338 * Get or set v:exception. If "oldval" == NULL, return the current value.
2339 * Otherwise, restore the value to "oldval" and return NULL.
2340 * Must always be called in pairs to save and restore v:exception! Does not
2341 * take care of memory allocations.
2342 */
2343 char_u *
2344v_exception(char_u *oldval)
2345{
2346 if (oldval == NULL)
2347 return vimvars[VV_EXCEPTION].vv_str;
2348
2349 vimvars[VV_EXCEPTION].vv_str = oldval;
2350 return NULL;
2351}
2352
2353/*
2354 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2355 * Otherwise, restore the value to "oldval" and return NULL.
2356 * Must always be called in pairs to save and restore v:throwpoint! Does not
2357 * take care of memory allocations.
2358 */
2359 char_u *
2360v_throwpoint(char_u *oldval)
2361{
2362 if (oldval == NULL)
2363 return vimvars[VV_THROWPOINT].vv_str;
2364
2365 vimvars[VV_THROWPOINT].vv_str = oldval;
2366 return NULL;
2367}
2368
2369/*
2370 * Set v:cmdarg.
2371 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2372 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2373 * Must always be called in pairs!
2374 */
2375 char_u *
2376set_cmdarg(exarg_T *eap, char_u *oldarg)
2377{
2378 char_u *oldval;
2379 char_u *newval;
2380 unsigned len;
2381
2382 oldval = vimvars[VV_CMDARG].vv_str;
2383 if (eap == NULL)
2384 {
2385 vim_free(oldval);
2386 vimvars[VV_CMDARG].vv_str = oldarg;
2387 return NULL;
2388 }
2389
2390 if (eap->force_bin == FORCE_BIN)
2391 len = 6;
2392 else if (eap->force_bin == FORCE_NOBIN)
2393 len = 8;
2394 else
2395 len = 0;
2396
2397 if (eap->read_edit)
2398 len += 7;
2399
2400 if (eap->force_ff != 0)
2401 len += 10; // " ++ff=unix"
2402 if (eap->force_enc != 0)
2403 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2404 if (eap->bad_char != 0)
2405 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2406
2407 newval = alloc(len + 1);
2408 if (newval == NULL)
2409 return NULL;
2410
2411 if (eap->force_bin == FORCE_BIN)
2412 sprintf((char *)newval, " ++bin");
2413 else if (eap->force_bin == FORCE_NOBIN)
2414 sprintf((char *)newval, " ++nobin");
2415 else
2416 *newval = NUL;
2417
2418 if (eap->read_edit)
2419 STRCAT(newval, " ++edit");
2420
2421 if (eap->force_ff != 0)
2422 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2423 eap->force_ff == 'u' ? "unix"
2424 : eap->force_ff == 'd' ? "dos"
2425 : "mac");
2426 if (eap->force_enc != 0)
2427 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2428 eap->cmd + eap->force_enc);
2429 if (eap->bad_char == BAD_KEEP)
2430 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2431 else if (eap->bad_char == BAD_DROP)
2432 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2433 else if (eap->bad_char != 0)
2434 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2435 vimvars[VV_CMDARG].vv_str = newval;
2436 return oldval;
2437}
2438
2439/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002440 * Get the value of internal variable "name".
2441 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2442 */
2443 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002444eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002445 char_u *name,
2446 int len, // length of "name"
2447 typval_T *rettv, // NULL when only checking existence
2448 dictitem_T **dip, // non-NULL when typval's dict item is needed
2449 int verbose, // may give error message
2450 int no_autoload) // do not use script autoloading
2451{
2452 int ret = OK;
2453 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002454 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002455 dictitem_T *v;
2456 int cc;
2457
2458 // truncate the name, so that we can use strcmp()
2459 cc = name[len];
2460 name[len] = NUL;
2461
2462 // Check for user-defined variables.
2463 v = find_var(name, NULL, no_autoload);
2464 if (v != NULL)
2465 {
2466 tv = &v->di_tv;
2467 if (dip != NULL)
2468 *dip = v;
2469 }
2470
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002471 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002473 imported_T *import;
2474 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2475
2476 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477
2478 // imported variable from another script
2479 if (import != NULL)
2480 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002481 if (import->imp_funcname != NULL)
2482 {
2483 foundFunc = TRUE;
2484 if (rettv != NULL)
2485 {
2486 rettv->v_type = VAR_FUNC;
2487 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2488 }
2489 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002490 else if (import->imp_all)
2491 {
2492 emsg("Sorry, 'import * as X' not implemented yet");
2493 ret = FAIL;
2494 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002495 else
2496 {
2497 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002498 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002500 tv = sv->sv_tv;
2501 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002503 else if (in_vim9script())
2504 {
2505 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2506
2507 if (ufunc != NULL)
2508 {
2509 foundFunc = TRUE;
2510 if (rettv != NULL)
2511 {
2512 rettv->v_type = VAR_FUNC;
2513 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2514 }
2515 }
2516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 }
2518
Bram Moolenaarc620c052020-07-08 15:16:19 +02002519 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002520 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002521 if (tv == NULL)
2522 {
2523 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002524 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002525 ret = FAIL;
2526 }
2527 else if (rettv != NULL)
2528 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002529 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002530
2531 name[len] = cc;
2532
2533 return ret;
2534}
2535
2536/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002537 * Check if variable "name[len]" is a local variable or an argument.
2538 * If so, "*eval_lavars_used" is set to TRUE.
2539 */
2540 void
2541check_vars(char_u *name, int len)
2542{
2543 int cc;
2544 char_u *varname;
2545 hashtab_T *ht;
2546
2547 if (eval_lavars_used == NULL)
2548 return;
2549
2550 // truncate the name, so that we can use strcmp()
2551 cc = name[len];
2552 name[len] = NUL;
2553
2554 ht = find_var_ht(name, &varname);
2555 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2556 {
2557 if (find_var(name, NULL, TRUE) != NULL)
2558 *eval_lavars_used = TRUE;
2559 }
2560
2561 name[len] = cc;
2562}
2563
2564/*
2565 * Find variable "name" in the list of variables.
2566 * Return a pointer to it if found, NULL if not found.
2567 * Careful: "a:0" variables don't have a name.
2568 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2569 * hashtab_T used.
2570 */
2571 dictitem_T *
2572find_var(char_u *name, hashtab_T **htp, int no_autoload)
2573{
2574 char_u *varname;
2575 hashtab_T *ht;
2576 dictitem_T *ret = NULL;
2577
2578 ht = find_var_ht(name, &varname);
2579 if (htp != NULL)
2580 *htp = ht;
2581 if (ht == NULL)
2582 return NULL;
2583 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2584 if (ret != NULL)
2585 return ret;
2586
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002587 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002588 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2589}
2590
2591/*
2592 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002593 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002594 * Returns NULL if not found.
2595 */
2596 dictitem_T *
2597find_var_in_ht(
2598 hashtab_T *ht,
2599 int htname,
2600 char_u *varname,
2601 int no_autoload)
2602{
2603 hashitem_T *hi;
2604
2605 if (*varname == NUL)
2606 {
2607 // Must be something like "s:", otherwise "ht" would be NULL.
2608 switch (htname)
2609 {
2610 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2611 case 'g': return &globvars_var;
2612 case 'v': return &vimvars_var;
2613 case 'b': return &curbuf->b_bufvar;
2614 case 'w': return &curwin->w_winvar;
2615 case 't': return &curtab->tp_winvar;
2616 case 'l': return get_funccal_local_var();
2617 case 'a': return get_funccal_args_var();
2618 }
2619 return NULL;
2620 }
2621
2622 hi = hash_find(ht, varname);
2623 if (HASHITEM_EMPTY(hi))
2624 {
2625 // For global variables we may try auto-loading the script. If it
2626 // worked find the variable again. Don't auto-load a script if it was
2627 // loaded already, otherwise it would be loaded every time when
2628 // checking if a function name is a Funcref variable.
2629 if (ht == &globvarht && !no_autoload)
2630 {
2631 // Note: script_autoload() may make "hi" invalid. It must either
2632 // be obtained again or not used.
2633 if (!script_autoload(varname, FALSE) || aborting())
2634 return NULL;
2635 hi = hash_find(ht, varname);
2636 }
2637 if (HASHITEM_EMPTY(hi))
2638 return NULL;
2639 }
2640 return HI2DI(hi);
2641}
2642
2643/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 * Get the script-local hashtab. NULL if not in a script context.
2645 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002646 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647get_script_local_ht(void)
2648{
2649 scid_T sid = current_sctx.sc_sid;
2650
Bram Moolenaare3d46852020-08-29 13:39:17 +02002651 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 return &SCRIPT_VARS(sid);
2653 return NULL;
2654}
2655
2656/*
2657 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002658 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002660 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2662{
2663 hashtab_T *ht = get_script_local_ht();
2664 char_u buffer[30];
2665 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002666 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 hashitem_T *hi;
2668
2669 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002670 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 if (len < sizeof(buffer) - 1)
2672 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002673 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 vim_strncpy(buffer, name, len);
2675 p = buffer;
2676 }
2677 else
2678 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002679 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002681 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 }
2683
2684 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002685 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686
2687 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002688 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2689 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690
2691 if (p != buffer)
2692 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002693 // Don't return "buffer", gcc complains.
2694 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695}
2696
2697/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002698 * Find the hashtab used for a variable name.
2699 * Return NULL if the name is not valid.
2700 * Set "varname" to the start of name without ':'.
2701 */
2702 hashtab_T *
2703find_var_ht(char_u *name, char_u **varname)
2704{
2705 hashitem_T *hi;
2706 hashtab_T *ht;
2707
2708 if (name[0] == NUL)
2709 return NULL;
2710 if (name[1] != ':')
2711 {
2712 // The name must not start with a colon or #.
2713 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2714 return NULL;
2715 *varname = name;
2716
2717 // "version" is "v:version" in all scopes if scriptversion < 3.
2718 // Same for a few other variables marked with VV_COMPAT.
2719 if (current_sctx.sc_version < 3)
2720 {
2721 hi = hash_find(&compat_hashtab, name);
2722 if (!HASHITEM_EMPTY(hi))
2723 return &compat_hashtab;
2724 }
2725
2726 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 if (ht != NULL)
2728 return ht; // local variable
2729
2730 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002731 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 {
2733 ht = get_script_local_ht();
2734 if (ht != NULL)
2735 return ht;
2736 }
2737
2738 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002739 }
2740 *varname = name + 2;
2741 if (*name == 'g') // global variable
2742 return &globvarht;
2743 // There must be no ':' or '#' in the rest of the name, unless g: is used
2744 if (vim_strchr(name + 2, ':') != NULL
2745 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2746 return NULL;
2747 if (*name == 'b') // buffer variable
2748 return &curbuf->b_vars->dv_hashtab;
2749 if (*name == 'w') // window variable
2750 return &curwin->w_vars->dv_hashtab;
2751 if (*name == 't') // tab page variable
2752 return &curtab->tp_vars->dv_hashtab;
2753 if (*name == 'v') // v: variable
2754 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002755 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002756 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002758 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 if (*name == 'a') // a: function argument
2760 return get_funccal_args_ht();
2761 if (*name == 'l') // l: local function variable
2762 return get_funccal_local_ht();
2763 }
2764 if (*name == 's') // script variable
2765 {
2766 ht = get_script_local_ht();
2767 if (ht != NULL)
2768 return ht;
2769 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002770 return NULL;
2771}
2772
2773/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002774 * Get the string value of a (global/local) variable.
2775 * Note: see tv_get_string() for how long the pointer remains valid.
2776 * Returns NULL when it doesn't exist.
2777 */
2778 char_u *
2779get_var_value(char_u *name)
2780{
2781 dictitem_T *v;
2782
2783 v = find_var(name, NULL, FALSE);
2784 if (v == NULL)
2785 return NULL;
2786 return tv_get_string(&v->di_tv);
2787}
2788
2789/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002790 * Allocate a new hashtab for a sourced script. It will be used while
2791 * sourcing this script and when executing functions defined in the script.
2792 */
2793 void
2794new_script_vars(scid_T id)
2795{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002796 scriptvar_T *sv;
2797
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002798 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2799 if (sv == NULL)
2800 return;
2801 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002802 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002803}
2804
2805/*
2806 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2807 * point to it.
2808 */
2809 void
2810init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2811{
2812 hash_init(&dict->dv_hashtab);
2813 dict->dv_lock = 0;
2814 dict->dv_scope = scope;
2815 dict->dv_refcount = DO_NOT_FREE_CNT;
2816 dict->dv_copyID = 0;
2817 dict_var->di_tv.vval.v_dict = dict;
2818 dict_var->di_tv.v_type = VAR_DICT;
2819 dict_var->di_tv.v_lock = VAR_FIXED;
2820 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2821 dict_var->di_key[0] = NUL;
2822}
2823
2824/*
2825 * Unreference a dictionary initialized by init_var_dict().
2826 */
2827 void
2828unref_var_dict(dict_T *dict)
2829{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002830 // Now the dict needs to be freed if no one else is using it, go back to
2831 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002832 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2833 dict_unref(dict);
2834}
2835
2836/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002837 * Clean up a list of internal variables.
2838 * Frees all allocated variables and the value they contain.
2839 * Clears hashtab "ht", does not free it.
2840 */
2841 void
2842vars_clear(hashtab_T *ht)
2843{
2844 vars_clear_ext(ht, TRUE);
2845}
2846
2847/*
2848 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2849 */
2850 void
2851vars_clear_ext(hashtab_T *ht, int free_val)
2852{
2853 int todo;
2854 hashitem_T *hi;
2855 dictitem_T *v;
2856
2857 hash_lock(ht);
2858 todo = (int)ht->ht_used;
2859 for (hi = ht->ht_array; todo > 0; ++hi)
2860 {
2861 if (!HASHITEM_EMPTY(hi))
2862 {
2863 --todo;
2864
2865 // Free the variable. Don't remove it from the hashtab,
2866 // ht_array might change then. hash_clear() takes care of it
2867 // later.
2868 v = HI2DI(hi);
2869 if (free_val)
2870 clear_tv(&v->di_tv);
2871 if (v->di_flags & DI_FLAGS_ALLOC)
2872 vim_free(v);
2873 }
2874 }
2875 hash_clear(ht);
2876 ht->ht_used = 0;
2877}
2878
2879/*
2880 * Delete a variable from hashtab "ht" at item "hi".
2881 * Clear the variable value and free the dictitem.
2882 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002883 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002884delete_var(hashtab_T *ht, hashitem_T *hi)
2885{
2886 dictitem_T *di = HI2DI(hi);
2887
2888 hash_remove(ht, hi);
2889 clear_tv(&di->di_tv);
2890 vim_free(di);
2891}
2892
2893/*
2894 * List the value of one internal variable.
2895 */
2896 static void
2897list_one_var(dictitem_T *v, char *prefix, int *first)
2898{
2899 char_u *tofree;
2900 char_u *s;
2901 char_u numbuf[NUMBUFLEN];
2902
2903 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2904 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2905 s == NULL ? (char_u *)"" : s, first);
2906 vim_free(tofree);
2907}
2908
2909 static void
2910list_one_var_a(
2911 char *prefix,
2912 char_u *name,
2913 int type,
2914 char_u *string,
2915 int *first) // when TRUE clear rest of screen and set to FALSE
2916{
2917 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2918 msg_start();
2919 msg_puts(prefix);
2920 if (name != NULL) // "a:" vars don't have a name stored
2921 msg_puts((char *)name);
2922 msg_putchar(' ');
2923 msg_advance(22);
2924 if (type == VAR_NUMBER)
2925 msg_putchar('#');
2926 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2927 msg_putchar('*');
2928 else if (type == VAR_LIST)
2929 {
2930 msg_putchar('[');
2931 if (*string == '[')
2932 ++string;
2933 }
2934 else if (type == VAR_DICT)
2935 {
2936 msg_putchar('{');
2937 if (*string == '{')
2938 ++string;
2939 }
2940 else
2941 msg_putchar(' ');
2942
2943 msg_outtrans(string);
2944
2945 if (type == VAR_FUNC || type == VAR_PARTIAL)
2946 msg_puts("()");
2947 if (*first)
2948 {
2949 msg_clr_eos();
2950 *first = FALSE;
2951 }
2952}
2953
2954/*
2955 * Set variable "name" to value in "tv".
2956 * If the variable already exists, the value is updated.
2957 * Otherwise the variable is created.
2958 */
2959 void
2960set_var(
2961 char_u *name,
2962 typval_T *tv,
2963 int copy) // make copy of value in "tv"
2964{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002965 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002966}
2967
2968/*
2969 * Set variable "name" to value in "tv".
2970 * If the variable already exists and "is_const" is FALSE the value is updated.
2971 * Otherwise the variable is created.
2972 */
2973 void
2974set_var_const(
2975 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002977 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002978 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002979 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002980{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002981 typval_T *tv = tv_arg;
2982 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002984 char_u *varname;
2985 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002986 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002987 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988
2989 ht = find_var_ht(name, &varname);
2990 if (ht == NULL || *varname == NUL)
2991 {
2992 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002993 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002995 is_script_local = ht == get_script_local_ht();
2996
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002997 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002998 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002999 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003000 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003001 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003002 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003003 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003004 }
3005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003007
3008 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 if (di == NULL)
3010 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003011
3012 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003013 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003014 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003015
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003016 if (need_convert_to_bool(type, tv))
3017 {
3018 // Destination is a bool and the value is not, but it can be converted.
3019 CLEAR_FIELD(bool_tv);
3020 bool_tv.v_type = VAR_BOOL;
3021 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3022 tv = &bool_tv;
3023 }
3024
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003026 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003028 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003029 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 {
3031 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003032 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003035 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003037 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003038 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003039 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003040 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003041 }
3042
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003043 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003044 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003045 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003047
Bram Moolenaara187c432020-09-16 21:08:28 +02003048 // Check in this order for backwards compatibility:
3049 // - Whether the variable is read-only
3050 // - Whether the variable value is locked
3051 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003052 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003053 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3054 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003055 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003056 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 else
3058 // can only redefine once
3059 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003060
3061 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003064 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003065 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003066 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003068 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003070 if (copy || tv->v_type != VAR_STRING)
3071 {
3072 char_u *val = tv_get_string(tv);
3073
3074 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003075 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 if (di->di_tv.vval.v_string == NULL)
3077 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003078 }
3079 else
3080 {
3081 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003083 tv->vval.v_string = NULL;
3084 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003085 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003088 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003092#ifdef FEAT_SEARCH_EXTRA
3093 else if (STRCMP(varname, "hlsearch") == 0)
3094 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003096 redraw_all_later(SOME_VALID);
3097 }
3098#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003099 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003102 {
3103 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003104 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003105 }
3106 }
3107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003109 }
3110 else // add a new variable
3111 {
3112 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003113 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003114 {
3115 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003116 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003117 }
3118
3119 // Make sure the variable name is valid.
3120 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003121 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3124 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003125 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 STRCPY(di->di_key, varname);
3127 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003128 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003130 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003131 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003133 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 di->di_flags |= DI_FLAGS_LOCK;
3135
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003136 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003138 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139
3140 // Store a pointer to the typval_T, so that it can be found by
3141 // index instead of using a hastab lookup.
3142 if (ga_grow(&si->sn_var_vals, 1) == OK)
3143 {
3144 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3145 + si->sn_var_vals.ga_len;
3146 sv->sv_name = di->di_key;
3147 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003148 if (type == NULL)
3149 sv->sv_type = typval2type(tv, &si->sn_type_list);
3150 else
3151 sv->sv_type = type;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003152 sv->sv_const = (flags & ASSIGN_CONST);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 sv->sv_export = is_export;
3154 ++si->sn_var_vals.ga_len;
3155
3156 // let ex_export() know the export worked.
3157 is_export = FALSE;
3158 }
3159 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003160 }
3161
3162 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003164 else
3165 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 di->di_tv = *tv;
3167 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003168 init_tv(tv);
3169 }
3170
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003171 // ":const var = val" locks the value
3172 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003173 // Like :lockvar! name: lock the value and what it contains, but only
3174 // if the reference count is up to one. That locks only literal
3175 // values.
3176 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003177 return;
3178failed:
3179 if (!copy)
3180 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003181}
3182
3183/*
3184 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3185 * Also give an error message.
3186 */
3187 int
3188var_check_ro(int flags, char_u *name, int use_gettext)
3189{
3190 if (flags & DI_FLAGS_RO)
3191 {
3192 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3193 return TRUE;
3194 }
3195 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3196 {
3197 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3198 return TRUE;
3199 }
3200 return FALSE;
3201}
3202
3203/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003204 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3205 * Also give an error message.
3206 */
3207 int
3208var_check_lock(int flags, char_u *name, int use_gettext)
3209{
3210 if (flags & DI_FLAGS_LOCK)
3211 {
3212 semsg(_(e_variable_is_locked_str),
3213 use_gettext ? (char_u *)_(name) : name);
3214 return TRUE;
3215 }
3216 return FALSE;
3217}
3218
3219/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003220 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3221 * Also give an error message.
3222 */
3223 int
3224var_check_fixed(int flags, char_u *name, int use_gettext)
3225{
3226 if (flags & DI_FLAGS_FIX)
3227 {
3228 semsg(_("E795: Cannot delete variable %s"),
3229 use_gettext ? (char_u *)_(name) : name);
3230 return TRUE;
3231 }
3232 return FALSE;
3233}
3234
3235/*
3236 * Check if a funcref is assigned to a valid variable name.
3237 * Return TRUE and give an error if not.
3238 */
3239 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003240var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003241 char_u *name, // points to start of variable name
3242 int new_var) // TRUE when creating the variable
3243{
3244 // Allow for w: b: s: and t:.
3245 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3246 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3247 ? name[2] : name[0]))
3248 {
3249 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3250 name);
3251 return TRUE;
3252 }
3253 // Don't allow hiding a function. When "v" is not NULL we might be
3254 // assigning another function to the same var, the type is checked
3255 // below.
3256 if (new_var && function_exists(name, FALSE))
3257 {
3258 semsg(_("E705: Variable name conflicts with existing function: %s"),
3259 name);
3260 return TRUE;
3261 }
3262 return FALSE;
3263}
3264
3265/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003266 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3267 * value. Also give an error message, using "name" or _("name") when
3268 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003269 */
3270 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003271value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003272{
3273 if (lock & VAR_LOCKED)
3274 {
3275 semsg(_("E741: Value is locked: %s"),
3276 name == NULL ? (char_u *)_("Unknown")
3277 : use_gettext ? (char_u *)_(name)
3278 : name);
3279 return TRUE;
3280 }
3281 if (lock & VAR_FIXED)
3282 {
3283 semsg(_("E742: Cannot change value of %s"),
3284 name == NULL ? (char_u *)_("Unknown")
3285 : use_gettext ? (char_u *)_(name)
3286 : name);
3287 return TRUE;
3288 }
3289 return FALSE;
3290}
3291
3292/*
3293 * Check if a variable name is valid.
3294 * Return FALSE and give an error if not.
3295 */
3296 int
3297valid_varname(char_u *varname)
3298{
3299 char_u *p;
3300
3301 for (p = varname; *p != NUL; ++p)
3302 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3303 && *p != AUTOLOAD_CHAR)
3304 {
3305 semsg(_(e_illvar), varname);
3306 return FALSE;
3307 }
3308 return TRUE;
3309}
3310
3311/*
3312 * getwinvar() and gettabwinvar()
3313 */
3314 static void
3315getwinvar(
3316 typval_T *argvars,
3317 typval_T *rettv,
3318 int off) // 1 for gettabwinvar()
3319{
3320 win_T *win;
3321 char_u *varname;
3322 dictitem_T *v;
3323 tabpage_T *tp = NULL;
3324 int done = FALSE;
3325 win_T *oldcurwin;
3326 tabpage_T *oldtabpage;
3327 int need_switch_win;
3328
3329 if (off == 1)
3330 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3331 else
3332 tp = curtab;
3333 win = find_win_by_nr(&argvars[off], tp);
3334 varname = tv_get_string_chk(&argvars[off + 1]);
3335 ++emsg_off;
3336
3337 rettv->v_type = VAR_STRING;
3338 rettv->vval.v_string = NULL;
3339
3340 if (win != NULL && varname != NULL)
3341 {
3342 // Set curwin to be our win, temporarily. Also set the tabpage,
3343 // otherwise the window is not valid. Only do this when needed,
3344 // autocommands get blocked.
3345 need_switch_win = !(tp == curtab && win == curwin);
3346 if (!need_switch_win
3347 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3348 {
3349 if (*varname == '&')
3350 {
3351 if (varname[1] == NUL)
3352 {
3353 // get all window-local options in a dict
3354 dict_T *opts = get_winbuf_options(FALSE);
3355
3356 if (opts != NULL)
3357 {
3358 rettv_dict_set(rettv, opts);
3359 done = TRUE;
3360 }
3361 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003362 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003363 // window-local-option
3364 done = TRUE;
3365 }
3366 else
3367 {
3368 // Look up the variable.
3369 // Let getwinvar({nr}, "") return the "w:" dictionary.
3370 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3371 varname, FALSE);
3372 if (v != NULL)
3373 {
3374 copy_tv(&v->di_tv, rettv);
3375 done = TRUE;
3376 }
3377 }
3378 }
3379
3380 if (need_switch_win)
3381 // restore previous notion of curwin
3382 restore_win(oldcurwin, oldtabpage, TRUE);
3383 }
3384
3385 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3386 // use the default return value
3387 copy_tv(&argvars[off + 2], rettv);
3388
3389 --emsg_off;
3390}
3391
3392/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003393 * Set option "varname" to the value of "varp" for the current buffer/window.
3394 */
3395 static void
3396set_option_from_tv(char_u *varname, typval_T *varp)
3397{
3398 long numval = 0;
3399 char_u *strval;
3400 char_u nbuf[NUMBUFLEN];
3401 int error = FALSE;
3402
3403 if (!in_vim9script() || varp->v_type != VAR_STRING)
3404 numval = (long)tv_get_number_chk(varp, &error);
3405 strval = tv_get_string_buf_chk(varp, nbuf);
3406 if (!error && strval != NULL)
3407 set_option_value(varname, numval, strval, OPT_LOCAL);
3408}
3409
3410/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003411 * "setwinvar()" and "settabwinvar()" functions
3412 */
3413 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003414setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003415{
3416 win_T *win;
3417 win_T *save_curwin;
3418 tabpage_T *save_curtab;
3419 int need_switch_win;
3420 char_u *varname, *winvarname;
3421 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003422 tabpage_T *tp = NULL;
3423
3424 if (check_secure())
3425 return;
3426
3427 if (off == 1)
3428 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3429 else
3430 tp = curtab;
3431 win = find_win_by_nr(&argvars[off], tp);
3432 varname = tv_get_string_chk(&argvars[off + 1]);
3433 varp = &argvars[off + 2];
3434
3435 if (win != NULL && varname != NULL && varp != NULL)
3436 {
3437 need_switch_win = !(tp == curtab && win == curwin);
3438 if (!need_switch_win
3439 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3440 {
3441 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003442 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003443 else
3444 {
3445 winvarname = alloc(STRLEN(varname) + 3);
3446 if (winvarname != NULL)
3447 {
3448 STRCPY(winvarname, "w:");
3449 STRCPY(winvarname + 2, varname);
3450 set_var(winvarname, varp, TRUE);
3451 vim_free(winvarname);
3452 }
3453 }
3454 }
3455 if (need_switch_win)
3456 restore_win(save_curwin, save_curtab, TRUE);
3457 }
3458}
3459
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003460/*
3461 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3462 * v:option_type, and v:option_command.
3463 */
3464 void
3465reset_v_option_vars(void)
3466{
3467 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3468 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3469 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3470 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3471 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3472 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3473}
3474
3475/*
3476 * Add an assert error to v:errors.
3477 */
3478 void
3479assert_error(garray_T *gap)
3480{
3481 struct vimvar *vp = &vimvars[VV_ERRORS];
3482
3483 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003484 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003485 set_vim_var_list(VV_ERRORS, list_alloc());
3486 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3487}
3488
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003489 int
3490var_exists(char_u *var)
3491{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003492 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003493 char_u *name;
3494 char_u *tofree;
3495 typval_T tv;
3496 int len = 0;
3497 int n = FALSE;
3498
3499 // get_name_len() takes care of expanding curly braces
3500 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003501 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003502 if (len > 0)
3503 {
3504 if (tofree != NULL)
3505 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003506 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003507 if (n)
3508 {
3509 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003510 arg = skipwhite(arg);
3511 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003512 if (n)
3513 clear_tv(&tv);
3514 }
3515 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003516 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003517 n = FALSE;
3518
3519 vim_free(tofree);
3520 return n;
3521}
3522
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003523static lval_T *redir_lval = NULL;
3524#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3525static garray_T redir_ga; // only valid when redir_lval is not NULL
3526static char_u *redir_endp = NULL;
3527static char_u *redir_varname = NULL;
3528
3529/*
3530 * Start recording command output to a variable
3531 * When "append" is TRUE append to an existing variable.
3532 * Returns OK if successfully completed the setup. FAIL otherwise.
3533 */
3534 int
3535var_redir_start(char_u *name, int append)
3536{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003537 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003538 typval_T tv;
3539
3540 // Catch a bad name early.
3541 if (!eval_isnamec1(*name))
3542 {
3543 emsg(_(e_invarg));
3544 return FAIL;
3545 }
3546
3547 // Make a copy of the name, it is used in redir_lval until redir ends.
3548 redir_varname = vim_strsave(name);
3549 if (redir_varname == NULL)
3550 return FAIL;
3551
3552 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3553 if (redir_lval == NULL)
3554 {
3555 var_redir_stop();
3556 return FAIL;
3557 }
3558
3559 // The output is stored in growarray "redir_ga" until redirection ends.
3560 ga_init2(&redir_ga, (int)sizeof(char), 500);
3561
3562 // Parse the variable name (can be a dict or list entry).
3563 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3564 FNE_CHECK_START);
3565 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3566 {
3567 clear_lval(redir_lval);
3568 if (redir_endp != NULL && *redir_endp != NUL)
3569 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003570 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003571 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003572 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003573 redir_endp = NULL; // don't store a value, only cleanup
3574 var_redir_stop();
3575 return FAIL;
3576 }
3577
3578 // check if we can write to the variable: set it to or append an empty
3579 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003580 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003581 tv.v_type = VAR_STRING;
3582 tv.vval.v_string = (char_u *)"";
3583 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003585 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003586 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003587 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003588 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003589 {
3590 redir_endp = NULL; // don't store a value, only cleanup
3591 var_redir_stop();
3592 return FAIL;
3593 }
3594
3595 return OK;
3596}
3597
3598/*
3599 * Append "value[value_len]" to the variable set by var_redir_start().
3600 * The actual appending is postponed until redirection ends, because the value
3601 * appended may in fact be the string we write to, changing it may cause freed
3602 * memory to be used:
3603 * :redir => foo
3604 * :let foo
3605 * :redir END
3606 */
3607 void
3608var_redir_str(char_u *value, int value_len)
3609{
3610 int len;
3611
3612 if (redir_lval == NULL)
3613 return;
3614
3615 if (value_len == -1)
3616 len = (int)STRLEN(value); // Append the entire string
3617 else
3618 len = value_len; // Append only "value_len" characters
3619
3620 if (ga_grow(&redir_ga, len) == OK)
3621 {
3622 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3623 redir_ga.ga_len += len;
3624 }
3625 else
3626 var_redir_stop();
3627}
3628
3629/*
3630 * Stop redirecting command output to a variable.
3631 * Frees the allocated memory.
3632 */
3633 void
3634var_redir_stop(void)
3635{
3636 typval_T tv;
3637
3638 if (EVALCMD_BUSY)
3639 {
3640 redir_lval = NULL;
3641 return;
3642 }
3643
3644 if (redir_lval != NULL)
3645 {
3646 // If there was no error: assign the text to the variable.
3647 if (redir_endp != NULL)
3648 {
3649 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3650 tv.v_type = VAR_STRING;
3651 tv.vval.v_string = redir_ga.ga_data;
3652 // Call get_lval() again, if it's inside a Dict or List it may
3653 // have changed.
3654 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3655 FALSE, FALSE, 0, FNE_CHECK_START);
3656 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003658 (char_u *)".");
3659 clear_lval(redir_lval);
3660 }
3661
3662 // free the collected output
3663 VIM_CLEAR(redir_ga.ga_data);
3664
3665 VIM_CLEAR(redir_lval);
3666 }
3667 VIM_CLEAR(redir_varname);
3668}
3669
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003670/*
3671 * "gettabvar()" function
3672 */
3673 void
3674f_gettabvar(typval_T *argvars, typval_T *rettv)
3675{
3676 win_T *oldcurwin;
3677 tabpage_T *tp, *oldtabpage;
3678 dictitem_T *v;
3679 char_u *varname;
3680 int done = FALSE;
3681
3682 rettv->v_type = VAR_STRING;
3683 rettv->vval.v_string = NULL;
3684
3685 varname = tv_get_string_chk(&argvars[1]);
3686 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3687 if (tp != NULL && varname != NULL)
3688 {
3689 // Set tp to be our tabpage, temporarily. Also set the window to the
3690 // first window in the tabpage, otherwise the window is not valid.
3691 if (switch_win(&oldcurwin, &oldtabpage,
3692 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3693 : tp->tp_firstwin, tp, TRUE) == OK)
3694 {
3695 // look up the variable
3696 // Let gettabvar({nr}, "") return the "t:" dictionary.
3697 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3698 if (v != NULL)
3699 {
3700 copy_tv(&v->di_tv, rettv);
3701 done = TRUE;
3702 }
3703 }
3704
3705 // restore previous notion of curwin
3706 restore_win(oldcurwin, oldtabpage, TRUE);
3707 }
3708
3709 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3710 copy_tv(&argvars[2], rettv);
3711}
3712
3713/*
3714 * "gettabwinvar()" function
3715 */
3716 void
3717f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3718{
3719 getwinvar(argvars, rettv, 1);
3720}
3721
3722/*
3723 * "getwinvar()" function
3724 */
3725 void
3726f_getwinvar(typval_T *argvars, typval_T *rettv)
3727{
3728 getwinvar(argvars, rettv, 0);
3729}
3730
3731/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003732 * "getbufvar()" function
3733 */
3734 void
3735f_getbufvar(typval_T *argvars, typval_T *rettv)
3736{
3737 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003738 char_u *varname;
3739 dictitem_T *v;
3740 int done = FALSE;
3741
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003742 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003743 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003744
3745 rettv->v_type = VAR_STRING;
3746 rettv->vval.v_string = NULL;
3747
3748 if (buf != NULL && varname != NULL)
3749 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003750 if (*varname == '&')
3751 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003752 buf_T *save_curbuf = curbuf;
3753
3754 // set curbuf to be our buf, temporarily
3755 curbuf = buf;
3756
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003757 if (varname[1] == NUL)
3758 {
3759 // get all buffer-local options in a dict
3760 dict_T *opts = get_winbuf_options(TRUE);
3761
3762 if (opts != NULL)
3763 {
3764 rettv_dict_set(rettv, opts);
3765 done = TRUE;
3766 }
3767 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003768 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003769 // buffer-local-option
3770 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003771
3772 // restore previous notion of curbuf
3773 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003774 }
3775 else
3776 {
3777 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003778 if (*varname == NUL)
3779 // Let getbufvar({nr}, "") return the "b:" dictionary.
3780 v = &buf->b_bufvar;
3781 else
3782 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3783 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003784 if (v != NULL)
3785 {
3786 copy_tv(&v->di_tv, rettv);
3787 done = TRUE;
3788 }
3789 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003790 }
3791
3792 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3793 // use the default value
3794 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003795}
3796
3797/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003798 * "settabvar()" function
3799 */
3800 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003801f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003802{
3803 tabpage_T *save_curtab;
3804 tabpage_T *tp;
3805 char_u *varname, *tabvarname;
3806 typval_T *varp;
3807
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003808 if (check_secure())
3809 return;
3810
3811 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3812 varname = tv_get_string_chk(&argvars[1]);
3813 varp = &argvars[2];
3814
3815 if (varname != NULL && varp != NULL && tp != NULL)
3816 {
3817 save_curtab = curtab;
3818 goto_tabpage_tp(tp, FALSE, FALSE);
3819
3820 tabvarname = alloc(STRLEN(varname) + 3);
3821 if (tabvarname != NULL)
3822 {
3823 STRCPY(tabvarname, "t:");
3824 STRCPY(tabvarname + 2, varname);
3825 set_var(tabvarname, varp, TRUE);
3826 vim_free(tabvarname);
3827 }
3828
3829 // Restore current tabpage
3830 if (valid_tabpage(save_curtab))
3831 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3832 }
3833}
3834
3835/*
3836 * "settabwinvar()" function
3837 */
3838 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003839f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003840{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003841 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003842}
3843
3844/*
3845 * "setwinvar()" function
3846 */
3847 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003848f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003849{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003850 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003851}
3852
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003853/*
3854 * "setbufvar()" function
3855 */
3856 void
3857f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3858{
3859 buf_T *buf;
3860 char_u *varname, *bufvarname;
3861 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003862
3863 if (check_secure())
3864 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003865 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003866 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003867 varp = &argvars[2];
3868
3869 if (buf != NULL && varname != NULL && varp != NULL)
3870 {
3871 if (*varname == '&')
3872 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003873 aco_save_T aco;
3874
3875 // set curbuf to be our buf, temporarily
3876 aucmd_prepbuf(&aco, buf);
3877
Bram Moolenaar191929b2020-08-19 21:20:49 +02003878 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003879
3880 // reset notion of buffer
3881 aucmd_restbuf(&aco);
3882 }
3883 else
3884 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003885 bufvarname = alloc(STRLEN(varname) + 3);
3886 if (bufvarname != NULL)
3887 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003888 buf_T *save_curbuf = curbuf;
3889
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003890 curbuf = buf;
3891 STRCPY(bufvarname, "b:");
3892 STRCPY(bufvarname + 2, varname);
3893 set_var(bufvarname, varp, TRUE);
3894 vim_free(bufvarname);
3895 curbuf = save_curbuf;
3896 }
3897 }
3898 }
3899}
3900
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003901/*
3902 * Get a callback from "arg". It can be a Funcref or a function name.
3903 * When "arg" is zero return an empty string.
3904 * "cb_name" is not allocated.
3905 * "cb_name" is set to NULL for an invalid argument.
3906 */
3907 callback_T
3908get_callback(typval_T *arg)
3909{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003910 callback_T res;
3911 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003912
3913 res.cb_free_name = FALSE;
3914 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3915 {
3916 res.cb_partial = arg->vval.v_partial;
3917 ++res.cb_partial->pt_refcount;
3918 res.cb_name = partial_name(res.cb_partial);
3919 }
3920 else
3921 {
3922 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003923 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3924 && isdigit(*arg->vval.v_string))
3925 r = FAIL;
3926 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003927 {
3928 // Note that we don't make a copy of the string.
3929 res.cb_name = arg->vval.v_string;
3930 func_ref(res.cb_name);
3931 }
3932 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003933 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003934 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003935 r = FAIL;
3936
3937 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003938 {
3939 emsg(_("E921: Invalid callback argument"));
3940 res.cb_name = NULL;
3941 }
3942 }
3943 return res;
3944}
3945
3946/*
3947 * Copy a callback into a typval_T.
3948 */
3949 void
3950put_callback(callback_T *cb, typval_T *tv)
3951{
3952 if (cb->cb_partial != NULL)
3953 {
3954 tv->v_type = VAR_PARTIAL;
3955 tv->vval.v_partial = cb->cb_partial;
3956 ++tv->vval.v_partial->pt_refcount;
3957 }
3958 else
3959 {
3960 tv->v_type = VAR_FUNC;
3961 tv->vval.v_string = vim_strsave(cb->cb_name);
3962 func_ref(cb->cb_name);
3963 }
3964}
3965
3966/*
3967 * Make a copy of "src" into "dest", allocating the function name if needed,
3968 * without incrementing the refcount.
3969 */
3970 void
3971set_callback(callback_T *dest, callback_T *src)
3972{
3973 if (src->cb_partial == NULL)
3974 {
3975 // just a function name, make a copy
3976 dest->cb_name = vim_strsave(src->cb_name);
3977 dest->cb_free_name = TRUE;
3978 }
3979 else
3980 {
3981 // cb_name is a pointer into cb_partial
3982 dest->cb_name = src->cb_name;
3983 dest->cb_free_name = FALSE;
3984 }
3985 dest->cb_partial = src->cb_partial;
3986}
3987
3988/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003989 * Copy callback from "src" to "dest", incrementing the refcounts.
3990 */
3991 void
3992copy_callback(callback_T *dest, callback_T *src)
3993{
3994 dest->cb_partial = src->cb_partial;
3995 if (dest->cb_partial != NULL)
3996 {
3997 dest->cb_name = src->cb_name;
3998 dest->cb_free_name = FALSE;
3999 ++dest->cb_partial->pt_refcount;
4000 }
4001 else
4002 {
4003 dest->cb_name = vim_strsave(src->cb_name);
4004 dest->cb_free_name = TRUE;
4005 func_ref(src->cb_name);
4006 }
4007}
4008
4009/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004010 * Unref/free "callback" returned by get_callback() or set_callback().
4011 */
4012 void
4013free_callback(callback_T *callback)
4014{
4015 if (callback->cb_partial != NULL)
4016 {
4017 partial_unref(callback->cb_partial);
4018 callback->cb_partial = NULL;
4019 }
4020 else if (callback->cb_name != NULL)
4021 func_unref(callback->cb_name);
4022 if (callback->cb_free_name)
4023 {
4024 vim_free(callback->cb_name);
4025 callback->cb_free_name = FALSE;
4026 }
4027 callback->cb_name = NULL;
4028}
4029
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004030#endif // FEAT_EVAL