blob: ec3f508ed3fcc51843ed9d1da511913770cccf64 [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 Moolenaar30fd8202020-09-26 15:09:30 +0200741 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 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 {
746 // In legacy Vim script ":final" is short for ":finally".
747 ex_finally(eap);
748 return;
749 }
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 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
756 // In legacy Vim script ":const" works like ":final".
757 eap->cmdidx = CMD_final;
758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 // detect Vim9 assignment without ":let" or ":const"
760 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200761 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200763 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200764 if (argend == NULL)
765 return;
766 if (argend > arg && argend[-1] == '.') // for var.='str'
767 --argend;
768 expr = skipwhite(argend);
769 concat = expr[0] == '.'
770 && ((expr[1] == '=' && current_sctx.sc_version < 2)
771 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200772 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
773 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200774 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 {
776 // ":let" without "=": list variables
777 if (*arg == '[')
778 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200779 else if (expr[0] == '.' && expr[1] == '=')
780 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200781 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200782 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200783 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200784 {
785 // Vim9 declaration ":let var: type"
786 arg = vim9_declare_scriptvar(eap, arg);
787 }
788 else
789 {
790 // ":let var1 var2" - list values
791 arg = list_arg_vars(eap, arg, &first);
792 }
793 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 else if (!eap->skip)
795 {
796 // ":let"
797 list_glob_vars(&first);
798 list_buf_vars(&first);
799 list_win_vars(&first);
800 list_tab_vars(&first);
801 list_script_vars(&first);
802 list_func_vars(&first);
803 list_vim_vars(&first);
804 }
805 eap->nextcmd = check_nextcmd(arg);
806 }
807 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
808 {
809 list_T *l;
810
811 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200812 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200813 if (l != NULL)
814 {
815 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200816 if (!eap->skip)
817 {
818 op[0] = '=';
819 op[1] = NUL;
820 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200822 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200823 clear_tv(&rettv);
824 }
825 }
826 else
827 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200828 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200829 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200830
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200831 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200832 i = FAIL;
833 if (has_assign || concat)
834 {
835 op[0] = '=';
836 op[1] = NUL;
837 if (*expr != '=')
838 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200839 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200840 {
841 // +=, /=, etc. require an existing variable
842 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
843 i = FAIL;
844 }
845 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200846 {
847 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200848 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200849 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200850 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200851 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200852 ++len;
853 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200855 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200856 }
857 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200858 ++expr;
859
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200860 if (vim9script && (!VIM_ISWHITE(*argend)
861 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200862 {
863 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200864 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200865 i = FAIL;
866 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200867
868 if (eap->skip)
869 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200870 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200871 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200872 if (getline_equal(eap->getline, eap->cookie, getsourceline))
873 {
874 evalarg.eval_getline = eap->getline;
875 evalarg.eval_cookie = eap->cookie;
876 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200877 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200878 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200879 if (eap->skip)
880 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200881 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200882 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200883 if (eap->skip)
884 {
885 if (i != FAIL)
886 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200887 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200888 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200889 {
890 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200891 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 clear_tv(&rettv);
893 }
894 }
895}
896
897/*
898 * Assign the typevalue "tv" to the variable or variables at "arg_start".
899 * Handles both "var" with any type and "[var, var; var]" with a list type.
900 * When "op" is not NULL it points to a string with characters that
901 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
902 * or concatenate.
903 * Returns OK or FAIL;
904 */
905 int
906ex_let_vars(
907 char_u *arg_start,
908 typval_T *tv,
909 int copy, // copy values from "tv", don't move
910 int semicolon, // from skip_var_list()
911 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200912 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200913 char_u *op)
914{
915 char_u *arg = arg_start;
916 list_T *l;
917 int i;
918 listitem_T *item;
919 typval_T ltv;
920
921 if (*arg != '[')
922 {
923 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200925 return FAIL;
926 return OK;
927 }
928
929 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
930 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
931 {
932 emsg(_(e_listreq));
933 return FAIL;
934 }
935
936 i = list_len(l);
937 if (semicolon == 0 && var_count < i)
938 {
939 emsg(_("E687: Less targets than List items"));
940 return FAIL;
941 }
942 if (var_count - semicolon > i)
943 {
944 emsg(_("E688: More targets than List items"));
945 return FAIL;
946 }
947
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200948 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200949 item = l->lv_first;
950 while (*arg != ']')
951 {
952 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200954 item = item->li_next;
955 if (arg == NULL)
956 return FAIL;
957
958 arg = skipwhite(arg);
959 if (*arg == ';')
960 {
961 // Put the rest of the list (may be empty) in the var after ';'.
962 // Create a new list for this.
963 l = list_alloc();
964 if (l == NULL)
965 return FAIL;
966 while (item != NULL)
967 {
968 list_append_tv(l, &item->li_tv);
969 item = item->li_next;
970 }
971
972 ltv.v_type = VAR_LIST;
973 ltv.v_lock = 0;
974 ltv.vval.v_list = l;
975 l->lv_refcount = 1;
976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200978 (char_u *)"]", op);
979 clear_tv(&ltv);
980 if (arg == NULL)
981 return FAIL;
982 break;
983 }
984 else if (*arg != ',' && *arg != ']')
985 {
986 internal_error("ex_let_vars()");
987 return FAIL;
988 }
989 }
990
991 return OK;
992}
993
994/*
995 * Skip over assignable variable "var" or list of variables "[var, var]".
996 * Used for ":let varvar = expr" and ":for varvar in expr".
997 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200998 * for "[var, var; var]" set "semicolon" to 1.
999 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001000 * Return NULL for an error.
1001 */
1002 char_u *
1003skip_var_list(
1004 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001006 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001007 int *semicolon,
1008 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009{
1010 char_u *p, *s;
1011
1012 if (*arg == '[')
1013 {
1014 // "[var, var]": find the matching ']'.
1015 p = arg;
1016 for (;;)
1017 {
1018 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001019 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001020 if (s == p)
1021 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001022 if (!silent)
1023 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001024 return NULL;
1025 }
1026 ++*var_count;
1027
1028 p = skipwhite(s);
1029 if (*p == ']')
1030 break;
1031 else if (*p == ';')
1032 {
1033 if (*semicolon == 1)
1034 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001035 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001036 return NULL;
1037 }
1038 *semicolon = 1;
1039 }
1040 else if (*p != ',')
1041 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001042 if (!silent)
1043 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001044 return NULL;
1045 }
1046 }
1047 return p + 1;
1048 }
1049 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051}
1052
1053/*
1054 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1055 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001058 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 char_u *end;
1062
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063 if (*arg == '@' && arg[1] != NUL)
1064 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001067 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001069 // "a: type" is declaring variable "a" with a type, not "a:".
1070 if (end == arg + 2 && end[-1] == ':')
1071 --end;
1072 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001073 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 }
1075 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001076}
1077
1078/*
1079 * List variables for hashtab "ht" with prefix "prefix".
1080 * If "empty" is TRUE also list NULL strings as empty strings.
1081 */
1082 void
1083list_hashtable_vars(
1084 hashtab_T *ht,
1085 char *prefix,
1086 int empty,
1087 int *first)
1088{
1089 hashitem_T *hi;
1090 dictitem_T *di;
1091 int todo;
1092 char_u buf[IOSIZE];
1093
1094 todo = (int)ht->ht_used;
1095 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1096 {
1097 if (!HASHITEM_EMPTY(hi))
1098 {
1099 --todo;
1100 di = HI2DI(hi);
1101
1102 // apply :filter /pat/ to variable name
1103 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1104 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1105 if (message_filtered(buf))
1106 continue;
1107
1108 if (empty || di->di_tv.v_type != VAR_STRING
1109 || di->di_tv.vval.v_string != NULL)
1110 list_one_var(di, prefix, first);
1111 }
1112 }
1113}
1114
1115/*
1116 * List global variables.
1117 */
1118 static void
1119list_glob_vars(int *first)
1120{
1121 list_hashtable_vars(&globvarht, "", TRUE, first);
1122}
1123
1124/*
1125 * List buffer variables.
1126 */
1127 static void
1128list_buf_vars(int *first)
1129{
1130 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1131}
1132
1133/*
1134 * List window variables.
1135 */
1136 static void
1137list_win_vars(int *first)
1138{
1139 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1140}
1141
1142/*
1143 * List tab page variables.
1144 */
1145 static void
1146list_tab_vars(int *first)
1147{
1148 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1149}
1150
1151/*
1152 * List variables in "arg".
1153 */
1154 static char_u *
1155list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1156{
1157 int error = FALSE;
1158 int len;
1159 char_u *name;
1160 char_u *name_start;
1161 char_u *arg_subsc;
1162 char_u *tofree;
1163 typval_T tv;
1164
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001165 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001166 {
1167 if (error || eap->skip)
1168 {
1169 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1170 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1171 {
1172 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001173 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001174 break;
1175 }
1176 }
1177 else
1178 {
1179 // get_name_len() takes care of expanding curly braces
1180 name_start = name = arg;
1181 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1182 if (len <= 0)
1183 {
1184 // This is mainly to keep test 49 working: when expanding
1185 // curly braces fails overrule the exception error message.
1186 if (len < 0 && !aborting())
1187 {
1188 emsg_severe = TRUE;
1189 semsg(_(e_invarg2), arg);
1190 break;
1191 }
1192 error = TRUE;
1193 }
1194 else
1195 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001196 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001197 if (tofree != NULL)
1198 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001199 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 error = TRUE;
1201 else
1202 {
1203 // handle d.key, l[idx], f(expr)
1204 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001205 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001206 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001207 error = TRUE;
1208 else
1209 {
1210 if (arg == arg_subsc && len == 2 && name[1] == ':')
1211 {
1212 switch (*name)
1213 {
1214 case 'g': list_glob_vars(first); break;
1215 case 'b': list_buf_vars(first); break;
1216 case 'w': list_win_vars(first); break;
1217 case 't': list_tab_vars(first); break;
1218 case 'v': list_vim_vars(first); break;
1219 case 's': list_script_vars(first); break;
1220 case 'l': list_func_vars(first); break;
1221 default:
1222 semsg(_("E738: Can't list variables for %s"), name);
1223 }
1224 }
1225 else
1226 {
1227 char_u numbuf[NUMBUFLEN];
1228 char_u *tf;
1229 int c;
1230 char_u *s;
1231
1232 s = echo_string(&tv, &tf, numbuf, 0);
1233 c = *arg;
1234 *arg = NUL;
1235 list_one_var_a("",
1236 arg == arg_subsc ? name : name_start,
1237 tv.v_type,
1238 s == NULL ? (char_u *)"" : s,
1239 first);
1240 *arg = c;
1241 vim_free(tf);
1242 }
1243 clear_tv(&tv);
1244 }
1245 }
1246 }
1247
1248 vim_free(tofree);
1249 }
1250
1251 arg = skipwhite(arg);
1252 }
1253
1254 return arg;
1255}
1256
1257/*
1258 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1259 * Returns a pointer to the char just after the var name.
1260 * Returns NULL if there is an error.
1261 */
1262 static char_u *
1263ex_let_one(
1264 char_u *arg, // points to variable name
1265 typval_T *tv, // value to assign to variable
1266 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001267 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001268 char_u *endchars, // valid chars after variable name or NULL
1269 char_u *op) // "+", "-", "." or NULL
1270{
1271 int c1;
1272 char_u *name;
1273 char_u *p;
1274 char_u *arg_end = NULL;
1275 int len;
1276 int opt_flags;
1277 char_u *tofree = NULL;
1278
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001279 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001280 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1281 {
1282 vim9_declare_error(arg);
1283 return NULL;
1284 }
1285
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001286 // ":let $VAR = expr": Set environment variable.
1287 if (*arg == '$')
1288 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001289 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001290 {
1291 emsg(_("E996: Cannot lock an environment variable"));
1292 return NULL;
1293 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001294
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001295 // Find the end of the name.
1296 ++arg;
1297 name = arg;
1298 len = get_env_len(&arg);
1299 if (len == 0)
1300 semsg(_(e_invarg2), name - 1);
1301 else
1302 {
1303 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1304 semsg(_(e_letwrong), op);
1305 else if (endchars != NULL
1306 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1307 emsg(_(e_letunexp));
1308 else if (!check_secure())
1309 {
1310 c1 = name[len];
1311 name[len] = NUL;
1312 p = tv_get_string_chk(tv);
1313 if (p != NULL && op != NULL && *op == '.')
1314 {
1315 int mustfree = FALSE;
1316 char_u *s = vim_getenv(name, &mustfree);
1317
1318 if (s != NULL)
1319 {
1320 p = tofree = concat_str(s, p);
1321 if (mustfree)
1322 vim_free(s);
1323 }
1324 }
1325 if (p != NULL)
1326 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001327 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001328 arg_end = arg;
1329 }
1330 name[len] = c1;
1331 vim_free(tofree);
1332 }
1333 }
1334 }
1335
1336 // ":let &option = expr": Set option value.
1337 // ":let &l:option = expr": Set local option value.
1338 // ":let &g:option = expr": Set global option value.
1339 else if (*arg == '&')
1340 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001341 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001342 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001344 return NULL;
1345 }
1346 // Find the end of the name.
1347 p = find_option_end(&arg, &opt_flags);
1348 if (p == NULL || (endchars != NULL
1349 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1350 emsg(_(e_letunexp));
1351 else
1352 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001353 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001354 int opt_type;
1355 long numval;
1356 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001357 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001358 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001359
1360 c1 = *p;
1361 *p = NUL;
1362
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001363 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1364 if ((opt_type == 1 || opt_type == -1)
1365 && (tv->v_type != VAR_STRING || !in_vim9script()))
1366 // number, possibly hidden
1367 n = (long)tv_get_number(tv);
1368
1369 // Avoid setting a string option to the text "v:false" or similar.
1370 // In Vim9 script also don't convert a number to string.
1371 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1372 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1373 s = tv_get_string_chk(tv);
1374
1375 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001376 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001377 if ((opt_type == 1 && *op == '.')
1378 || (opt_type == 0 && *op != '.'))
1379 {
1380 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001381 failed = TRUE; // don't set the value
1382
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383 }
1384 else
1385 {
1386 if (opt_type == 1) // number
1387 {
1388 switch (*op)
1389 {
1390 case '+': n = numval + n; break;
1391 case '-': n = numval - n; break;
1392 case '*': n = numval * n; break;
1393 case '/': n = (long)num_divide(numval, n); break;
1394 case '%': n = (long)num_modulus(numval, n); break;
1395 }
1396 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001397 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001398 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001399 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001400 s = concat_str(stringval, s);
1401 vim_free(stringval);
1402 stringval = s;
1403 }
1404 }
1405 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001406
1407 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001409 if (opt_type != 0 || s != NULL)
1410 {
1411 set_option_value(arg, n, s, opt_flags);
1412 arg_end = p;
1413 }
1414 else
1415 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001416 }
1417 *p = c1;
1418 vim_free(stringval);
1419 }
1420 }
1421
1422 // ":let @r = expr": Set register contents.
1423 else if (*arg == '@')
1424 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001425 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001426 {
1427 emsg(_("E996: Cannot lock a register"));
1428 return NULL;
1429 }
1430 ++arg;
1431 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1432 semsg(_(e_letwrong), op);
1433 else if (endchars != NULL
1434 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1435 emsg(_(e_letunexp));
1436 else
1437 {
1438 char_u *ptofree = NULL;
1439 char_u *s;
1440
1441 p = tv_get_string_chk(tv);
1442 if (p != NULL && op != NULL && *op == '.')
1443 {
1444 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1445 if (s != NULL)
1446 {
1447 p = ptofree = concat_str(s, p);
1448 vim_free(s);
1449 }
1450 }
1451 if (p != NULL)
1452 {
1453 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1454 arg_end = arg + 1;
1455 }
1456 vim_free(ptofree);
1457 }
1458 }
1459
1460 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001462 // ":let {expr} = expr": Idem, name made with curly braces
1463 else if (eval_isnamec1(*arg) || *arg == '{')
1464 {
1465 lval_T lv;
1466
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001467 p = get_lval(arg, tv, &lv, FALSE, FALSE,
1468 (flags & ASSIGN_NO_DECL) ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001469 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001470 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 if (endchars != NULL && vim_strchr(endchars,
1472 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001473 emsg(_(e_letunexp));
1474 else
1475 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001477 arg_end = p;
1478 }
1479 }
1480 clear_lval(&lv);
1481 }
1482
1483 else
1484 semsg(_(e_invarg2), arg);
1485
1486 return arg_end;
1487}
1488
1489/*
1490 * ":unlet[!] var1 ... " command.
1491 */
1492 void
1493ex_unlet(exarg_T *eap)
1494{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001495 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001496}
1497
1498/*
1499 * ":lockvar" and ":unlockvar" commands
1500 */
1501 void
1502ex_lockvar(exarg_T *eap)
1503{
1504 char_u *arg = eap->arg;
1505 int deep = 2;
1506
1507 if (eap->forceit)
1508 deep = -1;
1509 else if (vim_isdigit(*arg))
1510 {
1511 deep = getdigits(&arg);
1512 arg = skipwhite(arg);
1513 }
1514
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001515 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001516}
1517
1518/*
1519 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001520 * Also used for Vim9 script. "callback" is invoked as:
1521 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001522 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001523 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524ex_unletlock(
1525 exarg_T *eap,
1526 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001527 int deep,
1528 int glv_flags,
1529 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1530 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001531{
1532 char_u *arg = argstart;
1533 char_u *name_end;
1534 int error = FALSE;
1535 lval_T lv;
1536
1537 do
1538 {
1539 if (*arg == '$')
1540 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001541 lv.ll_name = arg;
1542 lv.ll_tv = NULL;
1543 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544 if (get_env_len(&arg) == 0)
1545 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001546 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001547 return;
1548 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001549 if (!error && !eap->skip
1550 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1551 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001552 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001554 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001555 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001556 // Parse the name and find the end.
1557 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1558 glv_flags, FNE_CHECK_START);
1559 if (lv.ll_name == NULL)
1560 error = TRUE; // error but continue parsing
1561 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1562 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001564 if (name_end != NULL)
1565 {
1566 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001567 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001568 }
1569 if (!(eap->skip || error))
1570 clear_lval(&lv);
1571 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001573
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001574 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001575 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001576 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001578 if (!eap->skip)
1579 clear_lval(&lv);
1580 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581
1582 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001583 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001584
1585 eap->nextcmd = check_nextcmd(arg);
1586}
1587
1588 static int
1589do_unlet_var(
1590 lval_T *lp,
1591 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001592 exarg_T *eap,
1593 int deep UNUSED,
1594 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001595{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001596 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597 int ret = OK;
1598 int cc;
1599
1600 if (lp->ll_tv == NULL)
1601 {
1602 cc = *name_end;
1603 *name_end = NUL;
1604
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001605 // Environment variable, normal name or expanded name.
1606 if (*lp->ll_name == '$')
1607 vim_unsetenv(lp->ll_name + 1);
1608 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001609 ret = FAIL;
1610 *name_end = cc;
1611 }
1612 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001613 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001614 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001615 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001616 return FAIL;
1617 else if (lp->ll_range)
1618 {
1619 listitem_T *li;
1620 listitem_T *ll_li = lp->ll_li;
1621 int ll_n1 = lp->ll_n1;
1622
1623 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1624 {
1625 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001626 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001627 return FAIL;
1628 ll_li = li;
1629 ++ll_n1;
1630 }
1631
1632 // Delete a range of List items.
1633 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1634 {
1635 li = lp->ll_li->li_next;
1636 listitem_remove(lp->ll_list, lp->ll_li);
1637 lp->ll_li = li;
1638 ++lp->ll_n1;
1639 }
1640 }
1641 else
1642 {
1643 if (lp->ll_list != NULL)
1644 // unlet a List item.
1645 listitem_remove(lp->ll_list, lp->ll_li);
1646 else
1647 // unlet a Dictionary item.
1648 dictitem_remove(lp->ll_dict, lp->ll_di);
1649 }
1650
1651 return ret;
1652}
1653
1654/*
1655 * "unlet" a variable. Return OK if it existed, FAIL if not.
1656 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1657 */
1658 int
1659do_unlet(char_u *name, int forceit)
1660{
1661 hashtab_T *ht;
1662 hashitem_T *hi;
1663 char_u *varname;
1664 dict_T *d;
1665 dictitem_T *di;
1666
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001667 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001668 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001669 return FAIL;
1670
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001671 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001672
1673 // can't :unlet a script variable in Vim9 script from a function
1674 if (ht == get_script_local_ht()
1675 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1676 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1677 == SCRIPT_VERSION_VIM9
1678 && check_vim9_unlet(name) == FAIL)
1679 return FAIL;
1680
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001681 if (ht != NULL && *varname != NUL)
1682 {
1683 d = get_current_funccal_dict(ht);
1684 if (d == NULL)
1685 {
1686 if (ht == &globvarht)
1687 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001688 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001689 d = &vimvardict;
1690 else
1691 {
1692 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1693 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1694 }
1695 if (d == NULL)
1696 {
1697 internal_error("do_unlet()");
1698 return FAIL;
1699 }
1700 }
1701 hi = hash_find(ht, varname);
1702 if (HASHITEM_EMPTY(hi))
1703 hi = find_hi_in_scoped_ht(name, &ht);
1704 if (hi != NULL && !HASHITEM_EMPTY(hi))
1705 {
1706 di = HI2DI(hi);
1707 if (var_check_fixed(di->di_flags, name, FALSE)
1708 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001709 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001710 return FAIL;
1711
1712 delete_var(ht, hi);
1713 return OK;
1714 }
1715 }
1716 if (forceit)
1717 return OK;
1718 semsg(_("E108: No such variable: \"%s\""), name);
1719 return FAIL;
1720}
1721
1722/*
1723 * Lock or unlock variable indicated by "lp".
1724 * "deep" is the levels to go (-1 for unlimited);
1725 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1726 */
1727 static int
1728do_lock_var(
1729 lval_T *lp,
1730 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001731 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001732 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001733 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001735 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 int ret = OK;
1737 int cc;
1738 dictitem_T *di;
1739
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740 if (lp->ll_tv == NULL)
1741 {
1742 cc = *name_end;
1743 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001744 if (*lp->ll_name == '$')
1745 {
1746 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001748 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001749 else
1750 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001751 // Normal name or expanded name.
1752 di = find_var(lp->ll_name, NULL, TRUE);
1753 if (di == NULL)
1754 ret = FAIL;
1755 else if ((di->di_flags & DI_FLAGS_FIX)
1756 && di->di_tv.v_type != VAR_DICT
1757 && di->di_tv.v_type != VAR_LIST)
1758 {
1759 // For historic reasons this error is not given for a list or
1760 // dict. E.g., the b: dict could be locked/unlocked.
1761 semsg(_(e_lock_unlock), lp->ll_name);
1762 ret = FAIL;
1763 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001764 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001765 {
1766 if (lock)
1767 di->di_flags |= DI_FLAGS_LOCK;
1768 else
1769 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001770 if (deep != 0)
1771 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001772 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001773 }
1774 *name_end = cc;
1775 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001776 else if (deep == 0)
1777 {
1778 // nothing to do
1779 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001780 else if (lp->ll_range)
1781 {
1782 listitem_T *li = lp->ll_li;
1783
1784 // (un)lock a range of List items.
1785 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1786 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001787 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001788 li = li->li_next;
1789 ++lp->ll_n1;
1790 }
1791 }
1792 else if (lp->ll_list != NULL)
1793 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001794 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001795 else
1796 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001797 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798
1799 return ret;
1800}
1801
1802/*
1803 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001804 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1805 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001806 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001807 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001808item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001809{
1810 static int recurse = 0;
1811 list_T *l;
1812 listitem_T *li;
1813 dict_T *d;
1814 blob_T *b;
1815 hashitem_T *hi;
1816 int todo;
1817
1818 if (recurse >= DICT_MAXNEST)
1819 {
1820 emsg(_("E743: variable nested too deep for (un)lock"));
1821 return;
1822 }
1823 if (deep == 0)
1824 return;
1825 ++recurse;
1826
1827 // lock/unlock the item itself
1828 if (lock)
1829 tv->v_lock |= VAR_LOCKED;
1830 else
1831 tv->v_lock &= ~VAR_LOCKED;
1832
1833 switch (tv->v_type)
1834 {
1835 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001836 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001838 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001840 case VAR_STRING:
1841 case VAR_FUNC:
1842 case VAR_PARTIAL:
1843 case VAR_FLOAT:
1844 case VAR_SPECIAL:
1845 case VAR_JOB:
1846 case VAR_CHANNEL:
1847 break;
1848
1849 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001850 if ((b = tv->vval.v_blob) != NULL
1851 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001852 {
1853 if (lock)
1854 b->bv_lock |= VAR_LOCKED;
1855 else
1856 b->bv_lock &= ~VAR_LOCKED;
1857 }
1858 break;
1859 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001860 if ((l = tv->vval.v_list) != NULL
1861 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 {
1863 if (lock)
1864 l->lv_lock |= VAR_LOCKED;
1865 else
1866 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001867 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001868 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001869 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001870 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001871 }
1872 break;
1873 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001874 if ((d = tv->vval.v_dict) != NULL
1875 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876 {
1877 if (lock)
1878 d->dv_lock |= VAR_LOCKED;
1879 else
1880 d->dv_lock &= ~VAR_LOCKED;
1881 if (deep < 0 || deep > 1)
1882 {
1883 // recursive: lock/unlock the items the List contains
1884 todo = (int)d->dv_hashtab.ht_used;
1885 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1886 {
1887 if (!HASHITEM_EMPTY(hi))
1888 {
1889 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001890 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1891 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001892 }
1893 }
1894 }
1895 }
1896 }
1897 --recurse;
1898}
1899
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001900#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1901/*
1902 * Delete all "menutrans_" variables.
1903 */
1904 void
1905del_menutrans_vars(void)
1906{
1907 hashitem_T *hi;
1908 int todo;
1909
1910 hash_lock(&globvarht);
1911 todo = (int)globvarht.ht_used;
1912 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1913 {
1914 if (!HASHITEM_EMPTY(hi))
1915 {
1916 --todo;
1917 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1918 delete_var(&globvarht, hi);
1919 }
1920 }
1921 hash_unlock(&globvarht);
1922}
1923#endif
1924
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001925/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001926 * Local string buffer for the next two functions to store a variable name
1927 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1928 * get_user_var_name().
1929 */
1930
1931static char_u *varnamebuf = NULL;
1932static int varnamebuflen = 0;
1933
1934/*
1935 * Function to concatenate a prefix and a variable name.
1936 */
1937 static char_u *
1938cat_prefix_varname(int prefix, char_u *name)
1939{
1940 int len;
1941
1942 len = (int)STRLEN(name) + 3;
1943 if (len > varnamebuflen)
1944 {
1945 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001946 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001947 varnamebuf = alloc(len);
1948 if (varnamebuf == NULL)
1949 {
1950 varnamebuflen = 0;
1951 return NULL;
1952 }
1953 varnamebuflen = len;
1954 }
1955 *varnamebuf = prefix;
1956 varnamebuf[1] = ':';
1957 STRCPY(varnamebuf + 2, name);
1958 return varnamebuf;
1959}
1960
1961/*
1962 * Function given to ExpandGeneric() to obtain the list of user defined
1963 * (global/buffer/window/built-in) variable names.
1964 */
1965 char_u *
1966get_user_var_name(expand_T *xp, int idx)
1967{
1968 static long_u gdone;
1969 static long_u bdone;
1970 static long_u wdone;
1971 static long_u tdone;
1972 static int vidx;
1973 static hashitem_T *hi;
1974 hashtab_T *ht;
1975
1976 if (idx == 0)
1977 {
1978 gdone = bdone = wdone = vidx = 0;
1979 tdone = 0;
1980 }
1981
1982 // Global variables
1983 if (gdone < globvarht.ht_used)
1984 {
1985 if (gdone++ == 0)
1986 hi = globvarht.ht_array;
1987 else
1988 ++hi;
1989 while (HASHITEM_EMPTY(hi))
1990 ++hi;
1991 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1992 return cat_prefix_varname('g', hi->hi_key);
1993 return hi->hi_key;
1994 }
1995
1996 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01001997 ht =
1998#ifdef FEAT_CMDWIN
1999 // In cmdwin, the alternative buffer should be used.
2000 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2001 &prevwin->w_buffer->b_vars->dv_hashtab :
2002#endif
2003 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002004 if (bdone < ht->ht_used)
2005 {
2006 if (bdone++ == 0)
2007 hi = ht->ht_array;
2008 else
2009 ++hi;
2010 while (HASHITEM_EMPTY(hi))
2011 ++hi;
2012 return cat_prefix_varname('b', hi->hi_key);
2013 }
2014
2015 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002016 ht =
2017#ifdef FEAT_CMDWIN
2018 // In cmdwin, the alternative window should be used.
2019 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2020 &prevwin->w_vars->dv_hashtab :
2021#endif
2022 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002023 if (wdone < ht->ht_used)
2024 {
2025 if (wdone++ == 0)
2026 hi = ht->ht_array;
2027 else
2028 ++hi;
2029 while (HASHITEM_EMPTY(hi))
2030 ++hi;
2031 return cat_prefix_varname('w', hi->hi_key);
2032 }
2033
2034 // t: variables
2035 ht = &curtab->tp_vars->dv_hashtab;
2036 if (tdone < ht->ht_used)
2037 {
2038 if (tdone++ == 0)
2039 hi = ht->ht_array;
2040 else
2041 ++hi;
2042 while (HASHITEM_EMPTY(hi))
2043 ++hi;
2044 return cat_prefix_varname('t', hi->hi_key);
2045 }
2046
2047 // v: variables
2048 if (vidx < VV_LEN)
2049 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2050
2051 VIM_CLEAR(varnamebuf);
2052 varnamebuflen = 0;
2053 return NULL;
2054}
2055
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002056 char *
2057get_var_special_name(int nr)
2058{
2059 switch (nr)
2060 {
2061 case VVAL_FALSE: return "v:false";
2062 case VVAL_TRUE: return "v:true";
2063 case VVAL_NONE: return "v:none";
2064 case VVAL_NULL: return "v:null";
2065 }
2066 internal_error("get_var_special_name()");
2067 return "42";
2068}
2069
2070/*
2071 * Returns the global variable dictionary
2072 */
2073 dict_T *
2074get_globvar_dict(void)
2075{
2076 return &globvardict;
2077}
2078
2079/*
2080 * Returns the global variable hash table
2081 */
2082 hashtab_T *
2083get_globvar_ht(void)
2084{
2085 return &globvarht;
2086}
2087
2088/*
2089 * Returns the v: variable dictionary
2090 */
2091 dict_T *
2092get_vimvar_dict(void)
2093{
2094 return &vimvardict;
2095}
2096
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002097/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002099 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 */
2101 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002102find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002104 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2105 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106
2107 if (di == NULL)
2108 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002109 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2111 return (int)(vv - vimvars);
2112}
2113
2114
2115/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002116 * Set type of v: variable to "type".
2117 */
2118 void
2119set_vim_var_type(int idx, vartype_T type)
2120{
2121 vimvars[idx].vv_type = type;
2122}
2123
2124/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002125 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002126 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002127 */
2128 void
2129set_vim_var_nr(int idx, varnumber_T val)
2130{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002131 vimvars[idx].vv_nr = val;
2132}
2133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 char *
2135get_vim_var_name(int idx)
2136{
2137 return vimvars[idx].vv_name;
2138}
2139
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002140/*
2141 * Get typval_T v: variable value.
2142 */
2143 typval_T *
2144get_vim_var_tv(int idx)
2145{
2146 return &vimvars[idx].vv_tv;
2147}
2148
2149/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002150 * Set v: variable to "tv". Only accepts the same type.
2151 * Takes over the value of "tv".
2152 */
2153 int
2154set_vim_var_tv(int idx, typval_T *tv)
2155{
2156 if (vimvars[idx].vv_type != tv->v_type)
2157 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002158 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002159 clear_tv(tv);
2160 return FAIL;
2161 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002162 // VV_RO is also checked when compiling, but let's check here as well.
2163 if (vimvars[idx].vv_flags & VV_RO)
2164 {
2165 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2166 return FAIL;
2167 }
2168 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2169 {
2170 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2171 return FAIL;
2172 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002173 clear_tv(&vimvars[idx].vv_di.di_tv);
2174 vimvars[idx].vv_di.di_tv = *tv;
2175 return OK;
2176}
2177
2178/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002179 * Get number v: variable value.
2180 */
2181 varnumber_T
2182get_vim_var_nr(int idx)
2183{
2184 return vimvars[idx].vv_nr;
2185}
2186
2187/*
2188 * Get string v: variable value. Uses a static buffer, can only be used once.
2189 * If the String variable has never been set, return an empty string.
2190 * Never returns NULL;
2191 */
2192 char_u *
2193get_vim_var_str(int idx)
2194{
2195 return tv_get_string(&vimvars[idx].vv_tv);
2196}
2197
2198/*
2199 * Get List v: variable value. Caller must take care of reference count when
2200 * needed.
2201 */
2202 list_T *
2203get_vim_var_list(int idx)
2204{
2205 return vimvars[idx].vv_list;
2206}
2207
2208/*
2209 * Get Dict v: variable value. Caller must take care of reference count when
2210 * needed.
2211 */
2212 dict_T *
2213get_vim_var_dict(int idx)
2214{
2215 return vimvars[idx].vv_dict;
2216}
2217
2218/*
2219 * Set v:char to character "c".
2220 */
2221 void
2222set_vim_var_char(int c)
2223{
2224 char_u buf[MB_MAXBYTES + 1];
2225
2226 if (has_mbyte)
2227 buf[(*mb_char2bytes)(c, buf)] = NUL;
2228 else
2229 {
2230 buf[0] = c;
2231 buf[1] = NUL;
2232 }
2233 set_vim_var_string(VV_CHAR, buf, -1);
2234}
2235
2236/*
2237 * Set v:count to "count" and v:count1 to "count1".
2238 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2239 */
2240 void
2241set_vcount(
2242 long count,
2243 long count1,
2244 int set_prevcount)
2245{
2246 if (set_prevcount)
2247 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2248 vimvars[VV_COUNT].vv_nr = count;
2249 vimvars[VV_COUNT1].vv_nr = count1;
2250}
2251
2252/*
2253 * Save variables that might be changed as a side effect. Used when executing
2254 * a timer callback.
2255 */
2256 void
2257save_vimvars(vimvars_save_T *vvsave)
2258{
2259 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2260 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2261 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2262}
2263
2264/*
2265 * Restore variables saved by save_vimvars().
2266 */
2267 void
2268restore_vimvars(vimvars_save_T *vvsave)
2269{
2270 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2271 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2272 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2273}
2274
2275/*
2276 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2277 * value.
2278 */
2279 void
2280set_vim_var_string(
2281 int idx,
2282 char_u *val,
2283 int len) // length of "val" to use or -1 (whole string)
2284{
2285 clear_tv(&vimvars[idx].vv_di.di_tv);
2286 vimvars[idx].vv_type = VAR_STRING;
2287 if (val == NULL)
2288 vimvars[idx].vv_str = NULL;
2289 else if (len == -1)
2290 vimvars[idx].vv_str = vim_strsave(val);
2291 else
2292 vimvars[idx].vv_str = vim_strnsave(val, len);
2293}
2294
2295/*
2296 * Set List v: variable to "val".
2297 */
2298 void
2299set_vim_var_list(int idx, list_T *val)
2300{
2301 clear_tv(&vimvars[idx].vv_di.di_tv);
2302 vimvars[idx].vv_type = VAR_LIST;
2303 vimvars[idx].vv_list = val;
2304 if (val != NULL)
2305 ++val->lv_refcount;
2306}
2307
2308/*
2309 * Set Dictionary v: variable to "val".
2310 */
2311 void
2312set_vim_var_dict(int idx, dict_T *val)
2313{
2314 clear_tv(&vimvars[idx].vv_di.di_tv);
2315 vimvars[idx].vv_type = VAR_DICT;
2316 vimvars[idx].vv_dict = val;
2317 if (val != NULL)
2318 {
2319 ++val->dv_refcount;
2320 dict_set_items_ro(val);
2321 }
2322}
2323
2324/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002325 * Set the v:argv list.
2326 */
2327 void
2328set_argv_var(char **argv, int argc)
2329{
2330 list_T *l = list_alloc();
2331 int i;
2332
2333 if (l == NULL)
2334 getout(1);
2335 l->lv_lock = VAR_FIXED;
2336 for (i = 0; i < argc; ++i)
2337 {
2338 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2339 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002340 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002341 }
2342 set_vim_var_list(VV_ARGV, l);
2343}
2344
2345/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002346 * Reset v:register, taking the 'clipboard' setting into account.
2347 */
2348 void
2349reset_reg_var(void)
2350{
2351 int regname = 0;
2352
2353 // Adjust the register according to 'clipboard', so that when
2354 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2355#ifdef FEAT_CLIPBOARD
2356 adjust_clip_reg(&regname);
2357#endif
2358 set_reg_var(regname);
2359}
2360
2361/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002362 * Set v:register if needed.
2363 */
2364 void
2365set_reg_var(int c)
2366{
2367 char_u regname;
2368
2369 if (c == 0 || c == ' ')
2370 regname = '"';
2371 else
2372 regname = c;
2373 // Avoid free/alloc when the value is already right.
2374 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2375 set_vim_var_string(VV_REG, &regname, 1);
2376}
2377
2378/*
2379 * Get or set v:exception. If "oldval" == NULL, return the current value.
2380 * Otherwise, restore the value to "oldval" and return NULL.
2381 * Must always be called in pairs to save and restore v:exception! Does not
2382 * take care of memory allocations.
2383 */
2384 char_u *
2385v_exception(char_u *oldval)
2386{
2387 if (oldval == NULL)
2388 return vimvars[VV_EXCEPTION].vv_str;
2389
2390 vimvars[VV_EXCEPTION].vv_str = oldval;
2391 return NULL;
2392}
2393
2394/*
2395 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2396 * Otherwise, restore the value to "oldval" and return NULL.
2397 * Must always be called in pairs to save and restore v:throwpoint! Does not
2398 * take care of memory allocations.
2399 */
2400 char_u *
2401v_throwpoint(char_u *oldval)
2402{
2403 if (oldval == NULL)
2404 return vimvars[VV_THROWPOINT].vv_str;
2405
2406 vimvars[VV_THROWPOINT].vv_str = oldval;
2407 return NULL;
2408}
2409
2410/*
2411 * Set v:cmdarg.
2412 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2413 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2414 * Must always be called in pairs!
2415 */
2416 char_u *
2417set_cmdarg(exarg_T *eap, char_u *oldarg)
2418{
2419 char_u *oldval;
2420 char_u *newval;
2421 unsigned len;
2422
2423 oldval = vimvars[VV_CMDARG].vv_str;
2424 if (eap == NULL)
2425 {
2426 vim_free(oldval);
2427 vimvars[VV_CMDARG].vv_str = oldarg;
2428 return NULL;
2429 }
2430
2431 if (eap->force_bin == FORCE_BIN)
2432 len = 6;
2433 else if (eap->force_bin == FORCE_NOBIN)
2434 len = 8;
2435 else
2436 len = 0;
2437
2438 if (eap->read_edit)
2439 len += 7;
2440
2441 if (eap->force_ff != 0)
2442 len += 10; // " ++ff=unix"
2443 if (eap->force_enc != 0)
2444 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2445 if (eap->bad_char != 0)
2446 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2447
2448 newval = alloc(len + 1);
2449 if (newval == NULL)
2450 return NULL;
2451
2452 if (eap->force_bin == FORCE_BIN)
2453 sprintf((char *)newval, " ++bin");
2454 else if (eap->force_bin == FORCE_NOBIN)
2455 sprintf((char *)newval, " ++nobin");
2456 else
2457 *newval = NUL;
2458
2459 if (eap->read_edit)
2460 STRCAT(newval, " ++edit");
2461
2462 if (eap->force_ff != 0)
2463 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2464 eap->force_ff == 'u' ? "unix"
2465 : eap->force_ff == 'd' ? "dos"
2466 : "mac");
2467 if (eap->force_enc != 0)
2468 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2469 eap->cmd + eap->force_enc);
2470 if (eap->bad_char == BAD_KEEP)
2471 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2472 else if (eap->bad_char == BAD_DROP)
2473 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2474 else if (eap->bad_char != 0)
2475 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2476 vimvars[VV_CMDARG].vv_str = newval;
2477 return oldval;
2478}
2479
2480/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002481 * Get the value of internal variable "name".
2482 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2483 */
2484 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002485eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002486 char_u *name,
2487 int len, // length of "name"
2488 typval_T *rettv, // NULL when only checking existence
2489 dictitem_T **dip, // non-NULL when typval's dict item is needed
2490 int verbose, // may give error message
2491 int no_autoload) // do not use script autoloading
2492{
2493 int ret = OK;
2494 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002495 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002496 dictitem_T *v;
2497 int cc;
2498
2499 // truncate the name, so that we can use strcmp()
2500 cc = name[len];
2501 name[len] = NUL;
2502
2503 // Check for user-defined variables.
2504 v = find_var(name, NULL, no_autoload);
2505 if (v != NULL)
2506 {
2507 tv = &v->di_tv;
2508 if (dip != NULL)
2509 *dip = v;
2510 }
2511
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002512 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002514 imported_T *import;
2515 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2516
2517 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518
2519 // imported variable from another script
2520 if (import != NULL)
2521 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002522 if (import->imp_funcname != NULL)
2523 {
2524 foundFunc = TRUE;
2525 if (rettv != NULL)
2526 {
2527 rettv->v_type = VAR_FUNC;
2528 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2529 }
2530 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002531 else if (import->imp_all)
2532 {
2533 emsg("Sorry, 'import * as X' not implemented yet");
2534 ret = FAIL;
2535 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002536 else
2537 {
2538 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002539 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002541 tv = sv->sv_tv;
2542 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002544 else if (in_vim9script())
2545 {
2546 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2547
2548 if (ufunc != NULL)
2549 {
2550 foundFunc = TRUE;
2551 if (rettv != NULL)
2552 {
2553 rettv->v_type = VAR_FUNC;
2554 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2555 }
2556 }
2557 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 }
2559
Bram Moolenaarc620c052020-07-08 15:16:19 +02002560 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002561 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002562 if (tv == NULL)
2563 {
2564 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002565 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002566 ret = FAIL;
2567 }
2568 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002569 {
2570 // If a list or dict variable wasn't initialized, do it now.
2571 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2572 {
2573 tv->vval.v_dict = dict_alloc();
2574 if (tv->vval.v_dict != NULL)
2575 ++tv->vval.v_dict->dv_refcount;
2576 }
2577 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2578 {
2579 tv->vval.v_list = list_alloc();
2580 if (tv->vval.v_list != NULL)
2581 ++tv->vval.v_list->lv_refcount;
2582 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002583 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002584 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002585 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002586
2587 name[len] = cc;
2588
2589 return ret;
2590}
2591
2592/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002593 * Check if variable "name[len]" is a local variable or an argument.
2594 * If so, "*eval_lavars_used" is set to TRUE.
2595 */
2596 void
2597check_vars(char_u *name, int len)
2598{
2599 int cc;
2600 char_u *varname;
2601 hashtab_T *ht;
2602
2603 if (eval_lavars_used == NULL)
2604 return;
2605
2606 // truncate the name, so that we can use strcmp()
2607 cc = name[len];
2608 name[len] = NUL;
2609
2610 ht = find_var_ht(name, &varname);
2611 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2612 {
2613 if (find_var(name, NULL, TRUE) != NULL)
2614 *eval_lavars_used = TRUE;
2615 }
2616
2617 name[len] = cc;
2618}
2619
2620/*
2621 * Find variable "name" in the list of variables.
2622 * Return a pointer to it if found, NULL if not found.
2623 * Careful: "a:0" variables don't have a name.
2624 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2625 * hashtab_T used.
2626 */
2627 dictitem_T *
2628find_var(char_u *name, hashtab_T **htp, int no_autoload)
2629{
2630 char_u *varname;
2631 hashtab_T *ht;
2632 dictitem_T *ret = NULL;
2633
2634 ht = find_var_ht(name, &varname);
2635 if (htp != NULL)
2636 *htp = ht;
2637 if (ht == NULL)
2638 return NULL;
2639 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2640 if (ret != NULL)
2641 return ret;
2642
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002643 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002644 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2645 if (ret != NULL)
2646 return ret;
2647
2648 // in Vim9 script items without a scope can be script-local
2649 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2650 {
2651 ht = get_script_local_ht();
2652 if (ht != NULL)
2653 {
2654 ret = find_var_in_ht(ht, *name, varname,
2655 no_autoload || htp != NULL);
2656 if (ret != NULL)
2657 {
2658 if (htp != NULL)
2659 *htp = ht;
2660 return ret;
2661 }
2662 }
2663 }
2664
2665 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002666}
2667
2668/*
2669 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002670 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002671 * Returns NULL if not found.
2672 */
2673 dictitem_T *
2674find_var_in_ht(
2675 hashtab_T *ht,
2676 int htname,
2677 char_u *varname,
2678 int no_autoload)
2679{
2680 hashitem_T *hi;
2681
2682 if (*varname == NUL)
2683 {
2684 // Must be something like "s:", otherwise "ht" would be NULL.
2685 switch (htname)
2686 {
2687 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2688 case 'g': return &globvars_var;
2689 case 'v': return &vimvars_var;
2690 case 'b': return &curbuf->b_bufvar;
2691 case 'w': return &curwin->w_winvar;
2692 case 't': return &curtab->tp_winvar;
2693 case 'l': return get_funccal_local_var();
2694 case 'a': return get_funccal_args_var();
2695 }
2696 return NULL;
2697 }
2698
2699 hi = hash_find(ht, varname);
2700 if (HASHITEM_EMPTY(hi))
2701 {
2702 // For global variables we may try auto-loading the script. If it
2703 // worked find the variable again. Don't auto-load a script if it was
2704 // loaded already, otherwise it would be loaded every time when
2705 // checking if a function name is a Funcref variable.
2706 if (ht == &globvarht && !no_autoload)
2707 {
2708 // Note: script_autoload() may make "hi" invalid. It must either
2709 // be obtained again or not used.
2710 if (!script_autoload(varname, FALSE) || aborting())
2711 return NULL;
2712 hi = hash_find(ht, varname);
2713 }
2714 if (HASHITEM_EMPTY(hi))
2715 return NULL;
2716 }
2717 return HI2DI(hi);
2718}
2719
2720/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 * Get the script-local hashtab. NULL if not in a script context.
2722 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002723 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724get_script_local_ht(void)
2725{
2726 scid_T sid = current_sctx.sc_sid;
2727
Bram Moolenaare3d46852020-08-29 13:39:17 +02002728 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 return &SCRIPT_VARS(sid);
2730 return NULL;
2731}
2732
2733/*
2734 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002735 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002737 int
2738lookup_scriptvar(
2739 char_u *name,
2740 size_t len,
2741 void *lvar UNUSED,
2742 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743{
2744 hashtab_T *ht = get_script_local_ht();
2745 char_u buffer[30];
2746 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002747 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 hashitem_T *hi;
2749
2750 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 if (len < sizeof(buffer) - 1)
2753 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002754 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 vim_strncpy(buffer, name, len);
2756 p = buffer;
2757 }
2758 else
2759 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002760 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002762 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 }
2764
2765 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002766 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767
2768 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002769 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2770 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771
2772 if (p != buffer)
2773 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002774 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775}
2776
2777/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002778 * Find the hashtab used for a variable name.
2779 * Return NULL if the name is not valid.
2780 * Set "varname" to the start of name without ':'.
2781 */
2782 hashtab_T *
2783find_var_ht(char_u *name, char_u **varname)
2784{
2785 hashitem_T *hi;
2786 hashtab_T *ht;
2787
2788 if (name[0] == NUL)
2789 return NULL;
2790 if (name[1] != ':')
2791 {
2792 // The name must not start with a colon or #.
2793 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2794 return NULL;
2795 *varname = name;
2796
2797 // "version" is "v:version" in all scopes if scriptversion < 3.
2798 // Same for a few other variables marked with VV_COMPAT.
2799 if (current_sctx.sc_version < 3)
2800 {
2801 hi = hash_find(&compat_hashtab, name);
2802 if (!HASHITEM_EMPTY(hi))
2803 return &compat_hashtab;
2804 }
2805
2806 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 if (ht != NULL)
2808 return ht; // local variable
2809
2810 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002811 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 {
2813 ht = get_script_local_ht();
2814 if (ht != NULL)
2815 return ht;
2816 }
2817
2818 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002819 }
2820 *varname = name + 2;
2821 if (*name == 'g') // global variable
2822 return &globvarht;
2823 // There must be no ':' or '#' in the rest of the name, unless g: is used
2824 if (vim_strchr(name + 2, ':') != NULL
2825 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2826 return NULL;
2827 if (*name == 'b') // buffer variable
2828 return &curbuf->b_vars->dv_hashtab;
2829 if (*name == 'w') // window variable
2830 return &curwin->w_vars->dv_hashtab;
2831 if (*name == 't') // tab page variable
2832 return &curtab->tp_vars->dv_hashtab;
2833 if (*name == 'v') // v: variable
2834 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002835 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002836 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002838 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 if (*name == 'a') // a: function argument
2840 return get_funccal_args_ht();
2841 if (*name == 'l') // l: local function variable
2842 return get_funccal_local_ht();
2843 }
2844 if (*name == 's') // script variable
2845 {
2846 ht = get_script_local_ht();
2847 if (ht != NULL)
2848 return ht;
2849 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002850 return NULL;
2851}
2852
2853/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002854 * Get the string value of a (global/local) variable.
2855 * Note: see tv_get_string() for how long the pointer remains valid.
2856 * Returns NULL when it doesn't exist.
2857 */
2858 char_u *
2859get_var_value(char_u *name)
2860{
2861 dictitem_T *v;
2862
2863 v = find_var(name, NULL, FALSE);
2864 if (v == NULL)
2865 return NULL;
2866 return tv_get_string(&v->di_tv);
2867}
2868
2869/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002870 * Allocate a new hashtab for a sourced script. It will be used while
2871 * sourcing this script and when executing functions defined in the script.
2872 */
2873 void
2874new_script_vars(scid_T id)
2875{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002876 scriptvar_T *sv;
2877
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002878 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2879 if (sv == NULL)
2880 return;
2881 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002882 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002883}
2884
2885/*
2886 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2887 * point to it.
2888 */
2889 void
2890init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2891{
2892 hash_init(&dict->dv_hashtab);
2893 dict->dv_lock = 0;
2894 dict->dv_scope = scope;
2895 dict->dv_refcount = DO_NOT_FREE_CNT;
2896 dict->dv_copyID = 0;
2897 dict_var->di_tv.vval.v_dict = dict;
2898 dict_var->di_tv.v_type = VAR_DICT;
2899 dict_var->di_tv.v_lock = VAR_FIXED;
2900 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2901 dict_var->di_key[0] = NUL;
2902}
2903
2904/*
2905 * Unreference a dictionary initialized by init_var_dict().
2906 */
2907 void
2908unref_var_dict(dict_T *dict)
2909{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002910 // Now the dict needs to be freed if no one else is using it, go back to
2911 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002912 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2913 dict_unref(dict);
2914}
2915
2916/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002917 * Clean up a list of internal variables.
2918 * Frees all allocated variables and the value they contain.
2919 * Clears hashtab "ht", does not free it.
2920 */
2921 void
2922vars_clear(hashtab_T *ht)
2923{
2924 vars_clear_ext(ht, TRUE);
2925}
2926
2927/*
2928 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2929 */
2930 void
2931vars_clear_ext(hashtab_T *ht, int free_val)
2932{
2933 int todo;
2934 hashitem_T *hi;
2935 dictitem_T *v;
2936
2937 hash_lock(ht);
2938 todo = (int)ht->ht_used;
2939 for (hi = ht->ht_array; todo > 0; ++hi)
2940 {
2941 if (!HASHITEM_EMPTY(hi))
2942 {
2943 --todo;
2944
2945 // Free the variable. Don't remove it from the hashtab,
2946 // ht_array might change then. hash_clear() takes care of it
2947 // later.
2948 v = HI2DI(hi);
2949 if (free_val)
2950 clear_tv(&v->di_tv);
2951 if (v->di_flags & DI_FLAGS_ALLOC)
2952 vim_free(v);
2953 }
2954 }
2955 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002956 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002957}
2958
2959/*
2960 * Delete a variable from hashtab "ht" at item "hi".
2961 * Clear the variable value and free the dictitem.
2962 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002963 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002964delete_var(hashtab_T *ht, hashitem_T *hi)
2965{
2966 dictitem_T *di = HI2DI(hi);
2967
2968 hash_remove(ht, hi);
2969 clear_tv(&di->di_tv);
2970 vim_free(di);
2971}
2972
2973/*
2974 * List the value of one internal variable.
2975 */
2976 static void
2977list_one_var(dictitem_T *v, char *prefix, int *first)
2978{
2979 char_u *tofree;
2980 char_u *s;
2981 char_u numbuf[NUMBUFLEN];
2982
2983 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2984 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2985 s == NULL ? (char_u *)"" : s, first);
2986 vim_free(tofree);
2987}
2988
2989 static void
2990list_one_var_a(
2991 char *prefix,
2992 char_u *name,
2993 int type,
2994 char_u *string,
2995 int *first) // when TRUE clear rest of screen and set to FALSE
2996{
2997 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2998 msg_start();
2999 msg_puts(prefix);
3000 if (name != NULL) // "a:" vars don't have a name stored
3001 msg_puts((char *)name);
3002 msg_putchar(' ');
3003 msg_advance(22);
3004 if (type == VAR_NUMBER)
3005 msg_putchar('#');
3006 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3007 msg_putchar('*');
3008 else if (type == VAR_LIST)
3009 {
3010 msg_putchar('[');
3011 if (*string == '[')
3012 ++string;
3013 }
3014 else if (type == VAR_DICT)
3015 {
3016 msg_putchar('{');
3017 if (*string == '{')
3018 ++string;
3019 }
3020 else
3021 msg_putchar(' ');
3022
3023 msg_outtrans(string);
3024
3025 if (type == VAR_FUNC || type == VAR_PARTIAL)
3026 msg_puts("()");
3027 if (*first)
3028 {
3029 msg_clr_eos();
3030 *first = FALSE;
3031 }
3032}
3033
3034/*
3035 * Set variable "name" to value in "tv".
3036 * If the variable already exists, the value is updated.
3037 * Otherwise the variable is created.
3038 */
3039 void
3040set_var(
3041 char_u *name,
3042 typval_T *tv,
3043 int copy) // make copy of value in "tv"
3044{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003045 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003046}
3047
3048/*
3049 * Set variable "name" to value in "tv".
3050 * If the variable already exists and "is_const" is FALSE the value is updated.
3051 * Otherwise the variable is created.
3052 */
3053 void
3054set_var_const(
3055 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003057 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003058 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003059 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003060{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003061 typval_T *tv = tv_arg;
3062 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003064 char_u *varname;
3065 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003067 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003068
3069 ht = find_var_ht(name, &varname);
3070 if (ht == NULL || *varname == NUL)
3071 {
3072 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003073 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003074 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003075 is_script_local = ht == get_script_local_ht();
3076
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003077 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003078 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003079 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003080 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003081 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003082 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003083 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003084 }
3085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003087
3088 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 if (di == NULL)
3090 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003091
3092 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003093 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003094 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003095
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003096 if (need_convert_to_bool(type, tv))
3097 {
3098 // Destination is a bool and the value is not, but it can be converted.
3099 CLEAR_FIELD(bool_tv);
3100 bool_tv.v_type = VAR_BOOL;
3101 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3102 tv = &bool_tv;
3103 }
3104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003106 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003108 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003109 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 {
3111 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003112 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 }
3114
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003115 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003117 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003118 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003119 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003120 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003121 }
3122
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003123 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003124 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003125 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003127
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003128 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003129 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003130 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 else
3132 // can only redefine once
3133 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003134
3135 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003139 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003140 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003142 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003144 if (copy || tv->v_type != VAR_STRING)
3145 {
3146 char_u *val = tv_get_string(tv);
3147
3148 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003149 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 if (di->di_tv.vval.v_string == NULL)
3151 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003152 }
3153 else
3154 {
3155 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003157 tv->vval.v_string = NULL;
3158 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003159 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003164 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003166#ifdef FEAT_SEARCH_EXTRA
3167 else if (STRCMP(varname, "hlsearch") == 0)
3168 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003170 redraw_all_later(SOME_VALID);
3171 }
3172#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003173 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003176 {
3177 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003178 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003179 }
3180 }
3181
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003183 }
3184 else // add a new variable
3185 {
3186 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003187 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003188 {
3189 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003190 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191 }
3192
Bram Moolenaar03290b82020-12-19 16:30:44 +01003193 // Make sure the variable name is valid. In Vim9 script an autoload
3194 // variable must be prefixed with "g:".
3195 if (!valid_varname(varname, !vim9script
3196 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003197 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3200 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003201 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 STRCPY(di->di_key, varname);
3203 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003204 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003206 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003207 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003209 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 di->di_flags |= DI_FLAGS_LOCK;
3211
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003212 // A Vim9 script-local variable is also added to sn_all_vars and
3213 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003214 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003215 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003216 }
3217
3218 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003220 else
3221 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003222 di->di_tv = *tv;
3223 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003224 init_tv(tv);
3225 }
3226
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003227 // ":const var = val" locks the value
3228 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003229 // Like :lockvar! name: lock the value and what it contains, but only
3230 // if the reference count is up to one. That locks only literal
3231 // values.
3232 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003233 return;
3234failed:
3235 if (!copy)
3236 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003237}
3238
3239/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003240 * Check in this order for backwards compatibility:
3241 * - Whether the variable is read-only
3242 * - Whether the variable value is locked
3243 * - Whether the variable is locked
3244 */
3245 int
3246var_check_permission(dictitem_T *di, char_u *name)
3247{
3248 if (var_check_ro(di->di_flags, name, FALSE)
3249 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3250 || var_check_lock(di->di_flags, name, FALSE))
3251 return FAIL;
3252 return OK;
3253}
3254
3255/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003256 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3257 * Also give an error message.
3258 */
3259 int
3260var_check_ro(int flags, char_u *name, int use_gettext)
3261{
3262 if (flags & DI_FLAGS_RO)
3263 {
3264 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3265 return TRUE;
3266 }
3267 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3268 {
3269 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3270 return TRUE;
3271 }
3272 return FALSE;
3273}
3274
3275/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003276 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3277 * Also give an error message.
3278 */
3279 int
3280var_check_lock(int flags, char_u *name, int use_gettext)
3281{
3282 if (flags & DI_FLAGS_LOCK)
3283 {
3284 semsg(_(e_variable_is_locked_str),
3285 use_gettext ? (char_u *)_(name) : name);
3286 return TRUE;
3287 }
3288 return FALSE;
3289}
3290
3291/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003292 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3293 * Also give an error message.
3294 */
3295 int
3296var_check_fixed(int flags, char_u *name, int use_gettext)
3297{
3298 if (flags & DI_FLAGS_FIX)
3299 {
3300 semsg(_("E795: Cannot delete variable %s"),
3301 use_gettext ? (char_u *)_(name) : name);
3302 return TRUE;
3303 }
3304 return FALSE;
3305}
3306
3307/*
3308 * Check if a funcref is assigned to a valid variable name.
3309 * Return TRUE and give an error if not.
3310 */
3311 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003312var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003313 char_u *name, // points to start of variable name
3314 int new_var) // TRUE when creating the variable
3315{
3316 // Allow for w: b: s: and t:.
3317 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3318 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3319 ? name[2] : name[0]))
3320 {
3321 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3322 name);
3323 return TRUE;
3324 }
3325 // Don't allow hiding a function. When "v" is not NULL we might be
3326 // assigning another function to the same var, the type is checked
3327 // below.
3328 if (new_var && function_exists(name, FALSE))
3329 {
3330 semsg(_("E705: Variable name conflicts with existing function: %s"),
3331 name);
3332 return TRUE;
3333 }
3334 return FALSE;
3335}
3336
3337/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003338 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3339 * value. Also give an error message, using "name" or _("name") when
3340 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003341 */
3342 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003343value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003344{
3345 if (lock & VAR_LOCKED)
3346 {
3347 semsg(_("E741: Value is locked: %s"),
3348 name == NULL ? (char_u *)_("Unknown")
3349 : use_gettext ? (char_u *)_(name)
3350 : name);
3351 return TRUE;
3352 }
3353 if (lock & VAR_FIXED)
3354 {
3355 semsg(_("E742: Cannot change value of %s"),
3356 name == NULL ? (char_u *)_("Unknown")
3357 : use_gettext ? (char_u *)_(name)
3358 : name);
3359 return TRUE;
3360 }
3361 return FALSE;
3362}
3363
3364/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003365 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003366 * Return FALSE and give an error if not.
3367 */
3368 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003369valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003370{
3371 char_u *p;
3372
3373 for (p = varname; *p != NUL; ++p)
3374 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003375 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003376 {
3377 semsg(_(e_illvar), varname);
3378 return FALSE;
3379 }
3380 return TRUE;
3381}
3382
3383/*
3384 * getwinvar() and gettabwinvar()
3385 */
3386 static void
3387getwinvar(
3388 typval_T *argvars,
3389 typval_T *rettv,
3390 int off) // 1 for gettabwinvar()
3391{
3392 win_T *win;
3393 char_u *varname;
3394 dictitem_T *v;
3395 tabpage_T *tp = NULL;
3396 int done = FALSE;
3397 win_T *oldcurwin;
3398 tabpage_T *oldtabpage;
3399 int need_switch_win;
3400
3401 if (off == 1)
3402 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3403 else
3404 tp = curtab;
3405 win = find_win_by_nr(&argvars[off], tp);
3406 varname = tv_get_string_chk(&argvars[off + 1]);
3407 ++emsg_off;
3408
3409 rettv->v_type = VAR_STRING;
3410 rettv->vval.v_string = NULL;
3411
3412 if (win != NULL && varname != NULL)
3413 {
3414 // Set curwin to be our win, temporarily. Also set the tabpage,
3415 // otherwise the window is not valid. Only do this when needed,
3416 // autocommands get blocked.
3417 need_switch_win = !(tp == curtab && win == curwin);
3418 if (!need_switch_win
3419 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3420 {
3421 if (*varname == '&')
3422 {
3423 if (varname[1] == NUL)
3424 {
3425 // get all window-local options in a dict
3426 dict_T *opts = get_winbuf_options(FALSE);
3427
3428 if (opts != NULL)
3429 {
3430 rettv_dict_set(rettv, opts);
3431 done = TRUE;
3432 }
3433 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003434 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003435 // window-local-option
3436 done = TRUE;
3437 }
3438 else
3439 {
3440 // Look up the variable.
3441 // Let getwinvar({nr}, "") return the "w:" dictionary.
3442 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3443 varname, FALSE);
3444 if (v != NULL)
3445 {
3446 copy_tv(&v->di_tv, rettv);
3447 done = TRUE;
3448 }
3449 }
3450 }
3451
3452 if (need_switch_win)
3453 // restore previous notion of curwin
3454 restore_win(oldcurwin, oldtabpage, TRUE);
3455 }
3456
3457 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3458 // use the default return value
3459 copy_tv(&argvars[off + 2], rettv);
3460
3461 --emsg_off;
3462}
3463
3464/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003465 * Set option "varname" to the value of "varp" for the current buffer/window.
3466 */
3467 static void
3468set_option_from_tv(char_u *varname, typval_T *varp)
3469{
3470 long numval = 0;
3471 char_u *strval;
3472 char_u nbuf[NUMBUFLEN];
3473 int error = FALSE;
3474
3475 if (!in_vim9script() || varp->v_type != VAR_STRING)
3476 numval = (long)tv_get_number_chk(varp, &error);
3477 strval = tv_get_string_buf_chk(varp, nbuf);
3478 if (!error && strval != NULL)
3479 set_option_value(varname, numval, strval, OPT_LOCAL);
3480}
3481
3482/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003483 * "setwinvar()" and "settabwinvar()" functions
3484 */
3485 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003486setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003487{
3488 win_T *win;
3489 win_T *save_curwin;
3490 tabpage_T *save_curtab;
3491 int need_switch_win;
3492 char_u *varname, *winvarname;
3493 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003494 tabpage_T *tp = NULL;
3495
3496 if (check_secure())
3497 return;
3498
3499 if (off == 1)
3500 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3501 else
3502 tp = curtab;
3503 win = find_win_by_nr(&argvars[off], tp);
3504 varname = tv_get_string_chk(&argvars[off + 1]);
3505 varp = &argvars[off + 2];
3506
3507 if (win != NULL && varname != NULL && varp != NULL)
3508 {
3509 need_switch_win = !(tp == curtab && win == curwin);
3510 if (!need_switch_win
3511 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3512 {
3513 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003514 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003515 else
3516 {
3517 winvarname = alloc(STRLEN(varname) + 3);
3518 if (winvarname != NULL)
3519 {
3520 STRCPY(winvarname, "w:");
3521 STRCPY(winvarname + 2, varname);
3522 set_var(winvarname, varp, TRUE);
3523 vim_free(winvarname);
3524 }
3525 }
3526 }
3527 if (need_switch_win)
3528 restore_win(save_curwin, save_curtab, TRUE);
3529 }
3530}
3531
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003532/*
3533 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3534 * v:option_type, and v:option_command.
3535 */
3536 void
3537reset_v_option_vars(void)
3538{
3539 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3540 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3541 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3542 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3543 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3544 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3545}
3546
3547/*
3548 * Add an assert error to v:errors.
3549 */
3550 void
3551assert_error(garray_T *gap)
3552{
3553 struct vimvar *vp = &vimvars[VV_ERRORS];
3554
3555 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003556 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003557 set_vim_var_list(VV_ERRORS, list_alloc());
3558 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3559}
3560
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003561 int
3562var_exists(char_u *var)
3563{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003564 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003565 char_u *name;
3566 char_u *tofree;
3567 typval_T tv;
3568 int len = 0;
3569 int n = FALSE;
3570
3571 // get_name_len() takes care of expanding curly braces
3572 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003573 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003574 if (len > 0)
3575 {
3576 if (tofree != NULL)
3577 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003578 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003579 if (n)
3580 {
3581 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003582 arg = skipwhite(arg);
3583 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003584 if (n)
3585 clear_tv(&tv);
3586 }
3587 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003588 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003589 n = FALSE;
3590
3591 vim_free(tofree);
3592 return n;
3593}
3594
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003595static lval_T *redir_lval = NULL;
3596#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3597static garray_T redir_ga; // only valid when redir_lval is not NULL
3598static char_u *redir_endp = NULL;
3599static char_u *redir_varname = NULL;
3600
3601/*
3602 * Start recording command output to a variable
3603 * When "append" is TRUE append to an existing variable.
3604 * Returns OK if successfully completed the setup. FAIL otherwise.
3605 */
3606 int
3607var_redir_start(char_u *name, int append)
3608{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003609 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003610 typval_T tv;
3611
3612 // Catch a bad name early.
3613 if (!eval_isnamec1(*name))
3614 {
3615 emsg(_(e_invarg));
3616 return FAIL;
3617 }
3618
3619 // Make a copy of the name, it is used in redir_lval until redir ends.
3620 redir_varname = vim_strsave(name);
3621 if (redir_varname == NULL)
3622 return FAIL;
3623
3624 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3625 if (redir_lval == NULL)
3626 {
3627 var_redir_stop();
3628 return FAIL;
3629 }
3630
3631 // The output is stored in growarray "redir_ga" until redirection ends.
3632 ga_init2(&redir_ga, (int)sizeof(char), 500);
3633
3634 // Parse the variable name (can be a dict or list entry).
3635 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3636 FNE_CHECK_START);
3637 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3638 {
3639 clear_lval(redir_lval);
3640 if (redir_endp != NULL && *redir_endp != NUL)
3641 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003642 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003643 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003644 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003645 redir_endp = NULL; // don't store a value, only cleanup
3646 var_redir_stop();
3647 return FAIL;
3648 }
3649
3650 // check if we can write to the variable: set it to or append an empty
3651 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003652 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003653 tv.v_type = VAR_STRING;
3654 tv.vval.v_string = (char_u *)"";
3655 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003656 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3657 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003658 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003659 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3660 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003661 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003662 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003663 {
3664 redir_endp = NULL; // don't store a value, only cleanup
3665 var_redir_stop();
3666 return FAIL;
3667 }
3668
3669 return OK;
3670}
3671
3672/*
3673 * Append "value[value_len]" to the variable set by var_redir_start().
3674 * The actual appending is postponed until redirection ends, because the value
3675 * appended may in fact be the string we write to, changing it may cause freed
3676 * memory to be used:
3677 * :redir => foo
3678 * :let foo
3679 * :redir END
3680 */
3681 void
3682var_redir_str(char_u *value, int value_len)
3683{
3684 int len;
3685
3686 if (redir_lval == NULL)
3687 return;
3688
3689 if (value_len == -1)
3690 len = (int)STRLEN(value); // Append the entire string
3691 else
3692 len = value_len; // Append only "value_len" characters
3693
3694 if (ga_grow(&redir_ga, len) == OK)
3695 {
3696 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3697 redir_ga.ga_len += len;
3698 }
3699 else
3700 var_redir_stop();
3701}
3702
3703/*
3704 * Stop redirecting command output to a variable.
3705 * Frees the allocated memory.
3706 */
3707 void
3708var_redir_stop(void)
3709{
3710 typval_T tv;
3711
3712 if (EVALCMD_BUSY)
3713 {
3714 redir_lval = NULL;
3715 return;
3716 }
3717
3718 if (redir_lval != NULL)
3719 {
3720 // If there was no error: assign the text to the variable.
3721 if (redir_endp != NULL)
3722 {
3723 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3724 tv.v_type = VAR_STRING;
3725 tv.vval.v_string = redir_ga.ga_data;
3726 // Call get_lval() again, if it's inside a Dict or List it may
3727 // have changed.
3728 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3729 FALSE, FALSE, 0, FNE_CHECK_START);
3730 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003732 (char_u *)".");
3733 clear_lval(redir_lval);
3734 }
3735
3736 // free the collected output
3737 VIM_CLEAR(redir_ga.ga_data);
3738
3739 VIM_CLEAR(redir_lval);
3740 }
3741 VIM_CLEAR(redir_varname);
3742}
3743
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003744/*
3745 * "gettabvar()" function
3746 */
3747 void
3748f_gettabvar(typval_T *argvars, typval_T *rettv)
3749{
3750 win_T *oldcurwin;
3751 tabpage_T *tp, *oldtabpage;
3752 dictitem_T *v;
3753 char_u *varname;
3754 int done = FALSE;
3755
3756 rettv->v_type = VAR_STRING;
3757 rettv->vval.v_string = NULL;
3758
3759 varname = tv_get_string_chk(&argvars[1]);
3760 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3761 if (tp != NULL && varname != NULL)
3762 {
3763 // Set tp to be our tabpage, temporarily. Also set the window to the
3764 // first window in the tabpage, otherwise the window is not valid.
3765 if (switch_win(&oldcurwin, &oldtabpage,
3766 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3767 : tp->tp_firstwin, tp, TRUE) == OK)
3768 {
3769 // look up the variable
3770 // Let gettabvar({nr}, "") return the "t:" dictionary.
3771 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3772 if (v != NULL)
3773 {
3774 copy_tv(&v->di_tv, rettv);
3775 done = TRUE;
3776 }
3777 }
3778
3779 // restore previous notion of curwin
3780 restore_win(oldcurwin, oldtabpage, TRUE);
3781 }
3782
3783 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3784 copy_tv(&argvars[2], rettv);
3785}
3786
3787/*
3788 * "gettabwinvar()" function
3789 */
3790 void
3791f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3792{
3793 getwinvar(argvars, rettv, 1);
3794}
3795
3796/*
3797 * "getwinvar()" function
3798 */
3799 void
3800f_getwinvar(typval_T *argvars, typval_T *rettv)
3801{
3802 getwinvar(argvars, rettv, 0);
3803}
3804
3805/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003806 * "getbufvar()" function
3807 */
3808 void
3809f_getbufvar(typval_T *argvars, typval_T *rettv)
3810{
3811 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003812 char_u *varname;
3813 dictitem_T *v;
3814 int done = FALSE;
3815
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003816 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003817 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003818
3819 rettv->v_type = VAR_STRING;
3820 rettv->vval.v_string = NULL;
3821
3822 if (buf != NULL && varname != NULL)
3823 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003824 if (*varname == '&')
3825 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003826 buf_T *save_curbuf = curbuf;
3827
3828 // set curbuf to be our buf, temporarily
3829 curbuf = buf;
3830
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003831 if (varname[1] == NUL)
3832 {
3833 // get all buffer-local options in a dict
3834 dict_T *opts = get_winbuf_options(TRUE);
3835
3836 if (opts != NULL)
3837 {
3838 rettv_dict_set(rettv, opts);
3839 done = TRUE;
3840 }
3841 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003842 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003843 // buffer-local-option
3844 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003845
3846 // restore previous notion of curbuf
3847 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003848 }
3849 else
3850 {
3851 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003852 if (*varname == NUL)
3853 // Let getbufvar({nr}, "") return the "b:" dictionary.
3854 v = &buf->b_bufvar;
3855 else
3856 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3857 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003858 if (v != NULL)
3859 {
3860 copy_tv(&v->di_tv, rettv);
3861 done = TRUE;
3862 }
3863 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003864 }
3865
3866 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3867 // use the default value
3868 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003869}
3870
3871/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003872 * "settabvar()" function
3873 */
3874 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003875f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003876{
3877 tabpage_T *save_curtab;
3878 tabpage_T *tp;
3879 char_u *varname, *tabvarname;
3880 typval_T *varp;
3881
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003882 if (check_secure())
3883 return;
3884
3885 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3886 varname = tv_get_string_chk(&argvars[1]);
3887 varp = &argvars[2];
3888
3889 if (varname != NULL && varp != NULL && tp != NULL)
3890 {
3891 save_curtab = curtab;
3892 goto_tabpage_tp(tp, FALSE, FALSE);
3893
3894 tabvarname = alloc(STRLEN(varname) + 3);
3895 if (tabvarname != NULL)
3896 {
3897 STRCPY(tabvarname, "t:");
3898 STRCPY(tabvarname + 2, varname);
3899 set_var(tabvarname, varp, TRUE);
3900 vim_free(tabvarname);
3901 }
3902
3903 // Restore current tabpage
3904 if (valid_tabpage(save_curtab))
3905 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3906 }
3907}
3908
3909/*
3910 * "settabwinvar()" function
3911 */
3912 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003913f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003914{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003915 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003916}
3917
3918/*
3919 * "setwinvar()" function
3920 */
3921 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003922f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003923{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003924 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003925}
3926
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003927/*
3928 * "setbufvar()" function
3929 */
3930 void
3931f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3932{
3933 buf_T *buf;
3934 char_u *varname, *bufvarname;
3935 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003936
3937 if (check_secure())
3938 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003939 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003940 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003941 varp = &argvars[2];
3942
3943 if (buf != NULL && varname != NULL && varp != NULL)
3944 {
3945 if (*varname == '&')
3946 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003947 aco_save_T aco;
3948
3949 // set curbuf to be our buf, temporarily
3950 aucmd_prepbuf(&aco, buf);
3951
Bram Moolenaar191929b2020-08-19 21:20:49 +02003952 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003953
3954 // reset notion of buffer
3955 aucmd_restbuf(&aco);
3956 }
3957 else
3958 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003959 bufvarname = alloc(STRLEN(varname) + 3);
3960 if (bufvarname != NULL)
3961 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003962 buf_T *save_curbuf = curbuf;
3963
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003964 curbuf = buf;
3965 STRCPY(bufvarname, "b:");
3966 STRCPY(bufvarname + 2, varname);
3967 set_var(bufvarname, varp, TRUE);
3968 vim_free(bufvarname);
3969 curbuf = save_curbuf;
3970 }
3971 }
3972 }
3973}
3974
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003975/*
3976 * Get a callback from "arg". It can be a Funcref or a function name.
3977 * When "arg" is zero return an empty string.
3978 * "cb_name" is not allocated.
3979 * "cb_name" is set to NULL for an invalid argument.
3980 */
3981 callback_T
3982get_callback(typval_T *arg)
3983{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003984 callback_T res;
3985 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003986
3987 res.cb_free_name = FALSE;
3988 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3989 {
3990 res.cb_partial = arg->vval.v_partial;
3991 ++res.cb_partial->pt_refcount;
3992 res.cb_name = partial_name(res.cb_partial);
3993 }
3994 else
3995 {
3996 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003997 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3998 && isdigit(*arg->vval.v_string))
3999 r = FAIL;
4000 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004001 {
4002 // Note that we don't make a copy of the string.
4003 res.cb_name = arg->vval.v_string;
4004 func_ref(res.cb_name);
4005 }
4006 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004007 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004008 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004009 r = FAIL;
4010
4011 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004012 {
4013 emsg(_("E921: Invalid callback argument"));
4014 res.cb_name = NULL;
4015 }
4016 }
4017 return res;
4018}
4019
4020/*
4021 * Copy a callback into a typval_T.
4022 */
4023 void
4024put_callback(callback_T *cb, typval_T *tv)
4025{
4026 if (cb->cb_partial != NULL)
4027 {
4028 tv->v_type = VAR_PARTIAL;
4029 tv->vval.v_partial = cb->cb_partial;
4030 ++tv->vval.v_partial->pt_refcount;
4031 }
4032 else
4033 {
4034 tv->v_type = VAR_FUNC;
4035 tv->vval.v_string = vim_strsave(cb->cb_name);
4036 func_ref(cb->cb_name);
4037 }
4038}
4039
4040/*
4041 * Make a copy of "src" into "dest", allocating the function name if needed,
4042 * without incrementing the refcount.
4043 */
4044 void
4045set_callback(callback_T *dest, callback_T *src)
4046{
4047 if (src->cb_partial == NULL)
4048 {
4049 // just a function name, make a copy
4050 dest->cb_name = vim_strsave(src->cb_name);
4051 dest->cb_free_name = TRUE;
4052 }
4053 else
4054 {
4055 // cb_name is a pointer into cb_partial
4056 dest->cb_name = src->cb_name;
4057 dest->cb_free_name = FALSE;
4058 }
4059 dest->cb_partial = src->cb_partial;
4060}
4061
4062/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004063 * Copy callback from "src" to "dest", incrementing the refcounts.
4064 */
4065 void
4066copy_callback(callback_T *dest, callback_T *src)
4067{
4068 dest->cb_partial = src->cb_partial;
4069 if (dest->cb_partial != NULL)
4070 {
4071 dest->cb_name = src->cb_name;
4072 dest->cb_free_name = FALSE;
4073 ++dest->cb_partial->pt_refcount;
4074 }
4075 else
4076 {
4077 dest->cb_name = vim_strsave(src->cb_name);
4078 dest->cb_free_name = TRUE;
4079 func_ref(src->cb_name);
4080 }
4081}
4082
4083/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004084 * Unref/free "callback" returned by get_callback() or set_callback().
4085 */
4086 void
4087free_callback(callback_T *callback)
4088{
4089 if (callback->cb_partial != NULL)
4090 {
4091 partial_unref(callback->cb_partial);
4092 callback->cb_partial = NULL;
4093 }
4094 else if (callback->cb_name != NULL)
4095 func_unref(callback->cb_name);
4096 if (callback->cb_free_name)
4097 {
4098 vim_free(callback->cb_name);
4099 callback->cb_free_name = FALSE;
4100 }
4101 callback->cb_name = NULL;
4102}
4103
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004104#endif // FEAT_EVAL