blob: 2de8b8dd5e85a43c0c126a175aed8b9912b48b52 [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 Moolenaarf0068c52020-11-30 17:42:10 +0100149 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200150};
151
152// shorthand
153#define vv_type vv_di.di_tv.v_type
154#define vv_nr vv_di.di_tv.vval.v_number
155#define vv_float vv_di.di_tv.vval.v_float
156#define vv_str vv_di.di_tv.vval.v_string
157#define vv_list vv_di.di_tv.vval.v_list
158#define vv_dict vv_di.di_tv.vval.v_dict
159#define vv_blob vv_di.di_tv.vval.v_blob
160#define vv_tv vv_di.di_tv
161
162static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200163static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200164#define vimvarht vimvardict.dv_hashtab
165
166// for VIM_VERSION_ defines
167#include "version.h"
168
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void list_glob_vars(int *first);
170static void list_buf_vars(int *first);
171static void list_win_vars(int *first);
172static void list_tab_vars(int *first);
173static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200175static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
176static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200217 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
218 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100222 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
Bram Moolenaared234f22020-10-15 20:42:20 +0200304 int i;
305 int idx;
306 int abort = FALSE;
307 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100309 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200310 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200311 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
312
Bram Moolenaared234f22020-10-15 20:42:20 +0200313 si = SCRIPT_ITEM(i);
314 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
315 {
316 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
317
318 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
319 }
320 }
321
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200322 return abort;
323}
324
325/*
326 * Set an internal variable to a string value. Creates the variable if it does
327 * not already exist.
328 */
329 void
330set_internal_string_var(char_u *name, char_u *value)
331{
332 char_u *val;
333 typval_T *tvp;
334
335 val = vim_strsave(value);
336 if (val != NULL)
337 {
338 tvp = alloc_string_tv(val);
339 if (tvp != NULL)
340 {
341 set_var(name, tvp, FALSE);
342 free_tv(tvp);
343 }
344 }
345}
346
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200347 int
348eval_charconvert(
349 char_u *enc_from,
350 char_u *enc_to,
351 char_u *fname_from,
352 char_u *fname_to)
353{
354 int err = FALSE;
355
356 set_vim_var_string(VV_CC_FROM, enc_from, -1);
357 set_vim_var_string(VV_CC_TO, enc_to, -1);
358 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
359 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
360 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
361 err = TRUE;
362 set_vim_var_string(VV_CC_FROM, NULL, -1);
363 set_vim_var_string(VV_CC_TO, NULL, -1);
364 set_vim_var_string(VV_FNAME_IN, NULL, -1);
365 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
366
367 if (err)
368 return FAIL;
369 return OK;
370}
371
372# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
373 int
374eval_printexpr(char_u *fname, char_u *args)
375{
376 int err = FALSE;
377
378 set_vim_var_string(VV_FNAME_IN, fname, -1);
379 set_vim_var_string(VV_CMDARG, args, -1);
380 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
381 err = TRUE;
382 set_vim_var_string(VV_FNAME_IN, NULL, -1);
383 set_vim_var_string(VV_CMDARG, NULL, -1);
384
385 if (err)
386 {
387 mch_remove(fname);
388 return FAIL;
389 }
390 return OK;
391}
392# endif
393
394# if defined(FEAT_DIFF) || defined(PROTO)
395 void
396eval_diff(
397 char_u *origfile,
398 char_u *newfile,
399 char_u *outfile)
400{
401 int err = FALSE;
402
403 set_vim_var_string(VV_FNAME_IN, origfile, -1);
404 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
405 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
406 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
407 set_vim_var_string(VV_FNAME_IN, NULL, -1);
408 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
409 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
410}
411
412 void
413eval_patch(
414 char_u *origfile,
415 char_u *difffile,
416 char_u *outfile)
417{
418 int err;
419
420 set_vim_var_string(VV_FNAME_IN, origfile, -1);
421 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
422 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
423 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
424 set_vim_var_string(VV_FNAME_IN, NULL, -1);
425 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
426 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
427}
428# endif
429
430#if defined(FEAT_SPELL) || defined(PROTO)
431/*
432 * Evaluate an expression to a list with suggestions.
433 * For the "expr:" part of 'spellsuggest'.
434 * Returns NULL when there is an error.
435 */
436 list_T *
437eval_spell_expr(char_u *badword, char_u *expr)
438{
439 typval_T save_val;
440 typval_T rettv;
441 list_T *list = NULL;
442 char_u *p = skipwhite(expr);
443
444 // Set "v:val" to the bad word.
445 prepare_vimvar(VV_VAL, &save_val);
446 set_vim_var_string(VV_VAL, badword, -1);
447 if (p_verbose == 0)
448 ++emsg_off;
449
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200450 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200451 {
452 if (rettv.v_type != VAR_LIST)
453 clear_tv(&rettv);
454 else
455 list = rettv.vval.v_list;
456 }
457
458 if (p_verbose == 0)
459 --emsg_off;
460 clear_tv(get_vim_var_tv(VV_VAL));
461 restore_vimvar(VV_VAL, &save_val);
462
463 return list;
464}
465
466/*
467 * "list" is supposed to contain two items: a word and a number. Return the
468 * word in "pp" and the number as the return value.
469 * Return -1 if anything isn't right.
470 * Used to get the good word and score from the eval_spell_expr() result.
471 */
472 int
473get_spellword(list_T *list, char_u **pp)
474{
475 listitem_T *li;
476
477 li = list->lv_first;
478 if (li == NULL)
479 return -1;
480 *pp = tv_get_string(&li->li_tv);
481
482 li = li->li_next;
483 if (li == NULL)
484 return -1;
485 return (int)tv_get_number(&li->li_tv);
486}
487#endif
488
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200489/*
490 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200491 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200492 * When not used yet add the variable to the v: hashtable.
493 */
494 void
495prepare_vimvar(int idx, typval_T *save_tv)
496{
497 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200498 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200499 if (vimvars[idx].vv_type == VAR_UNKNOWN)
500 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
501}
502
503/*
504 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200505 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200506 * When no longer defined, remove the variable from the v: hashtable.
507 */
508 void
509restore_vimvar(int idx, typval_T *save_tv)
510{
511 hashitem_T *hi;
512
513 vimvars[idx].vv_tv = *save_tv;
514 if (vimvars[idx].vv_type == VAR_UNKNOWN)
515 {
516 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
517 if (HASHITEM_EMPTY(hi))
518 internal_error("restore_vimvar()");
519 else
520 hash_remove(&vimvarht, hi);
521 }
522}
523
524/*
525 * List Vim variables.
526 */
527 static void
528list_vim_vars(int *first)
529{
530 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
531}
532
533/*
534 * List script-local variables, if there is a script.
535 */
536 static void
537list_script_vars(int *first)
538{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200539 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200540 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
541 "s:", FALSE, first);
542}
543
544/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200545 * Get a list of lines from a HERE document. The here document is a list of
546 * lines surrounded by a marker.
547 * cmd << {marker}
548 * {line1}
549 * {line2}
550 * ....
551 * {marker}
552 *
553 * The {marker} is a string. If the optional 'trim' word is supplied before the
554 * marker, then the leading indentation before the lines (matching the
555 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200556 *
557 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
558 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
559 * missing, then '.' is accepted as a marker.
560 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561 * Returns a List with {lines} or NULL.
562 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200564heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565{
566 char_u *theline;
567 char_u *marker;
568 list_T *l;
569 char_u *p;
570 int marker_indent_len = 0;
571 int text_indent_len = 0;
572 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200573 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200574 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200575
576 if (eap->getline == NULL)
577 {
578 emsg(_("E991: cannot use =<< here"));
579 return NULL;
580 }
581
582 // Check for the optional 'trim' word before the marker
583 cmd = skipwhite(cmd);
584 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
585 {
586 cmd = skipwhite(cmd + 4);
587
588 // Trim the indentation from all the lines in the here document.
589 // The amount of indentation trimmed is the same as the indentation of
590 // the first line after the :let command line. To find the end marker
591 // the indent of the :let command line is trimmed.
592 p = *eap->cmdlinep;
593 while (VIM_ISWHITE(*p))
594 {
595 p++;
596 marker_indent_len++;
597 }
598 text_indent_len = -1;
599 }
600
601 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200602 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200603 {
604 marker = skipwhite(cmd);
605 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200606 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200608 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200609 return NULL;
610 }
611 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200612 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200613 {
614 emsg(_("E221: Marker cannot start with lower case letter"));
615 return NULL;
616 }
617 }
618 else
619 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200620 // When getting lines for an embedded script, if the marker is missing,
621 // accept '.' as the marker.
622 if (script_get)
623 marker = dot;
624 else
625 {
626 emsg(_("E172: Missing marker"));
627 return NULL;
628 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200629 }
630
631 l = list_alloc();
632 if (l == NULL)
633 return NULL;
634
635 for (;;)
636 {
637 int mi = 0;
638 int ti = 0;
639
640 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
641 if (theline == NULL)
642 {
643 semsg(_("E990: Missing end marker '%s'"), marker);
644 break;
645 }
646
647 // with "trim": skip the indent matching the :let line to find the
648 // marker
649 if (marker_indent_len > 0
650 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
651 mi = marker_indent_len;
652 if (STRCMP(marker, theline + mi) == 0)
653 {
654 vim_free(theline);
655 break;
656 }
657
658 if (text_indent_len == -1 && *theline != NUL)
659 {
660 // set the text indent from the first line.
661 p = theline;
662 text_indent_len = 0;
663 while (VIM_ISWHITE(*p))
664 {
665 p++;
666 text_indent_len++;
667 }
668 text_indent = vim_strnsave(theline, text_indent_len);
669 }
670 // with "trim": skip the indent matching the first line
671 if (text_indent != NULL)
672 for (ti = 0; ti < text_indent_len; ++ti)
673 if (theline[ti] != text_indent[ti])
674 break;
675
676 if (list_append_string(l, theline + ti, -1) == FAIL)
677 break;
678 vim_free(theline);
679 }
680 vim_free(text_indent);
681
682 return l;
683}
684
685/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200686 * Vim9 variable declaration:
687 * ":var name"
688 * ":var name: type"
689 * ":var name = expr"
690 * ":var name: type = expr"
691 * etc.
692 */
693 void
694ex_var(exarg_T *eap)
695{
696 if (!in_vim9script())
697 {
698 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
699 return;
700 }
701 ex_let(eap);
702}
703
704/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200705 * ":let" list all variable values
706 * ":let var1 var2" list variable values
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 var .= expr" assignment command.
714 * ":let var ..= expr" assignment command.
715 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100717 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200718 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200719 * ":final var = expr" assignment command.
720 * ":final [var1, var2] = expr" unpack list.
721 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200722 * ":const" list all variable values
723 * ":const var1 var2" list variable values
724 * ":const var = expr" assignment command.
725 * ":const [var1, var2] = expr" unpack list.
726 */
727 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200728ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729{
730 char_u *arg = eap->arg;
731 char_u *expr = NULL;
732 typval_T rettv;
733 int i;
734 int var_count = 0;
735 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200736 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200737 char_u *argend;
738 int first = TRUE;
739 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200740 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100741 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200742 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200744 if (eap->cmdidx == CMD_final && !vim9script)
745 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap);
748 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200749 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200750 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200751 {
752 emsg(_(e_cannot_use_let_in_vim9_script));
753 return;
754 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200755
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100756 if (eap->cmdidx == CMD_const)
757 flags |= ASSIGN_CONST;
758 else if (eap->cmdidx == CMD_final)
759 flags |= ASSIGN_FINAL;
760
761 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200763 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200765 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200766 if (argend == NULL)
767 return;
768 if (argend > arg && argend[-1] == '.') // for var.='str'
769 --argend;
770 expr = skipwhite(argend);
771 concat = expr[0] == '.'
772 && ((expr[1] == '=' && current_sctx.sc_version < 2)
773 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200774 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
775 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200776 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 {
778 // ":let" without "=": list variables
779 if (*arg == '[')
780 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200781 else if (expr[0] == '.' && expr[1] == '=')
782 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200783 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200785 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200786 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100787 // Vim9 declaration ":var name: type"
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 arg = vim9_declare_scriptvar(eap, arg);
789 }
790 else
791 {
792 // ":let var1 var2" - list values
793 arg = list_arg_vars(eap, arg, &first);
794 }
795 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 else if (!eap->skip)
797 {
798 // ":let"
799 list_glob_vars(&first);
800 list_buf_vars(&first);
801 list_win_vars(&first);
802 list_tab_vars(&first);
803 list_script_vars(&first);
804 list_func_vars(&first);
805 list_vim_vars(&first);
806 }
807 eap->nextcmd = check_nextcmd(arg);
808 }
809 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
810 {
811 list_T *l;
812
813 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200814 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200815 if (l != NULL)
816 {
817 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200818 if (!eap->skip)
819 {
820 op[0] = '=';
821 op[1] = NUL;
822 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100823 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200824 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200825 clear_tv(&rettv);
826 }
827 }
828 else
829 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200830 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200831 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200832
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200833 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200834 i = FAIL;
835 if (has_assign || concat)
836 {
837 op[0] = '=';
838 op[1] = NUL;
839 if (*expr != '=')
840 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200841 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200842 {
843 // +=, /=, etc. require an existing variable
844 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
845 i = FAIL;
846 }
847 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848 {
849 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200853 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 ++len;
855 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200857 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200858 }
859 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200860 ++expr;
861
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200862 if (vim9script && (!VIM_ISWHITE(*argend)
863 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 {
865 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200866 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200867 i = FAIL;
868 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200869
870 if (eap->skip)
871 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200872 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200873 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200874 if (getline_equal(eap->getline, eap->cookie, getsourceline))
875 {
876 evalarg.eval_getline = eap->getline;
877 evalarg.eval_cookie = eap->cookie;
878 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200879 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200880 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200881 if (eap->skip)
882 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200883 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200884 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200885 if (eap->skip)
886 {
887 if (i != FAIL)
888 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200889 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200890 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200891 {
892 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200893 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200894 clear_tv(&rettv);
895 }
896 }
897}
898
899/*
900 * Assign the typevalue "tv" to the variable or variables at "arg_start".
901 * Handles both "var" with any type and "[var, var; var]" with a list type.
902 * When "op" is not NULL it points to a string with characters that
903 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
904 * or concatenate.
905 * Returns OK or FAIL;
906 */
907 int
908ex_let_vars(
909 char_u *arg_start,
910 typval_T *tv,
911 int copy, // copy values from "tv", don't move
912 int semicolon, // from skip_var_list()
913 int var_count, // from skip_var_list()
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100914 int flags, // ASSIGN_FINAL, ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200915 char_u *op)
916{
917 char_u *arg = arg_start;
918 list_T *l;
919 int i;
920 listitem_T *item;
921 typval_T ltv;
922
923 if (*arg != '[')
924 {
925 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200927 return FAIL;
928 return OK;
929 }
930
931 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
932 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
933 {
934 emsg(_(e_listreq));
935 return FAIL;
936 }
937
938 i = list_len(l);
939 if (semicolon == 0 && var_count < i)
940 {
941 emsg(_("E687: Less targets than List items"));
942 return FAIL;
943 }
944 if (var_count - semicolon > i)
945 {
946 emsg(_("E688: More targets than List items"));
947 return FAIL;
948 }
949
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200950 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200951 item = l->lv_first;
952 while (*arg != ']')
953 {
954 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200956 item = item->li_next;
957 if (arg == NULL)
958 return FAIL;
959
960 arg = skipwhite(arg);
961 if (*arg == ';')
962 {
963 // Put the rest of the list (may be empty) in the var after ';'.
964 // Create a new list for this.
965 l = list_alloc();
966 if (l == NULL)
967 return FAIL;
968 while (item != NULL)
969 {
970 list_append_tv(l, &item->li_tv);
971 item = item->li_next;
972 }
973
974 ltv.v_type = VAR_LIST;
975 ltv.v_lock = 0;
976 ltv.vval.v_list = l;
977 l->lv_refcount = 1;
978
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980 (char_u *)"]", op);
981 clear_tv(&ltv);
982 if (arg == NULL)
983 return FAIL;
984 break;
985 }
986 else if (*arg != ',' && *arg != ']')
987 {
988 internal_error("ex_let_vars()");
989 return FAIL;
990 }
991 }
992
993 return OK;
994}
995
996/*
997 * Skip over assignable variable "var" or list of variables "[var, var]".
998 * Used for ":let varvar = expr" and ":for varvar in expr".
999 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001000 * for "[var, var; var]" set "semicolon" to 1.
1001 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001002 * Return NULL for an error.
1003 */
1004 char_u *
1005skip_var_list(
1006 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001008 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001009 int *semicolon,
1010 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001011{
1012 char_u *p, *s;
1013
1014 if (*arg == '[')
1015 {
1016 // "[var, var]": find the matching ']'.
1017 p = arg;
1018 for (;;)
1019 {
1020 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001021 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001022 if (s == p)
1023 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001024 if (!silent)
1025 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001026 return NULL;
1027 }
1028 ++*var_count;
1029
1030 p = skipwhite(s);
1031 if (*p == ']')
1032 break;
1033 else if (*p == ';')
1034 {
1035 if (*semicolon == 1)
1036 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001037 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038 return NULL;
1039 }
1040 *semicolon = 1;
1041 }
1042 else if (*p != ',')
1043 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001044 if (!silent)
1045 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 return NULL;
1047 }
1048 }
1049 return p + 1;
1050 }
1051 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001053}
1054
1055/*
1056 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1057 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001059 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001060 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 char_u *end;
1064
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001065 if (*arg == '@' && arg[1] != NUL)
1066 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001068 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001069 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001071 // "a: type" is declaring variable "a" with a type, not "a:".
1072 if (end == arg + 2 && end[-1] == ':')
1073 --end;
1074 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001075 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 }
1077 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001078}
1079
1080/*
1081 * List variables for hashtab "ht" with prefix "prefix".
1082 * If "empty" is TRUE also list NULL strings as empty strings.
1083 */
1084 void
1085list_hashtable_vars(
1086 hashtab_T *ht,
1087 char *prefix,
1088 int empty,
1089 int *first)
1090{
1091 hashitem_T *hi;
1092 dictitem_T *di;
1093 int todo;
1094 char_u buf[IOSIZE];
1095
1096 todo = (int)ht->ht_used;
1097 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1098 {
1099 if (!HASHITEM_EMPTY(hi))
1100 {
1101 --todo;
1102 di = HI2DI(hi);
1103
1104 // apply :filter /pat/ to variable name
1105 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1106 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1107 if (message_filtered(buf))
1108 continue;
1109
1110 if (empty || di->di_tv.v_type != VAR_STRING
1111 || di->di_tv.vval.v_string != NULL)
1112 list_one_var(di, prefix, first);
1113 }
1114 }
1115}
1116
1117/*
1118 * List global variables.
1119 */
1120 static void
1121list_glob_vars(int *first)
1122{
1123 list_hashtable_vars(&globvarht, "", TRUE, first);
1124}
1125
1126/*
1127 * List buffer variables.
1128 */
1129 static void
1130list_buf_vars(int *first)
1131{
1132 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1133}
1134
1135/*
1136 * List window variables.
1137 */
1138 static void
1139list_win_vars(int *first)
1140{
1141 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1142}
1143
1144/*
1145 * List tab page variables.
1146 */
1147 static void
1148list_tab_vars(int *first)
1149{
1150 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1151}
1152
1153/*
1154 * List variables in "arg".
1155 */
1156 static char_u *
1157list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1158{
1159 int error = FALSE;
1160 int len;
1161 char_u *name;
1162 char_u *name_start;
1163 char_u *arg_subsc;
1164 char_u *tofree;
1165 typval_T tv;
1166
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001167 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001168 {
1169 if (error || eap->skip)
1170 {
1171 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1172 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1173 {
1174 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001175 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001176 break;
1177 }
1178 }
1179 else
1180 {
1181 // get_name_len() takes care of expanding curly braces
1182 name_start = name = arg;
1183 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1184 if (len <= 0)
1185 {
1186 // This is mainly to keep test 49 working: when expanding
1187 // curly braces fails overrule the exception error message.
1188 if (len < 0 && !aborting())
1189 {
1190 emsg_severe = TRUE;
1191 semsg(_(e_invarg2), arg);
1192 break;
1193 }
1194 error = TRUE;
1195 }
1196 else
1197 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001198 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001199 if (tofree != NULL)
1200 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001201 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001202 error = TRUE;
1203 else
1204 {
1205 // handle d.key, l[idx], f(expr)
1206 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001207 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001208 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001209 error = TRUE;
1210 else
1211 {
1212 if (arg == arg_subsc && len == 2 && name[1] == ':')
1213 {
1214 switch (*name)
1215 {
1216 case 'g': list_glob_vars(first); break;
1217 case 'b': list_buf_vars(first); break;
1218 case 'w': list_win_vars(first); break;
1219 case 't': list_tab_vars(first); break;
1220 case 'v': list_vim_vars(first); break;
1221 case 's': list_script_vars(first); break;
1222 case 'l': list_func_vars(first); break;
1223 default:
1224 semsg(_("E738: Can't list variables for %s"), name);
1225 }
1226 }
1227 else
1228 {
1229 char_u numbuf[NUMBUFLEN];
1230 char_u *tf;
1231 int c;
1232 char_u *s;
1233
1234 s = echo_string(&tv, &tf, numbuf, 0);
1235 c = *arg;
1236 *arg = NUL;
1237 list_one_var_a("",
1238 arg == arg_subsc ? name : name_start,
1239 tv.v_type,
1240 s == NULL ? (char_u *)"" : s,
1241 first);
1242 *arg = c;
1243 vim_free(tf);
1244 }
1245 clear_tv(&tv);
1246 }
1247 }
1248 }
1249
1250 vim_free(tofree);
1251 }
1252
1253 arg = skipwhite(arg);
1254 }
1255
1256 return arg;
1257}
1258
1259/*
1260 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1261 * Returns a pointer to the char just after the var name.
1262 * Returns NULL if there is an error.
1263 */
1264 static char_u *
1265ex_let_one(
1266 char_u *arg, // points to variable name
1267 typval_T *tv, // value to assign to variable
1268 int copy, // copy value from "tv"
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001269 int flags, // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001270 char_u *endchars, // valid chars after variable name or NULL
1271 char_u *op) // "+", "-", "." or NULL
1272{
1273 int c1;
1274 char_u *name;
1275 char_u *p;
1276 char_u *arg_end = NULL;
1277 int len;
1278 int opt_flags;
1279 char_u *tofree = NULL;
1280
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001281 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001282 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001283 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1284 {
1285 vim9_declare_error(arg);
1286 return NULL;
1287 }
1288
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289 // ":let $VAR = expr": Set environment variable.
1290 if (*arg == '$')
1291 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001292 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 {
1294 emsg(_("E996: Cannot lock an environment variable"));
1295 return NULL;
1296 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001297
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 // Find the end of the name.
1299 ++arg;
1300 name = arg;
1301 len = get_env_len(&arg);
1302 if (len == 0)
1303 semsg(_(e_invarg2), name - 1);
1304 else
1305 {
1306 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1307 semsg(_(e_letwrong), op);
1308 else if (endchars != NULL
1309 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1310 emsg(_(e_letunexp));
1311 else if (!check_secure())
1312 {
1313 c1 = name[len];
1314 name[len] = NUL;
1315 p = tv_get_string_chk(tv);
1316 if (p != NULL && op != NULL && *op == '.')
1317 {
1318 int mustfree = FALSE;
1319 char_u *s = vim_getenv(name, &mustfree);
1320
1321 if (s != NULL)
1322 {
1323 p = tofree = concat_str(s, p);
1324 if (mustfree)
1325 vim_free(s);
1326 }
1327 }
1328 if (p != NULL)
1329 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001331 arg_end = arg;
1332 }
1333 name[len] = c1;
1334 vim_free(tofree);
1335 }
1336 }
1337 }
1338
1339 // ":let &option = expr": Set option value.
1340 // ":let &l:option = expr": Set local option value.
1341 // ":let &g:option = expr": Set global option value.
1342 else if (*arg == '&')
1343 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001344 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001347 return NULL;
1348 }
1349 // Find the end of the name.
1350 p = find_option_end(&arg, &opt_flags);
1351 if (p == NULL || (endchars != NULL
1352 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1353 emsg(_(e_letunexp));
1354 else
1355 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001356 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357 int opt_type;
1358 long numval;
1359 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001360 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001361 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001362
1363 c1 = *p;
1364 *p = NUL;
1365
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001366 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1367 if ((opt_type == 1 || opt_type == -1)
1368 && (tv->v_type != VAR_STRING || !in_vim9script()))
1369 // number, possibly hidden
1370 n = (long)tv_get_number(tv);
1371
1372 // Avoid setting a string option to the text "v:false" or similar.
1373 // In Vim9 script also don't convert a number to string.
1374 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1375 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1376 s = tv_get_string_chk(tv);
1377
1378 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001379 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001380 if ((opt_type == 1 && *op == '.')
1381 || (opt_type == 0 && *op != '.'))
1382 {
1383 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001384 failed = TRUE; // don't set the value
1385
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001386 }
1387 else
1388 {
1389 if (opt_type == 1) // number
1390 {
1391 switch (*op)
1392 {
1393 case '+': n = numval + n; break;
1394 case '-': n = numval - n; break;
1395 case '*': n = numval * n; break;
1396 case '/': n = (long)num_divide(numval, n); break;
1397 case '%': n = (long)num_modulus(numval, n); break;
1398 }
1399 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001400 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001402 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403 s = concat_str(stringval, s);
1404 vim_free(stringval);
1405 stringval = s;
1406 }
1407 }
1408 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001409
1410 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001411 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001412 if (opt_type != 0 || s != NULL)
1413 {
1414 set_option_value(arg, n, s, opt_flags);
1415 arg_end = p;
1416 }
1417 else
1418 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001419 }
1420 *p = c1;
1421 vim_free(stringval);
1422 }
1423 }
1424
1425 // ":let @r = expr": Set register contents.
1426 else if (*arg == '@')
1427 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001428 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001429 {
1430 emsg(_("E996: Cannot lock a register"));
1431 return NULL;
1432 }
1433 ++arg;
1434 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1435 semsg(_(e_letwrong), op);
1436 else if (endchars != NULL
1437 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1438 emsg(_(e_letunexp));
1439 else
1440 {
1441 char_u *ptofree = NULL;
1442 char_u *s;
1443
1444 p = tv_get_string_chk(tv);
1445 if (p != NULL && op != NULL && *op == '.')
1446 {
1447 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1448 if (s != NULL)
1449 {
1450 p = ptofree = concat_str(s, p);
1451 vim_free(s);
1452 }
1453 }
1454 if (p != NULL)
1455 {
1456 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1457 arg_end = arg + 1;
1458 }
1459 vim_free(ptofree);
1460 }
1461 }
1462
1463 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 // ":let {expr} = expr": Idem, name made with curly braces
1466 else if (eval_isnamec1(*arg) || *arg == '{')
1467 {
1468 lval_T lv;
1469
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001470 p = get_lval(arg, tv, &lv, FALSE, FALSE,
1471 (flags & ASSIGN_NO_DECL) ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001472 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 if (endchars != NULL && vim_strchr(endchars,
1475 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 emsg(_(e_letunexp));
1477 else
1478 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001480 arg_end = p;
1481 }
1482 }
1483 clear_lval(&lv);
1484 }
1485
1486 else
1487 semsg(_(e_invarg2), arg);
1488
1489 return arg_end;
1490}
1491
1492/*
1493 * ":unlet[!] var1 ... " command.
1494 */
1495 void
1496ex_unlet(exarg_T *eap)
1497{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001498 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001499}
1500
1501/*
1502 * ":lockvar" and ":unlockvar" commands
1503 */
1504 void
1505ex_lockvar(exarg_T *eap)
1506{
1507 char_u *arg = eap->arg;
1508 int deep = 2;
1509
1510 if (eap->forceit)
1511 deep = -1;
1512 else if (vim_isdigit(*arg))
1513 {
1514 deep = getdigits(&arg);
1515 arg = skipwhite(arg);
1516 }
1517
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001518 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001519}
1520
1521/*
1522 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001523 * Also used for Vim9 script. "callback" is invoked as:
1524 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001525 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001526 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001527ex_unletlock(
1528 exarg_T *eap,
1529 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001530 int deep,
1531 int glv_flags,
1532 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1533 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001534{
1535 char_u *arg = argstart;
1536 char_u *name_end;
1537 int error = FALSE;
1538 lval_T lv;
1539
1540 do
1541 {
1542 if (*arg == '$')
1543 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001544 lv.ll_name = arg;
1545 lv.ll_tv = NULL;
1546 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001547 if (get_env_len(&arg) == 0)
1548 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001549 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001550 return;
1551 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001552 if (!error && !eap->skip
1553 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1554 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001555 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001556 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001557 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001558 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001559 // Parse the name and find the end.
1560 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1561 glv_flags, FNE_CHECK_START);
1562 if (lv.ll_name == NULL)
1563 error = TRUE; // error but continue parsing
1564 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1565 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001566 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001567 if (name_end != NULL)
1568 {
1569 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001570 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001571 }
1572 if (!(eap->skip || error))
1573 clear_lval(&lv);
1574 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001577 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001578 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001579 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001580
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001581 if (!eap->skip)
1582 clear_lval(&lv);
1583 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584
1585 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001586 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587
1588 eap->nextcmd = check_nextcmd(arg);
1589}
1590
1591 static int
1592do_unlet_var(
1593 lval_T *lp,
1594 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001595 exarg_T *eap,
1596 int deep UNUSED,
1597 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001598{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001599 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 int ret = OK;
1601 int cc;
1602
1603 if (lp->ll_tv == NULL)
1604 {
1605 cc = *name_end;
1606 *name_end = NUL;
1607
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001608 // Environment variable, normal name or expanded name.
1609 if (*lp->ll_name == '$')
1610 vim_unsetenv(lp->ll_name + 1);
1611 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612 ret = FAIL;
1613 *name_end = cc;
1614 }
1615 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001616 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001618 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001619 return FAIL;
1620 else if (lp->ll_range)
1621 {
1622 listitem_T *li;
1623 listitem_T *ll_li = lp->ll_li;
1624 int ll_n1 = lp->ll_n1;
1625
1626 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1627 {
1628 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001629 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001630 return FAIL;
1631 ll_li = li;
1632 ++ll_n1;
1633 }
1634
1635 // Delete a range of List items.
1636 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1637 {
1638 li = lp->ll_li->li_next;
1639 listitem_remove(lp->ll_list, lp->ll_li);
1640 lp->ll_li = li;
1641 ++lp->ll_n1;
1642 }
1643 }
1644 else
1645 {
1646 if (lp->ll_list != NULL)
1647 // unlet a List item.
1648 listitem_remove(lp->ll_list, lp->ll_li);
1649 else
1650 // unlet a Dictionary item.
1651 dictitem_remove(lp->ll_dict, lp->ll_di);
1652 }
1653
1654 return ret;
1655}
1656
1657/*
1658 * "unlet" a variable. Return OK if it existed, FAIL if not.
1659 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1660 */
1661 int
1662do_unlet(char_u *name, int forceit)
1663{
1664 hashtab_T *ht;
1665 hashitem_T *hi;
1666 char_u *varname;
1667 dict_T *d;
1668 dictitem_T *di;
1669
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001670 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001671 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001672 return FAIL;
1673
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001674 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001675
1676 // can't :unlet a script variable in Vim9 script from a function
1677 if (ht == get_script_local_ht()
1678 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1679 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1680 == SCRIPT_VERSION_VIM9
1681 && check_vim9_unlet(name) == FAIL)
1682 return FAIL;
1683
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001684 if (ht != NULL && *varname != NUL)
1685 {
1686 d = get_current_funccal_dict(ht);
1687 if (d == NULL)
1688 {
1689 if (ht == &globvarht)
1690 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001691 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001692 d = &vimvardict;
1693 else
1694 {
1695 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1696 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1697 }
1698 if (d == NULL)
1699 {
1700 internal_error("do_unlet()");
1701 return FAIL;
1702 }
1703 }
1704 hi = hash_find(ht, varname);
1705 if (HASHITEM_EMPTY(hi))
1706 hi = find_hi_in_scoped_ht(name, &ht);
1707 if (hi != NULL && !HASHITEM_EMPTY(hi))
1708 {
1709 di = HI2DI(hi);
1710 if (var_check_fixed(di->di_flags, name, FALSE)
1711 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001712 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001713 return FAIL;
1714
1715 delete_var(ht, hi);
1716 return OK;
1717 }
1718 }
1719 if (forceit)
1720 return OK;
1721 semsg(_("E108: No such variable: \"%s\""), name);
1722 return FAIL;
1723}
1724
1725/*
1726 * Lock or unlock variable indicated by "lp".
1727 * "deep" is the levels to go (-1 for unlimited);
1728 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1729 */
1730 static int
1731do_lock_var(
1732 lval_T *lp,
1733 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001734 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001736 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001737{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001738 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001739 int ret = OK;
1740 int cc;
1741 dictitem_T *di;
1742
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 if (lp->ll_tv == NULL)
1744 {
1745 cc = *name_end;
1746 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001747 if (*lp->ll_name == '$')
1748 {
1749 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001750 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001751 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001752 else
1753 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001754 // Normal name or expanded name.
1755 di = find_var(lp->ll_name, NULL, TRUE);
1756 if (di == NULL)
1757 ret = FAIL;
1758 else if ((di->di_flags & DI_FLAGS_FIX)
1759 && di->di_tv.v_type != VAR_DICT
1760 && di->di_tv.v_type != VAR_LIST)
1761 {
1762 // For historic reasons this error is not given for a list or
1763 // dict. E.g., the b: dict could be locked/unlocked.
1764 semsg(_(e_lock_unlock), lp->ll_name);
1765 ret = FAIL;
1766 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001767 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001768 {
1769 if (lock)
1770 di->di_flags |= DI_FLAGS_LOCK;
1771 else
1772 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001773 if (deep != 0)
1774 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001775 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776 }
1777 *name_end = cc;
1778 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001779 else if (deep == 0)
1780 {
1781 // nothing to do
1782 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001783 else if (lp->ll_range)
1784 {
1785 listitem_T *li = lp->ll_li;
1786
1787 // (un)lock a range of List items.
1788 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1789 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001790 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001791 li = li->li_next;
1792 ++lp->ll_n1;
1793 }
1794 }
1795 else if (lp->ll_list != NULL)
1796 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001797 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 else
1799 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001800 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801
1802 return ret;
1803}
1804
1805/*
1806 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001807 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1808 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001810 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001811item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812{
1813 static int recurse = 0;
1814 list_T *l;
1815 listitem_T *li;
1816 dict_T *d;
1817 blob_T *b;
1818 hashitem_T *hi;
1819 int todo;
1820
1821 if (recurse >= DICT_MAXNEST)
1822 {
1823 emsg(_("E743: variable nested too deep for (un)lock"));
1824 return;
1825 }
1826 if (deep == 0)
1827 return;
1828 ++recurse;
1829
1830 // lock/unlock the item itself
1831 if (lock)
1832 tv->v_lock |= VAR_LOCKED;
1833 else
1834 tv->v_lock &= ~VAR_LOCKED;
1835
1836 switch (tv->v_type)
1837 {
1838 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001839 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001843 case VAR_STRING:
1844 case VAR_FUNC:
1845 case VAR_PARTIAL:
1846 case VAR_FLOAT:
1847 case VAR_SPECIAL:
1848 case VAR_JOB:
1849 case VAR_CHANNEL:
1850 break;
1851
1852 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001853 if ((b = tv->vval.v_blob) != NULL
1854 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001855 {
1856 if (lock)
1857 b->bv_lock |= VAR_LOCKED;
1858 else
1859 b->bv_lock &= ~VAR_LOCKED;
1860 }
1861 break;
1862 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001863 if ((l = tv->vval.v_list) != NULL
1864 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 {
1866 if (lock)
1867 l->lv_lock |= VAR_LOCKED;
1868 else
1869 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001870 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001872 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001873 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001874 }
1875 break;
1876 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001877 if ((d = tv->vval.v_dict) != NULL
1878 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001879 {
1880 if (lock)
1881 d->dv_lock |= VAR_LOCKED;
1882 else
1883 d->dv_lock &= ~VAR_LOCKED;
1884 if (deep < 0 || deep > 1)
1885 {
1886 // recursive: lock/unlock the items the List contains
1887 todo = (int)d->dv_hashtab.ht_used;
1888 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1889 {
1890 if (!HASHITEM_EMPTY(hi))
1891 {
1892 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001893 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1894 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895 }
1896 }
1897 }
1898 }
1899 }
1900 --recurse;
1901}
1902
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001903#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1904/*
1905 * Delete all "menutrans_" variables.
1906 */
1907 void
1908del_menutrans_vars(void)
1909{
1910 hashitem_T *hi;
1911 int todo;
1912
1913 hash_lock(&globvarht);
1914 todo = (int)globvarht.ht_used;
1915 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1916 {
1917 if (!HASHITEM_EMPTY(hi))
1918 {
1919 --todo;
1920 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1921 delete_var(&globvarht, hi);
1922 }
1923 }
1924 hash_unlock(&globvarht);
1925}
1926#endif
1927
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001928/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001929 * Local string buffer for the next two functions to store a variable name
1930 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1931 * get_user_var_name().
1932 */
1933
1934static char_u *varnamebuf = NULL;
1935static int varnamebuflen = 0;
1936
1937/*
1938 * Function to concatenate a prefix and a variable name.
1939 */
1940 static char_u *
1941cat_prefix_varname(int prefix, char_u *name)
1942{
1943 int len;
1944
1945 len = (int)STRLEN(name) + 3;
1946 if (len > varnamebuflen)
1947 {
1948 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001949 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001950 varnamebuf = alloc(len);
1951 if (varnamebuf == NULL)
1952 {
1953 varnamebuflen = 0;
1954 return NULL;
1955 }
1956 varnamebuflen = len;
1957 }
1958 *varnamebuf = prefix;
1959 varnamebuf[1] = ':';
1960 STRCPY(varnamebuf + 2, name);
1961 return varnamebuf;
1962}
1963
1964/*
1965 * Function given to ExpandGeneric() to obtain the list of user defined
1966 * (global/buffer/window/built-in) variable names.
1967 */
1968 char_u *
1969get_user_var_name(expand_T *xp, int idx)
1970{
1971 static long_u gdone;
1972 static long_u bdone;
1973 static long_u wdone;
1974 static long_u tdone;
1975 static int vidx;
1976 static hashitem_T *hi;
1977 hashtab_T *ht;
1978
1979 if (idx == 0)
1980 {
1981 gdone = bdone = wdone = vidx = 0;
1982 tdone = 0;
1983 }
1984
1985 // Global variables
1986 if (gdone < globvarht.ht_used)
1987 {
1988 if (gdone++ == 0)
1989 hi = globvarht.ht_array;
1990 else
1991 ++hi;
1992 while (HASHITEM_EMPTY(hi))
1993 ++hi;
1994 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1995 return cat_prefix_varname('g', hi->hi_key);
1996 return hi->hi_key;
1997 }
1998
1999 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002000 ht =
2001#ifdef FEAT_CMDWIN
2002 // In cmdwin, the alternative buffer should be used.
2003 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2004 &prevwin->w_buffer->b_vars->dv_hashtab :
2005#endif
2006 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002007 if (bdone < ht->ht_used)
2008 {
2009 if (bdone++ == 0)
2010 hi = ht->ht_array;
2011 else
2012 ++hi;
2013 while (HASHITEM_EMPTY(hi))
2014 ++hi;
2015 return cat_prefix_varname('b', hi->hi_key);
2016 }
2017
2018 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002019 ht =
2020#ifdef FEAT_CMDWIN
2021 // In cmdwin, the alternative window should be used.
2022 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2023 &prevwin->w_vars->dv_hashtab :
2024#endif
2025 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002026 if (wdone < ht->ht_used)
2027 {
2028 if (wdone++ == 0)
2029 hi = ht->ht_array;
2030 else
2031 ++hi;
2032 while (HASHITEM_EMPTY(hi))
2033 ++hi;
2034 return cat_prefix_varname('w', hi->hi_key);
2035 }
2036
2037 // t: variables
2038 ht = &curtab->tp_vars->dv_hashtab;
2039 if (tdone < ht->ht_used)
2040 {
2041 if (tdone++ == 0)
2042 hi = ht->ht_array;
2043 else
2044 ++hi;
2045 while (HASHITEM_EMPTY(hi))
2046 ++hi;
2047 return cat_prefix_varname('t', hi->hi_key);
2048 }
2049
2050 // v: variables
2051 if (vidx < VV_LEN)
2052 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2053
2054 VIM_CLEAR(varnamebuf);
2055 varnamebuflen = 0;
2056 return NULL;
2057}
2058
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002059 char *
2060get_var_special_name(int nr)
2061{
2062 switch (nr)
2063 {
2064 case VVAL_FALSE: return "v:false";
2065 case VVAL_TRUE: return "v:true";
2066 case VVAL_NONE: return "v:none";
2067 case VVAL_NULL: return "v:null";
2068 }
2069 internal_error("get_var_special_name()");
2070 return "42";
2071}
2072
2073/*
2074 * Returns the global variable dictionary
2075 */
2076 dict_T *
2077get_globvar_dict(void)
2078{
2079 return &globvardict;
2080}
2081
2082/*
2083 * Returns the global variable hash table
2084 */
2085 hashtab_T *
2086get_globvar_ht(void)
2087{
2088 return &globvarht;
2089}
2090
2091/*
2092 * Returns the v: variable dictionary
2093 */
2094 dict_T *
2095get_vimvar_dict(void)
2096{
2097 return &vimvardict;
2098}
2099
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002100/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002102 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 */
2104 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002105find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002107 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2108 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109
2110 if (di == NULL)
2111 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002112 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2114 return (int)(vv - vimvars);
2115}
2116
2117
2118/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002119 * Set type of v: variable to "type".
2120 */
2121 void
2122set_vim_var_type(int idx, vartype_T type)
2123{
2124 vimvars[idx].vv_type = type;
2125}
2126
2127/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002128 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002129 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002130 */
2131 void
2132set_vim_var_nr(int idx, varnumber_T val)
2133{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002134 vimvars[idx].vv_nr = val;
2135}
2136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 char *
2138get_vim_var_name(int idx)
2139{
2140 return vimvars[idx].vv_name;
2141}
2142
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002143/*
2144 * Get typval_T v: variable value.
2145 */
2146 typval_T *
2147get_vim_var_tv(int idx)
2148{
2149 return &vimvars[idx].vv_tv;
2150}
2151
2152/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002153 * Set v: variable to "tv". Only accepts the same type.
2154 * Takes over the value of "tv".
2155 */
2156 int
2157set_vim_var_tv(int idx, typval_T *tv)
2158{
2159 if (vimvars[idx].vv_type != tv->v_type)
2160 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002161 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002162 clear_tv(tv);
2163 return FAIL;
2164 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002165 // VV_RO is also checked when compiling, but let's check here as well.
2166 if (vimvars[idx].vv_flags & VV_RO)
2167 {
2168 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2169 return FAIL;
2170 }
2171 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2172 {
2173 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2174 return FAIL;
2175 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002176 clear_tv(&vimvars[idx].vv_di.di_tv);
2177 vimvars[idx].vv_di.di_tv = *tv;
2178 return OK;
2179}
2180
2181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002182 * Get number v: variable value.
2183 */
2184 varnumber_T
2185get_vim_var_nr(int idx)
2186{
2187 return vimvars[idx].vv_nr;
2188}
2189
2190/*
2191 * Get string v: variable value. Uses a static buffer, can only be used once.
2192 * If the String variable has never been set, return an empty string.
2193 * Never returns NULL;
2194 */
2195 char_u *
2196get_vim_var_str(int idx)
2197{
2198 return tv_get_string(&vimvars[idx].vv_tv);
2199}
2200
2201/*
2202 * Get List v: variable value. Caller must take care of reference count when
2203 * needed.
2204 */
2205 list_T *
2206get_vim_var_list(int idx)
2207{
2208 return vimvars[idx].vv_list;
2209}
2210
2211/*
2212 * Get Dict v: variable value. Caller must take care of reference count when
2213 * needed.
2214 */
2215 dict_T *
2216get_vim_var_dict(int idx)
2217{
2218 return vimvars[idx].vv_dict;
2219}
2220
2221/*
2222 * Set v:char to character "c".
2223 */
2224 void
2225set_vim_var_char(int c)
2226{
2227 char_u buf[MB_MAXBYTES + 1];
2228
2229 if (has_mbyte)
2230 buf[(*mb_char2bytes)(c, buf)] = NUL;
2231 else
2232 {
2233 buf[0] = c;
2234 buf[1] = NUL;
2235 }
2236 set_vim_var_string(VV_CHAR, buf, -1);
2237}
2238
2239/*
2240 * Set v:count to "count" and v:count1 to "count1".
2241 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2242 */
2243 void
2244set_vcount(
2245 long count,
2246 long count1,
2247 int set_prevcount)
2248{
2249 if (set_prevcount)
2250 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2251 vimvars[VV_COUNT].vv_nr = count;
2252 vimvars[VV_COUNT1].vv_nr = count1;
2253}
2254
2255/*
2256 * Save variables that might be changed as a side effect. Used when executing
2257 * a timer callback.
2258 */
2259 void
2260save_vimvars(vimvars_save_T *vvsave)
2261{
2262 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2263 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2264 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2265}
2266
2267/*
2268 * Restore variables saved by save_vimvars().
2269 */
2270 void
2271restore_vimvars(vimvars_save_T *vvsave)
2272{
2273 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2274 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2275 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2276}
2277
2278/*
2279 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2280 * value.
2281 */
2282 void
2283set_vim_var_string(
2284 int idx,
2285 char_u *val,
2286 int len) // length of "val" to use or -1 (whole string)
2287{
2288 clear_tv(&vimvars[idx].vv_di.di_tv);
2289 vimvars[idx].vv_type = VAR_STRING;
2290 if (val == NULL)
2291 vimvars[idx].vv_str = NULL;
2292 else if (len == -1)
2293 vimvars[idx].vv_str = vim_strsave(val);
2294 else
2295 vimvars[idx].vv_str = vim_strnsave(val, len);
2296}
2297
2298/*
2299 * Set List v: variable to "val".
2300 */
2301 void
2302set_vim_var_list(int idx, list_T *val)
2303{
2304 clear_tv(&vimvars[idx].vv_di.di_tv);
2305 vimvars[idx].vv_type = VAR_LIST;
2306 vimvars[idx].vv_list = val;
2307 if (val != NULL)
2308 ++val->lv_refcount;
2309}
2310
2311/*
2312 * Set Dictionary v: variable to "val".
2313 */
2314 void
2315set_vim_var_dict(int idx, dict_T *val)
2316{
2317 clear_tv(&vimvars[idx].vv_di.di_tv);
2318 vimvars[idx].vv_type = VAR_DICT;
2319 vimvars[idx].vv_dict = val;
2320 if (val != NULL)
2321 {
2322 ++val->dv_refcount;
2323 dict_set_items_ro(val);
2324 }
2325}
2326
2327/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002328 * Set the v:argv list.
2329 */
2330 void
2331set_argv_var(char **argv, int argc)
2332{
2333 list_T *l = list_alloc();
2334 int i;
2335
2336 if (l == NULL)
2337 getout(1);
2338 l->lv_lock = VAR_FIXED;
2339 for (i = 0; i < argc; ++i)
2340 {
2341 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2342 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002343 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002344 }
2345 set_vim_var_list(VV_ARGV, l);
2346}
2347
2348/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002349 * Reset v:register, taking the 'clipboard' setting into account.
2350 */
2351 void
2352reset_reg_var(void)
2353{
2354 int regname = 0;
2355
2356 // Adjust the register according to 'clipboard', so that when
2357 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2358#ifdef FEAT_CLIPBOARD
2359 adjust_clip_reg(&regname);
2360#endif
2361 set_reg_var(regname);
2362}
2363
2364/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002365 * Set v:register if needed.
2366 */
2367 void
2368set_reg_var(int c)
2369{
2370 char_u regname;
2371
2372 if (c == 0 || c == ' ')
2373 regname = '"';
2374 else
2375 regname = c;
2376 // Avoid free/alloc when the value is already right.
2377 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2378 set_vim_var_string(VV_REG, &regname, 1);
2379}
2380
2381/*
2382 * Get or set v:exception. If "oldval" == NULL, return the current value.
2383 * Otherwise, restore the value to "oldval" and return NULL.
2384 * Must always be called in pairs to save and restore v:exception! Does not
2385 * take care of memory allocations.
2386 */
2387 char_u *
2388v_exception(char_u *oldval)
2389{
2390 if (oldval == NULL)
2391 return vimvars[VV_EXCEPTION].vv_str;
2392
2393 vimvars[VV_EXCEPTION].vv_str = oldval;
2394 return NULL;
2395}
2396
2397/*
2398 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2399 * Otherwise, restore the value to "oldval" and return NULL.
2400 * Must always be called in pairs to save and restore v:throwpoint! Does not
2401 * take care of memory allocations.
2402 */
2403 char_u *
2404v_throwpoint(char_u *oldval)
2405{
2406 if (oldval == NULL)
2407 return vimvars[VV_THROWPOINT].vv_str;
2408
2409 vimvars[VV_THROWPOINT].vv_str = oldval;
2410 return NULL;
2411}
2412
2413/*
2414 * Set v:cmdarg.
2415 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2416 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2417 * Must always be called in pairs!
2418 */
2419 char_u *
2420set_cmdarg(exarg_T *eap, char_u *oldarg)
2421{
2422 char_u *oldval;
2423 char_u *newval;
2424 unsigned len;
2425
2426 oldval = vimvars[VV_CMDARG].vv_str;
2427 if (eap == NULL)
2428 {
2429 vim_free(oldval);
2430 vimvars[VV_CMDARG].vv_str = oldarg;
2431 return NULL;
2432 }
2433
2434 if (eap->force_bin == FORCE_BIN)
2435 len = 6;
2436 else if (eap->force_bin == FORCE_NOBIN)
2437 len = 8;
2438 else
2439 len = 0;
2440
2441 if (eap->read_edit)
2442 len += 7;
2443
2444 if (eap->force_ff != 0)
2445 len += 10; // " ++ff=unix"
2446 if (eap->force_enc != 0)
2447 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2448 if (eap->bad_char != 0)
2449 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2450
2451 newval = alloc(len + 1);
2452 if (newval == NULL)
2453 return NULL;
2454
2455 if (eap->force_bin == FORCE_BIN)
2456 sprintf((char *)newval, " ++bin");
2457 else if (eap->force_bin == FORCE_NOBIN)
2458 sprintf((char *)newval, " ++nobin");
2459 else
2460 *newval = NUL;
2461
2462 if (eap->read_edit)
2463 STRCAT(newval, " ++edit");
2464
2465 if (eap->force_ff != 0)
2466 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2467 eap->force_ff == 'u' ? "unix"
2468 : eap->force_ff == 'd' ? "dos"
2469 : "mac");
2470 if (eap->force_enc != 0)
2471 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2472 eap->cmd + eap->force_enc);
2473 if (eap->bad_char == BAD_KEEP)
2474 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2475 else if (eap->bad_char == BAD_DROP)
2476 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2477 else if (eap->bad_char != 0)
2478 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2479 vimvars[VV_CMDARG].vv_str = newval;
2480 return oldval;
2481}
2482
2483/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002484 * Get the value of internal variable "name".
2485 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2486 */
2487 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002488eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002489 char_u *name,
2490 int len, // length of "name"
2491 typval_T *rettv, // NULL when only checking existence
2492 dictitem_T **dip, // non-NULL when typval's dict item is needed
2493 int verbose, // may give error message
2494 int no_autoload) // do not use script autoloading
2495{
2496 int ret = OK;
2497 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002498 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002499 dictitem_T *v;
2500 int cc;
2501
2502 // truncate the name, so that we can use strcmp()
2503 cc = name[len];
2504 name[len] = NUL;
2505
2506 // Check for user-defined variables.
2507 v = find_var(name, NULL, no_autoload);
2508 if (v != NULL)
2509 {
2510 tv = &v->di_tv;
2511 if (dip != NULL)
2512 *dip = v;
2513 }
2514
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002515 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002517 imported_T *import;
2518 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2519
2520 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521
2522 // imported variable from another script
2523 if (import != NULL)
2524 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002525 if (import->imp_funcname != NULL)
2526 {
2527 foundFunc = TRUE;
2528 if (rettv != NULL)
2529 {
2530 rettv->v_type = VAR_FUNC;
2531 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2532 }
2533 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002534 else if (import->imp_all)
2535 {
2536 emsg("Sorry, 'import * as X' not implemented yet");
2537 ret = FAIL;
2538 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002539 else
2540 {
2541 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002542 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002544 tv = sv->sv_tv;
2545 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002547 else if (in_vim9script())
2548 {
2549 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2550
2551 if (ufunc != NULL)
2552 {
2553 foundFunc = TRUE;
2554 if (rettv != NULL)
2555 {
2556 rettv->v_type = VAR_FUNC;
2557 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2558 }
2559 }
2560 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
2562
Bram Moolenaarc620c052020-07-08 15:16:19 +02002563 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002564 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002565 if (tv == NULL)
2566 {
2567 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002568 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002569 ret = FAIL;
2570 }
2571 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002572 {
2573 // If a list or dict variable wasn't initialized, do it now.
2574 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2575 {
2576 tv->vval.v_dict = dict_alloc();
2577 if (tv->vval.v_dict != NULL)
2578 ++tv->vval.v_dict->dv_refcount;
2579 }
2580 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2581 {
2582 tv->vval.v_list = list_alloc();
2583 if (tv->vval.v_list != NULL)
2584 ++tv->vval.v_list->lv_refcount;
2585 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002586 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002587 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002588 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002589
2590 name[len] = cc;
2591
2592 return ret;
2593}
2594
2595/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002596 * Check if variable "name[len]" is a local variable or an argument.
2597 * If so, "*eval_lavars_used" is set to TRUE.
2598 */
2599 void
2600check_vars(char_u *name, int len)
2601{
2602 int cc;
2603 char_u *varname;
2604 hashtab_T *ht;
2605
2606 if (eval_lavars_used == NULL)
2607 return;
2608
2609 // truncate the name, so that we can use strcmp()
2610 cc = name[len];
2611 name[len] = NUL;
2612
2613 ht = find_var_ht(name, &varname);
2614 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2615 {
2616 if (find_var(name, NULL, TRUE) != NULL)
2617 *eval_lavars_used = TRUE;
2618 }
2619
2620 name[len] = cc;
2621}
2622
2623/*
2624 * Find variable "name" in the list of variables.
2625 * Return a pointer to it if found, NULL if not found.
2626 * Careful: "a:0" variables don't have a name.
2627 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2628 * hashtab_T used.
2629 */
2630 dictitem_T *
2631find_var(char_u *name, hashtab_T **htp, int no_autoload)
2632{
2633 char_u *varname;
2634 hashtab_T *ht;
2635 dictitem_T *ret = NULL;
2636
2637 ht = find_var_ht(name, &varname);
2638 if (htp != NULL)
2639 *htp = ht;
2640 if (ht == NULL)
2641 return NULL;
2642 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2643 if (ret != NULL)
2644 return ret;
2645
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002646 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002647 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2648 if (ret != NULL)
2649 return ret;
2650
2651 // in Vim9 script items without a scope can be script-local
2652 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2653 {
2654 ht = get_script_local_ht();
2655 if (ht != NULL)
2656 {
2657 ret = find_var_in_ht(ht, *name, varname,
2658 no_autoload || htp != NULL);
2659 if (ret != NULL)
2660 {
2661 if (htp != NULL)
2662 *htp = ht;
2663 return ret;
2664 }
2665 }
2666 }
2667
2668 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002669}
2670
2671/*
2672 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002673 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002674 * Returns NULL if not found.
2675 */
2676 dictitem_T *
2677find_var_in_ht(
2678 hashtab_T *ht,
2679 int htname,
2680 char_u *varname,
2681 int no_autoload)
2682{
2683 hashitem_T *hi;
2684
2685 if (*varname == NUL)
2686 {
2687 // Must be something like "s:", otherwise "ht" would be NULL.
2688 switch (htname)
2689 {
2690 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2691 case 'g': return &globvars_var;
2692 case 'v': return &vimvars_var;
2693 case 'b': return &curbuf->b_bufvar;
2694 case 'w': return &curwin->w_winvar;
2695 case 't': return &curtab->tp_winvar;
2696 case 'l': return get_funccal_local_var();
2697 case 'a': return get_funccal_args_var();
2698 }
2699 return NULL;
2700 }
2701
2702 hi = hash_find(ht, varname);
2703 if (HASHITEM_EMPTY(hi))
2704 {
2705 // For global variables we may try auto-loading the script. If it
2706 // worked find the variable again. Don't auto-load a script if it was
2707 // loaded already, otherwise it would be loaded every time when
2708 // checking if a function name is a Funcref variable.
2709 if (ht == &globvarht && !no_autoload)
2710 {
2711 // Note: script_autoload() may make "hi" invalid. It must either
2712 // be obtained again or not used.
2713 if (!script_autoload(varname, FALSE) || aborting())
2714 return NULL;
2715 hi = hash_find(ht, varname);
2716 }
2717 if (HASHITEM_EMPTY(hi))
2718 return NULL;
2719 }
2720 return HI2DI(hi);
2721}
2722
2723/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 * Get the script-local hashtab. NULL if not in a script context.
2725 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002726 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727get_script_local_ht(void)
2728{
2729 scid_T sid = current_sctx.sc_sid;
2730
Bram Moolenaare3d46852020-08-29 13:39:17 +02002731 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002732 return &SCRIPT_VARS(sid);
2733 return NULL;
2734}
2735
2736/*
2737 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002738 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002740 int
2741lookup_scriptvar(
2742 char_u *name,
2743 size_t len,
2744 void *lvar UNUSED,
2745 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746{
2747 hashtab_T *ht = get_script_local_ht();
2748 char_u buffer[30];
2749 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002750 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 hashitem_T *hi;
2752
2753 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002754 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 if (len < sizeof(buffer) - 1)
2756 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002757 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 vim_strncpy(buffer, name, len);
2759 p = buffer;
2760 }
2761 else
2762 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002763 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002765 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002766 }
2767
2768 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002769 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770
2771 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002772 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2773 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774
2775 if (p != buffer)
2776 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002777 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778}
2779
2780/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002781 * Find the hashtab used for a variable name.
2782 * Return NULL if the name is not valid.
2783 * Set "varname" to the start of name without ':'.
2784 */
2785 hashtab_T *
2786find_var_ht(char_u *name, char_u **varname)
2787{
2788 hashitem_T *hi;
2789 hashtab_T *ht;
2790
2791 if (name[0] == NUL)
2792 return NULL;
2793 if (name[1] != ':')
2794 {
2795 // The name must not start with a colon or #.
2796 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2797 return NULL;
2798 *varname = name;
2799
2800 // "version" is "v:version" in all scopes if scriptversion < 3.
2801 // Same for a few other variables marked with VV_COMPAT.
2802 if (current_sctx.sc_version < 3)
2803 {
2804 hi = hash_find(&compat_hashtab, name);
2805 if (!HASHITEM_EMPTY(hi))
2806 return &compat_hashtab;
2807 }
2808
2809 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 if (ht != NULL)
2811 return ht; // local variable
2812
2813 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002814 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 {
2816 ht = get_script_local_ht();
2817 if (ht != NULL)
2818 return ht;
2819 }
2820
2821 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002822 }
2823 *varname = name + 2;
2824 if (*name == 'g') // global variable
2825 return &globvarht;
2826 // There must be no ':' or '#' in the rest of the name, unless g: is used
2827 if (vim_strchr(name + 2, ':') != NULL
2828 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2829 return NULL;
2830 if (*name == 'b') // buffer variable
2831 return &curbuf->b_vars->dv_hashtab;
2832 if (*name == 'w') // window variable
2833 return &curwin->w_vars->dv_hashtab;
2834 if (*name == 't') // tab page variable
2835 return &curtab->tp_vars->dv_hashtab;
2836 if (*name == 'v') // v: variable
2837 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002838 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002839 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002841 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 if (*name == 'a') // a: function argument
2843 return get_funccal_args_ht();
2844 if (*name == 'l') // l: local function variable
2845 return get_funccal_local_ht();
2846 }
2847 if (*name == 's') // script variable
2848 {
2849 ht = get_script_local_ht();
2850 if (ht != NULL)
2851 return ht;
2852 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002853 return NULL;
2854}
2855
2856/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002857 * Get the string value of a (global/local) variable.
2858 * Note: see tv_get_string() for how long the pointer remains valid.
2859 * Returns NULL when it doesn't exist.
2860 */
2861 char_u *
2862get_var_value(char_u *name)
2863{
2864 dictitem_T *v;
2865
2866 v = find_var(name, NULL, FALSE);
2867 if (v == NULL)
2868 return NULL;
2869 return tv_get_string(&v->di_tv);
2870}
2871
2872/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002873 * Allocate a new hashtab for a sourced script. It will be used while
2874 * sourcing this script and when executing functions defined in the script.
2875 */
2876 void
2877new_script_vars(scid_T id)
2878{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002879 scriptvar_T *sv;
2880
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002881 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2882 if (sv == NULL)
2883 return;
2884 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002885 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002886}
2887
2888/*
2889 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2890 * point to it.
2891 */
2892 void
2893init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2894{
2895 hash_init(&dict->dv_hashtab);
2896 dict->dv_lock = 0;
2897 dict->dv_scope = scope;
2898 dict->dv_refcount = DO_NOT_FREE_CNT;
2899 dict->dv_copyID = 0;
2900 dict_var->di_tv.vval.v_dict = dict;
2901 dict_var->di_tv.v_type = VAR_DICT;
2902 dict_var->di_tv.v_lock = VAR_FIXED;
2903 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2904 dict_var->di_key[0] = NUL;
2905}
2906
2907/*
2908 * Unreference a dictionary initialized by init_var_dict().
2909 */
2910 void
2911unref_var_dict(dict_T *dict)
2912{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002913 // Now the dict needs to be freed if no one else is using it, go back to
2914 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002915 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2916 dict_unref(dict);
2917}
2918
2919/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002920 * Clean up a list of internal variables.
2921 * Frees all allocated variables and the value they contain.
2922 * Clears hashtab "ht", does not free it.
2923 */
2924 void
2925vars_clear(hashtab_T *ht)
2926{
2927 vars_clear_ext(ht, TRUE);
2928}
2929
2930/*
2931 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2932 */
2933 void
2934vars_clear_ext(hashtab_T *ht, int free_val)
2935{
2936 int todo;
2937 hashitem_T *hi;
2938 dictitem_T *v;
2939
2940 hash_lock(ht);
2941 todo = (int)ht->ht_used;
2942 for (hi = ht->ht_array; todo > 0; ++hi)
2943 {
2944 if (!HASHITEM_EMPTY(hi))
2945 {
2946 --todo;
2947
2948 // Free the variable. Don't remove it from the hashtab,
2949 // ht_array might change then. hash_clear() takes care of it
2950 // later.
2951 v = HI2DI(hi);
2952 if (free_val)
2953 clear_tv(&v->di_tv);
2954 if (v->di_flags & DI_FLAGS_ALLOC)
2955 vim_free(v);
2956 }
2957 }
2958 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002959 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002960}
2961
2962/*
2963 * Delete a variable from hashtab "ht" at item "hi".
2964 * Clear the variable value and free the dictitem.
2965 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002966 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002967delete_var(hashtab_T *ht, hashitem_T *hi)
2968{
2969 dictitem_T *di = HI2DI(hi);
2970
2971 hash_remove(ht, hi);
2972 clear_tv(&di->di_tv);
2973 vim_free(di);
2974}
2975
2976/*
2977 * List the value of one internal variable.
2978 */
2979 static void
2980list_one_var(dictitem_T *v, char *prefix, int *first)
2981{
2982 char_u *tofree;
2983 char_u *s;
2984 char_u numbuf[NUMBUFLEN];
2985
2986 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2987 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2988 s == NULL ? (char_u *)"" : s, first);
2989 vim_free(tofree);
2990}
2991
2992 static void
2993list_one_var_a(
2994 char *prefix,
2995 char_u *name,
2996 int type,
2997 char_u *string,
2998 int *first) // when TRUE clear rest of screen and set to FALSE
2999{
3000 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3001 msg_start();
3002 msg_puts(prefix);
3003 if (name != NULL) // "a:" vars don't have a name stored
3004 msg_puts((char *)name);
3005 msg_putchar(' ');
3006 msg_advance(22);
3007 if (type == VAR_NUMBER)
3008 msg_putchar('#');
3009 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3010 msg_putchar('*');
3011 else if (type == VAR_LIST)
3012 {
3013 msg_putchar('[');
3014 if (*string == '[')
3015 ++string;
3016 }
3017 else if (type == VAR_DICT)
3018 {
3019 msg_putchar('{');
3020 if (*string == '{')
3021 ++string;
3022 }
3023 else
3024 msg_putchar(' ');
3025
3026 msg_outtrans(string);
3027
3028 if (type == VAR_FUNC || type == VAR_PARTIAL)
3029 msg_puts("()");
3030 if (*first)
3031 {
3032 msg_clr_eos();
3033 *first = FALSE;
3034 }
3035}
3036
3037/*
3038 * Set variable "name" to value in "tv".
3039 * If the variable already exists, the value is updated.
3040 * Otherwise the variable is created.
3041 */
3042 void
3043set_var(
3044 char_u *name,
3045 typval_T *tv,
3046 int copy) // make copy of value in "tv"
3047{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003048 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003049}
3050
3051/*
3052 * Set variable "name" to value in "tv".
3053 * If the variable already exists and "is_const" is FALSE the value is updated.
3054 * Otherwise the variable is created.
3055 */
3056 void
3057set_var_const(
3058 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003060 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003061 int copy, // make copy of value in "tv"
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003062 int flags) // ASSIGN_CONST, ASSIGN_FINAL, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003063{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003064 typval_T *tv = tv_arg;
3065 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003067 char_u *varname;
3068 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003070 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003071
3072 ht = find_var_ht(name, &varname);
3073 if (ht == NULL || *varname == NUL)
3074 {
3075 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003076 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003078 is_script_local = ht == get_script_local_ht();
3079
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003080 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003081 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003082 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003083 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003084 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003085 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003086 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003087 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003088 }
3089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003091
3092 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 if (di == NULL)
3094 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095
3096 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003097 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003098 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003100 if (need_convert_to_bool(type, tv))
3101 {
3102 // Destination is a bool and the value is not, but it can be converted.
3103 CLEAR_FIELD(bool_tv);
3104 bool_tv.v_type = VAR_BOOL;
3105 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3106 tv = &bool_tv;
3107 }
3108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003110 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003112 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003113 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 {
3115 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003116 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 }
3118
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003119 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003121 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003122 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003123 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003124 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003125 }
3126
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003127 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003128 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003129 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003131
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003132 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003133 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003134 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003136 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 // can only redefine once
3138 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003139
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003140 // A Vim9 script-local variable is also present in sn_all_vars and
3141 // sn_var_vals.
3142 if (is_script_local && vim9script)
3143 update_vim9_script_var(FALSE, di, tv, type);
3144 }
3145
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003146 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003149 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003150 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003151 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003153 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003155 if (copy || tv->v_type != VAR_STRING)
3156 {
3157 char_u *val = tv_get_string(tv);
3158
3159 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003160 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 if (di->di_tv.vval.v_string == NULL)
3162 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003163 }
3164 else
3165 {
3166 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003168 tv->vval.v_string = NULL;
3169 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003170 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003173 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003175 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003177#ifdef FEAT_SEARCH_EXTRA
3178 else if (STRCMP(varname, "hlsearch") == 0)
3179 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003181 redraw_all_later(SOME_VALID);
3182 }
3183#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003184 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003185 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003186 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003187 {
3188 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003189 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003190 }
3191 }
3192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194 }
3195 else // add a new variable
3196 {
3197 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003198 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003199 {
3200 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003201 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003202 }
3203
Bram Moolenaar03290b82020-12-19 16:30:44 +01003204 // Make sure the variable name is valid. In Vim9 script an autoload
3205 // variable must be prefixed with "g:".
3206 if (!valid_varname(varname, !vim9script
3207 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003208 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3211 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003212 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 STRCPY(di->di_key, varname);
3214 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003215 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003217 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003218 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003220 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003221 di->di_flags |= DI_FLAGS_LOCK;
3222
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003223 // A Vim9 script-local variable is also added to sn_all_vars and
3224 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003225 if (is_script_local && vim9script)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003226 update_vim9_script_var(TRUE, di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227 }
3228
3229 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003230 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003231 else
3232 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 di->di_tv = *tv;
3234 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003235 init_tv(tv);
3236 }
3237
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003238 // ":const var = value" locks the value
3239 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003240 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003241 // Like :lockvar! name: lock the value and what it contains, but only
3242 // if the reference count is up to one. That locks only literal
3243 // values.
3244 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003245 return;
3246failed:
3247 if (!copy)
3248 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003249}
3250
3251/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003252 * Check in this order for backwards compatibility:
3253 * - Whether the variable is read-only
3254 * - Whether the variable value is locked
3255 * - Whether the variable is locked
3256 */
3257 int
3258var_check_permission(dictitem_T *di, char_u *name)
3259{
3260 if (var_check_ro(di->di_flags, name, FALSE)
3261 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3262 || var_check_lock(di->di_flags, name, FALSE))
3263 return FAIL;
3264 return OK;
3265}
3266
3267/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003268 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3269 * Also give an error message.
3270 */
3271 int
3272var_check_ro(int flags, char_u *name, int use_gettext)
3273{
3274 if (flags & DI_FLAGS_RO)
3275 {
3276 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3277 return TRUE;
3278 }
3279 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3280 {
3281 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3282 return TRUE;
3283 }
3284 return FALSE;
3285}
3286
3287/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003288 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3289 * Also give an error message.
3290 */
3291 int
3292var_check_lock(int flags, char_u *name, int use_gettext)
3293{
3294 if (flags & DI_FLAGS_LOCK)
3295 {
3296 semsg(_(e_variable_is_locked_str),
3297 use_gettext ? (char_u *)_(name) : name);
3298 return TRUE;
3299 }
3300 return FALSE;
3301}
3302
3303/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003304 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3305 * Also give an error message.
3306 */
3307 int
3308var_check_fixed(int flags, char_u *name, int use_gettext)
3309{
3310 if (flags & DI_FLAGS_FIX)
3311 {
3312 semsg(_("E795: Cannot delete variable %s"),
3313 use_gettext ? (char_u *)_(name) : name);
3314 return TRUE;
3315 }
3316 return FALSE;
3317}
3318
3319/*
3320 * Check if a funcref is assigned to a valid variable name.
3321 * Return TRUE and give an error if not.
3322 */
3323 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003324var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003325 char_u *name, // points to start of variable name
3326 int new_var) // TRUE when creating the variable
3327{
3328 // Allow for w: b: s: and t:.
3329 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3330 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3331 ? name[2] : name[0]))
3332 {
3333 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3334 name);
3335 return TRUE;
3336 }
3337 // Don't allow hiding a function. When "v" is not NULL we might be
3338 // assigning another function to the same var, the type is checked
3339 // below.
3340 if (new_var && function_exists(name, FALSE))
3341 {
3342 semsg(_("E705: Variable name conflicts with existing function: %s"),
3343 name);
3344 return TRUE;
3345 }
3346 return FALSE;
3347}
3348
3349/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003350 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3351 * value. Also give an error message, using "name" or _("name") when
3352 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003353 */
3354 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003355value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003356{
3357 if (lock & VAR_LOCKED)
3358 {
3359 semsg(_("E741: Value is locked: %s"),
3360 name == NULL ? (char_u *)_("Unknown")
3361 : use_gettext ? (char_u *)_(name)
3362 : name);
3363 return TRUE;
3364 }
3365 if (lock & VAR_FIXED)
3366 {
3367 semsg(_("E742: Cannot change value of %s"),
3368 name == NULL ? (char_u *)_("Unknown")
3369 : use_gettext ? (char_u *)_(name)
3370 : name);
3371 return TRUE;
3372 }
3373 return FALSE;
3374}
3375
3376/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003377 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003378 * Return FALSE and give an error if not.
3379 */
3380 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003381valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003382{
3383 char_u *p;
3384
3385 for (p = varname; *p != NUL; ++p)
3386 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003387 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003388 {
3389 semsg(_(e_illvar), varname);
3390 return FALSE;
3391 }
3392 return TRUE;
3393}
3394
3395/*
3396 * getwinvar() and gettabwinvar()
3397 */
3398 static void
3399getwinvar(
3400 typval_T *argvars,
3401 typval_T *rettv,
3402 int off) // 1 for gettabwinvar()
3403{
3404 win_T *win;
3405 char_u *varname;
3406 dictitem_T *v;
3407 tabpage_T *tp = NULL;
3408 int done = FALSE;
3409 win_T *oldcurwin;
3410 tabpage_T *oldtabpage;
3411 int need_switch_win;
3412
3413 if (off == 1)
3414 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3415 else
3416 tp = curtab;
3417 win = find_win_by_nr(&argvars[off], tp);
3418 varname = tv_get_string_chk(&argvars[off + 1]);
3419 ++emsg_off;
3420
3421 rettv->v_type = VAR_STRING;
3422 rettv->vval.v_string = NULL;
3423
3424 if (win != NULL && varname != NULL)
3425 {
3426 // Set curwin to be our win, temporarily. Also set the tabpage,
3427 // otherwise the window is not valid. Only do this when needed,
3428 // autocommands get blocked.
3429 need_switch_win = !(tp == curtab && win == curwin);
3430 if (!need_switch_win
3431 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3432 {
3433 if (*varname == '&')
3434 {
3435 if (varname[1] == NUL)
3436 {
3437 // get all window-local options in a dict
3438 dict_T *opts = get_winbuf_options(FALSE);
3439
3440 if (opts != NULL)
3441 {
3442 rettv_dict_set(rettv, opts);
3443 done = TRUE;
3444 }
3445 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003446 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003447 // window-local-option
3448 done = TRUE;
3449 }
3450 else
3451 {
3452 // Look up the variable.
3453 // Let getwinvar({nr}, "") return the "w:" dictionary.
3454 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3455 varname, FALSE);
3456 if (v != NULL)
3457 {
3458 copy_tv(&v->di_tv, rettv);
3459 done = TRUE;
3460 }
3461 }
3462 }
3463
3464 if (need_switch_win)
3465 // restore previous notion of curwin
3466 restore_win(oldcurwin, oldtabpage, TRUE);
3467 }
3468
3469 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3470 // use the default return value
3471 copy_tv(&argvars[off + 2], rettv);
3472
3473 --emsg_off;
3474}
3475
3476/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003477 * Set option "varname" to the value of "varp" for the current buffer/window.
3478 */
3479 static void
3480set_option_from_tv(char_u *varname, typval_T *varp)
3481{
3482 long numval = 0;
3483 char_u *strval;
3484 char_u nbuf[NUMBUFLEN];
3485 int error = FALSE;
3486
3487 if (!in_vim9script() || varp->v_type != VAR_STRING)
3488 numval = (long)tv_get_number_chk(varp, &error);
3489 strval = tv_get_string_buf_chk(varp, nbuf);
3490 if (!error && strval != NULL)
3491 set_option_value(varname, numval, strval, OPT_LOCAL);
3492}
3493
3494/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003495 * "setwinvar()" and "settabwinvar()" functions
3496 */
3497 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003498setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003499{
3500 win_T *win;
3501 win_T *save_curwin;
3502 tabpage_T *save_curtab;
3503 int need_switch_win;
3504 char_u *varname, *winvarname;
3505 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003506 tabpage_T *tp = NULL;
3507
3508 if (check_secure())
3509 return;
3510
3511 if (off == 1)
3512 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3513 else
3514 tp = curtab;
3515 win = find_win_by_nr(&argvars[off], tp);
3516 varname = tv_get_string_chk(&argvars[off + 1]);
3517 varp = &argvars[off + 2];
3518
3519 if (win != NULL && varname != NULL && varp != NULL)
3520 {
3521 need_switch_win = !(tp == curtab && win == curwin);
3522 if (!need_switch_win
3523 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3524 {
3525 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003526 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003527 else
3528 {
3529 winvarname = alloc(STRLEN(varname) + 3);
3530 if (winvarname != NULL)
3531 {
3532 STRCPY(winvarname, "w:");
3533 STRCPY(winvarname + 2, varname);
3534 set_var(winvarname, varp, TRUE);
3535 vim_free(winvarname);
3536 }
3537 }
3538 }
3539 if (need_switch_win)
3540 restore_win(save_curwin, save_curtab, TRUE);
3541 }
3542}
3543
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003544/*
3545 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3546 * v:option_type, and v:option_command.
3547 */
3548 void
3549reset_v_option_vars(void)
3550{
3551 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3552 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3553 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3554 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3555 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3556 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3557}
3558
3559/*
3560 * Add an assert error to v:errors.
3561 */
3562 void
3563assert_error(garray_T *gap)
3564{
3565 struct vimvar *vp = &vimvars[VV_ERRORS];
3566
3567 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003568 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003569 set_vim_var_list(VV_ERRORS, list_alloc());
3570 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3571}
3572
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003573 int
3574var_exists(char_u *var)
3575{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003576 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003577 char_u *name;
3578 char_u *tofree;
3579 typval_T tv;
3580 int len = 0;
3581 int n = FALSE;
3582
3583 // get_name_len() takes care of expanding curly braces
3584 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003585 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003586 if (len > 0)
3587 {
3588 if (tofree != NULL)
3589 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003590 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003591 if (n)
3592 {
3593 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003594 arg = skipwhite(arg);
3595 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003596 if (n)
3597 clear_tv(&tv);
3598 }
3599 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003600 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003601 n = FALSE;
3602
3603 vim_free(tofree);
3604 return n;
3605}
3606
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003607static lval_T *redir_lval = NULL;
3608#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3609static garray_T redir_ga; // only valid when redir_lval is not NULL
3610static char_u *redir_endp = NULL;
3611static char_u *redir_varname = NULL;
3612
3613/*
3614 * Start recording command output to a variable
3615 * When "append" is TRUE append to an existing variable.
3616 * Returns OK if successfully completed the setup. FAIL otherwise.
3617 */
3618 int
3619var_redir_start(char_u *name, int append)
3620{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003621 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003622 typval_T tv;
3623
3624 // Catch a bad name early.
3625 if (!eval_isnamec1(*name))
3626 {
3627 emsg(_(e_invarg));
3628 return FAIL;
3629 }
3630
3631 // Make a copy of the name, it is used in redir_lval until redir ends.
3632 redir_varname = vim_strsave(name);
3633 if (redir_varname == NULL)
3634 return FAIL;
3635
3636 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3637 if (redir_lval == NULL)
3638 {
3639 var_redir_stop();
3640 return FAIL;
3641 }
3642
3643 // The output is stored in growarray "redir_ga" until redirection ends.
3644 ga_init2(&redir_ga, (int)sizeof(char), 500);
3645
3646 // Parse the variable name (can be a dict or list entry).
3647 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3648 FNE_CHECK_START);
3649 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3650 {
3651 clear_lval(redir_lval);
3652 if (redir_endp != NULL && *redir_endp != NUL)
3653 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003654 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003655 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003656 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003657 redir_endp = NULL; // don't store a value, only cleanup
3658 var_redir_stop();
3659 return FAIL;
3660 }
3661
3662 // check if we can write to the variable: set it to or append an empty
3663 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003664 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003665 tv.v_type = VAR_STRING;
3666 tv.vval.v_string = (char_u *)"";
3667 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003668 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3669 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003670 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003671 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3672 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003673 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003674 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003675 {
3676 redir_endp = NULL; // don't store a value, only cleanup
3677 var_redir_stop();
3678 return FAIL;
3679 }
3680
3681 return OK;
3682}
3683
3684/*
3685 * Append "value[value_len]" to the variable set by var_redir_start().
3686 * The actual appending is postponed until redirection ends, because the value
3687 * appended may in fact be the string we write to, changing it may cause freed
3688 * memory to be used:
3689 * :redir => foo
3690 * :let foo
3691 * :redir END
3692 */
3693 void
3694var_redir_str(char_u *value, int value_len)
3695{
3696 int len;
3697
3698 if (redir_lval == NULL)
3699 return;
3700
3701 if (value_len == -1)
3702 len = (int)STRLEN(value); // Append the entire string
3703 else
3704 len = value_len; // Append only "value_len" characters
3705
3706 if (ga_grow(&redir_ga, len) == OK)
3707 {
3708 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3709 redir_ga.ga_len += len;
3710 }
3711 else
3712 var_redir_stop();
3713}
3714
3715/*
3716 * Stop redirecting command output to a variable.
3717 * Frees the allocated memory.
3718 */
3719 void
3720var_redir_stop(void)
3721{
3722 typval_T tv;
3723
3724 if (EVALCMD_BUSY)
3725 {
3726 redir_lval = NULL;
3727 return;
3728 }
3729
3730 if (redir_lval != NULL)
3731 {
3732 // If there was no error: assign the text to the variable.
3733 if (redir_endp != NULL)
3734 {
3735 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3736 tv.v_type = VAR_STRING;
3737 tv.vval.v_string = redir_ga.ga_data;
3738 // Call get_lval() again, if it's inside a Dict or List it may
3739 // have changed.
3740 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3741 FALSE, FALSE, 0, FNE_CHECK_START);
3742 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003744 (char_u *)".");
3745 clear_lval(redir_lval);
3746 }
3747
3748 // free the collected output
3749 VIM_CLEAR(redir_ga.ga_data);
3750
3751 VIM_CLEAR(redir_lval);
3752 }
3753 VIM_CLEAR(redir_varname);
3754}
3755
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003756/*
3757 * "gettabvar()" function
3758 */
3759 void
3760f_gettabvar(typval_T *argvars, typval_T *rettv)
3761{
3762 win_T *oldcurwin;
3763 tabpage_T *tp, *oldtabpage;
3764 dictitem_T *v;
3765 char_u *varname;
3766 int done = FALSE;
3767
3768 rettv->v_type = VAR_STRING;
3769 rettv->vval.v_string = NULL;
3770
3771 varname = tv_get_string_chk(&argvars[1]);
3772 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3773 if (tp != NULL && varname != NULL)
3774 {
3775 // Set tp to be our tabpage, temporarily. Also set the window to the
3776 // first window in the tabpage, otherwise the window is not valid.
3777 if (switch_win(&oldcurwin, &oldtabpage,
3778 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3779 : tp->tp_firstwin, tp, TRUE) == OK)
3780 {
3781 // look up the variable
3782 // Let gettabvar({nr}, "") return the "t:" dictionary.
3783 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3784 if (v != NULL)
3785 {
3786 copy_tv(&v->di_tv, rettv);
3787 done = TRUE;
3788 }
3789 }
3790
3791 // restore previous notion of curwin
3792 restore_win(oldcurwin, oldtabpage, TRUE);
3793 }
3794
3795 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3796 copy_tv(&argvars[2], rettv);
3797}
3798
3799/*
3800 * "gettabwinvar()" function
3801 */
3802 void
3803f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3804{
3805 getwinvar(argvars, rettv, 1);
3806}
3807
3808/*
3809 * "getwinvar()" function
3810 */
3811 void
3812f_getwinvar(typval_T *argvars, typval_T *rettv)
3813{
3814 getwinvar(argvars, rettv, 0);
3815}
3816
3817/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003818 * "getbufvar()" function
3819 */
3820 void
3821f_getbufvar(typval_T *argvars, typval_T *rettv)
3822{
3823 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003824 char_u *varname;
3825 dictitem_T *v;
3826 int done = FALSE;
3827
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003828 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003829 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003830
3831 rettv->v_type = VAR_STRING;
3832 rettv->vval.v_string = NULL;
3833
3834 if (buf != NULL && varname != NULL)
3835 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003836 if (*varname == '&')
3837 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003838 buf_T *save_curbuf = curbuf;
3839
3840 // set curbuf to be our buf, temporarily
3841 curbuf = buf;
3842
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003843 if (varname[1] == NUL)
3844 {
3845 // get all buffer-local options in a dict
3846 dict_T *opts = get_winbuf_options(TRUE);
3847
3848 if (opts != NULL)
3849 {
3850 rettv_dict_set(rettv, opts);
3851 done = TRUE;
3852 }
3853 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003854 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003855 // buffer-local-option
3856 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003857
3858 // restore previous notion of curbuf
3859 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003860 }
3861 else
3862 {
3863 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003864 if (*varname == NUL)
3865 // Let getbufvar({nr}, "") return the "b:" dictionary.
3866 v = &buf->b_bufvar;
3867 else
3868 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3869 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003870 if (v != NULL)
3871 {
3872 copy_tv(&v->di_tv, rettv);
3873 done = TRUE;
3874 }
3875 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003876 }
3877
3878 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3879 // use the default value
3880 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003881}
3882
3883/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003884 * "settabvar()" function
3885 */
3886 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003887f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003888{
3889 tabpage_T *save_curtab;
3890 tabpage_T *tp;
3891 char_u *varname, *tabvarname;
3892 typval_T *varp;
3893
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003894 if (check_secure())
3895 return;
3896
3897 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3898 varname = tv_get_string_chk(&argvars[1]);
3899 varp = &argvars[2];
3900
3901 if (varname != NULL && varp != NULL && tp != NULL)
3902 {
3903 save_curtab = curtab;
3904 goto_tabpage_tp(tp, FALSE, FALSE);
3905
3906 tabvarname = alloc(STRLEN(varname) + 3);
3907 if (tabvarname != NULL)
3908 {
3909 STRCPY(tabvarname, "t:");
3910 STRCPY(tabvarname + 2, varname);
3911 set_var(tabvarname, varp, TRUE);
3912 vim_free(tabvarname);
3913 }
3914
3915 // Restore current tabpage
3916 if (valid_tabpage(save_curtab))
3917 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3918 }
3919}
3920
3921/*
3922 * "settabwinvar()" function
3923 */
3924 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003925f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003926{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003927 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003928}
3929
3930/*
3931 * "setwinvar()" function
3932 */
3933 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003934f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003935{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003936 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003937}
3938
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003939/*
3940 * "setbufvar()" function
3941 */
3942 void
3943f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3944{
3945 buf_T *buf;
3946 char_u *varname, *bufvarname;
3947 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003948
3949 if (check_secure())
3950 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003951 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003952 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003953 varp = &argvars[2];
3954
3955 if (buf != NULL && varname != NULL && varp != NULL)
3956 {
3957 if (*varname == '&')
3958 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003959 aco_save_T aco;
3960
3961 // set curbuf to be our buf, temporarily
3962 aucmd_prepbuf(&aco, buf);
3963
Bram Moolenaar191929b2020-08-19 21:20:49 +02003964 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003965
3966 // reset notion of buffer
3967 aucmd_restbuf(&aco);
3968 }
3969 else
3970 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003971 bufvarname = alloc(STRLEN(varname) + 3);
3972 if (bufvarname != NULL)
3973 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003974 buf_T *save_curbuf = curbuf;
3975
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003976 curbuf = buf;
3977 STRCPY(bufvarname, "b:");
3978 STRCPY(bufvarname + 2, varname);
3979 set_var(bufvarname, varp, TRUE);
3980 vim_free(bufvarname);
3981 curbuf = save_curbuf;
3982 }
3983 }
3984 }
3985}
3986
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003987/*
3988 * Get a callback from "arg". It can be a Funcref or a function name.
3989 * When "arg" is zero return an empty string.
3990 * "cb_name" is not allocated.
3991 * "cb_name" is set to NULL for an invalid argument.
3992 */
3993 callback_T
3994get_callback(typval_T *arg)
3995{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003996 callback_T res;
3997 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003998
3999 res.cb_free_name = FALSE;
4000 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4001 {
4002 res.cb_partial = arg->vval.v_partial;
4003 ++res.cb_partial->pt_refcount;
4004 res.cb_name = partial_name(res.cb_partial);
4005 }
4006 else
4007 {
4008 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004009 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4010 && isdigit(*arg->vval.v_string))
4011 r = FAIL;
4012 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004013 {
4014 // Note that we don't make a copy of the string.
4015 res.cb_name = arg->vval.v_string;
4016 func_ref(res.cb_name);
4017 }
4018 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004019 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004020 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004021 r = FAIL;
4022
4023 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004024 {
4025 emsg(_("E921: Invalid callback argument"));
4026 res.cb_name = NULL;
4027 }
4028 }
4029 return res;
4030}
4031
4032/*
4033 * Copy a callback into a typval_T.
4034 */
4035 void
4036put_callback(callback_T *cb, typval_T *tv)
4037{
4038 if (cb->cb_partial != NULL)
4039 {
4040 tv->v_type = VAR_PARTIAL;
4041 tv->vval.v_partial = cb->cb_partial;
4042 ++tv->vval.v_partial->pt_refcount;
4043 }
4044 else
4045 {
4046 tv->v_type = VAR_FUNC;
4047 tv->vval.v_string = vim_strsave(cb->cb_name);
4048 func_ref(cb->cb_name);
4049 }
4050}
4051
4052/*
4053 * Make a copy of "src" into "dest", allocating the function name if needed,
4054 * without incrementing the refcount.
4055 */
4056 void
4057set_callback(callback_T *dest, callback_T *src)
4058{
4059 if (src->cb_partial == NULL)
4060 {
4061 // just a function name, make a copy
4062 dest->cb_name = vim_strsave(src->cb_name);
4063 dest->cb_free_name = TRUE;
4064 }
4065 else
4066 {
4067 // cb_name is a pointer into cb_partial
4068 dest->cb_name = src->cb_name;
4069 dest->cb_free_name = FALSE;
4070 }
4071 dest->cb_partial = src->cb_partial;
4072}
4073
4074/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004075 * Copy callback from "src" to "dest", incrementing the refcounts.
4076 */
4077 void
4078copy_callback(callback_T *dest, callback_T *src)
4079{
4080 dest->cb_partial = src->cb_partial;
4081 if (dest->cb_partial != NULL)
4082 {
4083 dest->cb_name = src->cb_name;
4084 dest->cb_free_name = FALSE;
4085 ++dest->cb_partial->pt_refcount;
4086 }
4087 else
4088 {
4089 dest->cb_name = vim_strsave(src->cb_name);
4090 dest->cb_free_name = TRUE;
4091 func_ref(src->cb_name);
4092 }
4093}
4094
4095/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004096 * Unref/free "callback" returned by get_callback() or set_callback().
4097 */
4098 void
4099free_callback(callback_T *callback)
4100{
4101 if (callback->cb_partial != NULL)
4102 {
4103 partial_unref(callback->cb_partial);
4104 callback->cb_partial = NULL;
4105 }
4106 else if (callback->cb_name != NULL)
4107 func_unref(callback->cb_name);
4108 if (callback->cb_free_name)
4109 {
4110 vim_free(callback->cb_name);
4111 callback->cb_free_name = FALSE;
4112 }
4113 callback->cb_name = NULL;
4114}
4115
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004116#endif // FEAT_EVAL