blob: e747230033330ee4a785c0efa9b4f2e0b245532b [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 Moolenaare5cdf152019-08-29 22:09:46 +0200148};
149
150// shorthand
151#define vv_type vv_di.di_tv.v_type
152#define vv_nr vv_di.di_tv.vval.v_number
153#define vv_float vv_di.di_tv.vval.v_float
154#define vv_str vv_di.di_tv.vval.v_string
155#define vv_list vv_di.di_tv.vval.v_list
156#define vv_dict vv_di.di_tv.vval.v_dict
157#define vv_blob vv_di.di_tv.vval.v_blob
158#define vv_tv vv_di.di_tv
159
160static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200161static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vimvarht vimvardict.dv_hashtab
163
164// for VIM_VERSION_ defines
165#include "version.h"
166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167static char_u *skip_var_one(char_u *arg, int include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200168static void list_glob_vars(int *first);
169static void list_buf_vars(int *first);
170static void list_win_vars(int *first);
171static void list_tab_vars(int *first);
172static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200174static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
175static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200176static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void list_one_var(dictitem_T *v, char *prefix, int *first);
179static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
180
181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200182 * Initialize global and vim special variables
183 */
184 void
185evalvars_init(void)
186{
187 int i;
188 struct vimvar *p;
189
190 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
191 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
192 vimvardict.dv_lock = VAR_FIXED;
193 hash_init(&compat_hashtab);
194
195 for (i = 0; i < VV_LEN; ++i)
196 {
197 p = &vimvars[i];
198 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
199 {
200 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
201 getout(1);
202 }
203 STRCPY(p->vv_di.di_key, p->vv_name);
204 if (p->vv_flags & VV_RO)
205 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
206 else if (p->vv_flags & VV_RO_SBX)
207 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
208 else
209 p->vv_di.di_flags = DI_FLAGS_FIX;
210
211 // add to v: scope dict, unless the value is not always available
212 if (p->vv_type != VAR_UNKNOWN)
213 hash_add(&vimvarht, p->vv_di.di_key);
214 if (p->vv_flags & VV_COMPAT)
215 // add to compat scope dict
216 hash_add(&compat_hashtab, p->vv_di.di_key);
217 }
218 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
219 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
220
221 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
222 set_vim_var_nr(VV_HLSEARCH, 1L);
223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
247 set_reg_var(0); // default for v:register is not 0 but '"'
248}
249
250#if defined(EXITFREE) || defined(PROTO)
251/*
252 * Free all vim variables information on exit
253 */
254 void
255evalvars_clear(void)
256{
257 int i;
258 struct vimvar *p;
259
260 for (i = 0; i < VV_LEN; ++i)
261 {
262 p = &vimvars[i];
263 if (p->vv_di.di_tv.v_type == VAR_STRING)
264 VIM_CLEAR(p->vv_str);
265 else if (p->vv_di.di_tv.v_type == VAR_LIST)
266 {
267 list_unref(p->vv_list);
268 p->vv_list = NULL;
269 }
270 }
271 hash_clear(&vimvarht);
272 hash_init(&vimvarht); // garbage_collect() will access it
273 hash_clear(&compat_hashtab);
274
275 // global variables
276 vars_clear(&globvarht);
277
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100278 // Script-local variables. Clear all the variables here.
279 // The scriptvar_T is cleared later in free_scriptnames(), because a
280 // variable in one script might hold a reference to the whole scope of
281 // another script.
282 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200283 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284}
285#endif
286
287 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200288garbage_collect_globvars(int copyID)
289{
290 return set_ref_in_ht(&globvarht, copyID, NULL);
291}
292
293 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200294garbage_collect_vimvars(int copyID)
295{
296 return set_ref_in_ht(&vimvarht, copyID, NULL);
297}
298
299 int
300garbage_collect_scriptvars(int copyID)
301{
302 int i;
303 int abort = FALSE;
304
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100305 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200306 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
307
308 return abort;
309}
310
311/*
312 * Set an internal variable to a string value. Creates the variable if it does
313 * not already exist.
314 */
315 void
316set_internal_string_var(char_u *name, char_u *value)
317{
318 char_u *val;
319 typval_T *tvp;
320
321 val = vim_strsave(value);
322 if (val != NULL)
323 {
324 tvp = alloc_string_tv(val);
325 if (tvp != NULL)
326 {
327 set_var(name, tvp, FALSE);
328 free_tv(tvp);
329 }
330 }
331}
332
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200333 int
334eval_charconvert(
335 char_u *enc_from,
336 char_u *enc_to,
337 char_u *fname_from,
338 char_u *fname_to)
339{
340 int err = FALSE;
341
342 set_vim_var_string(VV_CC_FROM, enc_from, -1);
343 set_vim_var_string(VV_CC_TO, enc_to, -1);
344 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
345 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
346 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
347 err = TRUE;
348 set_vim_var_string(VV_CC_FROM, NULL, -1);
349 set_vim_var_string(VV_CC_TO, NULL, -1);
350 set_vim_var_string(VV_FNAME_IN, NULL, -1);
351 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
352
353 if (err)
354 return FAIL;
355 return OK;
356}
357
358# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
359 int
360eval_printexpr(char_u *fname, char_u *args)
361{
362 int err = FALSE;
363
364 set_vim_var_string(VV_FNAME_IN, fname, -1);
365 set_vim_var_string(VV_CMDARG, args, -1);
366 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
367 err = TRUE;
368 set_vim_var_string(VV_FNAME_IN, NULL, -1);
369 set_vim_var_string(VV_CMDARG, NULL, -1);
370
371 if (err)
372 {
373 mch_remove(fname);
374 return FAIL;
375 }
376 return OK;
377}
378# endif
379
380# if defined(FEAT_DIFF) || defined(PROTO)
381 void
382eval_diff(
383 char_u *origfile,
384 char_u *newfile,
385 char_u *outfile)
386{
387 int err = FALSE;
388
389 set_vim_var_string(VV_FNAME_IN, origfile, -1);
390 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
391 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
392 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
393 set_vim_var_string(VV_FNAME_IN, NULL, -1);
394 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
395 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
396}
397
398 void
399eval_patch(
400 char_u *origfile,
401 char_u *difffile,
402 char_u *outfile)
403{
404 int err;
405
406 set_vim_var_string(VV_FNAME_IN, origfile, -1);
407 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
408 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
409 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
410 set_vim_var_string(VV_FNAME_IN, NULL, -1);
411 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
412 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
413}
414# endif
415
416#if defined(FEAT_SPELL) || defined(PROTO)
417/*
418 * Evaluate an expression to a list with suggestions.
419 * For the "expr:" part of 'spellsuggest'.
420 * Returns NULL when there is an error.
421 */
422 list_T *
423eval_spell_expr(char_u *badword, char_u *expr)
424{
425 typval_T save_val;
426 typval_T rettv;
427 list_T *list = NULL;
428 char_u *p = skipwhite(expr);
429
430 // Set "v:val" to the bad word.
431 prepare_vimvar(VV_VAL, &save_val);
432 set_vim_var_string(VV_VAL, badword, -1);
433 if (p_verbose == 0)
434 ++emsg_off;
435
436 if (eval1(&p, &rettv, TRUE) == OK)
437 {
438 if (rettv.v_type != VAR_LIST)
439 clear_tv(&rettv);
440 else
441 list = rettv.vval.v_list;
442 }
443
444 if (p_verbose == 0)
445 --emsg_off;
446 clear_tv(get_vim_var_tv(VV_VAL));
447 restore_vimvar(VV_VAL, &save_val);
448
449 return list;
450}
451
452/*
453 * "list" is supposed to contain two items: a word and a number. Return the
454 * word in "pp" and the number as the return value.
455 * Return -1 if anything isn't right.
456 * Used to get the good word and score from the eval_spell_expr() result.
457 */
458 int
459get_spellword(list_T *list, char_u **pp)
460{
461 listitem_T *li;
462
463 li = list->lv_first;
464 if (li == NULL)
465 return -1;
466 *pp = tv_get_string(&li->li_tv);
467
468 li = li->li_next;
469 if (li == NULL)
470 return -1;
471 return (int)tv_get_number(&li->li_tv);
472}
473#endif
474
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200475/*
476 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200477 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200478 * When not used yet add the variable to the v: hashtable.
479 */
480 void
481prepare_vimvar(int idx, typval_T *save_tv)
482{
483 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200484 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200485 if (vimvars[idx].vv_type == VAR_UNKNOWN)
486 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
487}
488
489/*
490 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200491 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200492 * When no longer defined, remove the variable from the v: hashtable.
493 */
494 void
495restore_vimvar(int idx, typval_T *save_tv)
496{
497 hashitem_T *hi;
498
499 vimvars[idx].vv_tv = *save_tv;
500 if (vimvars[idx].vv_type == VAR_UNKNOWN)
501 {
502 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
503 if (HASHITEM_EMPTY(hi))
504 internal_error("restore_vimvar()");
505 else
506 hash_remove(&vimvarht, hi);
507 }
508}
509
510/*
511 * List Vim variables.
512 */
513 static void
514list_vim_vars(int *first)
515{
516 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
517}
518
519/*
520 * List script-local variables, if there is a script.
521 */
522 static void
523list_script_vars(int *first)
524{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100525 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200526 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
527 "s:", FALSE, first);
528}
529
530/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200531 * Get a list of lines from a HERE document. The here document is a list of
532 * lines surrounded by a marker.
533 * cmd << {marker}
534 * {line1}
535 * {line2}
536 * ....
537 * {marker}
538 *
539 * The {marker} is a string. If the optional 'trim' word is supplied before the
540 * marker, then the leading indentation before the lines (matching the
541 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200542 *
543 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
544 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
545 * missing, then '.' is accepted as a marker.
546 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200547 * Returns a List with {lines} or NULL.
548 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200550heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200551{
552 char_u *theline;
553 char_u *marker;
554 list_T *l;
555 char_u *p;
556 int marker_indent_len = 0;
557 int text_indent_len = 0;
558 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200559 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200560
561 if (eap->getline == NULL)
562 {
563 emsg(_("E991: cannot use =<< here"));
564 return NULL;
565 }
566
567 // Check for the optional 'trim' word before the marker
568 cmd = skipwhite(cmd);
569 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
570 {
571 cmd = skipwhite(cmd + 4);
572
573 // Trim the indentation from all the lines in the here document.
574 // The amount of indentation trimmed is the same as the indentation of
575 // the first line after the :let command line. To find the end marker
576 // the indent of the :let command line is trimmed.
577 p = *eap->cmdlinep;
578 while (VIM_ISWHITE(*p))
579 {
580 p++;
581 marker_indent_len++;
582 }
583 text_indent_len = -1;
584 }
585
586 // The marker is the next word.
587 if (*cmd != NUL && *cmd != '"')
588 {
589 marker = skipwhite(cmd);
590 p = skiptowhite(marker);
591 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
592 {
593 emsg(_(e_trailing));
594 return NULL;
595 }
596 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200597 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200598 {
599 emsg(_("E221: Marker cannot start with lower case letter"));
600 return NULL;
601 }
602 }
603 else
604 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200605 // When getting lines for an embedded script, if the marker is missing,
606 // accept '.' as the marker.
607 if (script_get)
608 marker = dot;
609 else
610 {
611 emsg(_("E172: Missing marker"));
612 return NULL;
613 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200614 }
615
616 l = list_alloc();
617 if (l == NULL)
618 return NULL;
619
620 for (;;)
621 {
622 int mi = 0;
623 int ti = 0;
624
625 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
626 if (theline == NULL)
627 {
628 semsg(_("E990: Missing end marker '%s'"), marker);
629 break;
630 }
631
632 // with "trim": skip the indent matching the :let line to find the
633 // marker
634 if (marker_indent_len > 0
635 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
636 mi = marker_indent_len;
637 if (STRCMP(marker, theline + mi) == 0)
638 {
639 vim_free(theline);
640 break;
641 }
642
643 if (text_indent_len == -1 && *theline != NUL)
644 {
645 // set the text indent from the first line.
646 p = theline;
647 text_indent_len = 0;
648 while (VIM_ISWHITE(*p))
649 {
650 p++;
651 text_indent_len++;
652 }
653 text_indent = vim_strnsave(theline, text_indent_len);
654 }
655 // with "trim": skip the indent matching the first line
656 if (text_indent != NULL)
657 for (ti = 0; ti < text_indent_len; ++ti)
658 if (theline[ti] != text_indent[ti])
659 break;
660
661 if (list_append_string(l, theline + ti, -1) == FAIL)
662 break;
663 vim_free(theline);
664 }
665 vim_free(text_indent);
666
667 return l;
668}
669
670/*
671 * ":let" list all variable values
672 * ":let var1 var2" list variable values
673 * ":let var = expr" assignment command.
674 * ":let var += expr" assignment command.
675 * ":let var -= expr" assignment command.
676 * ":let var *= expr" assignment command.
677 * ":let var /= expr" assignment command.
678 * ":let var %= expr" assignment command.
679 * ":let var .= expr" assignment command.
680 * ":let var ..= expr" assignment command.
681 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100683 * ":let var: string" Vim9 declaration
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200684 */
685 void
686ex_let(exarg_T *eap)
687{
688 ex_let_const(eap, FALSE);
689}
690
691/*
692 * ":const" list all variable values
693 * ":const var1 var2" list variable values
694 * ":const var = expr" assignment command.
695 * ":const [var1, var2] = expr" unpack list.
696 */
697 void
698ex_const(exarg_T *eap)
699{
Bram Moolenaar09689a02020-05-09 22:50:08 +0200700 ex_let_const(eap, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200701}
702
Bram Moolenaar09689a02020-05-09 22:50:08 +0200703/*
704 * When "redefine" is TRUE the command will be executed again, redefining the
705 * variable is OK then.
706 */
707 void
708ex_let_const(exarg_T *eap, int redefine)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200709{
710 char_u *arg = eap->arg;
711 char_u *expr = NULL;
712 typval_T rettv;
713 int i;
714 int var_count = 0;
715 int semicolon = 0;
716 char_u op[2];
717 char_u *argend;
718 int first = TRUE;
719 int concat;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200720 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200721
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 // detect Vim9 assignment without ":let" or ":const"
723 if (eap->arg == eap->cmd)
724 flags |= LET_NO_COMMAND;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200725 if (redefine)
726 flags |= LET_REDEFINE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727
728 argend = skip_var_list(arg, TRUE, &var_count, &semicolon);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729 if (argend == NULL)
730 return;
731 if (argend > arg && argend[-1] == '.') // for var.='str'
732 --argend;
733 expr = skipwhite(argend);
734 concat = expr[0] == '.'
735 && ((expr[1] == '=' && current_sctx.sc_version < 2)
736 || (expr[1] == '.' && expr[2] == '='));
737 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
738 && expr[1] == '=') || concat))
739 {
740 // ":let" without "=": list variables
741 if (*arg == '[')
742 emsg(_(e_invarg));
743 else if (expr[0] == '.')
744 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200745 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200746 // ":let var1 var2"
747 arg = list_arg_vars(eap, arg, &first);
748 else if (!eap->skip)
749 {
750 // ":let"
751 list_glob_vars(&first);
752 list_buf_vars(&first);
753 list_win_vars(&first);
754 list_tab_vars(&first);
755 list_script_vars(&first);
756 list_func_vars(&first);
757 list_vim_vars(&first);
758 }
759 eap->nextcmd = check_nextcmd(arg);
760 }
761 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
762 {
763 list_T *l;
764
765 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200766 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200767 if (l != NULL)
768 {
769 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200770 if (!eap->skip)
771 {
772 op[0] = '=';
773 op[1] = NUL;
774 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200776 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200777 clear_tv(&rettv);
778 }
779 }
780 else
781 {
782 op[0] = '=';
783 op[1] = NUL;
784 if (*expr != '=')
785 {
786 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
787 {
788 op[0] = *expr; // +=, -=, *=, /=, %= or .=
789 if (expr[0] == '.' && expr[1] == '.') // ..=
790 ++expr;
791 }
792 expr = skipwhite(expr + 2);
793 }
794 else
795 expr = skipwhite(expr + 1);
796
797 if (eap->skip)
798 ++emsg_skip;
799 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
800 if (eap->skip)
801 {
802 if (i != FAIL)
803 clear_tv(&rettv);
804 --emsg_skip;
805 }
806 else if (i != FAIL)
807 {
808 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200810 clear_tv(&rettv);
811 }
812 }
813}
814
815/*
816 * Assign the typevalue "tv" to the variable or variables at "arg_start".
817 * Handles both "var" with any type and "[var, var; var]" with a list type.
818 * When "op" is not NULL it points to a string with characters that
819 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
820 * or concatenate.
821 * Returns OK or FAIL;
822 */
823 int
824ex_let_vars(
825 char_u *arg_start,
826 typval_T *tv,
827 int copy, // copy values from "tv", don't move
828 int semicolon, // from skip_var_list()
829 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200831 char_u *op)
832{
833 char_u *arg = arg_start;
834 list_T *l;
835 int i;
836 listitem_T *item;
837 typval_T ltv;
838
839 if (*arg != '[')
840 {
841 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200843 return FAIL;
844 return OK;
845 }
846
847 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
848 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
849 {
850 emsg(_(e_listreq));
851 return FAIL;
852 }
853
854 i = list_len(l);
855 if (semicolon == 0 && var_count < i)
856 {
857 emsg(_("E687: Less targets than List items"));
858 return FAIL;
859 }
860 if (var_count - semicolon > i)
861 {
862 emsg(_("E688: More targets than List items"));
863 return FAIL;
864 }
865
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200866 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200867 item = l->lv_first;
868 while (*arg != ']')
869 {
870 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100871 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200872 item = item->li_next;
873 if (arg == NULL)
874 return FAIL;
875
876 arg = skipwhite(arg);
877 if (*arg == ';')
878 {
879 // Put the rest of the list (may be empty) in the var after ';'.
880 // Create a new list for this.
881 l = list_alloc();
882 if (l == NULL)
883 return FAIL;
884 while (item != NULL)
885 {
886 list_append_tv(l, &item->li_tv);
887 item = item->li_next;
888 }
889
890 ltv.v_type = VAR_LIST;
891 ltv.v_lock = 0;
892 ltv.vval.v_list = l;
893 l->lv_refcount = 1;
894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200896 (char_u *)"]", op);
897 clear_tv(&ltv);
898 if (arg == NULL)
899 return FAIL;
900 break;
901 }
902 else if (*arg != ',' && *arg != ']')
903 {
904 internal_error("ex_let_vars()");
905 return FAIL;
906 }
907 }
908
909 return OK;
910}
911
912/*
913 * Skip over assignable variable "var" or list of variables "[var, var]".
914 * Used for ":let varvar = expr" and ":for varvar in expr".
915 * For "[var, var]" increment "*var_count" for each variable.
916 * for "[var, var; var]" set "semicolon".
917 * Return NULL for an error.
918 */
919 char_u *
920skip_var_list(
921 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200923 int *var_count,
924 int *semicolon)
925{
926 char_u *p, *s;
927
928 if (*arg == '[')
929 {
930 // "[var, var]": find the matching ']'.
931 p = arg;
932 for (;;)
933 {
934 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 s = skip_var_one(p, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200936 if (s == p)
937 {
938 semsg(_(e_invarg2), p);
939 return NULL;
940 }
941 ++*var_count;
942
943 p = skipwhite(s);
944 if (*p == ']')
945 break;
946 else if (*p == ';')
947 {
948 if (*semicolon == 1)
949 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200950 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200951 return NULL;
952 }
953 *semicolon = 1;
954 }
955 else if (*p != ',')
956 {
957 semsg(_(e_invarg2), p);
958 return NULL;
959 }
960 }
961 return p + 1;
962 }
963 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100964 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200965}
966
967/*
968 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
969 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200971 */
972 static char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200974{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 char_u *end;
976
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200977 if (*arg == '@' && arg[1] != NUL)
978 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9
982 && *end == ':')
983 {
984 end = skip_type(skipwhite(end + 1));
985 }
986 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987}
988
989/*
990 * List variables for hashtab "ht" with prefix "prefix".
991 * If "empty" is TRUE also list NULL strings as empty strings.
992 */
993 void
994list_hashtable_vars(
995 hashtab_T *ht,
996 char *prefix,
997 int empty,
998 int *first)
999{
1000 hashitem_T *hi;
1001 dictitem_T *di;
1002 int todo;
1003 char_u buf[IOSIZE];
1004
1005 todo = (int)ht->ht_used;
1006 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1007 {
1008 if (!HASHITEM_EMPTY(hi))
1009 {
1010 --todo;
1011 di = HI2DI(hi);
1012
1013 // apply :filter /pat/ to variable name
1014 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1015 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1016 if (message_filtered(buf))
1017 continue;
1018
1019 if (empty || di->di_tv.v_type != VAR_STRING
1020 || di->di_tv.vval.v_string != NULL)
1021 list_one_var(di, prefix, first);
1022 }
1023 }
1024}
1025
1026/*
1027 * List global variables.
1028 */
1029 static void
1030list_glob_vars(int *first)
1031{
1032 list_hashtable_vars(&globvarht, "", TRUE, first);
1033}
1034
1035/*
1036 * List buffer variables.
1037 */
1038 static void
1039list_buf_vars(int *first)
1040{
1041 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1042}
1043
1044/*
1045 * List window variables.
1046 */
1047 static void
1048list_win_vars(int *first)
1049{
1050 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1051}
1052
1053/*
1054 * List tab page variables.
1055 */
1056 static void
1057list_tab_vars(int *first)
1058{
1059 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1060}
1061
1062/*
1063 * List variables in "arg".
1064 */
1065 static char_u *
1066list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1067{
1068 int error = FALSE;
1069 int len;
1070 char_u *name;
1071 char_u *name_start;
1072 char_u *arg_subsc;
1073 char_u *tofree;
1074 typval_T tv;
1075
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001076 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001077 {
1078 if (error || eap->skip)
1079 {
1080 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1081 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1082 {
1083 emsg_severe = TRUE;
1084 emsg(_(e_trailing));
1085 break;
1086 }
1087 }
1088 else
1089 {
1090 // get_name_len() takes care of expanding curly braces
1091 name_start = name = arg;
1092 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1093 if (len <= 0)
1094 {
1095 // This is mainly to keep test 49 working: when expanding
1096 // curly braces fails overrule the exception error message.
1097 if (len < 0 && !aborting())
1098 {
1099 emsg_severe = TRUE;
1100 semsg(_(e_invarg2), arg);
1101 break;
1102 }
1103 error = TRUE;
1104 }
1105 else
1106 {
1107 if (tofree != NULL)
1108 name = tofree;
1109 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1110 error = TRUE;
1111 else
1112 {
1113 // handle d.key, l[idx], f(expr)
1114 arg_subsc = arg;
1115 if (handle_subscript(&arg, &tv, TRUE, TRUE,
1116 name, &name) == FAIL)
1117 error = TRUE;
1118 else
1119 {
1120 if (arg == arg_subsc && len == 2 && name[1] == ':')
1121 {
1122 switch (*name)
1123 {
1124 case 'g': list_glob_vars(first); break;
1125 case 'b': list_buf_vars(first); break;
1126 case 'w': list_win_vars(first); break;
1127 case 't': list_tab_vars(first); break;
1128 case 'v': list_vim_vars(first); break;
1129 case 's': list_script_vars(first); break;
1130 case 'l': list_func_vars(first); break;
1131 default:
1132 semsg(_("E738: Can't list variables for %s"), name);
1133 }
1134 }
1135 else
1136 {
1137 char_u numbuf[NUMBUFLEN];
1138 char_u *tf;
1139 int c;
1140 char_u *s;
1141
1142 s = echo_string(&tv, &tf, numbuf, 0);
1143 c = *arg;
1144 *arg = NUL;
1145 list_one_var_a("",
1146 arg == arg_subsc ? name : name_start,
1147 tv.v_type,
1148 s == NULL ? (char_u *)"" : s,
1149 first);
1150 *arg = c;
1151 vim_free(tf);
1152 }
1153 clear_tv(&tv);
1154 }
1155 }
1156 }
1157
1158 vim_free(tofree);
1159 }
1160
1161 arg = skipwhite(arg);
1162 }
1163
1164 return arg;
1165}
1166
1167/*
1168 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1169 * Returns a pointer to the char just after the var name.
1170 * Returns NULL if there is an error.
1171 */
1172 static char_u *
1173ex_let_one(
1174 char_u *arg, // points to variable name
1175 typval_T *tv, // value to assign to variable
1176 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001178 char_u *endchars, // valid chars after variable name or NULL
1179 char_u *op) // "+", "-", "." or NULL
1180{
1181 int c1;
1182 char_u *name;
1183 char_u *p;
1184 char_u *arg_end = NULL;
1185 int len;
1186 int opt_flags;
1187 char_u *tofree = NULL;
1188
1189 // ":let $VAR = expr": Set environment variable.
1190 if (*arg == '$')
1191 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001193 {
1194 emsg(_("E996: Cannot lock an environment variable"));
1195 return NULL;
1196 }
1197 // Find the end of the name.
1198 ++arg;
1199 name = arg;
1200 len = get_env_len(&arg);
1201 if (len == 0)
1202 semsg(_(e_invarg2), name - 1);
1203 else
1204 {
1205 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1206 semsg(_(e_letwrong), op);
1207 else if (endchars != NULL
1208 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1209 emsg(_(e_letunexp));
1210 else if (!check_secure())
1211 {
1212 c1 = name[len];
1213 name[len] = NUL;
1214 p = tv_get_string_chk(tv);
1215 if (p != NULL && op != NULL && *op == '.')
1216 {
1217 int mustfree = FALSE;
1218 char_u *s = vim_getenv(name, &mustfree);
1219
1220 if (s != NULL)
1221 {
1222 p = tofree = concat_str(s, p);
1223 if (mustfree)
1224 vim_free(s);
1225 }
1226 }
1227 if (p != NULL)
1228 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001229 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001230 arg_end = arg;
1231 }
1232 name[len] = c1;
1233 vim_free(tofree);
1234 }
1235 }
1236 }
1237
1238 // ":let &option = expr": Set option value.
1239 // ":let &l:option = expr": Set local option value.
1240 // ":let &g:option = expr": Set global option value.
1241 else if (*arg == '&')
1242 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001244 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001246 return NULL;
1247 }
1248 // Find the end of the name.
1249 p = find_option_end(&arg, &opt_flags);
1250 if (p == NULL || (endchars != NULL
1251 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1252 emsg(_(e_letunexp));
1253 else
1254 {
1255 long n;
1256 int opt_type;
1257 long numval;
1258 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001259 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001260
1261 c1 = *p;
1262 *p = NUL;
1263
1264 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001265 // avoid setting a string option to the text "v:false" or similar.
1266 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1267 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001268 if (s != NULL && op != NULL && *op != '=')
1269 {
1270 opt_type = get_option_value(arg, &numval,
1271 &stringval, opt_flags);
1272 if ((opt_type == 1 && *op == '.')
1273 || (opt_type == 0 && *op != '.'))
1274 {
1275 semsg(_(e_letwrong), op);
1276 s = NULL; // don't set the value
1277 }
1278 else
1279 {
1280 if (opt_type == 1) // number
1281 {
1282 switch (*op)
1283 {
1284 case '+': n = numval + n; break;
1285 case '-': n = numval - n; break;
1286 case '*': n = numval * n; break;
1287 case '/': n = (long)num_divide(numval, n); break;
1288 case '%': n = (long)num_modulus(numval, n); break;
1289 }
1290 }
1291 else if (opt_type == 0 && stringval != NULL) // string
1292 {
1293 s = concat_str(stringval, s);
1294 vim_free(stringval);
1295 stringval = s;
1296 }
1297 }
1298 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001299 if (s != NULL || tv->v_type == VAR_BOOL
1300 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001301 {
1302 set_option_value(arg, n, s, opt_flags);
1303 arg_end = p;
1304 }
1305 *p = c1;
1306 vim_free(stringval);
1307 }
1308 }
1309
1310 // ":let @r = expr": Set register contents.
1311 else if (*arg == '@')
1312 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001314 {
1315 emsg(_("E996: Cannot lock a register"));
1316 return NULL;
1317 }
1318 ++arg;
1319 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1320 semsg(_(e_letwrong), op);
1321 else if (endchars != NULL
1322 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1323 emsg(_(e_letunexp));
1324 else
1325 {
1326 char_u *ptofree = NULL;
1327 char_u *s;
1328
1329 p = tv_get_string_chk(tv);
1330 if (p != NULL && op != NULL && *op == '.')
1331 {
1332 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1333 if (s != NULL)
1334 {
1335 p = ptofree = concat_str(s, p);
1336 vim_free(s);
1337 }
1338 }
1339 if (p != NULL)
1340 {
1341 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1342 arg_end = arg + 1;
1343 }
1344 vim_free(ptofree);
1345 }
1346 }
1347
1348 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001350 // ":let {expr} = expr": Idem, name made with curly braces
1351 else if (eval_isnamec1(*arg) || *arg == '{')
1352 {
1353 lval_T lv;
1354
1355 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
1356 if (p != NULL && lv.ll_name != NULL)
1357 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 if (endchars != NULL && vim_strchr(endchars,
1359 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001360 emsg(_(e_letunexp));
1361 else
1362 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001364 arg_end = p;
1365 }
1366 }
1367 clear_lval(&lv);
1368 }
1369
1370 else
1371 semsg(_(e_invarg2), arg);
1372
1373 return arg_end;
1374}
1375
1376/*
1377 * ":unlet[!] var1 ... " command.
1378 */
1379 void
1380ex_unlet(exarg_T *eap)
1381{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001382 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001383}
1384
1385/*
1386 * ":lockvar" and ":unlockvar" commands
1387 */
1388 void
1389ex_lockvar(exarg_T *eap)
1390{
1391 char_u *arg = eap->arg;
1392 int deep = 2;
1393
1394 if (eap->forceit)
1395 deep = -1;
1396 else if (vim_isdigit(*arg))
1397 {
1398 deep = getdigits(&arg);
1399 arg = skipwhite(arg);
1400 }
1401
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001402 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001403}
1404
1405/*
1406 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001407 * Also used for Vim9 script. "callback" is invoked as:
1408 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001409 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001410 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001411ex_unletlock(
1412 exarg_T *eap,
1413 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001414 int deep,
1415 int glv_flags,
1416 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1417 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001418{
1419 char_u *arg = argstart;
1420 char_u *name_end;
1421 int error = FALSE;
1422 lval_T lv;
1423
1424 do
1425 {
1426 if (*arg == '$')
1427 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001428 lv.ll_name = arg;
1429 lv.ll_tv = NULL;
1430 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001431 if (get_env_len(&arg) == 0)
1432 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001433 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001434 return;
1435 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001436 if (!error && !eap->skip
1437 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1438 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001439 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001440 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001441 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001442 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001443 // Parse the name and find the end.
1444 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1445 glv_flags, FNE_CHECK_START);
1446 if (lv.ll_name == NULL)
1447 error = TRUE; // error but continue parsing
1448 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1449 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001451 if (name_end != NULL)
1452 {
1453 emsg_severe = TRUE;
1454 emsg(_(e_trailing));
1455 }
1456 if (!(eap->skip || error))
1457 clear_lval(&lv);
1458 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001459 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001460
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001461 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001462 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001463 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001464
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001465 if (!eap->skip)
1466 clear_lval(&lv);
1467 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001468
1469 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001470 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001471
1472 eap->nextcmd = check_nextcmd(arg);
1473}
1474
1475 static int
1476do_unlet_var(
1477 lval_T *lp,
1478 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001479 exarg_T *eap,
1480 int deep UNUSED,
1481 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001482{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001483 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001484 int ret = OK;
1485 int cc;
1486
1487 if (lp->ll_tv == NULL)
1488 {
1489 cc = *name_end;
1490 *name_end = NUL;
1491
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001492 // Environment variable, normal name or expanded name.
1493 if (*lp->ll_name == '$')
1494 vim_unsetenv(lp->ll_name + 1);
1495 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001496 ret = FAIL;
1497 *name_end = cc;
1498 }
1499 else if ((lp->ll_list != NULL
1500 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1501 || (lp->ll_dict != NULL
1502 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1503 return FAIL;
1504 else if (lp->ll_range)
1505 {
1506 listitem_T *li;
1507 listitem_T *ll_li = lp->ll_li;
1508 int ll_n1 = lp->ll_n1;
1509
1510 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1511 {
1512 li = ll_li->li_next;
1513 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1514 return FAIL;
1515 ll_li = li;
1516 ++ll_n1;
1517 }
1518
1519 // Delete a range of List items.
1520 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1521 {
1522 li = lp->ll_li->li_next;
1523 listitem_remove(lp->ll_list, lp->ll_li);
1524 lp->ll_li = li;
1525 ++lp->ll_n1;
1526 }
1527 }
1528 else
1529 {
1530 if (lp->ll_list != NULL)
1531 // unlet a List item.
1532 listitem_remove(lp->ll_list, lp->ll_li);
1533 else
1534 // unlet a Dictionary item.
1535 dictitem_remove(lp->ll_dict, lp->ll_di);
1536 }
1537
1538 return ret;
1539}
1540
1541/*
1542 * "unlet" a variable. Return OK if it existed, FAIL if not.
1543 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1544 */
1545 int
1546do_unlet(char_u *name, int forceit)
1547{
1548 hashtab_T *ht;
1549 hashitem_T *hi;
1550 char_u *varname;
1551 dict_T *d;
1552 dictitem_T *di;
1553
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001554 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1555 && check_vim9_unlet(name) == FAIL)
1556 return FAIL;
1557
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001558 ht = find_var_ht(name, &varname);
1559 if (ht != NULL && *varname != NUL)
1560 {
1561 d = get_current_funccal_dict(ht);
1562 if (d == NULL)
1563 {
1564 if (ht == &globvarht)
1565 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001566 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001567 d = &vimvardict;
1568 else
1569 {
1570 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1571 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1572 }
1573 if (d == NULL)
1574 {
1575 internal_error("do_unlet()");
1576 return FAIL;
1577 }
1578 }
1579 hi = hash_find(ht, varname);
1580 if (HASHITEM_EMPTY(hi))
1581 hi = find_hi_in_scoped_ht(name, &ht);
1582 if (hi != NULL && !HASHITEM_EMPTY(hi))
1583 {
1584 di = HI2DI(hi);
1585 if (var_check_fixed(di->di_flags, name, FALSE)
1586 || var_check_ro(di->di_flags, name, FALSE)
1587 || var_check_lock(d->dv_lock, name, FALSE))
1588 return FAIL;
1589
1590 delete_var(ht, hi);
1591 return OK;
1592 }
1593 }
1594 if (forceit)
1595 return OK;
1596 semsg(_("E108: No such variable: \"%s\""), name);
1597 return FAIL;
1598}
1599
1600/*
1601 * Lock or unlock variable indicated by "lp".
1602 * "deep" is the levels to go (-1 for unlimited);
1603 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1604 */
1605 static int
1606do_lock_var(
1607 lval_T *lp,
1608 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001609 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001610 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001611 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001613 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001614 int ret = OK;
1615 int cc;
1616 dictitem_T *di;
1617
1618 if (deep == 0) // nothing to do
1619 return OK;
1620
1621 if (lp->ll_tv == NULL)
1622 {
1623 cc = *name_end;
1624 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001625 if (*lp->ll_name == '$')
1626 {
1627 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001628 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001629 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001630 else
1631 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001632 // Normal name or expanded name.
1633 di = find_var(lp->ll_name, NULL, TRUE);
1634 if (di == NULL)
1635 ret = FAIL;
1636 else if ((di->di_flags & DI_FLAGS_FIX)
1637 && di->di_tv.v_type != VAR_DICT
1638 && di->di_tv.v_type != VAR_LIST)
1639 {
1640 // For historic reasons this error is not given for a list or
1641 // dict. E.g., the b: dict could be locked/unlocked.
1642 semsg(_(e_lock_unlock), lp->ll_name);
1643 ret = FAIL;
1644 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001645 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001646 {
1647 if (lock)
1648 di->di_flags |= DI_FLAGS_LOCK;
1649 else
1650 di->di_flags &= ~DI_FLAGS_LOCK;
1651 item_lock(&di->di_tv, deep, lock);
1652 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001653 }
1654 *name_end = cc;
1655 }
1656 else if (lp->ll_range)
1657 {
1658 listitem_T *li = lp->ll_li;
1659
1660 // (un)lock a range of List items.
1661 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1662 {
1663 item_lock(&li->li_tv, deep, lock);
1664 li = li->li_next;
1665 ++lp->ll_n1;
1666 }
1667 }
1668 else if (lp->ll_list != NULL)
1669 // (un)lock a List item.
1670 item_lock(&lp->ll_li->li_tv, deep, lock);
1671 else
1672 // (un)lock a Dictionary item.
1673 item_lock(&lp->ll_di->di_tv, deep, lock);
1674
1675 return ret;
1676}
1677
1678/*
1679 * Lock or unlock an item. "deep" is nr of levels to go.
1680 */
1681 static void
1682item_lock(typval_T *tv, int deep, int lock)
1683{
1684 static int recurse = 0;
1685 list_T *l;
1686 listitem_T *li;
1687 dict_T *d;
1688 blob_T *b;
1689 hashitem_T *hi;
1690 int todo;
1691
1692 if (recurse >= DICT_MAXNEST)
1693 {
1694 emsg(_("E743: variable nested too deep for (un)lock"));
1695 return;
1696 }
1697 if (deep == 0)
1698 return;
1699 ++recurse;
1700
1701 // lock/unlock the item itself
1702 if (lock)
1703 tv->v_lock |= VAR_LOCKED;
1704 else
1705 tv->v_lock &= ~VAR_LOCKED;
1706
1707 switch (tv->v_type)
1708 {
1709 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001710 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001712 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001714 case VAR_STRING:
1715 case VAR_FUNC:
1716 case VAR_PARTIAL:
1717 case VAR_FLOAT:
1718 case VAR_SPECIAL:
1719 case VAR_JOB:
1720 case VAR_CHANNEL:
1721 break;
1722
1723 case VAR_BLOB:
1724 if ((b = tv->vval.v_blob) != NULL)
1725 {
1726 if (lock)
1727 b->bv_lock |= VAR_LOCKED;
1728 else
1729 b->bv_lock &= ~VAR_LOCKED;
1730 }
1731 break;
1732 case VAR_LIST:
1733 if ((l = tv->vval.v_list) != NULL)
1734 {
1735 if (lock)
1736 l->lv_lock |= VAR_LOCKED;
1737 else
1738 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001739 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001740 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001741 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001742 item_lock(&li->li_tv, deep - 1, lock);
1743 }
1744 break;
1745 case VAR_DICT:
1746 if ((d = tv->vval.v_dict) != NULL)
1747 {
1748 if (lock)
1749 d->dv_lock |= VAR_LOCKED;
1750 else
1751 d->dv_lock &= ~VAR_LOCKED;
1752 if (deep < 0 || deep > 1)
1753 {
1754 // recursive: lock/unlock the items the List contains
1755 todo = (int)d->dv_hashtab.ht_used;
1756 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1757 {
1758 if (!HASHITEM_EMPTY(hi))
1759 {
1760 --todo;
1761 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1762 }
1763 }
1764 }
1765 }
1766 }
1767 --recurse;
1768}
1769
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001770#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1771/*
1772 * Delete all "menutrans_" variables.
1773 */
1774 void
1775del_menutrans_vars(void)
1776{
1777 hashitem_T *hi;
1778 int todo;
1779
1780 hash_lock(&globvarht);
1781 todo = (int)globvarht.ht_used;
1782 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1783 {
1784 if (!HASHITEM_EMPTY(hi))
1785 {
1786 --todo;
1787 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1788 delete_var(&globvarht, hi);
1789 }
1790 }
1791 hash_unlock(&globvarht);
1792}
1793#endif
1794
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001795/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001796 * Local string buffer for the next two functions to store a variable name
1797 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1798 * get_user_var_name().
1799 */
1800
1801static char_u *varnamebuf = NULL;
1802static int varnamebuflen = 0;
1803
1804/*
1805 * Function to concatenate a prefix and a variable name.
1806 */
1807 static char_u *
1808cat_prefix_varname(int prefix, char_u *name)
1809{
1810 int len;
1811
1812 len = (int)STRLEN(name) + 3;
1813 if (len > varnamebuflen)
1814 {
1815 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001816 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001817 varnamebuf = alloc(len);
1818 if (varnamebuf == NULL)
1819 {
1820 varnamebuflen = 0;
1821 return NULL;
1822 }
1823 varnamebuflen = len;
1824 }
1825 *varnamebuf = prefix;
1826 varnamebuf[1] = ':';
1827 STRCPY(varnamebuf + 2, name);
1828 return varnamebuf;
1829}
1830
1831/*
1832 * Function given to ExpandGeneric() to obtain the list of user defined
1833 * (global/buffer/window/built-in) variable names.
1834 */
1835 char_u *
1836get_user_var_name(expand_T *xp, int idx)
1837{
1838 static long_u gdone;
1839 static long_u bdone;
1840 static long_u wdone;
1841 static long_u tdone;
1842 static int vidx;
1843 static hashitem_T *hi;
1844 hashtab_T *ht;
1845
1846 if (idx == 0)
1847 {
1848 gdone = bdone = wdone = vidx = 0;
1849 tdone = 0;
1850 }
1851
1852 // Global variables
1853 if (gdone < globvarht.ht_used)
1854 {
1855 if (gdone++ == 0)
1856 hi = globvarht.ht_array;
1857 else
1858 ++hi;
1859 while (HASHITEM_EMPTY(hi))
1860 ++hi;
1861 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1862 return cat_prefix_varname('g', hi->hi_key);
1863 return hi->hi_key;
1864 }
1865
1866 // b: variables
1867 ht = &curbuf->b_vars->dv_hashtab;
1868 if (bdone < ht->ht_used)
1869 {
1870 if (bdone++ == 0)
1871 hi = ht->ht_array;
1872 else
1873 ++hi;
1874 while (HASHITEM_EMPTY(hi))
1875 ++hi;
1876 return cat_prefix_varname('b', hi->hi_key);
1877 }
1878
1879 // w: variables
1880 ht = &curwin->w_vars->dv_hashtab;
1881 if (wdone < ht->ht_used)
1882 {
1883 if (wdone++ == 0)
1884 hi = ht->ht_array;
1885 else
1886 ++hi;
1887 while (HASHITEM_EMPTY(hi))
1888 ++hi;
1889 return cat_prefix_varname('w', hi->hi_key);
1890 }
1891
1892 // t: variables
1893 ht = &curtab->tp_vars->dv_hashtab;
1894 if (tdone < ht->ht_used)
1895 {
1896 if (tdone++ == 0)
1897 hi = ht->ht_array;
1898 else
1899 ++hi;
1900 while (HASHITEM_EMPTY(hi))
1901 ++hi;
1902 return cat_prefix_varname('t', hi->hi_key);
1903 }
1904
1905 // v: variables
1906 if (vidx < VV_LEN)
1907 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1908
1909 VIM_CLEAR(varnamebuf);
1910 varnamebuflen = 0;
1911 return NULL;
1912}
1913
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001914 char *
1915get_var_special_name(int nr)
1916{
1917 switch (nr)
1918 {
1919 case VVAL_FALSE: return "v:false";
1920 case VVAL_TRUE: return "v:true";
1921 case VVAL_NONE: return "v:none";
1922 case VVAL_NULL: return "v:null";
1923 }
1924 internal_error("get_var_special_name()");
1925 return "42";
1926}
1927
1928/*
1929 * Returns the global variable dictionary
1930 */
1931 dict_T *
1932get_globvar_dict(void)
1933{
1934 return &globvardict;
1935}
1936
1937/*
1938 * Returns the global variable hash table
1939 */
1940 hashtab_T *
1941get_globvar_ht(void)
1942{
1943 return &globvarht;
1944}
1945
1946/*
1947 * Returns the v: variable dictionary
1948 */
1949 dict_T *
1950get_vimvar_dict(void)
1951{
1952 return &vimvardict;
1953}
1954
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001955/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001957 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 */
1959 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001960find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001962 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1963 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964
1965 if (di == NULL)
1966 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001967 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1969 return (int)(vv - vimvars);
1970}
1971
1972
1973/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001974 * Set type of v: variable to "type".
1975 */
1976 void
1977set_vim_var_type(int idx, vartype_T type)
1978{
1979 vimvars[idx].vv_type = type;
1980}
1981
1982/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001983 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001984 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001985 */
1986 void
1987set_vim_var_nr(int idx, varnumber_T val)
1988{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001989 vimvars[idx].vv_nr = val;
1990}
1991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 char *
1993get_vim_var_name(int idx)
1994{
1995 return vimvars[idx].vv_name;
1996}
1997
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001998/*
1999 * Get typval_T v: variable value.
2000 */
2001 typval_T *
2002get_vim_var_tv(int idx)
2003{
2004 return &vimvars[idx].vv_tv;
2005}
2006
2007/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002008 * Set v: variable to "tv". Only accepts the same type.
2009 * Takes over the value of "tv".
2010 */
2011 int
2012set_vim_var_tv(int idx, typval_T *tv)
2013{
2014 if (vimvars[idx].vv_type != tv->v_type)
2015 {
2016 emsg(_("E1063: type mismatch for v: variable"));
2017 clear_tv(tv);
2018 return FAIL;
2019 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002020 // VV_RO is also checked when compiling, but let's check here as well.
2021 if (vimvars[idx].vv_flags & VV_RO)
2022 {
2023 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2024 return FAIL;
2025 }
2026 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2027 {
2028 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2029 return FAIL;
2030 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002031 clear_tv(&vimvars[idx].vv_di.di_tv);
2032 vimvars[idx].vv_di.di_tv = *tv;
2033 return OK;
2034}
2035
2036/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002037 * Get number v: variable value.
2038 */
2039 varnumber_T
2040get_vim_var_nr(int idx)
2041{
2042 return vimvars[idx].vv_nr;
2043}
2044
2045/*
2046 * Get string v: variable value. Uses a static buffer, can only be used once.
2047 * If the String variable has never been set, return an empty string.
2048 * Never returns NULL;
2049 */
2050 char_u *
2051get_vim_var_str(int idx)
2052{
2053 return tv_get_string(&vimvars[idx].vv_tv);
2054}
2055
2056/*
2057 * Get List v: variable value. Caller must take care of reference count when
2058 * needed.
2059 */
2060 list_T *
2061get_vim_var_list(int idx)
2062{
2063 return vimvars[idx].vv_list;
2064}
2065
2066/*
2067 * Get Dict v: variable value. Caller must take care of reference count when
2068 * needed.
2069 */
2070 dict_T *
2071get_vim_var_dict(int idx)
2072{
2073 return vimvars[idx].vv_dict;
2074}
2075
2076/*
2077 * Set v:char to character "c".
2078 */
2079 void
2080set_vim_var_char(int c)
2081{
2082 char_u buf[MB_MAXBYTES + 1];
2083
2084 if (has_mbyte)
2085 buf[(*mb_char2bytes)(c, buf)] = NUL;
2086 else
2087 {
2088 buf[0] = c;
2089 buf[1] = NUL;
2090 }
2091 set_vim_var_string(VV_CHAR, buf, -1);
2092}
2093
2094/*
2095 * Set v:count to "count" and v:count1 to "count1".
2096 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2097 */
2098 void
2099set_vcount(
2100 long count,
2101 long count1,
2102 int set_prevcount)
2103{
2104 if (set_prevcount)
2105 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2106 vimvars[VV_COUNT].vv_nr = count;
2107 vimvars[VV_COUNT1].vv_nr = count1;
2108}
2109
2110/*
2111 * Save variables that might be changed as a side effect. Used when executing
2112 * a timer callback.
2113 */
2114 void
2115save_vimvars(vimvars_save_T *vvsave)
2116{
2117 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2118 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2119 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2120}
2121
2122/*
2123 * Restore variables saved by save_vimvars().
2124 */
2125 void
2126restore_vimvars(vimvars_save_T *vvsave)
2127{
2128 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2129 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2130 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2131}
2132
2133/*
2134 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2135 * value.
2136 */
2137 void
2138set_vim_var_string(
2139 int idx,
2140 char_u *val,
2141 int len) // length of "val" to use or -1 (whole string)
2142{
2143 clear_tv(&vimvars[idx].vv_di.di_tv);
2144 vimvars[idx].vv_type = VAR_STRING;
2145 if (val == NULL)
2146 vimvars[idx].vv_str = NULL;
2147 else if (len == -1)
2148 vimvars[idx].vv_str = vim_strsave(val);
2149 else
2150 vimvars[idx].vv_str = vim_strnsave(val, len);
2151}
2152
2153/*
2154 * Set List v: variable to "val".
2155 */
2156 void
2157set_vim_var_list(int idx, list_T *val)
2158{
2159 clear_tv(&vimvars[idx].vv_di.di_tv);
2160 vimvars[idx].vv_type = VAR_LIST;
2161 vimvars[idx].vv_list = val;
2162 if (val != NULL)
2163 ++val->lv_refcount;
2164}
2165
2166/*
2167 * Set Dictionary v: variable to "val".
2168 */
2169 void
2170set_vim_var_dict(int idx, dict_T *val)
2171{
2172 clear_tv(&vimvars[idx].vv_di.di_tv);
2173 vimvars[idx].vv_type = VAR_DICT;
2174 vimvars[idx].vv_dict = val;
2175 if (val != NULL)
2176 {
2177 ++val->dv_refcount;
2178 dict_set_items_ro(val);
2179 }
2180}
2181
2182/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002183 * Set the v:argv list.
2184 */
2185 void
2186set_argv_var(char **argv, int argc)
2187{
2188 list_T *l = list_alloc();
2189 int i;
2190
2191 if (l == NULL)
2192 getout(1);
2193 l->lv_lock = VAR_FIXED;
2194 for (i = 0; i < argc; ++i)
2195 {
2196 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2197 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002198 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002199 }
2200 set_vim_var_list(VV_ARGV, l);
2201}
2202
2203/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002204 * Set v:register if needed.
2205 */
2206 void
2207set_reg_var(int c)
2208{
2209 char_u regname;
2210
2211 if (c == 0 || c == ' ')
2212 regname = '"';
2213 else
2214 regname = c;
2215 // Avoid free/alloc when the value is already right.
2216 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2217 set_vim_var_string(VV_REG, &regname, 1);
2218}
2219
2220/*
2221 * Get or set v:exception. If "oldval" == NULL, return the current value.
2222 * Otherwise, restore the value to "oldval" and return NULL.
2223 * Must always be called in pairs to save and restore v:exception! Does not
2224 * take care of memory allocations.
2225 */
2226 char_u *
2227v_exception(char_u *oldval)
2228{
2229 if (oldval == NULL)
2230 return vimvars[VV_EXCEPTION].vv_str;
2231
2232 vimvars[VV_EXCEPTION].vv_str = oldval;
2233 return NULL;
2234}
2235
2236/*
2237 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2238 * Otherwise, restore the value to "oldval" and return NULL.
2239 * Must always be called in pairs to save and restore v:throwpoint! Does not
2240 * take care of memory allocations.
2241 */
2242 char_u *
2243v_throwpoint(char_u *oldval)
2244{
2245 if (oldval == NULL)
2246 return vimvars[VV_THROWPOINT].vv_str;
2247
2248 vimvars[VV_THROWPOINT].vv_str = oldval;
2249 return NULL;
2250}
2251
2252/*
2253 * Set v:cmdarg.
2254 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2255 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2256 * Must always be called in pairs!
2257 */
2258 char_u *
2259set_cmdarg(exarg_T *eap, char_u *oldarg)
2260{
2261 char_u *oldval;
2262 char_u *newval;
2263 unsigned len;
2264
2265 oldval = vimvars[VV_CMDARG].vv_str;
2266 if (eap == NULL)
2267 {
2268 vim_free(oldval);
2269 vimvars[VV_CMDARG].vv_str = oldarg;
2270 return NULL;
2271 }
2272
2273 if (eap->force_bin == FORCE_BIN)
2274 len = 6;
2275 else if (eap->force_bin == FORCE_NOBIN)
2276 len = 8;
2277 else
2278 len = 0;
2279
2280 if (eap->read_edit)
2281 len += 7;
2282
2283 if (eap->force_ff != 0)
2284 len += 10; // " ++ff=unix"
2285 if (eap->force_enc != 0)
2286 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2287 if (eap->bad_char != 0)
2288 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2289
2290 newval = alloc(len + 1);
2291 if (newval == NULL)
2292 return NULL;
2293
2294 if (eap->force_bin == FORCE_BIN)
2295 sprintf((char *)newval, " ++bin");
2296 else if (eap->force_bin == FORCE_NOBIN)
2297 sprintf((char *)newval, " ++nobin");
2298 else
2299 *newval = NUL;
2300
2301 if (eap->read_edit)
2302 STRCAT(newval, " ++edit");
2303
2304 if (eap->force_ff != 0)
2305 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2306 eap->force_ff == 'u' ? "unix"
2307 : eap->force_ff == 'd' ? "dos"
2308 : "mac");
2309 if (eap->force_enc != 0)
2310 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2311 eap->cmd + eap->force_enc);
2312 if (eap->bad_char == BAD_KEEP)
2313 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2314 else if (eap->bad_char == BAD_DROP)
2315 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2316 else if (eap->bad_char != 0)
2317 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2318 vimvars[VV_CMDARG].vv_str = newval;
2319 return oldval;
2320}
2321
2322/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002323 * Get the value of internal variable "name".
2324 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2325 */
2326 int
2327get_var_tv(
2328 char_u *name,
2329 int len, // length of "name"
2330 typval_T *rettv, // NULL when only checking existence
2331 dictitem_T **dip, // non-NULL when typval's dict item is needed
2332 int verbose, // may give error message
2333 int no_autoload) // do not use script autoloading
2334{
2335 int ret = OK;
2336 typval_T *tv = NULL;
2337 dictitem_T *v;
2338 int cc;
2339
2340 // truncate the name, so that we can use strcmp()
2341 cc = name[len];
2342 name[len] = NUL;
2343
2344 // Check for user-defined variables.
2345 v = find_var(name, NULL, no_autoload);
2346 if (v != NULL)
2347 {
2348 tv = &v->di_tv;
2349 if (dip != NULL)
2350 *dip = v;
2351 }
2352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 if (tv == NULL && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2354 {
Bram Moolenaar4e12a5d2020-02-03 20:50:59 +01002355 imported_T *import = find_imported(name, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356
2357 // imported variable from another script
2358 if (import != NULL)
2359 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002360 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2362 + import->imp_var_vals_idx;
2363 tv = sv->sv_tv;
2364 }
2365 }
2366
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002367 if (tv == NULL)
2368 {
2369 if (rettv != NULL && verbose)
2370 semsg(_(e_undefvar), name);
2371 ret = FAIL;
2372 }
2373 else if (rettv != NULL)
2374 copy_tv(tv, rettv);
2375
2376 name[len] = cc;
2377
2378 return ret;
2379}
2380
2381/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002382 * Check if variable "name[len]" is a local variable or an argument.
2383 * If so, "*eval_lavars_used" is set to TRUE.
2384 */
2385 void
2386check_vars(char_u *name, int len)
2387{
2388 int cc;
2389 char_u *varname;
2390 hashtab_T *ht;
2391
2392 if (eval_lavars_used == NULL)
2393 return;
2394
2395 // truncate the name, so that we can use strcmp()
2396 cc = name[len];
2397 name[len] = NUL;
2398
2399 ht = find_var_ht(name, &varname);
2400 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2401 {
2402 if (find_var(name, NULL, TRUE) != NULL)
2403 *eval_lavars_used = TRUE;
2404 }
2405
2406 name[len] = cc;
2407}
2408
2409/*
2410 * Find variable "name" in the list of variables.
2411 * Return a pointer to it if found, NULL if not found.
2412 * Careful: "a:0" variables don't have a name.
2413 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2414 * hashtab_T used.
2415 */
2416 dictitem_T *
2417find_var(char_u *name, hashtab_T **htp, int no_autoload)
2418{
2419 char_u *varname;
2420 hashtab_T *ht;
2421 dictitem_T *ret = NULL;
2422
2423 ht = find_var_ht(name, &varname);
2424 if (htp != NULL)
2425 *htp = ht;
2426 if (ht == NULL)
2427 return NULL;
2428 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2429 if (ret != NULL)
2430 return ret;
2431
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002432 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002433 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2434}
2435
2436/*
2437 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002438 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002439 * Returns NULL if not found.
2440 */
2441 dictitem_T *
2442find_var_in_ht(
2443 hashtab_T *ht,
2444 int htname,
2445 char_u *varname,
2446 int no_autoload)
2447{
2448 hashitem_T *hi;
2449
2450 if (*varname == NUL)
2451 {
2452 // Must be something like "s:", otherwise "ht" would be NULL.
2453 switch (htname)
2454 {
2455 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2456 case 'g': return &globvars_var;
2457 case 'v': return &vimvars_var;
2458 case 'b': return &curbuf->b_bufvar;
2459 case 'w': return &curwin->w_winvar;
2460 case 't': return &curtab->tp_winvar;
2461 case 'l': return get_funccal_local_var();
2462 case 'a': return get_funccal_args_var();
2463 }
2464 return NULL;
2465 }
2466
2467 hi = hash_find(ht, varname);
2468 if (HASHITEM_EMPTY(hi))
2469 {
2470 // For global variables we may try auto-loading the script. If it
2471 // worked find the variable again. Don't auto-load a script if it was
2472 // loaded already, otherwise it would be loaded every time when
2473 // checking if a function name is a Funcref variable.
2474 if (ht == &globvarht && !no_autoload)
2475 {
2476 // Note: script_autoload() may make "hi" invalid. It must either
2477 // be obtained again or not used.
2478 if (!script_autoload(varname, FALSE) || aborting())
2479 return NULL;
2480 hi = hash_find(ht, varname);
2481 }
2482 if (HASHITEM_EMPTY(hi))
2483 return NULL;
2484 }
2485 return HI2DI(hi);
2486}
2487
2488/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 * Get the script-local hashtab. NULL if not in a script context.
2490 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002491 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492get_script_local_ht(void)
2493{
2494 scid_T sid = current_sctx.sc_sid;
2495
2496 if (sid > 0 && sid <= script_items.ga_len)
2497 return &SCRIPT_VARS(sid);
2498 return NULL;
2499}
2500
2501/*
2502 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002503 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002505 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2507{
2508 hashtab_T *ht = get_script_local_ht();
2509 char_u buffer[30];
2510 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002511 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 hashitem_T *hi;
2513
2514 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002515 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 if (len < sizeof(buffer) - 1)
2517 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002518 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 vim_strncpy(buffer, name, len);
2520 p = buffer;
2521 }
2522 else
2523 {
2524 p = vim_strnsave(name, (int)len);
2525 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002526 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 }
2528
2529 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002530 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531
2532 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002533 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2534 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535
2536 if (p != buffer)
2537 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002538 // Don't return "buffer", gcc complains.
2539 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540}
2541
2542/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002543 * Find the hashtab used for a variable name.
2544 * Return NULL if the name is not valid.
2545 * Set "varname" to the start of name without ':'.
2546 */
2547 hashtab_T *
2548find_var_ht(char_u *name, char_u **varname)
2549{
2550 hashitem_T *hi;
2551 hashtab_T *ht;
2552
2553 if (name[0] == NUL)
2554 return NULL;
2555 if (name[1] != ':')
2556 {
2557 // The name must not start with a colon or #.
2558 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2559 return NULL;
2560 *varname = name;
2561
2562 // "version" is "v:version" in all scopes if scriptversion < 3.
2563 // Same for a few other variables marked with VV_COMPAT.
2564 if (current_sctx.sc_version < 3)
2565 {
2566 hi = hash_find(&compat_hashtab, name);
2567 if (!HASHITEM_EMPTY(hi))
2568 return &compat_hashtab;
2569 }
2570
2571 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 if (ht != NULL)
2573 return ht; // local variable
2574
2575 // in Vim9 script items at the script level are script-local
2576 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2577 {
2578 ht = get_script_local_ht();
2579 if (ht != NULL)
2580 return ht;
2581 }
2582
2583 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002584 }
2585 *varname = name + 2;
2586 if (*name == 'g') // global variable
2587 return &globvarht;
2588 // There must be no ':' or '#' in the rest of the name, unless g: is used
2589 if (vim_strchr(name + 2, ':') != NULL
2590 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2591 return NULL;
2592 if (*name == 'b') // buffer variable
2593 return &curbuf->b_vars->dv_hashtab;
2594 if (*name == 'w') // window variable
2595 return &curwin->w_vars->dv_hashtab;
2596 if (*name == 't') // tab page variable
2597 return &curtab->tp_vars->dv_hashtab;
2598 if (*name == 'v') // v: variable
2599 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002600 if (get_current_funccal() != NULL
2601 && get_current_funccal()->func->uf_dfunc_idx < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002603 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 if (*name == 'a') // a: function argument
2605 return get_funccal_args_ht();
2606 if (*name == 'l') // l: local function variable
2607 return get_funccal_local_ht();
2608 }
2609 if (*name == 's') // script variable
2610 {
2611 ht = get_script_local_ht();
2612 if (ht != NULL)
2613 return ht;
2614 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002615 return NULL;
2616}
2617
2618/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002619 * Get the string value of a (global/local) variable.
2620 * Note: see tv_get_string() for how long the pointer remains valid.
2621 * Returns NULL when it doesn't exist.
2622 */
2623 char_u *
2624get_var_value(char_u *name)
2625{
2626 dictitem_T *v;
2627
2628 v = find_var(name, NULL, FALSE);
2629 if (v == NULL)
2630 return NULL;
2631 return tv_get_string(&v->di_tv);
2632}
2633
2634/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002635 * Allocate a new hashtab for a sourced script. It will be used while
2636 * sourcing this script and when executing functions defined in the script.
2637 */
2638 void
2639new_script_vars(scid_T id)
2640{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002641 scriptvar_T *sv;
2642
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002643 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2644 if (sv == NULL)
2645 return;
2646 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002647 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002648}
2649
2650/*
2651 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2652 * point to it.
2653 */
2654 void
2655init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2656{
2657 hash_init(&dict->dv_hashtab);
2658 dict->dv_lock = 0;
2659 dict->dv_scope = scope;
2660 dict->dv_refcount = DO_NOT_FREE_CNT;
2661 dict->dv_copyID = 0;
2662 dict_var->di_tv.vval.v_dict = dict;
2663 dict_var->di_tv.v_type = VAR_DICT;
2664 dict_var->di_tv.v_lock = VAR_FIXED;
2665 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2666 dict_var->di_key[0] = NUL;
2667}
2668
2669/*
2670 * Unreference a dictionary initialized by init_var_dict().
2671 */
2672 void
2673unref_var_dict(dict_T *dict)
2674{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002675 // Now the dict needs to be freed if no one else is using it, go back to
2676 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002677 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2678 dict_unref(dict);
2679}
2680
2681/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002682 * Clean up a list of internal variables.
2683 * Frees all allocated variables and the value they contain.
2684 * Clears hashtab "ht", does not free it.
2685 */
2686 void
2687vars_clear(hashtab_T *ht)
2688{
2689 vars_clear_ext(ht, TRUE);
2690}
2691
2692/*
2693 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2694 */
2695 void
2696vars_clear_ext(hashtab_T *ht, int free_val)
2697{
2698 int todo;
2699 hashitem_T *hi;
2700 dictitem_T *v;
2701
2702 hash_lock(ht);
2703 todo = (int)ht->ht_used;
2704 for (hi = ht->ht_array; todo > 0; ++hi)
2705 {
2706 if (!HASHITEM_EMPTY(hi))
2707 {
2708 --todo;
2709
2710 // Free the variable. Don't remove it from the hashtab,
2711 // ht_array might change then. hash_clear() takes care of it
2712 // later.
2713 v = HI2DI(hi);
2714 if (free_val)
2715 clear_tv(&v->di_tv);
2716 if (v->di_flags & DI_FLAGS_ALLOC)
2717 vim_free(v);
2718 }
2719 }
2720 hash_clear(ht);
2721 ht->ht_used = 0;
2722}
2723
2724/*
2725 * Delete a variable from hashtab "ht" at item "hi".
2726 * Clear the variable value and free the dictitem.
2727 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002728 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002729delete_var(hashtab_T *ht, hashitem_T *hi)
2730{
2731 dictitem_T *di = HI2DI(hi);
2732
2733 hash_remove(ht, hi);
2734 clear_tv(&di->di_tv);
2735 vim_free(di);
2736}
2737
2738/*
2739 * List the value of one internal variable.
2740 */
2741 static void
2742list_one_var(dictitem_T *v, char *prefix, int *first)
2743{
2744 char_u *tofree;
2745 char_u *s;
2746 char_u numbuf[NUMBUFLEN];
2747
2748 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2749 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2750 s == NULL ? (char_u *)"" : s, first);
2751 vim_free(tofree);
2752}
2753
2754 static void
2755list_one_var_a(
2756 char *prefix,
2757 char_u *name,
2758 int type,
2759 char_u *string,
2760 int *first) // when TRUE clear rest of screen and set to FALSE
2761{
2762 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2763 msg_start();
2764 msg_puts(prefix);
2765 if (name != NULL) // "a:" vars don't have a name stored
2766 msg_puts((char *)name);
2767 msg_putchar(' ');
2768 msg_advance(22);
2769 if (type == VAR_NUMBER)
2770 msg_putchar('#');
2771 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2772 msg_putchar('*');
2773 else if (type == VAR_LIST)
2774 {
2775 msg_putchar('[');
2776 if (*string == '[')
2777 ++string;
2778 }
2779 else if (type == VAR_DICT)
2780 {
2781 msg_putchar('{');
2782 if (*string == '{')
2783 ++string;
2784 }
2785 else
2786 msg_putchar(' ');
2787
2788 msg_outtrans(string);
2789
2790 if (type == VAR_FUNC || type == VAR_PARTIAL)
2791 msg_puts("()");
2792 if (*first)
2793 {
2794 msg_clr_eos();
2795 *first = FALSE;
2796 }
2797}
2798
2799/*
2800 * Set variable "name" to value in "tv".
2801 * If the variable already exists, the value is updated.
2802 * Otherwise the variable is created.
2803 */
2804 void
2805set_var(
2806 char_u *name,
2807 typval_T *tv,
2808 int copy) // make copy of value in "tv"
2809{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002811}
2812
2813/*
2814 * Set variable "name" to value in "tv".
2815 * If the variable already exists and "is_const" is FALSE the value is updated.
2816 * Otherwise the variable is created.
2817 */
2818 void
2819set_var_const(
2820 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002822 typval_T *tv,
2823 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002825{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002826 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002827 char_u *varname;
2828 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002830
2831 ht = find_var_ht(name, &varname);
2832 if (ht == NULL || *varname == NUL)
2833 {
2834 semsg(_(e_illvar), name);
2835 return;
2836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002837 is_script_local = ht == get_script_local_ht();
2838
2839 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002840
2841 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 if (di == NULL)
2843 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002844
2845 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002847 return;
2848
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002850 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002852 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 if (flags & LET_IS_CONST)
2854 {
2855 emsg(_(e_cannot_mod));
2856 return;
2857 }
2858
2859 if (var_check_ro(di->di_flags, name, FALSE)
2860 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2861 return;
2862
2863 if ((flags & LET_NO_COMMAND) == 0
2864 && is_script_local
2865 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2866 {
2867 semsg(_("E1041: Redefining script item %s"), name);
2868 return;
2869 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002870 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 else
2872 // can only redefine once
2873 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002874
2875 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002878 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002879 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002880 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002882 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002884 if (copy || tv->v_type != VAR_STRING)
2885 {
2886 char_u *val = tv_get_string(tv);
2887
2888 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002889 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 if (di->di_tv.vval.v_string == NULL)
2891 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002892 }
2893 else
2894 {
2895 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002897 tv->vval.v_string = NULL;
2898 }
2899 return;
2900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002902 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002904 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002906#ifdef FEAT_SEARCH_EXTRA
2907 else if (STRCMP(varname, "hlsearch") == 0)
2908 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002910 redraw_all_later(SOME_VALID);
2911 }
2912#endif
2913 return;
2914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002916 {
2917 semsg(_("E963: setting %s to value with wrong type"), name);
2918 return;
2919 }
2920 }
2921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002923 }
2924 else // add a new variable
2925 {
2926 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002927 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002928 {
2929 semsg(_(e_illvar), name);
2930 return;
2931 }
2932
2933 // Make sure the variable name is valid.
2934 if (!valid_varname(varname))
2935 return;
2936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2938 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002939 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 STRCPY(di->di_key, varname);
2941 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002942 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002944 return;
2945 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 di->di_flags = DI_FLAGS_ALLOC;
2947 if (flags & LET_IS_CONST)
2948 di->di_flags |= DI_FLAGS_LOCK;
2949
2950 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2951 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002952 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953
2954 // Store a pointer to the typval_T, so that it can be found by
2955 // index instead of using a hastab lookup.
2956 if (ga_grow(&si->sn_var_vals, 1) == OK)
2957 {
2958 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2959 + si->sn_var_vals.ga_len;
2960 sv->sv_name = di->di_key;
2961 sv->sv_tv = &di->di_tv;
2962 sv->sv_type = type == NULL ? &t_any : type;
2963 sv->sv_const = (flags & LET_IS_CONST);
2964 sv->sv_export = is_export;
2965 ++si->sn_var_vals.ga_len;
2966
2967 // let ex_export() know the export worked.
2968 is_export = FALSE;
2969 }
2970 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002971 }
2972
2973 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002974 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002975 else
2976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 di->di_tv = *tv;
2978 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002979 init_tv(tv);
2980 }
2981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 if (flags & LET_IS_CONST)
2983 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar09689a02020-05-09 22:50:08 +02002984 if (flags & LET_REDEFINE)
2985 di->di_flags |= DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002986}
2987
2988/*
2989 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2990 * Also give an error message.
2991 */
2992 int
2993var_check_ro(int flags, char_u *name, int use_gettext)
2994{
2995 if (flags & DI_FLAGS_RO)
2996 {
2997 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
2998 return TRUE;
2999 }
3000 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3001 {
3002 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3003 return TRUE;
3004 }
3005 return FALSE;
3006}
3007
3008/*
3009 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3010 * Also give an error message.
3011 */
3012 int
3013var_check_fixed(int flags, char_u *name, int use_gettext)
3014{
3015 if (flags & DI_FLAGS_FIX)
3016 {
3017 semsg(_("E795: Cannot delete variable %s"),
3018 use_gettext ? (char_u *)_(name) : name);
3019 return TRUE;
3020 }
3021 return FALSE;
3022}
3023
3024/*
3025 * Check if a funcref is assigned to a valid variable name.
3026 * Return TRUE and give an error if not.
3027 */
3028 int
3029var_check_func_name(
3030 char_u *name, // points to start of variable name
3031 int new_var) // TRUE when creating the variable
3032{
3033 // Allow for w: b: s: and t:.
3034 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3035 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3036 ? name[2] : name[0]))
3037 {
3038 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3039 name);
3040 return TRUE;
3041 }
3042 // Don't allow hiding a function. When "v" is not NULL we might be
3043 // assigning another function to the same var, the type is checked
3044 // below.
3045 if (new_var && function_exists(name, FALSE))
3046 {
3047 semsg(_("E705: Variable name conflicts with existing function: %s"),
3048 name);
3049 return TRUE;
3050 }
3051 return FALSE;
3052}
3053
3054/*
3055 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3056 * Also give an error message, using "name" or _("name") when use_gettext is
3057 * TRUE.
3058 */
3059 int
3060var_check_lock(int lock, char_u *name, int use_gettext)
3061{
3062 if (lock & VAR_LOCKED)
3063 {
3064 semsg(_("E741: Value is locked: %s"),
3065 name == NULL ? (char_u *)_("Unknown")
3066 : use_gettext ? (char_u *)_(name)
3067 : name);
3068 return TRUE;
3069 }
3070 if (lock & VAR_FIXED)
3071 {
3072 semsg(_("E742: Cannot change value of %s"),
3073 name == NULL ? (char_u *)_("Unknown")
3074 : use_gettext ? (char_u *)_(name)
3075 : name);
3076 return TRUE;
3077 }
3078 return FALSE;
3079}
3080
3081/*
3082 * Check if a variable name is valid.
3083 * Return FALSE and give an error if not.
3084 */
3085 int
3086valid_varname(char_u *varname)
3087{
3088 char_u *p;
3089
3090 for (p = varname; *p != NUL; ++p)
3091 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3092 && *p != AUTOLOAD_CHAR)
3093 {
3094 semsg(_(e_illvar), varname);
3095 return FALSE;
3096 }
3097 return TRUE;
3098}
3099
3100/*
3101 * getwinvar() and gettabwinvar()
3102 */
3103 static void
3104getwinvar(
3105 typval_T *argvars,
3106 typval_T *rettv,
3107 int off) // 1 for gettabwinvar()
3108{
3109 win_T *win;
3110 char_u *varname;
3111 dictitem_T *v;
3112 tabpage_T *tp = NULL;
3113 int done = FALSE;
3114 win_T *oldcurwin;
3115 tabpage_T *oldtabpage;
3116 int need_switch_win;
3117
3118 if (off == 1)
3119 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3120 else
3121 tp = curtab;
3122 win = find_win_by_nr(&argvars[off], tp);
3123 varname = tv_get_string_chk(&argvars[off + 1]);
3124 ++emsg_off;
3125
3126 rettv->v_type = VAR_STRING;
3127 rettv->vval.v_string = NULL;
3128
3129 if (win != NULL && varname != NULL)
3130 {
3131 // Set curwin to be our win, temporarily. Also set the tabpage,
3132 // otherwise the window is not valid. Only do this when needed,
3133 // autocommands get blocked.
3134 need_switch_win = !(tp == curtab && win == curwin);
3135 if (!need_switch_win
3136 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3137 {
3138 if (*varname == '&')
3139 {
3140 if (varname[1] == NUL)
3141 {
3142 // get all window-local options in a dict
3143 dict_T *opts = get_winbuf_options(FALSE);
3144
3145 if (opts != NULL)
3146 {
3147 rettv_dict_set(rettv, opts);
3148 done = TRUE;
3149 }
3150 }
3151 else if (get_option_tv(&varname, rettv, 1) == OK)
3152 // window-local-option
3153 done = TRUE;
3154 }
3155 else
3156 {
3157 // Look up the variable.
3158 // Let getwinvar({nr}, "") return the "w:" dictionary.
3159 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3160 varname, FALSE);
3161 if (v != NULL)
3162 {
3163 copy_tv(&v->di_tv, rettv);
3164 done = TRUE;
3165 }
3166 }
3167 }
3168
3169 if (need_switch_win)
3170 // restore previous notion of curwin
3171 restore_win(oldcurwin, oldtabpage, TRUE);
3172 }
3173
3174 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3175 // use the default return value
3176 copy_tv(&argvars[off + 2], rettv);
3177
3178 --emsg_off;
3179}
3180
3181/*
3182 * "setwinvar()" and "settabwinvar()" functions
3183 */
3184 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003185setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003186{
3187 win_T *win;
3188 win_T *save_curwin;
3189 tabpage_T *save_curtab;
3190 int need_switch_win;
3191 char_u *varname, *winvarname;
3192 typval_T *varp;
3193 char_u nbuf[NUMBUFLEN];
3194 tabpage_T *tp = NULL;
3195
3196 if (check_secure())
3197 return;
3198
3199 if (off == 1)
3200 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3201 else
3202 tp = curtab;
3203 win = find_win_by_nr(&argvars[off], tp);
3204 varname = tv_get_string_chk(&argvars[off + 1]);
3205 varp = &argvars[off + 2];
3206
3207 if (win != NULL && varname != NULL && varp != NULL)
3208 {
3209 need_switch_win = !(tp == curtab && win == curwin);
3210 if (!need_switch_win
3211 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3212 {
3213 if (*varname == '&')
3214 {
3215 long numval;
3216 char_u *strval;
3217 int error = FALSE;
3218
3219 ++varname;
3220 numval = (long)tv_get_number_chk(varp, &error);
3221 strval = tv_get_string_buf_chk(varp, nbuf);
3222 if (!error && strval != NULL)
3223 set_option_value(varname, numval, strval, OPT_LOCAL);
3224 }
3225 else
3226 {
3227 winvarname = alloc(STRLEN(varname) + 3);
3228 if (winvarname != NULL)
3229 {
3230 STRCPY(winvarname, "w:");
3231 STRCPY(winvarname + 2, varname);
3232 set_var(winvarname, varp, TRUE);
3233 vim_free(winvarname);
3234 }
3235 }
3236 }
3237 if (need_switch_win)
3238 restore_win(save_curwin, save_curtab, TRUE);
3239 }
3240}
3241
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003242/*
3243 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3244 * v:option_type, and v:option_command.
3245 */
3246 void
3247reset_v_option_vars(void)
3248{
3249 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3250 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3251 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3252 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3253 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3254 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3255}
3256
3257/*
3258 * Add an assert error to v:errors.
3259 */
3260 void
3261assert_error(garray_T *gap)
3262{
3263 struct vimvar *vp = &vimvars[VV_ERRORS];
3264
3265 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003266 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003267 set_vim_var_list(VV_ERRORS, list_alloc());
3268 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3269}
3270
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003271 int
3272var_exists(char_u *var)
3273{
3274 char_u *name;
3275 char_u *tofree;
3276 typval_T tv;
3277 int len = 0;
3278 int n = FALSE;
3279
3280 // get_name_len() takes care of expanding curly braces
3281 name = var;
3282 len = get_name_len(&var, &tofree, TRUE, FALSE);
3283 if (len > 0)
3284 {
3285 if (tofree != NULL)
3286 name = tofree;
3287 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3288 if (n)
3289 {
3290 // handle d.key, l[idx], f(expr)
3291 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
3292 if (n)
3293 clear_tv(&tv);
3294 }
3295 }
3296 if (*var != NUL)
3297 n = FALSE;
3298
3299 vim_free(tofree);
3300 return n;
3301}
3302
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003303static lval_T *redir_lval = NULL;
3304#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3305static garray_T redir_ga; // only valid when redir_lval is not NULL
3306static char_u *redir_endp = NULL;
3307static char_u *redir_varname = NULL;
3308
3309/*
3310 * Start recording command output to a variable
3311 * When "append" is TRUE append to an existing variable.
3312 * Returns OK if successfully completed the setup. FAIL otherwise.
3313 */
3314 int
3315var_redir_start(char_u *name, int append)
3316{
3317 int save_emsg;
3318 int err;
3319 typval_T tv;
3320
3321 // Catch a bad name early.
3322 if (!eval_isnamec1(*name))
3323 {
3324 emsg(_(e_invarg));
3325 return FAIL;
3326 }
3327
3328 // Make a copy of the name, it is used in redir_lval until redir ends.
3329 redir_varname = vim_strsave(name);
3330 if (redir_varname == NULL)
3331 return FAIL;
3332
3333 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3334 if (redir_lval == NULL)
3335 {
3336 var_redir_stop();
3337 return FAIL;
3338 }
3339
3340 // The output is stored in growarray "redir_ga" until redirection ends.
3341 ga_init2(&redir_ga, (int)sizeof(char), 500);
3342
3343 // Parse the variable name (can be a dict or list entry).
3344 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3345 FNE_CHECK_START);
3346 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3347 {
3348 clear_lval(redir_lval);
3349 if (redir_endp != NULL && *redir_endp != NUL)
3350 // Trailing characters are present after the variable name
3351 emsg(_(e_trailing));
3352 else
3353 emsg(_(e_invarg));
3354 redir_endp = NULL; // don't store a value, only cleanup
3355 var_redir_stop();
3356 return FAIL;
3357 }
3358
3359 // check if we can write to the variable: set it to or append an empty
3360 // string
3361 save_emsg = did_emsg;
3362 did_emsg = FALSE;
3363 tv.v_type = VAR_STRING;
3364 tv.vval.v_string = (char_u *)"";
3365 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003367 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003369 clear_lval(redir_lval);
3370 err = did_emsg;
3371 did_emsg |= save_emsg;
3372 if (err)
3373 {
3374 redir_endp = NULL; // don't store a value, only cleanup
3375 var_redir_stop();
3376 return FAIL;
3377 }
3378
3379 return OK;
3380}
3381
3382/*
3383 * Append "value[value_len]" to the variable set by var_redir_start().
3384 * The actual appending is postponed until redirection ends, because the value
3385 * appended may in fact be the string we write to, changing it may cause freed
3386 * memory to be used:
3387 * :redir => foo
3388 * :let foo
3389 * :redir END
3390 */
3391 void
3392var_redir_str(char_u *value, int value_len)
3393{
3394 int len;
3395
3396 if (redir_lval == NULL)
3397 return;
3398
3399 if (value_len == -1)
3400 len = (int)STRLEN(value); // Append the entire string
3401 else
3402 len = value_len; // Append only "value_len" characters
3403
3404 if (ga_grow(&redir_ga, len) == OK)
3405 {
3406 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3407 redir_ga.ga_len += len;
3408 }
3409 else
3410 var_redir_stop();
3411}
3412
3413/*
3414 * Stop redirecting command output to a variable.
3415 * Frees the allocated memory.
3416 */
3417 void
3418var_redir_stop(void)
3419{
3420 typval_T tv;
3421
3422 if (EVALCMD_BUSY)
3423 {
3424 redir_lval = NULL;
3425 return;
3426 }
3427
3428 if (redir_lval != NULL)
3429 {
3430 // If there was no error: assign the text to the variable.
3431 if (redir_endp != NULL)
3432 {
3433 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3434 tv.v_type = VAR_STRING;
3435 tv.vval.v_string = redir_ga.ga_data;
3436 // Call get_lval() again, if it's inside a Dict or List it may
3437 // have changed.
3438 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3439 FALSE, FALSE, 0, FNE_CHECK_START);
3440 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003442 (char_u *)".");
3443 clear_lval(redir_lval);
3444 }
3445
3446 // free the collected output
3447 VIM_CLEAR(redir_ga.ga_data);
3448
3449 VIM_CLEAR(redir_lval);
3450 }
3451 VIM_CLEAR(redir_varname);
3452}
3453
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003454/*
3455 * "gettabvar()" function
3456 */
3457 void
3458f_gettabvar(typval_T *argvars, typval_T *rettv)
3459{
3460 win_T *oldcurwin;
3461 tabpage_T *tp, *oldtabpage;
3462 dictitem_T *v;
3463 char_u *varname;
3464 int done = FALSE;
3465
3466 rettv->v_type = VAR_STRING;
3467 rettv->vval.v_string = NULL;
3468
3469 varname = tv_get_string_chk(&argvars[1]);
3470 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3471 if (tp != NULL && varname != NULL)
3472 {
3473 // Set tp to be our tabpage, temporarily. Also set the window to the
3474 // first window in the tabpage, otherwise the window is not valid.
3475 if (switch_win(&oldcurwin, &oldtabpage,
3476 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3477 : tp->tp_firstwin, tp, TRUE) == OK)
3478 {
3479 // look up the variable
3480 // Let gettabvar({nr}, "") return the "t:" dictionary.
3481 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3482 if (v != NULL)
3483 {
3484 copy_tv(&v->di_tv, rettv);
3485 done = TRUE;
3486 }
3487 }
3488
3489 // restore previous notion of curwin
3490 restore_win(oldcurwin, oldtabpage, TRUE);
3491 }
3492
3493 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3494 copy_tv(&argvars[2], rettv);
3495}
3496
3497/*
3498 * "gettabwinvar()" function
3499 */
3500 void
3501f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3502{
3503 getwinvar(argvars, rettv, 1);
3504}
3505
3506/*
3507 * "getwinvar()" function
3508 */
3509 void
3510f_getwinvar(typval_T *argvars, typval_T *rettv)
3511{
3512 getwinvar(argvars, rettv, 0);
3513}
3514
3515/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003516 * "getbufvar()" function
3517 */
3518 void
3519f_getbufvar(typval_T *argvars, typval_T *rettv)
3520{
3521 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003522 char_u *varname;
3523 dictitem_T *v;
3524 int done = FALSE;
3525
3526 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3527 varname = tv_get_string_chk(&argvars[1]);
3528 ++emsg_off;
3529 buf = tv_get_buf(&argvars[0], FALSE);
3530
3531 rettv->v_type = VAR_STRING;
3532 rettv->vval.v_string = NULL;
3533
3534 if (buf != NULL && varname != NULL)
3535 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003536 if (*varname == '&')
3537 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003538 buf_T *save_curbuf = curbuf;
3539
3540 // set curbuf to be our buf, temporarily
3541 curbuf = buf;
3542
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003543 if (varname[1] == NUL)
3544 {
3545 // get all buffer-local options in a dict
3546 dict_T *opts = get_winbuf_options(TRUE);
3547
3548 if (opts != NULL)
3549 {
3550 rettv_dict_set(rettv, opts);
3551 done = TRUE;
3552 }
3553 }
3554 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3555 // buffer-local-option
3556 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003557
3558 // restore previous notion of curbuf
3559 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003560 }
3561 else
3562 {
3563 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003564 if (*varname == NUL)
3565 // Let getbufvar({nr}, "") return the "b:" dictionary.
3566 v = &buf->b_bufvar;
3567 else
3568 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3569 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003570 if (v != NULL)
3571 {
3572 copy_tv(&v->di_tv, rettv);
3573 done = TRUE;
3574 }
3575 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003576 }
3577
3578 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3579 // use the default value
3580 copy_tv(&argvars[2], rettv);
3581
3582 --emsg_off;
3583}
3584
3585/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003586 * "settabvar()" function
3587 */
3588 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003589f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003590{
3591 tabpage_T *save_curtab;
3592 tabpage_T *tp;
3593 char_u *varname, *tabvarname;
3594 typval_T *varp;
3595
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003596 if (check_secure())
3597 return;
3598
3599 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3600 varname = tv_get_string_chk(&argvars[1]);
3601 varp = &argvars[2];
3602
3603 if (varname != NULL && varp != NULL && tp != NULL)
3604 {
3605 save_curtab = curtab;
3606 goto_tabpage_tp(tp, FALSE, FALSE);
3607
3608 tabvarname = alloc(STRLEN(varname) + 3);
3609 if (tabvarname != NULL)
3610 {
3611 STRCPY(tabvarname, "t:");
3612 STRCPY(tabvarname + 2, varname);
3613 set_var(tabvarname, varp, TRUE);
3614 vim_free(tabvarname);
3615 }
3616
3617 // Restore current tabpage
3618 if (valid_tabpage(save_curtab))
3619 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3620 }
3621}
3622
3623/*
3624 * "settabwinvar()" function
3625 */
3626 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003627f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003628{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003629 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003630}
3631
3632/*
3633 * "setwinvar()" function
3634 */
3635 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003636f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003637{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003638 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003639}
3640
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003641/*
3642 * "setbufvar()" function
3643 */
3644 void
3645f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3646{
3647 buf_T *buf;
3648 char_u *varname, *bufvarname;
3649 typval_T *varp;
3650 char_u nbuf[NUMBUFLEN];
3651
3652 if (check_secure())
3653 return;
3654 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3655 varname = tv_get_string_chk(&argvars[1]);
3656 buf = tv_get_buf(&argvars[0], FALSE);
3657 varp = &argvars[2];
3658
3659 if (buf != NULL && varname != NULL && varp != NULL)
3660 {
3661 if (*varname == '&')
3662 {
3663 long numval;
3664 char_u *strval;
3665 int error = FALSE;
3666 aco_save_T aco;
3667
3668 // set curbuf to be our buf, temporarily
3669 aucmd_prepbuf(&aco, buf);
3670
3671 ++varname;
3672 numval = (long)tv_get_number_chk(varp, &error);
3673 strval = tv_get_string_buf_chk(varp, nbuf);
3674 if (!error && strval != NULL)
3675 set_option_value(varname, numval, strval, OPT_LOCAL);
3676
3677 // reset notion of buffer
3678 aucmd_restbuf(&aco);
3679 }
3680 else
3681 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003682 bufvarname = alloc(STRLEN(varname) + 3);
3683 if (bufvarname != NULL)
3684 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003685 buf_T *save_curbuf = curbuf;
3686
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003687 curbuf = buf;
3688 STRCPY(bufvarname, "b:");
3689 STRCPY(bufvarname + 2, varname);
3690 set_var(bufvarname, varp, TRUE);
3691 vim_free(bufvarname);
3692 curbuf = save_curbuf;
3693 }
3694 }
3695 }
3696}
3697
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003698/*
3699 * Get a callback from "arg". It can be a Funcref or a function name.
3700 * When "arg" is zero return an empty string.
3701 * "cb_name" is not allocated.
3702 * "cb_name" is set to NULL for an invalid argument.
3703 */
3704 callback_T
3705get_callback(typval_T *arg)
3706{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003707 callback_T res;
3708 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003709
3710 res.cb_free_name = FALSE;
3711 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3712 {
3713 res.cb_partial = arg->vval.v_partial;
3714 ++res.cb_partial->pt_refcount;
3715 res.cb_name = partial_name(res.cb_partial);
3716 }
3717 else
3718 {
3719 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003720 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3721 && isdigit(*arg->vval.v_string))
3722 r = FAIL;
3723 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003724 {
3725 // Note that we don't make a copy of the string.
3726 res.cb_name = arg->vval.v_string;
3727 func_ref(res.cb_name);
3728 }
3729 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003730 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003731 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003732 r = FAIL;
3733
3734 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003735 {
3736 emsg(_("E921: Invalid callback argument"));
3737 res.cb_name = NULL;
3738 }
3739 }
3740 return res;
3741}
3742
3743/*
3744 * Copy a callback into a typval_T.
3745 */
3746 void
3747put_callback(callback_T *cb, typval_T *tv)
3748{
3749 if (cb->cb_partial != NULL)
3750 {
3751 tv->v_type = VAR_PARTIAL;
3752 tv->vval.v_partial = cb->cb_partial;
3753 ++tv->vval.v_partial->pt_refcount;
3754 }
3755 else
3756 {
3757 tv->v_type = VAR_FUNC;
3758 tv->vval.v_string = vim_strsave(cb->cb_name);
3759 func_ref(cb->cb_name);
3760 }
3761}
3762
3763/*
3764 * Make a copy of "src" into "dest", allocating the function name if needed,
3765 * without incrementing the refcount.
3766 */
3767 void
3768set_callback(callback_T *dest, callback_T *src)
3769{
3770 if (src->cb_partial == NULL)
3771 {
3772 // just a function name, make a copy
3773 dest->cb_name = vim_strsave(src->cb_name);
3774 dest->cb_free_name = TRUE;
3775 }
3776 else
3777 {
3778 // cb_name is a pointer into cb_partial
3779 dest->cb_name = src->cb_name;
3780 dest->cb_free_name = FALSE;
3781 }
3782 dest->cb_partial = src->cb_partial;
3783}
3784
3785/*
3786 * Unref/free "callback" returned by get_callback() or set_callback().
3787 */
3788 void
3789free_callback(callback_T *callback)
3790{
3791 if (callback->cb_partial != NULL)
3792 {
3793 partial_unref(callback->cb_partial);
3794 callback->cb_partial = NULL;
3795 }
3796 else if (callback->cb_name != NULL)
3797 func_unref(callback->cb_name);
3798 if (callback->cb_free_name)
3799 {
3800 vim_free(callback->cb_name);
3801 callback->cb_free_name = FALSE;
3802 }
3803 callback->cb_name = NULL;
3804}
3805
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003806#endif // FEAT_EVAL