blob: b3e89150ad4eded84f0d06c19548705d191d68d2 [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 Moolenaar32e35112020-05-14 22:41:15 +0200438 if (eval1(&p, &rettv, EVAL_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 Moolenaar32e35112020-05-14 22:41:15 +0200777 int eval_flags;
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;
800 eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200801 i = eval0(expr, &rettv, &eap->nextcmd, eval_flags);
802 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200803 if (eap->skip)
804 {
805 if (i != FAIL)
806 clear_tv(&rettv);
807 --emsg_skip;
808 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200809 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 {
811 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813 clear_tv(&rettv);
814 }
815 }
816}
817
818/*
819 * Assign the typevalue "tv" to the variable or variables at "arg_start".
820 * Handles both "var" with any type and "[var, var; var]" with a list type.
821 * When "op" is not NULL it points to a string with characters that
822 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
823 * or concatenate.
824 * Returns OK or FAIL;
825 */
826 int
827ex_let_vars(
828 char_u *arg_start,
829 typval_T *tv,
830 int copy, // copy values from "tv", don't move
831 int semicolon, // from skip_var_list()
832 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 char_u *op)
835{
836 char_u *arg = arg_start;
837 list_T *l;
838 int i;
839 listitem_T *item;
840 typval_T ltv;
841
842 if (*arg != '[')
843 {
844 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200846 return FAIL;
847 return OK;
848 }
849
850 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
851 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
852 {
853 emsg(_(e_listreq));
854 return FAIL;
855 }
856
857 i = list_len(l);
858 if (semicolon == 0 && var_count < i)
859 {
860 emsg(_("E687: Less targets than List items"));
861 return FAIL;
862 }
863 if (var_count - semicolon > i)
864 {
865 emsg(_("E688: More targets than List items"));
866 return FAIL;
867 }
868
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200869 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200870 item = l->lv_first;
871 while (*arg != ']')
872 {
873 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100874 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200875 item = item->li_next;
876 if (arg == NULL)
877 return FAIL;
878
879 arg = skipwhite(arg);
880 if (*arg == ';')
881 {
882 // Put the rest of the list (may be empty) in the var after ';'.
883 // Create a new list for this.
884 l = list_alloc();
885 if (l == NULL)
886 return FAIL;
887 while (item != NULL)
888 {
889 list_append_tv(l, &item->li_tv);
890 item = item->li_next;
891 }
892
893 ltv.v_type = VAR_LIST;
894 ltv.v_lock = 0;
895 ltv.vval.v_list = l;
896 l->lv_refcount = 1;
897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899 (char_u *)"]", op);
900 clear_tv(&ltv);
901 if (arg == NULL)
902 return FAIL;
903 break;
904 }
905 else if (*arg != ',' && *arg != ']')
906 {
907 internal_error("ex_let_vars()");
908 return FAIL;
909 }
910 }
911
912 return OK;
913}
914
915/*
916 * Skip over assignable variable "var" or list of variables "[var, var]".
917 * Used for ":let varvar = expr" and ":for varvar in expr".
918 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200919 * for "[var, var; var]" set "semicolon" to 1.
920 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200921 * Return NULL for an error.
922 */
923 char_u *
924skip_var_list(
925 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200927 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200928 int *semicolon,
929 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200930{
931 char_u *p, *s;
932
933 if (*arg == '[')
934 {
935 // "[var, var]": find the matching ']'.
936 p = arg;
937 for (;;)
938 {
939 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200940 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200941 if (s == p)
942 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200943 if (!silent)
944 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200945 return NULL;
946 }
947 ++*var_count;
948
949 p = skipwhite(s);
950 if (*p == ']')
951 break;
952 else if (*p == ';')
953 {
954 if (*semicolon == 1)
955 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200956 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 return NULL;
958 }
959 *semicolon = 1;
960 }
961 else if (*p != ',')
962 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200963 if (!silent)
964 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200965 return NULL;
966 }
967 }
968 return p + 1;
969 }
970 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972}
973
974/*
975 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
976 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200978 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200979 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 char_u *end;
983
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984 if (*arg == '@' && arg[1] != NUL)
985 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200988 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200990 // "a: type" is declaring variable "a" with a type, not "a:".
991 if (end == arg + 2 && end[-1] == ':')
992 --end;
993 if (*end == ':')
994 end = skip_type(skipwhite(end + 1));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 }
996 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200997}
998
999/*
1000 * List variables for hashtab "ht" with prefix "prefix".
1001 * If "empty" is TRUE also list NULL strings as empty strings.
1002 */
1003 void
1004list_hashtable_vars(
1005 hashtab_T *ht,
1006 char *prefix,
1007 int empty,
1008 int *first)
1009{
1010 hashitem_T *hi;
1011 dictitem_T *di;
1012 int todo;
1013 char_u buf[IOSIZE];
1014
1015 todo = (int)ht->ht_used;
1016 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1017 {
1018 if (!HASHITEM_EMPTY(hi))
1019 {
1020 --todo;
1021 di = HI2DI(hi);
1022
1023 // apply :filter /pat/ to variable name
1024 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1025 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1026 if (message_filtered(buf))
1027 continue;
1028
1029 if (empty || di->di_tv.v_type != VAR_STRING
1030 || di->di_tv.vval.v_string != NULL)
1031 list_one_var(di, prefix, first);
1032 }
1033 }
1034}
1035
1036/*
1037 * List global variables.
1038 */
1039 static void
1040list_glob_vars(int *first)
1041{
1042 list_hashtable_vars(&globvarht, "", TRUE, first);
1043}
1044
1045/*
1046 * List buffer variables.
1047 */
1048 static void
1049list_buf_vars(int *first)
1050{
1051 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1052}
1053
1054/*
1055 * List window variables.
1056 */
1057 static void
1058list_win_vars(int *first)
1059{
1060 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1061}
1062
1063/*
1064 * List tab page variables.
1065 */
1066 static void
1067list_tab_vars(int *first)
1068{
1069 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1070}
1071
1072/*
1073 * List variables in "arg".
1074 */
1075 static char_u *
1076list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1077{
1078 int error = FALSE;
1079 int len;
1080 char_u *name;
1081 char_u *name_start;
1082 char_u *arg_subsc;
1083 char_u *tofree;
1084 typval_T tv;
1085
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001086 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001087 {
1088 if (error || eap->skip)
1089 {
1090 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1091 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1092 {
1093 emsg_severe = TRUE;
1094 emsg(_(e_trailing));
1095 break;
1096 }
1097 }
1098 else
1099 {
1100 // get_name_len() takes care of expanding curly braces
1101 name_start = name = arg;
1102 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1103 if (len <= 0)
1104 {
1105 // This is mainly to keep test 49 working: when expanding
1106 // curly braces fails overrule the exception error message.
1107 if (len < 0 && !aborting())
1108 {
1109 emsg_severe = TRUE;
1110 semsg(_(e_invarg2), arg);
1111 break;
1112 }
1113 error = TRUE;
1114 }
1115 else
1116 {
1117 if (tofree != NULL)
1118 name = tofree;
1119 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1120 error = TRUE;
1121 else
1122 {
1123 // handle d.key, l[idx], f(expr)
1124 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001125 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001126 name, &name) == FAIL)
1127 error = TRUE;
1128 else
1129 {
1130 if (arg == arg_subsc && len == 2 && name[1] == ':')
1131 {
1132 switch (*name)
1133 {
1134 case 'g': list_glob_vars(first); break;
1135 case 'b': list_buf_vars(first); break;
1136 case 'w': list_win_vars(first); break;
1137 case 't': list_tab_vars(first); break;
1138 case 'v': list_vim_vars(first); break;
1139 case 's': list_script_vars(first); break;
1140 case 'l': list_func_vars(first); break;
1141 default:
1142 semsg(_("E738: Can't list variables for %s"), name);
1143 }
1144 }
1145 else
1146 {
1147 char_u numbuf[NUMBUFLEN];
1148 char_u *tf;
1149 int c;
1150 char_u *s;
1151
1152 s = echo_string(&tv, &tf, numbuf, 0);
1153 c = *arg;
1154 *arg = NUL;
1155 list_one_var_a("",
1156 arg == arg_subsc ? name : name_start,
1157 tv.v_type,
1158 s == NULL ? (char_u *)"" : s,
1159 first);
1160 *arg = c;
1161 vim_free(tf);
1162 }
1163 clear_tv(&tv);
1164 }
1165 }
1166 }
1167
1168 vim_free(tofree);
1169 }
1170
1171 arg = skipwhite(arg);
1172 }
1173
1174 return arg;
1175}
1176
1177/*
1178 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1179 * Returns a pointer to the char just after the var name.
1180 * Returns NULL if there is an error.
1181 */
1182 static char_u *
1183ex_let_one(
1184 char_u *arg, // points to variable name
1185 typval_T *tv, // value to assign to variable
1186 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 char_u *endchars, // valid chars after variable name or NULL
1189 char_u *op) // "+", "-", "." or NULL
1190{
1191 int c1;
1192 char_u *name;
1193 char_u *p;
1194 char_u *arg_end = NULL;
1195 int len;
1196 int opt_flags;
1197 char_u *tofree = NULL;
1198
1199 // ":let $VAR = expr": Set environment variable.
1200 if (*arg == '$')
1201 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001203 {
1204 emsg(_("E996: Cannot lock an environment variable"));
1205 return NULL;
1206 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001207 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1208 && (flags & LET_NO_COMMAND) == 0)
1209 {
1210 vim9_declare_error(arg);
1211 return NULL;
1212 }
1213
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001214 // Find the end of the name.
1215 ++arg;
1216 name = arg;
1217 len = get_env_len(&arg);
1218 if (len == 0)
1219 semsg(_(e_invarg2), name - 1);
1220 else
1221 {
1222 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1223 semsg(_(e_letwrong), op);
1224 else if (endchars != NULL
1225 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1226 emsg(_(e_letunexp));
1227 else if (!check_secure())
1228 {
1229 c1 = name[len];
1230 name[len] = NUL;
1231 p = tv_get_string_chk(tv);
1232 if (p != NULL && op != NULL && *op == '.')
1233 {
1234 int mustfree = FALSE;
1235 char_u *s = vim_getenv(name, &mustfree);
1236
1237 if (s != NULL)
1238 {
1239 p = tofree = concat_str(s, p);
1240 if (mustfree)
1241 vim_free(s);
1242 }
1243 }
1244 if (p != NULL)
1245 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001246 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001247 arg_end = arg;
1248 }
1249 name[len] = c1;
1250 vim_free(tofree);
1251 }
1252 }
1253 }
1254
1255 // ":let &option = expr": Set option value.
1256 // ":let &l:option = expr": Set local option value.
1257 // ":let &g:option = expr": Set global option value.
1258 else if (*arg == '&')
1259 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001261 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001263 return NULL;
1264 }
1265 // Find the end of the name.
1266 p = find_option_end(&arg, &opt_flags);
1267 if (p == NULL || (endchars != NULL
1268 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1269 emsg(_(e_letunexp));
1270 else
1271 {
1272 long n;
1273 int opt_type;
1274 long numval;
1275 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001276 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001277
1278 c1 = *p;
1279 *p = NUL;
1280
1281 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001282 // avoid setting a string option to the text "v:false" or similar.
1283 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1284 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001285 if (s != NULL && op != NULL && *op != '=')
1286 {
1287 opt_type = get_option_value(arg, &numval,
1288 &stringval, opt_flags);
1289 if ((opt_type == 1 && *op == '.')
1290 || (opt_type == 0 && *op != '.'))
1291 {
1292 semsg(_(e_letwrong), op);
1293 s = NULL; // don't set the value
1294 }
1295 else
1296 {
1297 if (opt_type == 1) // number
1298 {
1299 switch (*op)
1300 {
1301 case '+': n = numval + n; break;
1302 case '-': n = numval - n; break;
1303 case '*': n = numval * n; break;
1304 case '/': n = (long)num_divide(numval, n); break;
1305 case '%': n = (long)num_modulus(numval, n); break;
1306 }
1307 }
1308 else if (opt_type == 0 && stringval != NULL) // string
1309 {
1310 s = concat_str(stringval, s);
1311 vim_free(stringval);
1312 stringval = s;
1313 }
1314 }
1315 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001316 if (s != NULL || tv->v_type == VAR_BOOL
1317 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001318 {
1319 set_option_value(arg, n, s, opt_flags);
1320 arg_end = p;
1321 }
1322 *p = c1;
1323 vim_free(stringval);
1324 }
1325 }
1326
1327 // ":let @r = expr": Set register contents.
1328 else if (*arg == '@')
1329 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001331 {
1332 emsg(_("E996: Cannot lock a register"));
1333 return NULL;
1334 }
1335 ++arg;
1336 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1337 semsg(_(e_letwrong), op);
1338 else if (endchars != NULL
1339 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1340 emsg(_(e_letunexp));
1341 else
1342 {
1343 char_u *ptofree = NULL;
1344 char_u *s;
1345
1346 p = tv_get_string_chk(tv);
1347 if (p != NULL && op != NULL && *op == '.')
1348 {
1349 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1350 if (s != NULL)
1351 {
1352 p = ptofree = concat_str(s, p);
1353 vim_free(s);
1354 }
1355 }
1356 if (p != NULL)
1357 {
1358 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1359 arg_end = arg + 1;
1360 }
1361 vim_free(ptofree);
1362 }
1363 }
1364
1365 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 // ":let {expr} = expr": Idem, name made with curly braces
1368 else if (eval_isnamec1(*arg) || *arg == '{')
1369 {
1370 lval_T lv;
1371
1372 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001373 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001374 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 if (endchars != NULL && vim_strchr(endchars,
1376 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 emsg(_(e_letunexp));
1378 else
1379 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381 arg_end = p;
1382 }
1383 }
1384 clear_lval(&lv);
1385 }
1386
1387 else
1388 semsg(_(e_invarg2), arg);
1389
1390 return arg_end;
1391}
1392
1393/*
1394 * ":unlet[!] var1 ... " command.
1395 */
1396 void
1397ex_unlet(exarg_T *eap)
1398{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001399 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001400}
1401
1402/*
1403 * ":lockvar" and ":unlockvar" commands
1404 */
1405 void
1406ex_lockvar(exarg_T *eap)
1407{
1408 char_u *arg = eap->arg;
1409 int deep = 2;
1410
1411 if (eap->forceit)
1412 deep = -1;
1413 else if (vim_isdigit(*arg))
1414 {
1415 deep = getdigits(&arg);
1416 arg = skipwhite(arg);
1417 }
1418
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001419 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001420}
1421
1422/*
1423 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001424 * Also used for Vim9 script. "callback" is invoked as:
1425 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001427 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001428ex_unletlock(
1429 exarg_T *eap,
1430 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001431 int deep,
1432 int glv_flags,
1433 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1434 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435{
1436 char_u *arg = argstart;
1437 char_u *name_end;
1438 int error = FALSE;
1439 lval_T lv;
1440
1441 do
1442 {
1443 if (*arg == '$')
1444 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001445 lv.ll_name = arg;
1446 lv.ll_tv = NULL;
1447 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001448 if (get_env_len(&arg) == 0)
1449 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001450 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451 return;
1452 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001453 if (!error && !eap->skip
1454 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1455 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001456 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001457 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001458 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001459 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001460 // Parse the name and find the end.
1461 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1462 glv_flags, FNE_CHECK_START);
1463 if (lv.ll_name == NULL)
1464 error = TRUE; // error but continue parsing
1465 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1466 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001468 if (name_end != NULL)
1469 {
1470 emsg_severe = TRUE;
1471 emsg(_(e_trailing));
1472 }
1473 if (!(eap->skip || error))
1474 clear_lval(&lv);
1475 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001477
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001478 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001479 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001480 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001481
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001482 if (!eap->skip)
1483 clear_lval(&lv);
1484 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001485
1486 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001487 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001488
1489 eap->nextcmd = check_nextcmd(arg);
1490}
1491
1492 static int
1493do_unlet_var(
1494 lval_T *lp,
1495 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001496 exarg_T *eap,
1497 int deep UNUSED,
1498 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001499{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001500 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001501 int ret = OK;
1502 int cc;
1503
1504 if (lp->ll_tv == NULL)
1505 {
1506 cc = *name_end;
1507 *name_end = NUL;
1508
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001509 // Environment variable, normal name or expanded name.
1510 if (*lp->ll_name == '$')
1511 vim_unsetenv(lp->ll_name + 1);
1512 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001513 ret = FAIL;
1514 *name_end = cc;
1515 }
1516 else if ((lp->ll_list != NULL
1517 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1518 || (lp->ll_dict != NULL
1519 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1520 return FAIL;
1521 else if (lp->ll_range)
1522 {
1523 listitem_T *li;
1524 listitem_T *ll_li = lp->ll_li;
1525 int ll_n1 = lp->ll_n1;
1526
1527 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1528 {
1529 li = ll_li->li_next;
1530 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1531 return FAIL;
1532 ll_li = li;
1533 ++ll_n1;
1534 }
1535
1536 // Delete a range of List items.
1537 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1538 {
1539 li = lp->ll_li->li_next;
1540 listitem_remove(lp->ll_list, lp->ll_li);
1541 lp->ll_li = li;
1542 ++lp->ll_n1;
1543 }
1544 }
1545 else
1546 {
1547 if (lp->ll_list != NULL)
1548 // unlet a List item.
1549 listitem_remove(lp->ll_list, lp->ll_li);
1550 else
1551 // unlet a Dictionary item.
1552 dictitem_remove(lp->ll_dict, lp->ll_di);
1553 }
1554
1555 return ret;
1556}
1557
1558/*
1559 * "unlet" a variable. Return OK if it existed, FAIL if not.
1560 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1561 */
1562 int
1563do_unlet(char_u *name, int forceit)
1564{
1565 hashtab_T *ht;
1566 hashitem_T *hi;
1567 char_u *varname;
1568 dict_T *d;
1569 dictitem_T *di;
1570
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001571 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1572 && check_vim9_unlet(name) == FAIL)
1573 return FAIL;
1574
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575 ht = find_var_ht(name, &varname);
1576 if (ht != NULL && *varname != NUL)
1577 {
1578 d = get_current_funccal_dict(ht);
1579 if (d == NULL)
1580 {
1581 if (ht == &globvarht)
1582 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001583 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584 d = &vimvardict;
1585 else
1586 {
1587 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1588 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1589 }
1590 if (d == NULL)
1591 {
1592 internal_error("do_unlet()");
1593 return FAIL;
1594 }
1595 }
1596 hi = hash_find(ht, varname);
1597 if (HASHITEM_EMPTY(hi))
1598 hi = find_hi_in_scoped_ht(name, &ht);
1599 if (hi != NULL && !HASHITEM_EMPTY(hi))
1600 {
1601 di = HI2DI(hi);
1602 if (var_check_fixed(di->di_flags, name, FALSE)
1603 || var_check_ro(di->di_flags, name, FALSE)
1604 || var_check_lock(d->dv_lock, name, FALSE))
1605 return FAIL;
1606
1607 delete_var(ht, hi);
1608 return OK;
1609 }
1610 }
1611 if (forceit)
1612 return OK;
1613 semsg(_("E108: No such variable: \"%s\""), name);
1614 return FAIL;
1615}
1616
1617/*
1618 * Lock or unlock variable indicated by "lp".
1619 * "deep" is the levels to go (-1 for unlimited);
1620 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1621 */
1622 static int
1623do_lock_var(
1624 lval_T *lp,
1625 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001626 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001627 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001628 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001629{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001630 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001631 int ret = OK;
1632 int cc;
1633 dictitem_T *di;
1634
1635 if (deep == 0) // nothing to do
1636 return OK;
1637
1638 if (lp->ll_tv == NULL)
1639 {
1640 cc = *name_end;
1641 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001642 if (*lp->ll_name == '$')
1643 {
1644 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001645 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001646 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001647 else
1648 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001649 // Normal name or expanded name.
1650 di = find_var(lp->ll_name, NULL, TRUE);
1651 if (di == NULL)
1652 ret = FAIL;
1653 else if ((di->di_flags & DI_FLAGS_FIX)
1654 && di->di_tv.v_type != VAR_DICT
1655 && di->di_tv.v_type != VAR_LIST)
1656 {
1657 // For historic reasons this error is not given for a list or
1658 // dict. E.g., the b: dict could be locked/unlocked.
1659 semsg(_(e_lock_unlock), lp->ll_name);
1660 ret = FAIL;
1661 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001662 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001663 {
1664 if (lock)
1665 di->di_flags |= DI_FLAGS_LOCK;
1666 else
1667 di->di_flags &= ~DI_FLAGS_LOCK;
1668 item_lock(&di->di_tv, deep, lock);
1669 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001670 }
1671 *name_end = cc;
1672 }
1673 else if (lp->ll_range)
1674 {
1675 listitem_T *li = lp->ll_li;
1676
1677 // (un)lock a range of List items.
1678 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1679 {
1680 item_lock(&li->li_tv, deep, lock);
1681 li = li->li_next;
1682 ++lp->ll_n1;
1683 }
1684 }
1685 else if (lp->ll_list != NULL)
1686 // (un)lock a List item.
1687 item_lock(&lp->ll_li->li_tv, deep, lock);
1688 else
1689 // (un)lock a Dictionary item.
1690 item_lock(&lp->ll_di->di_tv, deep, lock);
1691
1692 return ret;
1693}
1694
1695/*
1696 * Lock or unlock an item. "deep" is nr of levels to go.
1697 */
1698 static void
1699item_lock(typval_T *tv, int deep, int lock)
1700{
1701 static int recurse = 0;
1702 list_T *l;
1703 listitem_T *li;
1704 dict_T *d;
1705 blob_T *b;
1706 hashitem_T *hi;
1707 int todo;
1708
1709 if (recurse >= DICT_MAXNEST)
1710 {
1711 emsg(_("E743: variable nested too deep for (un)lock"));
1712 return;
1713 }
1714 if (deep == 0)
1715 return;
1716 ++recurse;
1717
1718 // lock/unlock the item itself
1719 if (lock)
1720 tv->v_lock |= VAR_LOCKED;
1721 else
1722 tv->v_lock &= ~VAR_LOCKED;
1723
1724 switch (tv->v_type)
1725 {
1726 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001727 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001729 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001731 case VAR_STRING:
1732 case VAR_FUNC:
1733 case VAR_PARTIAL:
1734 case VAR_FLOAT:
1735 case VAR_SPECIAL:
1736 case VAR_JOB:
1737 case VAR_CHANNEL:
1738 break;
1739
1740 case VAR_BLOB:
1741 if ((b = tv->vval.v_blob) != NULL)
1742 {
1743 if (lock)
1744 b->bv_lock |= VAR_LOCKED;
1745 else
1746 b->bv_lock &= ~VAR_LOCKED;
1747 }
1748 break;
1749 case VAR_LIST:
1750 if ((l = tv->vval.v_list) != NULL)
1751 {
1752 if (lock)
1753 l->lv_lock |= VAR_LOCKED;
1754 else
1755 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001756 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001757 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001758 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001759 item_lock(&li->li_tv, deep - 1, lock);
1760 }
1761 break;
1762 case VAR_DICT:
1763 if ((d = tv->vval.v_dict) != NULL)
1764 {
1765 if (lock)
1766 d->dv_lock |= VAR_LOCKED;
1767 else
1768 d->dv_lock &= ~VAR_LOCKED;
1769 if (deep < 0 || deep > 1)
1770 {
1771 // recursive: lock/unlock the items the List contains
1772 todo = (int)d->dv_hashtab.ht_used;
1773 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1774 {
1775 if (!HASHITEM_EMPTY(hi))
1776 {
1777 --todo;
1778 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1779 }
1780 }
1781 }
1782 }
1783 }
1784 --recurse;
1785}
1786
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001787#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1788/*
1789 * Delete all "menutrans_" variables.
1790 */
1791 void
1792del_menutrans_vars(void)
1793{
1794 hashitem_T *hi;
1795 int todo;
1796
1797 hash_lock(&globvarht);
1798 todo = (int)globvarht.ht_used;
1799 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1800 {
1801 if (!HASHITEM_EMPTY(hi))
1802 {
1803 --todo;
1804 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1805 delete_var(&globvarht, hi);
1806 }
1807 }
1808 hash_unlock(&globvarht);
1809}
1810#endif
1811
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001813 * Local string buffer for the next two functions to store a variable name
1814 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1815 * get_user_var_name().
1816 */
1817
1818static char_u *varnamebuf = NULL;
1819static int varnamebuflen = 0;
1820
1821/*
1822 * Function to concatenate a prefix and a variable name.
1823 */
1824 static char_u *
1825cat_prefix_varname(int prefix, char_u *name)
1826{
1827 int len;
1828
1829 len = (int)STRLEN(name) + 3;
1830 if (len > varnamebuflen)
1831 {
1832 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001833 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001834 varnamebuf = alloc(len);
1835 if (varnamebuf == NULL)
1836 {
1837 varnamebuflen = 0;
1838 return NULL;
1839 }
1840 varnamebuflen = len;
1841 }
1842 *varnamebuf = prefix;
1843 varnamebuf[1] = ':';
1844 STRCPY(varnamebuf + 2, name);
1845 return varnamebuf;
1846}
1847
1848/*
1849 * Function given to ExpandGeneric() to obtain the list of user defined
1850 * (global/buffer/window/built-in) variable names.
1851 */
1852 char_u *
1853get_user_var_name(expand_T *xp, int idx)
1854{
1855 static long_u gdone;
1856 static long_u bdone;
1857 static long_u wdone;
1858 static long_u tdone;
1859 static int vidx;
1860 static hashitem_T *hi;
1861 hashtab_T *ht;
1862
1863 if (idx == 0)
1864 {
1865 gdone = bdone = wdone = vidx = 0;
1866 tdone = 0;
1867 }
1868
1869 // Global variables
1870 if (gdone < globvarht.ht_used)
1871 {
1872 if (gdone++ == 0)
1873 hi = globvarht.ht_array;
1874 else
1875 ++hi;
1876 while (HASHITEM_EMPTY(hi))
1877 ++hi;
1878 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1879 return cat_prefix_varname('g', hi->hi_key);
1880 return hi->hi_key;
1881 }
1882
1883 // b: variables
1884 ht = &curbuf->b_vars->dv_hashtab;
1885 if (bdone < ht->ht_used)
1886 {
1887 if (bdone++ == 0)
1888 hi = ht->ht_array;
1889 else
1890 ++hi;
1891 while (HASHITEM_EMPTY(hi))
1892 ++hi;
1893 return cat_prefix_varname('b', hi->hi_key);
1894 }
1895
1896 // w: variables
1897 ht = &curwin->w_vars->dv_hashtab;
1898 if (wdone < ht->ht_used)
1899 {
1900 if (wdone++ == 0)
1901 hi = ht->ht_array;
1902 else
1903 ++hi;
1904 while (HASHITEM_EMPTY(hi))
1905 ++hi;
1906 return cat_prefix_varname('w', hi->hi_key);
1907 }
1908
1909 // t: variables
1910 ht = &curtab->tp_vars->dv_hashtab;
1911 if (tdone < ht->ht_used)
1912 {
1913 if (tdone++ == 0)
1914 hi = ht->ht_array;
1915 else
1916 ++hi;
1917 while (HASHITEM_EMPTY(hi))
1918 ++hi;
1919 return cat_prefix_varname('t', hi->hi_key);
1920 }
1921
1922 // v: variables
1923 if (vidx < VV_LEN)
1924 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1925
1926 VIM_CLEAR(varnamebuf);
1927 varnamebuflen = 0;
1928 return NULL;
1929}
1930
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001931 char *
1932get_var_special_name(int nr)
1933{
1934 switch (nr)
1935 {
1936 case VVAL_FALSE: return "v:false";
1937 case VVAL_TRUE: return "v:true";
1938 case VVAL_NONE: return "v:none";
1939 case VVAL_NULL: return "v:null";
1940 }
1941 internal_error("get_var_special_name()");
1942 return "42";
1943}
1944
1945/*
1946 * Returns the global variable dictionary
1947 */
1948 dict_T *
1949get_globvar_dict(void)
1950{
1951 return &globvardict;
1952}
1953
1954/*
1955 * Returns the global variable hash table
1956 */
1957 hashtab_T *
1958get_globvar_ht(void)
1959{
1960 return &globvarht;
1961}
1962
1963/*
1964 * Returns the v: variable dictionary
1965 */
1966 dict_T *
1967get_vimvar_dict(void)
1968{
1969 return &vimvardict;
1970}
1971
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001972/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001974 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 */
1976 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001977find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001979 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1980 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981
1982 if (di == NULL)
1983 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001984 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1986 return (int)(vv - vimvars);
1987}
1988
1989
1990/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001991 * Set type of v: variable to "type".
1992 */
1993 void
1994set_vim_var_type(int idx, vartype_T type)
1995{
1996 vimvars[idx].vv_type = type;
1997}
1998
1999/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002000 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002001 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002002 */
2003 void
2004set_vim_var_nr(int idx, varnumber_T val)
2005{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002006 vimvars[idx].vv_nr = val;
2007}
2008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 char *
2010get_vim_var_name(int idx)
2011{
2012 return vimvars[idx].vv_name;
2013}
2014
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002015/*
2016 * Get typval_T v: variable value.
2017 */
2018 typval_T *
2019get_vim_var_tv(int idx)
2020{
2021 return &vimvars[idx].vv_tv;
2022}
2023
2024/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002025 * Set v: variable to "tv". Only accepts the same type.
2026 * Takes over the value of "tv".
2027 */
2028 int
2029set_vim_var_tv(int idx, typval_T *tv)
2030{
2031 if (vimvars[idx].vv_type != tv->v_type)
2032 {
2033 emsg(_("E1063: type mismatch for v: variable"));
2034 clear_tv(tv);
2035 return FAIL;
2036 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002037 // VV_RO is also checked when compiling, but let's check here as well.
2038 if (vimvars[idx].vv_flags & VV_RO)
2039 {
2040 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2041 return FAIL;
2042 }
2043 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2044 {
2045 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2046 return FAIL;
2047 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002048 clear_tv(&vimvars[idx].vv_di.di_tv);
2049 vimvars[idx].vv_di.di_tv = *tv;
2050 return OK;
2051}
2052
2053/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002054 * Get number v: variable value.
2055 */
2056 varnumber_T
2057get_vim_var_nr(int idx)
2058{
2059 return vimvars[idx].vv_nr;
2060}
2061
2062/*
2063 * Get string v: variable value. Uses a static buffer, can only be used once.
2064 * If the String variable has never been set, return an empty string.
2065 * Never returns NULL;
2066 */
2067 char_u *
2068get_vim_var_str(int idx)
2069{
2070 return tv_get_string(&vimvars[idx].vv_tv);
2071}
2072
2073/*
2074 * Get List v: variable value. Caller must take care of reference count when
2075 * needed.
2076 */
2077 list_T *
2078get_vim_var_list(int idx)
2079{
2080 return vimvars[idx].vv_list;
2081}
2082
2083/*
2084 * Get Dict v: variable value. Caller must take care of reference count when
2085 * needed.
2086 */
2087 dict_T *
2088get_vim_var_dict(int idx)
2089{
2090 return vimvars[idx].vv_dict;
2091}
2092
2093/*
2094 * Set v:char to character "c".
2095 */
2096 void
2097set_vim_var_char(int c)
2098{
2099 char_u buf[MB_MAXBYTES + 1];
2100
2101 if (has_mbyte)
2102 buf[(*mb_char2bytes)(c, buf)] = NUL;
2103 else
2104 {
2105 buf[0] = c;
2106 buf[1] = NUL;
2107 }
2108 set_vim_var_string(VV_CHAR, buf, -1);
2109}
2110
2111/*
2112 * Set v:count to "count" and v:count1 to "count1".
2113 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2114 */
2115 void
2116set_vcount(
2117 long count,
2118 long count1,
2119 int set_prevcount)
2120{
2121 if (set_prevcount)
2122 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2123 vimvars[VV_COUNT].vv_nr = count;
2124 vimvars[VV_COUNT1].vv_nr = count1;
2125}
2126
2127/*
2128 * Save variables that might be changed as a side effect. Used when executing
2129 * a timer callback.
2130 */
2131 void
2132save_vimvars(vimvars_save_T *vvsave)
2133{
2134 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2135 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2136 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2137}
2138
2139/*
2140 * Restore variables saved by save_vimvars().
2141 */
2142 void
2143restore_vimvars(vimvars_save_T *vvsave)
2144{
2145 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2146 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2147 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2148}
2149
2150/*
2151 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2152 * value.
2153 */
2154 void
2155set_vim_var_string(
2156 int idx,
2157 char_u *val,
2158 int len) // length of "val" to use or -1 (whole string)
2159{
2160 clear_tv(&vimvars[idx].vv_di.di_tv);
2161 vimvars[idx].vv_type = VAR_STRING;
2162 if (val == NULL)
2163 vimvars[idx].vv_str = NULL;
2164 else if (len == -1)
2165 vimvars[idx].vv_str = vim_strsave(val);
2166 else
2167 vimvars[idx].vv_str = vim_strnsave(val, len);
2168}
2169
2170/*
2171 * Set List v: variable to "val".
2172 */
2173 void
2174set_vim_var_list(int idx, list_T *val)
2175{
2176 clear_tv(&vimvars[idx].vv_di.di_tv);
2177 vimvars[idx].vv_type = VAR_LIST;
2178 vimvars[idx].vv_list = val;
2179 if (val != NULL)
2180 ++val->lv_refcount;
2181}
2182
2183/*
2184 * Set Dictionary v: variable to "val".
2185 */
2186 void
2187set_vim_var_dict(int idx, dict_T *val)
2188{
2189 clear_tv(&vimvars[idx].vv_di.di_tv);
2190 vimvars[idx].vv_type = VAR_DICT;
2191 vimvars[idx].vv_dict = val;
2192 if (val != NULL)
2193 {
2194 ++val->dv_refcount;
2195 dict_set_items_ro(val);
2196 }
2197}
2198
2199/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002200 * Set the v:argv list.
2201 */
2202 void
2203set_argv_var(char **argv, int argc)
2204{
2205 list_T *l = list_alloc();
2206 int i;
2207
2208 if (l == NULL)
2209 getout(1);
2210 l->lv_lock = VAR_FIXED;
2211 for (i = 0; i < argc; ++i)
2212 {
2213 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2214 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002215 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002216 }
2217 set_vim_var_list(VV_ARGV, l);
2218}
2219
2220/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002221 * Reset v:register, taking the 'clipboard' setting into account.
2222 */
2223 void
2224reset_reg_var(void)
2225{
2226 int regname = 0;
2227
2228 // Adjust the register according to 'clipboard', so that when
2229 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2230#ifdef FEAT_CLIPBOARD
2231 adjust_clip_reg(&regname);
2232#endif
2233 set_reg_var(regname);
2234}
2235
2236/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002237 * Set v:register if needed.
2238 */
2239 void
2240set_reg_var(int c)
2241{
2242 char_u regname;
2243
2244 if (c == 0 || c == ' ')
2245 regname = '"';
2246 else
2247 regname = c;
2248 // Avoid free/alloc when the value is already right.
2249 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2250 set_vim_var_string(VV_REG, &regname, 1);
2251}
2252
2253/*
2254 * Get or set v:exception. If "oldval" == NULL, return the current value.
2255 * Otherwise, restore the value to "oldval" and return NULL.
2256 * Must always be called in pairs to save and restore v:exception! Does not
2257 * take care of memory allocations.
2258 */
2259 char_u *
2260v_exception(char_u *oldval)
2261{
2262 if (oldval == NULL)
2263 return vimvars[VV_EXCEPTION].vv_str;
2264
2265 vimvars[VV_EXCEPTION].vv_str = oldval;
2266 return NULL;
2267}
2268
2269/*
2270 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2271 * Otherwise, restore the value to "oldval" and return NULL.
2272 * Must always be called in pairs to save and restore v:throwpoint! Does not
2273 * take care of memory allocations.
2274 */
2275 char_u *
2276v_throwpoint(char_u *oldval)
2277{
2278 if (oldval == NULL)
2279 return vimvars[VV_THROWPOINT].vv_str;
2280
2281 vimvars[VV_THROWPOINT].vv_str = oldval;
2282 return NULL;
2283}
2284
2285/*
2286 * Set v:cmdarg.
2287 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2288 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2289 * Must always be called in pairs!
2290 */
2291 char_u *
2292set_cmdarg(exarg_T *eap, char_u *oldarg)
2293{
2294 char_u *oldval;
2295 char_u *newval;
2296 unsigned len;
2297
2298 oldval = vimvars[VV_CMDARG].vv_str;
2299 if (eap == NULL)
2300 {
2301 vim_free(oldval);
2302 vimvars[VV_CMDARG].vv_str = oldarg;
2303 return NULL;
2304 }
2305
2306 if (eap->force_bin == FORCE_BIN)
2307 len = 6;
2308 else if (eap->force_bin == FORCE_NOBIN)
2309 len = 8;
2310 else
2311 len = 0;
2312
2313 if (eap->read_edit)
2314 len += 7;
2315
2316 if (eap->force_ff != 0)
2317 len += 10; // " ++ff=unix"
2318 if (eap->force_enc != 0)
2319 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2320 if (eap->bad_char != 0)
2321 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2322
2323 newval = alloc(len + 1);
2324 if (newval == NULL)
2325 return NULL;
2326
2327 if (eap->force_bin == FORCE_BIN)
2328 sprintf((char *)newval, " ++bin");
2329 else if (eap->force_bin == FORCE_NOBIN)
2330 sprintf((char *)newval, " ++nobin");
2331 else
2332 *newval = NUL;
2333
2334 if (eap->read_edit)
2335 STRCAT(newval, " ++edit");
2336
2337 if (eap->force_ff != 0)
2338 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2339 eap->force_ff == 'u' ? "unix"
2340 : eap->force_ff == 'd' ? "dos"
2341 : "mac");
2342 if (eap->force_enc != 0)
2343 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2344 eap->cmd + eap->force_enc);
2345 if (eap->bad_char == BAD_KEEP)
2346 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2347 else if (eap->bad_char == BAD_DROP)
2348 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2349 else if (eap->bad_char != 0)
2350 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2351 vimvars[VV_CMDARG].vv_str = newval;
2352 return oldval;
2353}
2354
2355/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002356 * Get the value of internal variable "name".
2357 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2358 */
2359 int
2360get_var_tv(
2361 char_u *name,
2362 int len, // length of "name"
2363 typval_T *rettv, // NULL when only checking existence
2364 dictitem_T **dip, // non-NULL when typval's dict item is needed
2365 int verbose, // may give error message
2366 int no_autoload) // do not use script autoloading
2367{
2368 int ret = OK;
2369 typval_T *tv = NULL;
2370 dictitem_T *v;
2371 int cc;
2372
2373 // truncate the name, so that we can use strcmp()
2374 cc = name[len];
2375 name[len] = NUL;
2376
2377 // Check for user-defined variables.
2378 v = find_var(name, NULL, no_autoload);
2379 if (v != NULL)
2380 {
2381 tv = &v->di_tv;
2382 if (dip != NULL)
2383 *dip = v;
2384 }
2385
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002386 if (tv == NULL && (current_sctx.sc_version == SCRIPT_VERSION_VIM9
2387 || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002389 imported_T *import;
2390 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2391
2392 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393
2394 // imported variable from another script
2395 if (import != NULL)
2396 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002397 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2399 + import->imp_var_vals_idx;
2400 tv = sv->sv_tv;
2401 }
2402 }
2403
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002404 if (tv == NULL)
2405 {
2406 if (rettv != NULL && verbose)
2407 semsg(_(e_undefvar), name);
2408 ret = FAIL;
2409 }
2410 else if (rettv != NULL)
2411 copy_tv(tv, rettv);
2412
2413 name[len] = cc;
2414
2415 return ret;
2416}
2417
2418/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002419 * Check if variable "name[len]" is a local variable or an argument.
2420 * If so, "*eval_lavars_used" is set to TRUE.
2421 */
2422 void
2423check_vars(char_u *name, int len)
2424{
2425 int cc;
2426 char_u *varname;
2427 hashtab_T *ht;
2428
2429 if (eval_lavars_used == NULL)
2430 return;
2431
2432 // truncate the name, so that we can use strcmp()
2433 cc = name[len];
2434 name[len] = NUL;
2435
2436 ht = find_var_ht(name, &varname);
2437 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2438 {
2439 if (find_var(name, NULL, TRUE) != NULL)
2440 *eval_lavars_used = TRUE;
2441 }
2442
2443 name[len] = cc;
2444}
2445
2446/*
2447 * Find variable "name" in the list of variables.
2448 * Return a pointer to it if found, NULL if not found.
2449 * Careful: "a:0" variables don't have a name.
2450 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2451 * hashtab_T used.
2452 */
2453 dictitem_T *
2454find_var(char_u *name, hashtab_T **htp, int no_autoload)
2455{
2456 char_u *varname;
2457 hashtab_T *ht;
2458 dictitem_T *ret = NULL;
2459
2460 ht = find_var_ht(name, &varname);
2461 if (htp != NULL)
2462 *htp = ht;
2463 if (ht == NULL)
2464 return NULL;
2465 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2466 if (ret != NULL)
2467 return ret;
2468
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002469 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002470 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2471}
2472
2473/*
2474 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002475 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002476 * Returns NULL if not found.
2477 */
2478 dictitem_T *
2479find_var_in_ht(
2480 hashtab_T *ht,
2481 int htname,
2482 char_u *varname,
2483 int no_autoload)
2484{
2485 hashitem_T *hi;
2486
2487 if (*varname == NUL)
2488 {
2489 // Must be something like "s:", otherwise "ht" would be NULL.
2490 switch (htname)
2491 {
2492 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2493 case 'g': return &globvars_var;
2494 case 'v': return &vimvars_var;
2495 case 'b': return &curbuf->b_bufvar;
2496 case 'w': return &curwin->w_winvar;
2497 case 't': return &curtab->tp_winvar;
2498 case 'l': return get_funccal_local_var();
2499 case 'a': return get_funccal_args_var();
2500 }
2501 return NULL;
2502 }
2503
2504 hi = hash_find(ht, varname);
2505 if (HASHITEM_EMPTY(hi))
2506 {
2507 // For global variables we may try auto-loading the script. If it
2508 // worked find the variable again. Don't auto-load a script if it was
2509 // loaded already, otherwise it would be loaded every time when
2510 // checking if a function name is a Funcref variable.
2511 if (ht == &globvarht && !no_autoload)
2512 {
2513 // Note: script_autoload() may make "hi" invalid. It must either
2514 // be obtained again or not used.
2515 if (!script_autoload(varname, FALSE) || aborting())
2516 return NULL;
2517 hi = hash_find(ht, varname);
2518 }
2519 if (HASHITEM_EMPTY(hi))
2520 return NULL;
2521 }
2522 return HI2DI(hi);
2523}
2524
2525/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 * Get the script-local hashtab. NULL if not in a script context.
2527 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002528 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529get_script_local_ht(void)
2530{
2531 scid_T sid = current_sctx.sc_sid;
2532
2533 if (sid > 0 && sid <= script_items.ga_len)
2534 return &SCRIPT_VARS(sid);
2535 return NULL;
2536}
2537
2538/*
2539 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002540 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002542 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2544{
2545 hashtab_T *ht = get_script_local_ht();
2546 char_u buffer[30];
2547 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002548 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 hashitem_T *hi;
2550
2551 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002552 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 if (len < sizeof(buffer) - 1)
2554 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002555 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 vim_strncpy(buffer, name, len);
2557 p = buffer;
2558 }
2559 else
2560 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002561 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002563 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 }
2565
2566 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002567 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568
2569 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002570 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2571 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572
2573 if (p != buffer)
2574 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002575 // Don't return "buffer", gcc complains.
2576 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577}
2578
2579/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002580 * Find the hashtab used for a variable name.
2581 * Return NULL if the name is not valid.
2582 * Set "varname" to the start of name without ':'.
2583 */
2584 hashtab_T *
2585find_var_ht(char_u *name, char_u **varname)
2586{
2587 hashitem_T *hi;
2588 hashtab_T *ht;
2589
2590 if (name[0] == NUL)
2591 return NULL;
2592 if (name[1] != ':')
2593 {
2594 // The name must not start with a colon or #.
2595 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2596 return NULL;
2597 *varname = name;
2598
2599 // "version" is "v:version" in all scopes if scriptversion < 3.
2600 // Same for a few other variables marked with VV_COMPAT.
2601 if (current_sctx.sc_version < 3)
2602 {
2603 hi = hash_find(&compat_hashtab, name);
2604 if (!HASHITEM_EMPTY(hi))
2605 return &compat_hashtab;
2606 }
2607
2608 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 if (ht != NULL)
2610 return ht; // local variable
2611
2612 // in Vim9 script items at the script level are script-local
2613 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2614 {
2615 ht = get_script_local_ht();
2616 if (ht != NULL)
2617 return ht;
2618 }
2619
2620 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002621 }
2622 *varname = name + 2;
2623 if (*name == 'g') // global variable
2624 return &globvarht;
2625 // There must be no ':' or '#' in the rest of the name, unless g: is used
2626 if (vim_strchr(name + 2, ':') != NULL
2627 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2628 return NULL;
2629 if (*name == 'b') // buffer variable
2630 return &curbuf->b_vars->dv_hashtab;
2631 if (*name == 'w') // window variable
2632 return &curwin->w_vars->dv_hashtab;
2633 if (*name == 't') // tab page variable
2634 return &curtab->tp_vars->dv_hashtab;
2635 if (*name == 'v') // v: variable
2636 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002637 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002638 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002640 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 if (*name == 'a') // a: function argument
2642 return get_funccal_args_ht();
2643 if (*name == 'l') // l: local function variable
2644 return get_funccal_local_ht();
2645 }
2646 if (*name == 's') // script variable
2647 {
2648 ht = get_script_local_ht();
2649 if (ht != NULL)
2650 return ht;
2651 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002652 return NULL;
2653}
2654
2655/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002656 * Get the string value of a (global/local) variable.
2657 * Note: see tv_get_string() for how long the pointer remains valid.
2658 * Returns NULL when it doesn't exist.
2659 */
2660 char_u *
2661get_var_value(char_u *name)
2662{
2663 dictitem_T *v;
2664
2665 v = find_var(name, NULL, FALSE);
2666 if (v == NULL)
2667 return NULL;
2668 return tv_get_string(&v->di_tv);
2669}
2670
2671/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002672 * Allocate a new hashtab for a sourced script. It will be used while
2673 * sourcing this script and when executing functions defined in the script.
2674 */
2675 void
2676new_script_vars(scid_T id)
2677{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002678 scriptvar_T *sv;
2679
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002680 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2681 if (sv == NULL)
2682 return;
2683 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002684 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002685}
2686
2687/*
2688 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2689 * point to it.
2690 */
2691 void
2692init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2693{
2694 hash_init(&dict->dv_hashtab);
2695 dict->dv_lock = 0;
2696 dict->dv_scope = scope;
2697 dict->dv_refcount = DO_NOT_FREE_CNT;
2698 dict->dv_copyID = 0;
2699 dict_var->di_tv.vval.v_dict = dict;
2700 dict_var->di_tv.v_type = VAR_DICT;
2701 dict_var->di_tv.v_lock = VAR_FIXED;
2702 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2703 dict_var->di_key[0] = NUL;
2704}
2705
2706/*
2707 * Unreference a dictionary initialized by init_var_dict().
2708 */
2709 void
2710unref_var_dict(dict_T *dict)
2711{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002712 // Now the dict needs to be freed if no one else is using it, go back to
2713 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002714 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2715 dict_unref(dict);
2716}
2717
2718/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002719 * Clean up a list of internal variables.
2720 * Frees all allocated variables and the value they contain.
2721 * Clears hashtab "ht", does not free it.
2722 */
2723 void
2724vars_clear(hashtab_T *ht)
2725{
2726 vars_clear_ext(ht, TRUE);
2727}
2728
2729/*
2730 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2731 */
2732 void
2733vars_clear_ext(hashtab_T *ht, int free_val)
2734{
2735 int todo;
2736 hashitem_T *hi;
2737 dictitem_T *v;
2738
2739 hash_lock(ht);
2740 todo = (int)ht->ht_used;
2741 for (hi = ht->ht_array; todo > 0; ++hi)
2742 {
2743 if (!HASHITEM_EMPTY(hi))
2744 {
2745 --todo;
2746
2747 // Free the variable. Don't remove it from the hashtab,
2748 // ht_array might change then. hash_clear() takes care of it
2749 // later.
2750 v = HI2DI(hi);
2751 if (free_val)
2752 clear_tv(&v->di_tv);
2753 if (v->di_flags & DI_FLAGS_ALLOC)
2754 vim_free(v);
2755 }
2756 }
2757 hash_clear(ht);
2758 ht->ht_used = 0;
2759}
2760
2761/*
2762 * Delete a variable from hashtab "ht" at item "hi".
2763 * Clear the variable value and free the dictitem.
2764 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002765 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002766delete_var(hashtab_T *ht, hashitem_T *hi)
2767{
2768 dictitem_T *di = HI2DI(hi);
2769
2770 hash_remove(ht, hi);
2771 clear_tv(&di->di_tv);
2772 vim_free(di);
2773}
2774
2775/*
2776 * List the value of one internal variable.
2777 */
2778 static void
2779list_one_var(dictitem_T *v, char *prefix, int *first)
2780{
2781 char_u *tofree;
2782 char_u *s;
2783 char_u numbuf[NUMBUFLEN];
2784
2785 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2786 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2787 s == NULL ? (char_u *)"" : s, first);
2788 vim_free(tofree);
2789}
2790
2791 static void
2792list_one_var_a(
2793 char *prefix,
2794 char_u *name,
2795 int type,
2796 char_u *string,
2797 int *first) // when TRUE clear rest of screen and set to FALSE
2798{
2799 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2800 msg_start();
2801 msg_puts(prefix);
2802 if (name != NULL) // "a:" vars don't have a name stored
2803 msg_puts((char *)name);
2804 msg_putchar(' ');
2805 msg_advance(22);
2806 if (type == VAR_NUMBER)
2807 msg_putchar('#');
2808 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2809 msg_putchar('*');
2810 else if (type == VAR_LIST)
2811 {
2812 msg_putchar('[');
2813 if (*string == '[')
2814 ++string;
2815 }
2816 else if (type == VAR_DICT)
2817 {
2818 msg_putchar('{');
2819 if (*string == '{')
2820 ++string;
2821 }
2822 else
2823 msg_putchar(' ');
2824
2825 msg_outtrans(string);
2826
2827 if (type == VAR_FUNC || type == VAR_PARTIAL)
2828 msg_puts("()");
2829 if (*first)
2830 {
2831 msg_clr_eos();
2832 *first = FALSE;
2833 }
2834}
2835
2836/*
2837 * Set variable "name" to value in "tv".
2838 * If the variable already exists, the value is updated.
2839 * Otherwise the variable is created.
2840 */
2841 void
2842set_var(
2843 char_u *name,
2844 typval_T *tv,
2845 int copy) // make copy of value in "tv"
2846{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002848}
2849
2850/*
2851 * Set variable "name" to value in "tv".
2852 * If the variable already exists and "is_const" is FALSE the value is updated.
2853 * Otherwise the variable is created.
2854 */
2855 void
2856set_var_const(
2857 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002859 typval_T *tv,
2860 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002862{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002864 char_u *varname;
2865 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867
2868 ht = find_var_ht(name, &varname);
2869 if (ht == NULL || *varname == NUL)
2870 {
2871 semsg(_(e_illvar), name);
2872 return;
2873 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002874 is_script_local = ht == get_script_local_ht();
2875
Bram Moolenaar67979662020-06-20 22:50:47 +02002876 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002877 && !is_script_local
2878 && (flags & LET_NO_COMMAND) == 0
2879 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002880 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002881 vim9_declare_error(name);
Bram Moolenaar67979662020-06-20 22:50:47 +02002882 return;
2883 }
2884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002886
2887 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 if (di == NULL)
2889 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002890
2891 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893 return;
2894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002896 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002898 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 if (flags & LET_IS_CONST)
2900 {
2901 emsg(_(e_cannot_mod));
2902 return;
2903 }
2904
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002905 if (is_script_local
2906 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002908 if ((flags & LET_NO_COMMAND) == 0)
2909 {
2910 semsg(_("E1041: Redefining script item %s"), name);
2911 return;
2912 }
2913
2914 // check the type
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002915 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
2916 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002918
2919 if (var_check_ro(di->di_flags, name, FALSE)
2920 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2921 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 else
2924 // can only redefine once
2925 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002926
2927 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002929 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002930 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002931 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002932 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002934 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002936 if (copy || tv->v_type != VAR_STRING)
2937 {
2938 char_u *val = tv_get_string(tv);
2939
2940 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002941 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 if (di->di_tv.vval.v_string == NULL)
2943 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002944 }
2945 else
2946 {
2947 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002949 tv->vval.v_string = NULL;
2950 }
2951 return;
2952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002954 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002956 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002958#ifdef FEAT_SEARCH_EXTRA
2959 else if (STRCMP(varname, "hlsearch") == 0)
2960 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002962 redraw_all_later(SOME_VALID);
2963 }
2964#endif
2965 return;
2966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002968 {
2969 semsg(_("E963: setting %s to value with wrong type"), name);
2970 return;
2971 }
2972 }
2973
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002975 }
2976 else // add a new variable
2977 {
2978 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002979 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002980 {
2981 semsg(_(e_illvar), name);
2982 return;
2983 }
2984
2985 // Make sure the variable name is valid.
2986 if (!valid_varname(varname))
2987 return;
2988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2990 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002991 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 STRCPY(di->di_key, varname);
2993 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002996 return;
2997 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002998 di->di_flags = DI_FLAGS_ALLOC;
2999 if (flags & LET_IS_CONST)
3000 di->di_flags |= DI_FLAGS_LOCK;
3001
3002 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
3003 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003004 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005
3006 // Store a pointer to the typval_T, so that it can be found by
3007 // index instead of using a hastab lookup.
3008 if (ga_grow(&si->sn_var_vals, 1) == OK)
3009 {
3010 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3011 + si->sn_var_vals.ga_len;
3012 sv->sv_name = di->di_key;
3013 sv->sv_tv = &di->di_tv;
3014 sv->sv_type = type == NULL ? &t_any : type;
3015 sv->sv_const = (flags & LET_IS_CONST);
3016 sv->sv_export = is_export;
3017 ++si->sn_var_vals.ga_len;
3018
3019 // let ex_export() know the export worked.
3020 is_export = FALSE;
3021 }
3022 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003023 }
3024
3025 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003027 else
3028 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 di->di_tv = *tv;
3030 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003031 init_tv(tv);
3032 }
3033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 if (flags & LET_IS_CONST)
3035 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003036}
3037
3038/*
3039 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3040 * Also give an error message.
3041 */
3042 int
3043var_check_ro(int flags, char_u *name, int use_gettext)
3044{
3045 if (flags & DI_FLAGS_RO)
3046 {
3047 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3048 return TRUE;
3049 }
3050 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3051 {
3052 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3053 return TRUE;
3054 }
3055 return FALSE;
3056}
3057
3058/*
3059 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3060 * Also give an error message.
3061 */
3062 int
3063var_check_fixed(int flags, char_u *name, int use_gettext)
3064{
3065 if (flags & DI_FLAGS_FIX)
3066 {
3067 semsg(_("E795: Cannot delete variable %s"),
3068 use_gettext ? (char_u *)_(name) : name);
3069 return TRUE;
3070 }
3071 return FALSE;
3072}
3073
3074/*
3075 * Check if a funcref is assigned to a valid variable name.
3076 * Return TRUE and give an error if not.
3077 */
3078 int
3079var_check_func_name(
3080 char_u *name, // points to start of variable name
3081 int new_var) // TRUE when creating the variable
3082{
3083 // Allow for w: b: s: and t:.
3084 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3085 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3086 ? name[2] : name[0]))
3087 {
3088 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3089 name);
3090 return TRUE;
3091 }
3092 // Don't allow hiding a function. When "v" is not NULL we might be
3093 // assigning another function to the same var, the type is checked
3094 // below.
3095 if (new_var && function_exists(name, FALSE))
3096 {
3097 semsg(_("E705: Variable name conflicts with existing function: %s"),
3098 name);
3099 return TRUE;
3100 }
3101 return FALSE;
3102}
3103
3104/*
3105 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3106 * Also give an error message, using "name" or _("name") when use_gettext is
3107 * TRUE.
3108 */
3109 int
3110var_check_lock(int lock, char_u *name, int use_gettext)
3111{
3112 if (lock & VAR_LOCKED)
3113 {
3114 semsg(_("E741: Value is locked: %s"),
3115 name == NULL ? (char_u *)_("Unknown")
3116 : use_gettext ? (char_u *)_(name)
3117 : name);
3118 return TRUE;
3119 }
3120 if (lock & VAR_FIXED)
3121 {
3122 semsg(_("E742: Cannot change value of %s"),
3123 name == NULL ? (char_u *)_("Unknown")
3124 : use_gettext ? (char_u *)_(name)
3125 : name);
3126 return TRUE;
3127 }
3128 return FALSE;
3129}
3130
3131/*
3132 * Check if a variable name is valid.
3133 * Return FALSE and give an error if not.
3134 */
3135 int
3136valid_varname(char_u *varname)
3137{
3138 char_u *p;
3139
3140 for (p = varname; *p != NUL; ++p)
3141 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3142 && *p != AUTOLOAD_CHAR)
3143 {
3144 semsg(_(e_illvar), varname);
3145 return FALSE;
3146 }
3147 return TRUE;
3148}
3149
3150/*
3151 * getwinvar() and gettabwinvar()
3152 */
3153 static void
3154getwinvar(
3155 typval_T *argvars,
3156 typval_T *rettv,
3157 int off) // 1 for gettabwinvar()
3158{
3159 win_T *win;
3160 char_u *varname;
3161 dictitem_T *v;
3162 tabpage_T *tp = NULL;
3163 int done = FALSE;
3164 win_T *oldcurwin;
3165 tabpage_T *oldtabpage;
3166 int need_switch_win;
3167
3168 if (off == 1)
3169 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3170 else
3171 tp = curtab;
3172 win = find_win_by_nr(&argvars[off], tp);
3173 varname = tv_get_string_chk(&argvars[off + 1]);
3174 ++emsg_off;
3175
3176 rettv->v_type = VAR_STRING;
3177 rettv->vval.v_string = NULL;
3178
3179 if (win != NULL && varname != NULL)
3180 {
3181 // Set curwin to be our win, temporarily. Also set the tabpage,
3182 // otherwise the window is not valid. Only do this when needed,
3183 // autocommands get blocked.
3184 need_switch_win = !(tp == curtab && win == curwin);
3185 if (!need_switch_win
3186 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3187 {
3188 if (*varname == '&')
3189 {
3190 if (varname[1] == NUL)
3191 {
3192 // get all window-local options in a dict
3193 dict_T *opts = get_winbuf_options(FALSE);
3194
3195 if (opts != NULL)
3196 {
3197 rettv_dict_set(rettv, opts);
3198 done = TRUE;
3199 }
3200 }
3201 else if (get_option_tv(&varname, rettv, 1) == OK)
3202 // window-local-option
3203 done = TRUE;
3204 }
3205 else
3206 {
3207 // Look up the variable.
3208 // Let getwinvar({nr}, "") return the "w:" dictionary.
3209 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3210 varname, FALSE);
3211 if (v != NULL)
3212 {
3213 copy_tv(&v->di_tv, rettv);
3214 done = TRUE;
3215 }
3216 }
3217 }
3218
3219 if (need_switch_win)
3220 // restore previous notion of curwin
3221 restore_win(oldcurwin, oldtabpage, TRUE);
3222 }
3223
3224 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3225 // use the default return value
3226 copy_tv(&argvars[off + 2], rettv);
3227
3228 --emsg_off;
3229}
3230
3231/*
3232 * "setwinvar()" and "settabwinvar()" functions
3233 */
3234 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003235setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003236{
3237 win_T *win;
3238 win_T *save_curwin;
3239 tabpage_T *save_curtab;
3240 int need_switch_win;
3241 char_u *varname, *winvarname;
3242 typval_T *varp;
3243 char_u nbuf[NUMBUFLEN];
3244 tabpage_T *tp = NULL;
3245
3246 if (check_secure())
3247 return;
3248
3249 if (off == 1)
3250 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3251 else
3252 tp = curtab;
3253 win = find_win_by_nr(&argvars[off], tp);
3254 varname = tv_get_string_chk(&argvars[off + 1]);
3255 varp = &argvars[off + 2];
3256
3257 if (win != NULL && varname != NULL && varp != NULL)
3258 {
3259 need_switch_win = !(tp == curtab && win == curwin);
3260 if (!need_switch_win
3261 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3262 {
3263 if (*varname == '&')
3264 {
3265 long numval;
3266 char_u *strval;
3267 int error = FALSE;
3268
3269 ++varname;
3270 numval = (long)tv_get_number_chk(varp, &error);
3271 strval = tv_get_string_buf_chk(varp, nbuf);
3272 if (!error && strval != NULL)
3273 set_option_value(varname, numval, strval, OPT_LOCAL);
3274 }
3275 else
3276 {
3277 winvarname = alloc(STRLEN(varname) + 3);
3278 if (winvarname != NULL)
3279 {
3280 STRCPY(winvarname, "w:");
3281 STRCPY(winvarname + 2, varname);
3282 set_var(winvarname, varp, TRUE);
3283 vim_free(winvarname);
3284 }
3285 }
3286 }
3287 if (need_switch_win)
3288 restore_win(save_curwin, save_curtab, TRUE);
3289 }
3290}
3291
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003292/*
3293 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3294 * v:option_type, and v:option_command.
3295 */
3296 void
3297reset_v_option_vars(void)
3298{
3299 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3300 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3301 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3302 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3303 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3304 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3305}
3306
3307/*
3308 * Add an assert error to v:errors.
3309 */
3310 void
3311assert_error(garray_T *gap)
3312{
3313 struct vimvar *vp = &vimvars[VV_ERRORS];
3314
3315 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003316 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003317 set_vim_var_list(VV_ERRORS, list_alloc());
3318 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3319}
3320
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003321 int
3322var_exists(char_u *var)
3323{
3324 char_u *name;
3325 char_u *tofree;
3326 typval_T tv;
3327 int len = 0;
3328 int n = FALSE;
3329
3330 // get_name_len() takes care of expanding curly braces
3331 name = var;
3332 len = get_name_len(&var, &tofree, TRUE, FALSE);
3333 if (len > 0)
3334 {
3335 if (tofree != NULL)
3336 name = tofree;
3337 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3338 if (n)
3339 {
3340 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003341 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3342 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003343 if (n)
3344 clear_tv(&tv);
3345 }
3346 }
3347 if (*var != NUL)
3348 n = FALSE;
3349
3350 vim_free(tofree);
3351 return n;
3352}
3353
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003354static lval_T *redir_lval = NULL;
3355#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3356static garray_T redir_ga; // only valid when redir_lval is not NULL
3357static char_u *redir_endp = NULL;
3358static char_u *redir_varname = NULL;
3359
3360/*
3361 * Start recording command output to a variable
3362 * When "append" is TRUE append to an existing variable.
3363 * Returns OK if successfully completed the setup. FAIL otherwise.
3364 */
3365 int
3366var_redir_start(char_u *name, int append)
3367{
3368 int save_emsg;
3369 int err;
3370 typval_T tv;
3371
3372 // Catch a bad name early.
3373 if (!eval_isnamec1(*name))
3374 {
3375 emsg(_(e_invarg));
3376 return FAIL;
3377 }
3378
3379 // Make a copy of the name, it is used in redir_lval until redir ends.
3380 redir_varname = vim_strsave(name);
3381 if (redir_varname == NULL)
3382 return FAIL;
3383
3384 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3385 if (redir_lval == NULL)
3386 {
3387 var_redir_stop();
3388 return FAIL;
3389 }
3390
3391 // The output is stored in growarray "redir_ga" until redirection ends.
3392 ga_init2(&redir_ga, (int)sizeof(char), 500);
3393
3394 // Parse the variable name (can be a dict or list entry).
3395 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3396 FNE_CHECK_START);
3397 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3398 {
3399 clear_lval(redir_lval);
3400 if (redir_endp != NULL && *redir_endp != NUL)
3401 // Trailing characters are present after the variable name
3402 emsg(_(e_trailing));
3403 else
3404 emsg(_(e_invarg));
3405 redir_endp = NULL; // don't store a value, only cleanup
3406 var_redir_stop();
3407 return FAIL;
3408 }
3409
3410 // check if we can write to the variable: set it to or append an empty
3411 // string
3412 save_emsg = did_emsg;
3413 did_emsg = FALSE;
3414 tv.v_type = VAR_STRING;
3415 tv.vval.v_string = (char_u *)"";
3416 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003418 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003420 clear_lval(redir_lval);
3421 err = did_emsg;
3422 did_emsg |= save_emsg;
3423 if (err)
3424 {
3425 redir_endp = NULL; // don't store a value, only cleanup
3426 var_redir_stop();
3427 return FAIL;
3428 }
3429
3430 return OK;
3431}
3432
3433/*
3434 * Append "value[value_len]" to the variable set by var_redir_start().
3435 * The actual appending is postponed until redirection ends, because the value
3436 * appended may in fact be the string we write to, changing it may cause freed
3437 * memory to be used:
3438 * :redir => foo
3439 * :let foo
3440 * :redir END
3441 */
3442 void
3443var_redir_str(char_u *value, int value_len)
3444{
3445 int len;
3446
3447 if (redir_lval == NULL)
3448 return;
3449
3450 if (value_len == -1)
3451 len = (int)STRLEN(value); // Append the entire string
3452 else
3453 len = value_len; // Append only "value_len" characters
3454
3455 if (ga_grow(&redir_ga, len) == OK)
3456 {
3457 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3458 redir_ga.ga_len += len;
3459 }
3460 else
3461 var_redir_stop();
3462}
3463
3464/*
3465 * Stop redirecting command output to a variable.
3466 * Frees the allocated memory.
3467 */
3468 void
3469var_redir_stop(void)
3470{
3471 typval_T tv;
3472
3473 if (EVALCMD_BUSY)
3474 {
3475 redir_lval = NULL;
3476 return;
3477 }
3478
3479 if (redir_lval != NULL)
3480 {
3481 // If there was no error: assign the text to the variable.
3482 if (redir_endp != NULL)
3483 {
3484 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3485 tv.v_type = VAR_STRING;
3486 tv.vval.v_string = redir_ga.ga_data;
3487 // Call get_lval() again, if it's inside a Dict or List it may
3488 // have changed.
3489 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3490 FALSE, FALSE, 0, FNE_CHECK_START);
3491 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003493 (char_u *)".");
3494 clear_lval(redir_lval);
3495 }
3496
3497 // free the collected output
3498 VIM_CLEAR(redir_ga.ga_data);
3499
3500 VIM_CLEAR(redir_lval);
3501 }
3502 VIM_CLEAR(redir_varname);
3503}
3504
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003505/*
3506 * "gettabvar()" function
3507 */
3508 void
3509f_gettabvar(typval_T *argvars, typval_T *rettv)
3510{
3511 win_T *oldcurwin;
3512 tabpage_T *tp, *oldtabpage;
3513 dictitem_T *v;
3514 char_u *varname;
3515 int done = FALSE;
3516
3517 rettv->v_type = VAR_STRING;
3518 rettv->vval.v_string = NULL;
3519
3520 varname = tv_get_string_chk(&argvars[1]);
3521 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3522 if (tp != NULL && varname != NULL)
3523 {
3524 // Set tp to be our tabpage, temporarily. Also set the window to the
3525 // first window in the tabpage, otherwise the window is not valid.
3526 if (switch_win(&oldcurwin, &oldtabpage,
3527 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3528 : tp->tp_firstwin, tp, TRUE) == OK)
3529 {
3530 // look up the variable
3531 // Let gettabvar({nr}, "") return the "t:" dictionary.
3532 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3533 if (v != NULL)
3534 {
3535 copy_tv(&v->di_tv, rettv);
3536 done = TRUE;
3537 }
3538 }
3539
3540 // restore previous notion of curwin
3541 restore_win(oldcurwin, oldtabpage, TRUE);
3542 }
3543
3544 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3545 copy_tv(&argvars[2], rettv);
3546}
3547
3548/*
3549 * "gettabwinvar()" function
3550 */
3551 void
3552f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3553{
3554 getwinvar(argvars, rettv, 1);
3555}
3556
3557/*
3558 * "getwinvar()" function
3559 */
3560 void
3561f_getwinvar(typval_T *argvars, typval_T *rettv)
3562{
3563 getwinvar(argvars, rettv, 0);
3564}
3565
3566/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003567 * "getbufvar()" function
3568 */
3569 void
3570f_getbufvar(typval_T *argvars, typval_T *rettv)
3571{
3572 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003573 char_u *varname;
3574 dictitem_T *v;
3575 int done = FALSE;
3576
3577 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3578 varname = tv_get_string_chk(&argvars[1]);
3579 ++emsg_off;
3580 buf = tv_get_buf(&argvars[0], FALSE);
3581
3582 rettv->v_type = VAR_STRING;
3583 rettv->vval.v_string = NULL;
3584
3585 if (buf != NULL && varname != NULL)
3586 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003587 if (*varname == '&')
3588 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003589 buf_T *save_curbuf = curbuf;
3590
3591 // set curbuf to be our buf, temporarily
3592 curbuf = buf;
3593
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003594 if (varname[1] == NUL)
3595 {
3596 // get all buffer-local options in a dict
3597 dict_T *opts = get_winbuf_options(TRUE);
3598
3599 if (opts != NULL)
3600 {
3601 rettv_dict_set(rettv, opts);
3602 done = TRUE;
3603 }
3604 }
3605 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3606 // buffer-local-option
3607 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003608
3609 // restore previous notion of curbuf
3610 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003611 }
3612 else
3613 {
3614 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003615 if (*varname == NUL)
3616 // Let getbufvar({nr}, "") return the "b:" dictionary.
3617 v = &buf->b_bufvar;
3618 else
3619 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3620 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003621 if (v != NULL)
3622 {
3623 copy_tv(&v->di_tv, rettv);
3624 done = TRUE;
3625 }
3626 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003627 }
3628
3629 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3630 // use the default value
3631 copy_tv(&argvars[2], rettv);
3632
3633 --emsg_off;
3634}
3635
3636/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003637 * "settabvar()" function
3638 */
3639 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003640f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003641{
3642 tabpage_T *save_curtab;
3643 tabpage_T *tp;
3644 char_u *varname, *tabvarname;
3645 typval_T *varp;
3646
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003647 if (check_secure())
3648 return;
3649
3650 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3651 varname = tv_get_string_chk(&argvars[1]);
3652 varp = &argvars[2];
3653
3654 if (varname != NULL && varp != NULL && tp != NULL)
3655 {
3656 save_curtab = curtab;
3657 goto_tabpage_tp(tp, FALSE, FALSE);
3658
3659 tabvarname = alloc(STRLEN(varname) + 3);
3660 if (tabvarname != NULL)
3661 {
3662 STRCPY(tabvarname, "t:");
3663 STRCPY(tabvarname + 2, varname);
3664 set_var(tabvarname, varp, TRUE);
3665 vim_free(tabvarname);
3666 }
3667
3668 // Restore current tabpage
3669 if (valid_tabpage(save_curtab))
3670 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3671 }
3672}
3673
3674/*
3675 * "settabwinvar()" function
3676 */
3677 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003678f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003679{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003680 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003681}
3682
3683/*
3684 * "setwinvar()" function
3685 */
3686 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003687f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003688{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003689 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003690}
3691
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003692/*
3693 * "setbufvar()" function
3694 */
3695 void
3696f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3697{
3698 buf_T *buf;
3699 char_u *varname, *bufvarname;
3700 typval_T *varp;
3701 char_u nbuf[NUMBUFLEN];
3702
3703 if (check_secure())
3704 return;
3705 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3706 varname = tv_get_string_chk(&argvars[1]);
3707 buf = tv_get_buf(&argvars[0], FALSE);
3708 varp = &argvars[2];
3709
3710 if (buf != NULL && varname != NULL && varp != NULL)
3711 {
3712 if (*varname == '&')
3713 {
3714 long numval;
3715 char_u *strval;
3716 int error = FALSE;
3717 aco_save_T aco;
3718
3719 // set curbuf to be our buf, temporarily
3720 aucmd_prepbuf(&aco, buf);
3721
3722 ++varname;
3723 numval = (long)tv_get_number_chk(varp, &error);
3724 strval = tv_get_string_buf_chk(varp, nbuf);
3725 if (!error && strval != NULL)
3726 set_option_value(varname, numval, strval, OPT_LOCAL);
3727
3728 // reset notion of buffer
3729 aucmd_restbuf(&aco);
3730 }
3731 else
3732 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003733 bufvarname = alloc(STRLEN(varname) + 3);
3734 if (bufvarname != NULL)
3735 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003736 buf_T *save_curbuf = curbuf;
3737
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003738 curbuf = buf;
3739 STRCPY(bufvarname, "b:");
3740 STRCPY(bufvarname + 2, varname);
3741 set_var(bufvarname, varp, TRUE);
3742 vim_free(bufvarname);
3743 curbuf = save_curbuf;
3744 }
3745 }
3746 }
3747}
3748
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003749/*
3750 * Get a callback from "arg". It can be a Funcref or a function name.
3751 * When "arg" is zero return an empty string.
3752 * "cb_name" is not allocated.
3753 * "cb_name" is set to NULL for an invalid argument.
3754 */
3755 callback_T
3756get_callback(typval_T *arg)
3757{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003758 callback_T res;
3759 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003760
3761 res.cb_free_name = FALSE;
3762 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3763 {
3764 res.cb_partial = arg->vval.v_partial;
3765 ++res.cb_partial->pt_refcount;
3766 res.cb_name = partial_name(res.cb_partial);
3767 }
3768 else
3769 {
3770 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003771 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3772 && isdigit(*arg->vval.v_string))
3773 r = FAIL;
3774 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003775 {
3776 // Note that we don't make a copy of the string.
3777 res.cb_name = arg->vval.v_string;
3778 func_ref(res.cb_name);
3779 }
3780 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003781 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003782 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003783 r = FAIL;
3784
3785 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003786 {
3787 emsg(_("E921: Invalid callback argument"));
3788 res.cb_name = NULL;
3789 }
3790 }
3791 return res;
3792}
3793
3794/*
3795 * Copy a callback into a typval_T.
3796 */
3797 void
3798put_callback(callback_T *cb, typval_T *tv)
3799{
3800 if (cb->cb_partial != NULL)
3801 {
3802 tv->v_type = VAR_PARTIAL;
3803 tv->vval.v_partial = cb->cb_partial;
3804 ++tv->vval.v_partial->pt_refcount;
3805 }
3806 else
3807 {
3808 tv->v_type = VAR_FUNC;
3809 tv->vval.v_string = vim_strsave(cb->cb_name);
3810 func_ref(cb->cb_name);
3811 }
3812}
3813
3814/*
3815 * Make a copy of "src" into "dest", allocating the function name if needed,
3816 * without incrementing the refcount.
3817 */
3818 void
3819set_callback(callback_T *dest, callback_T *src)
3820{
3821 if (src->cb_partial == NULL)
3822 {
3823 // just a function name, make a copy
3824 dest->cb_name = vim_strsave(src->cb_name);
3825 dest->cb_free_name = TRUE;
3826 }
3827 else
3828 {
3829 // cb_name is a pointer into cb_partial
3830 dest->cb_name = src->cb_name;
3831 dest->cb_free_name = FALSE;
3832 }
3833 dest->cb_partial = src->cb_partial;
3834}
3835
3836/*
3837 * Unref/free "callback" returned by get_callback() or set_callback().
3838 */
3839 void
3840free_callback(callback_T *callback)
3841{
3842 if (callback->cb_partial != NULL)
3843 {
3844 partial_unref(callback->cb_partial);
3845 callback->cb_partial = NULL;
3846 }
3847 else if (callback->cb_name != NULL)
3848 func_unref(callback->cb_name);
3849 if (callback->cb_free_name)
3850 {
3851 vim_free(callback->cb_name);
3852 callback->cb_free_name = FALSE;
3853 }
3854 callback->cb_name = NULL;
3855}
3856
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003857#endif // FEAT_EVAL