blob: 319717ec898b1afec514b78134b970b76bfd9cf4 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200149 {VV_NAME("disallow_let", VAR_NUMBER), 0}, // TODO: remove
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200150};
151
152// shorthand
153#define vv_type vv_di.di_tv.v_type
154#define vv_nr vv_di.di_tv.vval.v_number
155#define vv_float vv_di.di_tv.vval.v_float
156#define vv_str vv_di.di_tv.vval.v_string
157#define vv_list vv_di.di_tv.vval.v_list
158#define vv_dict vv_di.di_tv.vval.v_dict
159#define vv_blob vv_di.di_tv.vval.v_blob
160#define vv_tv vv_di.di_tv
161
162static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200163static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vimvarht vimvardict.dv_hashtab
165
166// for VIM_VERSION_ defines
167#include "version.h"
168
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void list_one_var(dictitem_T *v, char *prefix, int *first);
179static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
180
181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200182 * Initialize global and vim special variables
183 */
184 void
185evalvars_init(void)
186{
187 int i;
188 struct vimvar *p;
189
190 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
191 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
192 vimvardict.dv_lock = VAR_FIXED;
193 hash_init(&compat_hashtab);
194
195 for (i = 0; i < VV_LEN; ++i)
196 {
197 p = &vimvars[i];
198 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
199 {
200 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
201 getout(1);
202 }
203 STRCPY(p->vv_di.di_key, p->vv_name);
204 if (p->vv_flags & VV_RO)
205 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
206 else if (p->vv_flags & VV_RO_SBX)
207 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
208 else
209 p->vv_di.di_flags = DI_FLAGS_FIX;
210
211 // add to v: scope dict, unless the value is not always available
212 if (p->vv_type != VAR_UNKNOWN)
213 hash_add(&vimvarht, p->vv_di.di_key);
214 if (p->vv_flags & VV_COMPAT)
215 // add to compat scope dict
216 hash_add(&compat_hashtab, p->vv_di.di_key);
217 }
218 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
219 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
220
221 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
222 set_vim_var_nr(VV_HLSEARCH, 1L);
223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
304 int i;
305 int abort = FALSE;
306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
309
310 return abort;
311}
312
313/*
314 * Set an internal variable to a string value. Creates the variable if it does
315 * not already exist.
316 */
317 void
318set_internal_string_var(char_u *name, char_u *value)
319{
320 char_u *val;
321 typval_T *tvp;
322
323 val = vim_strsave(value);
324 if (val != NULL)
325 {
326 tvp = alloc_string_tv(val);
327 if (tvp != NULL)
328 {
329 set_var(name, tvp, FALSE);
330 free_tv(tvp);
331 }
332 }
333}
334
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200335 int
336eval_charconvert(
337 char_u *enc_from,
338 char_u *enc_to,
339 char_u *fname_from,
340 char_u *fname_to)
341{
342 int err = FALSE;
343
344 set_vim_var_string(VV_CC_FROM, enc_from, -1);
345 set_vim_var_string(VV_CC_TO, enc_to, -1);
346 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
347 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
348 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
349 err = TRUE;
350 set_vim_var_string(VV_CC_FROM, NULL, -1);
351 set_vim_var_string(VV_CC_TO, NULL, -1);
352 set_vim_var_string(VV_FNAME_IN, NULL, -1);
353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
354
355 if (err)
356 return FAIL;
357 return OK;
358}
359
360# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
361 int
362eval_printexpr(char_u *fname, char_u *args)
363{
364 int err = FALSE;
365
366 set_vim_var_string(VV_FNAME_IN, fname, -1);
367 set_vim_var_string(VV_CMDARG, args, -1);
368 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
369 err = TRUE;
370 set_vim_var_string(VV_FNAME_IN, NULL, -1);
371 set_vim_var_string(VV_CMDARG, NULL, -1);
372
373 if (err)
374 {
375 mch_remove(fname);
376 return FAIL;
377 }
378 return OK;
379}
380# endif
381
382# if defined(FEAT_DIFF) || defined(PROTO)
383 void
384eval_diff(
385 char_u *origfile,
386 char_u *newfile,
387 char_u *outfile)
388{
389 int err = FALSE;
390
391 set_vim_var_string(VV_FNAME_IN, origfile, -1);
392 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
393 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
394 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
395 set_vim_var_string(VV_FNAME_IN, NULL, -1);
396 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
397 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
398}
399
400 void
401eval_patch(
402 char_u *origfile,
403 char_u *difffile,
404 char_u *outfile)
405{
406 int err;
407
408 set_vim_var_string(VV_FNAME_IN, origfile, -1);
409 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
410 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
411 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
412 set_vim_var_string(VV_FNAME_IN, NULL, -1);
413 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
414 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
415}
416# endif
417
418#if defined(FEAT_SPELL) || defined(PROTO)
419/*
420 * Evaluate an expression to a list with suggestions.
421 * For the "expr:" part of 'spellsuggest'.
422 * Returns NULL when there is an error.
423 */
424 list_T *
425eval_spell_expr(char_u *badword, char_u *expr)
426{
427 typval_T save_val;
428 typval_T rettv;
429 list_T *list = NULL;
430 char_u *p = skipwhite(expr);
431
432 // Set "v:val" to the bad word.
433 prepare_vimvar(VV_VAL, &save_val);
434 set_vim_var_string(VV_VAL, badword, -1);
435 if (p_verbose == 0)
436 ++emsg_off;
437
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200438 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200439 {
440 if (rettv.v_type != VAR_LIST)
441 clear_tv(&rettv);
442 else
443 list = rettv.vval.v_list;
444 }
445
446 if (p_verbose == 0)
447 --emsg_off;
448 clear_tv(get_vim_var_tv(VV_VAL));
449 restore_vimvar(VV_VAL, &save_val);
450
451 return list;
452}
453
454/*
455 * "list" is supposed to contain two items: a word and a number. Return the
456 * word in "pp" and the number as the return value.
457 * Return -1 if anything isn't right.
458 * Used to get the good word and score from the eval_spell_expr() result.
459 */
460 int
461get_spellword(list_T *list, char_u **pp)
462{
463 listitem_T *li;
464
465 li = list->lv_first;
466 if (li == NULL)
467 return -1;
468 *pp = tv_get_string(&li->li_tv);
469
470 li = li->li_next;
471 if (li == NULL)
472 return -1;
473 return (int)tv_get_number(&li->li_tv);
474}
475#endif
476
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200477/*
478 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200479 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200480 * When not used yet add the variable to the v: hashtable.
481 */
482 void
483prepare_vimvar(int idx, typval_T *save_tv)
484{
485 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200486 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200487 if (vimvars[idx].vv_type == VAR_UNKNOWN)
488 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
489}
490
491/*
492 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200493 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494 * When no longer defined, remove the variable from the v: hashtable.
495 */
496 void
497restore_vimvar(int idx, typval_T *save_tv)
498{
499 hashitem_T *hi;
500
501 vimvars[idx].vv_tv = *save_tv;
502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
503 {
504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
505 if (HASHITEM_EMPTY(hi))
506 internal_error("restore_vimvar()");
507 else
508 hash_remove(&vimvarht, hi);
509 }
510}
511
512/*
513 * List Vim variables.
514 */
515 static void
516list_vim_vars(int *first)
517{
518 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
519}
520
521/*
522 * List script-local variables, if there is a script.
523 */
524 static void
525list_script_vars(int *first)
526{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200527 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200528 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
529 "s:", FALSE, first);
530}
531
532/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200533 * Get a list of lines from a HERE document. The here document is a list of
534 * lines surrounded by a marker.
535 * cmd << {marker}
536 * {line1}
537 * {line2}
538 * ....
539 * {marker}
540 *
541 * The {marker} is a string. If the optional 'trim' word is supplied before the
542 * marker, then the leading indentation before the lines (matching the
543 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200544 *
545 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
546 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
547 * missing, then '.' is accepted as a marker.
548 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200549 * Returns a List with {lines} or NULL.
550 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200552heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200553{
554 char_u *theline;
555 char_u *marker;
556 list_T *l;
557 char_u *p;
558 int marker_indent_len = 0;
559 int text_indent_len = 0;
560 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200561 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200562 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200563
564 if (eap->getline == NULL)
565 {
566 emsg(_("E991: cannot use =<< here"));
567 return NULL;
568 }
569
570 // Check for the optional 'trim' word before the marker
571 cmd = skipwhite(cmd);
572 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
573 {
574 cmd = skipwhite(cmd + 4);
575
576 // Trim the indentation from all the lines in the here document.
577 // The amount of indentation trimmed is the same as the indentation of
578 // the first line after the :let command line. To find the end marker
579 // the indent of the :let command line is trimmed.
580 p = *eap->cmdlinep;
581 while (VIM_ISWHITE(*p))
582 {
583 p++;
584 marker_indent_len++;
585 }
586 text_indent_len = -1;
587 }
588
589 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200590 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200591 {
592 marker = skipwhite(cmd);
593 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200594 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200595 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200596 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200597 return NULL;
598 }
599 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200600 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200601 {
602 emsg(_("E221: Marker cannot start with lower case letter"));
603 return NULL;
604 }
605 }
606 else
607 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200608 // When getting lines for an embedded script, if the marker is missing,
609 // accept '.' as the marker.
610 if (script_get)
611 marker = dot;
612 else
613 {
614 emsg(_("E172: Missing marker"));
615 return NULL;
616 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200617 }
618
619 l = list_alloc();
620 if (l == NULL)
621 return NULL;
622
623 for (;;)
624 {
625 int mi = 0;
626 int ti = 0;
627
628 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
629 if (theline == NULL)
630 {
631 semsg(_("E990: Missing end marker '%s'"), marker);
632 break;
633 }
634
635 // with "trim": skip the indent matching the :let line to find the
636 // marker
637 if (marker_indent_len > 0
638 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
639 mi = marker_indent_len;
640 if (STRCMP(marker, theline + mi) == 0)
641 {
642 vim_free(theline);
643 break;
644 }
645
646 if (text_indent_len == -1 && *theline != NUL)
647 {
648 // set the text indent from the first line.
649 p = theline;
650 text_indent_len = 0;
651 while (VIM_ISWHITE(*p))
652 {
653 p++;
654 text_indent_len++;
655 }
656 text_indent = vim_strnsave(theline, text_indent_len);
657 }
658 // with "trim": skip the indent matching the first line
659 if (text_indent != NULL)
660 for (ti = 0; ti < text_indent_len; ++ti)
661 if (theline[ti] != text_indent[ti])
662 break;
663
664 if (list_append_string(l, theline + ti, -1) == FAIL)
665 break;
666 vim_free(theline);
667 }
668 vim_free(text_indent);
669
670 return l;
671}
672
673/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200674 * Vim9 variable declaration:
675 * ":var name"
676 * ":var name: type"
677 * ":var name = expr"
678 * ":var name: type = expr"
679 * etc.
680 */
681 void
682ex_var(exarg_T *eap)
683{
684 if (!in_vim9script())
685 {
686 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
687 return;
688 }
689 ex_let(eap);
690}
691
692/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200693 * ":let" list all variable values
694 * ":let var1 var2" list variable values
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 var ..= expr" assignment command.
703 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100705 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200706 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200707 * ":final var = expr" assignment command.
708 * ":final [var1, var2] = expr" unpack list.
709 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200710 * ":const" list all variable values
711 * ":const var1 var2" list variable values
712 * ":const var = expr" assignment command.
713 * ":const [var1, var2] = expr" unpack list.
714 */
715 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200716ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200717{
718 char_u *arg = eap->arg;
719 char_u *expr = NULL;
720 typval_T rettv;
721 int i;
722 int var_count = 0;
723 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200724 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200725 char_u *argend;
726 int first = TRUE;
727 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200728 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200729 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200730 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200731
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200732 if (eap->cmdidx == CMD_final && !vim9script)
733 {
734 // In legacy Vim script ":final" is short for ":finally".
735 ex_finally(eap);
736 return;
737 }
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200738 if (get_vim_var_nr(VV_DISALLOW_LET)
739 && eap->cmdidx == CMD_let && vim9script)
740 {
741 emsg(_(e_cannot_use_let_in_vim9_script));
742 return;
743 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200744 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
745 // In legacy Vim script ":const" works like ":final".
746 eap->cmdidx = CMD_final;
747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 // detect Vim9 assignment without ":let" or ":const"
749 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200750 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200752 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200753 if (argend == NULL)
754 return;
755 if (argend > arg && argend[-1] == '.') // for var.='str'
756 --argend;
757 expr = skipwhite(argend);
758 concat = expr[0] == '.'
759 && ((expr[1] == '=' && current_sctx.sc_version < 2)
760 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200761 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
762 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200763 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 {
765 // ":let" without "=": list variables
766 if (*arg == '[')
767 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200768 else if (expr[0] == '.' && expr[1] == '=')
769 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200770 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200771 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200772 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200773 {
774 // Vim9 declaration ":let var: type"
775 arg = vim9_declare_scriptvar(eap, arg);
776 }
777 else
778 {
779 // ":let var1 var2" - list values
780 arg = list_arg_vars(eap, arg, &first);
781 }
782 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200783 else if (!eap->skip)
784 {
785 // ":let"
786 list_glob_vars(&first);
787 list_buf_vars(&first);
788 list_win_vars(&first);
789 list_tab_vars(&first);
790 list_script_vars(&first);
791 list_func_vars(&first);
792 list_vim_vars(&first);
793 }
794 eap->nextcmd = check_nextcmd(arg);
795 }
796 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
797 {
798 list_T *l;
799
800 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200801 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200802 if (l != NULL)
803 {
804 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200805 if (!eap->skip)
806 {
807 op[0] = '=';
808 op[1] = NUL;
809 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200811 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812 clear_tv(&rettv);
813 }
814 }
815 else
816 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200817 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200818 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200819
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200820 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200821 i = FAIL;
822 if (has_assign || concat)
823 {
824 op[0] = '=';
825 op[1] = NUL;
826 if (*expr != '=')
827 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200828 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200829 {
830 // +=, /=, etc. require an existing variable
831 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
832 i = FAIL;
833 }
834 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200835 {
836 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200837 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200838 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200839 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200840 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200841 ++len;
842 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200843 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200844 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200845 }
846 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200847 ++expr;
848
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200849 if (vim9script && (!VIM_ISWHITE(*argend)
850 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200851 {
852 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200853 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 i = FAIL;
855 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856
857 if (eap->skip)
858 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200859 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200860 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200861 if (getline_equal(eap->getline, eap->cookie, getsourceline))
862 {
863 evalarg.eval_getline = eap->getline;
864 evalarg.eval_cookie = eap->cookie;
865 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200866 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200867 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200868 if (eap->skip)
869 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200870 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200871 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 if (eap->skip)
873 {
874 if (i != FAIL)
875 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200876 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200877 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200878 {
879 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200880 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200881 clear_tv(&rettv);
882 }
883 }
884}
885
886/*
887 * Assign the typevalue "tv" to the variable or variables at "arg_start".
888 * Handles both "var" with any type and "[var, var; var]" with a list type.
889 * When "op" is not NULL it points to a string with characters that
890 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
891 * or concatenate.
892 * Returns OK or FAIL;
893 */
894 int
895ex_let_vars(
896 char_u *arg_start,
897 typval_T *tv,
898 int copy, // copy values from "tv", don't move
899 int semicolon, // from skip_var_list()
900 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200901 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200902 char_u *op)
903{
904 char_u *arg = arg_start;
905 list_T *l;
906 int i;
907 listitem_T *item;
908 typval_T ltv;
909
910 if (*arg != '[')
911 {
912 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 return FAIL;
915 return OK;
916 }
917
918 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
919 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
920 {
921 emsg(_(e_listreq));
922 return FAIL;
923 }
924
925 i = list_len(l);
926 if (semicolon == 0 && var_count < i)
927 {
928 emsg(_("E687: Less targets than List items"));
929 return FAIL;
930 }
931 if (var_count - semicolon > i)
932 {
933 emsg(_("E688: More targets than List items"));
934 return FAIL;
935 }
936
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200937 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200938 item = l->lv_first;
939 while (*arg != ']')
940 {
941 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200943 item = item->li_next;
944 if (arg == NULL)
945 return FAIL;
946
947 arg = skipwhite(arg);
948 if (*arg == ';')
949 {
950 // Put the rest of the list (may be empty) in the var after ';'.
951 // Create a new list for this.
952 l = list_alloc();
953 if (l == NULL)
954 return FAIL;
955 while (item != NULL)
956 {
957 list_append_tv(l, &item->li_tv);
958 item = item->li_next;
959 }
960
961 ltv.v_type = VAR_LIST;
962 ltv.v_lock = 0;
963 ltv.vval.v_list = l;
964 l->lv_refcount = 1;
965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200967 (char_u *)"]", op);
968 clear_tv(&ltv);
969 if (arg == NULL)
970 return FAIL;
971 break;
972 }
973 else if (*arg != ',' && *arg != ']')
974 {
975 internal_error("ex_let_vars()");
976 return FAIL;
977 }
978 }
979
980 return OK;
981}
982
983/*
984 * Skip over assignable variable "var" or list of variables "[var, var]".
985 * Used for ":let varvar = expr" and ":for varvar in expr".
986 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200987 * for "[var, var; var]" set "semicolon" to 1.
988 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200989 * Return NULL for an error.
990 */
991 char_u *
992skip_var_list(
993 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200996 int *semicolon,
997 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998{
999 char_u *p, *s;
1000
1001 if (*arg == '[')
1002 {
1003 // "[var, var]": find the matching ']'.
1004 p = arg;
1005 for (;;)
1006 {
1007 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001008 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009 if (s == p)
1010 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001011 if (!silent)
1012 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001013 return NULL;
1014 }
1015 ++*var_count;
1016
1017 p = skipwhite(s);
1018 if (*p == ']')
1019 break;
1020 else if (*p == ';')
1021 {
1022 if (*semicolon == 1)
1023 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001024 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 return NULL;
1026 }
1027 *semicolon = 1;
1028 }
1029 else if (*p != ',')
1030 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001031 if (!silent)
1032 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001033 return NULL;
1034 }
1035 }
1036 return p + 1;
1037 }
1038 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001040}
1041
1042/*
1043 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1044 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001047 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 char_u *end;
1051
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001052 if (*arg == '@' && arg[1] != NUL)
1053 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001056 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 // "a: type" is declaring variable "a" with a type, not "a:".
1059 if (end == arg + 2 && end[-1] == ':')
1060 --end;
1061 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001062 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 }
1064 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001065}
1066
1067/*
1068 * List variables for hashtab "ht" with prefix "prefix".
1069 * If "empty" is TRUE also list NULL strings as empty strings.
1070 */
1071 void
1072list_hashtable_vars(
1073 hashtab_T *ht,
1074 char *prefix,
1075 int empty,
1076 int *first)
1077{
1078 hashitem_T *hi;
1079 dictitem_T *di;
1080 int todo;
1081 char_u buf[IOSIZE];
1082
1083 todo = (int)ht->ht_used;
1084 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1085 {
1086 if (!HASHITEM_EMPTY(hi))
1087 {
1088 --todo;
1089 di = HI2DI(hi);
1090
1091 // apply :filter /pat/ to variable name
1092 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1093 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1094 if (message_filtered(buf))
1095 continue;
1096
1097 if (empty || di->di_tv.v_type != VAR_STRING
1098 || di->di_tv.vval.v_string != NULL)
1099 list_one_var(di, prefix, first);
1100 }
1101 }
1102}
1103
1104/*
1105 * List global variables.
1106 */
1107 static void
1108list_glob_vars(int *first)
1109{
1110 list_hashtable_vars(&globvarht, "", TRUE, first);
1111}
1112
1113/*
1114 * List buffer variables.
1115 */
1116 static void
1117list_buf_vars(int *first)
1118{
1119 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1120}
1121
1122/*
1123 * List window variables.
1124 */
1125 static void
1126list_win_vars(int *first)
1127{
1128 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1129}
1130
1131/*
1132 * List tab page variables.
1133 */
1134 static void
1135list_tab_vars(int *first)
1136{
1137 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1138}
1139
1140/*
1141 * List variables in "arg".
1142 */
1143 static char_u *
1144list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1145{
1146 int error = FALSE;
1147 int len;
1148 char_u *name;
1149 char_u *name_start;
1150 char_u *arg_subsc;
1151 char_u *tofree;
1152 typval_T tv;
1153
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001154 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001155 {
1156 if (error || eap->skip)
1157 {
1158 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1159 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1160 {
1161 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001162 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001163 break;
1164 }
1165 }
1166 else
1167 {
1168 // get_name_len() takes care of expanding curly braces
1169 name_start = name = arg;
1170 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1171 if (len <= 0)
1172 {
1173 // This is mainly to keep test 49 working: when expanding
1174 // curly braces fails overrule the exception error message.
1175 if (len < 0 && !aborting())
1176 {
1177 emsg_severe = TRUE;
1178 semsg(_(e_invarg2), arg);
1179 break;
1180 }
1181 error = TRUE;
1182 }
1183 else
1184 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001185 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001186 if (tofree != NULL)
1187 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001188 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001189 error = TRUE;
1190 else
1191 {
1192 // handle d.key, l[idx], f(expr)
1193 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001194 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001195 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 error = TRUE;
1197 else
1198 {
1199 if (arg == arg_subsc && len == 2 && name[1] == ':')
1200 {
1201 switch (*name)
1202 {
1203 case 'g': list_glob_vars(first); break;
1204 case 'b': list_buf_vars(first); break;
1205 case 'w': list_win_vars(first); break;
1206 case 't': list_tab_vars(first); break;
1207 case 'v': list_vim_vars(first); break;
1208 case 's': list_script_vars(first); break;
1209 case 'l': list_func_vars(first); break;
1210 default:
1211 semsg(_("E738: Can't list variables for %s"), name);
1212 }
1213 }
1214 else
1215 {
1216 char_u numbuf[NUMBUFLEN];
1217 char_u *tf;
1218 int c;
1219 char_u *s;
1220
1221 s = echo_string(&tv, &tf, numbuf, 0);
1222 c = *arg;
1223 *arg = NUL;
1224 list_one_var_a("",
1225 arg == arg_subsc ? name : name_start,
1226 tv.v_type,
1227 s == NULL ? (char_u *)"" : s,
1228 first);
1229 *arg = c;
1230 vim_free(tf);
1231 }
1232 clear_tv(&tv);
1233 }
1234 }
1235 }
1236
1237 vim_free(tofree);
1238 }
1239
1240 arg = skipwhite(arg);
1241 }
1242
1243 return arg;
1244}
1245
1246/*
1247 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1248 * Returns a pointer to the char just after the var name.
1249 * Returns NULL if there is an error.
1250 */
1251 static char_u *
1252ex_let_one(
1253 char_u *arg, // points to variable name
1254 typval_T *tv, // value to assign to variable
1255 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001256 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001257 char_u *endchars, // valid chars after variable name or NULL
1258 char_u *op) // "+", "-", "." or NULL
1259{
1260 int c1;
1261 char_u *name;
1262 char_u *p;
1263 char_u *arg_end = NULL;
1264 int len;
1265 int opt_flags;
1266 char_u *tofree = NULL;
1267
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001268 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001269 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1270 {
1271 vim9_declare_error(arg);
1272 return NULL;
1273 }
1274
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001275 // ":let $VAR = expr": Set environment variable.
1276 if (*arg == '$')
1277 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001278 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001279 {
1280 emsg(_("E996: Cannot lock an environment variable"));
1281 return NULL;
1282 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001283
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001284 // Find the end of the name.
1285 ++arg;
1286 name = arg;
1287 len = get_env_len(&arg);
1288 if (len == 0)
1289 semsg(_(e_invarg2), name - 1);
1290 else
1291 {
1292 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1293 semsg(_(e_letwrong), op);
1294 else if (endchars != NULL
1295 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1296 emsg(_(e_letunexp));
1297 else if (!check_secure())
1298 {
1299 c1 = name[len];
1300 name[len] = NUL;
1301 p = tv_get_string_chk(tv);
1302 if (p != NULL && op != NULL && *op == '.')
1303 {
1304 int mustfree = FALSE;
1305 char_u *s = vim_getenv(name, &mustfree);
1306
1307 if (s != NULL)
1308 {
1309 p = tofree = concat_str(s, p);
1310 if (mustfree)
1311 vim_free(s);
1312 }
1313 }
1314 if (p != NULL)
1315 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001316 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001317 arg_end = arg;
1318 }
1319 name[len] = c1;
1320 vim_free(tofree);
1321 }
1322 }
1323 }
1324
1325 // ":let &option = expr": Set option value.
1326 // ":let &l:option = expr": Set local option value.
1327 // ":let &g:option = expr": Set global option value.
1328 else if (*arg == '&')
1329 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001330 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001331 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001333 return NULL;
1334 }
1335 // Find the end of the name.
1336 p = find_option_end(&arg, &opt_flags);
1337 if (p == NULL || (endchars != NULL
1338 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1339 emsg(_(e_letunexp));
1340 else
1341 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001342 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001343 int opt_type;
1344 long numval;
1345 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001346 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001347 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348
1349 c1 = *p;
1350 *p = NUL;
1351
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001352 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1353 if ((opt_type == 1 || opt_type == -1)
1354 && (tv->v_type != VAR_STRING || !in_vim9script()))
1355 // number, possibly hidden
1356 n = (long)tv_get_number(tv);
1357
1358 // Avoid setting a string option to the text "v:false" or similar.
1359 // In Vim9 script also don't convert a number to string.
1360 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1361 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1362 s = tv_get_string_chk(tv);
1363
1364 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001365 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366 if ((opt_type == 1 && *op == '.')
1367 || (opt_type == 0 && *op != '.'))
1368 {
1369 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001370 failed = TRUE; // don't set the value
1371
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001372 }
1373 else
1374 {
1375 if (opt_type == 1) // number
1376 {
1377 switch (*op)
1378 {
1379 case '+': n = numval + n; break;
1380 case '-': n = numval - n; break;
1381 case '*': n = numval * n; break;
1382 case '/': n = (long)num_divide(numval, n); break;
1383 case '%': n = (long)num_modulus(numval, n); break;
1384 }
1385 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001386 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001387 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001388 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389 s = concat_str(stringval, s);
1390 vim_free(stringval);
1391 stringval = s;
1392 }
1393 }
1394 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001395
1396 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001397 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001398 if (opt_type != 0 || s != NULL)
1399 {
1400 set_option_value(arg, n, s, opt_flags);
1401 arg_end = p;
1402 }
1403 else
1404 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001405 }
1406 *p = c1;
1407 vim_free(stringval);
1408 }
1409 }
1410
1411 // ":let @r = expr": Set register contents.
1412 else if (*arg == '@')
1413 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001414 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001415 {
1416 emsg(_("E996: Cannot lock a register"));
1417 return NULL;
1418 }
1419 ++arg;
1420 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1421 semsg(_(e_letwrong), op);
1422 else if (endchars != NULL
1423 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1424 emsg(_(e_letunexp));
1425 else
1426 {
1427 char_u *ptofree = NULL;
1428 char_u *s;
1429
1430 p = tv_get_string_chk(tv);
1431 if (p != NULL && op != NULL && *op == '.')
1432 {
1433 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1434 if (s != NULL)
1435 {
1436 p = ptofree = concat_str(s, p);
1437 vim_free(s);
1438 }
1439 }
1440 if (p != NULL)
1441 {
1442 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1443 arg_end = arg + 1;
1444 }
1445 vim_free(ptofree);
1446 }
1447 }
1448
1449 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451 // ":let {expr} = expr": Idem, name made with curly braces
1452 else if (eval_isnamec1(*arg) || *arg == '{')
1453 {
1454 lval_T lv;
1455
1456 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001457 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001458 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 if (endchars != NULL && vim_strchr(endchars,
1460 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001461 emsg(_(e_letunexp));
1462 else
1463 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 arg_end = p;
1466 }
1467 }
1468 clear_lval(&lv);
1469 }
1470
1471 else
1472 semsg(_(e_invarg2), arg);
1473
1474 return arg_end;
1475}
1476
1477/*
1478 * ":unlet[!] var1 ... " command.
1479 */
1480 void
1481ex_unlet(exarg_T *eap)
1482{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001483 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001484}
1485
1486/*
1487 * ":lockvar" and ":unlockvar" commands
1488 */
1489 void
1490ex_lockvar(exarg_T *eap)
1491{
1492 char_u *arg = eap->arg;
1493 int deep = 2;
1494
1495 if (eap->forceit)
1496 deep = -1;
1497 else if (vim_isdigit(*arg))
1498 {
1499 deep = getdigits(&arg);
1500 arg = skipwhite(arg);
1501 }
1502
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001503 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504}
1505
1506/*
1507 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001508 * Also used for Vim9 script. "callback" is invoked as:
1509 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001510 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001511 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001512ex_unletlock(
1513 exarg_T *eap,
1514 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001515 int deep,
1516 int glv_flags,
1517 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1518 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001519{
1520 char_u *arg = argstart;
1521 char_u *name_end;
1522 int error = FALSE;
1523 lval_T lv;
1524
1525 do
1526 {
1527 if (*arg == '$')
1528 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001529 lv.ll_name = arg;
1530 lv.ll_tv = NULL;
1531 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001532 if (get_env_len(&arg) == 0)
1533 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001534 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001535 return;
1536 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001537 if (!error && !eap->skip
1538 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1539 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001540 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001541 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001542 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001544 // Parse the name and find the end.
1545 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1546 glv_flags, FNE_CHECK_START);
1547 if (lv.ll_name == NULL)
1548 error = TRUE; // error but continue parsing
1549 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1550 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001551 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001552 if (name_end != NULL)
1553 {
1554 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001555 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001556 }
1557 if (!(eap->skip || error))
1558 clear_lval(&lv);
1559 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001560 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001561
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001562 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001563 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001564 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001565
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001566 if (!eap->skip)
1567 clear_lval(&lv);
1568 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001569
1570 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001571 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572
1573 eap->nextcmd = check_nextcmd(arg);
1574}
1575
1576 static int
1577do_unlet_var(
1578 lval_T *lp,
1579 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001580 exarg_T *eap,
1581 int deep UNUSED,
1582 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001584 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001585 int ret = OK;
1586 int cc;
1587
1588 if (lp->ll_tv == NULL)
1589 {
1590 cc = *name_end;
1591 *name_end = NUL;
1592
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001593 // Environment variable, normal name or expanded name.
1594 if (*lp->ll_name == '$')
1595 vim_unsetenv(lp->ll_name + 1);
1596 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597 ret = FAIL;
1598 *name_end = cc;
1599 }
1600 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001601 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001602 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001603 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001604 return FAIL;
1605 else if (lp->ll_range)
1606 {
1607 listitem_T *li;
1608 listitem_T *ll_li = lp->ll_li;
1609 int ll_n1 = lp->ll_n1;
1610
1611 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1612 {
1613 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001614 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615 return FAIL;
1616 ll_li = li;
1617 ++ll_n1;
1618 }
1619
1620 // Delete a range of List items.
1621 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1622 {
1623 li = lp->ll_li->li_next;
1624 listitem_remove(lp->ll_list, lp->ll_li);
1625 lp->ll_li = li;
1626 ++lp->ll_n1;
1627 }
1628 }
1629 else
1630 {
1631 if (lp->ll_list != NULL)
1632 // unlet a List item.
1633 listitem_remove(lp->ll_list, lp->ll_li);
1634 else
1635 // unlet a Dictionary item.
1636 dictitem_remove(lp->ll_dict, lp->ll_di);
1637 }
1638
1639 return ret;
1640}
1641
1642/*
1643 * "unlet" a variable. Return OK if it existed, FAIL if not.
1644 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1645 */
1646 int
1647do_unlet(char_u *name, int forceit)
1648{
1649 hashtab_T *ht;
1650 hashitem_T *hi;
1651 char_u *varname;
1652 dict_T *d;
1653 dictitem_T *di;
1654
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001655 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001656 return FAIL;
1657
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001658 ht = find_var_ht(name, &varname);
1659 if (ht != NULL && *varname != NUL)
1660 {
1661 d = get_current_funccal_dict(ht);
1662 if (d == NULL)
1663 {
1664 if (ht == &globvarht)
1665 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001666 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667 d = &vimvardict;
1668 else
1669 {
1670 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1671 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1672 }
1673 if (d == NULL)
1674 {
1675 internal_error("do_unlet()");
1676 return FAIL;
1677 }
1678 }
1679 hi = hash_find(ht, varname);
1680 if (HASHITEM_EMPTY(hi))
1681 hi = find_hi_in_scoped_ht(name, &ht);
1682 if (hi != NULL && !HASHITEM_EMPTY(hi))
1683 {
1684 di = HI2DI(hi);
1685 if (var_check_fixed(di->di_flags, name, FALSE)
1686 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001687 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001688 return FAIL;
1689
1690 delete_var(ht, hi);
1691 return OK;
1692 }
1693 }
1694 if (forceit)
1695 return OK;
1696 semsg(_("E108: No such variable: \"%s\""), name);
1697 return FAIL;
1698}
1699
1700/*
1701 * Lock or unlock variable indicated by "lp".
1702 * "deep" is the levels to go (-1 for unlimited);
1703 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1704 */
1705 static int
1706do_lock_var(
1707 lval_T *lp,
1708 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001709 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001710 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001711 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001712{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001713 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001714 int ret = OK;
1715 int cc;
1716 dictitem_T *di;
1717
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001718 if (lp->ll_tv == NULL)
1719 {
1720 cc = *name_end;
1721 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001722 if (*lp->ll_name == '$')
1723 {
1724 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001725 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001726 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001727 else
1728 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001729 // Normal name or expanded name.
1730 di = find_var(lp->ll_name, NULL, TRUE);
1731 if (di == NULL)
1732 ret = FAIL;
1733 else if ((di->di_flags & DI_FLAGS_FIX)
1734 && di->di_tv.v_type != VAR_DICT
1735 && di->di_tv.v_type != VAR_LIST)
1736 {
1737 // For historic reasons this error is not given for a list or
1738 // dict. E.g., the b: dict could be locked/unlocked.
1739 semsg(_(e_lock_unlock), lp->ll_name);
1740 ret = FAIL;
1741 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001742 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001743 {
1744 if (lock)
1745 di->di_flags |= DI_FLAGS_LOCK;
1746 else
1747 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001748 if (deep != 0)
1749 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001750 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751 }
1752 *name_end = cc;
1753 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001754 else if (deep == 0)
1755 {
1756 // nothing to do
1757 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001758 else if (lp->ll_range)
1759 {
1760 listitem_T *li = lp->ll_li;
1761
1762 // (un)lock a range of List items.
1763 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1764 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001765 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001766 li = li->li_next;
1767 ++lp->ll_n1;
1768 }
1769 }
1770 else if (lp->ll_list != NULL)
1771 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001772 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001773 else
1774 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001775 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776
1777 return ret;
1778}
1779
1780/*
1781 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001782 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1783 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001784 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001785 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001786item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787{
1788 static int recurse = 0;
1789 list_T *l;
1790 listitem_T *li;
1791 dict_T *d;
1792 blob_T *b;
1793 hashitem_T *hi;
1794 int todo;
1795
1796 if (recurse >= DICT_MAXNEST)
1797 {
1798 emsg(_("E743: variable nested too deep for (un)lock"));
1799 return;
1800 }
1801 if (deep == 0)
1802 return;
1803 ++recurse;
1804
1805 // lock/unlock the item itself
1806 if (lock)
1807 tv->v_lock |= VAR_LOCKED;
1808 else
1809 tv->v_lock &= ~VAR_LOCKED;
1810
1811 switch (tv->v_type)
1812 {
1813 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001814 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001815 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001816 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001817 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001818 case VAR_STRING:
1819 case VAR_FUNC:
1820 case VAR_PARTIAL:
1821 case VAR_FLOAT:
1822 case VAR_SPECIAL:
1823 case VAR_JOB:
1824 case VAR_CHANNEL:
1825 break;
1826
1827 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001828 if ((b = tv->vval.v_blob) != NULL
1829 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001830 {
1831 if (lock)
1832 b->bv_lock |= VAR_LOCKED;
1833 else
1834 b->bv_lock &= ~VAR_LOCKED;
1835 }
1836 break;
1837 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001838 if ((l = tv->vval.v_list) != NULL
1839 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001840 {
1841 if (lock)
1842 l->lv_lock |= VAR_LOCKED;
1843 else
1844 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001845 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001847 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001848 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 }
1850 break;
1851 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001852 if ((d = tv->vval.v_dict) != NULL
1853 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 {
1855 if (lock)
1856 d->dv_lock |= VAR_LOCKED;
1857 else
1858 d->dv_lock &= ~VAR_LOCKED;
1859 if (deep < 0 || deep > 1)
1860 {
1861 // recursive: lock/unlock the items the List contains
1862 todo = (int)d->dv_hashtab.ht_used;
1863 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1864 {
1865 if (!HASHITEM_EMPTY(hi))
1866 {
1867 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001868 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1869 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001870 }
1871 }
1872 }
1873 }
1874 }
1875 --recurse;
1876}
1877
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001878#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1879/*
1880 * Delete all "menutrans_" variables.
1881 */
1882 void
1883del_menutrans_vars(void)
1884{
1885 hashitem_T *hi;
1886 int todo;
1887
1888 hash_lock(&globvarht);
1889 todo = (int)globvarht.ht_used;
1890 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1891 {
1892 if (!HASHITEM_EMPTY(hi))
1893 {
1894 --todo;
1895 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1896 delete_var(&globvarht, hi);
1897 }
1898 }
1899 hash_unlock(&globvarht);
1900}
1901#endif
1902
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001903/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001904 * Local string buffer for the next two functions to store a variable name
1905 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1906 * get_user_var_name().
1907 */
1908
1909static char_u *varnamebuf = NULL;
1910static int varnamebuflen = 0;
1911
1912/*
1913 * Function to concatenate a prefix and a variable name.
1914 */
1915 static char_u *
1916cat_prefix_varname(int prefix, char_u *name)
1917{
1918 int len;
1919
1920 len = (int)STRLEN(name) + 3;
1921 if (len > varnamebuflen)
1922 {
1923 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001924 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001925 varnamebuf = alloc(len);
1926 if (varnamebuf == NULL)
1927 {
1928 varnamebuflen = 0;
1929 return NULL;
1930 }
1931 varnamebuflen = len;
1932 }
1933 *varnamebuf = prefix;
1934 varnamebuf[1] = ':';
1935 STRCPY(varnamebuf + 2, name);
1936 return varnamebuf;
1937}
1938
1939/*
1940 * Function given to ExpandGeneric() to obtain the list of user defined
1941 * (global/buffer/window/built-in) variable names.
1942 */
1943 char_u *
1944get_user_var_name(expand_T *xp, int idx)
1945{
1946 static long_u gdone;
1947 static long_u bdone;
1948 static long_u wdone;
1949 static long_u tdone;
1950 static int vidx;
1951 static hashitem_T *hi;
1952 hashtab_T *ht;
1953
1954 if (idx == 0)
1955 {
1956 gdone = bdone = wdone = vidx = 0;
1957 tdone = 0;
1958 }
1959
1960 // Global variables
1961 if (gdone < globvarht.ht_used)
1962 {
1963 if (gdone++ == 0)
1964 hi = globvarht.ht_array;
1965 else
1966 ++hi;
1967 while (HASHITEM_EMPTY(hi))
1968 ++hi;
1969 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1970 return cat_prefix_varname('g', hi->hi_key);
1971 return hi->hi_key;
1972 }
1973
1974 // b: variables
1975 ht = &curbuf->b_vars->dv_hashtab;
1976 if (bdone < ht->ht_used)
1977 {
1978 if (bdone++ == 0)
1979 hi = ht->ht_array;
1980 else
1981 ++hi;
1982 while (HASHITEM_EMPTY(hi))
1983 ++hi;
1984 return cat_prefix_varname('b', hi->hi_key);
1985 }
1986
1987 // w: variables
1988 ht = &curwin->w_vars->dv_hashtab;
1989 if (wdone < ht->ht_used)
1990 {
1991 if (wdone++ == 0)
1992 hi = ht->ht_array;
1993 else
1994 ++hi;
1995 while (HASHITEM_EMPTY(hi))
1996 ++hi;
1997 return cat_prefix_varname('w', hi->hi_key);
1998 }
1999
2000 // t: variables
2001 ht = &curtab->tp_vars->dv_hashtab;
2002 if (tdone < ht->ht_used)
2003 {
2004 if (tdone++ == 0)
2005 hi = ht->ht_array;
2006 else
2007 ++hi;
2008 while (HASHITEM_EMPTY(hi))
2009 ++hi;
2010 return cat_prefix_varname('t', hi->hi_key);
2011 }
2012
2013 // v: variables
2014 if (vidx < VV_LEN)
2015 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2016
2017 VIM_CLEAR(varnamebuf);
2018 varnamebuflen = 0;
2019 return NULL;
2020}
2021
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002022 char *
2023get_var_special_name(int nr)
2024{
2025 switch (nr)
2026 {
2027 case VVAL_FALSE: return "v:false";
2028 case VVAL_TRUE: return "v:true";
2029 case VVAL_NONE: return "v:none";
2030 case VVAL_NULL: return "v:null";
2031 }
2032 internal_error("get_var_special_name()");
2033 return "42";
2034}
2035
2036/*
2037 * Returns the global variable dictionary
2038 */
2039 dict_T *
2040get_globvar_dict(void)
2041{
2042 return &globvardict;
2043}
2044
2045/*
2046 * Returns the global variable hash table
2047 */
2048 hashtab_T *
2049get_globvar_ht(void)
2050{
2051 return &globvarht;
2052}
2053
2054/*
2055 * Returns the v: variable dictionary
2056 */
2057 dict_T *
2058get_vimvar_dict(void)
2059{
2060 return &vimvardict;
2061}
2062
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002063/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002065 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 */
2067 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002068find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002069{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002070 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2071 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072
2073 if (di == NULL)
2074 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002075 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2077 return (int)(vv - vimvars);
2078}
2079
2080
2081/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002082 * Set type of v: variable to "type".
2083 */
2084 void
2085set_vim_var_type(int idx, vartype_T type)
2086{
2087 vimvars[idx].vv_type = type;
2088}
2089
2090/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002091 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002092 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002093 */
2094 void
2095set_vim_var_nr(int idx, varnumber_T val)
2096{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002097 vimvars[idx].vv_nr = val;
2098}
2099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 char *
2101get_vim_var_name(int idx)
2102{
2103 return vimvars[idx].vv_name;
2104}
2105
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002106/*
2107 * Get typval_T v: variable value.
2108 */
2109 typval_T *
2110get_vim_var_tv(int idx)
2111{
2112 return &vimvars[idx].vv_tv;
2113}
2114
2115/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002116 * Set v: variable to "tv". Only accepts the same type.
2117 * Takes over the value of "tv".
2118 */
2119 int
2120set_vim_var_tv(int idx, typval_T *tv)
2121{
2122 if (vimvars[idx].vv_type != tv->v_type)
2123 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002124 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002125 clear_tv(tv);
2126 return FAIL;
2127 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002128 // VV_RO is also checked when compiling, but let's check here as well.
2129 if (vimvars[idx].vv_flags & VV_RO)
2130 {
2131 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2132 return FAIL;
2133 }
2134 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2135 {
2136 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2137 return FAIL;
2138 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002139 clear_tv(&vimvars[idx].vv_di.di_tv);
2140 vimvars[idx].vv_di.di_tv = *tv;
2141 return OK;
2142}
2143
2144/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002145 * Get number v: variable value.
2146 */
2147 varnumber_T
2148get_vim_var_nr(int idx)
2149{
2150 return vimvars[idx].vv_nr;
2151}
2152
2153/*
2154 * Get string v: variable value. Uses a static buffer, can only be used once.
2155 * If the String variable has never been set, return an empty string.
2156 * Never returns NULL;
2157 */
2158 char_u *
2159get_vim_var_str(int idx)
2160{
2161 return tv_get_string(&vimvars[idx].vv_tv);
2162}
2163
2164/*
2165 * Get List v: variable value. Caller must take care of reference count when
2166 * needed.
2167 */
2168 list_T *
2169get_vim_var_list(int idx)
2170{
2171 return vimvars[idx].vv_list;
2172}
2173
2174/*
2175 * Get Dict v: variable value. Caller must take care of reference count when
2176 * needed.
2177 */
2178 dict_T *
2179get_vim_var_dict(int idx)
2180{
2181 return vimvars[idx].vv_dict;
2182}
2183
2184/*
2185 * Set v:char to character "c".
2186 */
2187 void
2188set_vim_var_char(int c)
2189{
2190 char_u buf[MB_MAXBYTES + 1];
2191
2192 if (has_mbyte)
2193 buf[(*mb_char2bytes)(c, buf)] = NUL;
2194 else
2195 {
2196 buf[0] = c;
2197 buf[1] = NUL;
2198 }
2199 set_vim_var_string(VV_CHAR, buf, -1);
2200}
2201
2202/*
2203 * Set v:count to "count" and v:count1 to "count1".
2204 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2205 */
2206 void
2207set_vcount(
2208 long count,
2209 long count1,
2210 int set_prevcount)
2211{
2212 if (set_prevcount)
2213 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2214 vimvars[VV_COUNT].vv_nr = count;
2215 vimvars[VV_COUNT1].vv_nr = count1;
2216}
2217
2218/*
2219 * Save variables that might be changed as a side effect. Used when executing
2220 * a timer callback.
2221 */
2222 void
2223save_vimvars(vimvars_save_T *vvsave)
2224{
2225 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2226 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2227 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2228}
2229
2230/*
2231 * Restore variables saved by save_vimvars().
2232 */
2233 void
2234restore_vimvars(vimvars_save_T *vvsave)
2235{
2236 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2237 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2238 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2239}
2240
2241/*
2242 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2243 * value.
2244 */
2245 void
2246set_vim_var_string(
2247 int idx,
2248 char_u *val,
2249 int len) // length of "val" to use or -1 (whole string)
2250{
2251 clear_tv(&vimvars[idx].vv_di.di_tv);
2252 vimvars[idx].vv_type = VAR_STRING;
2253 if (val == NULL)
2254 vimvars[idx].vv_str = NULL;
2255 else if (len == -1)
2256 vimvars[idx].vv_str = vim_strsave(val);
2257 else
2258 vimvars[idx].vv_str = vim_strnsave(val, len);
2259}
2260
2261/*
2262 * Set List v: variable to "val".
2263 */
2264 void
2265set_vim_var_list(int idx, list_T *val)
2266{
2267 clear_tv(&vimvars[idx].vv_di.di_tv);
2268 vimvars[idx].vv_type = VAR_LIST;
2269 vimvars[idx].vv_list = val;
2270 if (val != NULL)
2271 ++val->lv_refcount;
2272}
2273
2274/*
2275 * Set Dictionary v: variable to "val".
2276 */
2277 void
2278set_vim_var_dict(int idx, dict_T *val)
2279{
2280 clear_tv(&vimvars[idx].vv_di.di_tv);
2281 vimvars[idx].vv_type = VAR_DICT;
2282 vimvars[idx].vv_dict = val;
2283 if (val != NULL)
2284 {
2285 ++val->dv_refcount;
2286 dict_set_items_ro(val);
2287 }
2288}
2289
2290/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002291 * Set the v:argv list.
2292 */
2293 void
2294set_argv_var(char **argv, int argc)
2295{
2296 list_T *l = list_alloc();
2297 int i;
2298
2299 if (l == NULL)
2300 getout(1);
2301 l->lv_lock = VAR_FIXED;
2302 for (i = 0; i < argc; ++i)
2303 {
2304 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2305 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002306 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002307 }
2308 set_vim_var_list(VV_ARGV, l);
2309}
2310
2311/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002312 * Reset v:register, taking the 'clipboard' setting into account.
2313 */
2314 void
2315reset_reg_var(void)
2316{
2317 int regname = 0;
2318
2319 // Adjust the register according to 'clipboard', so that when
2320 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2321#ifdef FEAT_CLIPBOARD
2322 adjust_clip_reg(&regname);
2323#endif
2324 set_reg_var(regname);
2325}
2326
2327/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002328 * Set v:register if needed.
2329 */
2330 void
2331set_reg_var(int c)
2332{
2333 char_u regname;
2334
2335 if (c == 0 || c == ' ')
2336 regname = '"';
2337 else
2338 regname = c;
2339 // Avoid free/alloc when the value is already right.
2340 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2341 set_vim_var_string(VV_REG, &regname, 1);
2342}
2343
2344/*
2345 * Get or set v:exception. If "oldval" == NULL, return the current value.
2346 * Otherwise, restore the value to "oldval" and return NULL.
2347 * Must always be called in pairs to save and restore v:exception! Does not
2348 * take care of memory allocations.
2349 */
2350 char_u *
2351v_exception(char_u *oldval)
2352{
2353 if (oldval == NULL)
2354 return vimvars[VV_EXCEPTION].vv_str;
2355
2356 vimvars[VV_EXCEPTION].vv_str = oldval;
2357 return NULL;
2358}
2359
2360/*
2361 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2362 * Otherwise, restore the value to "oldval" and return NULL.
2363 * Must always be called in pairs to save and restore v:throwpoint! Does not
2364 * take care of memory allocations.
2365 */
2366 char_u *
2367v_throwpoint(char_u *oldval)
2368{
2369 if (oldval == NULL)
2370 return vimvars[VV_THROWPOINT].vv_str;
2371
2372 vimvars[VV_THROWPOINT].vv_str = oldval;
2373 return NULL;
2374}
2375
2376/*
2377 * Set v:cmdarg.
2378 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2379 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2380 * Must always be called in pairs!
2381 */
2382 char_u *
2383set_cmdarg(exarg_T *eap, char_u *oldarg)
2384{
2385 char_u *oldval;
2386 char_u *newval;
2387 unsigned len;
2388
2389 oldval = vimvars[VV_CMDARG].vv_str;
2390 if (eap == NULL)
2391 {
2392 vim_free(oldval);
2393 vimvars[VV_CMDARG].vv_str = oldarg;
2394 return NULL;
2395 }
2396
2397 if (eap->force_bin == FORCE_BIN)
2398 len = 6;
2399 else if (eap->force_bin == FORCE_NOBIN)
2400 len = 8;
2401 else
2402 len = 0;
2403
2404 if (eap->read_edit)
2405 len += 7;
2406
2407 if (eap->force_ff != 0)
2408 len += 10; // " ++ff=unix"
2409 if (eap->force_enc != 0)
2410 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2411 if (eap->bad_char != 0)
2412 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2413
2414 newval = alloc(len + 1);
2415 if (newval == NULL)
2416 return NULL;
2417
2418 if (eap->force_bin == FORCE_BIN)
2419 sprintf((char *)newval, " ++bin");
2420 else if (eap->force_bin == FORCE_NOBIN)
2421 sprintf((char *)newval, " ++nobin");
2422 else
2423 *newval = NUL;
2424
2425 if (eap->read_edit)
2426 STRCAT(newval, " ++edit");
2427
2428 if (eap->force_ff != 0)
2429 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2430 eap->force_ff == 'u' ? "unix"
2431 : eap->force_ff == 'd' ? "dos"
2432 : "mac");
2433 if (eap->force_enc != 0)
2434 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2435 eap->cmd + eap->force_enc);
2436 if (eap->bad_char == BAD_KEEP)
2437 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2438 else if (eap->bad_char == BAD_DROP)
2439 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2440 else if (eap->bad_char != 0)
2441 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2442 vimvars[VV_CMDARG].vv_str = newval;
2443 return oldval;
2444}
2445
2446/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002447 * Get the value of internal variable "name".
2448 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2449 */
2450 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002451eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002452 char_u *name,
2453 int len, // length of "name"
2454 typval_T *rettv, // NULL when only checking existence
2455 dictitem_T **dip, // non-NULL when typval's dict item is needed
2456 int verbose, // may give error message
2457 int no_autoload) // do not use script autoloading
2458{
2459 int ret = OK;
2460 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002461 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002462 dictitem_T *v;
2463 int cc;
2464
2465 // truncate the name, so that we can use strcmp()
2466 cc = name[len];
2467 name[len] = NUL;
2468
2469 // Check for user-defined variables.
2470 v = find_var(name, NULL, no_autoload);
2471 if (v != NULL)
2472 {
2473 tv = &v->di_tv;
2474 if (dip != NULL)
2475 *dip = v;
2476 }
2477
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002478 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002480 imported_T *import;
2481 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2482
2483 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484
2485 // imported variable from another script
2486 if (import != NULL)
2487 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002488 if (import->imp_funcname != NULL)
2489 {
2490 foundFunc = TRUE;
2491 if (rettv != NULL)
2492 {
2493 rettv->v_type = VAR_FUNC;
2494 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2495 }
2496 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002497 else if (import->imp_all)
2498 {
2499 emsg("Sorry, 'import * as X' not implemented yet");
2500 ret = FAIL;
2501 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002502 else
2503 {
2504 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002505 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002507 tv = sv->sv_tv;
2508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002510 else if (in_vim9script())
2511 {
2512 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2513
2514 if (ufunc != NULL)
2515 {
2516 foundFunc = TRUE;
2517 if (rettv != NULL)
2518 {
2519 rettv->v_type = VAR_FUNC;
2520 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2521 }
2522 }
2523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 }
2525
Bram Moolenaarc620c052020-07-08 15:16:19 +02002526 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002527 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002528 if (tv == NULL)
2529 {
2530 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002531 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002532 ret = FAIL;
2533 }
2534 else if (rettv != NULL)
2535 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002536 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002537
2538 name[len] = cc;
2539
2540 return ret;
2541}
2542
2543/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002544 * Check if variable "name[len]" is a local variable or an argument.
2545 * If so, "*eval_lavars_used" is set to TRUE.
2546 */
2547 void
2548check_vars(char_u *name, int len)
2549{
2550 int cc;
2551 char_u *varname;
2552 hashtab_T *ht;
2553
2554 if (eval_lavars_used == NULL)
2555 return;
2556
2557 // truncate the name, so that we can use strcmp()
2558 cc = name[len];
2559 name[len] = NUL;
2560
2561 ht = find_var_ht(name, &varname);
2562 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2563 {
2564 if (find_var(name, NULL, TRUE) != NULL)
2565 *eval_lavars_used = TRUE;
2566 }
2567
2568 name[len] = cc;
2569}
2570
2571/*
2572 * Find variable "name" in the list of variables.
2573 * Return a pointer to it if found, NULL if not found.
2574 * Careful: "a:0" variables don't have a name.
2575 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2576 * hashtab_T used.
2577 */
2578 dictitem_T *
2579find_var(char_u *name, hashtab_T **htp, int no_autoload)
2580{
2581 char_u *varname;
2582 hashtab_T *ht;
2583 dictitem_T *ret = NULL;
2584
2585 ht = find_var_ht(name, &varname);
2586 if (htp != NULL)
2587 *htp = ht;
2588 if (ht == NULL)
2589 return NULL;
2590 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2591 if (ret != NULL)
2592 return ret;
2593
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002594 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002595 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2596}
2597
2598/*
2599 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002600 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002601 * Returns NULL if not found.
2602 */
2603 dictitem_T *
2604find_var_in_ht(
2605 hashtab_T *ht,
2606 int htname,
2607 char_u *varname,
2608 int no_autoload)
2609{
2610 hashitem_T *hi;
2611
2612 if (*varname == NUL)
2613 {
2614 // Must be something like "s:", otherwise "ht" would be NULL.
2615 switch (htname)
2616 {
2617 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2618 case 'g': return &globvars_var;
2619 case 'v': return &vimvars_var;
2620 case 'b': return &curbuf->b_bufvar;
2621 case 'w': return &curwin->w_winvar;
2622 case 't': return &curtab->tp_winvar;
2623 case 'l': return get_funccal_local_var();
2624 case 'a': return get_funccal_args_var();
2625 }
2626 return NULL;
2627 }
2628
2629 hi = hash_find(ht, varname);
2630 if (HASHITEM_EMPTY(hi))
2631 {
2632 // For global variables we may try auto-loading the script. If it
2633 // worked find the variable again. Don't auto-load a script if it was
2634 // loaded already, otherwise it would be loaded every time when
2635 // checking if a function name is a Funcref variable.
2636 if (ht == &globvarht && !no_autoload)
2637 {
2638 // Note: script_autoload() may make "hi" invalid. It must either
2639 // be obtained again or not used.
2640 if (!script_autoload(varname, FALSE) || aborting())
2641 return NULL;
2642 hi = hash_find(ht, varname);
2643 }
2644 if (HASHITEM_EMPTY(hi))
2645 return NULL;
2646 }
2647 return HI2DI(hi);
2648}
2649
2650/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 * Get the script-local hashtab. NULL if not in a script context.
2652 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002653 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654get_script_local_ht(void)
2655{
2656 scid_T sid = current_sctx.sc_sid;
2657
Bram Moolenaare3d46852020-08-29 13:39:17 +02002658 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 return &SCRIPT_VARS(sid);
2660 return NULL;
2661}
2662
2663/*
2664 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002665 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002667 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2669{
2670 hashtab_T *ht = get_script_local_ht();
2671 char_u buffer[30];
2672 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002673 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 hashitem_T *hi;
2675
2676 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002677 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 if (len < sizeof(buffer) - 1)
2679 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002680 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 vim_strncpy(buffer, name, len);
2682 p = buffer;
2683 }
2684 else
2685 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002686 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002688 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 }
2690
2691 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002692 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693
2694 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002695 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2696 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697
2698 if (p != buffer)
2699 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002700 // Don't return "buffer", gcc complains.
2701 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702}
2703
2704/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002705 * Find the hashtab used for a variable name.
2706 * Return NULL if the name is not valid.
2707 * Set "varname" to the start of name without ':'.
2708 */
2709 hashtab_T *
2710find_var_ht(char_u *name, char_u **varname)
2711{
2712 hashitem_T *hi;
2713 hashtab_T *ht;
2714
2715 if (name[0] == NUL)
2716 return NULL;
2717 if (name[1] != ':')
2718 {
2719 // The name must not start with a colon or #.
2720 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2721 return NULL;
2722 *varname = name;
2723
2724 // "version" is "v:version" in all scopes if scriptversion < 3.
2725 // Same for a few other variables marked with VV_COMPAT.
2726 if (current_sctx.sc_version < 3)
2727 {
2728 hi = hash_find(&compat_hashtab, name);
2729 if (!HASHITEM_EMPTY(hi))
2730 return &compat_hashtab;
2731 }
2732
2733 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002734 if (ht != NULL)
2735 return ht; // local variable
2736
2737 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002738 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 {
2740 ht = get_script_local_ht();
2741 if (ht != NULL)
2742 return ht;
2743 }
2744
2745 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002746 }
2747 *varname = name + 2;
2748 if (*name == 'g') // global variable
2749 return &globvarht;
2750 // There must be no ':' or '#' in the rest of the name, unless g: is used
2751 if (vim_strchr(name + 2, ':') != NULL
2752 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2753 return NULL;
2754 if (*name == 'b') // buffer variable
2755 return &curbuf->b_vars->dv_hashtab;
2756 if (*name == 'w') // window variable
2757 return &curwin->w_vars->dv_hashtab;
2758 if (*name == 't') // tab page variable
2759 return &curtab->tp_vars->dv_hashtab;
2760 if (*name == 'v') // v: variable
2761 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002762 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002763 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002765 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 if (*name == 'a') // a: function argument
2767 return get_funccal_args_ht();
2768 if (*name == 'l') // l: local function variable
2769 return get_funccal_local_ht();
2770 }
2771 if (*name == 's') // script variable
2772 {
2773 ht = get_script_local_ht();
2774 if (ht != NULL)
2775 return ht;
2776 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002777 return NULL;
2778}
2779
2780/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002781 * Get the string value of a (global/local) variable.
2782 * Note: see tv_get_string() for how long the pointer remains valid.
2783 * Returns NULL when it doesn't exist.
2784 */
2785 char_u *
2786get_var_value(char_u *name)
2787{
2788 dictitem_T *v;
2789
2790 v = find_var(name, NULL, FALSE);
2791 if (v == NULL)
2792 return NULL;
2793 return tv_get_string(&v->di_tv);
2794}
2795
2796/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002797 * Allocate a new hashtab for a sourced script. It will be used while
2798 * sourcing this script and when executing functions defined in the script.
2799 */
2800 void
2801new_script_vars(scid_T id)
2802{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002803 scriptvar_T *sv;
2804
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002805 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2806 if (sv == NULL)
2807 return;
2808 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002809 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002810}
2811
2812/*
2813 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2814 * point to it.
2815 */
2816 void
2817init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2818{
2819 hash_init(&dict->dv_hashtab);
2820 dict->dv_lock = 0;
2821 dict->dv_scope = scope;
2822 dict->dv_refcount = DO_NOT_FREE_CNT;
2823 dict->dv_copyID = 0;
2824 dict_var->di_tv.vval.v_dict = dict;
2825 dict_var->di_tv.v_type = VAR_DICT;
2826 dict_var->di_tv.v_lock = VAR_FIXED;
2827 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2828 dict_var->di_key[0] = NUL;
2829}
2830
2831/*
2832 * Unreference a dictionary initialized by init_var_dict().
2833 */
2834 void
2835unref_var_dict(dict_T *dict)
2836{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002837 // Now the dict needs to be freed if no one else is using it, go back to
2838 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002839 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2840 dict_unref(dict);
2841}
2842
2843/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002844 * Clean up a list of internal variables.
2845 * Frees all allocated variables and the value they contain.
2846 * Clears hashtab "ht", does not free it.
2847 */
2848 void
2849vars_clear(hashtab_T *ht)
2850{
2851 vars_clear_ext(ht, TRUE);
2852}
2853
2854/*
2855 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2856 */
2857 void
2858vars_clear_ext(hashtab_T *ht, int free_val)
2859{
2860 int todo;
2861 hashitem_T *hi;
2862 dictitem_T *v;
2863
2864 hash_lock(ht);
2865 todo = (int)ht->ht_used;
2866 for (hi = ht->ht_array; todo > 0; ++hi)
2867 {
2868 if (!HASHITEM_EMPTY(hi))
2869 {
2870 --todo;
2871
2872 // Free the variable. Don't remove it from the hashtab,
2873 // ht_array might change then. hash_clear() takes care of it
2874 // later.
2875 v = HI2DI(hi);
2876 if (free_val)
2877 clear_tv(&v->di_tv);
2878 if (v->di_flags & DI_FLAGS_ALLOC)
2879 vim_free(v);
2880 }
2881 }
2882 hash_clear(ht);
2883 ht->ht_used = 0;
2884}
2885
2886/*
2887 * Delete a variable from hashtab "ht" at item "hi".
2888 * Clear the variable value and free the dictitem.
2889 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002890 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002891delete_var(hashtab_T *ht, hashitem_T *hi)
2892{
2893 dictitem_T *di = HI2DI(hi);
2894
2895 hash_remove(ht, hi);
2896 clear_tv(&di->di_tv);
2897 vim_free(di);
2898}
2899
2900/*
2901 * List the value of one internal variable.
2902 */
2903 static void
2904list_one_var(dictitem_T *v, char *prefix, int *first)
2905{
2906 char_u *tofree;
2907 char_u *s;
2908 char_u numbuf[NUMBUFLEN];
2909
2910 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2911 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2912 s == NULL ? (char_u *)"" : s, first);
2913 vim_free(tofree);
2914}
2915
2916 static void
2917list_one_var_a(
2918 char *prefix,
2919 char_u *name,
2920 int type,
2921 char_u *string,
2922 int *first) // when TRUE clear rest of screen and set to FALSE
2923{
2924 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2925 msg_start();
2926 msg_puts(prefix);
2927 if (name != NULL) // "a:" vars don't have a name stored
2928 msg_puts((char *)name);
2929 msg_putchar(' ');
2930 msg_advance(22);
2931 if (type == VAR_NUMBER)
2932 msg_putchar('#');
2933 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2934 msg_putchar('*');
2935 else if (type == VAR_LIST)
2936 {
2937 msg_putchar('[');
2938 if (*string == '[')
2939 ++string;
2940 }
2941 else if (type == VAR_DICT)
2942 {
2943 msg_putchar('{');
2944 if (*string == '{')
2945 ++string;
2946 }
2947 else
2948 msg_putchar(' ');
2949
2950 msg_outtrans(string);
2951
2952 if (type == VAR_FUNC || type == VAR_PARTIAL)
2953 msg_puts("()");
2954 if (*first)
2955 {
2956 msg_clr_eos();
2957 *first = FALSE;
2958 }
2959}
2960
2961/*
2962 * Set variable "name" to value in "tv".
2963 * If the variable already exists, the value is updated.
2964 * Otherwise the variable is created.
2965 */
2966 void
2967set_var(
2968 char_u *name,
2969 typval_T *tv,
2970 int copy) // make copy of value in "tv"
2971{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002972 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002973}
2974
2975/*
2976 * Set variable "name" to value in "tv".
2977 * If the variable already exists and "is_const" is FALSE the value is updated.
2978 * Otherwise the variable is created.
2979 */
2980 void
2981set_var_const(
2982 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002984 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002985 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002986 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002987{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002988 typval_T *tv = tv_arg;
2989 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002991 char_u *varname;
2992 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002994 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002995
2996 ht = find_var_ht(name, &varname);
2997 if (ht == NULL || *varname == NUL)
2998 {
2999 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003000 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003001 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003002 is_script_local = ht == get_script_local_ht();
3003
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003004 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003005 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003006 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003007 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003008 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003009 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003010 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003011 }
3012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003014
3015 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 if (di == NULL)
3017 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003018
3019 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003020 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003021 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003022
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003023 if (need_convert_to_bool(type, tv))
3024 {
3025 // Destination is a bool and the value is not, but it can be converted.
3026 CLEAR_FIELD(bool_tv);
3027 bool_tv.v_type = VAR_BOOL;
3028 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3029 tv = &bool_tv;
3030 }
3031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003033 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003035 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003036 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 {
3038 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003039 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 }
3041
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003042 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003044 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003045 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003046 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003047 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003048 }
3049
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003050 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003051 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003052 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003054
Bram Moolenaara187c432020-09-16 21:08:28 +02003055 // Check in this order for backwards compatibility:
3056 // - Whether the variable is read-only
3057 // - Whether the variable value is locked
3058 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003059 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003060 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3061 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003062 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003063 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003064 else
3065 // can only redefine once
3066 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003067
3068 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003069
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003071 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003072 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003073 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003075 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077 if (copy || tv->v_type != VAR_STRING)
3078 {
3079 char_u *val = tv_get_string(tv);
3080
3081 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003082 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 if (di->di_tv.vval.v_string == NULL)
3084 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003085 }
3086 else
3087 {
3088 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090 tv->vval.v_string = NULL;
3091 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003092 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003097 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099#ifdef FEAT_SEARCH_EXTRA
3100 else if (STRCMP(varname, "hlsearch") == 0)
3101 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103 redraw_all_later(SOME_VALID);
3104 }
3105#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003106 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003109 {
3110 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003111 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003112 }
3113 }
3114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003116 }
3117 else // add a new variable
3118 {
3119 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003120 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003121 {
3122 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003123 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003124 }
3125
3126 // Make sure the variable name is valid.
3127 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003128 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3131 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003132 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 STRCPY(di->di_key, varname);
3134 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003135 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003137 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003140 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 di->di_flags |= DI_FLAGS_LOCK;
3142
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003143 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003145 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146
3147 // Store a pointer to the typval_T, so that it can be found by
3148 // index instead of using a hastab lookup.
3149 if (ga_grow(&si->sn_var_vals, 1) == OK)
3150 {
3151 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3152 + si->sn_var_vals.ga_len;
3153 sv->sv_name = di->di_key;
3154 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003155 if (type == NULL)
3156 sv->sv_type = typval2type(tv, &si->sn_type_list);
3157 else
3158 sv->sv_type = type;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003159 sv->sv_const = (flags & ASSIGN_CONST);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 sv->sv_export = is_export;
3161 ++si->sn_var_vals.ga_len;
3162
3163 // let ex_export() know the export worked.
3164 is_export = FALSE;
3165 }
3166 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003167 }
3168
3169 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171 else
3172 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 di->di_tv = *tv;
3174 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003175 init_tv(tv);
3176 }
3177
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003178 // ":const var = val" locks the value
3179 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003180 // Like :lockvar! name: lock the value and what it contains, but only
3181 // if the reference count is up to one. That locks only literal
3182 // values.
3183 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003184 return;
3185failed:
3186 if (!copy)
3187 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003188}
3189
3190/*
3191 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3192 * Also give an error message.
3193 */
3194 int
3195var_check_ro(int flags, char_u *name, int use_gettext)
3196{
3197 if (flags & DI_FLAGS_RO)
3198 {
3199 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3200 return TRUE;
3201 }
3202 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3203 {
3204 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3205 return TRUE;
3206 }
3207 return FALSE;
3208}
3209
3210/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003211 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3212 * Also give an error message.
3213 */
3214 int
3215var_check_lock(int flags, char_u *name, int use_gettext)
3216{
3217 if (flags & DI_FLAGS_LOCK)
3218 {
3219 semsg(_(e_variable_is_locked_str),
3220 use_gettext ? (char_u *)_(name) : name);
3221 return TRUE;
3222 }
3223 return FALSE;
3224}
3225
3226/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3228 * Also give an error message.
3229 */
3230 int
3231var_check_fixed(int flags, char_u *name, int use_gettext)
3232{
3233 if (flags & DI_FLAGS_FIX)
3234 {
3235 semsg(_("E795: Cannot delete variable %s"),
3236 use_gettext ? (char_u *)_(name) : name);
3237 return TRUE;
3238 }
3239 return FALSE;
3240}
3241
3242/*
3243 * Check if a funcref is assigned to a valid variable name.
3244 * Return TRUE and give an error if not.
3245 */
3246 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003247var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003248 char_u *name, // points to start of variable name
3249 int new_var) // TRUE when creating the variable
3250{
3251 // Allow for w: b: s: and t:.
3252 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3253 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3254 ? name[2] : name[0]))
3255 {
3256 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3257 name);
3258 return TRUE;
3259 }
3260 // Don't allow hiding a function. When "v" is not NULL we might be
3261 // assigning another function to the same var, the type is checked
3262 // below.
3263 if (new_var && function_exists(name, FALSE))
3264 {
3265 semsg(_("E705: Variable name conflicts with existing function: %s"),
3266 name);
3267 return TRUE;
3268 }
3269 return FALSE;
3270}
3271
3272/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003273 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3274 * value. Also give an error message, using "name" or _("name") when
3275 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003276 */
3277 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003278value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003279{
3280 if (lock & VAR_LOCKED)
3281 {
3282 semsg(_("E741: Value is locked: %s"),
3283 name == NULL ? (char_u *)_("Unknown")
3284 : use_gettext ? (char_u *)_(name)
3285 : name);
3286 return TRUE;
3287 }
3288 if (lock & VAR_FIXED)
3289 {
3290 semsg(_("E742: Cannot change value of %s"),
3291 name == NULL ? (char_u *)_("Unknown")
3292 : use_gettext ? (char_u *)_(name)
3293 : name);
3294 return TRUE;
3295 }
3296 return FALSE;
3297}
3298
3299/*
3300 * Check if a variable name is valid.
3301 * Return FALSE and give an error if not.
3302 */
3303 int
3304valid_varname(char_u *varname)
3305{
3306 char_u *p;
3307
3308 for (p = varname; *p != NUL; ++p)
3309 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3310 && *p != AUTOLOAD_CHAR)
3311 {
3312 semsg(_(e_illvar), varname);
3313 return FALSE;
3314 }
3315 return TRUE;
3316}
3317
3318/*
3319 * getwinvar() and gettabwinvar()
3320 */
3321 static void
3322getwinvar(
3323 typval_T *argvars,
3324 typval_T *rettv,
3325 int off) // 1 for gettabwinvar()
3326{
3327 win_T *win;
3328 char_u *varname;
3329 dictitem_T *v;
3330 tabpage_T *tp = NULL;
3331 int done = FALSE;
3332 win_T *oldcurwin;
3333 tabpage_T *oldtabpage;
3334 int need_switch_win;
3335
3336 if (off == 1)
3337 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3338 else
3339 tp = curtab;
3340 win = find_win_by_nr(&argvars[off], tp);
3341 varname = tv_get_string_chk(&argvars[off + 1]);
3342 ++emsg_off;
3343
3344 rettv->v_type = VAR_STRING;
3345 rettv->vval.v_string = NULL;
3346
3347 if (win != NULL && varname != NULL)
3348 {
3349 // Set curwin to be our win, temporarily. Also set the tabpage,
3350 // otherwise the window is not valid. Only do this when needed,
3351 // autocommands get blocked.
3352 need_switch_win = !(tp == curtab && win == curwin);
3353 if (!need_switch_win
3354 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3355 {
3356 if (*varname == '&')
3357 {
3358 if (varname[1] == NUL)
3359 {
3360 // get all window-local options in a dict
3361 dict_T *opts = get_winbuf_options(FALSE);
3362
3363 if (opts != NULL)
3364 {
3365 rettv_dict_set(rettv, opts);
3366 done = TRUE;
3367 }
3368 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003369 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003370 // window-local-option
3371 done = TRUE;
3372 }
3373 else
3374 {
3375 // Look up the variable.
3376 // Let getwinvar({nr}, "") return the "w:" dictionary.
3377 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3378 varname, FALSE);
3379 if (v != NULL)
3380 {
3381 copy_tv(&v->di_tv, rettv);
3382 done = TRUE;
3383 }
3384 }
3385 }
3386
3387 if (need_switch_win)
3388 // restore previous notion of curwin
3389 restore_win(oldcurwin, oldtabpage, TRUE);
3390 }
3391
3392 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3393 // use the default return value
3394 copy_tv(&argvars[off + 2], rettv);
3395
3396 --emsg_off;
3397}
3398
3399/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003400 * Set option "varname" to the value of "varp" for the current buffer/window.
3401 */
3402 static void
3403set_option_from_tv(char_u *varname, typval_T *varp)
3404{
3405 long numval = 0;
3406 char_u *strval;
3407 char_u nbuf[NUMBUFLEN];
3408 int error = FALSE;
3409
3410 if (!in_vim9script() || varp->v_type != VAR_STRING)
3411 numval = (long)tv_get_number_chk(varp, &error);
3412 strval = tv_get_string_buf_chk(varp, nbuf);
3413 if (!error && strval != NULL)
3414 set_option_value(varname, numval, strval, OPT_LOCAL);
3415}
3416
3417/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003418 * "setwinvar()" and "settabwinvar()" functions
3419 */
3420 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003421setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003422{
3423 win_T *win;
3424 win_T *save_curwin;
3425 tabpage_T *save_curtab;
3426 int need_switch_win;
3427 char_u *varname, *winvarname;
3428 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003429 tabpage_T *tp = NULL;
3430
3431 if (check_secure())
3432 return;
3433
3434 if (off == 1)
3435 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3436 else
3437 tp = curtab;
3438 win = find_win_by_nr(&argvars[off], tp);
3439 varname = tv_get_string_chk(&argvars[off + 1]);
3440 varp = &argvars[off + 2];
3441
3442 if (win != NULL && varname != NULL && varp != NULL)
3443 {
3444 need_switch_win = !(tp == curtab && win == curwin);
3445 if (!need_switch_win
3446 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3447 {
3448 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003449 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003450 else
3451 {
3452 winvarname = alloc(STRLEN(varname) + 3);
3453 if (winvarname != NULL)
3454 {
3455 STRCPY(winvarname, "w:");
3456 STRCPY(winvarname + 2, varname);
3457 set_var(winvarname, varp, TRUE);
3458 vim_free(winvarname);
3459 }
3460 }
3461 }
3462 if (need_switch_win)
3463 restore_win(save_curwin, save_curtab, TRUE);
3464 }
3465}
3466
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003467/*
3468 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3469 * v:option_type, and v:option_command.
3470 */
3471 void
3472reset_v_option_vars(void)
3473{
3474 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3475 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3476 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3477 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3478 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3479 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3480}
3481
3482/*
3483 * Add an assert error to v:errors.
3484 */
3485 void
3486assert_error(garray_T *gap)
3487{
3488 struct vimvar *vp = &vimvars[VV_ERRORS];
3489
3490 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003491 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003492 set_vim_var_list(VV_ERRORS, list_alloc());
3493 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3494}
3495
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003496 int
3497var_exists(char_u *var)
3498{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003499 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003500 char_u *name;
3501 char_u *tofree;
3502 typval_T tv;
3503 int len = 0;
3504 int n = FALSE;
3505
3506 // get_name_len() takes care of expanding curly braces
3507 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003508 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003509 if (len > 0)
3510 {
3511 if (tofree != NULL)
3512 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003513 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003514 if (n)
3515 {
3516 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003517 arg = skipwhite(arg);
3518 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003519 if (n)
3520 clear_tv(&tv);
3521 }
3522 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003523 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003524 n = FALSE;
3525
3526 vim_free(tofree);
3527 return n;
3528}
3529
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003530static lval_T *redir_lval = NULL;
3531#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3532static garray_T redir_ga; // only valid when redir_lval is not NULL
3533static char_u *redir_endp = NULL;
3534static char_u *redir_varname = NULL;
3535
3536/*
3537 * Start recording command output to a variable
3538 * When "append" is TRUE append to an existing variable.
3539 * Returns OK if successfully completed the setup. FAIL otherwise.
3540 */
3541 int
3542var_redir_start(char_u *name, int append)
3543{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003544 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003545 typval_T tv;
3546
3547 // Catch a bad name early.
3548 if (!eval_isnamec1(*name))
3549 {
3550 emsg(_(e_invarg));
3551 return FAIL;
3552 }
3553
3554 // Make a copy of the name, it is used in redir_lval until redir ends.
3555 redir_varname = vim_strsave(name);
3556 if (redir_varname == NULL)
3557 return FAIL;
3558
3559 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3560 if (redir_lval == NULL)
3561 {
3562 var_redir_stop();
3563 return FAIL;
3564 }
3565
3566 // The output is stored in growarray "redir_ga" until redirection ends.
3567 ga_init2(&redir_ga, (int)sizeof(char), 500);
3568
3569 // Parse the variable name (can be a dict or list entry).
3570 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3571 FNE_CHECK_START);
3572 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3573 {
3574 clear_lval(redir_lval);
3575 if (redir_endp != NULL && *redir_endp != NUL)
3576 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003577 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003578 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003579 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003580 redir_endp = NULL; // don't store a value, only cleanup
3581 var_redir_stop();
3582 return FAIL;
3583 }
3584
3585 // check if we can write to the variable: set it to or append an empty
3586 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003587 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003588 tv.v_type = VAR_STRING;
3589 tv.vval.v_string = (char_u *)"";
3590 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003592 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003594 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003595 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003596 {
3597 redir_endp = NULL; // don't store a value, only cleanup
3598 var_redir_stop();
3599 return FAIL;
3600 }
3601
3602 return OK;
3603}
3604
3605/*
3606 * Append "value[value_len]" to the variable set by var_redir_start().
3607 * The actual appending is postponed until redirection ends, because the value
3608 * appended may in fact be the string we write to, changing it may cause freed
3609 * memory to be used:
3610 * :redir => foo
3611 * :let foo
3612 * :redir END
3613 */
3614 void
3615var_redir_str(char_u *value, int value_len)
3616{
3617 int len;
3618
3619 if (redir_lval == NULL)
3620 return;
3621
3622 if (value_len == -1)
3623 len = (int)STRLEN(value); // Append the entire string
3624 else
3625 len = value_len; // Append only "value_len" characters
3626
3627 if (ga_grow(&redir_ga, len) == OK)
3628 {
3629 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3630 redir_ga.ga_len += len;
3631 }
3632 else
3633 var_redir_stop();
3634}
3635
3636/*
3637 * Stop redirecting command output to a variable.
3638 * Frees the allocated memory.
3639 */
3640 void
3641var_redir_stop(void)
3642{
3643 typval_T tv;
3644
3645 if (EVALCMD_BUSY)
3646 {
3647 redir_lval = NULL;
3648 return;
3649 }
3650
3651 if (redir_lval != NULL)
3652 {
3653 // If there was no error: assign the text to the variable.
3654 if (redir_endp != NULL)
3655 {
3656 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3657 tv.v_type = VAR_STRING;
3658 tv.vval.v_string = redir_ga.ga_data;
3659 // Call get_lval() again, if it's inside a Dict or List it may
3660 // have changed.
3661 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3662 FALSE, FALSE, 0, FNE_CHECK_START);
3663 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003664 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003665 (char_u *)".");
3666 clear_lval(redir_lval);
3667 }
3668
3669 // free the collected output
3670 VIM_CLEAR(redir_ga.ga_data);
3671
3672 VIM_CLEAR(redir_lval);
3673 }
3674 VIM_CLEAR(redir_varname);
3675}
3676
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003677/*
3678 * "gettabvar()" function
3679 */
3680 void
3681f_gettabvar(typval_T *argvars, typval_T *rettv)
3682{
3683 win_T *oldcurwin;
3684 tabpage_T *tp, *oldtabpage;
3685 dictitem_T *v;
3686 char_u *varname;
3687 int done = FALSE;
3688
3689 rettv->v_type = VAR_STRING;
3690 rettv->vval.v_string = NULL;
3691
3692 varname = tv_get_string_chk(&argvars[1]);
3693 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3694 if (tp != NULL && varname != NULL)
3695 {
3696 // Set tp to be our tabpage, temporarily. Also set the window to the
3697 // first window in the tabpage, otherwise the window is not valid.
3698 if (switch_win(&oldcurwin, &oldtabpage,
3699 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3700 : tp->tp_firstwin, tp, TRUE) == OK)
3701 {
3702 // look up the variable
3703 // Let gettabvar({nr}, "") return the "t:" dictionary.
3704 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3705 if (v != NULL)
3706 {
3707 copy_tv(&v->di_tv, rettv);
3708 done = TRUE;
3709 }
3710 }
3711
3712 // restore previous notion of curwin
3713 restore_win(oldcurwin, oldtabpage, TRUE);
3714 }
3715
3716 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3717 copy_tv(&argvars[2], rettv);
3718}
3719
3720/*
3721 * "gettabwinvar()" function
3722 */
3723 void
3724f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3725{
3726 getwinvar(argvars, rettv, 1);
3727}
3728
3729/*
3730 * "getwinvar()" function
3731 */
3732 void
3733f_getwinvar(typval_T *argvars, typval_T *rettv)
3734{
3735 getwinvar(argvars, rettv, 0);
3736}
3737
3738/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003739 * "getbufvar()" function
3740 */
3741 void
3742f_getbufvar(typval_T *argvars, typval_T *rettv)
3743{
3744 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003745 char_u *varname;
3746 dictitem_T *v;
3747 int done = FALSE;
3748
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003749 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003750 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003751
3752 rettv->v_type = VAR_STRING;
3753 rettv->vval.v_string = NULL;
3754
3755 if (buf != NULL && varname != NULL)
3756 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003757 if (*varname == '&')
3758 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003759 buf_T *save_curbuf = curbuf;
3760
3761 // set curbuf to be our buf, temporarily
3762 curbuf = buf;
3763
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003764 if (varname[1] == NUL)
3765 {
3766 // get all buffer-local options in a dict
3767 dict_T *opts = get_winbuf_options(TRUE);
3768
3769 if (opts != NULL)
3770 {
3771 rettv_dict_set(rettv, opts);
3772 done = TRUE;
3773 }
3774 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003775 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003776 // buffer-local-option
3777 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003778
3779 // restore previous notion of curbuf
3780 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003781 }
3782 else
3783 {
3784 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003785 if (*varname == NUL)
3786 // Let getbufvar({nr}, "") return the "b:" dictionary.
3787 v = &buf->b_bufvar;
3788 else
3789 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3790 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003791 if (v != NULL)
3792 {
3793 copy_tv(&v->di_tv, rettv);
3794 done = TRUE;
3795 }
3796 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003797 }
3798
3799 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3800 // use the default value
3801 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003802}
3803
3804/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003805 * "settabvar()" function
3806 */
3807 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003808f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003809{
3810 tabpage_T *save_curtab;
3811 tabpage_T *tp;
3812 char_u *varname, *tabvarname;
3813 typval_T *varp;
3814
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003815 if (check_secure())
3816 return;
3817
3818 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3819 varname = tv_get_string_chk(&argvars[1]);
3820 varp = &argvars[2];
3821
3822 if (varname != NULL && varp != NULL && tp != NULL)
3823 {
3824 save_curtab = curtab;
3825 goto_tabpage_tp(tp, FALSE, FALSE);
3826
3827 tabvarname = alloc(STRLEN(varname) + 3);
3828 if (tabvarname != NULL)
3829 {
3830 STRCPY(tabvarname, "t:");
3831 STRCPY(tabvarname + 2, varname);
3832 set_var(tabvarname, varp, TRUE);
3833 vim_free(tabvarname);
3834 }
3835
3836 // Restore current tabpage
3837 if (valid_tabpage(save_curtab))
3838 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3839 }
3840}
3841
3842/*
3843 * "settabwinvar()" function
3844 */
3845 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003846f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003847{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003848 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003849}
3850
3851/*
3852 * "setwinvar()" function
3853 */
3854 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003855f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003856{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003857 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003858}
3859
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003860/*
3861 * "setbufvar()" function
3862 */
3863 void
3864f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3865{
3866 buf_T *buf;
3867 char_u *varname, *bufvarname;
3868 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003869
3870 if (check_secure())
3871 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003872 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003873 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003874 varp = &argvars[2];
3875
3876 if (buf != NULL && varname != NULL && varp != NULL)
3877 {
3878 if (*varname == '&')
3879 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003880 aco_save_T aco;
3881
3882 // set curbuf to be our buf, temporarily
3883 aucmd_prepbuf(&aco, buf);
3884
Bram Moolenaar191929b2020-08-19 21:20:49 +02003885 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003886
3887 // reset notion of buffer
3888 aucmd_restbuf(&aco);
3889 }
3890 else
3891 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003892 bufvarname = alloc(STRLEN(varname) + 3);
3893 if (bufvarname != NULL)
3894 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003895 buf_T *save_curbuf = curbuf;
3896
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003897 curbuf = buf;
3898 STRCPY(bufvarname, "b:");
3899 STRCPY(bufvarname + 2, varname);
3900 set_var(bufvarname, varp, TRUE);
3901 vim_free(bufvarname);
3902 curbuf = save_curbuf;
3903 }
3904 }
3905 }
3906}
3907
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003908/*
3909 * Get a callback from "arg". It can be a Funcref or a function name.
3910 * When "arg" is zero return an empty string.
3911 * "cb_name" is not allocated.
3912 * "cb_name" is set to NULL for an invalid argument.
3913 */
3914 callback_T
3915get_callback(typval_T *arg)
3916{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003917 callback_T res;
3918 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003919
3920 res.cb_free_name = FALSE;
3921 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3922 {
3923 res.cb_partial = arg->vval.v_partial;
3924 ++res.cb_partial->pt_refcount;
3925 res.cb_name = partial_name(res.cb_partial);
3926 }
3927 else
3928 {
3929 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003930 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3931 && isdigit(*arg->vval.v_string))
3932 r = FAIL;
3933 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003934 {
3935 // Note that we don't make a copy of the string.
3936 res.cb_name = arg->vval.v_string;
3937 func_ref(res.cb_name);
3938 }
3939 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003940 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003941 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003942 r = FAIL;
3943
3944 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003945 {
3946 emsg(_("E921: Invalid callback argument"));
3947 res.cb_name = NULL;
3948 }
3949 }
3950 return res;
3951}
3952
3953/*
3954 * Copy a callback into a typval_T.
3955 */
3956 void
3957put_callback(callback_T *cb, typval_T *tv)
3958{
3959 if (cb->cb_partial != NULL)
3960 {
3961 tv->v_type = VAR_PARTIAL;
3962 tv->vval.v_partial = cb->cb_partial;
3963 ++tv->vval.v_partial->pt_refcount;
3964 }
3965 else
3966 {
3967 tv->v_type = VAR_FUNC;
3968 tv->vval.v_string = vim_strsave(cb->cb_name);
3969 func_ref(cb->cb_name);
3970 }
3971}
3972
3973/*
3974 * Make a copy of "src" into "dest", allocating the function name if needed,
3975 * without incrementing the refcount.
3976 */
3977 void
3978set_callback(callback_T *dest, callback_T *src)
3979{
3980 if (src->cb_partial == NULL)
3981 {
3982 // just a function name, make a copy
3983 dest->cb_name = vim_strsave(src->cb_name);
3984 dest->cb_free_name = TRUE;
3985 }
3986 else
3987 {
3988 // cb_name is a pointer into cb_partial
3989 dest->cb_name = src->cb_name;
3990 dest->cb_free_name = FALSE;
3991 }
3992 dest->cb_partial = src->cb_partial;
3993}
3994
3995/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003996 * Copy callback from "src" to "dest", incrementing the refcounts.
3997 */
3998 void
3999copy_callback(callback_T *dest, callback_T *src)
4000{
4001 dest->cb_partial = src->cb_partial;
4002 if (dest->cb_partial != NULL)
4003 {
4004 dest->cb_name = src->cb_name;
4005 dest->cb_free_name = FALSE;
4006 ++dest->cb_partial->pt_refcount;
4007 }
4008 else
4009 {
4010 dest->cb_name = vim_strsave(src->cb_name);
4011 dest->cb_free_name = TRUE;
4012 func_ref(src->cb_name);
4013 }
4014}
4015
4016/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004017 * Unref/free "callback" returned by get_callback() or set_callback().
4018 */
4019 void
4020free_callback(callback_T *callback)
4021{
4022 if (callback->cb_partial != NULL)
4023 {
4024 partial_unref(callback->cb_partial);
4025 callback->cb_partial = NULL;
4026 }
4027 else if (callback->cb_name != NULL)
4028 func_unref(callback->cb_name);
4029 if (callback->cb_free_name)
4030 {
4031 vim_free(callback->cb_name);
4032 callback->cb_free_name = FALSE;
4033 }
4034 callback->cb_name = NULL;
4035}
4036
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004037#endif // FEAT_EVAL