blob: dea56be75facf92233b30dc80ba5a23e40abbe03 [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
1467 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001468 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001469 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 if (endchars != NULL && vim_strchr(endchars,
1471 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 emsg(_(e_letunexp));
1473 else
1474 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476 arg_end = p;
1477 }
1478 }
1479 clear_lval(&lv);
1480 }
1481
1482 else
1483 semsg(_(e_invarg2), arg);
1484
1485 return arg_end;
1486}
1487
1488/*
1489 * ":unlet[!] var1 ... " command.
1490 */
1491 void
1492ex_unlet(exarg_T *eap)
1493{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001494 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001495}
1496
1497/*
1498 * ":lockvar" and ":unlockvar" commands
1499 */
1500 void
1501ex_lockvar(exarg_T *eap)
1502{
1503 char_u *arg = eap->arg;
1504 int deep = 2;
1505
1506 if (eap->forceit)
1507 deep = -1;
1508 else if (vim_isdigit(*arg))
1509 {
1510 deep = getdigits(&arg);
1511 arg = skipwhite(arg);
1512 }
1513
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001514 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001515}
1516
1517/*
1518 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001519 * Also used for Vim9 script. "callback" is invoked as:
1520 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001522 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001523ex_unletlock(
1524 exarg_T *eap,
1525 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001526 int deep,
1527 int glv_flags,
1528 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1529 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001530{
1531 char_u *arg = argstart;
1532 char_u *name_end;
1533 int error = FALSE;
1534 lval_T lv;
1535
1536 do
1537 {
1538 if (*arg == '$')
1539 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001540 lv.ll_name = arg;
1541 lv.ll_tv = NULL;
1542 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 if (get_env_len(&arg) == 0)
1544 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001545 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546 return;
1547 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001548 if (!error && !eap->skip
1549 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1550 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001551 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001553 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001554 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001555 // Parse the name and find the end.
1556 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1557 glv_flags, FNE_CHECK_START);
1558 if (lv.ll_name == NULL)
1559 error = TRUE; // error but continue parsing
1560 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1561 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001562 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001563 if (name_end != NULL)
1564 {
1565 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001566 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001567 }
1568 if (!(eap->skip || error))
1569 clear_lval(&lv);
1570 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001571 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001573 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001574 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001575 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001576
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001577 if (!eap->skip)
1578 clear_lval(&lv);
1579 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001580
1581 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001582 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583
1584 eap->nextcmd = check_nextcmd(arg);
1585}
1586
1587 static int
1588do_unlet_var(
1589 lval_T *lp,
1590 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001591 exarg_T *eap,
1592 int deep UNUSED,
1593 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001595 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 int ret = OK;
1597 int cc;
1598
1599 if (lp->ll_tv == NULL)
1600 {
1601 cc = *name_end;
1602 *name_end = NUL;
1603
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001604 // Environment variable, normal name or expanded name.
1605 if (*lp->ll_name == '$')
1606 vim_unsetenv(lp->ll_name + 1);
1607 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001608 ret = FAIL;
1609 *name_end = cc;
1610 }
1611 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001612 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001614 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001615 return FAIL;
1616 else if (lp->ll_range)
1617 {
1618 listitem_T *li;
1619 listitem_T *ll_li = lp->ll_li;
1620 int ll_n1 = lp->ll_n1;
1621
1622 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1623 {
1624 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001625 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001626 return FAIL;
1627 ll_li = li;
1628 ++ll_n1;
1629 }
1630
1631 // Delete a range of List items.
1632 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1633 {
1634 li = lp->ll_li->li_next;
1635 listitem_remove(lp->ll_list, lp->ll_li);
1636 lp->ll_li = li;
1637 ++lp->ll_n1;
1638 }
1639 }
1640 else
1641 {
1642 if (lp->ll_list != NULL)
1643 // unlet a List item.
1644 listitem_remove(lp->ll_list, lp->ll_li);
1645 else
1646 // unlet a Dictionary item.
1647 dictitem_remove(lp->ll_dict, lp->ll_di);
1648 }
1649
1650 return ret;
1651}
1652
1653/*
1654 * "unlet" a variable. Return OK if it existed, FAIL if not.
1655 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1656 */
1657 int
1658do_unlet(char_u *name, int forceit)
1659{
1660 hashtab_T *ht;
1661 hashitem_T *hi;
1662 char_u *varname;
1663 dict_T *d;
1664 dictitem_T *di;
1665
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001666 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001667 return FAIL;
1668
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669 ht = find_var_ht(name, &varname);
1670 if (ht != NULL && *varname != NUL)
1671 {
1672 d = get_current_funccal_dict(ht);
1673 if (d == NULL)
1674 {
1675 if (ht == &globvarht)
1676 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001677 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001678 d = &vimvardict;
1679 else
1680 {
1681 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1682 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1683 }
1684 if (d == NULL)
1685 {
1686 internal_error("do_unlet()");
1687 return FAIL;
1688 }
1689 }
1690 hi = hash_find(ht, varname);
1691 if (HASHITEM_EMPTY(hi))
1692 hi = find_hi_in_scoped_ht(name, &ht);
1693 if (hi != NULL && !HASHITEM_EMPTY(hi))
1694 {
1695 di = HI2DI(hi);
1696 if (var_check_fixed(di->di_flags, name, FALSE)
1697 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001698 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001699 return FAIL;
1700
1701 delete_var(ht, hi);
1702 return OK;
1703 }
1704 }
1705 if (forceit)
1706 return OK;
1707 semsg(_("E108: No such variable: \"%s\""), name);
1708 return FAIL;
1709}
1710
1711/*
1712 * Lock or unlock variable indicated by "lp".
1713 * "deep" is the levels to go (-1 for unlimited);
1714 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1715 */
1716 static int
1717do_lock_var(
1718 lval_T *lp,
1719 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001720 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001721 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001722 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001723{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001724 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001725 int ret = OK;
1726 int cc;
1727 dictitem_T *di;
1728
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001729 if (lp->ll_tv == NULL)
1730 {
1731 cc = *name_end;
1732 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001733 if (*lp->ll_name == '$')
1734 {
1735 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001736 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001737 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001738 else
1739 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001740 // Normal name or expanded name.
1741 di = find_var(lp->ll_name, NULL, TRUE);
1742 if (di == NULL)
1743 ret = FAIL;
1744 else if ((di->di_flags & DI_FLAGS_FIX)
1745 && di->di_tv.v_type != VAR_DICT
1746 && di->di_tv.v_type != VAR_LIST)
1747 {
1748 // For historic reasons this error is not given for a list or
1749 // dict. E.g., the b: dict could be locked/unlocked.
1750 semsg(_(e_lock_unlock), lp->ll_name);
1751 ret = FAIL;
1752 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001753 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001754 {
1755 if (lock)
1756 di->di_flags |= DI_FLAGS_LOCK;
1757 else
1758 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001759 if (deep != 0)
1760 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001761 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001762 }
1763 *name_end = cc;
1764 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001765 else if (deep == 0)
1766 {
1767 // nothing to do
1768 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001769 else if (lp->ll_range)
1770 {
1771 listitem_T *li = lp->ll_li;
1772
1773 // (un)lock a range of List items.
1774 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1775 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001776 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001777 li = li->li_next;
1778 ++lp->ll_n1;
1779 }
1780 }
1781 else if (lp->ll_list != NULL)
1782 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001783 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001784 else
1785 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001786 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787
1788 return ret;
1789}
1790
1791/*
1792 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001793 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1794 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001795 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001796 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001797item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798{
1799 static int recurse = 0;
1800 list_T *l;
1801 listitem_T *li;
1802 dict_T *d;
1803 blob_T *b;
1804 hashitem_T *hi;
1805 int todo;
1806
1807 if (recurse >= DICT_MAXNEST)
1808 {
1809 emsg(_("E743: variable nested too deep for (un)lock"));
1810 return;
1811 }
1812 if (deep == 0)
1813 return;
1814 ++recurse;
1815
1816 // lock/unlock the item itself
1817 if (lock)
1818 tv->v_lock |= VAR_LOCKED;
1819 else
1820 tv->v_lock &= ~VAR_LOCKED;
1821
1822 switch (tv->v_type)
1823 {
1824 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001825 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001827 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001829 case VAR_STRING:
1830 case VAR_FUNC:
1831 case VAR_PARTIAL:
1832 case VAR_FLOAT:
1833 case VAR_SPECIAL:
1834 case VAR_JOB:
1835 case VAR_CHANNEL:
1836 break;
1837
1838 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001839 if ((b = tv->vval.v_blob) != NULL
1840 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 {
1842 if (lock)
1843 b->bv_lock |= VAR_LOCKED;
1844 else
1845 b->bv_lock &= ~VAR_LOCKED;
1846 }
1847 break;
1848 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001849 if ((l = tv->vval.v_list) != NULL
1850 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001851 {
1852 if (lock)
1853 l->lv_lock |= VAR_LOCKED;
1854 else
1855 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001856 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001857 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001858 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001859 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001860 }
1861 break;
1862 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001863 if ((d = tv->vval.v_dict) != NULL
1864 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001865 {
1866 if (lock)
1867 d->dv_lock |= VAR_LOCKED;
1868 else
1869 d->dv_lock &= ~VAR_LOCKED;
1870 if (deep < 0 || deep > 1)
1871 {
1872 // recursive: lock/unlock the items the List contains
1873 todo = (int)d->dv_hashtab.ht_used;
1874 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1875 {
1876 if (!HASHITEM_EMPTY(hi))
1877 {
1878 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001879 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1880 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001881 }
1882 }
1883 }
1884 }
1885 }
1886 --recurse;
1887}
1888
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001889#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1890/*
1891 * Delete all "menutrans_" variables.
1892 */
1893 void
1894del_menutrans_vars(void)
1895{
1896 hashitem_T *hi;
1897 int todo;
1898
1899 hash_lock(&globvarht);
1900 todo = (int)globvarht.ht_used;
1901 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1902 {
1903 if (!HASHITEM_EMPTY(hi))
1904 {
1905 --todo;
1906 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1907 delete_var(&globvarht, hi);
1908 }
1909 }
1910 hash_unlock(&globvarht);
1911}
1912#endif
1913
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001914/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001915 * Local string buffer for the next two functions to store a variable name
1916 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1917 * get_user_var_name().
1918 */
1919
1920static char_u *varnamebuf = NULL;
1921static int varnamebuflen = 0;
1922
1923/*
1924 * Function to concatenate a prefix and a variable name.
1925 */
1926 static char_u *
1927cat_prefix_varname(int prefix, char_u *name)
1928{
1929 int len;
1930
1931 len = (int)STRLEN(name) + 3;
1932 if (len > varnamebuflen)
1933 {
1934 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001935 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001936 varnamebuf = alloc(len);
1937 if (varnamebuf == NULL)
1938 {
1939 varnamebuflen = 0;
1940 return NULL;
1941 }
1942 varnamebuflen = len;
1943 }
1944 *varnamebuf = prefix;
1945 varnamebuf[1] = ':';
1946 STRCPY(varnamebuf + 2, name);
1947 return varnamebuf;
1948}
1949
1950/*
1951 * Function given to ExpandGeneric() to obtain the list of user defined
1952 * (global/buffer/window/built-in) variable names.
1953 */
1954 char_u *
1955get_user_var_name(expand_T *xp, int idx)
1956{
1957 static long_u gdone;
1958 static long_u bdone;
1959 static long_u wdone;
1960 static long_u tdone;
1961 static int vidx;
1962 static hashitem_T *hi;
1963 hashtab_T *ht;
1964
1965 if (idx == 0)
1966 {
1967 gdone = bdone = wdone = vidx = 0;
1968 tdone = 0;
1969 }
1970
1971 // Global variables
1972 if (gdone < globvarht.ht_used)
1973 {
1974 if (gdone++ == 0)
1975 hi = globvarht.ht_array;
1976 else
1977 ++hi;
1978 while (HASHITEM_EMPTY(hi))
1979 ++hi;
1980 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1981 return cat_prefix_varname('g', hi->hi_key);
1982 return hi->hi_key;
1983 }
1984
1985 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01001986 ht =
1987#ifdef FEAT_CMDWIN
1988 // In cmdwin, the alternative buffer should be used.
1989 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
1990 &prevwin->w_buffer->b_vars->dv_hashtab :
1991#endif
1992 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001993 if (bdone < ht->ht_used)
1994 {
1995 if (bdone++ == 0)
1996 hi = ht->ht_array;
1997 else
1998 ++hi;
1999 while (HASHITEM_EMPTY(hi))
2000 ++hi;
2001 return cat_prefix_varname('b', hi->hi_key);
2002 }
2003
2004 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002005 ht =
2006#ifdef FEAT_CMDWIN
2007 // In cmdwin, the alternative window should be used.
2008 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2009 &prevwin->w_vars->dv_hashtab :
2010#endif
2011 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002012 if (wdone < ht->ht_used)
2013 {
2014 if (wdone++ == 0)
2015 hi = ht->ht_array;
2016 else
2017 ++hi;
2018 while (HASHITEM_EMPTY(hi))
2019 ++hi;
2020 return cat_prefix_varname('w', hi->hi_key);
2021 }
2022
2023 // t: variables
2024 ht = &curtab->tp_vars->dv_hashtab;
2025 if (tdone < ht->ht_used)
2026 {
2027 if (tdone++ == 0)
2028 hi = ht->ht_array;
2029 else
2030 ++hi;
2031 while (HASHITEM_EMPTY(hi))
2032 ++hi;
2033 return cat_prefix_varname('t', hi->hi_key);
2034 }
2035
2036 // v: variables
2037 if (vidx < VV_LEN)
2038 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2039
2040 VIM_CLEAR(varnamebuf);
2041 varnamebuflen = 0;
2042 return NULL;
2043}
2044
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002045 char *
2046get_var_special_name(int nr)
2047{
2048 switch (nr)
2049 {
2050 case VVAL_FALSE: return "v:false";
2051 case VVAL_TRUE: return "v:true";
2052 case VVAL_NONE: return "v:none";
2053 case VVAL_NULL: return "v:null";
2054 }
2055 internal_error("get_var_special_name()");
2056 return "42";
2057}
2058
2059/*
2060 * Returns the global variable dictionary
2061 */
2062 dict_T *
2063get_globvar_dict(void)
2064{
2065 return &globvardict;
2066}
2067
2068/*
2069 * Returns the global variable hash table
2070 */
2071 hashtab_T *
2072get_globvar_ht(void)
2073{
2074 return &globvarht;
2075}
2076
2077/*
2078 * Returns the v: variable dictionary
2079 */
2080 dict_T *
2081get_vimvar_dict(void)
2082{
2083 return &vimvardict;
2084}
2085
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002086/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002088 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 */
2090 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002091find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002093 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2094 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095
2096 if (di == NULL)
2097 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002098 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2100 return (int)(vv - vimvars);
2101}
2102
2103
2104/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002105 * Set type of v: variable to "type".
2106 */
2107 void
2108set_vim_var_type(int idx, vartype_T type)
2109{
2110 vimvars[idx].vv_type = type;
2111}
2112
2113/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002114 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002115 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002116 */
2117 void
2118set_vim_var_nr(int idx, varnumber_T val)
2119{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002120 vimvars[idx].vv_nr = val;
2121}
2122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123 char *
2124get_vim_var_name(int idx)
2125{
2126 return vimvars[idx].vv_name;
2127}
2128
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002129/*
2130 * Get typval_T v: variable value.
2131 */
2132 typval_T *
2133get_vim_var_tv(int idx)
2134{
2135 return &vimvars[idx].vv_tv;
2136}
2137
2138/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002139 * Set v: variable to "tv". Only accepts the same type.
2140 * Takes over the value of "tv".
2141 */
2142 int
2143set_vim_var_tv(int idx, typval_T *tv)
2144{
2145 if (vimvars[idx].vv_type != tv->v_type)
2146 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002147 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002148 clear_tv(tv);
2149 return FAIL;
2150 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002151 // VV_RO is also checked when compiling, but let's check here as well.
2152 if (vimvars[idx].vv_flags & VV_RO)
2153 {
2154 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2155 return FAIL;
2156 }
2157 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2158 {
2159 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2160 return FAIL;
2161 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002162 clear_tv(&vimvars[idx].vv_di.di_tv);
2163 vimvars[idx].vv_di.di_tv = *tv;
2164 return OK;
2165}
2166
2167/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002168 * Get number v: variable value.
2169 */
2170 varnumber_T
2171get_vim_var_nr(int idx)
2172{
2173 return vimvars[idx].vv_nr;
2174}
2175
2176/*
2177 * Get string v: variable value. Uses a static buffer, can only be used once.
2178 * If the String variable has never been set, return an empty string.
2179 * Never returns NULL;
2180 */
2181 char_u *
2182get_vim_var_str(int idx)
2183{
2184 return tv_get_string(&vimvars[idx].vv_tv);
2185}
2186
2187/*
2188 * Get List v: variable value. Caller must take care of reference count when
2189 * needed.
2190 */
2191 list_T *
2192get_vim_var_list(int idx)
2193{
2194 return vimvars[idx].vv_list;
2195}
2196
2197/*
2198 * Get Dict v: variable value. Caller must take care of reference count when
2199 * needed.
2200 */
2201 dict_T *
2202get_vim_var_dict(int idx)
2203{
2204 return vimvars[idx].vv_dict;
2205}
2206
2207/*
2208 * Set v:char to character "c".
2209 */
2210 void
2211set_vim_var_char(int c)
2212{
2213 char_u buf[MB_MAXBYTES + 1];
2214
2215 if (has_mbyte)
2216 buf[(*mb_char2bytes)(c, buf)] = NUL;
2217 else
2218 {
2219 buf[0] = c;
2220 buf[1] = NUL;
2221 }
2222 set_vim_var_string(VV_CHAR, buf, -1);
2223}
2224
2225/*
2226 * Set v:count to "count" and v:count1 to "count1".
2227 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2228 */
2229 void
2230set_vcount(
2231 long count,
2232 long count1,
2233 int set_prevcount)
2234{
2235 if (set_prevcount)
2236 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2237 vimvars[VV_COUNT].vv_nr = count;
2238 vimvars[VV_COUNT1].vv_nr = count1;
2239}
2240
2241/*
2242 * Save variables that might be changed as a side effect. Used when executing
2243 * a timer callback.
2244 */
2245 void
2246save_vimvars(vimvars_save_T *vvsave)
2247{
2248 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2249 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2250 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2251}
2252
2253/*
2254 * Restore variables saved by save_vimvars().
2255 */
2256 void
2257restore_vimvars(vimvars_save_T *vvsave)
2258{
2259 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2260 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2261 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2262}
2263
2264/*
2265 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2266 * value.
2267 */
2268 void
2269set_vim_var_string(
2270 int idx,
2271 char_u *val,
2272 int len) // length of "val" to use or -1 (whole string)
2273{
2274 clear_tv(&vimvars[idx].vv_di.di_tv);
2275 vimvars[idx].vv_type = VAR_STRING;
2276 if (val == NULL)
2277 vimvars[idx].vv_str = NULL;
2278 else if (len == -1)
2279 vimvars[idx].vv_str = vim_strsave(val);
2280 else
2281 vimvars[idx].vv_str = vim_strnsave(val, len);
2282}
2283
2284/*
2285 * Set List v: variable to "val".
2286 */
2287 void
2288set_vim_var_list(int idx, list_T *val)
2289{
2290 clear_tv(&vimvars[idx].vv_di.di_tv);
2291 vimvars[idx].vv_type = VAR_LIST;
2292 vimvars[idx].vv_list = val;
2293 if (val != NULL)
2294 ++val->lv_refcount;
2295}
2296
2297/*
2298 * Set Dictionary v: variable to "val".
2299 */
2300 void
2301set_vim_var_dict(int idx, dict_T *val)
2302{
2303 clear_tv(&vimvars[idx].vv_di.di_tv);
2304 vimvars[idx].vv_type = VAR_DICT;
2305 vimvars[idx].vv_dict = val;
2306 if (val != NULL)
2307 {
2308 ++val->dv_refcount;
2309 dict_set_items_ro(val);
2310 }
2311}
2312
2313/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002314 * Set the v:argv list.
2315 */
2316 void
2317set_argv_var(char **argv, int argc)
2318{
2319 list_T *l = list_alloc();
2320 int i;
2321
2322 if (l == NULL)
2323 getout(1);
2324 l->lv_lock = VAR_FIXED;
2325 for (i = 0; i < argc; ++i)
2326 {
2327 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2328 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002329 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002330 }
2331 set_vim_var_list(VV_ARGV, l);
2332}
2333
2334/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002335 * Reset v:register, taking the 'clipboard' setting into account.
2336 */
2337 void
2338reset_reg_var(void)
2339{
2340 int regname = 0;
2341
2342 // Adjust the register according to 'clipboard', so that when
2343 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2344#ifdef FEAT_CLIPBOARD
2345 adjust_clip_reg(&regname);
2346#endif
2347 set_reg_var(regname);
2348}
2349
2350/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002351 * Set v:register if needed.
2352 */
2353 void
2354set_reg_var(int c)
2355{
2356 char_u regname;
2357
2358 if (c == 0 || c == ' ')
2359 regname = '"';
2360 else
2361 regname = c;
2362 // Avoid free/alloc when the value is already right.
2363 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2364 set_vim_var_string(VV_REG, &regname, 1);
2365}
2366
2367/*
2368 * Get or set v:exception. If "oldval" == NULL, return the current value.
2369 * Otherwise, restore the value to "oldval" and return NULL.
2370 * Must always be called in pairs to save and restore v:exception! Does not
2371 * take care of memory allocations.
2372 */
2373 char_u *
2374v_exception(char_u *oldval)
2375{
2376 if (oldval == NULL)
2377 return vimvars[VV_EXCEPTION].vv_str;
2378
2379 vimvars[VV_EXCEPTION].vv_str = oldval;
2380 return NULL;
2381}
2382
2383/*
2384 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2385 * Otherwise, restore the value to "oldval" and return NULL.
2386 * Must always be called in pairs to save and restore v:throwpoint! Does not
2387 * take care of memory allocations.
2388 */
2389 char_u *
2390v_throwpoint(char_u *oldval)
2391{
2392 if (oldval == NULL)
2393 return vimvars[VV_THROWPOINT].vv_str;
2394
2395 vimvars[VV_THROWPOINT].vv_str = oldval;
2396 return NULL;
2397}
2398
2399/*
2400 * Set v:cmdarg.
2401 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2402 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2403 * Must always be called in pairs!
2404 */
2405 char_u *
2406set_cmdarg(exarg_T *eap, char_u *oldarg)
2407{
2408 char_u *oldval;
2409 char_u *newval;
2410 unsigned len;
2411
2412 oldval = vimvars[VV_CMDARG].vv_str;
2413 if (eap == NULL)
2414 {
2415 vim_free(oldval);
2416 vimvars[VV_CMDARG].vv_str = oldarg;
2417 return NULL;
2418 }
2419
2420 if (eap->force_bin == FORCE_BIN)
2421 len = 6;
2422 else if (eap->force_bin == FORCE_NOBIN)
2423 len = 8;
2424 else
2425 len = 0;
2426
2427 if (eap->read_edit)
2428 len += 7;
2429
2430 if (eap->force_ff != 0)
2431 len += 10; // " ++ff=unix"
2432 if (eap->force_enc != 0)
2433 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2434 if (eap->bad_char != 0)
2435 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2436
2437 newval = alloc(len + 1);
2438 if (newval == NULL)
2439 return NULL;
2440
2441 if (eap->force_bin == FORCE_BIN)
2442 sprintf((char *)newval, " ++bin");
2443 else if (eap->force_bin == FORCE_NOBIN)
2444 sprintf((char *)newval, " ++nobin");
2445 else
2446 *newval = NUL;
2447
2448 if (eap->read_edit)
2449 STRCAT(newval, " ++edit");
2450
2451 if (eap->force_ff != 0)
2452 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2453 eap->force_ff == 'u' ? "unix"
2454 : eap->force_ff == 'd' ? "dos"
2455 : "mac");
2456 if (eap->force_enc != 0)
2457 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2458 eap->cmd + eap->force_enc);
2459 if (eap->bad_char == BAD_KEEP)
2460 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2461 else if (eap->bad_char == BAD_DROP)
2462 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2463 else if (eap->bad_char != 0)
2464 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2465 vimvars[VV_CMDARG].vv_str = newval;
2466 return oldval;
2467}
2468
2469/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002470 * Get the value of internal variable "name".
2471 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2472 */
2473 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002474eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002475 char_u *name,
2476 int len, // length of "name"
2477 typval_T *rettv, // NULL when only checking existence
2478 dictitem_T **dip, // non-NULL when typval's dict item is needed
2479 int verbose, // may give error message
2480 int no_autoload) // do not use script autoloading
2481{
2482 int ret = OK;
2483 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002484 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002485 dictitem_T *v;
2486 int cc;
2487
2488 // truncate the name, so that we can use strcmp()
2489 cc = name[len];
2490 name[len] = NUL;
2491
2492 // Check for user-defined variables.
2493 v = find_var(name, NULL, no_autoload);
2494 if (v != NULL)
2495 {
2496 tv = &v->di_tv;
2497 if (dip != NULL)
2498 *dip = v;
2499 }
2500
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002501 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002503 imported_T *import;
2504 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2505
2506 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507
2508 // imported variable from another script
2509 if (import != NULL)
2510 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002511 if (import->imp_funcname != NULL)
2512 {
2513 foundFunc = TRUE;
2514 if (rettv != NULL)
2515 {
2516 rettv->v_type = VAR_FUNC;
2517 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2518 }
2519 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002520 else if (import->imp_all)
2521 {
2522 emsg("Sorry, 'import * as X' not implemented yet");
2523 ret = FAIL;
2524 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002525 else
2526 {
2527 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002528 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002530 tv = sv->sv_tv;
2531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002533 else if (in_vim9script())
2534 {
2535 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2536
2537 if (ufunc != NULL)
2538 {
2539 foundFunc = TRUE;
2540 if (rettv != NULL)
2541 {
2542 rettv->v_type = VAR_FUNC;
2543 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2544 }
2545 }
2546 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 }
2548
Bram Moolenaarc620c052020-07-08 15:16:19 +02002549 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002550 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002551 if (tv == NULL)
2552 {
2553 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002554 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002555 ret = FAIL;
2556 }
2557 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002558 {
2559 // If a list or dict variable wasn't initialized, do it now.
2560 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2561 {
2562 tv->vval.v_dict = dict_alloc();
2563 if (tv->vval.v_dict != NULL)
2564 ++tv->vval.v_dict->dv_refcount;
2565 }
2566 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2567 {
2568 tv->vval.v_list = list_alloc();
2569 if (tv->vval.v_list != NULL)
2570 ++tv->vval.v_list->lv_refcount;
2571 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002572 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002573 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002574 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002575
2576 name[len] = cc;
2577
2578 return ret;
2579}
2580
2581/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002582 * Check if variable "name[len]" is a local variable or an argument.
2583 * If so, "*eval_lavars_used" is set to TRUE.
2584 */
2585 void
2586check_vars(char_u *name, int len)
2587{
2588 int cc;
2589 char_u *varname;
2590 hashtab_T *ht;
2591
2592 if (eval_lavars_used == NULL)
2593 return;
2594
2595 // truncate the name, so that we can use strcmp()
2596 cc = name[len];
2597 name[len] = NUL;
2598
2599 ht = find_var_ht(name, &varname);
2600 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2601 {
2602 if (find_var(name, NULL, TRUE) != NULL)
2603 *eval_lavars_used = TRUE;
2604 }
2605
2606 name[len] = cc;
2607}
2608
2609/*
2610 * Find variable "name" in the list of variables.
2611 * Return a pointer to it if found, NULL if not found.
2612 * Careful: "a:0" variables don't have a name.
2613 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2614 * hashtab_T used.
2615 */
2616 dictitem_T *
2617find_var(char_u *name, hashtab_T **htp, int no_autoload)
2618{
2619 char_u *varname;
2620 hashtab_T *ht;
2621 dictitem_T *ret = NULL;
2622
2623 ht = find_var_ht(name, &varname);
2624 if (htp != NULL)
2625 *htp = ht;
2626 if (ht == NULL)
2627 return NULL;
2628 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2629 if (ret != NULL)
2630 return ret;
2631
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002632 // Search in parent scope for lambda
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002633 ret = find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2634 if (ret != NULL)
2635 return ret;
2636
2637 // in Vim9 script items without a scope can be script-local
2638 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2639 {
2640 ht = get_script_local_ht();
2641 if (ht != NULL)
2642 {
2643 ret = find_var_in_ht(ht, *name, varname,
2644 no_autoload || htp != NULL);
2645 if (ret != NULL)
2646 {
2647 if (htp != NULL)
2648 *htp = ht;
2649 return ret;
2650 }
2651 }
2652 }
2653
2654 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002655}
2656
2657/*
2658 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002659 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002660 * Returns NULL if not found.
2661 */
2662 dictitem_T *
2663find_var_in_ht(
2664 hashtab_T *ht,
2665 int htname,
2666 char_u *varname,
2667 int no_autoload)
2668{
2669 hashitem_T *hi;
2670
2671 if (*varname == NUL)
2672 {
2673 // Must be something like "s:", otherwise "ht" would be NULL.
2674 switch (htname)
2675 {
2676 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2677 case 'g': return &globvars_var;
2678 case 'v': return &vimvars_var;
2679 case 'b': return &curbuf->b_bufvar;
2680 case 'w': return &curwin->w_winvar;
2681 case 't': return &curtab->tp_winvar;
2682 case 'l': return get_funccal_local_var();
2683 case 'a': return get_funccal_args_var();
2684 }
2685 return NULL;
2686 }
2687
2688 hi = hash_find(ht, varname);
2689 if (HASHITEM_EMPTY(hi))
2690 {
2691 // For global variables we may try auto-loading the script. If it
2692 // worked find the variable again. Don't auto-load a script if it was
2693 // loaded already, otherwise it would be loaded every time when
2694 // checking if a function name is a Funcref variable.
2695 if (ht == &globvarht && !no_autoload)
2696 {
2697 // Note: script_autoload() may make "hi" invalid. It must either
2698 // be obtained again or not used.
2699 if (!script_autoload(varname, FALSE) || aborting())
2700 return NULL;
2701 hi = hash_find(ht, varname);
2702 }
2703 if (HASHITEM_EMPTY(hi))
2704 return NULL;
2705 }
2706 return HI2DI(hi);
2707}
2708
2709/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 * Get the script-local hashtab. NULL if not in a script context.
2711 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002712 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713get_script_local_ht(void)
2714{
2715 scid_T sid = current_sctx.sc_sid;
2716
Bram Moolenaare3d46852020-08-29 13:39:17 +02002717 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 return &SCRIPT_VARS(sid);
2719 return NULL;
2720}
2721
2722/*
2723 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002724 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002726 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2728{
2729 hashtab_T *ht = get_script_local_ht();
2730 char_u buffer[30];
2731 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002732 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 hashitem_T *hi;
2734
2735 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002736 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 if (len < sizeof(buffer) - 1)
2738 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002739 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 vim_strncpy(buffer, name, len);
2741 p = buffer;
2742 }
2743 else
2744 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002745 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002747 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 }
2749
2750 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002751 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752
2753 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002754 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2755 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756
2757 if (p != buffer)
2758 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002759 // Don't return "buffer", gcc complains.
2760 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002761}
2762
2763/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002764 * Find the hashtab used for a variable name.
2765 * Return NULL if the name is not valid.
2766 * Set "varname" to the start of name without ':'.
2767 */
2768 hashtab_T *
2769find_var_ht(char_u *name, char_u **varname)
2770{
2771 hashitem_T *hi;
2772 hashtab_T *ht;
2773
2774 if (name[0] == NUL)
2775 return NULL;
2776 if (name[1] != ':')
2777 {
2778 // The name must not start with a colon or #.
2779 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2780 return NULL;
2781 *varname = name;
2782
2783 // "version" is "v:version" in all scopes if scriptversion < 3.
2784 // Same for a few other variables marked with VV_COMPAT.
2785 if (current_sctx.sc_version < 3)
2786 {
2787 hi = hash_find(&compat_hashtab, name);
2788 if (!HASHITEM_EMPTY(hi))
2789 return &compat_hashtab;
2790 }
2791
2792 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 if (ht != NULL)
2794 return ht; // local variable
2795
2796 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002797 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798 {
2799 ht = get_script_local_ht();
2800 if (ht != NULL)
2801 return ht;
2802 }
2803
2804 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002805 }
2806 *varname = name + 2;
2807 if (*name == 'g') // global variable
2808 return &globvarht;
2809 // There must be no ':' or '#' in the rest of the name, unless g: is used
2810 if (vim_strchr(name + 2, ':') != NULL
2811 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2812 return NULL;
2813 if (*name == 'b') // buffer variable
2814 return &curbuf->b_vars->dv_hashtab;
2815 if (*name == 'w') // window variable
2816 return &curwin->w_vars->dv_hashtab;
2817 if (*name == 't') // tab page variable
2818 return &curtab->tp_vars->dv_hashtab;
2819 if (*name == 'v') // v: variable
2820 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002821 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002822 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002823 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002824 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 if (*name == 'a') // a: function argument
2826 return get_funccal_args_ht();
2827 if (*name == 'l') // l: local function variable
2828 return get_funccal_local_ht();
2829 }
2830 if (*name == 's') // script variable
2831 {
2832 ht = get_script_local_ht();
2833 if (ht != NULL)
2834 return ht;
2835 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002836 return NULL;
2837}
2838
2839/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002840 * Get the string value of a (global/local) variable.
2841 * Note: see tv_get_string() for how long the pointer remains valid.
2842 * Returns NULL when it doesn't exist.
2843 */
2844 char_u *
2845get_var_value(char_u *name)
2846{
2847 dictitem_T *v;
2848
2849 v = find_var(name, NULL, FALSE);
2850 if (v == NULL)
2851 return NULL;
2852 return tv_get_string(&v->di_tv);
2853}
2854
2855/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002856 * Allocate a new hashtab for a sourced script. It will be used while
2857 * sourcing this script and when executing functions defined in the script.
2858 */
2859 void
2860new_script_vars(scid_T id)
2861{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002862 scriptvar_T *sv;
2863
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002864 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2865 if (sv == NULL)
2866 return;
2867 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002868 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002869}
2870
2871/*
2872 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2873 * point to it.
2874 */
2875 void
2876init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2877{
2878 hash_init(&dict->dv_hashtab);
2879 dict->dv_lock = 0;
2880 dict->dv_scope = scope;
2881 dict->dv_refcount = DO_NOT_FREE_CNT;
2882 dict->dv_copyID = 0;
2883 dict_var->di_tv.vval.v_dict = dict;
2884 dict_var->di_tv.v_type = VAR_DICT;
2885 dict_var->di_tv.v_lock = VAR_FIXED;
2886 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2887 dict_var->di_key[0] = NUL;
2888}
2889
2890/*
2891 * Unreference a dictionary initialized by init_var_dict().
2892 */
2893 void
2894unref_var_dict(dict_T *dict)
2895{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002896 // Now the dict needs to be freed if no one else is using it, go back to
2897 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002898 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2899 dict_unref(dict);
2900}
2901
2902/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002903 * Clean up a list of internal variables.
2904 * Frees all allocated variables and the value they contain.
2905 * Clears hashtab "ht", does not free it.
2906 */
2907 void
2908vars_clear(hashtab_T *ht)
2909{
2910 vars_clear_ext(ht, TRUE);
2911}
2912
2913/*
2914 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2915 */
2916 void
2917vars_clear_ext(hashtab_T *ht, int free_val)
2918{
2919 int todo;
2920 hashitem_T *hi;
2921 dictitem_T *v;
2922
2923 hash_lock(ht);
2924 todo = (int)ht->ht_used;
2925 for (hi = ht->ht_array; todo > 0; ++hi)
2926 {
2927 if (!HASHITEM_EMPTY(hi))
2928 {
2929 --todo;
2930
2931 // Free the variable. Don't remove it from the hashtab,
2932 // ht_array might change then. hash_clear() takes care of it
2933 // later.
2934 v = HI2DI(hi);
2935 if (free_val)
2936 clear_tv(&v->di_tv);
2937 if (v->di_flags & DI_FLAGS_ALLOC)
2938 vim_free(v);
2939 }
2940 }
2941 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002942 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002943}
2944
2945/*
2946 * Delete a variable from hashtab "ht" at item "hi".
2947 * Clear the variable value and free the dictitem.
2948 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002949 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002950delete_var(hashtab_T *ht, hashitem_T *hi)
2951{
2952 dictitem_T *di = HI2DI(hi);
2953
2954 hash_remove(ht, hi);
2955 clear_tv(&di->di_tv);
2956 vim_free(di);
2957}
2958
2959/*
2960 * List the value of one internal variable.
2961 */
2962 static void
2963list_one_var(dictitem_T *v, char *prefix, int *first)
2964{
2965 char_u *tofree;
2966 char_u *s;
2967 char_u numbuf[NUMBUFLEN];
2968
2969 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2970 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2971 s == NULL ? (char_u *)"" : s, first);
2972 vim_free(tofree);
2973}
2974
2975 static void
2976list_one_var_a(
2977 char *prefix,
2978 char_u *name,
2979 int type,
2980 char_u *string,
2981 int *first) // when TRUE clear rest of screen and set to FALSE
2982{
2983 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2984 msg_start();
2985 msg_puts(prefix);
2986 if (name != NULL) // "a:" vars don't have a name stored
2987 msg_puts((char *)name);
2988 msg_putchar(' ');
2989 msg_advance(22);
2990 if (type == VAR_NUMBER)
2991 msg_putchar('#');
2992 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2993 msg_putchar('*');
2994 else if (type == VAR_LIST)
2995 {
2996 msg_putchar('[');
2997 if (*string == '[')
2998 ++string;
2999 }
3000 else if (type == VAR_DICT)
3001 {
3002 msg_putchar('{');
3003 if (*string == '{')
3004 ++string;
3005 }
3006 else
3007 msg_putchar(' ');
3008
3009 msg_outtrans(string);
3010
3011 if (type == VAR_FUNC || type == VAR_PARTIAL)
3012 msg_puts("()");
3013 if (*first)
3014 {
3015 msg_clr_eos();
3016 *first = FALSE;
3017 }
3018}
3019
3020/*
3021 * Set variable "name" to value in "tv".
3022 * If the variable already exists, the value is updated.
3023 * Otherwise the variable is created.
3024 */
3025 void
3026set_var(
3027 char_u *name,
3028 typval_T *tv,
3029 int copy) // make copy of value in "tv"
3030{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003031 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003032}
3033
3034/*
3035 * Set variable "name" to value in "tv".
3036 * If the variable already exists and "is_const" is FALSE the value is updated.
3037 * Otherwise the variable is created.
3038 */
3039 void
3040set_var_const(
3041 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003043 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003044 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003045 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003046{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003047 typval_T *tv = tv_arg;
3048 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003050 char_u *varname;
3051 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003053 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003054
3055 ht = find_var_ht(name, &varname);
3056 if (ht == NULL || *varname == NUL)
3057 {
3058 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003059 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003060 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003061 is_script_local = ht == get_script_local_ht();
3062
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003063 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003064 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003065 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003066 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003067 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003068 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003069 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003070 }
3071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003073
3074 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 if (di == NULL)
3076 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077
3078 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003079 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003080 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003081
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003082 if (need_convert_to_bool(type, tv))
3083 {
3084 // Destination is a bool and the value is not, but it can be converted.
3085 CLEAR_FIELD(bool_tv);
3086 bool_tv.v_type = VAR_BOOL;
3087 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3088 tv = &bool_tv;
3089 }
3090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003092 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003094 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003095 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 {
3097 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003098 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 }
3100
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003101 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003103 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003104 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003105 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003106 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003107 }
3108
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003109 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003110 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003111 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003113
Bram Moolenaara187c432020-09-16 21:08:28 +02003114 // Check in this order for backwards compatibility:
3115 // - Whether the variable is read-only
3116 // - Whether the variable value is locked
3117 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003118 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003119 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3120 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003121 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 else
3124 // can only redefine once
3125 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003126
3127 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003130 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003131 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003132 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003134 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003136 if (copy || tv->v_type != VAR_STRING)
3137 {
3138 char_u *val = tv_get_string(tv);
3139
3140 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003141 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 if (di->di_tv.vval.v_string == NULL)
3143 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003144 }
3145 else
3146 {
3147 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003149 tv->vval.v_string = NULL;
3150 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003151 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003152 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003154 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003156 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003158#ifdef FEAT_SEARCH_EXTRA
3159 else if (STRCMP(varname, "hlsearch") == 0)
3160 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162 redraw_all_later(SOME_VALID);
3163 }
3164#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003165 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003168 {
3169 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003170 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003171 }
3172 }
3173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003175 }
3176 else // add a new variable
3177 {
3178 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003179 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003180 {
3181 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003182 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003183 }
3184
3185 // Make sure the variable name is valid.
3186 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003187 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003188
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3190 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003191 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 STRCPY(di->di_key, varname);
3193 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003196 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003197 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003199 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 di->di_flags |= DI_FLAGS_LOCK;
3201
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003202 // A Vim9 script-local variable is also added to sn_all_vars and
3203 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003204 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003205 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003206 }
3207
3208 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003210 else
3211 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 di->di_tv = *tv;
3213 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003214 init_tv(tv);
3215 }
3216
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003217 // ":const var = val" locks the value
3218 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003219 // Like :lockvar! name: lock the value and what it contains, but only
3220 // if the reference count is up to one. That locks only literal
3221 // values.
3222 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003223 return;
3224failed:
3225 if (!copy)
3226 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227}
3228
3229/*
3230 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3231 * Also give an error message.
3232 */
3233 int
3234var_check_ro(int flags, char_u *name, int use_gettext)
3235{
3236 if (flags & DI_FLAGS_RO)
3237 {
3238 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3239 return TRUE;
3240 }
3241 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3242 {
3243 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3244 return TRUE;
3245 }
3246 return FALSE;
3247}
3248
3249/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003250 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3251 * Also give an error message.
3252 */
3253 int
3254var_check_lock(int flags, char_u *name, int use_gettext)
3255{
3256 if (flags & DI_FLAGS_LOCK)
3257 {
3258 semsg(_(e_variable_is_locked_str),
3259 use_gettext ? (char_u *)_(name) : name);
3260 return TRUE;
3261 }
3262 return FALSE;
3263}
3264
3265/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003266 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3267 * Also give an error message.
3268 */
3269 int
3270var_check_fixed(int flags, char_u *name, int use_gettext)
3271{
3272 if (flags & DI_FLAGS_FIX)
3273 {
3274 semsg(_("E795: Cannot delete variable %s"),
3275 use_gettext ? (char_u *)_(name) : name);
3276 return TRUE;
3277 }
3278 return FALSE;
3279}
3280
3281/*
3282 * Check if a funcref is assigned to a valid variable name.
3283 * Return TRUE and give an error if not.
3284 */
3285 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003286var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003287 char_u *name, // points to start of variable name
3288 int new_var) // TRUE when creating the variable
3289{
3290 // Allow for w: b: s: and t:.
3291 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3292 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3293 ? name[2] : name[0]))
3294 {
3295 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3296 name);
3297 return TRUE;
3298 }
3299 // Don't allow hiding a function. When "v" is not NULL we might be
3300 // assigning another function to the same var, the type is checked
3301 // below.
3302 if (new_var && function_exists(name, FALSE))
3303 {
3304 semsg(_("E705: Variable name conflicts with existing function: %s"),
3305 name);
3306 return TRUE;
3307 }
3308 return FALSE;
3309}
3310
3311/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003312 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3313 * value. Also give an error message, using "name" or _("name") when
3314 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003315 */
3316 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003317value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003318{
3319 if (lock & VAR_LOCKED)
3320 {
3321 semsg(_("E741: Value is locked: %s"),
3322 name == NULL ? (char_u *)_("Unknown")
3323 : use_gettext ? (char_u *)_(name)
3324 : name);
3325 return TRUE;
3326 }
3327 if (lock & VAR_FIXED)
3328 {
3329 semsg(_("E742: Cannot change value of %s"),
3330 name == NULL ? (char_u *)_("Unknown")
3331 : use_gettext ? (char_u *)_(name)
3332 : name);
3333 return TRUE;
3334 }
3335 return FALSE;
3336}
3337
3338/*
3339 * Check if a variable name is valid.
3340 * Return FALSE and give an error if not.
3341 */
3342 int
3343valid_varname(char_u *varname)
3344{
3345 char_u *p;
3346
3347 for (p = varname; *p != NUL; ++p)
3348 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3349 && *p != AUTOLOAD_CHAR)
3350 {
3351 semsg(_(e_illvar), varname);
3352 return FALSE;
3353 }
3354 return TRUE;
3355}
3356
3357/*
3358 * getwinvar() and gettabwinvar()
3359 */
3360 static void
3361getwinvar(
3362 typval_T *argvars,
3363 typval_T *rettv,
3364 int off) // 1 for gettabwinvar()
3365{
3366 win_T *win;
3367 char_u *varname;
3368 dictitem_T *v;
3369 tabpage_T *tp = NULL;
3370 int done = FALSE;
3371 win_T *oldcurwin;
3372 tabpage_T *oldtabpage;
3373 int need_switch_win;
3374
3375 if (off == 1)
3376 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3377 else
3378 tp = curtab;
3379 win = find_win_by_nr(&argvars[off], tp);
3380 varname = tv_get_string_chk(&argvars[off + 1]);
3381 ++emsg_off;
3382
3383 rettv->v_type = VAR_STRING;
3384 rettv->vval.v_string = NULL;
3385
3386 if (win != NULL && varname != NULL)
3387 {
3388 // Set curwin to be our win, temporarily. Also set the tabpage,
3389 // otherwise the window is not valid. Only do this when needed,
3390 // autocommands get blocked.
3391 need_switch_win = !(tp == curtab && win == curwin);
3392 if (!need_switch_win
3393 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3394 {
3395 if (*varname == '&')
3396 {
3397 if (varname[1] == NUL)
3398 {
3399 // get all window-local options in a dict
3400 dict_T *opts = get_winbuf_options(FALSE);
3401
3402 if (opts != NULL)
3403 {
3404 rettv_dict_set(rettv, opts);
3405 done = TRUE;
3406 }
3407 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003408 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003409 // window-local-option
3410 done = TRUE;
3411 }
3412 else
3413 {
3414 // Look up the variable.
3415 // Let getwinvar({nr}, "") return the "w:" dictionary.
3416 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3417 varname, FALSE);
3418 if (v != NULL)
3419 {
3420 copy_tv(&v->di_tv, rettv);
3421 done = TRUE;
3422 }
3423 }
3424 }
3425
3426 if (need_switch_win)
3427 // restore previous notion of curwin
3428 restore_win(oldcurwin, oldtabpage, TRUE);
3429 }
3430
3431 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3432 // use the default return value
3433 copy_tv(&argvars[off + 2], rettv);
3434
3435 --emsg_off;
3436}
3437
3438/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003439 * Set option "varname" to the value of "varp" for the current buffer/window.
3440 */
3441 static void
3442set_option_from_tv(char_u *varname, typval_T *varp)
3443{
3444 long numval = 0;
3445 char_u *strval;
3446 char_u nbuf[NUMBUFLEN];
3447 int error = FALSE;
3448
3449 if (!in_vim9script() || varp->v_type != VAR_STRING)
3450 numval = (long)tv_get_number_chk(varp, &error);
3451 strval = tv_get_string_buf_chk(varp, nbuf);
3452 if (!error && strval != NULL)
3453 set_option_value(varname, numval, strval, OPT_LOCAL);
3454}
3455
3456/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003457 * "setwinvar()" and "settabwinvar()" functions
3458 */
3459 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003460setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003461{
3462 win_T *win;
3463 win_T *save_curwin;
3464 tabpage_T *save_curtab;
3465 int need_switch_win;
3466 char_u *varname, *winvarname;
3467 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003468 tabpage_T *tp = NULL;
3469
3470 if (check_secure())
3471 return;
3472
3473 if (off == 1)
3474 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3475 else
3476 tp = curtab;
3477 win = find_win_by_nr(&argvars[off], tp);
3478 varname = tv_get_string_chk(&argvars[off + 1]);
3479 varp = &argvars[off + 2];
3480
3481 if (win != NULL && varname != NULL && varp != NULL)
3482 {
3483 need_switch_win = !(tp == curtab && win == curwin);
3484 if (!need_switch_win
3485 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3486 {
3487 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003488 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003489 else
3490 {
3491 winvarname = alloc(STRLEN(varname) + 3);
3492 if (winvarname != NULL)
3493 {
3494 STRCPY(winvarname, "w:");
3495 STRCPY(winvarname + 2, varname);
3496 set_var(winvarname, varp, TRUE);
3497 vim_free(winvarname);
3498 }
3499 }
3500 }
3501 if (need_switch_win)
3502 restore_win(save_curwin, save_curtab, TRUE);
3503 }
3504}
3505
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003506/*
3507 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3508 * v:option_type, and v:option_command.
3509 */
3510 void
3511reset_v_option_vars(void)
3512{
3513 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3514 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3515 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3516 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3517 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3518 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3519}
3520
3521/*
3522 * Add an assert error to v:errors.
3523 */
3524 void
3525assert_error(garray_T *gap)
3526{
3527 struct vimvar *vp = &vimvars[VV_ERRORS];
3528
3529 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003530 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003531 set_vim_var_list(VV_ERRORS, list_alloc());
3532 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3533}
3534
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003535 int
3536var_exists(char_u *var)
3537{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003538 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003539 char_u *name;
3540 char_u *tofree;
3541 typval_T tv;
3542 int len = 0;
3543 int n = FALSE;
3544
3545 // get_name_len() takes care of expanding curly braces
3546 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003547 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003548 if (len > 0)
3549 {
3550 if (tofree != NULL)
3551 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003552 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003553 if (n)
3554 {
3555 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003556 arg = skipwhite(arg);
3557 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003558 if (n)
3559 clear_tv(&tv);
3560 }
3561 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003562 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003563 n = FALSE;
3564
3565 vim_free(tofree);
3566 return n;
3567}
3568
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003569static lval_T *redir_lval = NULL;
3570#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3571static garray_T redir_ga; // only valid when redir_lval is not NULL
3572static char_u *redir_endp = NULL;
3573static char_u *redir_varname = NULL;
3574
3575/*
3576 * Start recording command output to a variable
3577 * When "append" is TRUE append to an existing variable.
3578 * Returns OK if successfully completed the setup. FAIL otherwise.
3579 */
3580 int
3581var_redir_start(char_u *name, int append)
3582{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003583 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003584 typval_T tv;
3585
3586 // Catch a bad name early.
3587 if (!eval_isnamec1(*name))
3588 {
3589 emsg(_(e_invarg));
3590 return FAIL;
3591 }
3592
3593 // Make a copy of the name, it is used in redir_lval until redir ends.
3594 redir_varname = vim_strsave(name);
3595 if (redir_varname == NULL)
3596 return FAIL;
3597
3598 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3599 if (redir_lval == NULL)
3600 {
3601 var_redir_stop();
3602 return FAIL;
3603 }
3604
3605 // The output is stored in growarray "redir_ga" until redirection ends.
3606 ga_init2(&redir_ga, (int)sizeof(char), 500);
3607
3608 // Parse the variable name (can be a dict or list entry).
3609 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3610 FNE_CHECK_START);
3611 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3612 {
3613 clear_lval(redir_lval);
3614 if (redir_endp != NULL && *redir_endp != NUL)
3615 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003616 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003617 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003618 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003619 redir_endp = NULL; // don't store a value, only cleanup
3620 var_redir_stop();
3621 return FAIL;
3622 }
3623
3624 // check if we can write to the variable: set it to or append an empty
3625 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003626 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003627 tv.v_type = VAR_STRING;
3628 tv.vval.v_string = (char_u *)"";
3629 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003630 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3631 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003632 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003633 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3634 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003635 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003636 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003637 {
3638 redir_endp = NULL; // don't store a value, only cleanup
3639 var_redir_stop();
3640 return FAIL;
3641 }
3642
3643 return OK;
3644}
3645
3646/*
3647 * Append "value[value_len]" to the variable set by var_redir_start().
3648 * The actual appending is postponed until redirection ends, because the value
3649 * appended may in fact be the string we write to, changing it may cause freed
3650 * memory to be used:
3651 * :redir => foo
3652 * :let foo
3653 * :redir END
3654 */
3655 void
3656var_redir_str(char_u *value, int value_len)
3657{
3658 int len;
3659
3660 if (redir_lval == NULL)
3661 return;
3662
3663 if (value_len == -1)
3664 len = (int)STRLEN(value); // Append the entire string
3665 else
3666 len = value_len; // Append only "value_len" characters
3667
3668 if (ga_grow(&redir_ga, len) == OK)
3669 {
3670 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3671 redir_ga.ga_len += len;
3672 }
3673 else
3674 var_redir_stop();
3675}
3676
3677/*
3678 * Stop redirecting command output to a variable.
3679 * Frees the allocated memory.
3680 */
3681 void
3682var_redir_stop(void)
3683{
3684 typval_T tv;
3685
3686 if (EVALCMD_BUSY)
3687 {
3688 redir_lval = NULL;
3689 return;
3690 }
3691
3692 if (redir_lval != NULL)
3693 {
3694 // If there was no error: assign the text to the variable.
3695 if (redir_endp != NULL)
3696 {
3697 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3698 tv.v_type = VAR_STRING;
3699 tv.vval.v_string = redir_ga.ga_data;
3700 // Call get_lval() again, if it's inside a Dict or List it may
3701 // have changed.
3702 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3703 FALSE, FALSE, 0, FNE_CHECK_START);
3704 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003706 (char_u *)".");
3707 clear_lval(redir_lval);
3708 }
3709
3710 // free the collected output
3711 VIM_CLEAR(redir_ga.ga_data);
3712
3713 VIM_CLEAR(redir_lval);
3714 }
3715 VIM_CLEAR(redir_varname);
3716}
3717
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003718/*
3719 * "gettabvar()" function
3720 */
3721 void
3722f_gettabvar(typval_T *argvars, typval_T *rettv)
3723{
3724 win_T *oldcurwin;
3725 tabpage_T *tp, *oldtabpage;
3726 dictitem_T *v;
3727 char_u *varname;
3728 int done = FALSE;
3729
3730 rettv->v_type = VAR_STRING;
3731 rettv->vval.v_string = NULL;
3732
3733 varname = tv_get_string_chk(&argvars[1]);
3734 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3735 if (tp != NULL && varname != NULL)
3736 {
3737 // Set tp to be our tabpage, temporarily. Also set the window to the
3738 // first window in the tabpage, otherwise the window is not valid.
3739 if (switch_win(&oldcurwin, &oldtabpage,
3740 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3741 : tp->tp_firstwin, tp, TRUE) == OK)
3742 {
3743 // look up the variable
3744 // Let gettabvar({nr}, "") return the "t:" dictionary.
3745 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3746 if (v != NULL)
3747 {
3748 copy_tv(&v->di_tv, rettv);
3749 done = TRUE;
3750 }
3751 }
3752
3753 // restore previous notion of curwin
3754 restore_win(oldcurwin, oldtabpage, TRUE);
3755 }
3756
3757 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3758 copy_tv(&argvars[2], rettv);
3759}
3760
3761/*
3762 * "gettabwinvar()" function
3763 */
3764 void
3765f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3766{
3767 getwinvar(argvars, rettv, 1);
3768}
3769
3770/*
3771 * "getwinvar()" function
3772 */
3773 void
3774f_getwinvar(typval_T *argvars, typval_T *rettv)
3775{
3776 getwinvar(argvars, rettv, 0);
3777}
3778
3779/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003780 * "getbufvar()" function
3781 */
3782 void
3783f_getbufvar(typval_T *argvars, typval_T *rettv)
3784{
3785 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003786 char_u *varname;
3787 dictitem_T *v;
3788 int done = FALSE;
3789
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003790 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003791 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003792
3793 rettv->v_type = VAR_STRING;
3794 rettv->vval.v_string = NULL;
3795
3796 if (buf != NULL && varname != NULL)
3797 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003798 if (*varname == '&')
3799 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003800 buf_T *save_curbuf = curbuf;
3801
3802 // set curbuf to be our buf, temporarily
3803 curbuf = buf;
3804
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003805 if (varname[1] == NUL)
3806 {
3807 // get all buffer-local options in a dict
3808 dict_T *opts = get_winbuf_options(TRUE);
3809
3810 if (opts != NULL)
3811 {
3812 rettv_dict_set(rettv, opts);
3813 done = TRUE;
3814 }
3815 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003816 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003817 // buffer-local-option
3818 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003819
3820 // restore previous notion of curbuf
3821 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003822 }
3823 else
3824 {
3825 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003826 if (*varname == NUL)
3827 // Let getbufvar({nr}, "") return the "b:" dictionary.
3828 v = &buf->b_bufvar;
3829 else
3830 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3831 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003832 if (v != NULL)
3833 {
3834 copy_tv(&v->di_tv, rettv);
3835 done = TRUE;
3836 }
3837 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003838 }
3839
3840 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3841 // use the default value
3842 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003843}
3844
3845/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003846 * "settabvar()" function
3847 */
3848 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003849f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003850{
3851 tabpage_T *save_curtab;
3852 tabpage_T *tp;
3853 char_u *varname, *tabvarname;
3854 typval_T *varp;
3855
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003856 if (check_secure())
3857 return;
3858
3859 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3860 varname = tv_get_string_chk(&argvars[1]);
3861 varp = &argvars[2];
3862
3863 if (varname != NULL && varp != NULL && tp != NULL)
3864 {
3865 save_curtab = curtab;
3866 goto_tabpage_tp(tp, FALSE, FALSE);
3867
3868 tabvarname = alloc(STRLEN(varname) + 3);
3869 if (tabvarname != NULL)
3870 {
3871 STRCPY(tabvarname, "t:");
3872 STRCPY(tabvarname + 2, varname);
3873 set_var(tabvarname, varp, TRUE);
3874 vim_free(tabvarname);
3875 }
3876
3877 // Restore current tabpage
3878 if (valid_tabpage(save_curtab))
3879 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3880 }
3881}
3882
3883/*
3884 * "settabwinvar()" function
3885 */
3886 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003887f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003888{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003889 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003890}
3891
3892/*
3893 * "setwinvar()" function
3894 */
3895 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003896f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003897{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003898 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003899}
3900
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003901/*
3902 * "setbufvar()" function
3903 */
3904 void
3905f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3906{
3907 buf_T *buf;
3908 char_u *varname, *bufvarname;
3909 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003910
3911 if (check_secure())
3912 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003913 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003914 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003915 varp = &argvars[2];
3916
3917 if (buf != NULL && varname != NULL && varp != NULL)
3918 {
3919 if (*varname == '&')
3920 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003921 aco_save_T aco;
3922
3923 // set curbuf to be our buf, temporarily
3924 aucmd_prepbuf(&aco, buf);
3925
Bram Moolenaar191929b2020-08-19 21:20:49 +02003926 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003927
3928 // reset notion of buffer
3929 aucmd_restbuf(&aco);
3930 }
3931 else
3932 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003933 bufvarname = alloc(STRLEN(varname) + 3);
3934 if (bufvarname != NULL)
3935 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003936 buf_T *save_curbuf = curbuf;
3937
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003938 curbuf = buf;
3939 STRCPY(bufvarname, "b:");
3940 STRCPY(bufvarname + 2, varname);
3941 set_var(bufvarname, varp, TRUE);
3942 vim_free(bufvarname);
3943 curbuf = save_curbuf;
3944 }
3945 }
3946 }
3947}
3948
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003949/*
3950 * Get a callback from "arg". It can be a Funcref or a function name.
3951 * When "arg" is zero return an empty string.
3952 * "cb_name" is not allocated.
3953 * "cb_name" is set to NULL for an invalid argument.
3954 */
3955 callback_T
3956get_callback(typval_T *arg)
3957{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003958 callback_T res;
3959 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003960
3961 res.cb_free_name = FALSE;
3962 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3963 {
3964 res.cb_partial = arg->vval.v_partial;
3965 ++res.cb_partial->pt_refcount;
3966 res.cb_name = partial_name(res.cb_partial);
3967 }
3968 else
3969 {
3970 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003971 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3972 && isdigit(*arg->vval.v_string))
3973 r = FAIL;
3974 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003975 {
3976 // Note that we don't make a copy of the string.
3977 res.cb_name = arg->vval.v_string;
3978 func_ref(res.cb_name);
3979 }
3980 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003981 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003982 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003983 r = FAIL;
3984
3985 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003986 {
3987 emsg(_("E921: Invalid callback argument"));
3988 res.cb_name = NULL;
3989 }
3990 }
3991 return res;
3992}
3993
3994/*
3995 * Copy a callback into a typval_T.
3996 */
3997 void
3998put_callback(callback_T *cb, typval_T *tv)
3999{
4000 if (cb->cb_partial != NULL)
4001 {
4002 tv->v_type = VAR_PARTIAL;
4003 tv->vval.v_partial = cb->cb_partial;
4004 ++tv->vval.v_partial->pt_refcount;
4005 }
4006 else
4007 {
4008 tv->v_type = VAR_FUNC;
4009 tv->vval.v_string = vim_strsave(cb->cb_name);
4010 func_ref(cb->cb_name);
4011 }
4012}
4013
4014/*
4015 * Make a copy of "src" into "dest", allocating the function name if needed,
4016 * without incrementing the refcount.
4017 */
4018 void
4019set_callback(callback_T *dest, callback_T *src)
4020{
4021 if (src->cb_partial == NULL)
4022 {
4023 // just a function name, make a copy
4024 dest->cb_name = vim_strsave(src->cb_name);
4025 dest->cb_free_name = TRUE;
4026 }
4027 else
4028 {
4029 // cb_name is a pointer into cb_partial
4030 dest->cb_name = src->cb_name;
4031 dest->cb_free_name = FALSE;
4032 }
4033 dest->cb_partial = src->cb_partial;
4034}
4035
4036/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004037 * Copy callback from "src" to "dest", incrementing the refcounts.
4038 */
4039 void
4040copy_callback(callback_T *dest, callback_T *src)
4041{
4042 dest->cb_partial = src->cb_partial;
4043 if (dest->cb_partial != NULL)
4044 {
4045 dest->cb_name = src->cb_name;
4046 dest->cb_free_name = FALSE;
4047 ++dest->cb_partial->pt_refcount;
4048 }
4049 else
4050 {
4051 dest->cb_name = vim_strsave(src->cb_name);
4052 dest->cb_free_name = TRUE;
4053 func_ref(src->cb_name);
4054 }
4055}
4056
4057/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004058 * Unref/free "callback" returned by get_callback() or set_callback().
4059 */
4060 void
4061free_callback(callback_T *callback)
4062{
4063 if (callback->cb_partial != NULL)
4064 {
4065 partial_unref(callback->cb_partial);
4066 callback->cb_partial = NULL;
4067 }
4068 else if (callback->cb_name != NULL)
4069 func_unref(callback->cb_name);
4070 if (callback->cb_free_name)
4071 {
4072 vim_free(callback->cb_name);
4073 callback->cb_free_name = FALSE;
4074 }
4075 callback->cb_name = NULL;
4076}
4077
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004078#endif // FEAT_EVAL