blob: e64b44ed139e1bbb2204001deca2916dfe255ae9 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200176static void item_lock(typval_T *tv, int deep, int lock);
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 Moolenaar7ebcba62020-01-12 17:42:55 +0100527 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
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 Moolenaar0522ba02019-08-27 22:48:30 +0200562
563 if (eap->getline == NULL)
564 {
565 emsg(_("E991: cannot use =<< here"));
566 return NULL;
567 }
568
569 // Check for the optional 'trim' word before the marker
570 cmd = skipwhite(cmd);
571 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
572 {
573 cmd = skipwhite(cmd + 4);
574
575 // Trim the indentation from all the lines in the here document.
576 // The amount of indentation trimmed is the same as the indentation of
577 // the first line after the :let command line. To find the end marker
578 // the indent of the :let command line is trimmed.
579 p = *eap->cmdlinep;
580 while (VIM_ISWHITE(*p))
581 {
582 p++;
583 marker_indent_len++;
584 }
585 text_indent_len = -1;
586 }
587
588 // The marker is the next word.
589 if (*cmd != NUL && *cmd != '"')
590 {
591 marker = skipwhite(cmd);
592 p = skiptowhite(marker);
593 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
594 {
595 emsg(_(e_trailing));
596 return NULL;
597 }
598 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200599 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 {
601 emsg(_("E221: Marker cannot start with lower case letter"));
602 return NULL;
603 }
604 }
605 else
606 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200607 // When getting lines for an embedded script, if the marker is missing,
608 // accept '.' as the marker.
609 if (script_get)
610 marker = dot;
611 else
612 {
613 emsg(_("E172: Missing marker"));
614 return NULL;
615 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200616 }
617
618 l = list_alloc();
619 if (l == NULL)
620 return NULL;
621
622 for (;;)
623 {
624 int mi = 0;
625 int ti = 0;
626
627 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
628 if (theline == NULL)
629 {
630 semsg(_("E990: Missing end marker '%s'"), marker);
631 break;
632 }
633
634 // with "trim": skip the indent matching the :let line to find the
635 // marker
636 if (marker_indent_len > 0
637 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
638 mi = marker_indent_len;
639 if (STRCMP(marker, theline + mi) == 0)
640 {
641 vim_free(theline);
642 break;
643 }
644
645 if (text_indent_len == -1 && *theline != NUL)
646 {
647 // set the text indent from the first line.
648 p = theline;
649 text_indent_len = 0;
650 while (VIM_ISWHITE(*p))
651 {
652 p++;
653 text_indent_len++;
654 }
655 text_indent = vim_strnsave(theline, text_indent_len);
656 }
657 // with "trim": skip the indent matching the first line
658 if (text_indent != NULL)
659 for (ti = 0; ti < text_indent_len; ++ti)
660 if (theline[ti] != text_indent[ti])
661 break;
662
663 if (list_append_string(l, theline + ti, -1) == FAIL)
664 break;
665 vim_free(theline);
666 }
667 vim_free(text_indent);
668
669 return l;
670}
671
672/*
673 * ":let" list all variable values
674 * ":let var1 var2" list variable values
675 * ":let var = expr" assignment command.
676 * ":let var += expr" assignment command.
677 * ":let var -= expr" assignment command.
678 * ":let var *= expr" assignment command.
679 * ":let var /= expr" assignment command.
680 * ":let var %= expr" assignment command.
681 * ":let var .= expr" assignment command.
682 * ":let var ..= expr" assignment command.
683 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100685 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200686 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200687 * ":const" list all variable values
688 * ":const var1 var2" list variable values
689 * ":const var = expr" assignment command.
690 * ":const [var1, var2] = expr" unpack list.
691 */
692 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200693ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200694{
695 char_u *arg = eap->arg;
696 char_u *expr = NULL;
697 typval_T rettv;
698 int i;
699 int var_count = 0;
700 int semicolon = 0;
701 char_u op[2];
702 char_u *argend;
703 int first = TRUE;
704 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200705 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200706 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 // detect Vim9 assignment without ":let" or ":const"
709 if (eap->arg == eap->cmd)
710 flags |= LET_NO_COMMAND;
711
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200712 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200713 if (argend == NULL)
714 return;
715 if (argend > arg && argend[-1] == '.') // for var.='str'
716 --argend;
717 expr = skipwhite(argend);
718 concat = expr[0] == '.'
719 && ((expr[1] == '=' && current_sctx.sc_version < 2)
720 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200721 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
722 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200723 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200724 {
725 // ":let" without "=": list variables
726 if (*arg == '[')
727 emsg(_(e_invarg));
728 else if (expr[0] == '.')
729 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200730 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200731 {
732 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
733 {
734 // Vim9 declaration ":let var: type"
735 arg = vim9_declare_scriptvar(eap, arg);
736 }
737 else
738 {
739 // ":let var1 var2" - list values
740 arg = list_arg_vars(eap, arg, &first);
741 }
742 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743 else if (!eap->skip)
744 {
745 // ":let"
746 list_glob_vars(&first);
747 list_buf_vars(&first);
748 list_win_vars(&first);
749 list_tab_vars(&first);
750 list_script_vars(&first);
751 list_func_vars(&first);
752 list_vim_vars(&first);
753 }
754 eap->nextcmd = check_nextcmd(arg);
755 }
756 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
757 {
758 list_T *l;
759
760 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200761 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 if (l != NULL)
763 {
764 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200765 if (!eap->skip)
766 {
767 op[0] = '=';
768 op[1] = NUL;
769 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200771 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200772 clear_tv(&rettv);
773 }
774 }
775 else
776 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200777 evalarg_T evalarg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200778
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 rettv.v_type = VAR_UNKNOWN;
780 i = FAIL;
781 if (has_assign || concat)
782 {
783 op[0] = '=';
784 op[1] = NUL;
785 if (*expr != '=')
786 {
787 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
788 {
789 op[0] = *expr; // +=, -=, *=, /=, %= or .=
790 if (expr[0] == '.' && expr[1] == '.') // ..=
791 ++expr;
792 }
793 expr = skipwhite(expr + 2);
794 }
795 else
796 expr = skipwhite(expr + 1);
797
798 if (eap->skip)
799 ++emsg_skip;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200800 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
801 evalarg.eval_cookie = eap->getline == getsourceline
802 ? eap->cookie : NULL;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200803 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200804 if (eap->skip)
805 --emsg_skip;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200806 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200807 if (eap->skip)
808 {
809 if (i != FAIL)
810 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200811 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200812 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813 {
814 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 clear_tv(&rettv);
817 }
818 }
819}
820
821/*
822 * Assign the typevalue "tv" to the variable or variables at "arg_start".
823 * Handles both "var" with any type and "[var, var; var]" with a list type.
824 * When "op" is not NULL it points to a string with characters that
825 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
826 * or concatenate.
827 * Returns OK or FAIL;
828 */
829 int
830ex_let_vars(
831 char_u *arg_start,
832 typval_T *tv,
833 int copy, // copy values from "tv", don't move
834 int semicolon, // from skip_var_list()
835 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200837 char_u *op)
838{
839 char_u *arg = arg_start;
840 list_T *l;
841 int i;
842 listitem_T *item;
843 typval_T ltv;
844
845 if (*arg != '[')
846 {
847 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200849 return FAIL;
850 return OK;
851 }
852
853 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
854 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
855 {
856 emsg(_(e_listreq));
857 return FAIL;
858 }
859
860 i = list_len(l);
861 if (semicolon == 0 && var_count < i)
862 {
863 emsg(_("E687: Less targets than List items"));
864 return FAIL;
865 }
866 if (var_count - semicolon > i)
867 {
868 emsg(_("E688: More targets than List items"));
869 return FAIL;
870 }
871
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200872 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200873 item = l->lv_first;
874 while (*arg != ']')
875 {
876 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200878 item = item->li_next;
879 if (arg == NULL)
880 return FAIL;
881
882 arg = skipwhite(arg);
883 if (*arg == ';')
884 {
885 // Put the rest of the list (may be empty) in the var after ';'.
886 // Create a new list for this.
887 l = list_alloc();
888 if (l == NULL)
889 return FAIL;
890 while (item != NULL)
891 {
892 list_append_tv(l, &item->li_tv);
893 item = item->li_next;
894 }
895
896 ltv.v_type = VAR_LIST;
897 ltv.v_lock = 0;
898 ltv.vval.v_list = l;
899 l->lv_refcount = 1;
900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200902 (char_u *)"]", op);
903 clear_tv(&ltv);
904 if (arg == NULL)
905 return FAIL;
906 break;
907 }
908 else if (*arg != ',' && *arg != ']')
909 {
910 internal_error("ex_let_vars()");
911 return FAIL;
912 }
913 }
914
915 return OK;
916}
917
918/*
919 * Skip over assignable variable "var" or list of variables "[var, var]".
920 * Used for ":let varvar = expr" and ":for varvar in expr".
921 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200922 * for "[var, var; var]" set "semicolon" to 1.
923 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200924 * Return NULL for an error.
925 */
926 char_u *
927skip_var_list(
928 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100929 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200930 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200931 int *semicolon,
932 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200933{
934 char_u *p, *s;
935
936 if (*arg == '[')
937 {
938 // "[var, var]": find the matching ']'.
939 p = arg;
940 for (;;)
941 {
942 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200943 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200944 if (s == p)
945 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200946 if (!silent)
947 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200948 return NULL;
949 }
950 ++*var_count;
951
952 p = skipwhite(s);
953 if (*p == ']')
954 break;
955 else if (*p == ';')
956 {
957 if (*semicolon == 1)
958 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200959 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200960 return NULL;
961 }
962 *semicolon = 1;
963 }
964 else if (*p != ',')
965 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200966 if (!silent)
967 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 return NULL;
969 }
970 }
971 return p + 1;
972 }
973 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975}
976
977/*
978 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
979 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200982 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 char_u *end;
986
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987 if (*arg == '@' && arg[1] != NUL)
988 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200990 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200991 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200993 // "a: type" is declaring variable "a" with a type, not "a:".
994 if (end == arg + 2 && end[-1] == ':')
995 --end;
996 if (*end == ':')
997 end = skip_type(skipwhite(end + 1));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 }
999 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001000}
1001
1002/*
1003 * List variables for hashtab "ht" with prefix "prefix".
1004 * If "empty" is TRUE also list NULL strings as empty strings.
1005 */
1006 void
1007list_hashtable_vars(
1008 hashtab_T *ht,
1009 char *prefix,
1010 int empty,
1011 int *first)
1012{
1013 hashitem_T *hi;
1014 dictitem_T *di;
1015 int todo;
1016 char_u buf[IOSIZE];
1017
1018 todo = (int)ht->ht_used;
1019 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1020 {
1021 if (!HASHITEM_EMPTY(hi))
1022 {
1023 --todo;
1024 di = HI2DI(hi);
1025
1026 // apply :filter /pat/ to variable name
1027 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1028 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1029 if (message_filtered(buf))
1030 continue;
1031
1032 if (empty || di->di_tv.v_type != VAR_STRING
1033 || di->di_tv.vval.v_string != NULL)
1034 list_one_var(di, prefix, first);
1035 }
1036 }
1037}
1038
1039/*
1040 * List global variables.
1041 */
1042 static void
1043list_glob_vars(int *first)
1044{
1045 list_hashtable_vars(&globvarht, "", TRUE, first);
1046}
1047
1048/*
1049 * List buffer variables.
1050 */
1051 static void
1052list_buf_vars(int *first)
1053{
1054 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1055}
1056
1057/*
1058 * List window variables.
1059 */
1060 static void
1061list_win_vars(int *first)
1062{
1063 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1064}
1065
1066/*
1067 * List tab page variables.
1068 */
1069 static void
1070list_tab_vars(int *first)
1071{
1072 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1073}
1074
1075/*
1076 * List variables in "arg".
1077 */
1078 static char_u *
1079list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1080{
1081 int error = FALSE;
1082 int len;
1083 char_u *name;
1084 char_u *name_start;
1085 char_u *arg_subsc;
1086 char_u *tofree;
1087 typval_T tv;
1088
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001089 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001090 {
1091 if (error || eap->skip)
1092 {
1093 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1094 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1095 {
1096 emsg_severe = TRUE;
1097 emsg(_(e_trailing));
1098 break;
1099 }
1100 }
1101 else
1102 {
1103 // get_name_len() takes care of expanding curly braces
1104 name_start = name = arg;
1105 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1106 if (len <= 0)
1107 {
1108 // This is mainly to keep test 49 working: when expanding
1109 // curly braces fails overrule the exception error message.
1110 if (len < 0 && !aborting())
1111 {
1112 emsg_severe = TRUE;
1113 semsg(_(e_invarg2), arg);
1114 break;
1115 }
1116 error = TRUE;
1117 }
1118 else
1119 {
1120 if (tofree != NULL)
1121 name = tofree;
1122 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1123 error = TRUE;
1124 else
1125 {
1126 // handle d.key, l[idx], f(expr)
1127 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001128 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001129 name, &name) == FAIL)
1130 error = TRUE;
1131 else
1132 {
1133 if (arg == arg_subsc && len == 2 && name[1] == ':')
1134 {
1135 switch (*name)
1136 {
1137 case 'g': list_glob_vars(first); break;
1138 case 'b': list_buf_vars(first); break;
1139 case 'w': list_win_vars(first); break;
1140 case 't': list_tab_vars(first); break;
1141 case 'v': list_vim_vars(first); break;
1142 case 's': list_script_vars(first); break;
1143 case 'l': list_func_vars(first); break;
1144 default:
1145 semsg(_("E738: Can't list variables for %s"), name);
1146 }
1147 }
1148 else
1149 {
1150 char_u numbuf[NUMBUFLEN];
1151 char_u *tf;
1152 int c;
1153 char_u *s;
1154
1155 s = echo_string(&tv, &tf, numbuf, 0);
1156 c = *arg;
1157 *arg = NUL;
1158 list_one_var_a("",
1159 arg == arg_subsc ? name : name_start,
1160 tv.v_type,
1161 s == NULL ? (char_u *)"" : s,
1162 first);
1163 *arg = c;
1164 vim_free(tf);
1165 }
1166 clear_tv(&tv);
1167 }
1168 }
1169 }
1170
1171 vim_free(tofree);
1172 }
1173
1174 arg = skipwhite(arg);
1175 }
1176
1177 return arg;
1178}
1179
1180/*
1181 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1182 * Returns a pointer to the char just after the var name.
1183 * Returns NULL if there is an error.
1184 */
1185 static char_u *
1186ex_let_one(
1187 char_u *arg, // points to variable name
1188 typval_T *tv, // value to assign to variable
1189 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001191 char_u *endchars, // valid chars after variable name or NULL
1192 char_u *op) // "+", "-", "." or NULL
1193{
1194 int c1;
1195 char_u *name;
1196 char_u *p;
1197 char_u *arg_end = NULL;
1198 int len;
1199 int opt_flags;
1200 char_u *tofree = NULL;
1201
1202 // ":let $VAR = expr": Set environment variable.
1203 if (*arg == '$')
1204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001206 {
1207 emsg(_("E996: Cannot lock an environment variable"));
1208 return NULL;
1209 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001210 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1211 && (flags & LET_NO_COMMAND) == 0)
1212 {
1213 vim9_declare_error(arg);
1214 return NULL;
1215 }
1216
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001217 // Find the end of the name.
1218 ++arg;
1219 name = arg;
1220 len = get_env_len(&arg);
1221 if (len == 0)
1222 semsg(_(e_invarg2), name - 1);
1223 else
1224 {
1225 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1226 semsg(_(e_letwrong), op);
1227 else if (endchars != NULL
1228 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1229 emsg(_(e_letunexp));
1230 else if (!check_secure())
1231 {
1232 c1 = name[len];
1233 name[len] = NUL;
1234 p = tv_get_string_chk(tv);
1235 if (p != NULL && op != NULL && *op == '.')
1236 {
1237 int mustfree = FALSE;
1238 char_u *s = vim_getenv(name, &mustfree);
1239
1240 if (s != NULL)
1241 {
1242 p = tofree = concat_str(s, p);
1243 if (mustfree)
1244 vim_free(s);
1245 }
1246 }
1247 if (p != NULL)
1248 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001249 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001250 arg_end = arg;
1251 }
1252 name[len] = c1;
1253 vim_free(tofree);
1254 }
1255 }
1256 }
1257
1258 // ":let &option = expr": Set option value.
1259 // ":let &l:option = expr": Set local option value.
1260 // ":let &g:option = expr": Set global option value.
1261 else if (*arg == '&')
1262 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001264 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266 return NULL;
1267 }
1268 // Find the end of the name.
1269 p = find_option_end(&arg, &opt_flags);
1270 if (p == NULL || (endchars != NULL
1271 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1272 emsg(_(e_letunexp));
1273 else
1274 {
1275 long n;
1276 int opt_type;
1277 long numval;
1278 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001279 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001280
1281 c1 = *p;
1282 *p = NUL;
1283
1284 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001285 // avoid setting a string option to the text "v:false" or similar.
1286 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1287 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 if (s != NULL && op != NULL && *op != '=')
1289 {
1290 opt_type = get_option_value(arg, &numval,
1291 &stringval, opt_flags);
1292 if ((opt_type == 1 && *op == '.')
1293 || (opt_type == 0 && *op != '.'))
1294 {
1295 semsg(_(e_letwrong), op);
1296 s = NULL; // don't set the value
1297 }
1298 else
1299 {
1300 if (opt_type == 1) // number
1301 {
1302 switch (*op)
1303 {
1304 case '+': n = numval + n; break;
1305 case '-': n = numval - n; break;
1306 case '*': n = numval * n; break;
1307 case '/': n = (long)num_divide(numval, n); break;
1308 case '%': n = (long)num_modulus(numval, n); break;
1309 }
1310 }
1311 else if (opt_type == 0 && stringval != NULL) // string
1312 {
1313 s = concat_str(stringval, s);
1314 vim_free(stringval);
1315 stringval = s;
1316 }
1317 }
1318 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001319 if (s != NULL || tv->v_type == VAR_BOOL
1320 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001321 {
1322 set_option_value(arg, n, s, opt_flags);
1323 arg_end = p;
1324 }
1325 *p = c1;
1326 vim_free(stringval);
1327 }
1328 }
1329
1330 // ":let @r = expr": Set register contents.
1331 else if (*arg == '@')
1332 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001334 {
1335 emsg(_("E996: Cannot lock a register"));
1336 return NULL;
1337 }
1338 ++arg;
1339 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1340 semsg(_(e_letwrong), op);
1341 else if (endchars != NULL
1342 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1343 emsg(_(e_letunexp));
1344 else
1345 {
1346 char_u *ptofree = NULL;
1347 char_u *s;
1348
1349 p = tv_get_string_chk(tv);
1350 if (p != NULL && op != NULL && *op == '.')
1351 {
1352 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1353 if (s != NULL)
1354 {
1355 p = ptofree = concat_str(s, p);
1356 vim_free(s);
1357 }
1358 }
1359 if (p != NULL)
1360 {
1361 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1362 arg_end = arg + 1;
1363 }
1364 vim_free(ptofree);
1365 }
1366 }
1367
1368 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001370 // ":let {expr} = expr": Idem, name made with curly braces
1371 else if (eval_isnamec1(*arg) || *arg == '{')
1372 {
1373 lval_T lv;
1374
1375 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001376 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 if (endchars != NULL && vim_strchr(endchars,
1379 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001380 emsg(_(e_letunexp));
1381 else
1382 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001384 arg_end = p;
1385 }
1386 }
1387 clear_lval(&lv);
1388 }
1389
1390 else
1391 semsg(_(e_invarg2), arg);
1392
1393 return arg_end;
1394}
1395
1396/*
1397 * ":unlet[!] var1 ... " command.
1398 */
1399 void
1400ex_unlet(exarg_T *eap)
1401{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001402 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403}
1404
1405/*
1406 * ":lockvar" and ":unlockvar" commands
1407 */
1408 void
1409ex_lockvar(exarg_T *eap)
1410{
1411 char_u *arg = eap->arg;
1412 int deep = 2;
1413
1414 if (eap->forceit)
1415 deep = -1;
1416 else if (vim_isdigit(*arg))
1417 {
1418 deep = getdigits(&arg);
1419 arg = skipwhite(arg);
1420 }
1421
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001422 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001423}
1424
1425/*
1426 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001427 * Also used for Vim9 script. "callback" is invoked as:
1428 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001429 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001430 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001431ex_unletlock(
1432 exarg_T *eap,
1433 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001434 int deep,
1435 int glv_flags,
1436 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1437 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001438{
1439 char_u *arg = argstart;
1440 char_u *name_end;
1441 int error = FALSE;
1442 lval_T lv;
1443
1444 do
1445 {
1446 if (*arg == '$')
1447 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001448 lv.ll_name = arg;
1449 lv.ll_tv = NULL;
1450 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451 if (get_env_len(&arg) == 0)
1452 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001453 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001454 return;
1455 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001456 if (!error && !eap->skip
1457 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1458 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001459 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001460 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001461 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001463 // Parse the name and find the end.
1464 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1465 glv_flags, FNE_CHECK_START);
1466 if (lv.ll_name == NULL)
1467 error = TRUE; // error but continue parsing
1468 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1469 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001471 if (name_end != NULL)
1472 {
1473 emsg_severe = TRUE;
1474 emsg(_(e_trailing));
1475 }
1476 if (!(eap->skip || error))
1477 clear_lval(&lv);
1478 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001479 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001480
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001481 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001482 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001483 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001484
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001485 if (!eap->skip)
1486 clear_lval(&lv);
1487 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488
1489 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001490 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001491
1492 eap->nextcmd = check_nextcmd(arg);
1493}
1494
1495 static int
1496do_unlet_var(
1497 lval_T *lp,
1498 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001499 exarg_T *eap,
1500 int deep UNUSED,
1501 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001502{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001503 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504 int ret = OK;
1505 int cc;
1506
1507 if (lp->ll_tv == NULL)
1508 {
1509 cc = *name_end;
1510 *name_end = NUL;
1511
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001512 // Environment variable, normal name or expanded name.
1513 if (*lp->ll_name == '$')
1514 vim_unsetenv(lp->ll_name + 1);
1515 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001516 ret = FAIL;
1517 *name_end = cc;
1518 }
1519 else if ((lp->ll_list != NULL
1520 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1521 || (lp->ll_dict != NULL
1522 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1523 return FAIL;
1524 else if (lp->ll_range)
1525 {
1526 listitem_T *li;
1527 listitem_T *ll_li = lp->ll_li;
1528 int ll_n1 = lp->ll_n1;
1529
1530 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1531 {
1532 li = ll_li->li_next;
1533 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1534 return FAIL;
1535 ll_li = li;
1536 ++ll_n1;
1537 }
1538
1539 // Delete a range of List items.
1540 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1541 {
1542 li = lp->ll_li->li_next;
1543 listitem_remove(lp->ll_list, lp->ll_li);
1544 lp->ll_li = li;
1545 ++lp->ll_n1;
1546 }
1547 }
1548 else
1549 {
1550 if (lp->ll_list != NULL)
1551 // unlet a List item.
1552 listitem_remove(lp->ll_list, lp->ll_li);
1553 else
1554 // unlet a Dictionary item.
1555 dictitem_remove(lp->ll_dict, lp->ll_di);
1556 }
1557
1558 return ret;
1559}
1560
1561/*
1562 * "unlet" a variable. Return OK if it existed, FAIL if not.
1563 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1564 */
1565 int
1566do_unlet(char_u *name, int forceit)
1567{
1568 hashtab_T *ht;
1569 hashitem_T *hi;
1570 char_u *varname;
1571 dict_T *d;
1572 dictitem_T *di;
1573
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001574 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1575 && check_vim9_unlet(name) == FAIL)
1576 return FAIL;
1577
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578 ht = find_var_ht(name, &varname);
1579 if (ht != NULL && *varname != NUL)
1580 {
1581 d = get_current_funccal_dict(ht);
1582 if (d == NULL)
1583 {
1584 if (ht == &globvarht)
1585 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001586 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587 d = &vimvardict;
1588 else
1589 {
1590 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1591 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1592 }
1593 if (d == NULL)
1594 {
1595 internal_error("do_unlet()");
1596 return FAIL;
1597 }
1598 }
1599 hi = hash_find(ht, varname);
1600 if (HASHITEM_EMPTY(hi))
1601 hi = find_hi_in_scoped_ht(name, &ht);
1602 if (hi != NULL && !HASHITEM_EMPTY(hi))
1603 {
1604 di = HI2DI(hi);
1605 if (var_check_fixed(di->di_flags, name, FALSE)
1606 || var_check_ro(di->di_flags, name, FALSE)
1607 || var_check_lock(d->dv_lock, name, FALSE))
1608 return FAIL;
1609
1610 delete_var(ht, hi);
1611 return OK;
1612 }
1613 }
1614 if (forceit)
1615 return OK;
1616 semsg(_("E108: No such variable: \"%s\""), name);
1617 return FAIL;
1618}
1619
1620/*
1621 * Lock or unlock variable indicated by "lp".
1622 * "deep" is the levels to go (-1 for unlimited);
1623 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1624 */
1625 static int
1626do_lock_var(
1627 lval_T *lp,
1628 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001629 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001630 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001631 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001632{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001633 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001634 int ret = OK;
1635 int cc;
1636 dictitem_T *di;
1637
1638 if (deep == 0) // nothing to do
1639 return OK;
1640
1641 if (lp->ll_tv == NULL)
1642 {
1643 cc = *name_end;
1644 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001645 if (*lp->ll_name == '$')
1646 {
1647 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001648 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001649 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650 else
1651 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001652 // Normal name or expanded name.
1653 di = find_var(lp->ll_name, NULL, TRUE);
1654 if (di == NULL)
1655 ret = FAIL;
1656 else if ((di->di_flags & DI_FLAGS_FIX)
1657 && di->di_tv.v_type != VAR_DICT
1658 && di->di_tv.v_type != VAR_LIST)
1659 {
1660 // For historic reasons this error is not given for a list or
1661 // dict. E.g., the b: dict could be locked/unlocked.
1662 semsg(_(e_lock_unlock), lp->ll_name);
1663 ret = FAIL;
1664 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001665 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001666 {
1667 if (lock)
1668 di->di_flags |= DI_FLAGS_LOCK;
1669 else
1670 di->di_flags &= ~DI_FLAGS_LOCK;
1671 item_lock(&di->di_tv, deep, lock);
1672 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001673 }
1674 *name_end = cc;
1675 }
1676 else if (lp->ll_range)
1677 {
1678 listitem_T *li = lp->ll_li;
1679
1680 // (un)lock a range of List items.
1681 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1682 {
1683 item_lock(&li->li_tv, deep, lock);
1684 li = li->li_next;
1685 ++lp->ll_n1;
1686 }
1687 }
1688 else if (lp->ll_list != NULL)
1689 // (un)lock a List item.
1690 item_lock(&lp->ll_li->li_tv, deep, lock);
1691 else
1692 // (un)lock a Dictionary item.
1693 item_lock(&lp->ll_di->di_tv, deep, lock);
1694
1695 return ret;
1696}
1697
1698/*
1699 * Lock or unlock an item. "deep" is nr of levels to go.
1700 */
1701 static void
1702item_lock(typval_T *tv, int deep, int lock)
1703{
1704 static int recurse = 0;
1705 list_T *l;
1706 listitem_T *li;
1707 dict_T *d;
1708 blob_T *b;
1709 hashitem_T *hi;
1710 int todo;
1711
1712 if (recurse >= DICT_MAXNEST)
1713 {
1714 emsg(_("E743: variable nested too deep for (un)lock"));
1715 return;
1716 }
1717 if (deep == 0)
1718 return;
1719 ++recurse;
1720
1721 // lock/unlock the item itself
1722 if (lock)
1723 tv->v_lock |= VAR_LOCKED;
1724 else
1725 tv->v_lock &= ~VAR_LOCKED;
1726
1727 switch (tv->v_type)
1728 {
1729 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001730 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001732 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 case VAR_STRING:
1735 case VAR_FUNC:
1736 case VAR_PARTIAL:
1737 case VAR_FLOAT:
1738 case VAR_SPECIAL:
1739 case VAR_JOB:
1740 case VAR_CHANNEL:
1741 break;
1742
1743 case VAR_BLOB:
1744 if ((b = tv->vval.v_blob) != NULL)
1745 {
1746 if (lock)
1747 b->bv_lock |= VAR_LOCKED;
1748 else
1749 b->bv_lock &= ~VAR_LOCKED;
1750 }
1751 break;
1752 case VAR_LIST:
1753 if ((l = tv->vval.v_list) != NULL)
1754 {
1755 if (lock)
1756 l->lv_lock |= VAR_LOCKED;
1757 else
1758 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001759 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001761 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001762 item_lock(&li->li_tv, deep - 1, lock);
1763 }
1764 break;
1765 case VAR_DICT:
1766 if ((d = tv->vval.v_dict) != NULL)
1767 {
1768 if (lock)
1769 d->dv_lock |= VAR_LOCKED;
1770 else
1771 d->dv_lock &= ~VAR_LOCKED;
1772 if (deep < 0 || deep > 1)
1773 {
1774 // recursive: lock/unlock the items the List contains
1775 todo = (int)d->dv_hashtab.ht_used;
1776 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1777 {
1778 if (!HASHITEM_EMPTY(hi))
1779 {
1780 --todo;
1781 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1782 }
1783 }
1784 }
1785 }
1786 }
1787 --recurse;
1788}
1789
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001790#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1791/*
1792 * Delete all "menutrans_" variables.
1793 */
1794 void
1795del_menutrans_vars(void)
1796{
1797 hashitem_T *hi;
1798 int todo;
1799
1800 hash_lock(&globvarht);
1801 todo = (int)globvarht.ht_used;
1802 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1803 {
1804 if (!HASHITEM_EMPTY(hi))
1805 {
1806 --todo;
1807 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1808 delete_var(&globvarht, hi);
1809 }
1810 }
1811 hash_unlock(&globvarht);
1812}
1813#endif
1814
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001815/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001816 * Local string buffer for the next two functions to store a variable name
1817 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1818 * get_user_var_name().
1819 */
1820
1821static char_u *varnamebuf = NULL;
1822static int varnamebuflen = 0;
1823
1824/*
1825 * Function to concatenate a prefix and a variable name.
1826 */
1827 static char_u *
1828cat_prefix_varname(int prefix, char_u *name)
1829{
1830 int len;
1831
1832 len = (int)STRLEN(name) + 3;
1833 if (len > varnamebuflen)
1834 {
1835 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001836 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001837 varnamebuf = alloc(len);
1838 if (varnamebuf == NULL)
1839 {
1840 varnamebuflen = 0;
1841 return NULL;
1842 }
1843 varnamebuflen = len;
1844 }
1845 *varnamebuf = prefix;
1846 varnamebuf[1] = ':';
1847 STRCPY(varnamebuf + 2, name);
1848 return varnamebuf;
1849}
1850
1851/*
1852 * Function given to ExpandGeneric() to obtain the list of user defined
1853 * (global/buffer/window/built-in) variable names.
1854 */
1855 char_u *
1856get_user_var_name(expand_T *xp, int idx)
1857{
1858 static long_u gdone;
1859 static long_u bdone;
1860 static long_u wdone;
1861 static long_u tdone;
1862 static int vidx;
1863 static hashitem_T *hi;
1864 hashtab_T *ht;
1865
1866 if (idx == 0)
1867 {
1868 gdone = bdone = wdone = vidx = 0;
1869 tdone = 0;
1870 }
1871
1872 // Global variables
1873 if (gdone < globvarht.ht_used)
1874 {
1875 if (gdone++ == 0)
1876 hi = globvarht.ht_array;
1877 else
1878 ++hi;
1879 while (HASHITEM_EMPTY(hi))
1880 ++hi;
1881 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1882 return cat_prefix_varname('g', hi->hi_key);
1883 return hi->hi_key;
1884 }
1885
1886 // b: variables
1887 ht = &curbuf->b_vars->dv_hashtab;
1888 if (bdone < ht->ht_used)
1889 {
1890 if (bdone++ == 0)
1891 hi = ht->ht_array;
1892 else
1893 ++hi;
1894 while (HASHITEM_EMPTY(hi))
1895 ++hi;
1896 return cat_prefix_varname('b', hi->hi_key);
1897 }
1898
1899 // w: variables
1900 ht = &curwin->w_vars->dv_hashtab;
1901 if (wdone < ht->ht_used)
1902 {
1903 if (wdone++ == 0)
1904 hi = ht->ht_array;
1905 else
1906 ++hi;
1907 while (HASHITEM_EMPTY(hi))
1908 ++hi;
1909 return cat_prefix_varname('w', hi->hi_key);
1910 }
1911
1912 // t: variables
1913 ht = &curtab->tp_vars->dv_hashtab;
1914 if (tdone < ht->ht_used)
1915 {
1916 if (tdone++ == 0)
1917 hi = ht->ht_array;
1918 else
1919 ++hi;
1920 while (HASHITEM_EMPTY(hi))
1921 ++hi;
1922 return cat_prefix_varname('t', hi->hi_key);
1923 }
1924
1925 // v: variables
1926 if (vidx < VV_LEN)
1927 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1928
1929 VIM_CLEAR(varnamebuf);
1930 varnamebuflen = 0;
1931 return NULL;
1932}
1933
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001934 char *
1935get_var_special_name(int nr)
1936{
1937 switch (nr)
1938 {
1939 case VVAL_FALSE: return "v:false";
1940 case VVAL_TRUE: return "v:true";
1941 case VVAL_NONE: return "v:none";
1942 case VVAL_NULL: return "v:null";
1943 }
1944 internal_error("get_var_special_name()");
1945 return "42";
1946}
1947
1948/*
1949 * Returns the global variable dictionary
1950 */
1951 dict_T *
1952get_globvar_dict(void)
1953{
1954 return &globvardict;
1955}
1956
1957/*
1958 * Returns the global variable hash table
1959 */
1960 hashtab_T *
1961get_globvar_ht(void)
1962{
1963 return &globvarht;
1964}
1965
1966/*
1967 * Returns the v: variable dictionary
1968 */
1969 dict_T *
1970get_vimvar_dict(void)
1971{
1972 return &vimvardict;
1973}
1974
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001975/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001977 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 */
1979 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001980find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001982 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1983 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984
1985 if (di == NULL)
1986 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001987 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1989 return (int)(vv - vimvars);
1990}
1991
1992
1993/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001994 * Set type of v: variable to "type".
1995 */
1996 void
1997set_vim_var_type(int idx, vartype_T type)
1998{
1999 vimvars[idx].vv_type = type;
2000}
2001
2002/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002003 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002004 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002005 */
2006 void
2007set_vim_var_nr(int idx, varnumber_T val)
2008{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002009 vimvars[idx].vv_nr = val;
2010}
2011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 char *
2013get_vim_var_name(int idx)
2014{
2015 return vimvars[idx].vv_name;
2016}
2017
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002018/*
2019 * Get typval_T v: variable value.
2020 */
2021 typval_T *
2022get_vim_var_tv(int idx)
2023{
2024 return &vimvars[idx].vv_tv;
2025}
2026
2027/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002028 * Set v: variable to "tv". Only accepts the same type.
2029 * Takes over the value of "tv".
2030 */
2031 int
2032set_vim_var_tv(int idx, typval_T *tv)
2033{
2034 if (vimvars[idx].vv_type != tv->v_type)
2035 {
2036 emsg(_("E1063: type mismatch for v: variable"));
2037 clear_tv(tv);
2038 return FAIL;
2039 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002040 // VV_RO is also checked when compiling, but let's check here as well.
2041 if (vimvars[idx].vv_flags & VV_RO)
2042 {
2043 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2044 return FAIL;
2045 }
2046 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2047 {
2048 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2049 return FAIL;
2050 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002051 clear_tv(&vimvars[idx].vv_di.di_tv);
2052 vimvars[idx].vv_di.di_tv = *tv;
2053 return OK;
2054}
2055
2056/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002057 * Get number v: variable value.
2058 */
2059 varnumber_T
2060get_vim_var_nr(int idx)
2061{
2062 return vimvars[idx].vv_nr;
2063}
2064
2065/*
2066 * Get string v: variable value. Uses a static buffer, can only be used once.
2067 * If the String variable has never been set, return an empty string.
2068 * Never returns NULL;
2069 */
2070 char_u *
2071get_vim_var_str(int idx)
2072{
2073 return tv_get_string(&vimvars[idx].vv_tv);
2074}
2075
2076/*
2077 * Get List v: variable value. Caller must take care of reference count when
2078 * needed.
2079 */
2080 list_T *
2081get_vim_var_list(int idx)
2082{
2083 return vimvars[idx].vv_list;
2084}
2085
2086/*
2087 * Get Dict v: variable value. Caller must take care of reference count when
2088 * needed.
2089 */
2090 dict_T *
2091get_vim_var_dict(int idx)
2092{
2093 return vimvars[idx].vv_dict;
2094}
2095
2096/*
2097 * Set v:char to character "c".
2098 */
2099 void
2100set_vim_var_char(int c)
2101{
2102 char_u buf[MB_MAXBYTES + 1];
2103
2104 if (has_mbyte)
2105 buf[(*mb_char2bytes)(c, buf)] = NUL;
2106 else
2107 {
2108 buf[0] = c;
2109 buf[1] = NUL;
2110 }
2111 set_vim_var_string(VV_CHAR, buf, -1);
2112}
2113
2114/*
2115 * Set v:count to "count" and v:count1 to "count1".
2116 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2117 */
2118 void
2119set_vcount(
2120 long count,
2121 long count1,
2122 int set_prevcount)
2123{
2124 if (set_prevcount)
2125 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2126 vimvars[VV_COUNT].vv_nr = count;
2127 vimvars[VV_COUNT1].vv_nr = count1;
2128}
2129
2130/*
2131 * Save variables that might be changed as a side effect. Used when executing
2132 * a timer callback.
2133 */
2134 void
2135save_vimvars(vimvars_save_T *vvsave)
2136{
2137 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2138 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2139 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2140}
2141
2142/*
2143 * Restore variables saved by save_vimvars().
2144 */
2145 void
2146restore_vimvars(vimvars_save_T *vvsave)
2147{
2148 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2149 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2150 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2151}
2152
2153/*
2154 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2155 * value.
2156 */
2157 void
2158set_vim_var_string(
2159 int idx,
2160 char_u *val,
2161 int len) // length of "val" to use or -1 (whole string)
2162{
2163 clear_tv(&vimvars[idx].vv_di.di_tv);
2164 vimvars[idx].vv_type = VAR_STRING;
2165 if (val == NULL)
2166 vimvars[idx].vv_str = NULL;
2167 else if (len == -1)
2168 vimvars[idx].vv_str = vim_strsave(val);
2169 else
2170 vimvars[idx].vv_str = vim_strnsave(val, len);
2171}
2172
2173/*
2174 * Set List v: variable to "val".
2175 */
2176 void
2177set_vim_var_list(int idx, list_T *val)
2178{
2179 clear_tv(&vimvars[idx].vv_di.di_tv);
2180 vimvars[idx].vv_type = VAR_LIST;
2181 vimvars[idx].vv_list = val;
2182 if (val != NULL)
2183 ++val->lv_refcount;
2184}
2185
2186/*
2187 * Set Dictionary v: variable to "val".
2188 */
2189 void
2190set_vim_var_dict(int idx, dict_T *val)
2191{
2192 clear_tv(&vimvars[idx].vv_di.di_tv);
2193 vimvars[idx].vv_type = VAR_DICT;
2194 vimvars[idx].vv_dict = val;
2195 if (val != NULL)
2196 {
2197 ++val->dv_refcount;
2198 dict_set_items_ro(val);
2199 }
2200}
2201
2202/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002203 * Set the v:argv list.
2204 */
2205 void
2206set_argv_var(char **argv, int argc)
2207{
2208 list_T *l = list_alloc();
2209 int i;
2210
2211 if (l == NULL)
2212 getout(1);
2213 l->lv_lock = VAR_FIXED;
2214 for (i = 0; i < argc; ++i)
2215 {
2216 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2217 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002218 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002219 }
2220 set_vim_var_list(VV_ARGV, l);
2221}
2222
2223/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002224 * Reset v:register, taking the 'clipboard' setting into account.
2225 */
2226 void
2227reset_reg_var(void)
2228{
2229 int regname = 0;
2230
2231 // Adjust the register according to 'clipboard', so that when
2232 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2233#ifdef FEAT_CLIPBOARD
2234 adjust_clip_reg(&regname);
2235#endif
2236 set_reg_var(regname);
2237}
2238
2239/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002240 * Set v:register if needed.
2241 */
2242 void
2243set_reg_var(int c)
2244{
2245 char_u regname;
2246
2247 if (c == 0 || c == ' ')
2248 regname = '"';
2249 else
2250 regname = c;
2251 // Avoid free/alloc when the value is already right.
2252 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2253 set_vim_var_string(VV_REG, &regname, 1);
2254}
2255
2256/*
2257 * Get or set v:exception. If "oldval" == NULL, return the current value.
2258 * Otherwise, restore the value to "oldval" and return NULL.
2259 * Must always be called in pairs to save and restore v:exception! Does not
2260 * take care of memory allocations.
2261 */
2262 char_u *
2263v_exception(char_u *oldval)
2264{
2265 if (oldval == NULL)
2266 return vimvars[VV_EXCEPTION].vv_str;
2267
2268 vimvars[VV_EXCEPTION].vv_str = oldval;
2269 return NULL;
2270}
2271
2272/*
2273 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2274 * Otherwise, restore the value to "oldval" and return NULL.
2275 * Must always be called in pairs to save and restore v:throwpoint! Does not
2276 * take care of memory allocations.
2277 */
2278 char_u *
2279v_throwpoint(char_u *oldval)
2280{
2281 if (oldval == NULL)
2282 return vimvars[VV_THROWPOINT].vv_str;
2283
2284 vimvars[VV_THROWPOINT].vv_str = oldval;
2285 return NULL;
2286}
2287
2288/*
2289 * Set v:cmdarg.
2290 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2291 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2292 * Must always be called in pairs!
2293 */
2294 char_u *
2295set_cmdarg(exarg_T *eap, char_u *oldarg)
2296{
2297 char_u *oldval;
2298 char_u *newval;
2299 unsigned len;
2300
2301 oldval = vimvars[VV_CMDARG].vv_str;
2302 if (eap == NULL)
2303 {
2304 vim_free(oldval);
2305 vimvars[VV_CMDARG].vv_str = oldarg;
2306 return NULL;
2307 }
2308
2309 if (eap->force_bin == FORCE_BIN)
2310 len = 6;
2311 else if (eap->force_bin == FORCE_NOBIN)
2312 len = 8;
2313 else
2314 len = 0;
2315
2316 if (eap->read_edit)
2317 len += 7;
2318
2319 if (eap->force_ff != 0)
2320 len += 10; // " ++ff=unix"
2321 if (eap->force_enc != 0)
2322 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2323 if (eap->bad_char != 0)
2324 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2325
2326 newval = alloc(len + 1);
2327 if (newval == NULL)
2328 return NULL;
2329
2330 if (eap->force_bin == FORCE_BIN)
2331 sprintf((char *)newval, " ++bin");
2332 else if (eap->force_bin == FORCE_NOBIN)
2333 sprintf((char *)newval, " ++nobin");
2334 else
2335 *newval = NUL;
2336
2337 if (eap->read_edit)
2338 STRCAT(newval, " ++edit");
2339
2340 if (eap->force_ff != 0)
2341 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2342 eap->force_ff == 'u' ? "unix"
2343 : eap->force_ff == 'd' ? "dos"
2344 : "mac");
2345 if (eap->force_enc != 0)
2346 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2347 eap->cmd + eap->force_enc);
2348 if (eap->bad_char == BAD_KEEP)
2349 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2350 else if (eap->bad_char == BAD_DROP)
2351 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2352 else if (eap->bad_char != 0)
2353 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2354 vimvars[VV_CMDARG].vv_str = newval;
2355 return oldval;
2356}
2357
2358/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002359 * Get the value of internal variable "name".
2360 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2361 */
2362 int
2363get_var_tv(
2364 char_u *name,
2365 int len, // length of "name"
2366 typval_T *rettv, // NULL when only checking existence
2367 dictitem_T **dip, // non-NULL when typval's dict item is needed
2368 int verbose, // may give error message
2369 int no_autoload) // do not use script autoloading
2370{
2371 int ret = OK;
2372 typval_T *tv = NULL;
2373 dictitem_T *v;
2374 int cc;
2375
2376 // truncate the name, so that we can use strcmp()
2377 cc = name[len];
2378 name[len] = NUL;
2379
2380 // Check for user-defined variables.
2381 v = find_var(name, NULL, no_autoload);
2382 if (v != NULL)
2383 {
2384 tv = &v->di_tv;
2385 if (dip != NULL)
2386 *dip = v;
2387 }
2388
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002389 if (tv == NULL && (current_sctx.sc_version == SCRIPT_VERSION_VIM9
2390 || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002392 imported_T *import;
2393 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2394
2395 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396
2397 // imported variable from another script
2398 if (import != NULL)
2399 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002400 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2402 + import->imp_var_vals_idx;
2403 tv = sv->sv_tv;
2404 }
2405 }
2406
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002407 if (tv == NULL)
2408 {
2409 if (rettv != NULL && verbose)
2410 semsg(_(e_undefvar), name);
2411 ret = FAIL;
2412 }
2413 else if (rettv != NULL)
2414 copy_tv(tv, rettv);
2415
2416 name[len] = cc;
2417
2418 return ret;
2419}
2420
2421/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002422 * Check if variable "name[len]" is a local variable or an argument.
2423 * If so, "*eval_lavars_used" is set to TRUE.
2424 */
2425 void
2426check_vars(char_u *name, int len)
2427{
2428 int cc;
2429 char_u *varname;
2430 hashtab_T *ht;
2431
2432 if (eval_lavars_used == NULL)
2433 return;
2434
2435 // truncate the name, so that we can use strcmp()
2436 cc = name[len];
2437 name[len] = NUL;
2438
2439 ht = find_var_ht(name, &varname);
2440 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2441 {
2442 if (find_var(name, NULL, TRUE) != NULL)
2443 *eval_lavars_used = TRUE;
2444 }
2445
2446 name[len] = cc;
2447}
2448
2449/*
2450 * Find variable "name" in the list of variables.
2451 * Return a pointer to it if found, NULL if not found.
2452 * Careful: "a:0" variables don't have a name.
2453 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2454 * hashtab_T used.
2455 */
2456 dictitem_T *
2457find_var(char_u *name, hashtab_T **htp, int no_autoload)
2458{
2459 char_u *varname;
2460 hashtab_T *ht;
2461 dictitem_T *ret = NULL;
2462
2463 ht = find_var_ht(name, &varname);
2464 if (htp != NULL)
2465 *htp = ht;
2466 if (ht == NULL)
2467 return NULL;
2468 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2469 if (ret != NULL)
2470 return ret;
2471
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002472 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002473 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2474}
2475
2476/*
2477 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002478 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002479 * Returns NULL if not found.
2480 */
2481 dictitem_T *
2482find_var_in_ht(
2483 hashtab_T *ht,
2484 int htname,
2485 char_u *varname,
2486 int no_autoload)
2487{
2488 hashitem_T *hi;
2489
2490 if (*varname == NUL)
2491 {
2492 // Must be something like "s:", otherwise "ht" would be NULL.
2493 switch (htname)
2494 {
2495 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2496 case 'g': return &globvars_var;
2497 case 'v': return &vimvars_var;
2498 case 'b': return &curbuf->b_bufvar;
2499 case 'w': return &curwin->w_winvar;
2500 case 't': return &curtab->tp_winvar;
2501 case 'l': return get_funccal_local_var();
2502 case 'a': return get_funccal_args_var();
2503 }
2504 return NULL;
2505 }
2506
2507 hi = hash_find(ht, varname);
2508 if (HASHITEM_EMPTY(hi))
2509 {
2510 // For global variables we may try auto-loading the script. If it
2511 // worked find the variable again. Don't auto-load a script if it was
2512 // loaded already, otherwise it would be loaded every time when
2513 // checking if a function name is a Funcref variable.
2514 if (ht == &globvarht && !no_autoload)
2515 {
2516 // Note: script_autoload() may make "hi" invalid. It must either
2517 // be obtained again or not used.
2518 if (!script_autoload(varname, FALSE) || aborting())
2519 return NULL;
2520 hi = hash_find(ht, varname);
2521 }
2522 if (HASHITEM_EMPTY(hi))
2523 return NULL;
2524 }
2525 return HI2DI(hi);
2526}
2527
2528/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 * Get the script-local hashtab. NULL if not in a script context.
2530 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002531 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532get_script_local_ht(void)
2533{
2534 scid_T sid = current_sctx.sc_sid;
2535
2536 if (sid > 0 && sid <= script_items.ga_len)
2537 return &SCRIPT_VARS(sid);
2538 return NULL;
2539}
2540
2541/*
2542 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002543 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002545 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2547{
2548 hashtab_T *ht = get_script_local_ht();
2549 char_u buffer[30];
2550 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002551 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 hashitem_T *hi;
2553
2554 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002555 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 if (len < sizeof(buffer) - 1)
2557 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002558 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 vim_strncpy(buffer, name, len);
2560 p = buffer;
2561 }
2562 else
2563 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002564 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002566 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 }
2568
2569 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002570 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571
2572 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002573 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2574 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
2576 if (p != buffer)
2577 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002578 // Don't return "buffer", gcc complains.
2579 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580}
2581
2582/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002583 * Find the hashtab used for a variable name.
2584 * Return NULL if the name is not valid.
2585 * Set "varname" to the start of name without ':'.
2586 */
2587 hashtab_T *
2588find_var_ht(char_u *name, char_u **varname)
2589{
2590 hashitem_T *hi;
2591 hashtab_T *ht;
2592
2593 if (name[0] == NUL)
2594 return NULL;
2595 if (name[1] != ':')
2596 {
2597 // The name must not start with a colon or #.
2598 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2599 return NULL;
2600 *varname = name;
2601
2602 // "version" is "v:version" in all scopes if scriptversion < 3.
2603 // Same for a few other variables marked with VV_COMPAT.
2604 if (current_sctx.sc_version < 3)
2605 {
2606 hi = hash_find(&compat_hashtab, name);
2607 if (!HASHITEM_EMPTY(hi))
2608 return &compat_hashtab;
2609 }
2610
2611 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 if (ht != NULL)
2613 return ht; // local variable
2614
2615 // in Vim9 script items at the script level are script-local
2616 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2617 {
2618 ht = get_script_local_ht();
2619 if (ht != NULL)
2620 return ht;
2621 }
2622
2623 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002624 }
2625 *varname = name + 2;
2626 if (*name == 'g') // global variable
2627 return &globvarht;
2628 // There must be no ':' or '#' in the rest of the name, unless g: is used
2629 if (vim_strchr(name + 2, ':') != NULL
2630 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2631 return NULL;
2632 if (*name == 'b') // buffer variable
2633 return &curbuf->b_vars->dv_hashtab;
2634 if (*name == 'w') // window variable
2635 return &curwin->w_vars->dv_hashtab;
2636 if (*name == 't') // tab page variable
2637 return &curtab->tp_vars->dv_hashtab;
2638 if (*name == 'v') // v: variable
2639 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002640 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002641 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002643 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 if (*name == 'a') // a: function argument
2645 return get_funccal_args_ht();
2646 if (*name == 'l') // l: local function variable
2647 return get_funccal_local_ht();
2648 }
2649 if (*name == 's') // script variable
2650 {
2651 ht = get_script_local_ht();
2652 if (ht != NULL)
2653 return ht;
2654 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002655 return NULL;
2656}
2657
2658/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002659 * Get the string value of a (global/local) variable.
2660 * Note: see tv_get_string() for how long the pointer remains valid.
2661 * Returns NULL when it doesn't exist.
2662 */
2663 char_u *
2664get_var_value(char_u *name)
2665{
2666 dictitem_T *v;
2667
2668 v = find_var(name, NULL, FALSE);
2669 if (v == NULL)
2670 return NULL;
2671 return tv_get_string(&v->di_tv);
2672}
2673
2674/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002675 * Allocate a new hashtab for a sourced script. It will be used while
2676 * sourcing this script and when executing functions defined in the script.
2677 */
2678 void
2679new_script_vars(scid_T id)
2680{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002681 scriptvar_T *sv;
2682
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002683 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2684 if (sv == NULL)
2685 return;
2686 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002687 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002688}
2689
2690/*
2691 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2692 * point to it.
2693 */
2694 void
2695init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2696{
2697 hash_init(&dict->dv_hashtab);
2698 dict->dv_lock = 0;
2699 dict->dv_scope = scope;
2700 dict->dv_refcount = DO_NOT_FREE_CNT;
2701 dict->dv_copyID = 0;
2702 dict_var->di_tv.vval.v_dict = dict;
2703 dict_var->di_tv.v_type = VAR_DICT;
2704 dict_var->di_tv.v_lock = VAR_FIXED;
2705 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2706 dict_var->di_key[0] = NUL;
2707}
2708
2709/*
2710 * Unreference a dictionary initialized by init_var_dict().
2711 */
2712 void
2713unref_var_dict(dict_T *dict)
2714{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002715 // Now the dict needs to be freed if no one else is using it, go back to
2716 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002717 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2718 dict_unref(dict);
2719}
2720
2721/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002722 * Clean up a list of internal variables.
2723 * Frees all allocated variables and the value they contain.
2724 * Clears hashtab "ht", does not free it.
2725 */
2726 void
2727vars_clear(hashtab_T *ht)
2728{
2729 vars_clear_ext(ht, TRUE);
2730}
2731
2732/*
2733 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2734 */
2735 void
2736vars_clear_ext(hashtab_T *ht, int free_val)
2737{
2738 int todo;
2739 hashitem_T *hi;
2740 dictitem_T *v;
2741
2742 hash_lock(ht);
2743 todo = (int)ht->ht_used;
2744 for (hi = ht->ht_array; todo > 0; ++hi)
2745 {
2746 if (!HASHITEM_EMPTY(hi))
2747 {
2748 --todo;
2749
2750 // Free the variable. Don't remove it from the hashtab,
2751 // ht_array might change then. hash_clear() takes care of it
2752 // later.
2753 v = HI2DI(hi);
2754 if (free_val)
2755 clear_tv(&v->di_tv);
2756 if (v->di_flags & DI_FLAGS_ALLOC)
2757 vim_free(v);
2758 }
2759 }
2760 hash_clear(ht);
2761 ht->ht_used = 0;
2762}
2763
2764/*
2765 * Delete a variable from hashtab "ht" at item "hi".
2766 * Clear the variable value and free the dictitem.
2767 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002768 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002769delete_var(hashtab_T *ht, hashitem_T *hi)
2770{
2771 dictitem_T *di = HI2DI(hi);
2772
2773 hash_remove(ht, hi);
2774 clear_tv(&di->di_tv);
2775 vim_free(di);
2776}
2777
2778/*
2779 * List the value of one internal variable.
2780 */
2781 static void
2782list_one_var(dictitem_T *v, char *prefix, int *first)
2783{
2784 char_u *tofree;
2785 char_u *s;
2786 char_u numbuf[NUMBUFLEN];
2787
2788 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2789 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2790 s == NULL ? (char_u *)"" : s, first);
2791 vim_free(tofree);
2792}
2793
2794 static void
2795list_one_var_a(
2796 char *prefix,
2797 char_u *name,
2798 int type,
2799 char_u *string,
2800 int *first) // when TRUE clear rest of screen and set to FALSE
2801{
2802 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2803 msg_start();
2804 msg_puts(prefix);
2805 if (name != NULL) // "a:" vars don't have a name stored
2806 msg_puts((char *)name);
2807 msg_putchar(' ');
2808 msg_advance(22);
2809 if (type == VAR_NUMBER)
2810 msg_putchar('#');
2811 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2812 msg_putchar('*');
2813 else if (type == VAR_LIST)
2814 {
2815 msg_putchar('[');
2816 if (*string == '[')
2817 ++string;
2818 }
2819 else if (type == VAR_DICT)
2820 {
2821 msg_putchar('{');
2822 if (*string == '{')
2823 ++string;
2824 }
2825 else
2826 msg_putchar(' ');
2827
2828 msg_outtrans(string);
2829
2830 if (type == VAR_FUNC || type == VAR_PARTIAL)
2831 msg_puts("()");
2832 if (*first)
2833 {
2834 msg_clr_eos();
2835 *first = FALSE;
2836 }
2837}
2838
2839/*
2840 * Set variable "name" to value in "tv".
2841 * If the variable already exists, the value is updated.
2842 * Otherwise the variable is created.
2843 */
2844 void
2845set_var(
2846 char_u *name,
2847 typval_T *tv,
2848 int copy) // make copy of value in "tv"
2849{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002851}
2852
2853/*
2854 * Set variable "name" to value in "tv".
2855 * If the variable already exists and "is_const" is FALSE the value is updated.
2856 * Otherwise the variable is created.
2857 */
2858 void
2859set_var_const(
2860 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002862 typval_T *tv,
2863 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002865{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867 char_u *varname;
2868 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002870
2871 ht = find_var_ht(name, &varname);
2872 if (ht == NULL || *varname == NUL)
2873 {
2874 semsg(_(e_illvar), name);
2875 return;
2876 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002877 is_script_local = ht == get_script_local_ht();
2878
Bram Moolenaar67979662020-06-20 22:50:47 +02002879 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002880 && !is_script_local
2881 && (flags & LET_NO_COMMAND) == 0
2882 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002883 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002884 vim9_declare_error(name);
Bram Moolenaar67979662020-06-20 22:50:47 +02002885 return;
2886 }
2887
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002889
2890 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 if (di == NULL)
2892 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893
2894 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002896 return;
2897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002899 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002901 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902 if (flags & LET_IS_CONST)
2903 {
2904 emsg(_(e_cannot_mod));
2905 return;
2906 }
2907
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002908 if (is_script_local
2909 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002911 if ((flags & LET_NO_COMMAND) == 0)
2912 {
2913 semsg(_("E1041: Redefining script item %s"), name);
2914 return;
2915 }
2916
2917 // check the type
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002918 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
2919 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002921
2922 if (var_check_ro(di->di_flags, name, FALSE)
2923 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2924 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002925 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 else
2927 // can only redefine once
2928 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002929
2930 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002933 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002934 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002935 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002937 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002939 if (copy || tv->v_type != VAR_STRING)
2940 {
2941 char_u *val = tv_get_string(tv);
2942
2943 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002944 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 if (di->di_tv.vval.v_string == NULL)
2946 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002947 }
2948 else
2949 {
2950 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002952 tv->vval.v_string = NULL;
2953 }
2954 return;
2955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002957 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002959 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002961#ifdef FEAT_SEARCH_EXTRA
2962 else if (STRCMP(varname, "hlsearch") == 0)
2963 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002965 redraw_all_later(SOME_VALID);
2966 }
2967#endif
2968 return;
2969 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002971 {
2972 semsg(_("E963: setting %s to value with wrong type"), name);
2973 return;
2974 }
2975 }
2976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002978 }
2979 else // add a new variable
2980 {
2981 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002982 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002983 {
2984 semsg(_(e_illvar), name);
2985 return;
2986 }
2987
2988 // Make sure the variable name is valid.
2989 if (!valid_varname(varname))
2990 return;
2991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2993 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 STRCPY(di->di_key, varname);
2996 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002997 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002999 return;
3000 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 di->di_flags = DI_FLAGS_ALLOC;
3002 if (flags & LET_IS_CONST)
3003 di->di_flags |= DI_FLAGS_LOCK;
3004
3005 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
3006 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003007 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008
3009 // Store a pointer to the typval_T, so that it can be found by
3010 // index instead of using a hastab lookup.
3011 if (ga_grow(&si->sn_var_vals, 1) == OK)
3012 {
3013 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3014 + si->sn_var_vals.ga_len;
3015 sv->sv_name = di->di_key;
3016 sv->sv_tv = &di->di_tv;
3017 sv->sv_type = type == NULL ? &t_any : type;
3018 sv->sv_const = (flags & LET_IS_CONST);
3019 sv->sv_export = is_export;
3020 ++si->sn_var_vals.ga_len;
3021
3022 // let ex_export() know the export worked.
3023 is_export = FALSE;
3024 }
3025 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003026 }
3027
3028 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003030 else
3031 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 di->di_tv = *tv;
3033 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003034 init_tv(tv);
3035 }
3036
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 if (flags & LET_IS_CONST)
3038 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003039}
3040
3041/*
3042 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3043 * Also give an error message.
3044 */
3045 int
3046var_check_ro(int flags, char_u *name, int use_gettext)
3047{
3048 if (flags & DI_FLAGS_RO)
3049 {
3050 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3051 return TRUE;
3052 }
3053 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3054 {
3055 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3056 return TRUE;
3057 }
3058 return FALSE;
3059}
3060
3061/*
3062 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3063 * Also give an error message.
3064 */
3065 int
3066var_check_fixed(int flags, char_u *name, int use_gettext)
3067{
3068 if (flags & DI_FLAGS_FIX)
3069 {
3070 semsg(_("E795: Cannot delete variable %s"),
3071 use_gettext ? (char_u *)_(name) : name);
3072 return TRUE;
3073 }
3074 return FALSE;
3075}
3076
3077/*
3078 * Check if a funcref is assigned to a valid variable name.
3079 * Return TRUE and give an error if not.
3080 */
3081 int
3082var_check_func_name(
3083 char_u *name, // points to start of variable name
3084 int new_var) // TRUE when creating the variable
3085{
3086 // Allow for w: b: s: and t:.
3087 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3088 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3089 ? name[2] : name[0]))
3090 {
3091 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3092 name);
3093 return TRUE;
3094 }
3095 // Don't allow hiding a function. When "v" is not NULL we might be
3096 // assigning another function to the same var, the type is checked
3097 // below.
3098 if (new_var && function_exists(name, FALSE))
3099 {
3100 semsg(_("E705: Variable name conflicts with existing function: %s"),
3101 name);
3102 return TRUE;
3103 }
3104 return FALSE;
3105}
3106
3107/*
3108 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3109 * Also give an error message, using "name" or _("name") when use_gettext is
3110 * TRUE.
3111 */
3112 int
3113var_check_lock(int lock, char_u *name, int use_gettext)
3114{
3115 if (lock & VAR_LOCKED)
3116 {
3117 semsg(_("E741: Value is locked: %s"),
3118 name == NULL ? (char_u *)_("Unknown")
3119 : use_gettext ? (char_u *)_(name)
3120 : name);
3121 return TRUE;
3122 }
3123 if (lock & VAR_FIXED)
3124 {
3125 semsg(_("E742: Cannot change value of %s"),
3126 name == NULL ? (char_u *)_("Unknown")
3127 : use_gettext ? (char_u *)_(name)
3128 : name);
3129 return TRUE;
3130 }
3131 return FALSE;
3132}
3133
3134/*
3135 * Check if a variable name is valid.
3136 * Return FALSE and give an error if not.
3137 */
3138 int
3139valid_varname(char_u *varname)
3140{
3141 char_u *p;
3142
3143 for (p = varname; *p != NUL; ++p)
3144 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3145 && *p != AUTOLOAD_CHAR)
3146 {
3147 semsg(_(e_illvar), varname);
3148 return FALSE;
3149 }
3150 return TRUE;
3151}
3152
3153/*
3154 * getwinvar() and gettabwinvar()
3155 */
3156 static void
3157getwinvar(
3158 typval_T *argvars,
3159 typval_T *rettv,
3160 int off) // 1 for gettabwinvar()
3161{
3162 win_T *win;
3163 char_u *varname;
3164 dictitem_T *v;
3165 tabpage_T *tp = NULL;
3166 int done = FALSE;
3167 win_T *oldcurwin;
3168 tabpage_T *oldtabpage;
3169 int need_switch_win;
3170
3171 if (off == 1)
3172 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3173 else
3174 tp = curtab;
3175 win = find_win_by_nr(&argvars[off], tp);
3176 varname = tv_get_string_chk(&argvars[off + 1]);
3177 ++emsg_off;
3178
3179 rettv->v_type = VAR_STRING;
3180 rettv->vval.v_string = NULL;
3181
3182 if (win != NULL && varname != NULL)
3183 {
3184 // Set curwin to be our win, temporarily. Also set the tabpage,
3185 // otherwise the window is not valid. Only do this when needed,
3186 // autocommands get blocked.
3187 need_switch_win = !(tp == curtab && win == curwin);
3188 if (!need_switch_win
3189 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3190 {
3191 if (*varname == '&')
3192 {
3193 if (varname[1] == NUL)
3194 {
3195 // get all window-local options in a dict
3196 dict_T *opts = get_winbuf_options(FALSE);
3197
3198 if (opts != NULL)
3199 {
3200 rettv_dict_set(rettv, opts);
3201 done = TRUE;
3202 }
3203 }
3204 else if (get_option_tv(&varname, rettv, 1) == OK)
3205 // window-local-option
3206 done = TRUE;
3207 }
3208 else
3209 {
3210 // Look up the variable.
3211 // Let getwinvar({nr}, "") return the "w:" dictionary.
3212 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3213 varname, FALSE);
3214 if (v != NULL)
3215 {
3216 copy_tv(&v->di_tv, rettv);
3217 done = TRUE;
3218 }
3219 }
3220 }
3221
3222 if (need_switch_win)
3223 // restore previous notion of curwin
3224 restore_win(oldcurwin, oldtabpage, TRUE);
3225 }
3226
3227 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3228 // use the default return value
3229 copy_tv(&argvars[off + 2], rettv);
3230
3231 --emsg_off;
3232}
3233
3234/*
3235 * "setwinvar()" and "settabwinvar()" functions
3236 */
3237 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003238setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003239{
3240 win_T *win;
3241 win_T *save_curwin;
3242 tabpage_T *save_curtab;
3243 int need_switch_win;
3244 char_u *varname, *winvarname;
3245 typval_T *varp;
3246 char_u nbuf[NUMBUFLEN];
3247 tabpage_T *tp = NULL;
3248
3249 if (check_secure())
3250 return;
3251
3252 if (off == 1)
3253 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3254 else
3255 tp = curtab;
3256 win = find_win_by_nr(&argvars[off], tp);
3257 varname = tv_get_string_chk(&argvars[off + 1]);
3258 varp = &argvars[off + 2];
3259
3260 if (win != NULL && varname != NULL && varp != NULL)
3261 {
3262 need_switch_win = !(tp == curtab && win == curwin);
3263 if (!need_switch_win
3264 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3265 {
3266 if (*varname == '&')
3267 {
3268 long numval;
3269 char_u *strval;
3270 int error = FALSE;
3271
3272 ++varname;
3273 numval = (long)tv_get_number_chk(varp, &error);
3274 strval = tv_get_string_buf_chk(varp, nbuf);
3275 if (!error && strval != NULL)
3276 set_option_value(varname, numval, strval, OPT_LOCAL);
3277 }
3278 else
3279 {
3280 winvarname = alloc(STRLEN(varname) + 3);
3281 if (winvarname != NULL)
3282 {
3283 STRCPY(winvarname, "w:");
3284 STRCPY(winvarname + 2, varname);
3285 set_var(winvarname, varp, TRUE);
3286 vim_free(winvarname);
3287 }
3288 }
3289 }
3290 if (need_switch_win)
3291 restore_win(save_curwin, save_curtab, TRUE);
3292 }
3293}
3294
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003295/*
3296 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3297 * v:option_type, and v:option_command.
3298 */
3299 void
3300reset_v_option_vars(void)
3301{
3302 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3303 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3304 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3305 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3306 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3307 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3308}
3309
3310/*
3311 * Add an assert error to v:errors.
3312 */
3313 void
3314assert_error(garray_T *gap)
3315{
3316 struct vimvar *vp = &vimvars[VV_ERRORS];
3317
3318 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003319 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003320 set_vim_var_list(VV_ERRORS, list_alloc());
3321 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3322}
3323
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003324 int
3325var_exists(char_u *var)
3326{
3327 char_u *name;
3328 char_u *tofree;
3329 typval_T tv;
3330 int len = 0;
3331 int n = FALSE;
3332
3333 // get_name_len() takes care of expanding curly braces
3334 name = var;
3335 len = get_name_len(&var, &tofree, TRUE, FALSE);
3336 if (len > 0)
3337 {
3338 if (tofree != NULL)
3339 name = tofree;
3340 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3341 if (n)
3342 {
3343 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003344 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3345 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003346 if (n)
3347 clear_tv(&tv);
3348 }
3349 }
3350 if (*var != NUL)
3351 n = FALSE;
3352
3353 vim_free(tofree);
3354 return n;
3355}
3356
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003357static lval_T *redir_lval = NULL;
3358#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3359static garray_T redir_ga; // only valid when redir_lval is not NULL
3360static char_u *redir_endp = NULL;
3361static char_u *redir_varname = NULL;
3362
3363/*
3364 * Start recording command output to a variable
3365 * When "append" is TRUE append to an existing variable.
3366 * Returns OK if successfully completed the setup. FAIL otherwise.
3367 */
3368 int
3369var_redir_start(char_u *name, int append)
3370{
3371 int save_emsg;
3372 int err;
3373 typval_T tv;
3374
3375 // Catch a bad name early.
3376 if (!eval_isnamec1(*name))
3377 {
3378 emsg(_(e_invarg));
3379 return FAIL;
3380 }
3381
3382 // Make a copy of the name, it is used in redir_lval until redir ends.
3383 redir_varname = vim_strsave(name);
3384 if (redir_varname == NULL)
3385 return FAIL;
3386
3387 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3388 if (redir_lval == NULL)
3389 {
3390 var_redir_stop();
3391 return FAIL;
3392 }
3393
3394 // The output is stored in growarray "redir_ga" until redirection ends.
3395 ga_init2(&redir_ga, (int)sizeof(char), 500);
3396
3397 // Parse the variable name (can be a dict or list entry).
3398 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3399 FNE_CHECK_START);
3400 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3401 {
3402 clear_lval(redir_lval);
3403 if (redir_endp != NULL && *redir_endp != NUL)
3404 // Trailing characters are present after the variable name
3405 emsg(_(e_trailing));
3406 else
3407 emsg(_(e_invarg));
3408 redir_endp = NULL; // don't store a value, only cleanup
3409 var_redir_stop();
3410 return FAIL;
3411 }
3412
3413 // check if we can write to the variable: set it to or append an empty
3414 // string
3415 save_emsg = did_emsg;
3416 did_emsg = FALSE;
3417 tv.v_type = VAR_STRING;
3418 tv.vval.v_string = (char_u *)"";
3419 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003421 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003422 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003423 clear_lval(redir_lval);
3424 err = did_emsg;
3425 did_emsg |= save_emsg;
3426 if (err)
3427 {
3428 redir_endp = NULL; // don't store a value, only cleanup
3429 var_redir_stop();
3430 return FAIL;
3431 }
3432
3433 return OK;
3434}
3435
3436/*
3437 * Append "value[value_len]" to the variable set by var_redir_start().
3438 * The actual appending is postponed until redirection ends, because the value
3439 * appended may in fact be the string we write to, changing it may cause freed
3440 * memory to be used:
3441 * :redir => foo
3442 * :let foo
3443 * :redir END
3444 */
3445 void
3446var_redir_str(char_u *value, int value_len)
3447{
3448 int len;
3449
3450 if (redir_lval == NULL)
3451 return;
3452
3453 if (value_len == -1)
3454 len = (int)STRLEN(value); // Append the entire string
3455 else
3456 len = value_len; // Append only "value_len" characters
3457
3458 if (ga_grow(&redir_ga, len) == OK)
3459 {
3460 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3461 redir_ga.ga_len += len;
3462 }
3463 else
3464 var_redir_stop();
3465}
3466
3467/*
3468 * Stop redirecting command output to a variable.
3469 * Frees the allocated memory.
3470 */
3471 void
3472var_redir_stop(void)
3473{
3474 typval_T tv;
3475
3476 if (EVALCMD_BUSY)
3477 {
3478 redir_lval = NULL;
3479 return;
3480 }
3481
3482 if (redir_lval != NULL)
3483 {
3484 // If there was no error: assign the text to the variable.
3485 if (redir_endp != NULL)
3486 {
3487 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3488 tv.v_type = VAR_STRING;
3489 tv.vval.v_string = redir_ga.ga_data;
3490 // Call get_lval() again, if it's inside a Dict or List it may
3491 // have changed.
3492 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3493 FALSE, FALSE, 0, FNE_CHECK_START);
3494 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003495 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003496 (char_u *)".");
3497 clear_lval(redir_lval);
3498 }
3499
3500 // free the collected output
3501 VIM_CLEAR(redir_ga.ga_data);
3502
3503 VIM_CLEAR(redir_lval);
3504 }
3505 VIM_CLEAR(redir_varname);
3506}
3507
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003508/*
3509 * "gettabvar()" function
3510 */
3511 void
3512f_gettabvar(typval_T *argvars, typval_T *rettv)
3513{
3514 win_T *oldcurwin;
3515 tabpage_T *tp, *oldtabpage;
3516 dictitem_T *v;
3517 char_u *varname;
3518 int done = FALSE;
3519
3520 rettv->v_type = VAR_STRING;
3521 rettv->vval.v_string = NULL;
3522
3523 varname = tv_get_string_chk(&argvars[1]);
3524 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3525 if (tp != NULL && varname != NULL)
3526 {
3527 // Set tp to be our tabpage, temporarily. Also set the window to the
3528 // first window in the tabpage, otherwise the window is not valid.
3529 if (switch_win(&oldcurwin, &oldtabpage,
3530 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3531 : tp->tp_firstwin, tp, TRUE) == OK)
3532 {
3533 // look up the variable
3534 // Let gettabvar({nr}, "") return the "t:" dictionary.
3535 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3536 if (v != NULL)
3537 {
3538 copy_tv(&v->di_tv, rettv);
3539 done = TRUE;
3540 }
3541 }
3542
3543 // restore previous notion of curwin
3544 restore_win(oldcurwin, oldtabpage, TRUE);
3545 }
3546
3547 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3548 copy_tv(&argvars[2], rettv);
3549}
3550
3551/*
3552 * "gettabwinvar()" function
3553 */
3554 void
3555f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3556{
3557 getwinvar(argvars, rettv, 1);
3558}
3559
3560/*
3561 * "getwinvar()" function
3562 */
3563 void
3564f_getwinvar(typval_T *argvars, typval_T *rettv)
3565{
3566 getwinvar(argvars, rettv, 0);
3567}
3568
3569/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003570 * "getbufvar()" function
3571 */
3572 void
3573f_getbufvar(typval_T *argvars, typval_T *rettv)
3574{
3575 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003576 char_u *varname;
3577 dictitem_T *v;
3578 int done = FALSE;
3579
3580 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3581 varname = tv_get_string_chk(&argvars[1]);
3582 ++emsg_off;
3583 buf = tv_get_buf(&argvars[0], FALSE);
3584
3585 rettv->v_type = VAR_STRING;
3586 rettv->vval.v_string = NULL;
3587
3588 if (buf != NULL && varname != NULL)
3589 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003590 if (*varname == '&')
3591 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003592 buf_T *save_curbuf = curbuf;
3593
3594 // set curbuf to be our buf, temporarily
3595 curbuf = buf;
3596
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003597 if (varname[1] == NUL)
3598 {
3599 // get all buffer-local options in a dict
3600 dict_T *opts = get_winbuf_options(TRUE);
3601
3602 if (opts != NULL)
3603 {
3604 rettv_dict_set(rettv, opts);
3605 done = TRUE;
3606 }
3607 }
3608 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3609 // buffer-local-option
3610 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003611
3612 // restore previous notion of curbuf
3613 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003614 }
3615 else
3616 {
3617 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003618 if (*varname == NUL)
3619 // Let getbufvar({nr}, "") return the "b:" dictionary.
3620 v = &buf->b_bufvar;
3621 else
3622 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3623 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003624 if (v != NULL)
3625 {
3626 copy_tv(&v->di_tv, rettv);
3627 done = TRUE;
3628 }
3629 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003630 }
3631
3632 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3633 // use the default value
3634 copy_tv(&argvars[2], rettv);
3635
3636 --emsg_off;
3637}
3638
3639/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003640 * "settabvar()" function
3641 */
3642 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003643f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003644{
3645 tabpage_T *save_curtab;
3646 tabpage_T *tp;
3647 char_u *varname, *tabvarname;
3648 typval_T *varp;
3649
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003650 if (check_secure())
3651 return;
3652
3653 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3654 varname = tv_get_string_chk(&argvars[1]);
3655 varp = &argvars[2];
3656
3657 if (varname != NULL && varp != NULL && tp != NULL)
3658 {
3659 save_curtab = curtab;
3660 goto_tabpage_tp(tp, FALSE, FALSE);
3661
3662 tabvarname = alloc(STRLEN(varname) + 3);
3663 if (tabvarname != NULL)
3664 {
3665 STRCPY(tabvarname, "t:");
3666 STRCPY(tabvarname + 2, varname);
3667 set_var(tabvarname, varp, TRUE);
3668 vim_free(tabvarname);
3669 }
3670
3671 // Restore current tabpage
3672 if (valid_tabpage(save_curtab))
3673 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3674 }
3675}
3676
3677/*
3678 * "settabwinvar()" function
3679 */
3680 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003681f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003682{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003683 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003684}
3685
3686/*
3687 * "setwinvar()" function
3688 */
3689 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003690f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003691{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003692 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003693}
3694
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003695/*
3696 * "setbufvar()" function
3697 */
3698 void
3699f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3700{
3701 buf_T *buf;
3702 char_u *varname, *bufvarname;
3703 typval_T *varp;
3704 char_u nbuf[NUMBUFLEN];
3705
3706 if (check_secure())
3707 return;
3708 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3709 varname = tv_get_string_chk(&argvars[1]);
3710 buf = tv_get_buf(&argvars[0], FALSE);
3711 varp = &argvars[2];
3712
3713 if (buf != NULL && varname != NULL && varp != NULL)
3714 {
3715 if (*varname == '&')
3716 {
3717 long numval;
3718 char_u *strval;
3719 int error = FALSE;
3720 aco_save_T aco;
3721
3722 // set curbuf to be our buf, temporarily
3723 aucmd_prepbuf(&aco, buf);
3724
3725 ++varname;
3726 numval = (long)tv_get_number_chk(varp, &error);
3727 strval = tv_get_string_buf_chk(varp, nbuf);
3728 if (!error && strval != NULL)
3729 set_option_value(varname, numval, strval, OPT_LOCAL);
3730
3731 // reset notion of buffer
3732 aucmd_restbuf(&aco);
3733 }
3734 else
3735 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003736 bufvarname = alloc(STRLEN(varname) + 3);
3737 if (bufvarname != NULL)
3738 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003739 buf_T *save_curbuf = curbuf;
3740
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003741 curbuf = buf;
3742 STRCPY(bufvarname, "b:");
3743 STRCPY(bufvarname + 2, varname);
3744 set_var(bufvarname, varp, TRUE);
3745 vim_free(bufvarname);
3746 curbuf = save_curbuf;
3747 }
3748 }
3749 }
3750}
3751
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003752/*
3753 * Get a callback from "arg". It can be a Funcref or a function name.
3754 * When "arg" is zero return an empty string.
3755 * "cb_name" is not allocated.
3756 * "cb_name" is set to NULL for an invalid argument.
3757 */
3758 callback_T
3759get_callback(typval_T *arg)
3760{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003761 callback_T res;
3762 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003763
3764 res.cb_free_name = FALSE;
3765 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3766 {
3767 res.cb_partial = arg->vval.v_partial;
3768 ++res.cb_partial->pt_refcount;
3769 res.cb_name = partial_name(res.cb_partial);
3770 }
3771 else
3772 {
3773 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003774 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3775 && isdigit(*arg->vval.v_string))
3776 r = FAIL;
3777 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003778 {
3779 // Note that we don't make a copy of the string.
3780 res.cb_name = arg->vval.v_string;
3781 func_ref(res.cb_name);
3782 }
3783 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003784 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003785 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003786 r = FAIL;
3787
3788 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003789 {
3790 emsg(_("E921: Invalid callback argument"));
3791 res.cb_name = NULL;
3792 }
3793 }
3794 return res;
3795}
3796
3797/*
3798 * Copy a callback into a typval_T.
3799 */
3800 void
3801put_callback(callback_T *cb, typval_T *tv)
3802{
3803 if (cb->cb_partial != NULL)
3804 {
3805 tv->v_type = VAR_PARTIAL;
3806 tv->vval.v_partial = cb->cb_partial;
3807 ++tv->vval.v_partial->pt_refcount;
3808 }
3809 else
3810 {
3811 tv->v_type = VAR_FUNC;
3812 tv->vval.v_string = vim_strsave(cb->cb_name);
3813 func_ref(cb->cb_name);
3814 }
3815}
3816
3817/*
3818 * Make a copy of "src" into "dest", allocating the function name if needed,
3819 * without incrementing the refcount.
3820 */
3821 void
3822set_callback(callback_T *dest, callback_T *src)
3823{
3824 if (src->cb_partial == NULL)
3825 {
3826 // just a function name, make a copy
3827 dest->cb_name = vim_strsave(src->cb_name);
3828 dest->cb_free_name = TRUE;
3829 }
3830 else
3831 {
3832 // cb_name is a pointer into cb_partial
3833 dest->cb_name = src->cb_name;
3834 dest->cb_free_name = FALSE;
3835 }
3836 dest->cb_partial = src->cb_partial;
3837}
3838
3839/*
3840 * Unref/free "callback" returned by get_callback() or set_callback().
3841 */
3842 void
3843free_callback(callback_T *callback)
3844{
3845 if (callback->cb_partial != NULL)
3846 {
3847 partial_unref(callback->cb_partial);
3848 callback->cb_partial = NULL;
3849 }
3850 else if (callback->cb_name != NULL)
3851 func_unref(callback->cb_name);
3852 if (callback->cb_free_name)
3853 {
3854 vim_free(callback->cb_name);
3855 callback->cb_free_name = FALSE;
3856 }
3857 callback->cb_name = NULL;
3858}
3859
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003860#endif // FEAT_EVAL