blob: e7a1b6d7cecde44617bbd92e5d56d80140e7304f [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200148};
149
150// shorthand
151#define vv_type vv_di.di_tv.v_type
152#define vv_nr vv_di.di_tv.vval.v_number
153#define vv_float vv_di.di_tv.vval.v_float
154#define vv_str vv_di.di_tv.vval.v_string
155#define vv_list vv_di.di_tv.vval.v_list
156#define vv_dict vv_di.di_tv.vval.v_dict
157#define vv_blob vv_di.di_tv.vval.v_blob
158#define vv_tv vv_di.di_tv
159
160static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200161static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vimvarht vimvardict.dv_hashtab
163
164// for VIM_VERSION_ defines
165#include "version.h"
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167static char_u *skip_var_one(char_u *arg, int include_type);
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
247 set_reg_var(0); // default for v:register is not 0 but '"'
248}
249
250#if defined(EXITFREE) || defined(PROTO)
251/*
252 * Free all vim variables information on exit
253 */
254 void
255evalvars_clear(void)
256{
257 int i;
258 struct vimvar *p;
259
260 for (i = 0; i < VV_LEN; ++i)
261 {
262 p = &vimvars[i];
263 if (p->vv_di.di_tv.v_type == VAR_STRING)
264 VIM_CLEAR(p->vv_str);
265 else if (p->vv_di.di_tv.v_type == VAR_LIST)
266 {
267 list_unref(p->vv_list);
268 p->vv_list = NULL;
269 }
270 }
271 hash_clear(&vimvarht);
272 hash_init(&vimvarht); // garbage_collect() will access it
273 hash_clear(&compat_hashtab);
274
275 // global variables
276 vars_clear(&globvarht);
277
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100278 // Script-local variables. Clear all the variables here.
279 // The scriptvar_T is cleared later in free_scriptnames(), because a
280 // variable in one script might hold a reference to the whole scope of
281 // another script.
282 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200283 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284}
285#endif
286
287 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200288garbage_collect_globvars(int copyID)
289{
290 return set_ref_in_ht(&globvarht, copyID, NULL);
291}
292
293 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200294garbage_collect_vimvars(int copyID)
295{
296 return set_ref_in_ht(&vimvarht, copyID, NULL);
297}
298
299 int
300garbage_collect_scriptvars(int copyID)
301{
302 int i;
303 int abort = FALSE;
304
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100305 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200306 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
307
308 return abort;
309}
310
311/*
312 * Set an internal variable to a string value. Creates the variable if it does
313 * not already exist.
314 */
315 void
316set_internal_string_var(char_u *name, char_u *value)
317{
318 char_u *val;
319 typval_T *tvp;
320
321 val = vim_strsave(value);
322 if (val != NULL)
323 {
324 tvp = alloc_string_tv(val);
325 if (tvp != NULL)
326 {
327 set_var(name, tvp, FALSE);
328 free_tv(tvp);
329 }
330 }
331}
332
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200333 int
334eval_charconvert(
335 char_u *enc_from,
336 char_u *enc_to,
337 char_u *fname_from,
338 char_u *fname_to)
339{
340 int err = FALSE;
341
342 set_vim_var_string(VV_CC_FROM, enc_from, -1);
343 set_vim_var_string(VV_CC_TO, enc_to, -1);
344 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
345 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
346 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
347 err = TRUE;
348 set_vim_var_string(VV_CC_FROM, NULL, -1);
349 set_vim_var_string(VV_CC_TO, NULL, -1);
350 set_vim_var_string(VV_FNAME_IN, NULL, -1);
351 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
352
353 if (err)
354 return FAIL;
355 return OK;
356}
357
358# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
359 int
360eval_printexpr(char_u *fname, char_u *args)
361{
362 int err = FALSE;
363
364 set_vim_var_string(VV_FNAME_IN, fname, -1);
365 set_vim_var_string(VV_CMDARG, args, -1);
366 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
367 err = TRUE;
368 set_vim_var_string(VV_FNAME_IN, NULL, -1);
369 set_vim_var_string(VV_CMDARG, NULL, -1);
370
371 if (err)
372 {
373 mch_remove(fname);
374 return FAIL;
375 }
376 return OK;
377}
378# endif
379
380# if defined(FEAT_DIFF) || defined(PROTO)
381 void
382eval_diff(
383 char_u *origfile,
384 char_u *newfile,
385 char_u *outfile)
386{
387 int err = FALSE;
388
389 set_vim_var_string(VV_FNAME_IN, origfile, -1);
390 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
391 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
392 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
393 set_vim_var_string(VV_FNAME_IN, NULL, -1);
394 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
395 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
396}
397
398 void
399eval_patch(
400 char_u *origfile,
401 char_u *difffile,
402 char_u *outfile)
403{
404 int err;
405
406 set_vim_var_string(VV_FNAME_IN, origfile, -1);
407 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
408 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
409 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
410 set_vim_var_string(VV_FNAME_IN, NULL, -1);
411 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
412 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
413}
414# endif
415
416#if defined(FEAT_SPELL) || defined(PROTO)
417/*
418 * Evaluate an expression to a list with suggestions.
419 * For the "expr:" part of 'spellsuggest'.
420 * Returns NULL when there is an error.
421 */
422 list_T *
423eval_spell_expr(char_u *badword, char_u *expr)
424{
425 typval_T save_val;
426 typval_T rettv;
427 list_T *list = NULL;
428 char_u *p = skipwhite(expr);
429
430 // Set "v:val" to the bad word.
431 prepare_vimvar(VV_VAL, &save_val);
432 set_vim_var_string(VV_VAL, badword, -1);
433 if (p_verbose == 0)
434 ++emsg_off;
435
Bram Moolenaar32e35112020-05-14 22:41:15 +0200436 if (eval1(&p, &rettv, EVAL_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200437 {
438 if (rettv.v_type != VAR_LIST)
439 clear_tv(&rettv);
440 else
441 list = rettv.vval.v_list;
442 }
443
444 if (p_verbose == 0)
445 --emsg_off;
446 clear_tv(get_vim_var_tv(VV_VAL));
447 restore_vimvar(VV_VAL, &save_val);
448
449 return list;
450}
451
452/*
453 * "list" is supposed to contain two items: a word and a number. Return the
454 * word in "pp" and the number as the return value.
455 * Return -1 if anything isn't right.
456 * Used to get the good word and score from the eval_spell_expr() result.
457 */
458 int
459get_spellword(list_T *list, char_u **pp)
460{
461 listitem_T *li;
462
463 li = list->lv_first;
464 if (li == NULL)
465 return -1;
466 *pp = tv_get_string(&li->li_tv);
467
468 li = li->li_next;
469 if (li == NULL)
470 return -1;
471 return (int)tv_get_number(&li->li_tv);
472}
473#endif
474
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200475/*
476 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200477 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200478 * When not used yet add the variable to the v: hashtable.
479 */
480 void
481prepare_vimvar(int idx, typval_T *save_tv)
482{
483 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200484 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200485 if (vimvars[idx].vv_type == VAR_UNKNOWN)
486 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
487}
488
489/*
490 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200491 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200492 * When no longer defined, remove the variable from the v: hashtable.
493 */
494 void
495restore_vimvar(int idx, typval_T *save_tv)
496{
497 hashitem_T *hi;
498
499 vimvars[idx].vv_tv = *save_tv;
500 if (vimvars[idx].vv_type == VAR_UNKNOWN)
501 {
502 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
503 if (HASHITEM_EMPTY(hi))
504 internal_error("restore_vimvar()");
505 else
506 hash_remove(&vimvarht, hi);
507 }
508}
509
510/*
511 * List Vim variables.
512 */
513 static void
514list_vim_vars(int *first)
515{
516 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
517}
518
519/*
520 * List script-local variables, if there is a script.
521 */
522 static void
523list_script_vars(int *first)
524{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100525 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200526 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
527 "s:", FALSE, first);
528}
529
530/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200531 * Get a list of lines from a HERE document. The here document is a list of
532 * lines surrounded by a marker.
533 * cmd << {marker}
534 * {line1}
535 * {line2}
536 * ....
537 * {marker}
538 *
539 * The {marker} is a string. If the optional 'trim' word is supplied before the
540 * marker, then the leading indentation before the lines (matching the
541 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200542 *
543 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
544 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
545 * missing, then '.' is accepted as a marker.
546 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200547 * Returns a List with {lines} or NULL.
548 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200550heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200551{
552 char_u *theline;
553 char_u *marker;
554 list_T *l;
555 char_u *p;
556 int marker_indent_len = 0;
557 int text_indent_len = 0;
558 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200559 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200560
561 if (eap->getline == NULL)
562 {
563 emsg(_("E991: cannot use =<< here"));
564 return NULL;
565 }
566
567 // Check for the optional 'trim' word before the marker
568 cmd = skipwhite(cmd);
569 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
570 {
571 cmd = skipwhite(cmd + 4);
572
573 // Trim the indentation from all the lines in the here document.
574 // The amount of indentation trimmed is the same as the indentation of
575 // the first line after the :let command line. To find the end marker
576 // the indent of the :let command line is trimmed.
577 p = *eap->cmdlinep;
578 while (VIM_ISWHITE(*p))
579 {
580 p++;
581 marker_indent_len++;
582 }
583 text_indent_len = -1;
584 }
585
586 // The marker is the next word.
587 if (*cmd != NUL && *cmd != '"')
588 {
589 marker = skipwhite(cmd);
590 p = skiptowhite(marker);
591 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
592 {
593 emsg(_(e_trailing));
594 return NULL;
595 }
596 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200597 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200598 {
599 emsg(_("E221: Marker cannot start with lower case letter"));
600 return NULL;
601 }
602 }
603 else
604 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200605 // When getting lines for an embedded script, if the marker is missing,
606 // accept '.' as the marker.
607 if (script_get)
608 marker = dot;
609 else
610 {
611 emsg(_("E172: Missing marker"));
612 return NULL;
613 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200614 }
615
616 l = list_alloc();
617 if (l == NULL)
618 return NULL;
619
620 for (;;)
621 {
622 int mi = 0;
623 int ti = 0;
624
625 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
626 if (theline == NULL)
627 {
628 semsg(_("E990: Missing end marker '%s'"), marker);
629 break;
630 }
631
632 // with "trim": skip the indent matching the :let line to find the
633 // marker
634 if (marker_indent_len > 0
635 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
636 mi = marker_indent_len;
637 if (STRCMP(marker, theline + mi) == 0)
638 {
639 vim_free(theline);
640 break;
641 }
642
643 if (text_indent_len == -1 && *theline != NUL)
644 {
645 // set the text indent from the first line.
646 p = theline;
647 text_indent_len = 0;
648 while (VIM_ISWHITE(*p))
649 {
650 p++;
651 text_indent_len++;
652 }
653 text_indent = vim_strnsave(theline, text_indent_len);
654 }
655 // with "trim": skip the indent matching the first line
656 if (text_indent != NULL)
657 for (ti = 0; ti < text_indent_len; ++ti)
658 if (theline[ti] != text_indent[ti])
659 break;
660
661 if (list_append_string(l, theline + ti, -1) == FAIL)
662 break;
663 vim_free(theline);
664 }
665 vim_free(text_indent);
666
667 return l;
668}
669
670/*
671 * ":let" list all variable values
672 * ":let var1 var2" list variable values
673 * ":let var = expr" assignment command.
674 * ":let var += expr" assignment command.
675 * ":let var -= expr" assignment command.
676 * ":let var *= expr" assignment command.
677 * ":let var /= expr" assignment command.
678 * ":let var %= expr" assignment command.
679 * ":let var .= expr" assignment command.
680 * ":let var ..= expr" assignment command.
681 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100683 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200684 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200685 * ":const" list all variable values
686 * ":const var1 var2" list variable values
687 * ":const var = expr" assignment command.
688 * ":const [var1, var2] = expr" unpack list.
689 */
690 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200691ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200692{
693 char_u *arg = eap->arg;
694 char_u *expr = NULL;
695 typval_T rettv;
696 int i;
697 int var_count = 0;
698 int semicolon = 0;
699 char_u op[2];
700 char_u *argend;
701 int first = TRUE;
702 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200703 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200704 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 // detect Vim9 assignment without ":let" or ":const"
707 if (eap->arg == eap->cmd)
708 flags |= LET_NO_COMMAND;
709
710 argend = skip_var_list(arg, TRUE, &var_count, &semicolon);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200711 if (argend == NULL)
712 return;
713 if (argend > arg && argend[-1] == '.') // for var.='str'
714 --argend;
715 expr = skipwhite(argend);
716 concat = expr[0] == '.'
717 && ((expr[1] == '=' && current_sctx.sc_version < 2)
718 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200719 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
720 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200721 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 {
723 // ":let" without "=": list variables
724 if (*arg == '[')
725 emsg(_(e_invarg));
726 else if (expr[0] == '.')
727 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200728 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729 // ":let var1 var2"
730 arg = list_arg_vars(eap, arg, &first);
731 else if (!eap->skip)
732 {
733 // ":let"
734 list_glob_vars(&first);
735 list_buf_vars(&first);
736 list_win_vars(&first);
737 list_tab_vars(&first);
738 list_script_vars(&first);
739 list_func_vars(&first);
740 list_vim_vars(&first);
741 }
742 eap->nextcmd = check_nextcmd(arg);
743 }
744 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
745 {
746 list_T *l;
747
748 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200749 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200750 if (l != NULL)
751 {
752 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200753 if (!eap->skip)
754 {
755 op[0] = '=';
756 op[1] = NUL;
757 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200759 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200760 clear_tv(&rettv);
761 }
762 }
763 else
764 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200765 int eval_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766
Bram Moolenaar32e35112020-05-14 22:41:15 +0200767 rettv.v_type = VAR_UNKNOWN;
768 i = FAIL;
769 if (has_assign || concat)
770 {
771 op[0] = '=';
772 op[1] = NUL;
773 if (*expr != '=')
774 {
775 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
776 {
777 op[0] = *expr; // +=, -=, *=, /=, %= or .=
778 if (expr[0] == '.' && expr[1] == '.') // ..=
779 ++expr;
780 }
781 expr = skipwhite(expr + 2);
782 }
783 else
784 expr = skipwhite(expr + 1);
785
786 if (eap->skip)
787 ++emsg_skip;
788 eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200789 i = eval0(expr, &rettv, &eap->nextcmd, eval_flags);
790 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200791 if (eap->skip)
792 {
793 if (i != FAIL)
794 clear_tv(&rettv);
795 --emsg_skip;
796 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200797 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200798 {
799 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200801 clear_tv(&rettv);
802 }
803 }
804}
805
806/*
807 * Assign the typevalue "tv" to the variable or variables at "arg_start".
808 * Handles both "var" with any type and "[var, var; var]" with a list type.
809 * When "op" is not NULL it points to a string with characters that
810 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
811 * or concatenate.
812 * Returns OK or FAIL;
813 */
814 int
815ex_let_vars(
816 char_u *arg_start,
817 typval_T *tv,
818 int copy, // copy values from "tv", don't move
819 int semicolon, // from skip_var_list()
820 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200822 char_u *op)
823{
824 char_u *arg = arg_start;
825 list_T *l;
826 int i;
827 listitem_T *item;
828 typval_T ltv;
829
830 if (*arg != '[')
831 {
832 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200834 return FAIL;
835 return OK;
836 }
837
838 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
839 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
840 {
841 emsg(_(e_listreq));
842 return FAIL;
843 }
844
845 i = list_len(l);
846 if (semicolon == 0 && var_count < i)
847 {
848 emsg(_("E687: Less targets than List items"));
849 return FAIL;
850 }
851 if (var_count - semicolon > i)
852 {
853 emsg(_("E688: More targets than List items"));
854 return FAIL;
855 }
856
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200857 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200858 item = l->lv_first;
859 while (*arg != ']')
860 {
861 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200863 item = item->li_next;
864 if (arg == NULL)
865 return FAIL;
866
867 arg = skipwhite(arg);
868 if (*arg == ';')
869 {
870 // Put the rest of the list (may be empty) in the var after ';'.
871 // Create a new list for this.
872 l = list_alloc();
873 if (l == NULL)
874 return FAIL;
875 while (item != NULL)
876 {
877 list_append_tv(l, &item->li_tv);
878 item = item->li_next;
879 }
880
881 ltv.v_type = VAR_LIST;
882 ltv.v_lock = 0;
883 ltv.vval.v_list = l;
884 l->lv_refcount = 1;
885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200887 (char_u *)"]", op);
888 clear_tv(&ltv);
889 if (arg == NULL)
890 return FAIL;
891 break;
892 }
893 else if (*arg != ',' && *arg != ']')
894 {
895 internal_error("ex_let_vars()");
896 return FAIL;
897 }
898 }
899
900 return OK;
901}
902
903/*
904 * Skip over assignable variable "var" or list of variables "[var, var]".
905 * Used for ":let varvar = expr" and ":for varvar in expr".
906 * For "[var, var]" increment "*var_count" for each variable.
907 * for "[var, var; var]" set "semicolon".
908 * Return NULL for an error.
909 */
910 char_u *
911skip_var_list(
912 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200914 int *var_count,
915 int *semicolon)
916{
917 char_u *p, *s;
918
919 if (*arg == '[')
920 {
921 // "[var, var]": find the matching ']'.
922 p = arg;
923 for (;;)
924 {
925 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200927 if (s == p)
928 {
929 semsg(_(e_invarg2), p);
930 return NULL;
931 }
932 ++*var_count;
933
934 p = skipwhite(s);
935 if (*p == ']')
936 break;
937 else if (*p == ';')
938 {
939 if (*semicolon == 1)
940 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200941 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200942 return NULL;
943 }
944 *semicolon = 1;
945 }
946 else if (*p != ',')
947 {
948 semsg(_(e_invarg2), p);
949 return NULL;
950 }
951 }
952 return p + 1;
953 }
954 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200956}
957
958/*
959 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
960 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200962 */
963 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200965{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 char_u *end;
967
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200968 if (*arg == '@' && arg[1] != NUL)
969 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
973 && *end == ':')
974 {
975 end = skip_type(skipwhite(end + 1));
976 }
977 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200978}
979
980/*
981 * List variables for hashtab "ht" with prefix "prefix".
982 * If "empty" is TRUE also list NULL strings as empty strings.
983 */
984 void
985list_hashtable_vars(
986 hashtab_T *ht,
987 char *prefix,
988 int empty,
989 int *first)
990{
991 hashitem_T *hi;
992 dictitem_T *di;
993 int todo;
994 char_u buf[IOSIZE];
995
996 todo = (int)ht->ht_used;
997 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
998 {
999 if (!HASHITEM_EMPTY(hi))
1000 {
1001 --todo;
1002 di = HI2DI(hi);
1003
1004 // apply :filter /pat/ to variable name
1005 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1006 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1007 if (message_filtered(buf))
1008 continue;
1009
1010 if (empty || di->di_tv.v_type != VAR_STRING
1011 || di->di_tv.vval.v_string != NULL)
1012 list_one_var(di, prefix, first);
1013 }
1014 }
1015}
1016
1017/*
1018 * List global variables.
1019 */
1020 static void
1021list_glob_vars(int *first)
1022{
1023 list_hashtable_vars(&globvarht, "", TRUE, first);
1024}
1025
1026/*
1027 * List buffer variables.
1028 */
1029 static void
1030list_buf_vars(int *first)
1031{
1032 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1033}
1034
1035/*
1036 * List window variables.
1037 */
1038 static void
1039list_win_vars(int *first)
1040{
1041 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1042}
1043
1044/*
1045 * List tab page variables.
1046 */
1047 static void
1048list_tab_vars(int *first)
1049{
1050 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1051}
1052
1053/*
1054 * List variables in "arg".
1055 */
1056 static char_u *
1057list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1058{
1059 int error = FALSE;
1060 int len;
1061 char_u *name;
1062 char_u *name_start;
1063 char_u *arg_subsc;
1064 char_u *tofree;
1065 typval_T tv;
1066
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001067 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001068 {
1069 if (error || eap->skip)
1070 {
1071 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1072 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1073 {
1074 emsg_severe = TRUE;
1075 emsg(_(e_trailing));
1076 break;
1077 }
1078 }
1079 else
1080 {
1081 // get_name_len() takes care of expanding curly braces
1082 name_start = name = arg;
1083 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1084 if (len <= 0)
1085 {
1086 // This is mainly to keep test 49 working: when expanding
1087 // curly braces fails overrule the exception error message.
1088 if (len < 0 && !aborting())
1089 {
1090 emsg_severe = TRUE;
1091 semsg(_(e_invarg2), arg);
1092 break;
1093 }
1094 error = TRUE;
1095 }
1096 else
1097 {
1098 if (tofree != NULL)
1099 name = tofree;
1100 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1101 error = TRUE;
1102 else
1103 {
1104 // handle d.key, l[idx], f(expr)
1105 arg_subsc = arg;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001106 if (handle_subscript(&arg, &tv, EVAL_EVALUATE, TRUE,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001107 name, &name) == FAIL)
1108 error = TRUE;
1109 else
1110 {
1111 if (arg == arg_subsc && len == 2 && name[1] == ':')
1112 {
1113 switch (*name)
1114 {
1115 case 'g': list_glob_vars(first); break;
1116 case 'b': list_buf_vars(first); break;
1117 case 'w': list_win_vars(first); break;
1118 case 't': list_tab_vars(first); break;
1119 case 'v': list_vim_vars(first); break;
1120 case 's': list_script_vars(first); break;
1121 case 'l': list_func_vars(first); break;
1122 default:
1123 semsg(_("E738: Can't list variables for %s"), name);
1124 }
1125 }
1126 else
1127 {
1128 char_u numbuf[NUMBUFLEN];
1129 char_u *tf;
1130 int c;
1131 char_u *s;
1132
1133 s = echo_string(&tv, &tf, numbuf, 0);
1134 c = *arg;
1135 *arg = NUL;
1136 list_one_var_a("",
1137 arg == arg_subsc ? name : name_start,
1138 tv.v_type,
1139 s == NULL ? (char_u *)"" : s,
1140 first);
1141 *arg = c;
1142 vim_free(tf);
1143 }
1144 clear_tv(&tv);
1145 }
1146 }
1147 }
1148
1149 vim_free(tofree);
1150 }
1151
1152 arg = skipwhite(arg);
1153 }
1154
1155 return arg;
1156}
1157
1158/*
1159 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1160 * Returns a pointer to the char just after the var name.
1161 * Returns NULL if there is an error.
1162 */
1163 static char_u *
1164ex_let_one(
1165 char_u *arg, // points to variable name
1166 typval_T *tv, // value to assign to variable
1167 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001169 char_u *endchars, // valid chars after variable name or NULL
1170 char_u *op) // "+", "-", "." or NULL
1171{
1172 int c1;
1173 char_u *name;
1174 char_u *p;
1175 char_u *arg_end = NULL;
1176 int len;
1177 int opt_flags;
1178 char_u *tofree = NULL;
1179
1180 // ":let $VAR = expr": Set environment variable.
1181 if (*arg == '$')
1182 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001184 {
1185 emsg(_("E996: Cannot lock an environment variable"));
1186 return NULL;
1187 }
1188 // Find the end of the name.
1189 ++arg;
1190 name = arg;
1191 len = get_env_len(&arg);
1192 if (len == 0)
1193 semsg(_(e_invarg2), name - 1);
1194 else
1195 {
1196 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1197 semsg(_(e_letwrong), op);
1198 else if (endchars != NULL
1199 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1200 emsg(_(e_letunexp));
1201 else if (!check_secure())
1202 {
1203 c1 = name[len];
1204 name[len] = NUL;
1205 p = tv_get_string_chk(tv);
1206 if (p != NULL && op != NULL && *op == '.')
1207 {
1208 int mustfree = FALSE;
1209 char_u *s = vim_getenv(name, &mustfree);
1210
1211 if (s != NULL)
1212 {
1213 p = tofree = concat_str(s, p);
1214 if (mustfree)
1215 vim_free(s);
1216 }
1217 }
1218 if (p != NULL)
1219 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001220 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 arg_end = arg;
1222 }
1223 name[len] = c1;
1224 vim_free(tofree);
1225 }
1226 }
1227 }
1228
1229 // ":let &option = expr": Set option value.
1230 // ":let &l:option = expr": Set local option value.
1231 // ":let &g:option = expr": Set global option value.
1232 else if (*arg == '&')
1233 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001235 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 return NULL;
1238 }
1239 // Find the end of the name.
1240 p = find_option_end(&arg, &opt_flags);
1241 if (p == NULL || (endchars != NULL
1242 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1243 emsg(_(e_letunexp));
1244 else
1245 {
1246 long n;
1247 int opt_type;
1248 long numval;
1249 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001250 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001251
1252 c1 = *p;
1253 *p = NUL;
1254
1255 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001256 // avoid setting a string option to the text "v:false" or similar.
1257 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1258 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001259 if (s != NULL && op != NULL && *op != '=')
1260 {
1261 opt_type = get_option_value(arg, &numval,
1262 &stringval, opt_flags);
1263 if ((opt_type == 1 && *op == '.')
1264 || (opt_type == 0 && *op != '.'))
1265 {
1266 semsg(_(e_letwrong), op);
1267 s = NULL; // don't set the value
1268 }
1269 else
1270 {
1271 if (opt_type == 1) // number
1272 {
1273 switch (*op)
1274 {
1275 case '+': n = numval + n; break;
1276 case '-': n = numval - n; break;
1277 case '*': n = numval * n; break;
1278 case '/': n = (long)num_divide(numval, n); break;
1279 case '%': n = (long)num_modulus(numval, n); break;
1280 }
1281 }
1282 else if (opt_type == 0 && stringval != NULL) // string
1283 {
1284 s = concat_str(stringval, s);
1285 vim_free(stringval);
1286 stringval = s;
1287 }
1288 }
1289 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001290 if (s != NULL || tv->v_type == VAR_BOOL
1291 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001292 {
1293 set_option_value(arg, n, s, opt_flags);
1294 arg_end = p;
1295 }
1296 *p = c1;
1297 vim_free(stringval);
1298 }
1299 }
1300
1301 // ":let @r = expr": Set register contents.
1302 else if (*arg == '@')
1303 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001305 {
1306 emsg(_("E996: Cannot lock a register"));
1307 return NULL;
1308 }
1309 ++arg;
1310 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1311 semsg(_(e_letwrong), op);
1312 else if (endchars != NULL
1313 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1314 emsg(_(e_letunexp));
1315 else
1316 {
1317 char_u *ptofree = NULL;
1318 char_u *s;
1319
1320 p = tv_get_string_chk(tv);
1321 if (p != NULL && op != NULL && *op == '.')
1322 {
1323 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1324 if (s != NULL)
1325 {
1326 p = ptofree = concat_str(s, p);
1327 vim_free(s);
1328 }
1329 }
1330 if (p != NULL)
1331 {
1332 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1333 arg_end = arg + 1;
1334 }
1335 vim_free(ptofree);
1336 }
1337 }
1338
1339 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001341 // ":let {expr} = expr": Idem, name made with curly braces
1342 else if (eval_isnamec1(*arg) || *arg == '{')
1343 {
1344 lval_T lv;
1345
1346 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001347 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001348 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 if (endchars != NULL && vim_strchr(endchars,
1350 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001351 emsg(_(e_letunexp));
1352 else
1353 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001355 arg_end = p;
1356 }
1357 }
1358 clear_lval(&lv);
1359 }
1360
1361 else
1362 semsg(_(e_invarg2), arg);
1363
1364 return arg_end;
1365}
1366
1367/*
1368 * ":unlet[!] var1 ... " command.
1369 */
1370 void
1371ex_unlet(exarg_T *eap)
1372{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001373 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001374}
1375
1376/*
1377 * ":lockvar" and ":unlockvar" commands
1378 */
1379 void
1380ex_lockvar(exarg_T *eap)
1381{
1382 char_u *arg = eap->arg;
1383 int deep = 2;
1384
1385 if (eap->forceit)
1386 deep = -1;
1387 else if (vim_isdigit(*arg))
1388 {
1389 deep = getdigits(&arg);
1390 arg = skipwhite(arg);
1391 }
1392
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001393 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001394}
1395
1396/*
1397 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001398 * Also used for Vim9 script. "callback" is invoked as:
1399 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001400 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001401 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001402ex_unletlock(
1403 exarg_T *eap,
1404 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001405 int deep,
1406 int glv_flags,
1407 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1408 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001409{
1410 char_u *arg = argstart;
1411 char_u *name_end;
1412 int error = FALSE;
1413 lval_T lv;
1414
1415 do
1416 {
1417 if (*arg == '$')
1418 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001419 lv.ll_name = arg;
1420 lv.ll_tv = NULL;
1421 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001422 if (get_env_len(&arg) == 0)
1423 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001424 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001425 return;
1426 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001427 if (!error && !eap->skip
1428 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1429 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001430 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001431 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001432 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001433 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001434 // Parse the name and find the end.
1435 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1436 glv_flags, FNE_CHECK_START);
1437 if (lv.ll_name == NULL)
1438 error = TRUE; // error but continue parsing
1439 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1440 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001441 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001442 if (name_end != NULL)
1443 {
1444 emsg_severe = TRUE;
1445 emsg(_(e_trailing));
1446 }
1447 if (!(eap->skip || error))
1448 clear_lval(&lv);
1449 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001451
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001452 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001453 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001454 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001455
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001456 if (!eap->skip)
1457 clear_lval(&lv);
1458 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001459
1460 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001461 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462
1463 eap->nextcmd = check_nextcmd(arg);
1464}
1465
1466 static int
1467do_unlet_var(
1468 lval_T *lp,
1469 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001470 exarg_T *eap,
1471 int deep UNUSED,
1472 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001474 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001475 int ret = OK;
1476 int cc;
1477
1478 if (lp->ll_tv == NULL)
1479 {
1480 cc = *name_end;
1481 *name_end = NUL;
1482
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001483 // Environment variable, normal name or expanded name.
1484 if (*lp->ll_name == '$')
1485 vim_unsetenv(lp->ll_name + 1);
1486 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001487 ret = FAIL;
1488 *name_end = cc;
1489 }
1490 else if ((lp->ll_list != NULL
1491 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1492 || (lp->ll_dict != NULL
1493 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1494 return FAIL;
1495 else if (lp->ll_range)
1496 {
1497 listitem_T *li;
1498 listitem_T *ll_li = lp->ll_li;
1499 int ll_n1 = lp->ll_n1;
1500
1501 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1502 {
1503 li = ll_li->li_next;
1504 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1505 return FAIL;
1506 ll_li = li;
1507 ++ll_n1;
1508 }
1509
1510 // Delete a range of List items.
1511 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1512 {
1513 li = lp->ll_li->li_next;
1514 listitem_remove(lp->ll_list, lp->ll_li);
1515 lp->ll_li = li;
1516 ++lp->ll_n1;
1517 }
1518 }
1519 else
1520 {
1521 if (lp->ll_list != NULL)
1522 // unlet a List item.
1523 listitem_remove(lp->ll_list, lp->ll_li);
1524 else
1525 // unlet a Dictionary item.
1526 dictitem_remove(lp->ll_dict, lp->ll_di);
1527 }
1528
1529 return ret;
1530}
1531
1532/*
1533 * "unlet" a variable. Return OK if it existed, FAIL if not.
1534 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1535 */
1536 int
1537do_unlet(char_u *name, int forceit)
1538{
1539 hashtab_T *ht;
1540 hashitem_T *hi;
1541 char_u *varname;
1542 dict_T *d;
1543 dictitem_T *di;
1544
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001545 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1546 && check_vim9_unlet(name) == FAIL)
1547 return FAIL;
1548
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001549 ht = find_var_ht(name, &varname);
1550 if (ht != NULL && *varname != NUL)
1551 {
1552 d = get_current_funccal_dict(ht);
1553 if (d == NULL)
1554 {
1555 if (ht == &globvarht)
1556 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001557 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001558 d = &vimvardict;
1559 else
1560 {
1561 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1562 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1563 }
1564 if (d == NULL)
1565 {
1566 internal_error("do_unlet()");
1567 return FAIL;
1568 }
1569 }
1570 hi = hash_find(ht, varname);
1571 if (HASHITEM_EMPTY(hi))
1572 hi = find_hi_in_scoped_ht(name, &ht);
1573 if (hi != NULL && !HASHITEM_EMPTY(hi))
1574 {
1575 di = HI2DI(hi);
1576 if (var_check_fixed(di->di_flags, name, FALSE)
1577 || var_check_ro(di->di_flags, name, FALSE)
1578 || var_check_lock(d->dv_lock, name, FALSE))
1579 return FAIL;
1580
1581 delete_var(ht, hi);
1582 return OK;
1583 }
1584 }
1585 if (forceit)
1586 return OK;
1587 semsg(_("E108: No such variable: \"%s\""), name);
1588 return FAIL;
1589}
1590
1591/*
1592 * Lock or unlock variable indicated by "lp".
1593 * "deep" is the levels to go (-1 for unlimited);
1594 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1595 */
1596 static int
1597do_lock_var(
1598 lval_T *lp,
1599 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001600 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001602 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001603{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001604 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605 int ret = OK;
1606 int cc;
1607 dictitem_T *di;
1608
1609 if (deep == 0) // nothing to do
1610 return OK;
1611
1612 if (lp->ll_tv == NULL)
1613 {
1614 cc = *name_end;
1615 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001616 if (*lp->ll_name == '$')
1617 {
1618 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001620 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 else
1622 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001623 // Normal name or expanded name.
1624 di = find_var(lp->ll_name, NULL, TRUE);
1625 if (di == NULL)
1626 ret = FAIL;
1627 else if ((di->di_flags & DI_FLAGS_FIX)
1628 && di->di_tv.v_type != VAR_DICT
1629 && di->di_tv.v_type != VAR_LIST)
1630 {
1631 // For historic reasons this error is not given for a list or
1632 // dict. E.g., the b: dict could be locked/unlocked.
1633 semsg(_(e_lock_unlock), lp->ll_name);
1634 ret = FAIL;
1635 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001636 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001637 {
1638 if (lock)
1639 di->di_flags |= DI_FLAGS_LOCK;
1640 else
1641 di->di_flags &= ~DI_FLAGS_LOCK;
1642 item_lock(&di->di_tv, deep, lock);
1643 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001644 }
1645 *name_end = cc;
1646 }
1647 else if (lp->ll_range)
1648 {
1649 listitem_T *li = lp->ll_li;
1650
1651 // (un)lock a range of List items.
1652 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1653 {
1654 item_lock(&li->li_tv, deep, lock);
1655 li = li->li_next;
1656 ++lp->ll_n1;
1657 }
1658 }
1659 else if (lp->ll_list != NULL)
1660 // (un)lock a List item.
1661 item_lock(&lp->ll_li->li_tv, deep, lock);
1662 else
1663 // (un)lock a Dictionary item.
1664 item_lock(&lp->ll_di->di_tv, deep, lock);
1665
1666 return ret;
1667}
1668
1669/*
1670 * Lock or unlock an item. "deep" is nr of levels to go.
1671 */
1672 static void
1673item_lock(typval_T *tv, int deep, int lock)
1674{
1675 static int recurse = 0;
1676 list_T *l;
1677 listitem_T *li;
1678 dict_T *d;
1679 blob_T *b;
1680 hashitem_T *hi;
1681 int todo;
1682
1683 if (recurse >= DICT_MAXNEST)
1684 {
1685 emsg(_("E743: variable nested too deep for (un)lock"));
1686 return;
1687 }
1688 if (deep == 0)
1689 return;
1690 ++recurse;
1691
1692 // lock/unlock the item itself
1693 if (lock)
1694 tv->v_lock |= VAR_LOCKED;
1695 else
1696 tv->v_lock &= ~VAR_LOCKED;
1697
1698 switch (tv->v_type)
1699 {
1700 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001701 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001703 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001705 case VAR_STRING:
1706 case VAR_FUNC:
1707 case VAR_PARTIAL:
1708 case VAR_FLOAT:
1709 case VAR_SPECIAL:
1710 case VAR_JOB:
1711 case VAR_CHANNEL:
1712 break;
1713
1714 case VAR_BLOB:
1715 if ((b = tv->vval.v_blob) != NULL)
1716 {
1717 if (lock)
1718 b->bv_lock |= VAR_LOCKED;
1719 else
1720 b->bv_lock &= ~VAR_LOCKED;
1721 }
1722 break;
1723 case VAR_LIST:
1724 if ((l = tv->vval.v_list) != NULL)
1725 {
1726 if (lock)
1727 l->lv_lock |= VAR_LOCKED;
1728 else
1729 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001730 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001731 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001732 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733 item_lock(&li->li_tv, deep - 1, lock);
1734 }
1735 break;
1736 case VAR_DICT:
1737 if ((d = tv->vval.v_dict) != NULL)
1738 {
1739 if (lock)
1740 d->dv_lock |= VAR_LOCKED;
1741 else
1742 d->dv_lock &= ~VAR_LOCKED;
1743 if (deep < 0 || deep > 1)
1744 {
1745 // recursive: lock/unlock the items the List contains
1746 todo = (int)d->dv_hashtab.ht_used;
1747 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1748 {
1749 if (!HASHITEM_EMPTY(hi))
1750 {
1751 --todo;
1752 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1753 }
1754 }
1755 }
1756 }
1757 }
1758 --recurse;
1759}
1760
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001761#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1762/*
1763 * Delete all "menutrans_" variables.
1764 */
1765 void
1766del_menutrans_vars(void)
1767{
1768 hashitem_T *hi;
1769 int todo;
1770
1771 hash_lock(&globvarht);
1772 todo = (int)globvarht.ht_used;
1773 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1774 {
1775 if (!HASHITEM_EMPTY(hi))
1776 {
1777 --todo;
1778 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1779 delete_var(&globvarht, hi);
1780 }
1781 }
1782 hash_unlock(&globvarht);
1783}
1784#endif
1785
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001786/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001787 * Local string buffer for the next two functions to store a variable name
1788 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1789 * get_user_var_name().
1790 */
1791
1792static char_u *varnamebuf = NULL;
1793static int varnamebuflen = 0;
1794
1795/*
1796 * Function to concatenate a prefix and a variable name.
1797 */
1798 static char_u *
1799cat_prefix_varname(int prefix, char_u *name)
1800{
1801 int len;
1802
1803 len = (int)STRLEN(name) + 3;
1804 if (len > varnamebuflen)
1805 {
1806 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001807 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001808 varnamebuf = alloc(len);
1809 if (varnamebuf == NULL)
1810 {
1811 varnamebuflen = 0;
1812 return NULL;
1813 }
1814 varnamebuflen = len;
1815 }
1816 *varnamebuf = prefix;
1817 varnamebuf[1] = ':';
1818 STRCPY(varnamebuf + 2, name);
1819 return varnamebuf;
1820}
1821
1822/*
1823 * Function given to ExpandGeneric() to obtain the list of user defined
1824 * (global/buffer/window/built-in) variable names.
1825 */
1826 char_u *
1827get_user_var_name(expand_T *xp, int idx)
1828{
1829 static long_u gdone;
1830 static long_u bdone;
1831 static long_u wdone;
1832 static long_u tdone;
1833 static int vidx;
1834 static hashitem_T *hi;
1835 hashtab_T *ht;
1836
1837 if (idx == 0)
1838 {
1839 gdone = bdone = wdone = vidx = 0;
1840 tdone = 0;
1841 }
1842
1843 // Global variables
1844 if (gdone < globvarht.ht_used)
1845 {
1846 if (gdone++ == 0)
1847 hi = globvarht.ht_array;
1848 else
1849 ++hi;
1850 while (HASHITEM_EMPTY(hi))
1851 ++hi;
1852 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1853 return cat_prefix_varname('g', hi->hi_key);
1854 return hi->hi_key;
1855 }
1856
1857 // b: variables
1858 ht = &curbuf->b_vars->dv_hashtab;
1859 if (bdone < ht->ht_used)
1860 {
1861 if (bdone++ == 0)
1862 hi = ht->ht_array;
1863 else
1864 ++hi;
1865 while (HASHITEM_EMPTY(hi))
1866 ++hi;
1867 return cat_prefix_varname('b', hi->hi_key);
1868 }
1869
1870 // w: variables
1871 ht = &curwin->w_vars->dv_hashtab;
1872 if (wdone < ht->ht_used)
1873 {
1874 if (wdone++ == 0)
1875 hi = ht->ht_array;
1876 else
1877 ++hi;
1878 while (HASHITEM_EMPTY(hi))
1879 ++hi;
1880 return cat_prefix_varname('w', hi->hi_key);
1881 }
1882
1883 // t: variables
1884 ht = &curtab->tp_vars->dv_hashtab;
1885 if (tdone < ht->ht_used)
1886 {
1887 if (tdone++ == 0)
1888 hi = ht->ht_array;
1889 else
1890 ++hi;
1891 while (HASHITEM_EMPTY(hi))
1892 ++hi;
1893 return cat_prefix_varname('t', hi->hi_key);
1894 }
1895
1896 // v: variables
1897 if (vidx < VV_LEN)
1898 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1899
1900 VIM_CLEAR(varnamebuf);
1901 varnamebuflen = 0;
1902 return NULL;
1903}
1904
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001905 char *
1906get_var_special_name(int nr)
1907{
1908 switch (nr)
1909 {
1910 case VVAL_FALSE: return "v:false";
1911 case VVAL_TRUE: return "v:true";
1912 case VVAL_NONE: return "v:none";
1913 case VVAL_NULL: return "v:null";
1914 }
1915 internal_error("get_var_special_name()");
1916 return "42";
1917}
1918
1919/*
1920 * Returns the global variable dictionary
1921 */
1922 dict_T *
1923get_globvar_dict(void)
1924{
1925 return &globvardict;
1926}
1927
1928/*
1929 * Returns the global variable hash table
1930 */
1931 hashtab_T *
1932get_globvar_ht(void)
1933{
1934 return &globvarht;
1935}
1936
1937/*
1938 * Returns the v: variable dictionary
1939 */
1940 dict_T *
1941get_vimvar_dict(void)
1942{
1943 return &vimvardict;
1944}
1945
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001946/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001947 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001948 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 */
1950 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001951find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001953 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1954 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
1956 if (di == NULL)
1957 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001958 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1960 return (int)(vv - vimvars);
1961}
1962
1963
1964/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001965 * Set type of v: variable to "type".
1966 */
1967 void
1968set_vim_var_type(int idx, vartype_T type)
1969{
1970 vimvars[idx].vv_type = type;
1971}
1972
1973/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001974 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001975 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001976 */
1977 void
1978set_vim_var_nr(int idx, varnumber_T val)
1979{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001980 vimvars[idx].vv_nr = val;
1981}
1982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 char *
1984get_vim_var_name(int idx)
1985{
1986 return vimvars[idx].vv_name;
1987}
1988
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001989/*
1990 * Get typval_T v: variable value.
1991 */
1992 typval_T *
1993get_vim_var_tv(int idx)
1994{
1995 return &vimvars[idx].vv_tv;
1996}
1997
1998/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001999 * Set v: variable to "tv". Only accepts the same type.
2000 * Takes over the value of "tv".
2001 */
2002 int
2003set_vim_var_tv(int idx, typval_T *tv)
2004{
2005 if (vimvars[idx].vv_type != tv->v_type)
2006 {
2007 emsg(_("E1063: type mismatch for v: variable"));
2008 clear_tv(tv);
2009 return FAIL;
2010 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002011 // VV_RO is also checked when compiling, but let's check here as well.
2012 if (vimvars[idx].vv_flags & VV_RO)
2013 {
2014 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2015 return FAIL;
2016 }
2017 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2018 {
2019 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2020 return FAIL;
2021 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002022 clear_tv(&vimvars[idx].vv_di.di_tv);
2023 vimvars[idx].vv_di.di_tv = *tv;
2024 return OK;
2025}
2026
2027/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002028 * Get number v: variable value.
2029 */
2030 varnumber_T
2031get_vim_var_nr(int idx)
2032{
2033 return vimvars[idx].vv_nr;
2034}
2035
2036/*
2037 * Get string v: variable value. Uses a static buffer, can only be used once.
2038 * If the String variable has never been set, return an empty string.
2039 * Never returns NULL;
2040 */
2041 char_u *
2042get_vim_var_str(int idx)
2043{
2044 return tv_get_string(&vimvars[idx].vv_tv);
2045}
2046
2047/*
2048 * Get List v: variable value. Caller must take care of reference count when
2049 * needed.
2050 */
2051 list_T *
2052get_vim_var_list(int idx)
2053{
2054 return vimvars[idx].vv_list;
2055}
2056
2057/*
2058 * Get Dict v: variable value. Caller must take care of reference count when
2059 * needed.
2060 */
2061 dict_T *
2062get_vim_var_dict(int idx)
2063{
2064 return vimvars[idx].vv_dict;
2065}
2066
2067/*
2068 * Set v:char to character "c".
2069 */
2070 void
2071set_vim_var_char(int c)
2072{
2073 char_u buf[MB_MAXBYTES + 1];
2074
2075 if (has_mbyte)
2076 buf[(*mb_char2bytes)(c, buf)] = NUL;
2077 else
2078 {
2079 buf[0] = c;
2080 buf[1] = NUL;
2081 }
2082 set_vim_var_string(VV_CHAR, buf, -1);
2083}
2084
2085/*
2086 * Set v:count to "count" and v:count1 to "count1".
2087 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2088 */
2089 void
2090set_vcount(
2091 long count,
2092 long count1,
2093 int set_prevcount)
2094{
2095 if (set_prevcount)
2096 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2097 vimvars[VV_COUNT].vv_nr = count;
2098 vimvars[VV_COUNT1].vv_nr = count1;
2099}
2100
2101/*
2102 * Save variables that might be changed as a side effect. Used when executing
2103 * a timer callback.
2104 */
2105 void
2106save_vimvars(vimvars_save_T *vvsave)
2107{
2108 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2109 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2110 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2111}
2112
2113/*
2114 * Restore variables saved by save_vimvars().
2115 */
2116 void
2117restore_vimvars(vimvars_save_T *vvsave)
2118{
2119 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2120 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2121 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2122}
2123
2124/*
2125 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2126 * value.
2127 */
2128 void
2129set_vim_var_string(
2130 int idx,
2131 char_u *val,
2132 int len) // length of "val" to use or -1 (whole string)
2133{
2134 clear_tv(&vimvars[idx].vv_di.di_tv);
2135 vimvars[idx].vv_type = VAR_STRING;
2136 if (val == NULL)
2137 vimvars[idx].vv_str = NULL;
2138 else if (len == -1)
2139 vimvars[idx].vv_str = vim_strsave(val);
2140 else
2141 vimvars[idx].vv_str = vim_strnsave(val, len);
2142}
2143
2144/*
2145 * Set List v: variable to "val".
2146 */
2147 void
2148set_vim_var_list(int idx, list_T *val)
2149{
2150 clear_tv(&vimvars[idx].vv_di.di_tv);
2151 vimvars[idx].vv_type = VAR_LIST;
2152 vimvars[idx].vv_list = val;
2153 if (val != NULL)
2154 ++val->lv_refcount;
2155}
2156
2157/*
2158 * Set Dictionary v: variable to "val".
2159 */
2160 void
2161set_vim_var_dict(int idx, dict_T *val)
2162{
2163 clear_tv(&vimvars[idx].vv_di.di_tv);
2164 vimvars[idx].vv_type = VAR_DICT;
2165 vimvars[idx].vv_dict = val;
2166 if (val != NULL)
2167 {
2168 ++val->dv_refcount;
2169 dict_set_items_ro(val);
2170 }
2171}
2172
2173/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002174 * Set the v:argv list.
2175 */
2176 void
2177set_argv_var(char **argv, int argc)
2178{
2179 list_T *l = list_alloc();
2180 int i;
2181
2182 if (l == NULL)
2183 getout(1);
2184 l->lv_lock = VAR_FIXED;
2185 for (i = 0; i < argc; ++i)
2186 {
2187 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2188 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002189 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002190 }
2191 set_vim_var_list(VV_ARGV, l);
2192}
2193
2194/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002195 * Set v:register if needed.
2196 */
2197 void
2198set_reg_var(int c)
2199{
2200 char_u regname;
2201
2202 if (c == 0 || c == ' ')
2203 regname = '"';
2204 else
2205 regname = c;
2206 // Avoid free/alloc when the value is already right.
2207 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2208 set_vim_var_string(VV_REG, &regname, 1);
2209}
2210
2211/*
2212 * Get or set v:exception. If "oldval" == NULL, return the current value.
2213 * Otherwise, restore the value to "oldval" and return NULL.
2214 * Must always be called in pairs to save and restore v:exception! Does not
2215 * take care of memory allocations.
2216 */
2217 char_u *
2218v_exception(char_u *oldval)
2219{
2220 if (oldval == NULL)
2221 return vimvars[VV_EXCEPTION].vv_str;
2222
2223 vimvars[VV_EXCEPTION].vv_str = oldval;
2224 return NULL;
2225}
2226
2227/*
2228 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2229 * Otherwise, restore the value to "oldval" and return NULL.
2230 * Must always be called in pairs to save and restore v:throwpoint! Does not
2231 * take care of memory allocations.
2232 */
2233 char_u *
2234v_throwpoint(char_u *oldval)
2235{
2236 if (oldval == NULL)
2237 return vimvars[VV_THROWPOINT].vv_str;
2238
2239 vimvars[VV_THROWPOINT].vv_str = oldval;
2240 return NULL;
2241}
2242
2243/*
2244 * Set v:cmdarg.
2245 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2246 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2247 * Must always be called in pairs!
2248 */
2249 char_u *
2250set_cmdarg(exarg_T *eap, char_u *oldarg)
2251{
2252 char_u *oldval;
2253 char_u *newval;
2254 unsigned len;
2255
2256 oldval = vimvars[VV_CMDARG].vv_str;
2257 if (eap == NULL)
2258 {
2259 vim_free(oldval);
2260 vimvars[VV_CMDARG].vv_str = oldarg;
2261 return NULL;
2262 }
2263
2264 if (eap->force_bin == FORCE_BIN)
2265 len = 6;
2266 else if (eap->force_bin == FORCE_NOBIN)
2267 len = 8;
2268 else
2269 len = 0;
2270
2271 if (eap->read_edit)
2272 len += 7;
2273
2274 if (eap->force_ff != 0)
2275 len += 10; // " ++ff=unix"
2276 if (eap->force_enc != 0)
2277 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2278 if (eap->bad_char != 0)
2279 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2280
2281 newval = alloc(len + 1);
2282 if (newval == NULL)
2283 return NULL;
2284
2285 if (eap->force_bin == FORCE_BIN)
2286 sprintf((char *)newval, " ++bin");
2287 else if (eap->force_bin == FORCE_NOBIN)
2288 sprintf((char *)newval, " ++nobin");
2289 else
2290 *newval = NUL;
2291
2292 if (eap->read_edit)
2293 STRCAT(newval, " ++edit");
2294
2295 if (eap->force_ff != 0)
2296 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2297 eap->force_ff == 'u' ? "unix"
2298 : eap->force_ff == 'd' ? "dos"
2299 : "mac");
2300 if (eap->force_enc != 0)
2301 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2302 eap->cmd + eap->force_enc);
2303 if (eap->bad_char == BAD_KEEP)
2304 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2305 else if (eap->bad_char == BAD_DROP)
2306 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2307 else if (eap->bad_char != 0)
2308 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2309 vimvars[VV_CMDARG].vv_str = newval;
2310 return oldval;
2311}
2312
2313/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002314 * Get the value of internal variable "name".
2315 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2316 */
2317 int
2318get_var_tv(
2319 char_u *name,
2320 int len, // length of "name"
2321 typval_T *rettv, // NULL when only checking existence
2322 dictitem_T **dip, // non-NULL when typval's dict item is needed
2323 int verbose, // may give error message
2324 int no_autoload) // do not use script autoloading
2325{
2326 int ret = OK;
2327 typval_T *tv = NULL;
2328 dictitem_T *v;
2329 int cc;
2330
2331 // truncate the name, so that we can use strcmp()
2332 cc = name[len];
2333 name[len] = NUL;
2334
2335 // Check for user-defined variables.
2336 v = find_var(name, NULL, no_autoload);
2337 if (v != NULL)
2338 {
2339 tv = &v->di_tv;
2340 if (dip != NULL)
2341 *dip = v;
2342 }
2343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2345 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002346 imported_T *import = find_imported(name, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347
2348 // imported variable from another script
2349 if (import != NULL)
2350 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002351 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2353 + import->imp_var_vals_idx;
2354 tv = sv->sv_tv;
2355 }
2356 }
2357
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002358 if (tv == NULL)
2359 {
2360 if (rettv != NULL && verbose)
2361 semsg(_(e_undefvar), name);
2362 ret = FAIL;
2363 }
2364 else if (rettv != NULL)
2365 copy_tv(tv, rettv);
2366
2367 name[len] = cc;
2368
2369 return ret;
2370}
2371
2372/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002373 * Check if variable "name[len]" is a local variable or an argument.
2374 * If so, "*eval_lavars_used" is set to TRUE.
2375 */
2376 void
2377check_vars(char_u *name, int len)
2378{
2379 int cc;
2380 char_u *varname;
2381 hashtab_T *ht;
2382
2383 if (eval_lavars_used == NULL)
2384 return;
2385
2386 // truncate the name, so that we can use strcmp()
2387 cc = name[len];
2388 name[len] = NUL;
2389
2390 ht = find_var_ht(name, &varname);
2391 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2392 {
2393 if (find_var(name, NULL, TRUE) != NULL)
2394 *eval_lavars_used = TRUE;
2395 }
2396
2397 name[len] = cc;
2398}
2399
2400/*
2401 * Find variable "name" in the list of variables.
2402 * Return a pointer to it if found, NULL if not found.
2403 * Careful: "a:0" variables don't have a name.
2404 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2405 * hashtab_T used.
2406 */
2407 dictitem_T *
2408find_var(char_u *name, hashtab_T **htp, int no_autoload)
2409{
2410 char_u *varname;
2411 hashtab_T *ht;
2412 dictitem_T *ret = NULL;
2413
2414 ht = find_var_ht(name, &varname);
2415 if (htp != NULL)
2416 *htp = ht;
2417 if (ht == NULL)
2418 return NULL;
2419 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2420 if (ret != NULL)
2421 return ret;
2422
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002423 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002424 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2425}
2426
2427/*
2428 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002429 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002430 * Returns NULL if not found.
2431 */
2432 dictitem_T *
2433find_var_in_ht(
2434 hashtab_T *ht,
2435 int htname,
2436 char_u *varname,
2437 int no_autoload)
2438{
2439 hashitem_T *hi;
2440
2441 if (*varname == NUL)
2442 {
2443 // Must be something like "s:", otherwise "ht" would be NULL.
2444 switch (htname)
2445 {
2446 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2447 case 'g': return &globvars_var;
2448 case 'v': return &vimvars_var;
2449 case 'b': return &curbuf->b_bufvar;
2450 case 'w': return &curwin->w_winvar;
2451 case 't': return &curtab->tp_winvar;
2452 case 'l': return get_funccal_local_var();
2453 case 'a': return get_funccal_args_var();
2454 }
2455 return NULL;
2456 }
2457
2458 hi = hash_find(ht, varname);
2459 if (HASHITEM_EMPTY(hi))
2460 {
2461 // For global variables we may try auto-loading the script. If it
2462 // worked find the variable again. Don't auto-load a script if it was
2463 // loaded already, otherwise it would be loaded every time when
2464 // checking if a function name is a Funcref variable.
2465 if (ht == &globvarht && !no_autoload)
2466 {
2467 // Note: script_autoload() may make "hi" invalid. It must either
2468 // be obtained again or not used.
2469 if (!script_autoload(varname, FALSE) || aborting())
2470 return NULL;
2471 hi = hash_find(ht, varname);
2472 }
2473 if (HASHITEM_EMPTY(hi))
2474 return NULL;
2475 }
2476 return HI2DI(hi);
2477}
2478
2479/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 * Get the script-local hashtab. NULL if not in a script context.
2481 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002482 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483get_script_local_ht(void)
2484{
2485 scid_T sid = current_sctx.sc_sid;
2486
2487 if (sid > 0 && sid <= script_items.ga_len)
2488 return &SCRIPT_VARS(sid);
2489 return NULL;
2490}
2491
2492/*
2493 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002494 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002496 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2498{
2499 hashtab_T *ht = get_script_local_ht();
2500 char_u buffer[30];
2501 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002502 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 hashitem_T *hi;
2504
2505 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002506 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 if (len < sizeof(buffer) - 1)
2508 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002509 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 vim_strncpy(buffer, name, len);
2511 p = buffer;
2512 }
2513 else
2514 {
2515 p = vim_strnsave(name, (int)len);
2516 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002517 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 }
2519
2520 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002521 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522
2523 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002524 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2525 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526
2527 if (p != buffer)
2528 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002529 // Don't return "buffer", gcc complains.
2530 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531}
2532
2533/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002534 * Find the hashtab used for a variable name.
2535 * Return NULL if the name is not valid.
2536 * Set "varname" to the start of name without ':'.
2537 */
2538 hashtab_T *
2539find_var_ht(char_u *name, char_u **varname)
2540{
2541 hashitem_T *hi;
2542 hashtab_T *ht;
2543
2544 if (name[0] == NUL)
2545 return NULL;
2546 if (name[1] != ':')
2547 {
2548 // The name must not start with a colon or #.
2549 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2550 return NULL;
2551 *varname = name;
2552
2553 // "version" is "v:version" in all scopes if scriptversion < 3.
2554 // Same for a few other variables marked with VV_COMPAT.
2555 if (current_sctx.sc_version < 3)
2556 {
2557 hi = hash_find(&compat_hashtab, name);
2558 if (!HASHITEM_EMPTY(hi))
2559 return &compat_hashtab;
2560 }
2561
2562 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 if (ht != NULL)
2564 return ht; // local variable
2565
2566 // in Vim9 script items at the script level are script-local
2567 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2568 {
2569 ht = get_script_local_ht();
2570 if (ht != NULL)
2571 return ht;
2572 }
2573
2574 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002575 }
2576 *varname = name + 2;
2577 if (*name == 'g') // global variable
2578 return &globvarht;
2579 // There must be no ':' or '#' in the rest of the name, unless g: is used
2580 if (vim_strchr(name + 2, ':') != NULL
2581 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2582 return NULL;
2583 if (*name == 'b') // buffer variable
2584 return &curbuf->b_vars->dv_hashtab;
2585 if (*name == 'w') // window variable
2586 return &curwin->w_vars->dv_hashtab;
2587 if (*name == 't') // tab page variable
2588 return &curtab->tp_vars->dv_hashtab;
2589 if (*name == 'v') // v: variable
2590 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002591 if (get_current_funccal() != NULL
Bram Moolenaar822ba242020-05-24 23:00:18 +02002592 && get_current_funccal()->func->uf_dfunc_idx == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002594 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 if (*name == 'a') // a: function argument
2596 return get_funccal_args_ht();
2597 if (*name == 'l') // l: local function variable
2598 return get_funccal_local_ht();
2599 }
2600 if (*name == 's') // script variable
2601 {
2602 ht = get_script_local_ht();
2603 if (ht != NULL)
2604 return ht;
2605 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002606 return NULL;
2607}
2608
2609/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002610 * Get the string value of a (global/local) variable.
2611 * Note: see tv_get_string() for how long the pointer remains valid.
2612 * Returns NULL when it doesn't exist.
2613 */
2614 char_u *
2615get_var_value(char_u *name)
2616{
2617 dictitem_T *v;
2618
2619 v = find_var(name, NULL, FALSE);
2620 if (v == NULL)
2621 return NULL;
2622 return tv_get_string(&v->di_tv);
2623}
2624
2625/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002626 * Allocate a new hashtab for a sourced script. It will be used while
2627 * sourcing this script and when executing functions defined in the script.
2628 */
2629 void
2630new_script_vars(scid_T id)
2631{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002632 scriptvar_T *sv;
2633
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002634 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2635 if (sv == NULL)
2636 return;
2637 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002638 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002639}
2640
2641/*
2642 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2643 * point to it.
2644 */
2645 void
2646init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2647{
2648 hash_init(&dict->dv_hashtab);
2649 dict->dv_lock = 0;
2650 dict->dv_scope = scope;
2651 dict->dv_refcount = DO_NOT_FREE_CNT;
2652 dict->dv_copyID = 0;
2653 dict_var->di_tv.vval.v_dict = dict;
2654 dict_var->di_tv.v_type = VAR_DICT;
2655 dict_var->di_tv.v_lock = VAR_FIXED;
2656 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2657 dict_var->di_key[0] = NUL;
2658}
2659
2660/*
2661 * Unreference a dictionary initialized by init_var_dict().
2662 */
2663 void
2664unref_var_dict(dict_T *dict)
2665{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002666 // Now the dict needs to be freed if no one else is using it, go back to
2667 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002668 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2669 dict_unref(dict);
2670}
2671
2672/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002673 * Clean up a list of internal variables.
2674 * Frees all allocated variables and the value they contain.
2675 * Clears hashtab "ht", does not free it.
2676 */
2677 void
2678vars_clear(hashtab_T *ht)
2679{
2680 vars_clear_ext(ht, TRUE);
2681}
2682
2683/*
2684 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2685 */
2686 void
2687vars_clear_ext(hashtab_T *ht, int free_val)
2688{
2689 int todo;
2690 hashitem_T *hi;
2691 dictitem_T *v;
2692
2693 hash_lock(ht);
2694 todo = (int)ht->ht_used;
2695 for (hi = ht->ht_array; todo > 0; ++hi)
2696 {
2697 if (!HASHITEM_EMPTY(hi))
2698 {
2699 --todo;
2700
2701 // Free the variable. Don't remove it from the hashtab,
2702 // ht_array might change then. hash_clear() takes care of it
2703 // later.
2704 v = HI2DI(hi);
2705 if (free_val)
2706 clear_tv(&v->di_tv);
2707 if (v->di_flags & DI_FLAGS_ALLOC)
2708 vim_free(v);
2709 }
2710 }
2711 hash_clear(ht);
2712 ht->ht_used = 0;
2713}
2714
2715/*
2716 * Delete a variable from hashtab "ht" at item "hi".
2717 * Clear the variable value and free the dictitem.
2718 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002719 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002720delete_var(hashtab_T *ht, hashitem_T *hi)
2721{
2722 dictitem_T *di = HI2DI(hi);
2723
2724 hash_remove(ht, hi);
2725 clear_tv(&di->di_tv);
2726 vim_free(di);
2727}
2728
2729/*
2730 * List the value of one internal variable.
2731 */
2732 static void
2733list_one_var(dictitem_T *v, char *prefix, int *first)
2734{
2735 char_u *tofree;
2736 char_u *s;
2737 char_u numbuf[NUMBUFLEN];
2738
2739 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2740 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2741 s == NULL ? (char_u *)"" : s, first);
2742 vim_free(tofree);
2743}
2744
2745 static void
2746list_one_var_a(
2747 char *prefix,
2748 char_u *name,
2749 int type,
2750 char_u *string,
2751 int *first) // when TRUE clear rest of screen and set to FALSE
2752{
2753 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2754 msg_start();
2755 msg_puts(prefix);
2756 if (name != NULL) // "a:" vars don't have a name stored
2757 msg_puts((char *)name);
2758 msg_putchar(' ');
2759 msg_advance(22);
2760 if (type == VAR_NUMBER)
2761 msg_putchar('#');
2762 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2763 msg_putchar('*');
2764 else if (type == VAR_LIST)
2765 {
2766 msg_putchar('[');
2767 if (*string == '[')
2768 ++string;
2769 }
2770 else if (type == VAR_DICT)
2771 {
2772 msg_putchar('{');
2773 if (*string == '{')
2774 ++string;
2775 }
2776 else
2777 msg_putchar(' ');
2778
2779 msg_outtrans(string);
2780
2781 if (type == VAR_FUNC || type == VAR_PARTIAL)
2782 msg_puts("()");
2783 if (*first)
2784 {
2785 msg_clr_eos();
2786 *first = FALSE;
2787 }
2788}
2789
2790/*
2791 * Set variable "name" to value in "tv".
2792 * If the variable already exists, the value is updated.
2793 * Otherwise the variable is created.
2794 */
2795 void
2796set_var(
2797 char_u *name,
2798 typval_T *tv,
2799 int copy) // make copy of value in "tv"
2800{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002802}
2803
2804/*
2805 * Set variable "name" to value in "tv".
2806 * If the variable already exists and "is_const" is FALSE the value is updated.
2807 * Otherwise the variable is created.
2808 */
2809 void
2810set_var_const(
2811 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002813 typval_T *tv,
2814 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002816{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002818 char_u *varname;
2819 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002820 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002821
2822 ht = find_var_ht(name, &varname);
2823 if (ht == NULL || *varname == NUL)
2824 {
2825 semsg(_(e_illvar), name);
2826 return;
2827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 is_script_local = ht == get_script_local_ht();
2829
2830 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002831
2832 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 if (di == NULL)
2834 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002835
2836 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002838 return;
2839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002841 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002843 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 if (flags & LET_IS_CONST)
2845 {
2846 emsg(_(e_cannot_mod));
2847 return;
2848 }
2849
2850 if (var_check_ro(di->di_flags, name, FALSE)
2851 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2852 return;
2853
2854 if ((flags & LET_NO_COMMAND) == 0
2855 && is_script_local
2856 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2857 {
2858 semsg(_("E1041: Redefining script item %s"), name);
2859 return;
2860 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002861 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 else
2863 // can only redefine once
2864 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002865
2866 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002869 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002870 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002871 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002873 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002875 if (copy || tv->v_type != VAR_STRING)
2876 {
2877 char_u *val = tv_get_string(tv);
2878
2879 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002880 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 if (di->di_tv.vval.v_string == NULL)
2882 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002883 }
2884 else
2885 {
2886 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002888 tv->vval.v_string = NULL;
2889 }
2890 return;
2891 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002895 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002897#ifdef FEAT_SEARCH_EXTRA
2898 else if (STRCMP(varname, "hlsearch") == 0)
2899 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002901 redraw_all_later(SOME_VALID);
2902 }
2903#endif
2904 return;
2905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002907 {
2908 semsg(_("E963: setting %s to value with wrong type"), name);
2909 return;
2910 }
2911 }
2912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002914 }
2915 else // add a new variable
2916 {
2917 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002918 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002919 {
2920 semsg(_(e_illvar), name);
2921 return;
2922 }
2923
2924 // Make sure the variable name is valid.
2925 if (!valid_varname(varname))
2926 return;
2927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2929 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002930 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 STRCPY(di->di_key, varname);
2932 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002933 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002935 return;
2936 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 di->di_flags = DI_FLAGS_ALLOC;
2938 if (flags & LET_IS_CONST)
2939 di->di_flags |= DI_FLAGS_LOCK;
2940
2941 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2942 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002943 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944
2945 // Store a pointer to the typval_T, so that it can be found by
2946 // index instead of using a hastab lookup.
2947 if (ga_grow(&si->sn_var_vals, 1) == OK)
2948 {
2949 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2950 + si->sn_var_vals.ga_len;
2951 sv->sv_name = di->di_key;
2952 sv->sv_tv = &di->di_tv;
2953 sv->sv_type = type == NULL ? &t_any : type;
2954 sv->sv_const = (flags & LET_IS_CONST);
2955 sv->sv_export = is_export;
2956 ++si->sn_var_vals.ga_len;
2957
2958 // let ex_export() know the export worked.
2959 is_export = FALSE;
2960 }
2961 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002962 }
2963
2964 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002966 else
2967 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 di->di_tv = *tv;
2969 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002970 init_tv(tv);
2971 }
2972
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 if (flags & LET_IS_CONST)
2974 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002975}
2976
2977/*
2978 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2979 * Also give an error message.
2980 */
2981 int
2982var_check_ro(int flags, char_u *name, int use_gettext)
2983{
2984 if (flags & DI_FLAGS_RO)
2985 {
2986 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
2987 return TRUE;
2988 }
2989 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
2990 {
2991 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
2992 return TRUE;
2993 }
2994 return FALSE;
2995}
2996
2997/*
2998 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
2999 * Also give an error message.
3000 */
3001 int
3002var_check_fixed(int flags, char_u *name, int use_gettext)
3003{
3004 if (flags & DI_FLAGS_FIX)
3005 {
3006 semsg(_("E795: Cannot delete variable %s"),
3007 use_gettext ? (char_u *)_(name) : name);
3008 return TRUE;
3009 }
3010 return FALSE;
3011}
3012
3013/*
3014 * Check if a funcref is assigned to a valid variable name.
3015 * Return TRUE and give an error if not.
3016 */
3017 int
3018var_check_func_name(
3019 char_u *name, // points to start of variable name
3020 int new_var) // TRUE when creating the variable
3021{
3022 // Allow for w: b: s: and t:.
3023 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3024 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3025 ? name[2] : name[0]))
3026 {
3027 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3028 name);
3029 return TRUE;
3030 }
3031 // Don't allow hiding a function. When "v" is not NULL we might be
3032 // assigning another function to the same var, the type is checked
3033 // below.
3034 if (new_var && function_exists(name, FALSE))
3035 {
3036 semsg(_("E705: Variable name conflicts with existing function: %s"),
3037 name);
3038 return TRUE;
3039 }
3040 return FALSE;
3041}
3042
3043/*
3044 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3045 * Also give an error message, using "name" or _("name") when use_gettext is
3046 * TRUE.
3047 */
3048 int
3049var_check_lock(int lock, char_u *name, int use_gettext)
3050{
3051 if (lock & VAR_LOCKED)
3052 {
3053 semsg(_("E741: Value is locked: %s"),
3054 name == NULL ? (char_u *)_("Unknown")
3055 : use_gettext ? (char_u *)_(name)
3056 : name);
3057 return TRUE;
3058 }
3059 if (lock & VAR_FIXED)
3060 {
3061 semsg(_("E742: Cannot change value of %s"),
3062 name == NULL ? (char_u *)_("Unknown")
3063 : use_gettext ? (char_u *)_(name)
3064 : name);
3065 return TRUE;
3066 }
3067 return FALSE;
3068}
3069
3070/*
3071 * Check if a variable name is valid.
3072 * Return FALSE and give an error if not.
3073 */
3074 int
3075valid_varname(char_u *varname)
3076{
3077 char_u *p;
3078
3079 for (p = varname; *p != NUL; ++p)
3080 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3081 && *p != AUTOLOAD_CHAR)
3082 {
3083 semsg(_(e_illvar), varname);
3084 return FALSE;
3085 }
3086 return TRUE;
3087}
3088
3089/*
3090 * getwinvar() and gettabwinvar()
3091 */
3092 static void
3093getwinvar(
3094 typval_T *argvars,
3095 typval_T *rettv,
3096 int off) // 1 for gettabwinvar()
3097{
3098 win_T *win;
3099 char_u *varname;
3100 dictitem_T *v;
3101 tabpage_T *tp = NULL;
3102 int done = FALSE;
3103 win_T *oldcurwin;
3104 tabpage_T *oldtabpage;
3105 int need_switch_win;
3106
3107 if (off == 1)
3108 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3109 else
3110 tp = curtab;
3111 win = find_win_by_nr(&argvars[off], tp);
3112 varname = tv_get_string_chk(&argvars[off + 1]);
3113 ++emsg_off;
3114
3115 rettv->v_type = VAR_STRING;
3116 rettv->vval.v_string = NULL;
3117
3118 if (win != NULL && varname != NULL)
3119 {
3120 // Set curwin to be our win, temporarily. Also set the tabpage,
3121 // otherwise the window is not valid. Only do this when needed,
3122 // autocommands get blocked.
3123 need_switch_win = !(tp == curtab && win == curwin);
3124 if (!need_switch_win
3125 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3126 {
3127 if (*varname == '&')
3128 {
3129 if (varname[1] == NUL)
3130 {
3131 // get all window-local options in a dict
3132 dict_T *opts = get_winbuf_options(FALSE);
3133
3134 if (opts != NULL)
3135 {
3136 rettv_dict_set(rettv, opts);
3137 done = TRUE;
3138 }
3139 }
3140 else if (get_option_tv(&varname, rettv, 1) == OK)
3141 // window-local-option
3142 done = TRUE;
3143 }
3144 else
3145 {
3146 // Look up the variable.
3147 // Let getwinvar({nr}, "") return the "w:" dictionary.
3148 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3149 varname, FALSE);
3150 if (v != NULL)
3151 {
3152 copy_tv(&v->di_tv, rettv);
3153 done = TRUE;
3154 }
3155 }
3156 }
3157
3158 if (need_switch_win)
3159 // restore previous notion of curwin
3160 restore_win(oldcurwin, oldtabpage, TRUE);
3161 }
3162
3163 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3164 // use the default return value
3165 copy_tv(&argvars[off + 2], rettv);
3166
3167 --emsg_off;
3168}
3169
3170/*
3171 * "setwinvar()" and "settabwinvar()" functions
3172 */
3173 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003174setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003175{
3176 win_T *win;
3177 win_T *save_curwin;
3178 tabpage_T *save_curtab;
3179 int need_switch_win;
3180 char_u *varname, *winvarname;
3181 typval_T *varp;
3182 char_u nbuf[NUMBUFLEN];
3183 tabpage_T *tp = NULL;
3184
3185 if (check_secure())
3186 return;
3187
3188 if (off == 1)
3189 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3190 else
3191 tp = curtab;
3192 win = find_win_by_nr(&argvars[off], tp);
3193 varname = tv_get_string_chk(&argvars[off + 1]);
3194 varp = &argvars[off + 2];
3195
3196 if (win != NULL && varname != NULL && varp != NULL)
3197 {
3198 need_switch_win = !(tp == curtab && win == curwin);
3199 if (!need_switch_win
3200 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3201 {
3202 if (*varname == '&')
3203 {
3204 long numval;
3205 char_u *strval;
3206 int error = FALSE;
3207
3208 ++varname;
3209 numval = (long)tv_get_number_chk(varp, &error);
3210 strval = tv_get_string_buf_chk(varp, nbuf);
3211 if (!error && strval != NULL)
3212 set_option_value(varname, numval, strval, OPT_LOCAL);
3213 }
3214 else
3215 {
3216 winvarname = alloc(STRLEN(varname) + 3);
3217 if (winvarname != NULL)
3218 {
3219 STRCPY(winvarname, "w:");
3220 STRCPY(winvarname + 2, varname);
3221 set_var(winvarname, varp, TRUE);
3222 vim_free(winvarname);
3223 }
3224 }
3225 }
3226 if (need_switch_win)
3227 restore_win(save_curwin, save_curtab, TRUE);
3228 }
3229}
3230
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003231/*
3232 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3233 * v:option_type, and v:option_command.
3234 */
3235 void
3236reset_v_option_vars(void)
3237{
3238 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3239 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3240 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3241 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3242 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3243 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3244}
3245
3246/*
3247 * Add an assert error to v:errors.
3248 */
3249 void
3250assert_error(garray_T *gap)
3251{
3252 struct vimvar *vp = &vimvars[VV_ERRORS];
3253
3254 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003255 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003256 set_vim_var_list(VV_ERRORS, list_alloc());
3257 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3258}
3259
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003260 int
3261var_exists(char_u *var)
3262{
3263 char_u *name;
3264 char_u *tofree;
3265 typval_T tv;
3266 int len = 0;
3267 int n = FALSE;
3268
3269 // get_name_len() takes care of expanding curly braces
3270 name = var;
3271 len = get_name_len(&var, &tofree, TRUE, FALSE);
3272 if (len > 0)
3273 {
3274 if (tofree != NULL)
3275 name = tofree;
3276 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3277 if (n)
3278 {
3279 // handle d.key, l[idx], f(expr)
Bram Moolenaar32e35112020-05-14 22:41:15 +02003280 n = (handle_subscript(&var, &tv, EVAL_EVALUATE,
3281 FALSE, name, &name) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003282 if (n)
3283 clear_tv(&tv);
3284 }
3285 }
3286 if (*var != NUL)
3287 n = FALSE;
3288
3289 vim_free(tofree);
3290 return n;
3291}
3292
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003293static lval_T *redir_lval = NULL;
3294#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3295static garray_T redir_ga; // only valid when redir_lval is not NULL
3296static char_u *redir_endp = NULL;
3297static char_u *redir_varname = NULL;
3298
3299/*
3300 * Start recording command output to a variable
3301 * When "append" is TRUE append to an existing variable.
3302 * Returns OK if successfully completed the setup. FAIL otherwise.
3303 */
3304 int
3305var_redir_start(char_u *name, int append)
3306{
3307 int save_emsg;
3308 int err;
3309 typval_T tv;
3310
3311 // Catch a bad name early.
3312 if (!eval_isnamec1(*name))
3313 {
3314 emsg(_(e_invarg));
3315 return FAIL;
3316 }
3317
3318 // Make a copy of the name, it is used in redir_lval until redir ends.
3319 redir_varname = vim_strsave(name);
3320 if (redir_varname == NULL)
3321 return FAIL;
3322
3323 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3324 if (redir_lval == NULL)
3325 {
3326 var_redir_stop();
3327 return FAIL;
3328 }
3329
3330 // The output is stored in growarray "redir_ga" until redirection ends.
3331 ga_init2(&redir_ga, (int)sizeof(char), 500);
3332
3333 // Parse the variable name (can be a dict or list entry).
3334 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3335 FNE_CHECK_START);
3336 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3337 {
3338 clear_lval(redir_lval);
3339 if (redir_endp != NULL && *redir_endp != NUL)
3340 // Trailing characters are present after the variable name
3341 emsg(_(e_trailing));
3342 else
3343 emsg(_(e_invarg));
3344 redir_endp = NULL; // don't store a value, only cleanup
3345 var_redir_stop();
3346 return FAIL;
3347 }
3348
3349 // check if we can write to the variable: set it to or append an empty
3350 // string
3351 save_emsg = did_emsg;
3352 did_emsg = FALSE;
3353 tv.v_type = VAR_STRING;
3354 tv.vval.v_string = (char_u *)"";
3355 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003357 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003359 clear_lval(redir_lval);
3360 err = did_emsg;
3361 did_emsg |= save_emsg;
3362 if (err)
3363 {
3364 redir_endp = NULL; // don't store a value, only cleanup
3365 var_redir_stop();
3366 return FAIL;
3367 }
3368
3369 return OK;
3370}
3371
3372/*
3373 * Append "value[value_len]" to the variable set by var_redir_start().
3374 * The actual appending is postponed until redirection ends, because the value
3375 * appended may in fact be the string we write to, changing it may cause freed
3376 * memory to be used:
3377 * :redir => foo
3378 * :let foo
3379 * :redir END
3380 */
3381 void
3382var_redir_str(char_u *value, int value_len)
3383{
3384 int len;
3385
3386 if (redir_lval == NULL)
3387 return;
3388
3389 if (value_len == -1)
3390 len = (int)STRLEN(value); // Append the entire string
3391 else
3392 len = value_len; // Append only "value_len" characters
3393
3394 if (ga_grow(&redir_ga, len) == OK)
3395 {
3396 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3397 redir_ga.ga_len += len;
3398 }
3399 else
3400 var_redir_stop();
3401}
3402
3403/*
3404 * Stop redirecting command output to a variable.
3405 * Frees the allocated memory.
3406 */
3407 void
3408var_redir_stop(void)
3409{
3410 typval_T tv;
3411
3412 if (EVALCMD_BUSY)
3413 {
3414 redir_lval = NULL;
3415 return;
3416 }
3417
3418 if (redir_lval != NULL)
3419 {
3420 // If there was no error: assign the text to the variable.
3421 if (redir_endp != NULL)
3422 {
3423 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3424 tv.v_type = VAR_STRING;
3425 tv.vval.v_string = redir_ga.ga_data;
3426 // Call get_lval() again, if it's inside a Dict or List it may
3427 // have changed.
3428 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3429 FALSE, FALSE, 0, FNE_CHECK_START);
3430 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003431 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003432 (char_u *)".");
3433 clear_lval(redir_lval);
3434 }
3435
3436 // free the collected output
3437 VIM_CLEAR(redir_ga.ga_data);
3438
3439 VIM_CLEAR(redir_lval);
3440 }
3441 VIM_CLEAR(redir_varname);
3442}
3443
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003444/*
3445 * "gettabvar()" function
3446 */
3447 void
3448f_gettabvar(typval_T *argvars, typval_T *rettv)
3449{
3450 win_T *oldcurwin;
3451 tabpage_T *tp, *oldtabpage;
3452 dictitem_T *v;
3453 char_u *varname;
3454 int done = FALSE;
3455
3456 rettv->v_type = VAR_STRING;
3457 rettv->vval.v_string = NULL;
3458
3459 varname = tv_get_string_chk(&argvars[1]);
3460 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3461 if (tp != NULL && varname != NULL)
3462 {
3463 // Set tp to be our tabpage, temporarily. Also set the window to the
3464 // first window in the tabpage, otherwise the window is not valid.
3465 if (switch_win(&oldcurwin, &oldtabpage,
3466 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3467 : tp->tp_firstwin, tp, TRUE) == OK)
3468 {
3469 // look up the variable
3470 // Let gettabvar({nr}, "") return the "t:" dictionary.
3471 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3472 if (v != NULL)
3473 {
3474 copy_tv(&v->di_tv, rettv);
3475 done = TRUE;
3476 }
3477 }
3478
3479 // restore previous notion of curwin
3480 restore_win(oldcurwin, oldtabpage, TRUE);
3481 }
3482
3483 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3484 copy_tv(&argvars[2], rettv);
3485}
3486
3487/*
3488 * "gettabwinvar()" function
3489 */
3490 void
3491f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3492{
3493 getwinvar(argvars, rettv, 1);
3494}
3495
3496/*
3497 * "getwinvar()" function
3498 */
3499 void
3500f_getwinvar(typval_T *argvars, typval_T *rettv)
3501{
3502 getwinvar(argvars, rettv, 0);
3503}
3504
3505/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003506 * "getbufvar()" function
3507 */
3508 void
3509f_getbufvar(typval_T *argvars, typval_T *rettv)
3510{
3511 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003512 char_u *varname;
3513 dictitem_T *v;
3514 int done = FALSE;
3515
3516 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3517 varname = tv_get_string_chk(&argvars[1]);
3518 ++emsg_off;
3519 buf = tv_get_buf(&argvars[0], FALSE);
3520
3521 rettv->v_type = VAR_STRING;
3522 rettv->vval.v_string = NULL;
3523
3524 if (buf != NULL && varname != NULL)
3525 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003526 if (*varname == '&')
3527 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003528 buf_T *save_curbuf = curbuf;
3529
3530 // set curbuf to be our buf, temporarily
3531 curbuf = buf;
3532
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003533 if (varname[1] == NUL)
3534 {
3535 // get all buffer-local options in a dict
3536 dict_T *opts = get_winbuf_options(TRUE);
3537
3538 if (opts != NULL)
3539 {
3540 rettv_dict_set(rettv, opts);
3541 done = TRUE;
3542 }
3543 }
3544 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3545 // buffer-local-option
3546 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003547
3548 // restore previous notion of curbuf
3549 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003550 }
3551 else
3552 {
3553 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003554 if (*varname == NUL)
3555 // Let getbufvar({nr}, "") return the "b:" dictionary.
3556 v = &buf->b_bufvar;
3557 else
3558 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3559 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003560 if (v != NULL)
3561 {
3562 copy_tv(&v->di_tv, rettv);
3563 done = TRUE;
3564 }
3565 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003566 }
3567
3568 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3569 // use the default value
3570 copy_tv(&argvars[2], rettv);
3571
3572 --emsg_off;
3573}
3574
3575/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003576 * "settabvar()" function
3577 */
3578 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003579f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003580{
3581 tabpage_T *save_curtab;
3582 tabpage_T *tp;
3583 char_u *varname, *tabvarname;
3584 typval_T *varp;
3585
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003586 if (check_secure())
3587 return;
3588
3589 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3590 varname = tv_get_string_chk(&argvars[1]);
3591 varp = &argvars[2];
3592
3593 if (varname != NULL && varp != NULL && tp != NULL)
3594 {
3595 save_curtab = curtab;
3596 goto_tabpage_tp(tp, FALSE, FALSE);
3597
3598 tabvarname = alloc(STRLEN(varname) + 3);
3599 if (tabvarname != NULL)
3600 {
3601 STRCPY(tabvarname, "t:");
3602 STRCPY(tabvarname + 2, varname);
3603 set_var(tabvarname, varp, TRUE);
3604 vim_free(tabvarname);
3605 }
3606
3607 // Restore current tabpage
3608 if (valid_tabpage(save_curtab))
3609 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3610 }
3611}
3612
3613/*
3614 * "settabwinvar()" function
3615 */
3616 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003617f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003618{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003619 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003620}
3621
3622/*
3623 * "setwinvar()" function
3624 */
3625 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003626f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003627{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003628 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003629}
3630
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003631/*
3632 * "setbufvar()" function
3633 */
3634 void
3635f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3636{
3637 buf_T *buf;
3638 char_u *varname, *bufvarname;
3639 typval_T *varp;
3640 char_u nbuf[NUMBUFLEN];
3641
3642 if (check_secure())
3643 return;
3644 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3645 varname = tv_get_string_chk(&argvars[1]);
3646 buf = tv_get_buf(&argvars[0], FALSE);
3647 varp = &argvars[2];
3648
3649 if (buf != NULL && varname != NULL && varp != NULL)
3650 {
3651 if (*varname == '&')
3652 {
3653 long numval;
3654 char_u *strval;
3655 int error = FALSE;
3656 aco_save_T aco;
3657
3658 // set curbuf to be our buf, temporarily
3659 aucmd_prepbuf(&aco, buf);
3660
3661 ++varname;
3662 numval = (long)tv_get_number_chk(varp, &error);
3663 strval = tv_get_string_buf_chk(varp, nbuf);
3664 if (!error && strval != NULL)
3665 set_option_value(varname, numval, strval, OPT_LOCAL);
3666
3667 // reset notion of buffer
3668 aucmd_restbuf(&aco);
3669 }
3670 else
3671 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003672 bufvarname = alloc(STRLEN(varname) + 3);
3673 if (bufvarname != NULL)
3674 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003675 buf_T *save_curbuf = curbuf;
3676
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003677 curbuf = buf;
3678 STRCPY(bufvarname, "b:");
3679 STRCPY(bufvarname + 2, varname);
3680 set_var(bufvarname, varp, TRUE);
3681 vim_free(bufvarname);
3682 curbuf = save_curbuf;
3683 }
3684 }
3685 }
3686}
3687
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003688/*
3689 * Get a callback from "arg". It can be a Funcref or a function name.
3690 * When "arg" is zero return an empty string.
3691 * "cb_name" is not allocated.
3692 * "cb_name" is set to NULL for an invalid argument.
3693 */
3694 callback_T
3695get_callback(typval_T *arg)
3696{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003697 callback_T res;
3698 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003699
3700 res.cb_free_name = FALSE;
3701 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3702 {
3703 res.cb_partial = arg->vval.v_partial;
3704 ++res.cb_partial->pt_refcount;
3705 res.cb_name = partial_name(res.cb_partial);
3706 }
3707 else
3708 {
3709 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003710 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3711 && isdigit(*arg->vval.v_string))
3712 r = FAIL;
3713 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003714 {
3715 // Note that we don't make a copy of the string.
3716 res.cb_name = arg->vval.v_string;
3717 func_ref(res.cb_name);
3718 }
3719 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003720 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003721 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003722 r = FAIL;
3723
3724 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003725 {
3726 emsg(_("E921: Invalid callback argument"));
3727 res.cb_name = NULL;
3728 }
3729 }
3730 return res;
3731}
3732
3733/*
3734 * Copy a callback into a typval_T.
3735 */
3736 void
3737put_callback(callback_T *cb, typval_T *tv)
3738{
3739 if (cb->cb_partial != NULL)
3740 {
3741 tv->v_type = VAR_PARTIAL;
3742 tv->vval.v_partial = cb->cb_partial;
3743 ++tv->vval.v_partial->pt_refcount;
3744 }
3745 else
3746 {
3747 tv->v_type = VAR_FUNC;
3748 tv->vval.v_string = vim_strsave(cb->cb_name);
3749 func_ref(cb->cb_name);
3750 }
3751}
3752
3753/*
3754 * Make a copy of "src" into "dest", allocating the function name if needed,
3755 * without incrementing the refcount.
3756 */
3757 void
3758set_callback(callback_T *dest, callback_T *src)
3759{
3760 if (src->cb_partial == NULL)
3761 {
3762 // just a function name, make a copy
3763 dest->cb_name = vim_strsave(src->cb_name);
3764 dest->cb_free_name = TRUE;
3765 }
3766 else
3767 {
3768 // cb_name is a pointer into cb_partial
3769 dest->cb_name = src->cb_name;
3770 dest->cb_free_name = FALSE;
3771 }
3772 dest->cb_partial = src->cb_partial;
3773}
3774
3775/*
3776 * Unref/free "callback" returned by get_callback() or set_callback().
3777 */
3778 void
3779free_callback(callback_T *callback)
3780{
3781 if (callback->cb_partial != NULL)
3782 {
3783 partial_unref(callback->cb_partial);
3784 callback->cb_partial = NULL;
3785 }
3786 else if (callback->cb_name != NULL)
3787 func_unref(callback->cb_name);
3788 if (callback->cb_free_name)
3789 {
3790 vim_free(callback->cb_name);
3791 callback->cb_free_name = FALSE;
3792 }
3793 callback->cb_name = NULL;
3794}
3795
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003796#endif // FEAT_EVAL