blob: fd67ad0f5ec43870eb2eeb8e0cb910558bbe1e48 [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 Moolenaarcfcd0112020-09-27 15:19:27 +0200149 {VV_NAME("disallow_let", VAR_NUMBER), 0}, // TODO: remove
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);
222 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
223 set_vim_var_list(VV_ERRORS, list_alloc());
224 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
225
226 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
227 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
228 set_vim_var_nr(VV_NONE, VVAL_NONE);
229 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100230 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
233 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
234 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
235 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
236 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
237 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
238 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
239 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
240 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
241 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
242 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
243
244 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
245
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200246 // TODO: remove later
247 set_vim_var_nr(VV_DISALLOW_LET, 1);
248
Bram Moolenaar439c0362020-06-06 15:58:03 +0200249 // Default for v:register is not 0 but '"'. This is adjusted once the
250 // clipboard has been setup by calling reset_reg_var().
251 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200252}
253
254#if defined(EXITFREE) || defined(PROTO)
255/*
256 * Free all vim variables information on exit
257 */
258 void
259evalvars_clear(void)
260{
261 int i;
262 struct vimvar *p;
263
264 for (i = 0; i < VV_LEN; ++i)
265 {
266 p = &vimvars[i];
267 if (p->vv_di.di_tv.v_type == VAR_STRING)
268 VIM_CLEAR(p->vv_str);
269 else if (p->vv_di.di_tv.v_type == VAR_LIST)
270 {
271 list_unref(p->vv_list);
272 p->vv_list = NULL;
273 }
274 }
275 hash_clear(&vimvarht);
276 hash_init(&vimvarht); // garbage_collect() will access it
277 hash_clear(&compat_hashtab);
278
279 // global variables
280 vars_clear(&globvarht);
281
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100282 // Script-local variables. Clear all the variables here.
283 // The scriptvar_T is cleared later in free_scriptnames(), because a
284 // variable in one script might hold a reference to the whole scope of
285 // another script.
286 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200287 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200288}
289#endif
290
291 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200292garbage_collect_globvars(int copyID)
293{
294 return set_ref_in_ht(&globvarht, copyID, NULL);
295}
296
297 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200298garbage_collect_vimvars(int copyID)
299{
300 return set_ref_in_ht(&vimvarht, copyID, NULL);
301}
302
303 int
304garbage_collect_scriptvars(int copyID)
305{
Bram Moolenaared234f22020-10-15 20:42:20 +0200306 int i;
307 int idx;
308 int abort = FALSE;
309 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200310
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100311 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200312 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200313 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
314
Bram Moolenaared234f22020-10-15 20:42:20 +0200315 si = SCRIPT_ITEM(i);
316 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
317 {
318 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
319
320 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
321 }
322 }
323
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324 return abort;
325}
326
327/*
328 * Set an internal variable to a string value. Creates the variable if it does
329 * not already exist.
330 */
331 void
332set_internal_string_var(char_u *name, char_u *value)
333{
334 char_u *val;
335 typval_T *tvp;
336
337 val = vim_strsave(value);
338 if (val != NULL)
339 {
340 tvp = alloc_string_tv(val);
341 if (tvp != NULL)
342 {
343 set_var(name, tvp, FALSE);
344 free_tv(tvp);
345 }
346 }
347}
348
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200349 int
350eval_charconvert(
351 char_u *enc_from,
352 char_u *enc_to,
353 char_u *fname_from,
354 char_u *fname_to)
355{
356 int err = FALSE;
357
358 set_vim_var_string(VV_CC_FROM, enc_from, -1);
359 set_vim_var_string(VV_CC_TO, enc_to, -1);
360 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
361 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
362 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
363 err = TRUE;
364 set_vim_var_string(VV_CC_FROM, NULL, -1);
365 set_vim_var_string(VV_CC_TO, NULL, -1);
366 set_vim_var_string(VV_FNAME_IN, NULL, -1);
367 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
368
369 if (err)
370 return FAIL;
371 return OK;
372}
373
374# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
375 int
376eval_printexpr(char_u *fname, char_u *args)
377{
378 int err = FALSE;
379
380 set_vim_var_string(VV_FNAME_IN, fname, -1);
381 set_vim_var_string(VV_CMDARG, args, -1);
382 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
383 err = TRUE;
384 set_vim_var_string(VV_FNAME_IN, NULL, -1);
385 set_vim_var_string(VV_CMDARG, NULL, -1);
386
387 if (err)
388 {
389 mch_remove(fname);
390 return FAIL;
391 }
392 return OK;
393}
394# endif
395
396# if defined(FEAT_DIFF) || defined(PROTO)
397 void
398eval_diff(
399 char_u *origfile,
400 char_u *newfile,
401 char_u *outfile)
402{
403 int err = FALSE;
404
405 set_vim_var_string(VV_FNAME_IN, origfile, -1);
406 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
407 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
408 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
409 set_vim_var_string(VV_FNAME_IN, NULL, -1);
410 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
411 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
412}
413
414 void
415eval_patch(
416 char_u *origfile,
417 char_u *difffile,
418 char_u *outfile)
419{
420 int err;
421
422 set_vim_var_string(VV_FNAME_IN, origfile, -1);
423 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
424 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
425 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
426 set_vim_var_string(VV_FNAME_IN, NULL, -1);
427 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
428 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
429}
430# endif
431
432#if defined(FEAT_SPELL) || defined(PROTO)
433/*
434 * Evaluate an expression to a list with suggestions.
435 * For the "expr:" part of 'spellsuggest'.
436 * Returns NULL when there is an error.
437 */
438 list_T *
439eval_spell_expr(char_u *badword, char_u *expr)
440{
441 typval_T save_val;
442 typval_T rettv;
443 list_T *list = NULL;
444 char_u *p = skipwhite(expr);
445
446 // Set "v:val" to the bad word.
447 prepare_vimvar(VV_VAL, &save_val);
448 set_vim_var_string(VV_VAL, badword, -1);
449 if (p_verbose == 0)
450 ++emsg_off;
451
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200452 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200453 {
454 if (rettv.v_type != VAR_LIST)
455 clear_tv(&rettv);
456 else
457 list = rettv.vval.v_list;
458 }
459
460 if (p_verbose == 0)
461 --emsg_off;
462 clear_tv(get_vim_var_tv(VV_VAL));
463 restore_vimvar(VV_VAL, &save_val);
464
465 return list;
466}
467
468/*
469 * "list" is supposed to contain two items: a word and a number. Return the
470 * word in "pp" and the number as the return value.
471 * Return -1 if anything isn't right.
472 * Used to get the good word and score from the eval_spell_expr() result.
473 */
474 int
475get_spellword(list_T *list, char_u **pp)
476{
477 listitem_T *li;
478
479 li = list->lv_first;
480 if (li == NULL)
481 return -1;
482 *pp = tv_get_string(&li->li_tv);
483
484 li = li->li_next;
485 if (li == NULL)
486 return -1;
487 return (int)tv_get_number(&li->li_tv);
488}
489#endif
490
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200491/*
492 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200493 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494 * When not used yet add the variable to the v: hashtable.
495 */
496 void
497prepare_vimvar(int idx, typval_T *save_tv)
498{
499 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200500 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200501 if (vimvars[idx].vv_type == VAR_UNKNOWN)
502 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
503}
504
505/*
506 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200507 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200508 * When no longer defined, remove the variable from the v: hashtable.
509 */
510 void
511restore_vimvar(int idx, typval_T *save_tv)
512{
513 hashitem_T *hi;
514
515 vimvars[idx].vv_tv = *save_tv;
516 if (vimvars[idx].vv_type == VAR_UNKNOWN)
517 {
518 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
519 if (HASHITEM_EMPTY(hi))
520 internal_error("restore_vimvar()");
521 else
522 hash_remove(&vimvarht, hi);
523 }
524}
525
526/*
527 * List Vim variables.
528 */
529 static void
530list_vim_vars(int *first)
531{
532 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
533}
534
535/*
536 * List script-local variables, if there is a script.
537 */
538 static void
539list_script_vars(int *first)
540{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200541 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200542 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
543 "s:", FALSE, first);
544}
545
546/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200547 * Get a list of lines from a HERE document. The here document is a list of
548 * lines surrounded by a marker.
549 * cmd << {marker}
550 * {line1}
551 * {line2}
552 * ....
553 * {marker}
554 *
555 * The {marker} is a string. If the optional 'trim' word is supplied before the
556 * marker, then the leading indentation before the lines (matching the
557 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200558 *
559 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
560 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
561 * missing, then '.' is accepted as a marker.
562 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200563 * Returns a List with {lines} or NULL.
564 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200566heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200567{
568 char_u *theline;
569 char_u *marker;
570 list_T *l;
571 char_u *p;
572 int marker_indent_len = 0;
573 int text_indent_len = 0;
574 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200575 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200576 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200577
578 if (eap->getline == NULL)
579 {
580 emsg(_("E991: cannot use =<< here"));
581 return NULL;
582 }
583
584 // Check for the optional 'trim' word before the marker
585 cmd = skipwhite(cmd);
586 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
587 {
588 cmd = skipwhite(cmd + 4);
589
590 // Trim the indentation from all the lines in the here document.
591 // The amount of indentation trimmed is the same as the indentation of
592 // the first line after the :let command line. To find the end marker
593 // the indent of the :let command line is trimmed.
594 p = *eap->cmdlinep;
595 while (VIM_ISWHITE(*p))
596 {
597 p++;
598 marker_indent_len++;
599 }
600 text_indent_len = -1;
601 }
602
603 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200604 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200605 {
606 marker = skipwhite(cmd);
607 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200608 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200609 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200610 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200611 return NULL;
612 }
613 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200614 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200615 {
616 emsg(_("E221: Marker cannot start with lower case letter"));
617 return NULL;
618 }
619 }
620 else
621 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200622 // When getting lines for an embedded script, if the marker is missing,
623 // accept '.' as the marker.
624 if (script_get)
625 marker = dot;
626 else
627 {
628 emsg(_("E172: Missing marker"));
629 return NULL;
630 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200631 }
632
633 l = list_alloc();
634 if (l == NULL)
635 return NULL;
636
637 for (;;)
638 {
639 int mi = 0;
640 int ti = 0;
641
642 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
643 if (theline == NULL)
644 {
645 semsg(_("E990: Missing end marker '%s'"), marker);
646 break;
647 }
648
649 // with "trim": skip the indent matching the :let line to find the
650 // marker
651 if (marker_indent_len > 0
652 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
653 mi = marker_indent_len;
654 if (STRCMP(marker, theline + mi) == 0)
655 {
656 vim_free(theline);
657 break;
658 }
659
660 if (text_indent_len == -1 && *theline != NUL)
661 {
662 // set the text indent from the first line.
663 p = theline;
664 text_indent_len = 0;
665 while (VIM_ISWHITE(*p))
666 {
667 p++;
668 text_indent_len++;
669 }
670 text_indent = vim_strnsave(theline, text_indent_len);
671 }
672 // with "trim": skip the indent matching the first line
673 if (text_indent != NULL)
674 for (ti = 0; ti < text_indent_len; ++ti)
675 if (theline[ti] != text_indent[ti])
676 break;
677
678 if (list_append_string(l, theline + ti, -1) == FAIL)
679 break;
680 vim_free(theline);
681 }
682 vim_free(text_indent);
683
684 return l;
685}
686
687/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200688 * Vim9 variable declaration:
689 * ":var name"
690 * ":var name: type"
691 * ":var name = expr"
692 * ":var name: type = expr"
693 * etc.
694 */
695 void
696ex_var(exarg_T *eap)
697{
698 if (!in_vim9script())
699 {
700 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
701 return;
702 }
703 ex_let(eap);
704}
705
706/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200707 * ":let" list all variable values
708 * ":let var1 var2" list variable values
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 var .= expr" assignment command.
716 * ":let var ..= expr" assignment command.
717 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100719 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200720 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200721 * ":final var = expr" assignment command.
722 * ":final [var1, var2] = expr" unpack list.
723 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200724 * ":const" list all variable values
725 * ":const var1 var2" list variable values
726 * ":const var = expr" assignment command.
727 * ":const [var1, var2] = expr" unpack list.
728 */
729 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200730ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200731{
732 char_u *arg = eap->arg;
733 char_u *expr = NULL;
734 typval_T rettv;
735 int i;
736 int var_count = 0;
737 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200738 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200739 char_u *argend;
740 int first = TRUE;
741 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200742 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200743 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200744 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200745
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200746 if (eap->cmdidx == CMD_final && !vim9script)
747 {
748 // In legacy Vim script ":final" is short for ":finally".
749 ex_finally(eap);
750 return;
751 }
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200752 if (get_vim_var_nr(VV_DISALLOW_LET)
753 && eap->cmdidx == CMD_let && vim9script)
754 {
755 emsg(_(e_cannot_use_let_in_vim9_script));
756 return;
757 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200758 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
759 // In legacy Vim script ":const" works like ":final".
760 eap->cmdidx = CMD_final;
761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 // detect Vim9 assignment without ":let" or ":const"
763 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200764 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200766 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767 if (argend == NULL)
768 return;
769 if (argend > arg && argend[-1] == '.') // for var.='str'
770 --argend;
771 expr = skipwhite(argend);
772 concat = expr[0] == '.'
773 && ((expr[1] == '=' && current_sctx.sc_version < 2)
774 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200775 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
776 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200777 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200778 {
779 // ":let" without "=": list variables
780 if (*arg == '[')
781 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200782 else if (expr[0] == '.' && expr[1] == '=')
783 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200784 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200785 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200786 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200787 {
788 // Vim9 declaration ":let var: type"
789 arg = vim9_declare_scriptvar(eap, arg);
790 }
791 else
792 {
793 // ":let var1 var2" - list values
794 arg = list_arg_vars(eap, arg, &first);
795 }
796 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200797 else if (!eap->skip)
798 {
799 // ":let"
800 list_glob_vars(&first);
801 list_buf_vars(&first);
802 list_win_vars(&first);
803 list_tab_vars(&first);
804 list_script_vars(&first);
805 list_func_vars(&first);
806 list_vim_vars(&first);
807 }
808 eap->nextcmd = check_nextcmd(arg);
809 }
810 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
811 {
812 list_T *l;
813
814 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200815 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 if (l != NULL)
817 {
818 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200819 if (!eap->skip)
820 {
821 op[0] = '=';
822 op[1] = NUL;
823 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200825 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200826 clear_tv(&rettv);
827 }
828 }
829 else
830 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200831 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200832 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200833
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200834 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200835 i = FAIL;
836 if (has_assign || concat)
837 {
838 op[0] = '=';
839 op[1] = NUL;
840 if (*expr != '=')
841 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200842 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200843 {
844 // +=, /=, etc. require an existing variable
845 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
846 i = FAIL;
847 }
848 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200849 {
850 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200851 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200852 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200853 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200854 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200855 ++len;
856 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200857 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200858 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200859 }
860 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200861 ++expr;
862
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200863 if (vim9script && (!VIM_ISWHITE(*argend)
864 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200865 {
866 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200867 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200868 i = FAIL;
869 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200870
871 if (eap->skip)
872 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200873 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200874 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200875 if (getline_equal(eap->getline, eap->cookie, getsourceline))
876 {
877 evalarg.eval_getline = eap->getline;
878 evalarg.eval_cookie = eap->cookie;
879 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200880 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200881 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200882 if (eap->skip)
883 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200884 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200885 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200886 if (eap->skip)
887 {
888 if (i != FAIL)
889 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200890 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200891 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200892 {
893 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200894 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200895 clear_tv(&rettv);
896 }
897 }
898}
899
900/*
901 * Assign the typevalue "tv" to the variable or variables at "arg_start".
902 * Handles both "var" with any type and "[var, var; var]" with a list type.
903 * When "op" is not NULL it points to a string with characters that
904 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
905 * or concatenate.
906 * Returns OK or FAIL;
907 */
908 int
909ex_let_vars(
910 char_u *arg_start,
911 typval_T *tv,
912 int copy, // copy values from "tv", don't move
913 int semicolon, // from skip_var_list()
914 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200915 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200916 char_u *op)
917{
918 char_u *arg = arg_start;
919 list_T *l;
920 int i;
921 listitem_T *item;
922 typval_T ltv;
923
924 if (*arg != '[')
925 {
926 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200928 return FAIL;
929 return OK;
930 }
931
932 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
933 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
934 {
935 emsg(_(e_listreq));
936 return FAIL;
937 }
938
939 i = list_len(l);
940 if (semicolon == 0 && var_count < i)
941 {
942 emsg(_("E687: Less targets than List items"));
943 return FAIL;
944 }
945 if (var_count - semicolon > i)
946 {
947 emsg(_("E688: More targets than List items"));
948 return FAIL;
949 }
950
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200951 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200952 item = l->lv_first;
953 while (*arg != ']')
954 {
955 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 item = item->li_next;
958 if (arg == NULL)
959 return FAIL;
960
961 arg = skipwhite(arg);
962 if (*arg == ';')
963 {
964 // Put the rest of the list (may be empty) in the var after ';'.
965 // Create a new list for this.
966 l = list_alloc();
967 if (l == NULL)
968 return FAIL;
969 while (item != NULL)
970 {
971 list_append_tv(l, &item->li_tv);
972 item = item->li_next;
973 }
974
975 ltv.v_type = VAR_LIST;
976 ltv.v_lock = 0;
977 ltv.vval.v_list = l;
978 l->lv_refcount = 1;
979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 (char_u *)"]", op);
982 clear_tv(&ltv);
983 if (arg == NULL)
984 return FAIL;
985 break;
986 }
987 else if (*arg != ',' && *arg != ']')
988 {
989 internal_error("ex_let_vars()");
990 return FAIL;
991 }
992 }
993
994 return OK;
995}
996
997/*
998 * Skip over assignable variable "var" or list of variables "[var, var]".
999 * Used for ":let varvar = expr" and ":for varvar in expr".
1000 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001001 * for "[var, var; var]" set "semicolon" to 1.
1002 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001003 * Return NULL for an error.
1004 */
1005 char_u *
1006skip_var_list(
1007 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001009 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001010 int *semicolon,
1011 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001012{
1013 char_u *p, *s;
1014
1015 if (*arg == '[')
1016 {
1017 // "[var, var]": find the matching ']'.
1018 p = arg;
1019 for (;;)
1020 {
1021 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001022 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001023 if (s == p)
1024 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001025 if (!silent)
1026 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027 return NULL;
1028 }
1029 ++*var_count;
1030
1031 p = skipwhite(s);
1032 if (*p == ']')
1033 break;
1034 else if (*p == ';')
1035 {
1036 if (*semicolon == 1)
1037 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001038 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001039 return NULL;
1040 }
1041 *semicolon = 1;
1042 }
1043 else if (*p != ',')
1044 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001045 if (!silent)
1046 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 return NULL;
1048 }
1049 }
1050 return p + 1;
1051 }
1052 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001054}
1055
1056/*
1057 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1058 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001060 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001061 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001063{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 char_u *end;
1065
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 if (*arg == '@' && arg[1] != NUL)
1067 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001069 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001070 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001072 // "a: type" is declaring variable "a" with a type, not "a:".
1073 if (end == arg + 2 && end[-1] == ':')
1074 --end;
1075 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001076 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 }
1078 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001079}
1080
1081/*
1082 * List variables for hashtab "ht" with prefix "prefix".
1083 * If "empty" is TRUE also list NULL strings as empty strings.
1084 */
1085 void
1086list_hashtable_vars(
1087 hashtab_T *ht,
1088 char *prefix,
1089 int empty,
1090 int *first)
1091{
1092 hashitem_T *hi;
1093 dictitem_T *di;
1094 int todo;
1095 char_u buf[IOSIZE];
1096
1097 todo = (int)ht->ht_used;
1098 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1099 {
1100 if (!HASHITEM_EMPTY(hi))
1101 {
1102 --todo;
1103 di = HI2DI(hi);
1104
1105 // apply :filter /pat/ to variable name
1106 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1107 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1108 if (message_filtered(buf))
1109 continue;
1110
1111 if (empty || di->di_tv.v_type != VAR_STRING
1112 || di->di_tv.vval.v_string != NULL)
1113 list_one_var(di, prefix, first);
1114 }
1115 }
1116}
1117
1118/*
1119 * List global variables.
1120 */
1121 static void
1122list_glob_vars(int *first)
1123{
1124 list_hashtable_vars(&globvarht, "", TRUE, first);
1125}
1126
1127/*
1128 * List buffer variables.
1129 */
1130 static void
1131list_buf_vars(int *first)
1132{
1133 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1134}
1135
1136/*
1137 * List window variables.
1138 */
1139 static void
1140list_win_vars(int *first)
1141{
1142 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1143}
1144
1145/*
1146 * List tab page variables.
1147 */
1148 static void
1149list_tab_vars(int *first)
1150{
1151 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1152}
1153
1154/*
1155 * List variables in "arg".
1156 */
1157 static char_u *
1158list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1159{
1160 int error = FALSE;
1161 int len;
1162 char_u *name;
1163 char_u *name_start;
1164 char_u *arg_subsc;
1165 char_u *tofree;
1166 typval_T tv;
1167
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001168 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001169 {
1170 if (error || eap->skip)
1171 {
1172 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1173 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1174 {
1175 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001176 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001177 break;
1178 }
1179 }
1180 else
1181 {
1182 // get_name_len() takes care of expanding curly braces
1183 name_start = name = arg;
1184 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1185 if (len <= 0)
1186 {
1187 // This is mainly to keep test 49 working: when expanding
1188 // curly braces fails overrule the exception error message.
1189 if (len < 0 && !aborting())
1190 {
1191 emsg_severe = TRUE;
1192 semsg(_(e_invarg2), arg);
1193 break;
1194 }
1195 error = TRUE;
1196 }
1197 else
1198 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001199 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001200 if (tofree != NULL)
1201 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001202 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001203 error = TRUE;
1204 else
1205 {
1206 // handle d.key, l[idx], f(expr)
1207 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001208 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001209 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001210 error = TRUE;
1211 else
1212 {
1213 if (arg == arg_subsc && len == 2 && name[1] == ':')
1214 {
1215 switch (*name)
1216 {
1217 case 'g': list_glob_vars(first); break;
1218 case 'b': list_buf_vars(first); break;
1219 case 'w': list_win_vars(first); break;
1220 case 't': list_tab_vars(first); break;
1221 case 'v': list_vim_vars(first); break;
1222 case 's': list_script_vars(first); break;
1223 case 'l': list_func_vars(first); break;
1224 default:
1225 semsg(_("E738: Can't list variables for %s"), name);
1226 }
1227 }
1228 else
1229 {
1230 char_u numbuf[NUMBUFLEN];
1231 char_u *tf;
1232 int c;
1233 char_u *s;
1234
1235 s = echo_string(&tv, &tf, numbuf, 0);
1236 c = *arg;
1237 *arg = NUL;
1238 list_one_var_a("",
1239 arg == arg_subsc ? name : name_start,
1240 tv.v_type,
1241 s == NULL ? (char_u *)"" : s,
1242 first);
1243 *arg = c;
1244 vim_free(tf);
1245 }
1246 clear_tv(&tv);
1247 }
1248 }
1249 }
1250
1251 vim_free(tofree);
1252 }
1253
1254 arg = skipwhite(arg);
1255 }
1256
1257 return arg;
1258}
1259
1260/*
1261 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1262 * Returns a pointer to the char just after the var name.
1263 * Returns NULL if there is an error.
1264 */
1265 static char_u *
1266ex_let_one(
1267 char_u *arg, // points to variable name
1268 typval_T *tv, // value to assign to variable
1269 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001270 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 char_u *endchars, // valid chars after variable name or NULL
1272 char_u *op) // "+", "-", "." or NULL
1273{
1274 int c1;
1275 char_u *name;
1276 char_u *p;
1277 char_u *arg_end = NULL;
1278 int len;
1279 int opt_flags;
1280 char_u *tofree = NULL;
1281
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001282 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001283 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1284 {
1285 vim9_declare_error(arg);
1286 return NULL;
1287 }
1288
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001289 // ":let $VAR = expr": Set environment variable.
1290 if (*arg == '$')
1291 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001292 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 {
1294 emsg(_("E996: Cannot lock an environment variable"));
1295 return NULL;
1296 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001297
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 // Find the end of the name.
1299 ++arg;
1300 name = arg;
1301 len = get_env_len(&arg);
1302 if (len == 0)
1303 semsg(_(e_invarg2), name - 1);
1304 else
1305 {
1306 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1307 semsg(_(e_letwrong), op);
1308 else if (endchars != NULL
1309 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1310 emsg(_(e_letunexp));
1311 else if (!check_secure())
1312 {
1313 c1 = name[len];
1314 name[len] = NUL;
1315 p = tv_get_string_chk(tv);
1316 if (p != NULL && op != NULL && *op == '.')
1317 {
1318 int mustfree = FALSE;
1319 char_u *s = vim_getenv(name, &mustfree);
1320
1321 if (s != NULL)
1322 {
1323 p = tofree = concat_str(s, p);
1324 if (mustfree)
1325 vim_free(s);
1326 }
1327 }
1328 if (p != NULL)
1329 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001331 arg_end = arg;
1332 }
1333 name[len] = c1;
1334 vim_free(tofree);
1335 }
1336 }
1337 }
1338
1339 // ":let &option = expr": Set option value.
1340 // ":let &l:option = expr": Set local option value.
1341 // ":let &g:option = expr": Set global option value.
1342 else if (*arg == '&')
1343 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001344 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001345 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001347 return NULL;
1348 }
1349 // Find the end of the name.
1350 p = find_option_end(&arg, &opt_flags);
1351 if (p == NULL || (endchars != NULL
1352 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1353 emsg(_(e_letunexp));
1354 else
1355 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001356 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357 int opt_type;
1358 long numval;
1359 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001360 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001361 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001362
1363 c1 = *p;
1364 *p = NUL;
1365
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001366 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1367 if ((opt_type == 1 || opt_type == -1)
1368 && (tv->v_type != VAR_STRING || !in_vim9script()))
1369 // number, possibly hidden
1370 n = (long)tv_get_number(tv);
1371
1372 // Avoid setting a string option to the text "v:false" or similar.
1373 // In Vim9 script also don't convert a number to string.
1374 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1375 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1376 s = tv_get_string_chk(tv);
1377
1378 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001379 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001380 if ((opt_type == 1 && *op == '.')
1381 || (opt_type == 0 && *op != '.'))
1382 {
1383 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001384 failed = TRUE; // don't set the value
1385
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001386 }
1387 else
1388 {
1389 if (opt_type == 1) // number
1390 {
1391 switch (*op)
1392 {
1393 case '+': n = numval + n; break;
1394 case '-': n = numval - n; break;
1395 case '*': n = numval * n; break;
1396 case '/': n = (long)num_divide(numval, n); break;
1397 case '%': n = (long)num_modulus(numval, n); break;
1398 }
1399 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001400 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001402 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403 s = concat_str(stringval, s);
1404 vim_free(stringval);
1405 stringval = s;
1406 }
1407 }
1408 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001409
1410 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001411 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001412 if (opt_type != 0 || s != NULL)
1413 {
1414 set_option_value(arg, n, s, opt_flags);
1415 arg_end = p;
1416 }
1417 else
1418 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001419 }
1420 *p = c1;
1421 vim_free(stringval);
1422 }
1423 }
1424
1425 // ":let @r = expr": Set register contents.
1426 else if (*arg == '@')
1427 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001428 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001429 {
1430 emsg(_("E996: Cannot lock a register"));
1431 return NULL;
1432 }
1433 ++arg;
1434 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1435 semsg(_(e_letwrong), op);
1436 else if (endchars != NULL
1437 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1438 emsg(_(e_letunexp));
1439 else
1440 {
1441 char_u *ptofree = NULL;
1442 char_u *s;
1443
1444 p = tv_get_string_chk(tv);
1445 if (p != NULL && op != NULL && *op == '.')
1446 {
1447 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1448 if (s != NULL)
1449 {
1450 p = ptofree = concat_str(s, p);
1451 vim_free(s);
1452 }
1453 }
1454 if (p != NULL)
1455 {
1456 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1457 arg_end = arg + 1;
1458 }
1459 vim_free(ptofree);
1460 }
1461 }
1462
1463 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 // ":let {expr} = expr": Idem, name made with curly braces
1466 else if (eval_isnamec1(*arg) || *arg == '{')
1467 {
1468 lval_T lv;
1469
1470 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001471 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001472 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 if (endchars != NULL && vim_strchr(endchars,
1474 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001475 emsg(_(e_letunexp));
1476 else
1477 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001479 arg_end = p;
1480 }
1481 }
1482 clear_lval(&lv);
1483 }
1484
1485 else
1486 semsg(_(e_invarg2), arg);
1487
1488 return arg_end;
1489}
1490
1491/*
1492 * ":unlet[!] var1 ... " command.
1493 */
1494 void
1495ex_unlet(exarg_T *eap)
1496{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001497 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001498}
1499
1500/*
1501 * ":lockvar" and ":unlockvar" commands
1502 */
1503 void
1504ex_lockvar(exarg_T *eap)
1505{
1506 char_u *arg = eap->arg;
1507 int deep = 2;
1508
1509 if (eap->forceit)
1510 deep = -1;
1511 else if (vim_isdigit(*arg))
1512 {
1513 deep = getdigits(&arg);
1514 arg = skipwhite(arg);
1515 }
1516
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001517 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001518}
1519
1520/*
1521 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001522 * Also used for Vim9 script. "callback" is invoked as:
1523 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001525 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001526ex_unletlock(
1527 exarg_T *eap,
1528 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001529 int deep,
1530 int glv_flags,
1531 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1532 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001533{
1534 char_u *arg = argstart;
1535 char_u *name_end;
1536 int error = FALSE;
1537 lval_T lv;
1538
1539 do
1540 {
1541 if (*arg == '$')
1542 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001543 lv.ll_name = arg;
1544 lv.ll_tv = NULL;
1545 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546 if (get_env_len(&arg) == 0)
1547 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001548 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001549 return;
1550 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001551 if (!error && !eap->skip
1552 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1553 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001554 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001555 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001556 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001557 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001558 // Parse the name and find the end.
1559 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1560 glv_flags, FNE_CHECK_START);
1561 if (lv.ll_name == NULL)
1562 error = TRUE; // error but continue parsing
1563 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1564 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001565 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001566 if (name_end != NULL)
1567 {
1568 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001569 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001570 }
1571 if (!(eap->skip || error))
1572 clear_lval(&lv);
1573 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001574 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001576 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001577 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001578 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001579
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001580 if (!eap->skip)
1581 clear_lval(&lv);
1582 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583
1584 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001585 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001586
1587 eap->nextcmd = check_nextcmd(arg);
1588}
1589
1590 static int
1591do_unlet_var(
1592 lval_T *lp,
1593 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001594 exarg_T *eap,
1595 int deep UNUSED,
1596 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001597{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001598 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599 int ret = OK;
1600 int cc;
1601
1602 if (lp->ll_tv == NULL)
1603 {
1604 cc = *name_end;
1605 *name_end = NUL;
1606
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001607 // Environment variable, normal name or expanded name.
1608 if (*lp->ll_name == '$')
1609 vim_unsetenv(lp->ll_name + 1);
1610 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001611 ret = FAIL;
1612 *name_end = cc;
1613 }
1614 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001615 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001616 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001617 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001618 return FAIL;
1619 else if (lp->ll_range)
1620 {
1621 listitem_T *li;
1622 listitem_T *ll_li = lp->ll_li;
1623 int ll_n1 = lp->ll_n1;
1624
1625 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1626 {
1627 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001628 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001629 return FAIL;
1630 ll_li = li;
1631 ++ll_n1;
1632 }
1633
1634 // Delete a range of List items.
1635 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1636 {
1637 li = lp->ll_li->li_next;
1638 listitem_remove(lp->ll_list, lp->ll_li);
1639 lp->ll_li = li;
1640 ++lp->ll_n1;
1641 }
1642 }
1643 else
1644 {
1645 if (lp->ll_list != NULL)
1646 // unlet a List item.
1647 listitem_remove(lp->ll_list, lp->ll_li);
1648 else
1649 // unlet a Dictionary item.
1650 dictitem_remove(lp->ll_dict, lp->ll_di);
1651 }
1652
1653 return ret;
1654}
1655
1656/*
1657 * "unlet" a variable. Return OK if it existed, FAIL if not.
1658 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1659 */
1660 int
1661do_unlet(char_u *name, int forceit)
1662{
1663 hashtab_T *ht;
1664 hashitem_T *hi;
1665 char_u *varname;
1666 dict_T *d;
1667 dictitem_T *di;
1668
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001669 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001670 return FAIL;
1671
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001672 ht = find_var_ht(name, &varname);
1673 if (ht != NULL && *varname != NUL)
1674 {
1675 d = get_current_funccal_dict(ht);
1676 if (d == NULL)
1677 {
1678 if (ht == &globvarht)
1679 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001680 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001681 d = &vimvardict;
1682 else
1683 {
1684 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1685 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1686 }
1687 if (d == NULL)
1688 {
1689 internal_error("do_unlet()");
1690 return FAIL;
1691 }
1692 }
1693 hi = hash_find(ht, varname);
1694 if (HASHITEM_EMPTY(hi))
1695 hi = find_hi_in_scoped_ht(name, &ht);
1696 if (hi != NULL && !HASHITEM_EMPTY(hi))
1697 {
1698 di = HI2DI(hi);
1699 if (var_check_fixed(di->di_flags, name, FALSE)
1700 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001701 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001702 return FAIL;
1703
1704 delete_var(ht, hi);
1705 return OK;
1706 }
1707 }
1708 if (forceit)
1709 return OK;
1710 semsg(_("E108: No such variable: \"%s\""), name);
1711 return FAIL;
1712}
1713
1714/*
1715 * Lock or unlock variable indicated by "lp".
1716 * "deep" is the levels to go (-1 for unlimited);
1717 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1718 */
1719 static int
1720do_lock_var(
1721 lval_T *lp,
1722 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001723 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001724 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001725 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001726{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001727 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001728 int ret = OK;
1729 int cc;
1730 dictitem_T *di;
1731
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001732 if (lp->ll_tv == NULL)
1733 {
1734 cc = *name_end;
1735 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001736 if (*lp->ll_name == '$')
1737 {
1738 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001739 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001740 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001741 else
1742 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001743 // Normal name or expanded name.
1744 di = find_var(lp->ll_name, NULL, TRUE);
1745 if (di == NULL)
1746 ret = FAIL;
1747 else if ((di->di_flags & DI_FLAGS_FIX)
1748 && di->di_tv.v_type != VAR_DICT
1749 && di->di_tv.v_type != VAR_LIST)
1750 {
1751 // For historic reasons this error is not given for a list or
1752 // dict. E.g., the b: dict could be locked/unlocked.
1753 semsg(_(e_lock_unlock), lp->ll_name);
1754 ret = FAIL;
1755 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001756 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001757 {
1758 if (lock)
1759 di->di_flags |= DI_FLAGS_LOCK;
1760 else
1761 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001762 if (deep != 0)
1763 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001764 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001765 }
1766 *name_end = cc;
1767 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001768 else if (deep == 0)
1769 {
1770 // nothing to do
1771 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001772 else if (lp->ll_range)
1773 {
1774 listitem_T *li = lp->ll_li;
1775
1776 // (un)lock a range of List items.
1777 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1778 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001779 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001780 li = li->li_next;
1781 ++lp->ll_n1;
1782 }
1783 }
1784 else if (lp->ll_list != NULL)
1785 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001786 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001787 else
1788 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001789 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001790
1791 return ret;
1792}
1793
1794/*
1795 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001796 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1797 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001798 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001799 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001800item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801{
1802 static int recurse = 0;
1803 list_T *l;
1804 listitem_T *li;
1805 dict_T *d;
1806 blob_T *b;
1807 hashitem_T *hi;
1808 int todo;
1809
1810 if (recurse >= DICT_MAXNEST)
1811 {
1812 emsg(_("E743: variable nested too deep for (un)lock"));
1813 return;
1814 }
1815 if (deep == 0)
1816 return;
1817 ++recurse;
1818
1819 // lock/unlock the item itself
1820 if (lock)
1821 tv->v_lock |= VAR_LOCKED;
1822 else
1823 tv->v_lock &= ~VAR_LOCKED;
1824
1825 switch (tv->v_type)
1826 {
1827 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001828 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001830 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 case VAR_STRING:
1833 case VAR_FUNC:
1834 case VAR_PARTIAL:
1835 case VAR_FLOAT:
1836 case VAR_SPECIAL:
1837 case VAR_JOB:
1838 case VAR_CHANNEL:
1839 break;
1840
1841 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001842 if ((b = tv->vval.v_blob) != NULL
1843 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001844 {
1845 if (lock)
1846 b->bv_lock |= VAR_LOCKED;
1847 else
1848 b->bv_lock &= ~VAR_LOCKED;
1849 }
1850 break;
1851 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001852 if ((l = tv->vval.v_list) != NULL
1853 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001854 {
1855 if (lock)
1856 l->lv_lock |= VAR_LOCKED;
1857 else
1858 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001859 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001860 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001861 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001862 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001863 }
1864 break;
1865 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001866 if ((d = tv->vval.v_dict) != NULL
1867 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001868 {
1869 if (lock)
1870 d->dv_lock |= VAR_LOCKED;
1871 else
1872 d->dv_lock &= ~VAR_LOCKED;
1873 if (deep < 0 || deep > 1)
1874 {
1875 // recursive: lock/unlock the items the List contains
1876 todo = (int)d->dv_hashtab.ht_used;
1877 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1878 {
1879 if (!HASHITEM_EMPTY(hi))
1880 {
1881 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001882 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1883 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001884 }
1885 }
1886 }
1887 }
1888 }
1889 --recurse;
1890}
1891
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001892#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1893/*
1894 * Delete all "menutrans_" variables.
1895 */
1896 void
1897del_menutrans_vars(void)
1898{
1899 hashitem_T *hi;
1900 int todo;
1901
1902 hash_lock(&globvarht);
1903 todo = (int)globvarht.ht_used;
1904 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1905 {
1906 if (!HASHITEM_EMPTY(hi))
1907 {
1908 --todo;
1909 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1910 delete_var(&globvarht, hi);
1911 }
1912 }
1913 hash_unlock(&globvarht);
1914}
1915#endif
1916
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001917/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001918 * Local string buffer for the next two functions to store a variable name
1919 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1920 * get_user_var_name().
1921 */
1922
1923static char_u *varnamebuf = NULL;
1924static int varnamebuflen = 0;
1925
1926/*
1927 * Function to concatenate a prefix and a variable name.
1928 */
1929 static char_u *
1930cat_prefix_varname(int prefix, char_u *name)
1931{
1932 int len;
1933
1934 len = (int)STRLEN(name) + 3;
1935 if (len > varnamebuflen)
1936 {
1937 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001938 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001939 varnamebuf = alloc(len);
1940 if (varnamebuf == NULL)
1941 {
1942 varnamebuflen = 0;
1943 return NULL;
1944 }
1945 varnamebuflen = len;
1946 }
1947 *varnamebuf = prefix;
1948 varnamebuf[1] = ':';
1949 STRCPY(varnamebuf + 2, name);
1950 return varnamebuf;
1951}
1952
1953/*
1954 * Function given to ExpandGeneric() to obtain the list of user defined
1955 * (global/buffer/window/built-in) variable names.
1956 */
1957 char_u *
1958get_user_var_name(expand_T *xp, int idx)
1959{
1960 static long_u gdone;
1961 static long_u bdone;
1962 static long_u wdone;
1963 static long_u tdone;
1964 static int vidx;
1965 static hashitem_T *hi;
1966 hashtab_T *ht;
1967
1968 if (idx == 0)
1969 {
1970 gdone = bdone = wdone = vidx = 0;
1971 tdone = 0;
1972 }
1973
1974 // Global variables
1975 if (gdone < globvarht.ht_used)
1976 {
1977 if (gdone++ == 0)
1978 hi = globvarht.ht_array;
1979 else
1980 ++hi;
1981 while (HASHITEM_EMPTY(hi))
1982 ++hi;
1983 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1984 return cat_prefix_varname('g', hi->hi_key);
1985 return hi->hi_key;
1986 }
1987
1988 // b: variables
1989 ht = &curbuf->b_vars->dv_hashtab;
1990 if (bdone < ht->ht_used)
1991 {
1992 if (bdone++ == 0)
1993 hi = ht->ht_array;
1994 else
1995 ++hi;
1996 while (HASHITEM_EMPTY(hi))
1997 ++hi;
1998 return cat_prefix_varname('b', hi->hi_key);
1999 }
2000
2001 // w: variables
2002 ht = &curwin->w_vars->dv_hashtab;
2003 if (wdone < ht->ht_used)
2004 {
2005 if (wdone++ == 0)
2006 hi = ht->ht_array;
2007 else
2008 ++hi;
2009 while (HASHITEM_EMPTY(hi))
2010 ++hi;
2011 return cat_prefix_varname('w', hi->hi_key);
2012 }
2013
2014 // t: variables
2015 ht = &curtab->tp_vars->dv_hashtab;
2016 if (tdone < ht->ht_used)
2017 {
2018 if (tdone++ == 0)
2019 hi = ht->ht_array;
2020 else
2021 ++hi;
2022 while (HASHITEM_EMPTY(hi))
2023 ++hi;
2024 return cat_prefix_varname('t', hi->hi_key);
2025 }
2026
2027 // v: variables
2028 if (vidx < VV_LEN)
2029 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2030
2031 VIM_CLEAR(varnamebuf);
2032 varnamebuflen = 0;
2033 return NULL;
2034}
2035
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002036 char *
2037get_var_special_name(int nr)
2038{
2039 switch (nr)
2040 {
2041 case VVAL_FALSE: return "v:false";
2042 case VVAL_TRUE: return "v:true";
2043 case VVAL_NONE: return "v:none";
2044 case VVAL_NULL: return "v:null";
2045 }
2046 internal_error("get_var_special_name()");
2047 return "42";
2048}
2049
2050/*
2051 * Returns the global variable dictionary
2052 */
2053 dict_T *
2054get_globvar_dict(void)
2055{
2056 return &globvardict;
2057}
2058
2059/*
2060 * Returns the global variable hash table
2061 */
2062 hashtab_T *
2063get_globvar_ht(void)
2064{
2065 return &globvarht;
2066}
2067
2068/*
2069 * Returns the v: variable dictionary
2070 */
2071 dict_T *
2072get_vimvar_dict(void)
2073{
2074 return &vimvardict;
2075}
2076
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002077/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002079 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 */
2081 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002082find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002084 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2085 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086
2087 if (di == NULL)
2088 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002089 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2091 return (int)(vv - vimvars);
2092}
2093
2094
2095/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002096 * Set type of v: variable to "type".
2097 */
2098 void
2099set_vim_var_type(int idx, vartype_T type)
2100{
2101 vimvars[idx].vv_type = type;
2102}
2103
2104/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002105 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002106 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002107 */
2108 void
2109set_vim_var_nr(int idx, varnumber_T val)
2110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002111 vimvars[idx].vv_nr = val;
2112}
2113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 char *
2115get_vim_var_name(int idx)
2116{
2117 return vimvars[idx].vv_name;
2118}
2119
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002120/*
2121 * Get typval_T v: variable value.
2122 */
2123 typval_T *
2124get_vim_var_tv(int idx)
2125{
2126 return &vimvars[idx].vv_tv;
2127}
2128
2129/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002130 * Set v: variable to "tv". Only accepts the same type.
2131 * Takes over the value of "tv".
2132 */
2133 int
2134set_vim_var_tv(int idx, typval_T *tv)
2135{
2136 if (vimvars[idx].vv_type != tv->v_type)
2137 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002138 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002139 clear_tv(tv);
2140 return FAIL;
2141 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002142 // VV_RO is also checked when compiling, but let's check here as well.
2143 if (vimvars[idx].vv_flags & VV_RO)
2144 {
2145 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2146 return FAIL;
2147 }
2148 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2149 {
2150 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2151 return FAIL;
2152 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002153 clear_tv(&vimvars[idx].vv_di.di_tv);
2154 vimvars[idx].vv_di.di_tv = *tv;
2155 return OK;
2156}
2157
2158/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002159 * Get number v: variable value.
2160 */
2161 varnumber_T
2162get_vim_var_nr(int idx)
2163{
2164 return vimvars[idx].vv_nr;
2165}
2166
2167/*
2168 * Get string v: variable value. Uses a static buffer, can only be used once.
2169 * If the String variable has never been set, return an empty string.
2170 * Never returns NULL;
2171 */
2172 char_u *
2173get_vim_var_str(int idx)
2174{
2175 return tv_get_string(&vimvars[idx].vv_tv);
2176}
2177
2178/*
2179 * Get List v: variable value. Caller must take care of reference count when
2180 * needed.
2181 */
2182 list_T *
2183get_vim_var_list(int idx)
2184{
2185 return vimvars[idx].vv_list;
2186}
2187
2188/*
2189 * Get Dict v: variable value. Caller must take care of reference count when
2190 * needed.
2191 */
2192 dict_T *
2193get_vim_var_dict(int idx)
2194{
2195 return vimvars[idx].vv_dict;
2196}
2197
2198/*
2199 * Set v:char to character "c".
2200 */
2201 void
2202set_vim_var_char(int c)
2203{
2204 char_u buf[MB_MAXBYTES + 1];
2205
2206 if (has_mbyte)
2207 buf[(*mb_char2bytes)(c, buf)] = NUL;
2208 else
2209 {
2210 buf[0] = c;
2211 buf[1] = NUL;
2212 }
2213 set_vim_var_string(VV_CHAR, buf, -1);
2214}
2215
2216/*
2217 * Set v:count to "count" and v:count1 to "count1".
2218 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2219 */
2220 void
2221set_vcount(
2222 long count,
2223 long count1,
2224 int set_prevcount)
2225{
2226 if (set_prevcount)
2227 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2228 vimvars[VV_COUNT].vv_nr = count;
2229 vimvars[VV_COUNT1].vv_nr = count1;
2230}
2231
2232/*
2233 * Save variables that might be changed as a side effect. Used when executing
2234 * a timer callback.
2235 */
2236 void
2237save_vimvars(vimvars_save_T *vvsave)
2238{
2239 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2240 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2241 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2242}
2243
2244/*
2245 * Restore variables saved by save_vimvars().
2246 */
2247 void
2248restore_vimvars(vimvars_save_T *vvsave)
2249{
2250 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2251 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2252 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2253}
2254
2255/*
2256 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2257 * value.
2258 */
2259 void
2260set_vim_var_string(
2261 int idx,
2262 char_u *val,
2263 int len) // length of "val" to use or -1 (whole string)
2264{
2265 clear_tv(&vimvars[idx].vv_di.di_tv);
2266 vimvars[idx].vv_type = VAR_STRING;
2267 if (val == NULL)
2268 vimvars[idx].vv_str = NULL;
2269 else if (len == -1)
2270 vimvars[idx].vv_str = vim_strsave(val);
2271 else
2272 vimvars[idx].vv_str = vim_strnsave(val, len);
2273}
2274
2275/*
2276 * Set List v: variable to "val".
2277 */
2278 void
2279set_vim_var_list(int idx, list_T *val)
2280{
2281 clear_tv(&vimvars[idx].vv_di.di_tv);
2282 vimvars[idx].vv_type = VAR_LIST;
2283 vimvars[idx].vv_list = val;
2284 if (val != NULL)
2285 ++val->lv_refcount;
2286}
2287
2288/*
2289 * Set Dictionary v: variable to "val".
2290 */
2291 void
2292set_vim_var_dict(int idx, dict_T *val)
2293{
2294 clear_tv(&vimvars[idx].vv_di.di_tv);
2295 vimvars[idx].vv_type = VAR_DICT;
2296 vimvars[idx].vv_dict = val;
2297 if (val != NULL)
2298 {
2299 ++val->dv_refcount;
2300 dict_set_items_ro(val);
2301 }
2302}
2303
2304/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002305 * Set the v:argv list.
2306 */
2307 void
2308set_argv_var(char **argv, int argc)
2309{
2310 list_T *l = list_alloc();
2311 int i;
2312
2313 if (l == NULL)
2314 getout(1);
2315 l->lv_lock = VAR_FIXED;
2316 for (i = 0; i < argc; ++i)
2317 {
2318 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2319 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002320 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002321 }
2322 set_vim_var_list(VV_ARGV, l);
2323}
2324
2325/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002326 * Reset v:register, taking the 'clipboard' setting into account.
2327 */
2328 void
2329reset_reg_var(void)
2330{
2331 int regname = 0;
2332
2333 // Adjust the register according to 'clipboard', so that when
2334 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2335#ifdef FEAT_CLIPBOARD
2336 adjust_clip_reg(&regname);
2337#endif
2338 set_reg_var(regname);
2339}
2340
2341/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002342 * Set v:register if needed.
2343 */
2344 void
2345set_reg_var(int c)
2346{
2347 char_u regname;
2348
2349 if (c == 0 || c == ' ')
2350 regname = '"';
2351 else
2352 regname = c;
2353 // Avoid free/alloc when the value is already right.
2354 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2355 set_vim_var_string(VV_REG, &regname, 1);
2356}
2357
2358/*
2359 * Get or set v:exception. If "oldval" == NULL, return the current value.
2360 * Otherwise, restore the value to "oldval" and return NULL.
2361 * Must always be called in pairs to save and restore v:exception! Does not
2362 * take care of memory allocations.
2363 */
2364 char_u *
2365v_exception(char_u *oldval)
2366{
2367 if (oldval == NULL)
2368 return vimvars[VV_EXCEPTION].vv_str;
2369
2370 vimvars[VV_EXCEPTION].vv_str = oldval;
2371 return NULL;
2372}
2373
2374/*
2375 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2376 * Otherwise, restore the value to "oldval" and return NULL.
2377 * Must always be called in pairs to save and restore v:throwpoint! Does not
2378 * take care of memory allocations.
2379 */
2380 char_u *
2381v_throwpoint(char_u *oldval)
2382{
2383 if (oldval == NULL)
2384 return vimvars[VV_THROWPOINT].vv_str;
2385
2386 vimvars[VV_THROWPOINT].vv_str = oldval;
2387 return NULL;
2388}
2389
2390/*
2391 * Set v:cmdarg.
2392 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2393 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2394 * Must always be called in pairs!
2395 */
2396 char_u *
2397set_cmdarg(exarg_T *eap, char_u *oldarg)
2398{
2399 char_u *oldval;
2400 char_u *newval;
2401 unsigned len;
2402
2403 oldval = vimvars[VV_CMDARG].vv_str;
2404 if (eap == NULL)
2405 {
2406 vim_free(oldval);
2407 vimvars[VV_CMDARG].vv_str = oldarg;
2408 return NULL;
2409 }
2410
2411 if (eap->force_bin == FORCE_BIN)
2412 len = 6;
2413 else if (eap->force_bin == FORCE_NOBIN)
2414 len = 8;
2415 else
2416 len = 0;
2417
2418 if (eap->read_edit)
2419 len += 7;
2420
2421 if (eap->force_ff != 0)
2422 len += 10; // " ++ff=unix"
2423 if (eap->force_enc != 0)
2424 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2425 if (eap->bad_char != 0)
2426 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2427
2428 newval = alloc(len + 1);
2429 if (newval == NULL)
2430 return NULL;
2431
2432 if (eap->force_bin == FORCE_BIN)
2433 sprintf((char *)newval, " ++bin");
2434 else if (eap->force_bin == FORCE_NOBIN)
2435 sprintf((char *)newval, " ++nobin");
2436 else
2437 *newval = NUL;
2438
2439 if (eap->read_edit)
2440 STRCAT(newval, " ++edit");
2441
2442 if (eap->force_ff != 0)
2443 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2444 eap->force_ff == 'u' ? "unix"
2445 : eap->force_ff == 'd' ? "dos"
2446 : "mac");
2447 if (eap->force_enc != 0)
2448 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2449 eap->cmd + eap->force_enc);
2450 if (eap->bad_char == BAD_KEEP)
2451 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2452 else if (eap->bad_char == BAD_DROP)
2453 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2454 else if (eap->bad_char != 0)
2455 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2456 vimvars[VV_CMDARG].vv_str = newval;
2457 return oldval;
2458}
2459
2460/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002461 * Get the value of internal variable "name".
2462 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2463 */
2464 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002465eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002466 char_u *name,
2467 int len, // length of "name"
2468 typval_T *rettv, // NULL when only checking existence
2469 dictitem_T **dip, // non-NULL when typval's dict item is needed
2470 int verbose, // may give error message
2471 int no_autoload) // do not use script autoloading
2472{
2473 int ret = OK;
2474 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002475 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002476 dictitem_T *v;
2477 int cc;
2478
2479 // truncate the name, so that we can use strcmp()
2480 cc = name[len];
2481 name[len] = NUL;
2482
2483 // Check for user-defined variables.
2484 v = find_var(name, NULL, no_autoload);
2485 if (v != NULL)
2486 {
2487 tv = &v->di_tv;
2488 if (dip != NULL)
2489 *dip = v;
2490 }
2491
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002492 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002494 imported_T *import;
2495 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2496
2497 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498
2499 // imported variable from another script
2500 if (import != NULL)
2501 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002502 if (import->imp_funcname != NULL)
2503 {
2504 foundFunc = TRUE;
2505 if (rettv != NULL)
2506 {
2507 rettv->v_type = VAR_FUNC;
2508 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2509 }
2510 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002511 else if (import->imp_all)
2512 {
2513 emsg("Sorry, 'import * as X' not implemented yet");
2514 ret = FAIL;
2515 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002516 else
2517 {
2518 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002519 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002521 tv = sv->sv_tv;
2522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002524 else if (in_vim9script())
2525 {
2526 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2527
2528 if (ufunc != NULL)
2529 {
2530 foundFunc = TRUE;
2531 if (rettv != NULL)
2532 {
2533 rettv->v_type = VAR_FUNC;
2534 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2535 }
2536 }
2537 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 }
2539
Bram Moolenaarc620c052020-07-08 15:16:19 +02002540 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002541 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002542 if (tv == NULL)
2543 {
2544 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002545 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002546 ret = FAIL;
2547 }
2548 else if (rettv != NULL)
2549 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002550 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002551
2552 name[len] = cc;
2553
2554 return ret;
2555}
2556
2557/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002558 * Check if variable "name[len]" is a local variable or an argument.
2559 * If so, "*eval_lavars_used" is set to TRUE.
2560 */
2561 void
2562check_vars(char_u *name, int len)
2563{
2564 int cc;
2565 char_u *varname;
2566 hashtab_T *ht;
2567
2568 if (eval_lavars_used == NULL)
2569 return;
2570
2571 // truncate the name, so that we can use strcmp()
2572 cc = name[len];
2573 name[len] = NUL;
2574
2575 ht = find_var_ht(name, &varname);
2576 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2577 {
2578 if (find_var(name, NULL, TRUE) != NULL)
2579 *eval_lavars_used = TRUE;
2580 }
2581
2582 name[len] = cc;
2583}
2584
2585/*
2586 * Find variable "name" in the list of variables.
2587 * Return a pointer to it if found, NULL if not found.
2588 * Careful: "a:0" variables don't have a name.
2589 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2590 * hashtab_T used.
2591 */
2592 dictitem_T *
2593find_var(char_u *name, hashtab_T **htp, int no_autoload)
2594{
2595 char_u *varname;
2596 hashtab_T *ht;
2597 dictitem_T *ret = NULL;
2598
2599 ht = find_var_ht(name, &varname);
2600 if (htp != NULL)
2601 *htp = ht;
2602 if (ht == NULL)
2603 return NULL;
2604 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2605 if (ret != NULL)
2606 return ret;
2607
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002608 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002609 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2610}
2611
2612/*
2613 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002614 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002615 * Returns NULL if not found.
2616 */
2617 dictitem_T *
2618find_var_in_ht(
2619 hashtab_T *ht,
2620 int htname,
2621 char_u *varname,
2622 int no_autoload)
2623{
2624 hashitem_T *hi;
2625
2626 if (*varname == NUL)
2627 {
2628 // Must be something like "s:", otherwise "ht" would be NULL.
2629 switch (htname)
2630 {
2631 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2632 case 'g': return &globvars_var;
2633 case 'v': return &vimvars_var;
2634 case 'b': return &curbuf->b_bufvar;
2635 case 'w': return &curwin->w_winvar;
2636 case 't': return &curtab->tp_winvar;
2637 case 'l': return get_funccal_local_var();
2638 case 'a': return get_funccal_args_var();
2639 }
2640 return NULL;
2641 }
2642
2643 hi = hash_find(ht, varname);
2644 if (HASHITEM_EMPTY(hi))
2645 {
2646 // For global variables we may try auto-loading the script. If it
2647 // worked find the variable again. Don't auto-load a script if it was
2648 // loaded already, otherwise it would be loaded every time when
2649 // checking if a function name is a Funcref variable.
2650 if (ht == &globvarht && !no_autoload)
2651 {
2652 // Note: script_autoload() may make "hi" invalid. It must either
2653 // be obtained again or not used.
2654 if (!script_autoload(varname, FALSE) || aborting())
2655 return NULL;
2656 hi = hash_find(ht, varname);
2657 }
2658 if (HASHITEM_EMPTY(hi))
2659 return NULL;
2660 }
2661 return HI2DI(hi);
2662}
2663
2664/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 * Get the script-local hashtab. NULL if not in a script context.
2666 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002667 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668get_script_local_ht(void)
2669{
2670 scid_T sid = current_sctx.sc_sid;
2671
Bram Moolenaare3d46852020-08-29 13:39:17 +02002672 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 return &SCRIPT_VARS(sid);
2674 return NULL;
2675}
2676
2677/*
2678 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002679 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002681 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2683{
2684 hashtab_T *ht = get_script_local_ht();
2685 char_u buffer[30];
2686 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002687 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 hashitem_T *hi;
2689
2690 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002691 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 if (len < sizeof(buffer) - 1)
2693 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002694 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 vim_strncpy(buffer, name, len);
2696 p = buffer;
2697 }
2698 else
2699 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002700 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002702 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 }
2704
2705 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002706 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707
2708 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002709 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2710 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711
2712 if (p != buffer)
2713 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002714 // Don't return "buffer", gcc complains.
2715 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716}
2717
2718/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002719 * Find the hashtab used for a variable name.
2720 * Return NULL if the name is not valid.
2721 * Set "varname" to the start of name without ':'.
2722 */
2723 hashtab_T *
2724find_var_ht(char_u *name, char_u **varname)
2725{
2726 hashitem_T *hi;
2727 hashtab_T *ht;
2728
2729 if (name[0] == NUL)
2730 return NULL;
2731 if (name[1] != ':')
2732 {
2733 // The name must not start with a colon or #.
2734 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2735 return NULL;
2736 *varname = name;
2737
2738 // "version" is "v:version" in all scopes if scriptversion < 3.
2739 // Same for a few other variables marked with VV_COMPAT.
2740 if (current_sctx.sc_version < 3)
2741 {
2742 hi = hash_find(&compat_hashtab, name);
2743 if (!HASHITEM_EMPTY(hi))
2744 return &compat_hashtab;
2745 }
2746
2747 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 if (ht != NULL)
2749 return ht; // local variable
2750
2751 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002752 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 {
2754 ht = get_script_local_ht();
2755 if (ht != NULL)
2756 return ht;
2757 }
2758
2759 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002760 }
2761 *varname = name + 2;
2762 if (*name == 'g') // global variable
2763 return &globvarht;
2764 // There must be no ':' or '#' in the rest of the name, unless g: is used
2765 if (vim_strchr(name + 2, ':') != NULL
2766 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2767 return NULL;
2768 if (*name == 'b') // buffer variable
2769 return &curbuf->b_vars->dv_hashtab;
2770 if (*name == 'w') // window variable
2771 return &curwin->w_vars->dv_hashtab;
2772 if (*name == 't') // tab page variable
2773 return &curtab->tp_vars->dv_hashtab;
2774 if (*name == 'v') // v: variable
2775 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002776 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002777 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002779 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002780 if (*name == 'a') // a: function argument
2781 return get_funccal_args_ht();
2782 if (*name == 'l') // l: local function variable
2783 return get_funccal_local_ht();
2784 }
2785 if (*name == 's') // script variable
2786 {
2787 ht = get_script_local_ht();
2788 if (ht != NULL)
2789 return ht;
2790 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002791 return NULL;
2792}
2793
2794/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002795 * Get the string value of a (global/local) variable.
2796 * Note: see tv_get_string() for how long the pointer remains valid.
2797 * Returns NULL when it doesn't exist.
2798 */
2799 char_u *
2800get_var_value(char_u *name)
2801{
2802 dictitem_T *v;
2803
2804 v = find_var(name, NULL, FALSE);
2805 if (v == NULL)
2806 return NULL;
2807 return tv_get_string(&v->di_tv);
2808}
2809
2810/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002811 * Allocate a new hashtab for a sourced script. It will be used while
2812 * sourcing this script and when executing functions defined in the script.
2813 */
2814 void
2815new_script_vars(scid_T id)
2816{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002817 scriptvar_T *sv;
2818
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002819 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2820 if (sv == NULL)
2821 return;
2822 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002823 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002824}
2825
2826/*
2827 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2828 * point to it.
2829 */
2830 void
2831init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2832{
2833 hash_init(&dict->dv_hashtab);
2834 dict->dv_lock = 0;
2835 dict->dv_scope = scope;
2836 dict->dv_refcount = DO_NOT_FREE_CNT;
2837 dict->dv_copyID = 0;
2838 dict_var->di_tv.vval.v_dict = dict;
2839 dict_var->di_tv.v_type = VAR_DICT;
2840 dict_var->di_tv.v_lock = VAR_FIXED;
2841 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2842 dict_var->di_key[0] = NUL;
2843}
2844
2845/*
2846 * Unreference a dictionary initialized by init_var_dict().
2847 */
2848 void
2849unref_var_dict(dict_T *dict)
2850{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002851 // Now the dict needs to be freed if no one else is using it, go back to
2852 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002853 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2854 dict_unref(dict);
2855}
2856
2857/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002858 * Clean up a list of internal variables.
2859 * Frees all allocated variables and the value they contain.
2860 * Clears hashtab "ht", does not free it.
2861 */
2862 void
2863vars_clear(hashtab_T *ht)
2864{
2865 vars_clear_ext(ht, TRUE);
2866}
2867
2868/*
2869 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2870 */
2871 void
2872vars_clear_ext(hashtab_T *ht, int free_val)
2873{
2874 int todo;
2875 hashitem_T *hi;
2876 dictitem_T *v;
2877
2878 hash_lock(ht);
2879 todo = (int)ht->ht_used;
2880 for (hi = ht->ht_array; todo > 0; ++hi)
2881 {
2882 if (!HASHITEM_EMPTY(hi))
2883 {
2884 --todo;
2885
2886 // Free the variable. Don't remove it from the hashtab,
2887 // ht_array might change then. hash_clear() takes care of it
2888 // later.
2889 v = HI2DI(hi);
2890 if (free_val)
2891 clear_tv(&v->di_tv);
2892 if (v->di_flags & DI_FLAGS_ALLOC)
2893 vim_free(v);
2894 }
2895 }
2896 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002897 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002898}
2899
2900/*
2901 * Delete a variable from hashtab "ht" at item "hi".
2902 * Clear the variable value and free the dictitem.
2903 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002904 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002905delete_var(hashtab_T *ht, hashitem_T *hi)
2906{
2907 dictitem_T *di = HI2DI(hi);
2908
2909 hash_remove(ht, hi);
2910 clear_tv(&di->di_tv);
2911 vim_free(di);
2912}
2913
2914/*
2915 * List the value of one internal variable.
2916 */
2917 static void
2918list_one_var(dictitem_T *v, char *prefix, int *first)
2919{
2920 char_u *tofree;
2921 char_u *s;
2922 char_u numbuf[NUMBUFLEN];
2923
2924 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2925 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2926 s == NULL ? (char_u *)"" : s, first);
2927 vim_free(tofree);
2928}
2929
2930 static void
2931list_one_var_a(
2932 char *prefix,
2933 char_u *name,
2934 int type,
2935 char_u *string,
2936 int *first) // when TRUE clear rest of screen and set to FALSE
2937{
2938 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2939 msg_start();
2940 msg_puts(prefix);
2941 if (name != NULL) // "a:" vars don't have a name stored
2942 msg_puts((char *)name);
2943 msg_putchar(' ');
2944 msg_advance(22);
2945 if (type == VAR_NUMBER)
2946 msg_putchar('#');
2947 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2948 msg_putchar('*');
2949 else if (type == VAR_LIST)
2950 {
2951 msg_putchar('[');
2952 if (*string == '[')
2953 ++string;
2954 }
2955 else if (type == VAR_DICT)
2956 {
2957 msg_putchar('{');
2958 if (*string == '{')
2959 ++string;
2960 }
2961 else
2962 msg_putchar(' ');
2963
2964 msg_outtrans(string);
2965
2966 if (type == VAR_FUNC || type == VAR_PARTIAL)
2967 msg_puts("()");
2968 if (*first)
2969 {
2970 msg_clr_eos();
2971 *first = FALSE;
2972 }
2973}
2974
2975/*
2976 * Set variable "name" to value in "tv".
2977 * If the variable already exists, the value is updated.
2978 * Otherwise the variable is created.
2979 */
2980 void
2981set_var(
2982 char_u *name,
2983 typval_T *tv,
2984 int copy) // make copy of value in "tv"
2985{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002986 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002987}
2988
2989/*
2990 * Set variable "name" to value in "tv".
2991 * If the variable already exists and "is_const" is FALSE the value is updated.
2992 * Otherwise the variable is created.
2993 */
2994 void
2995set_var_const(
2996 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002998 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002999 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003000 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003001{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003002 typval_T *tv = tv_arg;
3003 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003005 char_u *varname;
3006 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003008 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003009
3010 ht = find_var_ht(name, &varname);
3011 if (ht == NULL || *varname == NUL)
3012 {
3013 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003014 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003015 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003016 is_script_local = ht == get_script_local_ht();
3017
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003018 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003019 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003020 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003021 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003022 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003023 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003024 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003025 }
3026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003028
3029 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 if (di == NULL)
3031 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003032
3033 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003034 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003035 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003036
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003037 if (need_convert_to_bool(type, tv))
3038 {
3039 // Destination is a bool and the value is not, but it can be converted.
3040 CLEAR_FIELD(bool_tv);
3041 bool_tv.v_type = VAR_BOOL;
3042 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3043 tv = &bool_tv;
3044 }
3045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003047 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003049 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003050 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 {
3052 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003053 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003054 }
3055
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003056 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003058 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003059 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003060 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003061 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003062 }
3063
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003064 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003065 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003066 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003068
Bram Moolenaara187c432020-09-16 21:08:28 +02003069 // Check in this order for backwards compatibility:
3070 // - Whether the variable is read-only
3071 // - Whether the variable value is locked
3072 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003073 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003074 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3075 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003076 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 else
3079 // can only redefine once
3080 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003081
3082 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003085 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003086 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003087 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003089 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003091 if (copy || tv->v_type != VAR_STRING)
3092 {
3093 char_u *val = tv_get_string(tv);
3094
3095 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003096 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 if (di->di_tv.vval.v_string == NULL)
3098 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 }
3100 else
3101 {
3102 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003104 tv->vval.v_string = NULL;
3105 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003106 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003107 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003109 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003111 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003112 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003113#ifdef FEAT_SEARCH_EXTRA
3114 else if (STRCMP(varname, "hlsearch") == 0)
3115 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003117 redraw_all_later(SOME_VALID);
3118 }
3119#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003120 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003123 {
3124 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003125 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003126 }
3127 }
3128
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003130 }
3131 else // add a new variable
3132 {
3133 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003134 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003135 {
3136 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003137 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003138 }
3139
3140 // Make sure the variable name is valid.
3141 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003142 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3145 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003146 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 STRCPY(di->di_key, varname);
3148 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003149 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003150 vim_free(di);
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 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003154 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155 di->di_flags |= DI_FLAGS_LOCK;
3156
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003157 // A Vim9 script-local variable is also added to sn_all_vars and
3158 // sn_var_vals.
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003159 if (is_script_local && vim9script)
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003160 add_vim9_script_var(di, tv, type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003161 }
3162
3163 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003165 else
3166 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 di->di_tv = *tv;
3168 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003169 init_tv(tv);
3170 }
3171
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003172 // ":const var = val" locks the value
3173 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003174 // Like :lockvar! name: lock the value and what it contains, but only
3175 // if the reference count is up to one. That locks only literal
3176 // values.
3177 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003178 return;
3179failed:
3180 if (!copy)
3181 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003182}
3183
3184/*
3185 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3186 * Also give an error message.
3187 */
3188 int
3189var_check_ro(int flags, char_u *name, int use_gettext)
3190{
3191 if (flags & DI_FLAGS_RO)
3192 {
3193 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3194 return TRUE;
3195 }
3196 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3197 {
3198 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3199 return TRUE;
3200 }
3201 return FALSE;
3202}
3203
3204/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003205 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3206 * Also give an error message.
3207 */
3208 int
3209var_check_lock(int flags, char_u *name, int use_gettext)
3210{
3211 if (flags & DI_FLAGS_LOCK)
3212 {
3213 semsg(_(e_variable_is_locked_str),
3214 use_gettext ? (char_u *)_(name) : name);
3215 return TRUE;
3216 }
3217 return FALSE;
3218}
3219
3220/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003221 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3222 * Also give an error message.
3223 */
3224 int
3225var_check_fixed(int flags, char_u *name, int use_gettext)
3226{
3227 if (flags & DI_FLAGS_FIX)
3228 {
3229 semsg(_("E795: Cannot delete variable %s"),
3230 use_gettext ? (char_u *)_(name) : name);
3231 return TRUE;
3232 }
3233 return FALSE;
3234}
3235
3236/*
3237 * Check if a funcref is assigned to a valid variable name.
3238 * Return TRUE and give an error if not.
3239 */
3240 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003241var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003242 char_u *name, // points to start of variable name
3243 int new_var) // TRUE when creating the variable
3244{
3245 // Allow for w: b: s: and t:.
3246 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3247 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3248 ? name[2] : name[0]))
3249 {
3250 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3251 name);
3252 return TRUE;
3253 }
3254 // Don't allow hiding a function. When "v" is not NULL we might be
3255 // assigning another function to the same var, the type is checked
3256 // below.
3257 if (new_var && function_exists(name, FALSE))
3258 {
3259 semsg(_("E705: Variable name conflicts with existing function: %s"),
3260 name);
3261 return TRUE;
3262 }
3263 return FALSE;
3264}
3265
3266/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003267 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3268 * value. Also give an error message, using "name" or _("name") when
3269 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003270 */
3271 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003272value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003273{
3274 if (lock & VAR_LOCKED)
3275 {
3276 semsg(_("E741: Value is locked: %s"),
3277 name == NULL ? (char_u *)_("Unknown")
3278 : use_gettext ? (char_u *)_(name)
3279 : name);
3280 return TRUE;
3281 }
3282 if (lock & VAR_FIXED)
3283 {
3284 semsg(_("E742: Cannot change value of %s"),
3285 name == NULL ? (char_u *)_("Unknown")
3286 : use_gettext ? (char_u *)_(name)
3287 : name);
3288 return TRUE;
3289 }
3290 return FALSE;
3291}
3292
3293/*
3294 * Check if a variable name is valid.
3295 * Return FALSE and give an error if not.
3296 */
3297 int
3298valid_varname(char_u *varname)
3299{
3300 char_u *p;
3301
3302 for (p = varname; *p != NUL; ++p)
3303 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3304 && *p != AUTOLOAD_CHAR)
3305 {
3306 semsg(_(e_illvar), varname);
3307 return FALSE;
3308 }
3309 return TRUE;
3310}
3311
3312/*
3313 * getwinvar() and gettabwinvar()
3314 */
3315 static void
3316getwinvar(
3317 typval_T *argvars,
3318 typval_T *rettv,
3319 int off) // 1 for gettabwinvar()
3320{
3321 win_T *win;
3322 char_u *varname;
3323 dictitem_T *v;
3324 tabpage_T *tp = NULL;
3325 int done = FALSE;
3326 win_T *oldcurwin;
3327 tabpage_T *oldtabpage;
3328 int need_switch_win;
3329
3330 if (off == 1)
3331 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3332 else
3333 tp = curtab;
3334 win = find_win_by_nr(&argvars[off], tp);
3335 varname = tv_get_string_chk(&argvars[off + 1]);
3336 ++emsg_off;
3337
3338 rettv->v_type = VAR_STRING;
3339 rettv->vval.v_string = NULL;
3340
3341 if (win != NULL && varname != NULL)
3342 {
3343 // Set curwin to be our win, temporarily. Also set the tabpage,
3344 // otherwise the window is not valid. Only do this when needed,
3345 // autocommands get blocked.
3346 need_switch_win = !(tp == curtab && win == curwin);
3347 if (!need_switch_win
3348 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3349 {
3350 if (*varname == '&')
3351 {
3352 if (varname[1] == NUL)
3353 {
3354 // get all window-local options in a dict
3355 dict_T *opts = get_winbuf_options(FALSE);
3356
3357 if (opts != NULL)
3358 {
3359 rettv_dict_set(rettv, opts);
3360 done = TRUE;
3361 }
3362 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003363 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003364 // window-local-option
3365 done = TRUE;
3366 }
3367 else
3368 {
3369 // Look up the variable.
3370 // Let getwinvar({nr}, "") return the "w:" dictionary.
3371 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3372 varname, FALSE);
3373 if (v != NULL)
3374 {
3375 copy_tv(&v->di_tv, rettv);
3376 done = TRUE;
3377 }
3378 }
3379 }
3380
3381 if (need_switch_win)
3382 // restore previous notion of curwin
3383 restore_win(oldcurwin, oldtabpage, TRUE);
3384 }
3385
3386 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3387 // use the default return value
3388 copy_tv(&argvars[off + 2], rettv);
3389
3390 --emsg_off;
3391}
3392
3393/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003394 * Set option "varname" to the value of "varp" for the current buffer/window.
3395 */
3396 static void
3397set_option_from_tv(char_u *varname, typval_T *varp)
3398{
3399 long numval = 0;
3400 char_u *strval;
3401 char_u nbuf[NUMBUFLEN];
3402 int error = FALSE;
3403
3404 if (!in_vim9script() || varp->v_type != VAR_STRING)
3405 numval = (long)tv_get_number_chk(varp, &error);
3406 strval = tv_get_string_buf_chk(varp, nbuf);
3407 if (!error && strval != NULL)
3408 set_option_value(varname, numval, strval, OPT_LOCAL);
3409}
3410
3411/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003412 * "setwinvar()" and "settabwinvar()" functions
3413 */
3414 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003415setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003416{
3417 win_T *win;
3418 win_T *save_curwin;
3419 tabpage_T *save_curtab;
3420 int need_switch_win;
3421 char_u *varname, *winvarname;
3422 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003423 tabpage_T *tp = NULL;
3424
3425 if (check_secure())
3426 return;
3427
3428 if (off == 1)
3429 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3430 else
3431 tp = curtab;
3432 win = find_win_by_nr(&argvars[off], tp);
3433 varname = tv_get_string_chk(&argvars[off + 1]);
3434 varp = &argvars[off + 2];
3435
3436 if (win != NULL && varname != NULL && varp != NULL)
3437 {
3438 need_switch_win = !(tp == curtab && win == curwin);
3439 if (!need_switch_win
3440 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3441 {
3442 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003443 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003444 else
3445 {
3446 winvarname = alloc(STRLEN(varname) + 3);
3447 if (winvarname != NULL)
3448 {
3449 STRCPY(winvarname, "w:");
3450 STRCPY(winvarname + 2, varname);
3451 set_var(winvarname, varp, TRUE);
3452 vim_free(winvarname);
3453 }
3454 }
3455 }
3456 if (need_switch_win)
3457 restore_win(save_curwin, save_curtab, TRUE);
3458 }
3459}
3460
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003461/*
3462 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3463 * v:option_type, and v:option_command.
3464 */
3465 void
3466reset_v_option_vars(void)
3467{
3468 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3469 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3470 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3471 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3472 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3473 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3474}
3475
3476/*
3477 * Add an assert error to v:errors.
3478 */
3479 void
3480assert_error(garray_T *gap)
3481{
3482 struct vimvar *vp = &vimvars[VV_ERRORS];
3483
3484 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003485 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003486 set_vim_var_list(VV_ERRORS, list_alloc());
3487 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3488}
3489
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003490 int
3491var_exists(char_u *var)
3492{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003493 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003494 char_u *name;
3495 char_u *tofree;
3496 typval_T tv;
3497 int len = 0;
3498 int n = FALSE;
3499
3500 // get_name_len() takes care of expanding curly braces
3501 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003502 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003503 if (len > 0)
3504 {
3505 if (tofree != NULL)
3506 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003507 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003508 if (n)
3509 {
3510 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003511 arg = skipwhite(arg);
3512 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003513 if (n)
3514 clear_tv(&tv);
3515 }
3516 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003517 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003518 n = FALSE;
3519
3520 vim_free(tofree);
3521 return n;
3522}
3523
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003524static lval_T *redir_lval = NULL;
3525#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3526static garray_T redir_ga; // only valid when redir_lval is not NULL
3527static char_u *redir_endp = NULL;
3528static char_u *redir_varname = NULL;
3529
3530/*
3531 * Start recording command output to a variable
3532 * When "append" is TRUE append to an existing variable.
3533 * Returns OK if successfully completed the setup. FAIL otherwise.
3534 */
3535 int
3536var_redir_start(char_u *name, int append)
3537{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003538 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003539 typval_T tv;
3540
3541 // Catch a bad name early.
3542 if (!eval_isnamec1(*name))
3543 {
3544 emsg(_(e_invarg));
3545 return FAIL;
3546 }
3547
3548 // Make a copy of the name, it is used in redir_lval until redir ends.
3549 redir_varname = vim_strsave(name);
3550 if (redir_varname == NULL)
3551 return FAIL;
3552
3553 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3554 if (redir_lval == NULL)
3555 {
3556 var_redir_stop();
3557 return FAIL;
3558 }
3559
3560 // The output is stored in growarray "redir_ga" until redirection ends.
3561 ga_init2(&redir_ga, (int)sizeof(char), 500);
3562
3563 // Parse the variable name (can be a dict or list entry).
3564 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3565 FNE_CHECK_START);
3566 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3567 {
3568 clear_lval(redir_lval);
3569 if (redir_endp != NULL && *redir_endp != NUL)
3570 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003571 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003572 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003573 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003574 redir_endp = NULL; // don't store a value, only cleanup
3575 var_redir_stop();
3576 return FAIL;
3577 }
3578
3579 // check if we can write to the variable: set it to or append an empty
3580 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003581 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003582 tv.v_type = VAR_STRING;
3583 tv.vval.v_string = (char_u *)"";
3584 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003586 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003587 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003588 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003589 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003590 {
3591 redir_endp = NULL; // don't store a value, only cleanup
3592 var_redir_stop();
3593 return FAIL;
3594 }
3595
3596 return OK;
3597}
3598
3599/*
3600 * Append "value[value_len]" to the variable set by var_redir_start().
3601 * The actual appending is postponed until redirection ends, because the value
3602 * appended may in fact be the string we write to, changing it may cause freed
3603 * memory to be used:
3604 * :redir => foo
3605 * :let foo
3606 * :redir END
3607 */
3608 void
3609var_redir_str(char_u *value, int value_len)
3610{
3611 int len;
3612
3613 if (redir_lval == NULL)
3614 return;
3615
3616 if (value_len == -1)
3617 len = (int)STRLEN(value); // Append the entire string
3618 else
3619 len = value_len; // Append only "value_len" characters
3620
3621 if (ga_grow(&redir_ga, len) == OK)
3622 {
3623 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3624 redir_ga.ga_len += len;
3625 }
3626 else
3627 var_redir_stop();
3628}
3629
3630/*
3631 * Stop redirecting command output to a variable.
3632 * Frees the allocated memory.
3633 */
3634 void
3635var_redir_stop(void)
3636{
3637 typval_T tv;
3638
3639 if (EVALCMD_BUSY)
3640 {
3641 redir_lval = NULL;
3642 return;
3643 }
3644
3645 if (redir_lval != NULL)
3646 {
3647 // If there was no error: assign the text to the variable.
3648 if (redir_endp != NULL)
3649 {
3650 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3651 tv.v_type = VAR_STRING;
3652 tv.vval.v_string = redir_ga.ga_data;
3653 // Call get_lval() again, if it's inside a Dict or List it may
3654 // have changed.
3655 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3656 FALSE, FALSE, 0, FNE_CHECK_START);
3657 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003659 (char_u *)".");
3660 clear_lval(redir_lval);
3661 }
3662
3663 // free the collected output
3664 VIM_CLEAR(redir_ga.ga_data);
3665
3666 VIM_CLEAR(redir_lval);
3667 }
3668 VIM_CLEAR(redir_varname);
3669}
3670
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003671/*
3672 * "gettabvar()" function
3673 */
3674 void
3675f_gettabvar(typval_T *argvars, typval_T *rettv)
3676{
3677 win_T *oldcurwin;
3678 tabpage_T *tp, *oldtabpage;
3679 dictitem_T *v;
3680 char_u *varname;
3681 int done = FALSE;
3682
3683 rettv->v_type = VAR_STRING;
3684 rettv->vval.v_string = NULL;
3685
3686 varname = tv_get_string_chk(&argvars[1]);
3687 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3688 if (tp != NULL && varname != NULL)
3689 {
3690 // Set tp to be our tabpage, temporarily. Also set the window to the
3691 // first window in the tabpage, otherwise the window is not valid.
3692 if (switch_win(&oldcurwin, &oldtabpage,
3693 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3694 : tp->tp_firstwin, tp, TRUE) == OK)
3695 {
3696 // look up the variable
3697 // Let gettabvar({nr}, "") return the "t:" dictionary.
3698 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3699 if (v != NULL)
3700 {
3701 copy_tv(&v->di_tv, rettv);
3702 done = TRUE;
3703 }
3704 }
3705
3706 // restore previous notion of curwin
3707 restore_win(oldcurwin, oldtabpage, TRUE);
3708 }
3709
3710 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3711 copy_tv(&argvars[2], rettv);
3712}
3713
3714/*
3715 * "gettabwinvar()" function
3716 */
3717 void
3718f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3719{
3720 getwinvar(argvars, rettv, 1);
3721}
3722
3723/*
3724 * "getwinvar()" function
3725 */
3726 void
3727f_getwinvar(typval_T *argvars, typval_T *rettv)
3728{
3729 getwinvar(argvars, rettv, 0);
3730}
3731
3732/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003733 * "getbufvar()" function
3734 */
3735 void
3736f_getbufvar(typval_T *argvars, typval_T *rettv)
3737{
3738 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003739 char_u *varname;
3740 dictitem_T *v;
3741 int done = FALSE;
3742
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003743 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003744 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003745
3746 rettv->v_type = VAR_STRING;
3747 rettv->vval.v_string = NULL;
3748
3749 if (buf != NULL && varname != NULL)
3750 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003751 if (*varname == '&')
3752 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003753 buf_T *save_curbuf = curbuf;
3754
3755 // set curbuf to be our buf, temporarily
3756 curbuf = buf;
3757
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003758 if (varname[1] == NUL)
3759 {
3760 // get all buffer-local options in a dict
3761 dict_T *opts = get_winbuf_options(TRUE);
3762
3763 if (opts != NULL)
3764 {
3765 rettv_dict_set(rettv, opts);
3766 done = TRUE;
3767 }
3768 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003769 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003770 // buffer-local-option
3771 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003772
3773 // restore previous notion of curbuf
3774 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003775 }
3776 else
3777 {
3778 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003779 if (*varname == NUL)
3780 // Let getbufvar({nr}, "") return the "b:" dictionary.
3781 v = &buf->b_bufvar;
3782 else
3783 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3784 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003785 if (v != NULL)
3786 {
3787 copy_tv(&v->di_tv, rettv);
3788 done = TRUE;
3789 }
3790 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003791 }
3792
3793 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3794 // use the default value
3795 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003796}
3797
3798/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003799 * "settabvar()" function
3800 */
3801 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003802f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003803{
3804 tabpage_T *save_curtab;
3805 tabpage_T *tp;
3806 char_u *varname, *tabvarname;
3807 typval_T *varp;
3808
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003809 if (check_secure())
3810 return;
3811
3812 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3813 varname = tv_get_string_chk(&argvars[1]);
3814 varp = &argvars[2];
3815
3816 if (varname != NULL && varp != NULL && tp != NULL)
3817 {
3818 save_curtab = curtab;
3819 goto_tabpage_tp(tp, FALSE, FALSE);
3820
3821 tabvarname = alloc(STRLEN(varname) + 3);
3822 if (tabvarname != NULL)
3823 {
3824 STRCPY(tabvarname, "t:");
3825 STRCPY(tabvarname + 2, varname);
3826 set_var(tabvarname, varp, TRUE);
3827 vim_free(tabvarname);
3828 }
3829
3830 // Restore current tabpage
3831 if (valid_tabpage(save_curtab))
3832 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3833 }
3834}
3835
3836/*
3837 * "settabwinvar()" function
3838 */
3839 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003840f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003841{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003842 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003843}
3844
3845/*
3846 * "setwinvar()" function
3847 */
3848 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003849f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003850{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003851 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003852}
3853
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003854/*
3855 * "setbufvar()" function
3856 */
3857 void
3858f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3859{
3860 buf_T *buf;
3861 char_u *varname, *bufvarname;
3862 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003863
3864 if (check_secure())
3865 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003866 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003867 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003868 varp = &argvars[2];
3869
3870 if (buf != NULL && varname != NULL && varp != NULL)
3871 {
3872 if (*varname == '&')
3873 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003874 aco_save_T aco;
3875
3876 // set curbuf to be our buf, temporarily
3877 aucmd_prepbuf(&aco, buf);
3878
Bram Moolenaar191929b2020-08-19 21:20:49 +02003879 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003880
3881 // reset notion of buffer
3882 aucmd_restbuf(&aco);
3883 }
3884 else
3885 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003886 bufvarname = alloc(STRLEN(varname) + 3);
3887 if (bufvarname != NULL)
3888 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003889 buf_T *save_curbuf = curbuf;
3890
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003891 curbuf = buf;
3892 STRCPY(bufvarname, "b:");
3893 STRCPY(bufvarname + 2, varname);
3894 set_var(bufvarname, varp, TRUE);
3895 vim_free(bufvarname);
3896 curbuf = save_curbuf;
3897 }
3898 }
3899 }
3900}
3901
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003902/*
3903 * Get a callback from "arg". It can be a Funcref or a function name.
3904 * When "arg" is zero return an empty string.
3905 * "cb_name" is not allocated.
3906 * "cb_name" is set to NULL for an invalid argument.
3907 */
3908 callback_T
3909get_callback(typval_T *arg)
3910{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003911 callback_T res;
3912 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003913
3914 res.cb_free_name = FALSE;
3915 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3916 {
3917 res.cb_partial = arg->vval.v_partial;
3918 ++res.cb_partial->pt_refcount;
3919 res.cb_name = partial_name(res.cb_partial);
3920 }
3921 else
3922 {
3923 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003924 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3925 && isdigit(*arg->vval.v_string))
3926 r = FAIL;
3927 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003928 {
3929 // Note that we don't make a copy of the string.
3930 res.cb_name = arg->vval.v_string;
3931 func_ref(res.cb_name);
3932 }
3933 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003934 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003935 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003936 r = FAIL;
3937
3938 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003939 {
3940 emsg(_("E921: Invalid callback argument"));
3941 res.cb_name = NULL;
3942 }
3943 }
3944 return res;
3945}
3946
3947/*
3948 * Copy a callback into a typval_T.
3949 */
3950 void
3951put_callback(callback_T *cb, typval_T *tv)
3952{
3953 if (cb->cb_partial != NULL)
3954 {
3955 tv->v_type = VAR_PARTIAL;
3956 tv->vval.v_partial = cb->cb_partial;
3957 ++tv->vval.v_partial->pt_refcount;
3958 }
3959 else
3960 {
3961 tv->v_type = VAR_FUNC;
3962 tv->vval.v_string = vim_strsave(cb->cb_name);
3963 func_ref(cb->cb_name);
3964 }
3965}
3966
3967/*
3968 * Make a copy of "src" into "dest", allocating the function name if needed,
3969 * without incrementing the refcount.
3970 */
3971 void
3972set_callback(callback_T *dest, callback_T *src)
3973{
3974 if (src->cb_partial == NULL)
3975 {
3976 // just a function name, make a copy
3977 dest->cb_name = vim_strsave(src->cb_name);
3978 dest->cb_free_name = TRUE;
3979 }
3980 else
3981 {
3982 // cb_name is a pointer into cb_partial
3983 dest->cb_name = src->cb_name;
3984 dest->cb_free_name = FALSE;
3985 }
3986 dest->cb_partial = src->cb_partial;
3987}
3988
3989/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003990 * Copy callback from "src" to "dest", incrementing the refcounts.
3991 */
3992 void
3993copy_callback(callback_T *dest, callback_T *src)
3994{
3995 dest->cb_partial = src->cb_partial;
3996 if (dest->cb_partial != NULL)
3997 {
3998 dest->cb_name = src->cb_name;
3999 dest->cb_free_name = FALSE;
4000 ++dest->cb_partial->pt_refcount;
4001 }
4002 else
4003 {
4004 dest->cb_name = vim_strsave(src->cb_name);
4005 dest->cb_free_name = TRUE;
4006 func_ref(src->cb_name);
4007 }
4008}
4009
4010/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004011 * Unref/free "callback" returned by get_callback() or set_callback().
4012 */
4013 void
4014free_callback(callback_T *callback)
4015{
4016 if (callback->cb_partial != NULL)
4017 {
4018 partial_unref(callback->cb_partial);
4019 callback->cb_partial = NULL;
4020 }
4021 else if (callback->cb_name != NULL)
4022 func_unref(callback->cb_name);
4023 if (callback->cb_free_name)
4024 {
4025 vim_free(callback->cb_name);
4026 callback->cb_free_name = FALSE;
4027 }
4028 callback->cb_name = NULL;
4029}
4030
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004031#endif // FEAT_EVAL