blob: 1f418ae22848af2a056a5cb523600bb34fd9d786 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200176static void list_one_var(dictitem_T *v, char *prefix, int *first);
177static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
178
179/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200180 * Initialize global and vim special variables
181 */
182 void
183evalvars_init(void)
184{
185 int i;
186 struct vimvar *p;
187
188 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
189 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
190 vimvardict.dv_lock = VAR_FIXED;
191 hash_init(&compat_hashtab);
192
193 for (i = 0; i < VV_LEN; ++i)
194 {
195 p = &vimvars[i];
196 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
197 {
198 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
199 getout(1);
200 }
201 STRCPY(p->vv_di.di_key, p->vv_name);
202 if (p->vv_flags & VV_RO)
203 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
204 else if (p->vv_flags & VV_RO_SBX)
205 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
206 else
207 p->vv_di.di_flags = DI_FLAGS_FIX;
208
209 // add to v: scope dict, unless the value is not always available
210 if (p->vv_type != VAR_UNKNOWN)
211 hash_add(&vimvarht, p->vv_di.di_key);
212 if (p->vv_flags & VV_COMPAT)
213 // add to compat scope dict
214 hash_add(&compat_hashtab, p->vv_di.di_key);
215 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200216 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
217 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200218
219 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
220 set_vim_var_nr(VV_HLSEARCH, 1L);
221 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
222 set_vim_var_list(VV_ERRORS, list_alloc());
223 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
224
225 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
226 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
227 set_vim_var_nr(VV_NONE, VVAL_NONE);
228 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100229 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200230
231 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
232 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
233 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
234 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
235 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
236 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
237 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
238 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
239 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
240 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
241 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
242
243 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
244
Bram Moolenaar439c0362020-06-06 15:58:03 +0200245 // Default for v:register is not 0 but '"'. This is adjusted once the
246 // clipboard has been setup by calling reset_reg_var().
247 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200248}
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{
Bram Moolenaared234f22020-10-15 20:42:20 +0200302 int i;
303 int idx;
304 int abort = FALSE;
305 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200308 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200309 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
310
Bram Moolenaared234f22020-10-15 20:42:20 +0200311 si = SCRIPT_ITEM(i);
312 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
313 {
314 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
315
316 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
317 }
318 }
319
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200320 return abort;
321}
322
323/*
324 * Set an internal variable to a string value. Creates the variable if it does
325 * not already exist.
326 */
327 void
328set_internal_string_var(char_u *name, char_u *value)
329{
330 char_u *val;
331 typval_T *tvp;
332
333 val = vim_strsave(value);
334 if (val != NULL)
335 {
336 tvp = alloc_string_tv(val);
337 if (tvp != NULL)
338 {
339 set_var(name, tvp, FALSE);
340 free_tv(tvp);
341 }
342 }
343}
344
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200345 int
346eval_charconvert(
347 char_u *enc_from,
348 char_u *enc_to,
349 char_u *fname_from,
350 char_u *fname_to)
351{
352 int err = FALSE;
353
354 set_vim_var_string(VV_CC_FROM, enc_from, -1);
355 set_vim_var_string(VV_CC_TO, enc_to, -1);
356 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
357 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
358 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
359 err = TRUE;
360 set_vim_var_string(VV_CC_FROM, NULL, -1);
361 set_vim_var_string(VV_CC_TO, NULL, -1);
362 set_vim_var_string(VV_FNAME_IN, NULL, -1);
363 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
364
365 if (err)
366 return FAIL;
367 return OK;
368}
369
370# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
371 int
372eval_printexpr(char_u *fname, char_u *args)
373{
374 int err = FALSE;
375
376 set_vim_var_string(VV_FNAME_IN, fname, -1);
377 set_vim_var_string(VV_CMDARG, args, -1);
378 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
379 err = TRUE;
380 set_vim_var_string(VV_FNAME_IN, NULL, -1);
381 set_vim_var_string(VV_CMDARG, NULL, -1);
382
383 if (err)
384 {
385 mch_remove(fname);
386 return FAIL;
387 }
388 return OK;
389}
390# endif
391
392# if defined(FEAT_DIFF) || defined(PROTO)
393 void
394eval_diff(
395 char_u *origfile,
396 char_u *newfile,
397 char_u *outfile)
398{
399 int err = FALSE;
400
401 set_vim_var_string(VV_FNAME_IN, origfile, -1);
402 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
403 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
404 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
405 set_vim_var_string(VV_FNAME_IN, NULL, -1);
406 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
407 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
408}
409
410 void
411eval_patch(
412 char_u *origfile,
413 char_u *difffile,
414 char_u *outfile)
415{
416 int err;
417
418 set_vim_var_string(VV_FNAME_IN, origfile, -1);
419 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
420 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
421 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
422 set_vim_var_string(VV_FNAME_IN, NULL, -1);
423 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
424 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
425}
426# endif
427
428#if defined(FEAT_SPELL) || defined(PROTO)
429/*
430 * Evaluate an expression to a list with suggestions.
431 * For the "expr:" part of 'spellsuggest'.
432 * Returns NULL when there is an error.
433 */
434 list_T *
435eval_spell_expr(char_u *badword, char_u *expr)
436{
437 typval_T save_val;
438 typval_T rettv;
439 list_T *list = NULL;
440 char_u *p = skipwhite(expr);
441
442 // Set "v:val" to the bad word.
443 prepare_vimvar(VV_VAL, &save_val);
444 set_vim_var_string(VV_VAL, badword, -1);
445 if (p_verbose == 0)
446 ++emsg_off;
447
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200448 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200449 {
450 if (rettv.v_type != VAR_LIST)
451 clear_tv(&rettv);
452 else
453 list = rettv.vval.v_list;
454 }
455
456 if (p_verbose == 0)
457 --emsg_off;
458 clear_tv(get_vim_var_tv(VV_VAL));
459 restore_vimvar(VV_VAL, &save_val);
460
461 return list;
462}
463
464/*
465 * "list" is supposed to contain two items: a word and a number. Return the
466 * word in "pp" and the number as the return value.
467 * Return -1 if anything isn't right.
468 * Used to get the good word and score from the eval_spell_expr() result.
469 */
470 int
471get_spellword(list_T *list, char_u **pp)
472{
473 listitem_T *li;
474
475 li = list->lv_first;
476 if (li == NULL)
477 return -1;
478 *pp = tv_get_string(&li->li_tv);
479
480 li = li->li_next;
481 if (li == NULL)
482 return -1;
483 return (int)tv_get_number(&li->li_tv);
484}
485#endif
486
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200487/*
488 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200489 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200490 * When not used yet add the variable to the v: hashtable.
491 */
492 void
493prepare_vimvar(int idx, typval_T *save_tv)
494{
495 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200496 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200497 if (vimvars[idx].vv_type == VAR_UNKNOWN)
498 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
499}
500
501/*
502 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200503 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200504 * When no longer defined, remove the variable from the v: hashtable.
505 */
506 void
507restore_vimvar(int idx, typval_T *save_tv)
508{
509 hashitem_T *hi;
510
511 vimvars[idx].vv_tv = *save_tv;
512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
513 {
514 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
515 if (HASHITEM_EMPTY(hi))
516 internal_error("restore_vimvar()");
517 else
518 hash_remove(&vimvarht, hi);
519 }
520}
521
522/*
523 * List Vim variables.
524 */
525 static void
526list_vim_vars(int *first)
527{
528 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
529}
530
531/*
532 * List script-local variables, if there is a script.
533 */
534 static void
535list_script_vars(int *first)
536{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200537 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200538 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
539 "s:", FALSE, first);
540}
541
542/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200543 * Get a list of lines from a HERE document. The here document is a list of
544 * lines surrounded by a marker.
545 * cmd << {marker}
546 * {line1}
547 * {line2}
548 * ....
549 * {marker}
550 *
551 * The {marker} is a string. If the optional 'trim' word is supplied before the
552 * marker, then the leading indentation before the lines (matching the
553 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200554 *
555 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
556 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
557 * missing, then '.' is accepted as a marker.
558 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200559 * Returns a List with {lines} or NULL.
560 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200562heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200563{
564 char_u *theline;
565 char_u *marker;
566 list_T *l;
567 char_u *p;
568 int marker_indent_len = 0;
569 int text_indent_len = 0;
570 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200571 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200572 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200573
574 if (eap->getline == NULL)
575 {
576 emsg(_("E991: cannot use =<< here"));
577 return NULL;
578 }
579
580 // Check for the optional 'trim' word before the marker
581 cmd = skipwhite(cmd);
582 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
583 {
584 cmd = skipwhite(cmd + 4);
585
586 // Trim the indentation from all the lines in the here document.
587 // The amount of indentation trimmed is the same as the indentation of
588 // the first line after the :let command line. To find the end marker
589 // the indent of the :let command line is trimmed.
590 p = *eap->cmdlinep;
591 while (VIM_ISWHITE(*p))
592 {
593 p++;
594 marker_indent_len++;
595 }
596 text_indent_len = -1;
597 }
598
599 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200600 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200601 {
602 marker = skipwhite(cmd);
603 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200604 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200605 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200606 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 return NULL;
608 }
609 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200610 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200611 {
612 emsg(_("E221: Marker cannot start with lower case letter"));
613 return NULL;
614 }
615 }
616 else
617 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200618 // When getting lines for an embedded script, if the marker is missing,
619 // accept '.' as the marker.
620 if (script_get)
621 marker = dot;
622 else
623 {
624 emsg(_("E172: Missing marker"));
625 return NULL;
626 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200627 }
628
629 l = list_alloc();
630 if (l == NULL)
631 return NULL;
632
633 for (;;)
634 {
635 int mi = 0;
636 int ti = 0;
637
638 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
639 if (theline == NULL)
640 {
641 semsg(_("E990: Missing end marker '%s'"), marker);
642 break;
643 }
644
645 // with "trim": skip the indent matching the :let line to find the
646 // marker
647 if (marker_indent_len > 0
648 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
649 mi = marker_indent_len;
650 if (STRCMP(marker, theline + mi) == 0)
651 {
652 vim_free(theline);
653 break;
654 }
655
656 if (text_indent_len == -1 && *theline != NUL)
657 {
658 // set the text indent from the first line.
659 p = theline;
660 text_indent_len = 0;
661 while (VIM_ISWHITE(*p))
662 {
663 p++;
664 text_indent_len++;
665 }
666 text_indent = vim_strnsave(theline, text_indent_len);
667 }
668 // with "trim": skip the indent matching the first line
669 if (text_indent != NULL)
670 for (ti = 0; ti < text_indent_len; ++ti)
671 if (theline[ti] != text_indent[ti])
672 break;
673
674 if (list_append_string(l, theline + ti, -1) == FAIL)
675 break;
676 vim_free(theline);
677 }
678 vim_free(text_indent);
679
680 return l;
681}
682
683/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200684 * Vim9 variable declaration:
685 * ":var name"
686 * ":var name: type"
687 * ":var name = expr"
688 * ":var name: type = expr"
689 * etc.
690 */
691 void
692ex_var(exarg_T *eap)
693{
694 if (!in_vim9script())
695 {
696 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
697 return;
698 }
699 ex_let(eap);
700}
701
702/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200703 * ":let" list all variable values
704 * ":let var1 var2" list variable values
705 * ":let var = expr" assignment command.
706 * ":let var += expr" assignment command.
707 * ":let var -= expr" assignment command.
708 * ":let var *= expr" assignment command.
709 * ":let var /= expr" assignment command.
710 * ":let var %= expr" assignment command.
711 * ":let var .= expr" assignment command.
712 * ":let var ..= expr" assignment command.
713 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100715 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200716 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200717 * ":final var = expr" assignment command.
718 * ":final [var1, var2] = expr" unpack list.
719 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200720 * ":const" list all variable values
721 * ":const var1 var2" list variable values
722 * ":const var = expr" assignment command.
723 * ":const [var1, var2] = expr" unpack list.
724 */
725 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200726ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200727{
728 char_u *arg = eap->arg;
729 char_u *expr = NULL;
730 typval_T rettv;
731 int i;
732 int var_count = 0;
733 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200734 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200735 char_u *argend;
736 int first = TRUE;
737 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200738 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200739 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200740 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200741
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200742 if (eap->cmdidx == CMD_final && !vim9script)
743 {
744 // In legacy Vim script ":final" is short for ":finally".
745 ex_finally(eap);
746 return;
747 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200748 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200749 {
750 emsg(_(e_cannot_use_let_in_vim9_script));
751 return;
752 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200753 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
754 // In legacy Vim script ":const" works like ":final".
755 eap->cmdidx = CMD_final;
756
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 // detect Vim9 assignment without ":let" or ":const"
758 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200759 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200761 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 if (argend == NULL)
763 return;
764 if (argend > arg && argend[-1] == '.') // for var.='str'
765 --argend;
766 expr = skipwhite(argend);
767 concat = expr[0] == '.'
768 && ((expr[1] == '=' && current_sctx.sc_version < 2)
769 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200770 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
771 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200772 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200773 {
774 // ":let" without "=": list variables
775 if (*arg == '[')
776 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200777 else if (expr[0] == '.' && expr[1] == '=')
778 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200779 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200780 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200782 {
783 // Vim9 declaration ":let var: type"
784 arg = vim9_declare_scriptvar(eap, arg);
785 }
786 else
787 {
788 // ":let var1 var2" - list values
789 arg = list_arg_vars(eap, arg, &first);
790 }
791 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200792 else if (!eap->skip)
793 {
794 // ":let"
795 list_glob_vars(&first);
796 list_buf_vars(&first);
797 list_win_vars(&first);
798 list_tab_vars(&first);
799 list_script_vars(&first);
800 list_func_vars(&first);
801 list_vim_vars(&first);
802 }
803 eap->nextcmd = check_nextcmd(arg);
804 }
805 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
806 {
807 list_T *l;
808
809 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200810 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200811 if (l != NULL)
812 {
813 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200814 if (!eap->skip)
815 {
816 op[0] = '=';
817 op[1] = NUL;
818 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200820 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 clear_tv(&rettv);
822 }
823 }
824 else
825 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200826 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200827 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200828
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200829 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200830 i = FAIL;
831 if (has_assign || concat)
832 {
833 op[0] = '=';
834 op[1] = NUL;
835 if (*expr != '=')
836 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200837 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200838 {
839 // +=, /=, etc. require an existing variable
840 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
841 i = FAIL;
842 }
843 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200844 {
845 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200846 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200847 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200848 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200849 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++len;
851 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200852 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200853 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 }
855 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200856 ++expr;
857
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200858 if (vim9script && (!VIM_ISWHITE(*argend)
859 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 {
861 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200862 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200863 i = FAIL;
864 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200865
866 if (eap->skip)
867 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200868 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200869 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200870 if (getline_equal(eap->getline, eap->cookie, getsourceline))
871 {
872 evalarg.eval_getline = eap->getline;
873 evalarg.eval_cookie = eap->cookie;
874 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200875 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200876 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200877 if (eap->skip)
878 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200879 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200880 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200881 if (eap->skip)
882 {
883 if (i != FAIL)
884 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200885 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200886 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200887 {
888 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200889 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200890 clear_tv(&rettv);
891 }
892 }
893}
894
895/*
896 * Assign the typevalue "tv" to the variable or variables at "arg_start".
897 * Handles both "var" with any type and "[var, var; var]" with a list type.
898 * When "op" is not NULL it points to a string with characters that
899 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
900 * or concatenate.
901 * Returns OK or FAIL;
902 */
903 int
904ex_let_vars(
905 char_u *arg_start,
906 typval_T *tv,
907 int copy, // copy values from "tv", don't move
908 int semicolon, // from skip_var_list()
909 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200910 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200911 char_u *op)
912{
913 char_u *arg = arg_start;
914 list_T *l;
915 int i;
916 listitem_T *item;
917 typval_T ltv;
918
919 if (*arg != '[')
920 {
921 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200923 return FAIL;
924 return OK;
925 }
926
927 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
928 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
929 {
930 emsg(_(e_listreq));
931 return FAIL;
932 }
933
934 i = list_len(l);
935 if (semicolon == 0 && var_count < i)
936 {
937 emsg(_("E687: Less targets than List items"));
938 return FAIL;
939 }
940 if (var_count - semicolon > i)
941 {
942 emsg(_("E688: More targets than List items"));
943 return FAIL;
944 }
945
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200946 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200947 item = l->lv_first;
948 while (*arg != ']')
949 {
950 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952 item = item->li_next;
953 if (arg == NULL)
954 return FAIL;
955
956 arg = skipwhite(arg);
957 if (*arg == ';')
958 {
959 // Put the rest of the list (may be empty) in the var after ';'.
960 // Create a new list for this.
961 l = list_alloc();
962 if (l == NULL)
963 return FAIL;
964 while (item != NULL)
965 {
966 list_append_tv(l, &item->li_tv);
967 item = item->li_next;
968 }
969
970 ltv.v_type = VAR_LIST;
971 ltv.v_lock = 0;
972 ltv.vval.v_list = l;
973 l->lv_refcount = 1;
974
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200976 (char_u *)"]", op);
977 clear_tv(&ltv);
978 if (arg == NULL)
979 return FAIL;
980 break;
981 }
982 else if (*arg != ',' && *arg != ']')
983 {
984 internal_error("ex_let_vars()");
985 return FAIL;
986 }
987 }
988
989 return OK;
990}
991
992/*
993 * Skip over assignable variable "var" or list of variables "[var, var]".
994 * Used for ":let varvar = expr" and ":for varvar in expr".
995 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200996 * for "[var, var; var]" set "semicolon" to 1.
997 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200998 * Return NULL for an error.
999 */
1000 char_u *
1001skip_var_list(
1002 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001004 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001005 int *semicolon,
1006 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007{
1008 char_u *p, *s;
1009
1010 if (*arg == '[')
1011 {
1012 // "[var, var]": find the matching ']'.
1013 p = arg;
1014 for (;;)
1015 {
1016 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001017 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001018 if (s == p)
1019 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001020 if (!silent)
1021 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 return NULL;
1023 }
1024 ++*var_count;
1025
1026 p = skipwhite(s);
1027 if (*p == ']')
1028 break;
1029 else if (*p == ';')
1030 {
1031 if (*semicolon == 1)
1032 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001033 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001034 return NULL;
1035 }
1036 *semicolon = 1;
1037 }
1038 else if (*p != ',')
1039 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001040 if (!silent)
1041 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 return NULL;
1043 }
1044 }
1045 return p + 1;
1046 }
1047 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001048 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049}
1050
1051/*
1052 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1053 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001056 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 char_u *end;
1060
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001061 if (*arg == '@' && arg[1] != NUL)
1062 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001064 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001065 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001067 // "a: type" is declaring variable "a" with a type, not "a:".
1068 if (end == arg + 2 && end[-1] == ':')
1069 --end;
1070 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001071 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 }
1073 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001074}
1075
1076/*
1077 * List variables for hashtab "ht" with prefix "prefix".
1078 * If "empty" is TRUE also list NULL strings as empty strings.
1079 */
1080 void
1081list_hashtable_vars(
1082 hashtab_T *ht,
1083 char *prefix,
1084 int empty,
1085 int *first)
1086{
1087 hashitem_T *hi;
1088 dictitem_T *di;
1089 int todo;
1090 char_u buf[IOSIZE];
1091
1092 todo = (int)ht->ht_used;
1093 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1094 {
1095 if (!HASHITEM_EMPTY(hi))
1096 {
1097 --todo;
1098 di = HI2DI(hi);
1099
1100 // apply :filter /pat/ to variable name
1101 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1102 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1103 if (message_filtered(buf))
1104 continue;
1105
1106 if (empty || di->di_tv.v_type != VAR_STRING
1107 || di->di_tv.vval.v_string != NULL)
1108 list_one_var(di, prefix, first);
1109 }
1110 }
1111}
1112
1113/*
1114 * List global variables.
1115 */
1116 static void
1117list_glob_vars(int *first)
1118{
1119 list_hashtable_vars(&globvarht, "", TRUE, first);
1120}
1121
1122/*
1123 * List buffer variables.
1124 */
1125 static void
1126list_buf_vars(int *first)
1127{
1128 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1129}
1130
1131/*
1132 * List window variables.
1133 */
1134 static void
1135list_win_vars(int *first)
1136{
1137 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1138}
1139
1140/*
1141 * List tab page variables.
1142 */
1143 static void
1144list_tab_vars(int *first)
1145{
1146 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1147}
1148
1149/*
1150 * List variables in "arg".
1151 */
1152 static char_u *
1153list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1154{
1155 int error = FALSE;
1156 int len;
1157 char_u *name;
1158 char_u *name_start;
1159 char_u *arg_subsc;
1160 char_u *tofree;
1161 typval_T tv;
1162
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001163 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001164 {
1165 if (error || eap->skip)
1166 {
1167 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1168 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1169 {
1170 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001171 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001172 break;
1173 }
1174 }
1175 else
1176 {
1177 // get_name_len() takes care of expanding curly braces
1178 name_start = name = arg;
1179 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1180 if (len <= 0)
1181 {
1182 // This is mainly to keep test 49 working: when expanding
1183 // curly braces fails overrule the exception error message.
1184 if (len < 0 && !aborting())
1185 {
1186 emsg_severe = TRUE;
1187 semsg(_(e_invarg2), arg);
1188 break;
1189 }
1190 error = TRUE;
1191 }
1192 else
1193 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001194 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001195 if (tofree != NULL)
1196 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001197 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001198 error = TRUE;
1199 else
1200 {
1201 // handle d.key, l[idx], f(expr)
1202 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001203 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001204 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001205 error = TRUE;
1206 else
1207 {
1208 if (arg == arg_subsc && len == 2 && name[1] == ':')
1209 {
1210 switch (*name)
1211 {
1212 case 'g': list_glob_vars(first); break;
1213 case 'b': list_buf_vars(first); break;
1214 case 'w': list_win_vars(first); break;
1215 case 't': list_tab_vars(first); break;
1216 case 'v': list_vim_vars(first); break;
1217 case 's': list_script_vars(first); break;
1218 case 'l': list_func_vars(first); break;
1219 default:
1220 semsg(_("E738: Can't list variables for %s"), name);
1221 }
1222 }
1223 else
1224 {
1225 char_u numbuf[NUMBUFLEN];
1226 char_u *tf;
1227 int c;
1228 char_u *s;
1229
1230 s = echo_string(&tv, &tf, numbuf, 0);
1231 c = *arg;
1232 *arg = NUL;
1233 list_one_var_a("",
1234 arg == arg_subsc ? name : name_start,
1235 tv.v_type,
1236 s == NULL ? (char_u *)"" : s,
1237 first);
1238 *arg = c;
1239 vim_free(tf);
1240 }
1241 clear_tv(&tv);
1242 }
1243 }
1244 }
1245
1246 vim_free(tofree);
1247 }
1248
1249 arg = skipwhite(arg);
1250 }
1251
1252 return arg;
1253}
1254
1255/*
1256 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1257 * Returns a pointer to the char just after the var name.
1258 * Returns NULL if there is an error.
1259 */
1260 static char_u *
1261ex_let_one(
1262 char_u *arg, // points to variable name
1263 typval_T *tv, // value to assign to variable
1264 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001265 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266 char_u *endchars, // valid chars after variable name or NULL
1267 char_u *op) // "+", "-", "." or NULL
1268{
1269 int c1;
1270 char_u *name;
1271 char_u *p;
1272 char_u *arg_end = NULL;
1273 int len;
1274 int opt_flags;
1275 char_u *tofree = NULL;
1276
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001277 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001278 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1279 {
1280 vim9_declare_error(arg);
1281 return NULL;
1282 }
1283
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001284 // ":let $VAR = expr": Set environment variable.
1285 if (*arg == '$')
1286 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001287 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001288 {
1289 emsg(_("E996: Cannot lock an environment variable"));
1290 return NULL;
1291 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001292
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 // Find the end of the name.
1294 ++arg;
1295 name = arg;
1296 len = get_env_len(&arg);
1297 if (len == 0)
1298 semsg(_(e_invarg2), name - 1);
1299 else
1300 {
1301 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1302 semsg(_(e_letwrong), op);
1303 else if (endchars != NULL
1304 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1305 emsg(_(e_letunexp));
1306 else if (!check_secure())
1307 {
1308 c1 = name[len];
1309 name[len] = NUL;
1310 p = tv_get_string_chk(tv);
1311 if (p != NULL && op != NULL && *op == '.')
1312 {
1313 int mustfree = FALSE;
1314 char_u *s = vim_getenv(name, &mustfree);
1315
1316 if (s != NULL)
1317 {
1318 p = tofree = concat_str(s, p);
1319 if (mustfree)
1320 vim_free(s);
1321 }
1322 }
1323 if (p != NULL)
1324 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001325 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 arg_end = arg;
1327 }
1328 name[len] = c1;
1329 vim_free(tofree);
1330 }
1331 }
1332 }
1333
1334 // ":let &option = expr": Set option value.
1335 // ":let &l:option = expr": Set local option value.
1336 // ":let &g:option = expr": Set global option value.
1337 else if (*arg == '&')
1338 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001339 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 return NULL;
1343 }
1344 // Find the end of the name.
1345 p = find_option_end(&arg, &opt_flags);
1346 if (p == NULL || (endchars != NULL
1347 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1348 emsg(_(e_letunexp));
1349 else
1350 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001351 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001352 int opt_type;
1353 long numval;
1354 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001355 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001356 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357
1358 c1 = *p;
1359 *p = NUL;
1360
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001361 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1362 if ((opt_type == 1 || opt_type == -1)
1363 && (tv->v_type != VAR_STRING || !in_vim9script()))
1364 // number, possibly hidden
1365 n = (long)tv_get_number(tv);
1366
1367 // Avoid setting a string option to the text "v:false" or similar.
1368 // In Vim9 script also don't convert a number to string.
1369 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1370 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1371 s = tv_get_string_chk(tv);
1372
1373 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001374 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375 if ((opt_type == 1 && *op == '.')
1376 || (opt_type == 0 && *op != '.'))
1377 {
1378 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001379 failed = TRUE; // don't set the value
1380
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381 }
1382 else
1383 {
1384 if (opt_type == 1) // number
1385 {
1386 switch (*op)
1387 {
1388 case '+': n = numval + n; break;
1389 case '-': n = numval - n; break;
1390 case '*': n = numval * n; break;
1391 case '/': n = (long)num_divide(numval, n); break;
1392 case '%': n = (long)num_modulus(numval, n); break;
1393 }
1394 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001395 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001396 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001397 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001398 s = concat_str(stringval, s);
1399 vim_free(stringval);
1400 stringval = s;
1401 }
1402 }
1403 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001404
1405 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001406 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001407 if (opt_type != 0 || s != NULL)
1408 {
1409 set_option_value(arg, n, s, opt_flags);
1410 arg_end = p;
1411 }
1412 else
1413 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001414 }
1415 *p = c1;
1416 vim_free(stringval);
1417 }
1418 }
1419
1420 // ":let @r = expr": Set register contents.
1421 else if (*arg == '@')
1422 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001423 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001424 {
1425 emsg(_("E996: Cannot lock a register"));
1426 return NULL;
1427 }
1428 ++arg;
1429 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1430 semsg(_(e_letwrong), op);
1431 else if (endchars != NULL
1432 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1433 emsg(_(e_letunexp));
1434 else
1435 {
1436 char_u *ptofree = NULL;
1437 char_u *s;
1438
1439 p = tv_get_string_chk(tv);
1440 if (p != NULL && op != NULL && *op == '.')
1441 {
1442 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1443 if (s != NULL)
1444 {
1445 p = ptofree = concat_str(s, p);
1446 vim_free(s);
1447 }
1448 }
1449 if (p != NULL)
1450 {
1451 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1452 arg_end = arg + 1;
1453 }
1454 vim_free(ptofree);
1455 }
1456 }
1457
1458 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001460 // ":let {expr} = expr": Idem, name made with curly braces
1461 else if (eval_isnamec1(*arg) || *arg == '{')
1462 {
1463 lval_T lv;
1464
1465 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001466 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 if (endchars != NULL && vim_strchr(endchars,
1469 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470 emsg(_(e_letunexp));
1471 else
1472 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001474 arg_end = p;
1475 }
1476 }
1477 clear_lval(&lv);
1478 }
1479
1480 else
1481 semsg(_(e_invarg2), arg);
1482
1483 return arg_end;
1484}
1485
1486/*
1487 * ":unlet[!] var1 ... " command.
1488 */
1489 void
1490ex_unlet(exarg_T *eap)
1491{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001492 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001493}
1494
1495/*
1496 * ":lockvar" and ":unlockvar" commands
1497 */
1498 void
1499ex_lockvar(exarg_T *eap)
1500{
1501 char_u *arg = eap->arg;
1502 int deep = 2;
1503
1504 if (eap->forceit)
1505 deep = -1;
1506 else if (vim_isdigit(*arg))
1507 {
1508 deep = getdigits(&arg);
1509 arg = skipwhite(arg);
1510 }
1511
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001512 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001513}
1514
1515/*
1516 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001517 * Also used for Vim9 script. "callback" is invoked as:
1518 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001519 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001520 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521ex_unletlock(
1522 exarg_T *eap,
1523 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001524 int deep,
1525 int glv_flags,
1526 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1527 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001528{
1529 char_u *arg = argstart;
1530 char_u *name_end;
1531 int error = FALSE;
1532 lval_T lv;
1533
1534 do
1535 {
1536 if (*arg == '$')
1537 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001538 lv.ll_name = arg;
1539 lv.ll_tv = NULL;
1540 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001541 if (get_env_len(&arg) == 0)
1542 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001543 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544 return;
1545 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001546 if (!error && !eap->skip
1547 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1548 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001549 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001550 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001551 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001553 // Parse the name and find the end.
1554 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1555 glv_flags, FNE_CHECK_START);
1556 if (lv.ll_name == NULL)
1557 error = TRUE; // error but continue parsing
1558 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1559 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001560 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001561 if (name_end != NULL)
1562 {
1563 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001564 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001565 }
1566 if (!(eap->skip || error))
1567 clear_lval(&lv);
1568 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001569 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001570
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001571 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001572 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001573 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001574
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001575 if (!eap->skip)
1576 clear_lval(&lv);
1577 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001578
1579 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001580 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581
1582 eap->nextcmd = check_nextcmd(arg);
1583}
1584
1585 static int
1586do_unlet_var(
1587 lval_T *lp,
1588 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001589 exarg_T *eap,
1590 int deep UNUSED,
1591 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001592{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001593 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 int ret = OK;
1595 int cc;
1596
1597 if (lp->ll_tv == NULL)
1598 {
1599 cc = *name_end;
1600 *name_end = NUL;
1601
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001602 // Environment variable, normal name or expanded name.
1603 if (*lp->ll_name == '$')
1604 vim_unsetenv(lp->ll_name + 1);
1605 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001606 ret = FAIL;
1607 *name_end = cc;
1608 }
1609 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001610 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001611 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001612 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 return FAIL;
1614 else if (lp->ll_range)
1615 {
1616 listitem_T *li;
1617 listitem_T *ll_li = lp->ll_li;
1618 int ll_n1 = lp->ll_n1;
1619
1620 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1621 {
1622 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001623 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001624 return FAIL;
1625 ll_li = li;
1626 ++ll_n1;
1627 }
1628
1629 // Delete a range of List items.
1630 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1631 {
1632 li = lp->ll_li->li_next;
1633 listitem_remove(lp->ll_list, lp->ll_li);
1634 lp->ll_li = li;
1635 ++lp->ll_n1;
1636 }
1637 }
1638 else
1639 {
1640 if (lp->ll_list != NULL)
1641 // unlet a List item.
1642 listitem_remove(lp->ll_list, lp->ll_li);
1643 else
1644 // unlet a Dictionary item.
1645 dictitem_remove(lp->ll_dict, lp->ll_di);
1646 }
1647
1648 return ret;
1649}
1650
1651/*
1652 * "unlet" a variable. Return OK if it existed, FAIL if not.
1653 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1654 */
1655 int
1656do_unlet(char_u *name, int forceit)
1657{
1658 hashtab_T *ht;
1659 hashitem_T *hi;
1660 char_u *varname;
1661 dict_T *d;
1662 dictitem_T *di;
1663
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001664 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001665 return FAIL;
1666
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001667 ht = find_var_ht(name, &varname);
1668 if (ht != NULL && *varname != NUL)
1669 {
1670 d = get_current_funccal_dict(ht);
1671 if (d == NULL)
1672 {
1673 if (ht == &globvarht)
1674 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001675 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001676 d = &vimvardict;
1677 else
1678 {
1679 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1680 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1681 }
1682 if (d == NULL)
1683 {
1684 internal_error("do_unlet()");
1685 return FAIL;
1686 }
1687 }
1688 hi = hash_find(ht, varname);
1689 if (HASHITEM_EMPTY(hi))
1690 hi = find_hi_in_scoped_ht(name, &ht);
1691 if (hi != NULL && !HASHITEM_EMPTY(hi))
1692 {
1693 di = HI2DI(hi);
1694 if (var_check_fixed(di->di_flags, name, FALSE)
1695 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001696 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001697 return FAIL;
1698
1699 delete_var(ht, hi);
1700 return OK;
1701 }
1702 }
1703 if (forceit)
1704 return OK;
1705 semsg(_("E108: No such variable: \"%s\""), name);
1706 return FAIL;
1707}
1708
1709/*
1710 * Lock or unlock variable indicated by "lp".
1711 * "deep" is the levels to go (-1 for unlimited);
1712 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1713 */
1714 static int
1715do_lock_var(
1716 lval_T *lp,
1717 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001718 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001720 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001722 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001723 int ret = OK;
1724 int cc;
1725 dictitem_T *di;
1726
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001727 if (lp->ll_tv == NULL)
1728 {
1729 cc = *name_end;
1730 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001731 if (*lp->ll_name == '$')
1732 {
1733 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001735 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 else
1737 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001738 // Normal name or expanded name.
1739 di = find_var(lp->ll_name, NULL, TRUE);
1740 if (di == NULL)
1741 ret = FAIL;
1742 else if ((di->di_flags & DI_FLAGS_FIX)
1743 && di->di_tv.v_type != VAR_DICT
1744 && di->di_tv.v_type != VAR_LIST)
1745 {
1746 // For historic reasons this error is not given for a list or
1747 // dict. E.g., the b: dict could be locked/unlocked.
1748 semsg(_(e_lock_unlock), lp->ll_name);
1749 ret = FAIL;
1750 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001751 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001752 {
1753 if (lock)
1754 di->di_flags |= DI_FLAGS_LOCK;
1755 else
1756 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001757 if (deep != 0)
1758 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001759 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760 }
1761 *name_end = cc;
1762 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001763 else if (deep == 0)
1764 {
1765 // nothing to do
1766 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001767 else if (lp->ll_range)
1768 {
1769 listitem_T *li = lp->ll_li;
1770
1771 // (un)lock a range of List items.
1772 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1773 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001774 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001775 li = li->li_next;
1776 ++lp->ll_n1;
1777 }
1778 }
1779 else if (lp->ll_list != NULL)
1780 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001781 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001782 else
1783 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001784 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001785
1786 return ret;
1787}
1788
1789/*
1790 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001791 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1792 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001793 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001794 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001795item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001796{
1797 static int recurse = 0;
1798 list_T *l;
1799 listitem_T *li;
1800 dict_T *d;
1801 blob_T *b;
1802 hashitem_T *hi;
1803 int todo;
1804
1805 if (recurse >= DICT_MAXNEST)
1806 {
1807 emsg(_("E743: variable nested too deep for (un)lock"));
1808 return;
1809 }
1810 if (deep == 0)
1811 return;
1812 ++recurse;
1813
1814 // lock/unlock the item itself
1815 if (lock)
1816 tv->v_lock |= VAR_LOCKED;
1817 else
1818 tv->v_lock &= ~VAR_LOCKED;
1819
1820 switch (tv->v_type)
1821 {
1822 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001823 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001825 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 case VAR_STRING:
1828 case VAR_FUNC:
1829 case VAR_PARTIAL:
1830 case VAR_FLOAT:
1831 case VAR_SPECIAL:
1832 case VAR_JOB:
1833 case VAR_CHANNEL:
1834 break;
1835
1836 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001837 if ((b = tv->vval.v_blob) != NULL
1838 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001839 {
1840 if (lock)
1841 b->bv_lock |= VAR_LOCKED;
1842 else
1843 b->bv_lock &= ~VAR_LOCKED;
1844 }
1845 break;
1846 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001847 if ((l = tv->vval.v_list) != NULL
1848 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001849 {
1850 if (lock)
1851 l->lv_lock |= VAR_LOCKED;
1852 else
1853 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001854 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001856 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001857 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001858 }
1859 break;
1860 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001861 if ((d = tv->vval.v_dict) != NULL
1862 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 {
1864 if (lock)
1865 d->dv_lock |= VAR_LOCKED;
1866 else
1867 d->dv_lock &= ~VAR_LOCKED;
1868 if (deep < 0 || deep > 1)
1869 {
1870 // recursive: lock/unlock the items the List contains
1871 todo = (int)d->dv_hashtab.ht_used;
1872 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1873 {
1874 if (!HASHITEM_EMPTY(hi))
1875 {
1876 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001877 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1878 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 }
1880 }
1881 }
1882 }
1883 }
1884 --recurse;
1885}
1886
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001887#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1888/*
1889 * Delete all "menutrans_" variables.
1890 */
1891 void
1892del_menutrans_vars(void)
1893{
1894 hashitem_T *hi;
1895 int todo;
1896
1897 hash_lock(&globvarht);
1898 todo = (int)globvarht.ht_used;
1899 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1900 {
1901 if (!HASHITEM_EMPTY(hi))
1902 {
1903 --todo;
1904 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1905 delete_var(&globvarht, hi);
1906 }
1907 }
1908 hash_unlock(&globvarht);
1909}
1910#endif
1911
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001912/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001913 * Local string buffer for the next two functions to store a variable name
1914 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1915 * get_user_var_name().
1916 */
1917
1918static char_u *varnamebuf = NULL;
1919static int varnamebuflen = 0;
1920
1921/*
1922 * Function to concatenate a prefix and a variable name.
1923 */
1924 static char_u *
1925cat_prefix_varname(int prefix, char_u *name)
1926{
1927 int len;
1928
1929 len = (int)STRLEN(name) + 3;
1930 if (len > varnamebuflen)
1931 {
1932 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001933 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001934 varnamebuf = alloc(len);
1935 if (varnamebuf == NULL)
1936 {
1937 varnamebuflen = 0;
1938 return NULL;
1939 }
1940 varnamebuflen = len;
1941 }
1942 *varnamebuf = prefix;
1943 varnamebuf[1] = ':';
1944 STRCPY(varnamebuf + 2, name);
1945 return varnamebuf;
1946}
1947
1948/*
1949 * Function given to ExpandGeneric() to obtain the list of user defined
1950 * (global/buffer/window/built-in) variable names.
1951 */
1952 char_u *
1953get_user_var_name(expand_T *xp, int idx)
1954{
1955 static long_u gdone;
1956 static long_u bdone;
1957 static long_u wdone;
1958 static long_u tdone;
1959 static int vidx;
1960 static hashitem_T *hi;
1961 hashtab_T *ht;
1962
1963 if (idx == 0)
1964 {
1965 gdone = bdone = wdone = vidx = 0;
1966 tdone = 0;
1967 }
1968
1969 // Global variables
1970 if (gdone < globvarht.ht_used)
1971 {
1972 if (gdone++ == 0)
1973 hi = globvarht.ht_array;
1974 else
1975 ++hi;
1976 while (HASHITEM_EMPTY(hi))
1977 ++hi;
1978 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1979 return cat_prefix_varname('g', hi->hi_key);
1980 return hi->hi_key;
1981 }
1982
1983 // b: variables
1984 ht = &curbuf->b_vars->dv_hashtab;
1985 if (bdone < ht->ht_used)
1986 {
1987 if (bdone++ == 0)
1988 hi = ht->ht_array;
1989 else
1990 ++hi;
1991 while (HASHITEM_EMPTY(hi))
1992 ++hi;
1993 return cat_prefix_varname('b', hi->hi_key);
1994 }
1995
1996 // w: variables
1997 ht = &curwin->w_vars->dv_hashtab;
1998 if (wdone < ht->ht_used)
1999 {
2000 if (wdone++ == 0)
2001 hi = ht->ht_array;
2002 else
2003 ++hi;
2004 while (HASHITEM_EMPTY(hi))
2005 ++hi;
2006 return cat_prefix_varname('w', hi->hi_key);
2007 }
2008
2009 // t: variables
2010 ht = &curtab->tp_vars->dv_hashtab;
2011 if (tdone < ht->ht_used)
2012 {
2013 if (tdone++ == 0)
2014 hi = ht->ht_array;
2015 else
2016 ++hi;
2017 while (HASHITEM_EMPTY(hi))
2018 ++hi;
2019 return cat_prefix_varname('t', hi->hi_key);
2020 }
2021
2022 // v: variables
2023 if (vidx < VV_LEN)
2024 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2025
2026 VIM_CLEAR(varnamebuf);
2027 varnamebuflen = 0;
2028 return NULL;
2029}
2030
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002031 char *
2032get_var_special_name(int nr)
2033{
2034 switch (nr)
2035 {
2036 case VVAL_FALSE: return "v:false";
2037 case VVAL_TRUE: return "v:true";
2038 case VVAL_NONE: return "v:none";
2039 case VVAL_NULL: return "v:null";
2040 }
2041 internal_error("get_var_special_name()");
2042 return "42";
2043}
2044
2045/*
2046 * Returns the global variable dictionary
2047 */
2048 dict_T *
2049get_globvar_dict(void)
2050{
2051 return &globvardict;
2052}
2053
2054/*
2055 * Returns the global variable hash table
2056 */
2057 hashtab_T *
2058get_globvar_ht(void)
2059{
2060 return &globvarht;
2061}
2062
2063/*
2064 * Returns the v: variable dictionary
2065 */
2066 dict_T *
2067get_vimvar_dict(void)
2068{
2069 return &vimvardict;
2070}
2071
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002072/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002074 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 */
2076 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002077find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002079 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2080 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081
2082 if (di == NULL)
2083 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002084 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2086 return (int)(vv - vimvars);
2087}
2088
2089
2090/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002091 * Set type of v: variable to "type".
2092 */
2093 void
2094set_vim_var_type(int idx, vartype_T type)
2095{
2096 vimvars[idx].vv_type = type;
2097}
2098
2099/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002100 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002101 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002102 */
2103 void
2104set_vim_var_nr(int idx, varnumber_T val)
2105{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002106 vimvars[idx].vv_nr = val;
2107}
2108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 char *
2110get_vim_var_name(int idx)
2111{
2112 return vimvars[idx].vv_name;
2113}
2114
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002115/*
2116 * Get typval_T v: variable value.
2117 */
2118 typval_T *
2119get_vim_var_tv(int idx)
2120{
2121 return &vimvars[idx].vv_tv;
2122}
2123
2124/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002125 * Set v: variable to "tv". Only accepts the same type.
2126 * Takes over the value of "tv".
2127 */
2128 int
2129set_vim_var_tv(int idx, typval_T *tv)
2130{
2131 if (vimvars[idx].vv_type != tv->v_type)
2132 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002133 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002134 clear_tv(tv);
2135 return FAIL;
2136 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002137 // VV_RO is also checked when compiling, but let's check here as well.
2138 if (vimvars[idx].vv_flags & VV_RO)
2139 {
2140 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2141 return FAIL;
2142 }
2143 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2144 {
2145 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2146 return FAIL;
2147 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002148 clear_tv(&vimvars[idx].vv_di.di_tv);
2149 vimvars[idx].vv_di.di_tv = *tv;
2150 return OK;
2151}
2152
2153/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002154 * Get number v: variable value.
2155 */
2156 varnumber_T
2157get_vim_var_nr(int idx)
2158{
2159 return vimvars[idx].vv_nr;
2160}
2161
2162/*
2163 * Get string v: variable value. Uses a static buffer, can only be used once.
2164 * If the String variable has never been set, return an empty string.
2165 * Never returns NULL;
2166 */
2167 char_u *
2168get_vim_var_str(int idx)
2169{
2170 return tv_get_string(&vimvars[idx].vv_tv);
2171}
2172
2173/*
2174 * Get List v: variable value. Caller must take care of reference count when
2175 * needed.
2176 */
2177 list_T *
2178get_vim_var_list(int idx)
2179{
2180 return vimvars[idx].vv_list;
2181}
2182
2183/*
2184 * Get Dict v: variable value. Caller must take care of reference count when
2185 * needed.
2186 */
2187 dict_T *
2188get_vim_var_dict(int idx)
2189{
2190 return vimvars[idx].vv_dict;
2191}
2192
2193/*
2194 * Set v:char to character "c".
2195 */
2196 void
2197set_vim_var_char(int c)
2198{
2199 char_u buf[MB_MAXBYTES + 1];
2200
2201 if (has_mbyte)
2202 buf[(*mb_char2bytes)(c, buf)] = NUL;
2203 else
2204 {
2205 buf[0] = c;
2206 buf[1] = NUL;
2207 }
2208 set_vim_var_string(VV_CHAR, buf, -1);
2209}
2210
2211/*
2212 * Set v:count to "count" and v:count1 to "count1".
2213 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2214 */
2215 void
2216set_vcount(
2217 long count,
2218 long count1,
2219 int set_prevcount)
2220{
2221 if (set_prevcount)
2222 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2223 vimvars[VV_COUNT].vv_nr = count;
2224 vimvars[VV_COUNT1].vv_nr = count1;
2225}
2226
2227/*
2228 * Save variables that might be changed as a side effect. Used when executing
2229 * a timer callback.
2230 */
2231 void
2232save_vimvars(vimvars_save_T *vvsave)
2233{
2234 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2235 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2236 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2237}
2238
2239/*
2240 * Restore variables saved by save_vimvars().
2241 */
2242 void
2243restore_vimvars(vimvars_save_T *vvsave)
2244{
2245 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2246 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2247 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2248}
2249
2250/*
2251 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2252 * value.
2253 */
2254 void
2255set_vim_var_string(
2256 int idx,
2257 char_u *val,
2258 int len) // length of "val" to use or -1 (whole string)
2259{
2260 clear_tv(&vimvars[idx].vv_di.di_tv);
2261 vimvars[idx].vv_type = VAR_STRING;
2262 if (val == NULL)
2263 vimvars[idx].vv_str = NULL;
2264 else if (len == -1)
2265 vimvars[idx].vv_str = vim_strsave(val);
2266 else
2267 vimvars[idx].vv_str = vim_strnsave(val, len);
2268}
2269
2270/*
2271 * Set List v: variable to "val".
2272 */
2273 void
2274set_vim_var_list(int idx, list_T *val)
2275{
2276 clear_tv(&vimvars[idx].vv_di.di_tv);
2277 vimvars[idx].vv_type = VAR_LIST;
2278 vimvars[idx].vv_list = val;
2279 if (val != NULL)
2280 ++val->lv_refcount;
2281}
2282
2283/*
2284 * Set Dictionary v: variable to "val".
2285 */
2286 void
2287set_vim_var_dict(int idx, dict_T *val)
2288{
2289 clear_tv(&vimvars[idx].vv_di.di_tv);
2290 vimvars[idx].vv_type = VAR_DICT;
2291 vimvars[idx].vv_dict = val;
2292 if (val != NULL)
2293 {
2294 ++val->dv_refcount;
2295 dict_set_items_ro(val);
2296 }
2297}
2298
2299/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002300 * Set the v:argv list.
2301 */
2302 void
2303set_argv_var(char **argv, int argc)
2304{
2305 list_T *l = list_alloc();
2306 int i;
2307
2308 if (l == NULL)
2309 getout(1);
2310 l->lv_lock = VAR_FIXED;
2311 for (i = 0; i < argc; ++i)
2312 {
2313 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2314 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002315 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002316 }
2317 set_vim_var_list(VV_ARGV, l);
2318}
2319
2320/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002321 * Reset v:register, taking the 'clipboard' setting into account.
2322 */
2323 void
2324reset_reg_var(void)
2325{
2326 int regname = 0;
2327
2328 // Adjust the register according to 'clipboard', so that when
2329 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2330#ifdef FEAT_CLIPBOARD
2331 adjust_clip_reg(&regname);
2332#endif
2333 set_reg_var(regname);
2334}
2335
2336/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002337 * Set v:register if needed.
2338 */
2339 void
2340set_reg_var(int c)
2341{
2342 char_u regname;
2343
2344 if (c == 0 || c == ' ')
2345 regname = '"';
2346 else
2347 regname = c;
2348 // Avoid free/alloc when the value is already right.
2349 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2350 set_vim_var_string(VV_REG, &regname, 1);
2351}
2352
2353/*
2354 * Get or set v:exception. If "oldval" == NULL, return the current value.
2355 * Otherwise, restore the value to "oldval" and return NULL.
2356 * Must always be called in pairs to save and restore v:exception! Does not
2357 * take care of memory allocations.
2358 */
2359 char_u *
2360v_exception(char_u *oldval)
2361{
2362 if (oldval == NULL)
2363 return vimvars[VV_EXCEPTION].vv_str;
2364
2365 vimvars[VV_EXCEPTION].vv_str = oldval;
2366 return NULL;
2367}
2368
2369/*
2370 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2371 * Otherwise, restore the value to "oldval" and return NULL.
2372 * Must always be called in pairs to save and restore v:throwpoint! Does not
2373 * take care of memory allocations.
2374 */
2375 char_u *
2376v_throwpoint(char_u *oldval)
2377{
2378 if (oldval == NULL)
2379 return vimvars[VV_THROWPOINT].vv_str;
2380
2381 vimvars[VV_THROWPOINT].vv_str = oldval;
2382 return NULL;
2383}
2384
2385/*
2386 * Set v:cmdarg.
2387 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2388 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2389 * Must always be called in pairs!
2390 */
2391 char_u *
2392set_cmdarg(exarg_T *eap, char_u *oldarg)
2393{
2394 char_u *oldval;
2395 char_u *newval;
2396 unsigned len;
2397
2398 oldval = vimvars[VV_CMDARG].vv_str;
2399 if (eap == NULL)
2400 {
2401 vim_free(oldval);
2402 vimvars[VV_CMDARG].vv_str = oldarg;
2403 return NULL;
2404 }
2405
2406 if (eap->force_bin == FORCE_BIN)
2407 len = 6;
2408 else if (eap->force_bin == FORCE_NOBIN)
2409 len = 8;
2410 else
2411 len = 0;
2412
2413 if (eap->read_edit)
2414 len += 7;
2415
2416 if (eap->force_ff != 0)
2417 len += 10; // " ++ff=unix"
2418 if (eap->force_enc != 0)
2419 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2420 if (eap->bad_char != 0)
2421 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2422
2423 newval = alloc(len + 1);
2424 if (newval == NULL)
2425 return NULL;
2426
2427 if (eap->force_bin == FORCE_BIN)
2428 sprintf((char *)newval, " ++bin");
2429 else if (eap->force_bin == FORCE_NOBIN)
2430 sprintf((char *)newval, " ++nobin");
2431 else
2432 *newval = NUL;
2433
2434 if (eap->read_edit)
2435 STRCAT(newval, " ++edit");
2436
2437 if (eap->force_ff != 0)
2438 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2439 eap->force_ff == 'u' ? "unix"
2440 : eap->force_ff == 'd' ? "dos"
2441 : "mac");
2442 if (eap->force_enc != 0)
2443 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2444 eap->cmd + eap->force_enc);
2445 if (eap->bad_char == BAD_KEEP)
2446 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2447 else if (eap->bad_char == BAD_DROP)
2448 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2449 else if (eap->bad_char != 0)
2450 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2451 vimvars[VV_CMDARG].vv_str = newval;
2452 return oldval;
2453}
2454
2455/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002456 * Get the value of internal variable "name".
2457 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2458 */
2459 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002460eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002461 char_u *name,
2462 int len, // length of "name"
2463 typval_T *rettv, // NULL when only checking existence
2464 dictitem_T **dip, // non-NULL when typval's dict item is needed
2465 int verbose, // may give error message
2466 int no_autoload) // do not use script autoloading
2467{
2468 int ret = OK;
2469 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002470 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002471 dictitem_T *v;
2472 int cc;
2473
2474 // truncate the name, so that we can use strcmp()
2475 cc = name[len];
2476 name[len] = NUL;
2477
2478 // Check for user-defined variables.
2479 v = find_var(name, NULL, no_autoload);
2480 if (v != NULL)
2481 {
2482 tv = &v->di_tv;
2483 if (dip != NULL)
2484 *dip = v;
2485 }
2486
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002487 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002489 imported_T *import;
2490 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2491
2492 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493
2494 // imported variable from another script
2495 if (import != NULL)
2496 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002497 if (import->imp_funcname != NULL)
2498 {
2499 foundFunc = TRUE;
2500 if (rettv != NULL)
2501 {
2502 rettv->v_type = VAR_FUNC;
2503 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2504 }
2505 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002506 else if (import->imp_all)
2507 {
2508 emsg("Sorry, 'import * as X' not implemented yet");
2509 ret = FAIL;
2510 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002511 else
2512 {
2513 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002514 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002516 tv = sv->sv_tv;
2517 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002519 else if (in_vim9script())
2520 {
2521 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2522
2523 if (ufunc != NULL)
2524 {
2525 foundFunc = TRUE;
2526 if (rettv != NULL)
2527 {
2528 rettv->v_type = VAR_FUNC;
2529 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2530 }
2531 }
2532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
2534
Bram Moolenaarc620c052020-07-08 15:16:19 +02002535 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002536 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002537 if (tv == NULL)
2538 {
2539 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002540 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002541 ret = FAIL;
2542 }
2543 else if (rettv != NULL)
2544 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002545 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002546
2547 name[len] = cc;
2548
2549 return ret;
2550}
2551
2552/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002553 * Check if variable "name[len]" is a local variable or an argument.
2554 * If so, "*eval_lavars_used" is set to TRUE.
2555 */
2556 void
2557check_vars(char_u *name, int len)
2558{
2559 int cc;
2560 char_u *varname;
2561 hashtab_T *ht;
2562
2563 if (eval_lavars_used == NULL)
2564 return;
2565
2566 // truncate the name, so that we can use strcmp()
2567 cc = name[len];
2568 name[len] = NUL;
2569
2570 ht = find_var_ht(name, &varname);
2571 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2572 {
2573 if (find_var(name, NULL, TRUE) != NULL)
2574 *eval_lavars_used = TRUE;
2575 }
2576
2577 name[len] = cc;
2578}
2579
2580/*
2581 * Find variable "name" in the list of variables.
2582 * Return a pointer to it if found, NULL if not found.
2583 * Careful: "a:0" variables don't have a name.
2584 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2585 * hashtab_T used.
2586 */
2587 dictitem_T *
2588find_var(char_u *name, hashtab_T **htp, int no_autoload)
2589{
2590 char_u *varname;
2591 hashtab_T *ht;
2592 dictitem_T *ret = NULL;
2593
2594 ht = find_var_ht(name, &varname);
2595 if (htp != NULL)
2596 *htp = ht;
2597 if (ht == NULL)
2598 return NULL;
2599 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2600 if (ret != NULL)
2601 return ret;
2602
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002603 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002604 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2605}
2606
2607/*
2608 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002609 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002610 * Returns NULL if not found.
2611 */
2612 dictitem_T *
2613find_var_in_ht(
2614 hashtab_T *ht,
2615 int htname,
2616 char_u *varname,
2617 int no_autoload)
2618{
2619 hashitem_T *hi;
2620
2621 if (*varname == NUL)
2622 {
2623 // Must be something like "s:", otherwise "ht" would be NULL.
2624 switch (htname)
2625 {
2626 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2627 case 'g': return &globvars_var;
2628 case 'v': return &vimvars_var;
2629 case 'b': return &curbuf->b_bufvar;
2630 case 'w': return &curwin->w_winvar;
2631 case 't': return &curtab->tp_winvar;
2632 case 'l': return get_funccal_local_var();
2633 case 'a': return get_funccal_args_var();
2634 }
2635 return NULL;
2636 }
2637
2638 hi = hash_find(ht, varname);
2639 if (HASHITEM_EMPTY(hi))
2640 {
2641 // For global variables we may try auto-loading the script. If it
2642 // worked find the variable again. Don't auto-load a script if it was
2643 // loaded already, otherwise it would be loaded every time when
2644 // checking if a function name is a Funcref variable.
2645 if (ht == &globvarht && !no_autoload)
2646 {
2647 // Note: script_autoload() may make "hi" invalid. It must either
2648 // be obtained again or not used.
2649 if (!script_autoload(varname, FALSE) || aborting())
2650 return NULL;
2651 hi = hash_find(ht, varname);
2652 }
2653 if (HASHITEM_EMPTY(hi))
2654 return NULL;
2655 }
2656 return HI2DI(hi);
2657}
2658
2659/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 * Get the script-local hashtab. NULL if not in a script context.
2661 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002662 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663get_script_local_ht(void)
2664{
2665 scid_T sid = current_sctx.sc_sid;
2666
Bram Moolenaare3d46852020-08-29 13:39:17 +02002667 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 return &SCRIPT_VARS(sid);
2669 return NULL;
2670}
2671
2672/*
2673 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002674 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002676 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002677lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2678{
2679 hashtab_T *ht = get_script_local_ht();
2680 char_u buffer[30];
2681 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002682 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 hashitem_T *hi;
2684
2685 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002686 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 if (len < sizeof(buffer) - 1)
2688 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002689 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 vim_strncpy(buffer, name, len);
2691 p = buffer;
2692 }
2693 else
2694 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002695 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002697 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 }
2699
2700 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002701 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702
2703 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002704 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2705 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706
2707 if (p != buffer)
2708 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002709 // Don't return "buffer", gcc complains.
2710 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711}
2712
2713/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002714 * Find the hashtab used for a variable name.
2715 * Return NULL if the name is not valid.
2716 * Set "varname" to the start of name without ':'.
2717 */
2718 hashtab_T *
2719find_var_ht(char_u *name, char_u **varname)
2720{
2721 hashitem_T *hi;
2722 hashtab_T *ht;
2723
2724 if (name[0] == NUL)
2725 return NULL;
2726 if (name[1] != ':')
2727 {
2728 // The name must not start with a colon or #.
2729 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2730 return NULL;
2731 *varname = name;
2732
2733 // "version" is "v:version" in all scopes if scriptversion < 3.
2734 // Same for a few other variables marked with VV_COMPAT.
2735 if (current_sctx.sc_version < 3)
2736 {
2737 hi = hash_find(&compat_hashtab, name);
2738 if (!HASHITEM_EMPTY(hi))
2739 return &compat_hashtab;
2740 }
2741
2742 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 if (ht != NULL)
2744 return ht; // local variable
2745
2746 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002747 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 {
2749 ht = get_script_local_ht();
2750 if (ht != NULL)
2751 return ht;
2752 }
2753
2754 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002755 }
2756 *varname = name + 2;
2757 if (*name == 'g') // global variable
2758 return &globvarht;
2759 // There must be no ':' or '#' in the rest of the name, unless g: is used
2760 if (vim_strchr(name + 2, ':') != NULL
2761 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2762 return NULL;
2763 if (*name == 'b') // buffer variable
2764 return &curbuf->b_vars->dv_hashtab;
2765 if (*name == 'w') // window variable
2766 return &curwin->w_vars->dv_hashtab;
2767 if (*name == 't') // tab page variable
2768 return &curtab->tp_vars->dv_hashtab;
2769 if (*name == 'v') // v: variable
2770 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002771 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002772 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002774 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 if (*name == 'a') // a: function argument
2776 return get_funccal_args_ht();
2777 if (*name == 'l') // l: local function variable
2778 return get_funccal_local_ht();
2779 }
2780 if (*name == 's') // script variable
2781 {
2782 ht = get_script_local_ht();
2783 if (ht != NULL)
2784 return ht;
2785 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002786 return NULL;
2787}
2788
2789/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002790 * Get the string value of a (global/local) variable.
2791 * Note: see tv_get_string() for how long the pointer remains valid.
2792 * Returns NULL when it doesn't exist.
2793 */
2794 char_u *
2795get_var_value(char_u *name)
2796{
2797 dictitem_T *v;
2798
2799 v = find_var(name, NULL, FALSE);
2800 if (v == NULL)
2801 return NULL;
2802 return tv_get_string(&v->di_tv);
2803}
2804
2805/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002806 * Allocate a new hashtab for a sourced script. It will be used while
2807 * sourcing this script and when executing functions defined in the script.
2808 */
2809 void
2810new_script_vars(scid_T id)
2811{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002812 scriptvar_T *sv;
2813
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002814 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2815 if (sv == NULL)
2816 return;
2817 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002818 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002819}
2820
2821/*
2822 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2823 * point to it.
2824 */
2825 void
2826init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2827{
2828 hash_init(&dict->dv_hashtab);
2829 dict->dv_lock = 0;
2830 dict->dv_scope = scope;
2831 dict->dv_refcount = DO_NOT_FREE_CNT;
2832 dict->dv_copyID = 0;
2833 dict_var->di_tv.vval.v_dict = dict;
2834 dict_var->di_tv.v_type = VAR_DICT;
2835 dict_var->di_tv.v_lock = VAR_FIXED;
2836 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2837 dict_var->di_key[0] = NUL;
2838}
2839
2840/*
2841 * Unreference a dictionary initialized by init_var_dict().
2842 */
2843 void
2844unref_var_dict(dict_T *dict)
2845{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002846 // Now the dict needs to be freed if no one else is using it, go back to
2847 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002848 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2849 dict_unref(dict);
2850}
2851
2852/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002853 * Clean up a list of internal variables.
2854 * Frees all allocated variables and the value they contain.
2855 * Clears hashtab "ht", does not free it.
2856 */
2857 void
2858vars_clear(hashtab_T *ht)
2859{
2860 vars_clear_ext(ht, TRUE);
2861}
2862
2863/*
2864 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2865 */
2866 void
2867vars_clear_ext(hashtab_T *ht, int free_val)
2868{
2869 int todo;
2870 hashitem_T *hi;
2871 dictitem_T *v;
2872
2873 hash_lock(ht);
2874 todo = (int)ht->ht_used;
2875 for (hi = ht->ht_array; todo > 0; ++hi)
2876 {
2877 if (!HASHITEM_EMPTY(hi))
2878 {
2879 --todo;
2880
2881 // Free the variable. Don't remove it from the hashtab,
2882 // ht_array might change then. hash_clear() takes care of it
2883 // later.
2884 v = HI2DI(hi);
2885 if (free_val)
2886 clear_tv(&v->di_tv);
2887 if (v->di_flags & DI_FLAGS_ALLOC)
2888 vim_free(v);
2889 }
2890 }
2891 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002892 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002893}
2894
2895/*
2896 * Delete a variable from hashtab "ht" at item "hi".
2897 * Clear the variable value and free the dictitem.
2898 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002899 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002900delete_var(hashtab_T *ht, hashitem_T *hi)
2901{
2902 dictitem_T *di = HI2DI(hi);
2903
2904 hash_remove(ht, hi);
2905 clear_tv(&di->di_tv);
2906 vim_free(di);
2907}
2908
2909/*
2910 * List the value of one internal variable.
2911 */
2912 static void
2913list_one_var(dictitem_T *v, char *prefix, int *first)
2914{
2915 char_u *tofree;
2916 char_u *s;
2917 char_u numbuf[NUMBUFLEN];
2918
2919 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2920 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2921 s == NULL ? (char_u *)"" : s, first);
2922 vim_free(tofree);
2923}
2924
2925 static void
2926list_one_var_a(
2927 char *prefix,
2928 char_u *name,
2929 int type,
2930 char_u *string,
2931 int *first) // when TRUE clear rest of screen and set to FALSE
2932{
2933 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2934 msg_start();
2935 msg_puts(prefix);
2936 if (name != NULL) // "a:" vars don't have a name stored
2937 msg_puts((char *)name);
2938 msg_putchar(' ');
2939 msg_advance(22);
2940 if (type == VAR_NUMBER)
2941 msg_putchar('#');
2942 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2943 msg_putchar('*');
2944 else if (type == VAR_LIST)
2945 {
2946 msg_putchar('[');
2947 if (*string == '[')
2948 ++string;
2949 }
2950 else if (type == VAR_DICT)
2951 {
2952 msg_putchar('{');
2953 if (*string == '{')
2954 ++string;
2955 }
2956 else
2957 msg_putchar(' ');
2958
2959 msg_outtrans(string);
2960
2961 if (type == VAR_FUNC || type == VAR_PARTIAL)
2962 msg_puts("()");
2963 if (*first)
2964 {
2965 msg_clr_eos();
2966 *first = FALSE;
2967 }
2968}
2969
2970/*
2971 * Set variable "name" to value in "tv".
2972 * If the variable already exists, the value is updated.
2973 * Otherwise the variable is created.
2974 */
2975 void
2976set_var(
2977 char_u *name,
2978 typval_T *tv,
2979 int copy) // make copy of value in "tv"
2980{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002981 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002982}
2983
2984/*
2985 * Set variable "name" to value in "tv".
2986 * If the variable already exists and "is_const" is FALSE the value is updated.
2987 * Otherwise the variable is created.
2988 */
2989 void
2990set_var_const(
2991 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002993 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002994 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002995 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002996{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002997 typval_T *tv = tv_arg;
2998 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003000 char_u *varname;
3001 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003002 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003003 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003004
3005 ht = find_var_ht(name, &varname);
3006 if (ht == NULL || *varname == NUL)
3007 {
3008 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003009 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003010 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003011 is_script_local = ht == get_script_local_ht();
3012
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003013 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003014 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003015 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003016 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003017 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003018 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003019 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003020 }
3021
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003023
3024 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 if (di == NULL)
3026 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003027
3028 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003029 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003030 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003031
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003032 if (need_convert_to_bool(type, tv))
3033 {
3034 // Destination is a bool and the value is not, but it can be converted.
3035 CLEAR_FIELD(bool_tv);
3036 bool_tv.v_type = VAR_BOOL;
3037 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3038 tv = &bool_tv;
3039 }
3040
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003042 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003044 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003045 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 {
3047 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003048 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 }
3050
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003051 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003053 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003054 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003055 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003056 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003057 }
3058
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003059 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003060 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003061 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003063
Bram Moolenaara187c432020-09-16 21:08:28 +02003064 // Check in this order for backwards compatibility:
3065 // - Whether the variable is read-only
3066 // - Whether the variable value is locked
3067 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003068 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003069 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3070 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003071 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003072 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 else
3074 // can only redefine once
3075 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003076
3077 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003078
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003080 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003081 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003082 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003084 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086 if (copy || tv->v_type != VAR_STRING)
3087 {
3088 char_u *val = tv_get_string(tv);
3089
3090 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003091 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 if (di->di_tv.vval.v_string == NULL)
3093 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003094 }
3095 else
3096 {
3097 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 tv->vval.v_string = NULL;
3100 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003101 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003102 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003104 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003106 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003108#ifdef FEAT_SEARCH_EXTRA
3109 else if (STRCMP(varname, "hlsearch") == 0)
3110 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003112 redraw_all_later(SOME_VALID);
3113 }
3114#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003115 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003118 {
3119 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003120 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003121 }
3122 }
3123
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003124 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003125 }
3126 else // add a new variable
3127 {
3128 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003129 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003130 {
3131 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003132 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003133 }
3134
3135 // Make sure the variable name is valid.
3136 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003137 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3140 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003141 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 STRCPY(di->di_key, varname);
3143 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003144 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003146 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003149 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 di->di_flags |= DI_FLAGS_LOCK;
3151
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003152 // A Vim9 script-local variable is also added to sn_all_vars and
3153 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003154 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003155 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003156 }
3157
3158 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003160 else
3161 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 di->di_tv = *tv;
3163 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003164 init_tv(tv);
3165 }
3166
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003167 // ":const var = val" locks the value
3168 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003169 // Like :lockvar! name: lock the value and what it contains, but only
3170 // if the reference count is up to one. That locks only literal
3171 // values.
3172 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003173 return;
3174failed:
3175 if (!copy)
3176 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003177}
3178
3179/*
3180 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3181 * Also give an error message.
3182 */
3183 int
3184var_check_ro(int flags, char_u *name, int use_gettext)
3185{
3186 if (flags & DI_FLAGS_RO)
3187 {
3188 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3189 return TRUE;
3190 }
3191 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3192 {
3193 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3194 return TRUE;
3195 }
3196 return FALSE;
3197}
3198
3199/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003200 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3201 * Also give an error message.
3202 */
3203 int
3204var_check_lock(int flags, char_u *name, int use_gettext)
3205{
3206 if (flags & DI_FLAGS_LOCK)
3207 {
3208 semsg(_(e_variable_is_locked_str),
3209 use_gettext ? (char_u *)_(name) : name);
3210 return TRUE;
3211 }
3212 return FALSE;
3213}
3214
3215/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003216 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3217 * Also give an error message.
3218 */
3219 int
3220var_check_fixed(int flags, char_u *name, int use_gettext)
3221{
3222 if (flags & DI_FLAGS_FIX)
3223 {
3224 semsg(_("E795: Cannot delete variable %s"),
3225 use_gettext ? (char_u *)_(name) : name);
3226 return TRUE;
3227 }
3228 return FALSE;
3229}
3230
3231/*
3232 * Check if a funcref is assigned to a valid variable name.
3233 * Return TRUE and give an error if not.
3234 */
3235 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003236var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003237 char_u *name, // points to start of variable name
3238 int new_var) // TRUE when creating the variable
3239{
3240 // Allow for w: b: s: and t:.
3241 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3242 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3243 ? name[2] : name[0]))
3244 {
3245 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3246 name);
3247 return TRUE;
3248 }
3249 // Don't allow hiding a function. When "v" is not NULL we might be
3250 // assigning another function to the same var, the type is checked
3251 // below.
3252 if (new_var && function_exists(name, FALSE))
3253 {
3254 semsg(_("E705: Variable name conflicts with existing function: %s"),
3255 name);
3256 return TRUE;
3257 }
3258 return FALSE;
3259}
3260
3261/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003262 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3263 * value. Also give an error message, using "name" or _("name") when
3264 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003265 */
3266 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003267value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003268{
3269 if (lock & VAR_LOCKED)
3270 {
3271 semsg(_("E741: Value is locked: %s"),
3272 name == NULL ? (char_u *)_("Unknown")
3273 : use_gettext ? (char_u *)_(name)
3274 : name);
3275 return TRUE;
3276 }
3277 if (lock & VAR_FIXED)
3278 {
3279 semsg(_("E742: Cannot change value of %s"),
3280 name == NULL ? (char_u *)_("Unknown")
3281 : use_gettext ? (char_u *)_(name)
3282 : name);
3283 return TRUE;
3284 }
3285 return FALSE;
3286}
3287
3288/*
3289 * Check if a variable name is valid.
3290 * Return FALSE and give an error if not.
3291 */
3292 int
3293valid_varname(char_u *varname)
3294{
3295 char_u *p;
3296
3297 for (p = varname; *p != NUL; ++p)
3298 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3299 && *p != AUTOLOAD_CHAR)
3300 {
3301 semsg(_(e_illvar), varname);
3302 return FALSE;
3303 }
3304 return TRUE;
3305}
3306
3307/*
3308 * getwinvar() and gettabwinvar()
3309 */
3310 static void
3311getwinvar(
3312 typval_T *argvars,
3313 typval_T *rettv,
3314 int off) // 1 for gettabwinvar()
3315{
3316 win_T *win;
3317 char_u *varname;
3318 dictitem_T *v;
3319 tabpage_T *tp = NULL;
3320 int done = FALSE;
3321 win_T *oldcurwin;
3322 tabpage_T *oldtabpage;
3323 int need_switch_win;
3324
3325 if (off == 1)
3326 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3327 else
3328 tp = curtab;
3329 win = find_win_by_nr(&argvars[off], tp);
3330 varname = tv_get_string_chk(&argvars[off + 1]);
3331 ++emsg_off;
3332
3333 rettv->v_type = VAR_STRING;
3334 rettv->vval.v_string = NULL;
3335
3336 if (win != NULL && varname != NULL)
3337 {
3338 // Set curwin to be our win, temporarily. Also set the tabpage,
3339 // otherwise the window is not valid. Only do this when needed,
3340 // autocommands get blocked.
3341 need_switch_win = !(tp == curtab && win == curwin);
3342 if (!need_switch_win
3343 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3344 {
3345 if (*varname == '&')
3346 {
3347 if (varname[1] == NUL)
3348 {
3349 // get all window-local options in a dict
3350 dict_T *opts = get_winbuf_options(FALSE);
3351
3352 if (opts != NULL)
3353 {
3354 rettv_dict_set(rettv, opts);
3355 done = TRUE;
3356 }
3357 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003358 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003359 // window-local-option
3360 done = TRUE;
3361 }
3362 else
3363 {
3364 // Look up the variable.
3365 // Let getwinvar({nr}, "") return the "w:" dictionary.
3366 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3367 varname, FALSE);
3368 if (v != NULL)
3369 {
3370 copy_tv(&v->di_tv, rettv);
3371 done = TRUE;
3372 }
3373 }
3374 }
3375
3376 if (need_switch_win)
3377 // restore previous notion of curwin
3378 restore_win(oldcurwin, oldtabpage, TRUE);
3379 }
3380
3381 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3382 // use the default return value
3383 copy_tv(&argvars[off + 2], rettv);
3384
3385 --emsg_off;
3386}
3387
3388/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003389 * Set option "varname" to the value of "varp" for the current buffer/window.
3390 */
3391 static void
3392set_option_from_tv(char_u *varname, typval_T *varp)
3393{
3394 long numval = 0;
3395 char_u *strval;
3396 char_u nbuf[NUMBUFLEN];
3397 int error = FALSE;
3398
3399 if (!in_vim9script() || varp->v_type != VAR_STRING)
3400 numval = (long)tv_get_number_chk(varp, &error);
3401 strval = tv_get_string_buf_chk(varp, nbuf);
3402 if (!error && strval != NULL)
3403 set_option_value(varname, numval, strval, OPT_LOCAL);
3404}
3405
3406/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003407 * "setwinvar()" and "settabwinvar()" functions
3408 */
3409 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003410setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003411{
3412 win_T *win;
3413 win_T *save_curwin;
3414 tabpage_T *save_curtab;
3415 int need_switch_win;
3416 char_u *varname, *winvarname;
3417 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003418 tabpage_T *tp = NULL;
3419
3420 if (check_secure())
3421 return;
3422
3423 if (off == 1)
3424 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3425 else
3426 tp = curtab;
3427 win = find_win_by_nr(&argvars[off], tp);
3428 varname = tv_get_string_chk(&argvars[off + 1]);
3429 varp = &argvars[off + 2];
3430
3431 if (win != NULL && varname != NULL && varp != NULL)
3432 {
3433 need_switch_win = !(tp == curtab && win == curwin);
3434 if (!need_switch_win
3435 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3436 {
3437 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003438 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003439 else
3440 {
3441 winvarname = alloc(STRLEN(varname) + 3);
3442 if (winvarname != NULL)
3443 {
3444 STRCPY(winvarname, "w:");
3445 STRCPY(winvarname + 2, varname);
3446 set_var(winvarname, varp, TRUE);
3447 vim_free(winvarname);
3448 }
3449 }
3450 }
3451 if (need_switch_win)
3452 restore_win(save_curwin, save_curtab, TRUE);
3453 }
3454}
3455
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003456/*
3457 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3458 * v:option_type, and v:option_command.
3459 */
3460 void
3461reset_v_option_vars(void)
3462{
3463 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3464 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3465 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3466 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3467 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3468 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3469}
3470
3471/*
3472 * Add an assert error to v:errors.
3473 */
3474 void
3475assert_error(garray_T *gap)
3476{
3477 struct vimvar *vp = &vimvars[VV_ERRORS];
3478
3479 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003480 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003481 set_vim_var_list(VV_ERRORS, list_alloc());
3482 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3483}
3484
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003485 int
3486var_exists(char_u *var)
3487{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003488 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003489 char_u *name;
3490 char_u *tofree;
3491 typval_T tv;
3492 int len = 0;
3493 int n = FALSE;
3494
3495 // get_name_len() takes care of expanding curly braces
3496 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003497 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003498 if (len > 0)
3499 {
3500 if (tofree != NULL)
3501 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003502 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003503 if (n)
3504 {
3505 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003506 arg = skipwhite(arg);
3507 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003508 if (n)
3509 clear_tv(&tv);
3510 }
3511 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003512 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003513 n = FALSE;
3514
3515 vim_free(tofree);
3516 return n;
3517}
3518
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003519static lval_T *redir_lval = NULL;
3520#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3521static garray_T redir_ga; // only valid when redir_lval is not NULL
3522static char_u *redir_endp = NULL;
3523static char_u *redir_varname = NULL;
3524
3525/*
3526 * Start recording command output to a variable
3527 * When "append" is TRUE append to an existing variable.
3528 * Returns OK if successfully completed the setup. FAIL otherwise.
3529 */
3530 int
3531var_redir_start(char_u *name, int append)
3532{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003533 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003534 typval_T tv;
3535
3536 // Catch a bad name early.
3537 if (!eval_isnamec1(*name))
3538 {
3539 emsg(_(e_invarg));
3540 return FAIL;
3541 }
3542
3543 // Make a copy of the name, it is used in redir_lval until redir ends.
3544 redir_varname = vim_strsave(name);
3545 if (redir_varname == NULL)
3546 return FAIL;
3547
3548 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3549 if (redir_lval == NULL)
3550 {
3551 var_redir_stop();
3552 return FAIL;
3553 }
3554
3555 // The output is stored in growarray "redir_ga" until redirection ends.
3556 ga_init2(&redir_ga, (int)sizeof(char), 500);
3557
3558 // Parse the variable name (can be a dict or list entry).
3559 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3560 FNE_CHECK_START);
3561 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3562 {
3563 clear_lval(redir_lval);
3564 if (redir_endp != NULL && *redir_endp != NUL)
3565 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003566 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003567 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003568 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003569 redir_endp = NULL; // don't store a value, only cleanup
3570 var_redir_stop();
3571 return FAIL;
3572 }
3573
3574 // check if we can write to the variable: set it to or append an empty
3575 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003576 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003577 tv.v_type = VAR_STRING;
3578 tv.vval.v_string = (char_u *)"";
3579 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003580 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3581 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003582 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003583 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3584 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003585 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003586 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003587 {
3588 redir_endp = NULL; // don't store a value, only cleanup
3589 var_redir_stop();
3590 return FAIL;
3591 }
3592
3593 return OK;
3594}
3595
3596/*
3597 * Append "value[value_len]" to the variable set by var_redir_start().
3598 * The actual appending is postponed until redirection ends, because the value
3599 * appended may in fact be the string we write to, changing it may cause freed
3600 * memory to be used:
3601 * :redir => foo
3602 * :let foo
3603 * :redir END
3604 */
3605 void
3606var_redir_str(char_u *value, int value_len)
3607{
3608 int len;
3609
3610 if (redir_lval == NULL)
3611 return;
3612
3613 if (value_len == -1)
3614 len = (int)STRLEN(value); // Append the entire string
3615 else
3616 len = value_len; // Append only "value_len" characters
3617
3618 if (ga_grow(&redir_ga, len) == OK)
3619 {
3620 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3621 redir_ga.ga_len += len;
3622 }
3623 else
3624 var_redir_stop();
3625}
3626
3627/*
3628 * Stop redirecting command output to a variable.
3629 * Frees the allocated memory.
3630 */
3631 void
3632var_redir_stop(void)
3633{
3634 typval_T tv;
3635
3636 if (EVALCMD_BUSY)
3637 {
3638 redir_lval = NULL;
3639 return;
3640 }
3641
3642 if (redir_lval != NULL)
3643 {
3644 // If there was no error: assign the text to the variable.
3645 if (redir_endp != NULL)
3646 {
3647 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3648 tv.v_type = VAR_STRING;
3649 tv.vval.v_string = redir_ga.ga_data;
3650 // Call get_lval() again, if it's inside a Dict or List it may
3651 // have changed.
3652 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3653 FALSE, FALSE, 0, FNE_CHECK_START);
3654 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003656 (char_u *)".");
3657 clear_lval(redir_lval);
3658 }
3659
3660 // free the collected output
3661 VIM_CLEAR(redir_ga.ga_data);
3662
3663 VIM_CLEAR(redir_lval);
3664 }
3665 VIM_CLEAR(redir_varname);
3666}
3667
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003668/*
3669 * "gettabvar()" function
3670 */
3671 void
3672f_gettabvar(typval_T *argvars, typval_T *rettv)
3673{
3674 win_T *oldcurwin;
3675 tabpage_T *tp, *oldtabpage;
3676 dictitem_T *v;
3677 char_u *varname;
3678 int done = FALSE;
3679
3680 rettv->v_type = VAR_STRING;
3681 rettv->vval.v_string = NULL;
3682
3683 varname = tv_get_string_chk(&argvars[1]);
3684 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3685 if (tp != NULL && varname != NULL)
3686 {
3687 // Set tp to be our tabpage, temporarily. Also set the window to the
3688 // first window in the tabpage, otherwise the window is not valid.
3689 if (switch_win(&oldcurwin, &oldtabpage,
3690 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3691 : tp->tp_firstwin, tp, TRUE) == OK)
3692 {
3693 // look up the variable
3694 // Let gettabvar({nr}, "") return the "t:" dictionary.
3695 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3696 if (v != NULL)
3697 {
3698 copy_tv(&v->di_tv, rettv);
3699 done = TRUE;
3700 }
3701 }
3702
3703 // restore previous notion of curwin
3704 restore_win(oldcurwin, oldtabpage, TRUE);
3705 }
3706
3707 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3708 copy_tv(&argvars[2], rettv);
3709}
3710
3711/*
3712 * "gettabwinvar()" function
3713 */
3714 void
3715f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3716{
3717 getwinvar(argvars, rettv, 1);
3718}
3719
3720/*
3721 * "getwinvar()" function
3722 */
3723 void
3724f_getwinvar(typval_T *argvars, typval_T *rettv)
3725{
3726 getwinvar(argvars, rettv, 0);
3727}
3728
3729/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003730 * "getbufvar()" function
3731 */
3732 void
3733f_getbufvar(typval_T *argvars, typval_T *rettv)
3734{
3735 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003736 char_u *varname;
3737 dictitem_T *v;
3738 int done = FALSE;
3739
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003740 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003741 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003742
3743 rettv->v_type = VAR_STRING;
3744 rettv->vval.v_string = NULL;
3745
3746 if (buf != NULL && varname != NULL)
3747 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003748 if (*varname == '&')
3749 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003750 buf_T *save_curbuf = curbuf;
3751
3752 // set curbuf to be our buf, temporarily
3753 curbuf = buf;
3754
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003755 if (varname[1] == NUL)
3756 {
3757 // get all buffer-local options in a dict
3758 dict_T *opts = get_winbuf_options(TRUE);
3759
3760 if (opts != NULL)
3761 {
3762 rettv_dict_set(rettv, opts);
3763 done = TRUE;
3764 }
3765 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003766 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003767 // buffer-local-option
3768 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003769
3770 // restore previous notion of curbuf
3771 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003772 }
3773 else
3774 {
3775 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003776 if (*varname == NUL)
3777 // Let getbufvar({nr}, "") return the "b:" dictionary.
3778 v = &buf->b_bufvar;
3779 else
3780 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3781 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003782 if (v != NULL)
3783 {
3784 copy_tv(&v->di_tv, rettv);
3785 done = TRUE;
3786 }
3787 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003788 }
3789
3790 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3791 // use the default value
3792 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003793}
3794
3795/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003796 * "settabvar()" function
3797 */
3798 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003799f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003800{
3801 tabpage_T *save_curtab;
3802 tabpage_T *tp;
3803 char_u *varname, *tabvarname;
3804 typval_T *varp;
3805
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003806 if (check_secure())
3807 return;
3808
3809 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3810 varname = tv_get_string_chk(&argvars[1]);
3811 varp = &argvars[2];
3812
3813 if (varname != NULL && varp != NULL && tp != NULL)
3814 {
3815 save_curtab = curtab;
3816 goto_tabpage_tp(tp, FALSE, FALSE);
3817
3818 tabvarname = alloc(STRLEN(varname) + 3);
3819 if (tabvarname != NULL)
3820 {
3821 STRCPY(tabvarname, "t:");
3822 STRCPY(tabvarname + 2, varname);
3823 set_var(tabvarname, varp, TRUE);
3824 vim_free(tabvarname);
3825 }
3826
3827 // Restore current tabpage
3828 if (valid_tabpage(save_curtab))
3829 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3830 }
3831}
3832
3833/*
3834 * "settabwinvar()" function
3835 */
3836 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003837f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003838{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003839 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003840}
3841
3842/*
3843 * "setwinvar()" function
3844 */
3845 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003846f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003847{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003848 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003849}
3850
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003851/*
3852 * "setbufvar()" function
3853 */
3854 void
3855f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3856{
3857 buf_T *buf;
3858 char_u *varname, *bufvarname;
3859 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003860
3861 if (check_secure())
3862 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003863 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003864 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003865 varp = &argvars[2];
3866
3867 if (buf != NULL && varname != NULL && varp != NULL)
3868 {
3869 if (*varname == '&')
3870 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003871 aco_save_T aco;
3872
3873 // set curbuf to be our buf, temporarily
3874 aucmd_prepbuf(&aco, buf);
3875
Bram Moolenaar191929b2020-08-19 21:20:49 +02003876 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003877
3878 // reset notion of buffer
3879 aucmd_restbuf(&aco);
3880 }
3881 else
3882 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003883 bufvarname = alloc(STRLEN(varname) + 3);
3884 if (bufvarname != NULL)
3885 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003886 buf_T *save_curbuf = curbuf;
3887
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003888 curbuf = buf;
3889 STRCPY(bufvarname, "b:");
3890 STRCPY(bufvarname + 2, varname);
3891 set_var(bufvarname, varp, TRUE);
3892 vim_free(bufvarname);
3893 curbuf = save_curbuf;
3894 }
3895 }
3896 }
3897}
3898
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003899/*
3900 * Get a callback from "arg". It can be a Funcref or a function name.
3901 * When "arg" is zero return an empty string.
3902 * "cb_name" is not allocated.
3903 * "cb_name" is set to NULL for an invalid argument.
3904 */
3905 callback_T
3906get_callback(typval_T *arg)
3907{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003908 callback_T res;
3909 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003910
3911 res.cb_free_name = FALSE;
3912 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3913 {
3914 res.cb_partial = arg->vval.v_partial;
3915 ++res.cb_partial->pt_refcount;
3916 res.cb_name = partial_name(res.cb_partial);
3917 }
3918 else
3919 {
3920 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003921 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3922 && isdigit(*arg->vval.v_string))
3923 r = FAIL;
3924 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003925 {
3926 // Note that we don't make a copy of the string.
3927 res.cb_name = arg->vval.v_string;
3928 func_ref(res.cb_name);
3929 }
3930 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003931 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003932 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003933 r = FAIL;
3934
3935 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003936 {
3937 emsg(_("E921: Invalid callback argument"));
3938 res.cb_name = NULL;
3939 }
3940 }
3941 return res;
3942}
3943
3944/*
3945 * Copy a callback into a typval_T.
3946 */
3947 void
3948put_callback(callback_T *cb, typval_T *tv)
3949{
3950 if (cb->cb_partial != NULL)
3951 {
3952 tv->v_type = VAR_PARTIAL;
3953 tv->vval.v_partial = cb->cb_partial;
3954 ++tv->vval.v_partial->pt_refcount;
3955 }
3956 else
3957 {
3958 tv->v_type = VAR_FUNC;
3959 tv->vval.v_string = vim_strsave(cb->cb_name);
3960 func_ref(cb->cb_name);
3961 }
3962}
3963
3964/*
3965 * Make a copy of "src" into "dest", allocating the function name if needed,
3966 * without incrementing the refcount.
3967 */
3968 void
3969set_callback(callback_T *dest, callback_T *src)
3970{
3971 if (src->cb_partial == NULL)
3972 {
3973 // just a function name, make a copy
3974 dest->cb_name = vim_strsave(src->cb_name);
3975 dest->cb_free_name = TRUE;
3976 }
3977 else
3978 {
3979 // cb_name is a pointer into cb_partial
3980 dest->cb_name = src->cb_name;
3981 dest->cb_free_name = FALSE;
3982 }
3983 dest->cb_partial = src->cb_partial;
3984}
3985
3986/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003987 * Copy callback from "src" to "dest", incrementing the refcounts.
3988 */
3989 void
3990copy_callback(callback_T *dest, callback_T *src)
3991{
3992 dest->cb_partial = src->cb_partial;
3993 if (dest->cb_partial != NULL)
3994 {
3995 dest->cb_name = src->cb_name;
3996 dest->cb_free_name = FALSE;
3997 ++dest->cb_partial->pt_refcount;
3998 }
3999 else
4000 {
4001 dest->cb_name = vim_strsave(src->cb_name);
4002 dest->cb_free_name = TRUE;
4003 func_ref(src->cb_name);
4004 }
4005}
4006
4007/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004008 * Unref/free "callback" returned by get_callback() or set_callback().
4009 */
4010 void
4011free_callback(callback_T *callback)
4012{
4013 if (callback->cb_partial != NULL)
4014 {
4015 partial_unref(callback->cb_partial);
4016 callback->cb_partial = NULL;
4017 }
4018 else if (callback->cb_name != NULL)
4019 func_unref(callback->cb_name);
4020 if (callback->cb_free_name)
4021 {
4022 vim_free(callback->cb_name);
4023 callback->cb_free_name = FALSE;
4024 }
4025 callback->cb_name = NULL;
4026}
4027
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004028#endif // FEAT_EVAL