blob: 087a0cbdb98ac4e5c568e715cb218461dc77c128 [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
48 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
49} vimvars[VV_LEN] =
50{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020051 // The order here must match the VV_ defines in vim.h!
52 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaare5cdf152019-08-29 22:09:46 +020053 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
54 {VV_NAME("count1", VAR_NUMBER), VV_RO},
55 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
56 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
57 {VV_NAME("warningmsg", VAR_STRING), 0},
58 {VV_NAME("statusmsg", VAR_STRING), 0},
59 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
60 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
61 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
62 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
63 {VV_NAME("termresponse", VAR_STRING), VV_RO},
64 {VV_NAME("fname", VAR_STRING), VV_RO},
65 {VV_NAME("lang", VAR_STRING), VV_RO},
66 {VV_NAME("lc_time", VAR_STRING), VV_RO},
67 {VV_NAME("ctype", VAR_STRING), VV_RO},
68 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
69 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
70 {VV_NAME("fname_in", VAR_STRING), VV_RO},
71 {VV_NAME("fname_out", VAR_STRING), VV_RO},
72 {VV_NAME("fname_new", VAR_STRING), VV_RO},
73 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
74 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
75 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
76 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
77 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
78 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
79 {VV_NAME("progname", VAR_STRING), VV_RO},
80 {VV_NAME("servername", VAR_STRING), VV_RO},
81 {VV_NAME("dying", VAR_NUMBER), VV_RO},
82 {VV_NAME("exception", VAR_STRING), VV_RO},
83 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
84 {VV_NAME("register", VAR_STRING), VV_RO},
85 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
86 {VV_NAME("insertmode", VAR_STRING), VV_RO},
87 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
88 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
89 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
90 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
91 {VV_NAME("fcs_choice", VAR_STRING), 0},
92 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
93 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
94 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
95 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
96 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
97 {VV_NAME("beval_text", VAR_STRING), VV_RO},
98 {VV_NAME("scrollstart", VAR_STRING), 0},
99 {VV_NAME("swapname", VAR_STRING), VV_RO},
100 {VV_NAME("swapchoice", VAR_STRING), 0},
101 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
102 {VV_NAME("char", VAR_STRING), 0},
103 {VV_NAME("mouse_win", VAR_NUMBER), 0},
104 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
105 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
106 {VV_NAME("mouse_col", VAR_NUMBER), 0},
107 {VV_NAME("operator", VAR_STRING), VV_RO},
108 {VV_NAME("searchforward", VAR_NUMBER), 0},
109 {VV_NAME("hlsearch", VAR_NUMBER), 0},
110 {VV_NAME("oldfiles", VAR_LIST), 0},
111 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
112 {VV_NAME("progpath", VAR_STRING), VV_RO},
113 {VV_NAME("completed_item", VAR_DICT), VV_RO},
114 {VV_NAME("option_new", VAR_STRING), VV_RO},
115 {VV_NAME("option_old", VAR_STRING), VV_RO},
116 {VV_NAME("option_oldlocal", VAR_STRING), VV_RO},
117 {VV_NAME("option_oldglobal", VAR_STRING), VV_RO},
118 {VV_NAME("option_command", VAR_STRING), VV_RO},
119 {VV_NAME("option_type", VAR_STRING), VV_RO},
120 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100121 {VV_NAME("false", VAR_BOOL), VV_RO},
122 {VV_NAME("true", VAR_BOOL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200123 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100124 {VV_NAME("null", VAR_SPECIAL), VV_RO},
125 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200126 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
127 {VV_NAME("testing", VAR_NUMBER), 0},
128 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
139 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
141 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
142 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
143 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
144 {VV_NAME("event", VAR_DICT), VV_RO},
145 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
146 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100147 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200148 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200149};
150
151// shorthand
152#define vv_type vv_di.di_tv.v_type
153#define vv_nr vv_di.di_tv.vval.v_number
154#define vv_float vv_di.di_tv.vval.v_float
155#define vv_str vv_di.di_tv.vval.v_string
156#define vv_list vv_di.di_tv.vval.v_list
157#define vv_dict vv_di.di_tv.vval.v_dict
158#define vv_blob vv_di.di_tv.vval.v_blob
159#define vv_tv vv_di.di_tv
160
161static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200162static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200163#define vimvarht vimvardict.dv_hashtab
164
165// for VIM_VERSION_ defines
166#include "version.h"
167
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 Moolenaarda6c0332019-09-01 16:01:30 +0200176static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_one_var(dictitem_T *v, char *prefix, int *first);
178static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
179
180/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200181 * Initialize global and vim special variables
182 */
183 void
184evalvars_init(void)
185{
186 int i;
187 struct vimvar *p;
188
189 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
190 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
191 vimvardict.dv_lock = VAR_FIXED;
192 hash_init(&compat_hashtab);
193
194 for (i = 0; i < VV_LEN; ++i)
195 {
196 p = &vimvars[i];
197 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
198 {
199 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
200 getout(1);
201 }
202 STRCPY(p->vv_di.di_key, p->vv_name);
203 if (p->vv_flags & VV_RO)
204 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
205 else if (p->vv_flags & VV_RO_SBX)
206 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
207 else
208 p->vv_di.di_flags = DI_FLAGS_FIX;
209
210 // add to v: scope dict, unless the value is not always available
211 if (p->vv_type != VAR_UNKNOWN)
212 hash_add(&vimvarht, p->vv_di.di_key);
213 if (p->vv_flags & VV_COMPAT)
214 // add to compat scope dict
215 hash_add(&compat_hashtab, p->vv_di.di_key);
216 }
217 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
218 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
219
220 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
221 set_vim_var_nr(VV_HLSEARCH, 1L);
222 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
223 set_vim_var_list(VV_ERRORS, list_alloc());
224 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
225
226 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
227 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
228 set_vim_var_nr(VV_NONE, VVAL_NONE);
229 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100230 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231
232 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
233 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
234 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
235 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
236 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
237 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
238 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
239 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
240 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
241 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
242 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
243
244 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
245
Bram Moolenaar439c0362020-06-06 15:58:03 +0200246 // Default for v:register is not 0 but '"'. This is adjusted once the
247 // clipboard has been setup by calling reset_reg_var().
248 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200249}
250
251#if defined(EXITFREE) || defined(PROTO)
252/*
253 * Free all vim variables information on exit
254 */
255 void
256evalvars_clear(void)
257{
258 int i;
259 struct vimvar *p;
260
261 for (i = 0; i < VV_LEN; ++i)
262 {
263 p = &vimvars[i];
264 if (p->vv_di.di_tv.v_type == VAR_STRING)
265 VIM_CLEAR(p->vv_str);
266 else if (p->vv_di.di_tv.v_type == VAR_LIST)
267 {
268 list_unref(p->vv_list);
269 p->vv_list = NULL;
270 }
271 }
272 hash_clear(&vimvarht);
273 hash_init(&vimvarht); // garbage_collect() will access it
274 hash_clear(&compat_hashtab);
275
276 // global variables
277 vars_clear(&globvarht);
278
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100279 // Script-local variables. Clear all the variables here.
280 // The scriptvar_T is cleared later in free_scriptnames(), because a
281 // variable in one script might hold a reference to the whole scope of
282 // another script.
283 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200284 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285}
286#endif
287
288 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200289garbage_collect_globvars(int copyID)
290{
291 return set_ref_in_ht(&globvarht, copyID, NULL);
292}
293
294 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200295garbage_collect_vimvars(int copyID)
296{
297 return set_ref_in_ht(&vimvarht, copyID, NULL);
298}
299
300 int
301garbage_collect_scriptvars(int copyID)
302{
303 int i;
304 int abort = FALSE;
305
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100306 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200307 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
308
309 return abort;
310}
311
312/*
313 * Set an internal variable to a string value. Creates the variable if it does
314 * not already exist.
315 */
316 void
317set_internal_string_var(char_u *name, char_u *value)
318{
319 char_u *val;
320 typval_T *tvp;
321
322 val = vim_strsave(value);
323 if (val != NULL)
324 {
325 tvp = alloc_string_tv(val);
326 if (tvp != NULL)
327 {
328 set_var(name, tvp, FALSE);
329 free_tv(tvp);
330 }
331 }
332}
333
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200334 int
335eval_charconvert(
336 char_u *enc_from,
337 char_u *enc_to,
338 char_u *fname_from,
339 char_u *fname_to)
340{
341 int err = FALSE;
342
343 set_vim_var_string(VV_CC_FROM, enc_from, -1);
344 set_vim_var_string(VV_CC_TO, enc_to, -1);
345 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
346 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
347 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
348 err = TRUE;
349 set_vim_var_string(VV_CC_FROM, NULL, -1);
350 set_vim_var_string(VV_CC_TO, NULL, -1);
351 set_vim_var_string(VV_FNAME_IN, NULL, -1);
352 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
353
354 if (err)
355 return FAIL;
356 return OK;
357}
358
359# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
360 int
361eval_printexpr(char_u *fname, char_u *args)
362{
363 int err = FALSE;
364
365 set_vim_var_string(VV_FNAME_IN, fname, -1);
366 set_vim_var_string(VV_CMDARG, args, -1);
367 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
368 err = TRUE;
369 set_vim_var_string(VV_FNAME_IN, NULL, -1);
370 set_vim_var_string(VV_CMDARG, NULL, -1);
371
372 if (err)
373 {
374 mch_remove(fname);
375 return FAIL;
376 }
377 return OK;
378}
379# endif
380
381# if defined(FEAT_DIFF) || defined(PROTO)
382 void
383eval_diff(
384 char_u *origfile,
385 char_u *newfile,
386 char_u *outfile)
387{
388 int err = FALSE;
389
390 set_vim_var_string(VV_FNAME_IN, origfile, -1);
391 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
392 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
393 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
394 set_vim_var_string(VV_FNAME_IN, NULL, -1);
395 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
396 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
397}
398
399 void
400eval_patch(
401 char_u *origfile,
402 char_u *difffile,
403 char_u *outfile)
404{
405 int err;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415# endif
416
417#if defined(FEAT_SPELL) || defined(PROTO)
418/*
419 * Evaluate an expression to a list with suggestions.
420 * For the "expr:" part of 'spellsuggest'.
421 * Returns NULL when there is an error.
422 */
423 list_T *
424eval_spell_expr(char_u *badword, char_u *expr)
425{
426 typval_T save_val;
427 typval_T rettv;
428 list_T *list = NULL;
429 char_u *p = skipwhite(expr);
430
431 // Set "v:val" to the bad word.
432 prepare_vimvar(VV_VAL, &save_val);
433 set_vim_var_string(VV_VAL, badword, -1);
434 if (p_verbose == 0)
435 ++emsg_off;
436
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200437 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438 {
439 if (rettv.v_type != VAR_LIST)
440 clear_tv(&rettv);
441 else
442 list = rettv.vval.v_list;
443 }
444
445 if (p_verbose == 0)
446 --emsg_off;
447 clear_tv(get_vim_var_tv(VV_VAL));
448 restore_vimvar(VV_VAL, &save_val);
449
450 return list;
451}
452
453/*
454 * "list" is supposed to contain two items: a word and a number. Return the
455 * word in "pp" and the number as the return value.
456 * Return -1 if anything isn't right.
457 * Used to get the good word and score from the eval_spell_expr() result.
458 */
459 int
460get_spellword(list_T *list, char_u **pp)
461{
462 listitem_T *li;
463
464 li = list->lv_first;
465 if (li == NULL)
466 return -1;
467 *pp = tv_get_string(&li->li_tv);
468
469 li = li->li_next;
470 if (li == NULL)
471 return -1;
472 return (int)tv_get_number(&li->li_tv);
473}
474#endif
475
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200476/*
477 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200478 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200479 * When not used yet add the variable to the v: hashtable.
480 */
481 void
482prepare_vimvar(int idx, typval_T *save_tv)
483{
484 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200485 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200486 if (vimvars[idx].vv_type == VAR_UNKNOWN)
487 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
488}
489
490/*
491 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200492 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493 * When no longer defined, remove the variable from the v: hashtable.
494 */
495 void
496restore_vimvar(int idx, typval_T *save_tv)
497{
498 hashitem_T *hi;
499
500 vimvars[idx].vv_tv = *save_tv;
501 if (vimvars[idx].vv_type == VAR_UNKNOWN)
502 {
503 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
504 if (HASHITEM_EMPTY(hi))
505 internal_error("restore_vimvar()");
506 else
507 hash_remove(&vimvarht, hi);
508 }
509}
510
511/*
512 * List Vim variables.
513 */
514 static void
515list_vim_vars(int *first)
516{
517 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
518}
519
520/*
521 * List script-local variables, if there is a script.
522 */
523 static void
524list_script_vars(int *first)
525{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200526 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200527 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
528 "s:", FALSE, first);
529}
530
531/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200532 * Get a list of lines from a HERE document. The here document is a list of
533 * lines surrounded by a marker.
534 * cmd << {marker}
535 * {line1}
536 * {line2}
537 * ....
538 * {marker}
539 *
540 * The {marker} is a string. If the optional 'trim' word is supplied before the
541 * marker, then the leading indentation before the lines (matching the
542 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200543 *
544 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
545 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
546 * missing, then '.' is accepted as a marker.
547 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200548 * Returns a List with {lines} or NULL.
549 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200551heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200552{
553 char_u *theline;
554 char_u *marker;
555 list_T *l;
556 char_u *p;
557 int marker_indent_len = 0;
558 int text_indent_len = 0;
559 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200560 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200561
562 if (eap->getline == NULL)
563 {
564 emsg(_("E991: cannot use =<< here"));
565 return NULL;
566 }
567
568 // Check for the optional 'trim' word before the marker
569 cmd = skipwhite(cmd);
570 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
571 {
572 cmd = skipwhite(cmd + 4);
573
574 // Trim the indentation from all the lines in the here document.
575 // The amount of indentation trimmed is the same as the indentation of
576 // the first line after the :let command line. To find the end marker
577 // the indent of the :let command line is trimmed.
578 p = *eap->cmdlinep;
579 while (VIM_ISWHITE(*p))
580 {
581 p++;
582 marker_indent_len++;
583 }
584 text_indent_len = -1;
585 }
586
587 // The marker is the next word.
588 if (*cmd != NUL && *cmd != '"')
589 {
590 marker = skipwhite(cmd);
591 p = skiptowhite(marker);
592 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
593 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200594 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200595 return NULL;
596 }
597 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200598 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200599 {
600 emsg(_("E221: Marker cannot start with lower case letter"));
601 return NULL;
602 }
603 }
604 else
605 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200606 // When getting lines for an embedded script, if the marker is missing,
607 // accept '.' as the marker.
608 if (script_get)
609 marker = dot;
610 else
611 {
612 emsg(_("E172: Missing marker"));
613 return NULL;
614 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200615 }
616
617 l = list_alloc();
618 if (l == NULL)
619 return NULL;
620
621 for (;;)
622 {
623 int mi = 0;
624 int ti = 0;
625
626 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
627 if (theline == NULL)
628 {
629 semsg(_("E990: Missing end marker '%s'"), marker);
630 break;
631 }
632
633 // with "trim": skip the indent matching the :let line to find the
634 // marker
635 if (marker_indent_len > 0
636 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
637 mi = marker_indent_len;
638 if (STRCMP(marker, theline + mi) == 0)
639 {
640 vim_free(theline);
641 break;
642 }
643
644 if (text_indent_len == -1 && *theline != NUL)
645 {
646 // set the text indent from the first line.
647 p = theline;
648 text_indent_len = 0;
649 while (VIM_ISWHITE(*p))
650 {
651 p++;
652 text_indent_len++;
653 }
654 text_indent = vim_strnsave(theline, text_indent_len);
655 }
656 // with "trim": skip the indent matching the first line
657 if (text_indent != NULL)
658 for (ti = 0; ti < text_indent_len; ++ti)
659 if (theline[ti] != text_indent[ti])
660 break;
661
662 if (list_append_string(l, theline + ti, -1) == FAIL)
663 break;
664 vim_free(theline);
665 }
666 vim_free(text_indent);
667
668 return l;
669}
670
671/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200672 * Vim9 variable declaration:
673 * ":var name"
674 * ":var name: type"
675 * ":var name = expr"
676 * ":var name: type = expr"
677 * etc.
678 */
679 void
680ex_var(exarg_T *eap)
681{
682 if (!in_vim9script())
683 {
684 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
685 return;
686 }
687 ex_let(eap);
688}
689
690/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200691 * ":let" list all variable values
692 * ":let var1 var2" list variable values
693 * ":let var = expr" assignment command.
694 * ":let var += expr" assignment command.
695 * ":let var -= expr" assignment command.
696 * ":let var *= expr" assignment command.
697 * ":let var /= expr" assignment command.
698 * ":let var %= expr" assignment command.
699 * ":let var .= expr" assignment command.
700 * ":let var ..= expr" assignment command.
701 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100703 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200704 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200705 * ":final var = expr" assignment command.
706 * ":final [var1, var2] = expr" unpack list.
707 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200708 * ":const" list all variable values
709 * ":const var1 var2" list variable values
710 * ":const var = expr" assignment command.
711 * ":const [var1, var2] = expr" unpack list.
712 */
713 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200714ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200715{
716 char_u *arg = eap->arg;
717 char_u *expr = NULL;
718 typval_T rettv;
719 int i;
720 int var_count = 0;
721 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200722 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200723 char_u *argend;
724 int first = TRUE;
725 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200726 int has_assign;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200727 int flags = eap->cmdidx == CMD_const ? ASSIGN_CONST : 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200728 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200729
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200730 if (eap->cmdidx == CMD_final && !vim9script)
731 {
732 // In legacy Vim script ":final" is short for ":finally".
733 ex_finally(eap);
734 return;
735 }
736 if (eap->cmdidx == CMD_const && !vim9script && !eap->forceit)
737 // In legacy Vim script ":const" works like ":final".
738 eap->cmdidx = CMD_final;
739
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100740 // detect Vim9 assignment without ":let" or ":const"
741 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200742 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200744 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200745 if (argend == NULL)
746 return;
747 if (argend > arg && argend[-1] == '.') // for var.='str'
748 --argend;
749 expr = skipwhite(argend);
750 concat = expr[0] == '.'
751 && ((expr[1] == '=' && current_sctx.sc_version < 2)
752 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200753 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
754 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200755 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200756 {
757 // ":let" without "=": list variables
758 if (*arg == '[')
759 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200760 else if (expr[0] == '.' && expr[1] == '=')
761 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200762 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200763 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200764 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200765 {
766 // Vim9 declaration ":let var: type"
767 arg = vim9_declare_scriptvar(eap, arg);
768 }
769 else
770 {
771 // ":let var1 var2" - list values
772 arg = list_arg_vars(eap, arg, &first);
773 }
774 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200775 else if (!eap->skip)
776 {
777 // ":let"
778 list_glob_vars(&first);
779 list_buf_vars(&first);
780 list_win_vars(&first);
781 list_tab_vars(&first);
782 list_script_vars(&first);
783 list_func_vars(&first);
784 list_vim_vars(&first);
785 }
786 eap->nextcmd = check_nextcmd(arg);
787 }
788 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
789 {
790 list_T *l;
791
792 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200793 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 if (l != NULL)
795 {
796 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200797 if (!eap->skip)
798 {
799 op[0] = '=';
800 op[1] = NUL;
801 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200803 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200804 clear_tv(&rettv);
805 }
806 }
807 else
808 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200809 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200810 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200811
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200812 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200813 i = FAIL;
814 if (has_assign || concat)
815 {
816 op[0] = '=';
817 op[1] = NUL;
818 if (*expr != '=')
819 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200820 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200821 {
822 // +=, /=, etc. require an existing variable
823 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
824 i = FAIL;
825 }
826 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200827 {
828 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200829 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200830 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200831 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200832 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200833 ++len;
834 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200835 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200836 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200837 }
838 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200839 ++expr;
840
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200841 if (vim9script && (!VIM_ISWHITE(*argend)
842 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200843 {
844 vim_strncpy(op, expr - len, len);
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200845 semsg(_(e_white_space_required_before_and_after_str), op);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200846 i = FAIL;
847 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200848
849 if (eap->skip)
850 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200851 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200852 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200853 if (getline_equal(eap->getline, eap->cookie, getsourceline))
854 {
855 evalarg.eval_getline = eap->getline;
856 evalarg.eval_cookie = eap->cookie;
857 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200858 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200859 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200860 if (eap->skip)
861 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200862 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200863 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200864 if (eap->skip)
865 {
866 if (i != FAIL)
867 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200868 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200869 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200870 {
871 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200872 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200873 clear_tv(&rettv);
874 }
875 }
876}
877
878/*
879 * Assign the typevalue "tv" to the variable or variables at "arg_start".
880 * Handles both "var" with any type and "[var, var; var]" with a list type.
881 * When "op" is not NULL it points to a string with characters that
882 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
883 * or concatenate.
884 * Returns OK or FAIL;
885 */
886 int
887ex_let_vars(
888 char_u *arg_start,
889 typval_T *tv,
890 int copy, // copy values from "tv", don't move
891 int semicolon, // from skip_var_list()
892 int var_count, // from skip_var_list()
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200893 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200894 char_u *op)
895{
896 char_u *arg = arg_start;
897 list_T *l;
898 int i;
899 listitem_T *item;
900 typval_T ltv;
901
902 if (*arg != '[')
903 {
904 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200906 return FAIL;
907 return OK;
908 }
909
910 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
911 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
912 {
913 emsg(_(e_listreq));
914 return FAIL;
915 }
916
917 i = list_len(l);
918 if (semicolon == 0 && var_count < i)
919 {
920 emsg(_("E687: Less targets than List items"));
921 return FAIL;
922 }
923 if (var_count - semicolon > i)
924 {
925 emsg(_("E688: More targets than List items"));
926 return FAIL;
927 }
928
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200929 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200930 item = l->lv_first;
931 while (*arg != ']')
932 {
933 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200935 item = item->li_next;
936 if (arg == NULL)
937 return FAIL;
938
939 arg = skipwhite(arg);
940 if (*arg == ';')
941 {
942 // Put the rest of the list (may be empty) in the var after ';'.
943 // Create a new list for this.
944 l = list_alloc();
945 if (l == NULL)
946 return FAIL;
947 while (item != NULL)
948 {
949 list_append_tv(l, &item->li_tv);
950 item = item->li_next;
951 }
952
953 ltv.v_type = VAR_LIST;
954 ltv.v_lock = 0;
955 ltv.vval.v_list = l;
956 l->lv_refcount = 1;
957
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200959 (char_u *)"]", op);
960 clear_tv(&ltv);
961 if (arg == NULL)
962 return FAIL;
963 break;
964 }
965 else if (*arg != ',' && *arg != ']')
966 {
967 internal_error("ex_let_vars()");
968 return FAIL;
969 }
970 }
971
972 return OK;
973}
974
975/*
976 * Skip over assignable variable "var" or list of variables "[var, var]".
977 * Used for ":let varvar = expr" and ":for varvar in expr".
978 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200979 * for "[var, var; var]" set "semicolon" to 1.
980 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200981 * Return NULL for an error.
982 */
983 char_u *
984skip_var_list(
985 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200987 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200988 int *semicolon,
989 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200990{
991 char_u *p, *s;
992
993 if (*arg == '[')
994 {
995 // "[var, var]": find the matching ']'.
996 p = arg;
997 for (;;)
998 {
999 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001000 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001001 if (s == p)
1002 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001003 if (!silent)
1004 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005 return NULL;
1006 }
1007 ++*var_count;
1008
1009 p = skipwhite(s);
1010 if (*p == ']')
1011 break;
1012 else if (*p == ';')
1013 {
1014 if (*semicolon == 1)
1015 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001016 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001017 return NULL;
1018 }
1019 *semicolon = 1;
1020 }
1021 else if (*p != ',')
1022 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001023 if (!silent)
1024 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001025 return NULL;
1026 }
1027 }
1028 return p + 1;
1029 }
1030 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001032}
1033
1034/*
1035 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1036 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001038 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001039 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001041{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 char_u *end;
1043
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001044 if (*arg == '@' && arg[1] != NUL)
1045 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001048 if (include_type && in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001050 // "a: type" is declaring variable "a" with a type, not "a:".
1051 if (end == arg + 2 && end[-1] == ':')
1052 --end;
1053 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001054 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 }
1056 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001057}
1058
1059/*
1060 * List variables for hashtab "ht" with prefix "prefix".
1061 * If "empty" is TRUE also list NULL strings as empty strings.
1062 */
1063 void
1064list_hashtable_vars(
1065 hashtab_T *ht,
1066 char *prefix,
1067 int empty,
1068 int *first)
1069{
1070 hashitem_T *hi;
1071 dictitem_T *di;
1072 int todo;
1073 char_u buf[IOSIZE];
1074
1075 todo = (int)ht->ht_used;
1076 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1077 {
1078 if (!HASHITEM_EMPTY(hi))
1079 {
1080 --todo;
1081 di = HI2DI(hi);
1082
1083 // apply :filter /pat/ to variable name
1084 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1085 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1086 if (message_filtered(buf))
1087 continue;
1088
1089 if (empty || di->di_tv.v_type != VAR_STRING
1090 || di->di_tv.vval.v_string != NULL)
1091 list_one_var(di, prefix, first);
1092 }
1093 }
1094}
1095
1096/*
1097 * List global variables.
1098 */
1099 static void
1100list_glob_vars(int *first)
1101{
1102 list_hashtable_vars(&globvarht, "", TRUE, first);
1103}
1104
1105/*
1106 * List buffer variables.
1107 */
1108 static void
1109list_buf_vars(int *first)
1110{
1111 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1112}
1113
1114/*
1115 * List window variables.
1116 */
1117 static void
1118list_win_vars(int *first)
1119{
1120 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1121}
1122
1123/*
1124 * List tab page variables.
1125 */
1126 static void
1127list_tab_vars(int *first)
1128{
1129 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1130}
1131
1132/*
1133 * List variables in "arg".
1134 */
1135 static char_u *
1136list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1137{
1138 int error = FALSE;
1139 int len;
1140 char_u *name;
1141 char_u *name_start;
1142 char_u *arg_subsc;
1143 char_u *tofree;
1144 typval_T tv;
1145
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001146 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001147 {
1148 if (error || eap->skip)
1149 {
1150 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1151 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1152 {
1153 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001154 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001155 break;
1156 }
1157 }
1158 else
1159 {
1160 // get_name_len() takes care of expanding curly braces
1161 name_start = name = arg;
1162 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1163 if (len <= 0)
1164 {
1165 // This is mainly to keep test 49 working: when expanding
1166 // curly braces fails overrule the exception error message.
1167 if (len < 0 && !aborting())
1168 {
1169 emsg_severe = TRUE;
1170 semsg(_(e_invarg2), arg);
1171 break;
1172 }
1173 error = TRUE;
1174 }
1175 else
1176 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001177 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001178 if (tofree != NULL)
1179 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001180 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001181 error = TRUE;
1182 else
1183 {
1184 // handle d.key, l[idx], f(expr)
1185 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001186 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001187 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001188 error = TRUE;
1189 else
1190 {
1191 if (arg == arg_subsc && len == 2 && name[1] == ':')
1192 {
1193 switch (*name)
1194 {
1195 case 'g': list_glob_vars(first); break;
1196 case 'b': list_buf_vars(first); break;
1197 case 'w': list_win_vars(first); break;
1198 case 't': list_tab_vars(first); break;
1199 case 'v': list_vim_vars(first); break;
1200 case 's': list_script_vars(first); break;
1201 case 'l': list_func_vars(first); break;
1202 default:
1203 semsg(_("E738: Can't list variables for %s"), name);
1204 }
1205 }
1206 else
1207 {
1208 char_u numbuf[NUMBUFLEN];
1209 char_u *tf;
1210 int c;
1211 char_u *s;
1212
1213 s = echo_string(&tv, &tf, numbuf, 0);
1214 c = *arg;
1215 *arg = NUL;
1216 list_one_var_a("",
1217 arg == arg_subsc ? name : name_start,
1218 tv.v_type,
1219 s == NULL ? (char_u *)"" : s,
1220 first);
1221 *arg = c;
1222 vim_free(tf);
1223 }
1224 clear_tv(&tv);
1225 }
1226 }
1227 }
1228
1229 vim_free(tofree);
1230 }
1231
1232 arg = skipwhite(arg);
1233 }
1234
1235 return arg;
1236}
1237
1238/*
1239 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1240 * Returns a pointer to the char just after the var name.
1241 * Returns NULL if there is an error.
1242 */
1243 static char_u *
1244ex_let_one(
1245 char_u *arg, // points to variable name
1246 typval_T *tv, // value to assign to variable
1247 int copy, // copy value from "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001248 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001249 char_u *endchars, // valid chars after variable name or NULL
1250 char_u *op) // "+", "-", "." or NULL
1251{
1252 int c1;
1253 char_u *name;
1254 char_u *p;
1255 char_u *arg_end = NULL;
1256 int len;
1257 int opt_flags;
1258 char_u *tofree = NULL;
1259
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001260 if (in_vim9script() && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001261 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1262 {
1263 vim9_declare_error(arg);
1264 return NULL;
1265 }
1266
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001267 // ":let $VAR = expr": Set environment variable.
1268 if (*arg == '$')
1269 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001270 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 {
1272 emsg(_("E996: Cannot lock an environment variable"));
1273 return NULL;
1274 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001275
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001276 // Find the end of the name.
1277 ++arg;
1278 name = arg;
1279 len = get_env_len(&arg);
1280 if (len == 0)
1281 semsg(_(e_invarg2), name - 1);
1282 else
1283 {
1284 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1285 semsg(_(e_letwrong), op);
1286 else if (endchars != NULL
1287 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1288 emsg(_(e_letunexp));
1289 else if (!check_secure())
1290 {
1291 c1 = name[len];
1292 name[len] = NUL;
1293 p = tv_get_string_chk(tv);
1294 if (p != NULL && op != NULL && *op == '.')
1295 {
1296 int mustfree = FALSE;
1297 char_u *s = vim_getenv(name, &mustfree);
1298
1299 if (s != NULL)
1300 {
1301 p = tofree = concat_str(s, p);
1302 if (mustfree)
1303 vim_free(s);
1304 }
1305 }
1306 if (p != NULL)
1307 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001308 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001309 arg_end = arg;
1310 }
1311 name[len] = c1;
1312 vim_free(tofree);
1313 }
1314 }
1315 }
1316
1317 // ":let &option = expr": Set option value.
1318 // ":let &l:option = expr": Set local option value.
1319 // ":let &g:option = expr": Set global option value.
1320 else if (*arg == '&')
1321 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001322 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001323 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001325 return NULL;
1326 }
1327 // Find the end of the name.
1328 p = find_option_end(&arg, &opt_flags);
1329 if (p == NULL || (endchars != NULL
1330 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1331 emsg(_(e_letunexp));
1332 else
1333 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001334 long n = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001335 int opt_type;
1336 long numval;
1337 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001338 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001339 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340
1341 c1 = *p;
1342 *p = NUL;
1343
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001344 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
1345 if ((opt_type == 1 || opt_type == -1)
1346 && (tv->v_type != VAR_STRING || !in_vim9script()))
1347 // number, possibly hidden
1348 n = (long)tv_get_number(tv);
1349
1350 // Avoid setting a string option to the text "v:false" or similar.
1351 // In Vim9 script also don't convert a number to string.
1352 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1353 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1354 s = tv_get_string_chk(tv);
1355
1356 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001357 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001358 if ((opt_type == 1 && *op == '.')
1359 || (opt_type == 0 && *op != '.'))
1360 {
1361 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001362 failed = TRUE; // don't set the value
1363
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001364 }
1365 else
1366 {
1367 if (opt_type == 1) // number
1368 {
1369 switch (*op)
1370 {
1371 case '+': n = numval + n; break;
1372 case '-': n = numval - n; break;
1373 case '*': n = numval * n; break;
1374 case '/': n = (long)num_divide(numval, n); break;
1375 case '%': n = (long)num_modulus(numval, n); break;
1376 }
1377 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001378 else if (opt_type == 0 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001379 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001380 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381 s = concat_str(stringval, s);
1382 vim_free(stringval);
1383 stringval = s;
1384 }
1385 }
1386 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001387
1388 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001390 if (opt_type != 0 || s != NULL)
1391 {
1392 set_option_value(arg, n, s, opt_flags);
1393 arg_end = p;
1394 }
1395 else
1396 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001397 }
1398 *p = c1;
1399 vim_free(stringval);
1400 }
1401 }
1402
1403 // ":let @r = expr": Set register contents.
1404 else if (*arg == '@')
1405 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001406 if (flags & ASSIGN_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001407 {
1408 emsg(_("E996: Cannot lock a register"));
1409 return NULL;
1410 }
1411 ++arg;
1412 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1413 semsg(_(e_letwrong), op);
1414 else if (endchars != NULL
1415 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1416 emsg(_(e_letunexp));
1417 else
1418 {
1419 char_u *ptofree = NULL;
1420 char_u *s;
1421
1422 p = tv_get_string_chk(tv);
1423 if (p != NULL && op != NULL && *op == '.')
1424 {
1425 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1426 if (s != NULL)
1427 {
1428 p = ptofree = concat_str(s, p);
1429 vim_free(s);
1430 }
1431 }
1432 if (p != NULL)
1433 {
1434 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1435 arg_end = arg + 1;
1436 }
1437 vim_free(ptofree);
1438 }
1439 }
1440
1441 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 // ":let {expr} = expr": Idem, name made with curly braces
1444 else if (eval_isnamec1(*arg) || *arg == '{')
1445 {
1446 lval_T lv;
1447
1448 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001449 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 if (endchars != NULL && vim_strchr(endchars,
1452 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 emsg(_(e_letunexp));
1454 else
1455 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001457 arg_end = p;
1458 }
1459 }
1460 clear_lval(&lv);
1461 }
1462
1463 else
1464 semsg(_(e_invarg2), arg);
1465
1466 return arg_end;
1467}
1468
1469/*
1470 * ":unlet[!] var1 ... " command.
1471 */
1472 void
1473ex_unlet(exarg_T *eap)
1474{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001475 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001476}
1477
1478/*
1479 * ":lockvar" and ":unlockvar" commands
1480 */
1481 void
1482ex_lockvar(exarg_T *eap)
1483{
1484 char_u *arg = eap->arg;
1485 int deep = 2;
1486
1487 if (eap->forceit)
1488 deep = -1;
1489 else if (vim_isdigit(*arg))
1490 {
1491 deep = getdigits(&arg);
1492 arg = skipwhite(arg);
1493 }
1494
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001495 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001496}
1497
1498/*
1499 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001500 * Also used for Vim9 script. "callback" is invoked as:
1501 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001502 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001503 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001504ex_unletlock(
1505 exarg_T *eap,
1506 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001507 int deep,
1508 int glv_flags,
1509 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1510 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001511{
1512 char_u *arg = argstart;
1513 char_u *name_end;
1514 int error = FALSE;
1515 lval_T lv;
1516
1517 do
1518 {
1519 if (*arg == '$')
1520 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001521 lv.ll_name = arg;
1522 lv.ll_tv = NULL;
1523 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524 if (get_env_len(&arg) == 0)
1525 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001526 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001527 return;
1528 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001529 if (!error && !eap->skip
1530 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1531 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001532 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001533 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001534 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001535 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001536 // Parse the name and find the end.
1537 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1538 glv_flags, FNE_CHECK_START);
1539 if (lv.ll_name == NULL)
1540 error = TRUE; // error but continue parsing
1541 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1542 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001543 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001544 if (name_end != NULL)
1545 {
1546 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001547 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001548 }
1549 if (!(eap->skip || error))
1550 clear_lval(&lv);
1551 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001554 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001555 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001556 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001557
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001558 if (!eap->skip)
1559 clear_lval(&lv);
1560 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001561
1562 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001563 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001564
1565 eap->nextcmd = check_nextcmd(arg);
1566}
1567
1568 static int
1569do_unlet_var(
1570 lval_T *lp,
1571 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001572 exarg_T *eap,
1573 int deep UNUSED,
1574 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001576 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577 int ret = OK;
1578 int cc;
1579
1580 if (lp->ll_tv == NULL)
1581 {
1582 cc = *name_end;
1583 *name_end = NUL;
1584
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001585 // Environment variable, normal name or expanded name.
1586 if (*lp->ll_name == '$')
1587 vim_unsetenv(lp->ll_name + 1);
1588 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001589 ret = FAIL;
1590 *name_end = cc;
1591 }
1592 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001593 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001595 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001596 return FAIL;
1597 else if (lp->ll_range)
1598 {
1599 listitem_T *li;
1600 listitem_T *ll_li = lp->ll_li;
1601 int ll_n1 = lp->ll_n1;
1602
1603 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1604 {
1605 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001606 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 return FAIL;
1608 ll_li = li;
1609 ++ll_n1;
1610 }
1611
1612 // Delete a range of List items.
1613 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1614 {
1615 li = lp->ll_li->li_next;
1616 listitem_remove(lp->ll_list, lp->ll_li);
1617 lp->ll_li = li;
1618 ++lp->ll_n1;
1619 }
1620 }
1621 else
1622 {
1623 if (lp->ll_list != NULL)
1624 // unlet a List item.
1625 listitem_remove(lp->ll_list, lp->ll_li);
1626 else
1627 // unlet a Dictionary item.
1628 dictitem_remove(lp->ll_dict, lp->ll_di);
1629 }
1630
1631 return ret;
1632}
1633
1634/*
1635 * "unlet" a variable. Return OK if it existed, FAIL if not.
1636 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1637 */
1638 int
1639do_unlet(char_u *name, int forceit)
1640{
1641 hashtab_T *ht;
1642 hashitem_T *hi;
1643 char_u *varname;
1644 dict_T *d;
1645 dictitem_T *di;
1646
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001647 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001648 return FAIL;
1649
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001650 ht = find_var_ht(name, &varname);
1651 if (ht != NULL && *varname != NUL)
1652 {
1653 d = get_current_funccal_dict(ht);
1654 if (d == NULL)
1655 {
1656 if (ht == &globvarht)
1657 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001658 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001659 d = &vimvardict;
1660 else
1661 {
1662 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1663 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1664 }
1665 if (d == NULL)
1666 {
1667 internal_error("do_unlet()");
1668 return FAIL;
1669 }
1670 }
1671 hi = hash_find(ht, varname);
1672 if (HASHITEM_EMPTY(hi))
1673 hi = find_hi_in_scoped_ht(name, &ht);
1674 if (hi != NULL && !HASHITEM_EMPTY(hi))
1675 {
1676 di = HI2DI(hi);
1677 if (var_check_fixed(di->di_flags, name, FALSE)
1678 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001679 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001680 return FAIL;
1681
1682 delete_var(ht, hi);
1683 return OK;
1684 }
1685 }
1686 if (forceit)
1687 return OK;
1688 semsg(_("E108: No such variable: \"%s\""), name);
1689 return FAIL;
1690}
1691
1692/*
1693 * Lock or unlock variable indicated by "lp".
1694 * "deep" is the levels to go (-1 for unlimited);
1695 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1696 */
1697 static int
1698do_lock_var(
1699 lval_T *lp,
1700 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001701 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001702 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001703 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001704{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001705 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001706 int ret = OK;
1707 int cc;
1708 dictitem_T *di;
1709
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001710 if (lp->ll_tv == NULL)
1711 {
1712 cc = *name_end;
1713 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001714 if (*lp->ll_name == '$')
1715 {
1716 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001717 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001718 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719 else
1720 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001721 // Normal name or expanded name.
1722 di = find_var(lp->ll_name, NULL, TRUE);
1723 if (di == NULL)
1724 ret = FAIL;
1725 else if ((di->di_flags & DI_FLAGS_FIX)
1726 && di->di_tv.v_type != VAR_DICT
1727 && di->di_tv.v_type != VAR_LIST)
1728 {
1729 // For historic reasons this error is not given for a list or
1730 // dict. E.g., the b: dict could be locked/unlocked.
1731 semsg(_(e_lock_unlock), lp->ll_name);
1732 ret = FAIL;
1733 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001734 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001735 {
1736 if (lock)
1737 di->di_flags |= DI_FLAGS_LOCK;
1738 else
1739 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001740 if (deep != 0)
1741 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001742 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743 }
1744 *name_end = cc;
1745 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001746 else if (deep == 0)
1747 {
1748 // nothing to do
1749 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001750 else if (lp->ll_range)
1751 {
1752 listitem_T *li = lp->ll_li;
1753
1754 // (un)lock a range of List items.
1755 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1756 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001757 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001758 li = li->li_next;
1759 ++lp->ll_n1;
1760 }
1761 }
1762 else if (lp->ll_list != NULL)
1763 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001764 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001765 else
1766 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001767 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001768
1769 return ret;
1770}
1771
1772/*
1773 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001774 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1775 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001776 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001777 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001778item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001779{
1780 static int recurse = 0;
1781 list_T *l;
1782 listitem_T *li;
1783 dict_T *d;
1784 blob_T *b;
1785 hashitem_T *hi;
1786 int todo;
1787
1788 if (recurse >= DICT_MAXNEST)
1789 {
1790 emsg(_("E743: variable nested too deep for (un)lock"));
1791 return;
1792 }
1793 if (deep == 0)
1794 return;
1795 ++recurse;
1796
1797 // lock/unlock the item itself
1798 if (lock)
1799 tv->v_lock |= VAR_LOCKED;
1800 else
1801 tv->v_lock &= ~VAR_LOCKED;
1802
1803 switch (tv->v_type)
1804 {
1805 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001806 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001810 case VAR_STRING:
1811 case VAR_FUNC:
1812 case VAR_PARTIAL:
1813 case VAR_FLOAT:
1814 case VAR_SPECIAL:
1815 case VAR_JOB:
1816 case VAR_CHANNEL:
1817 break;
1818
1819 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001820 if ((b = tv->vval.v_blob) != NULL
1821 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001822 {
1823 if (lock)
1824 b->bv_lock |= VAR_LOCKED;
1825 else
1826 b->bv_lock &= ~VAR_LOCKED;
1827 }
1828 break;
1829 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001830 if ((l = tv->vval.v_list) != NULL
1831 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001832 {
1833 if (lock)
1834 l->lv_lock |= VAR_LOCKED;
1835 else
1836 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001837 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001838 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001839 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001840 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001841 }
1842 break;
1843 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001844 if ((d = tv->vval.v_dict) != NULL
1845 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 {
1847 if (lock)
1848 d->dv_lock |= VAR_LOCKED;
1849 else
1850 d->dv_lock &= ~VAR_LOCKED;
1851 if (deep < 0 || deep > 1)
1852 {
1853 // recursive: lock/unlock the items the List contains
1854 todo = (int)d->dv_hashtab.ht_used;
1855 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1856 {
1857 if (!HASHITEM_EMPTY(hi))
1858 {
1859 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001860 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1861 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 }
1863 }
1864 }
1865 }
1866 }
1867 --recurse;
1868}
1869
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001870#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1871/*
1872 * Delete all "menutrans_" variables.
1873 */
1874 void
1875del_menutrans_vars(void)
1876{
1877 hashitem_T *hi;
1878 int todo;
1879
1880 hash_lock(&globvarht);
1881 todo = (int)globvarht.ht_used;
1882 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1883 {
1884 if (!HASHITEM_EMPTY(hi))
1885 {
1886 --todo;
1887 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1888 delete_var(&globvarht, hi);
1889 }
1890 }
1891 hash_unlock(&globvarht);
1892}
1893#endif
1894
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001895/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001896 * Local string buffer for the next two functions to store a variable name
1897 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1898 * get_user_var_name().
1899 */
1900
1901static char_u *varnamebuf = NULL;
1902static int varnamebuflen = 0;
1903
1904/*
1905 * Function to concatenate a prefix and a variable name.
1906 */
1907 static char_u *
1908cat_prefix_varname(int prefix, char_u *name)
1909{
1910 int len;
1911
1912 len = (int)STRLEN(name) + 3;
1913 if (len > varnamebuflen)
1914 {
1915 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001916 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001917 varnamebuf = alloc(len);
1918 if (varnamebuf == NULL)
1919 {
1920 varnamebuflen = 0;
1921 return NULL;
1922 }
1923 varnamebuflen = len;
1924 }
1925 *varnamebuf = prefix;
1926 varnamebuf[1] = ':';
1927 STRCPY(varnamebuf + 2, name);
1928 return varnamebuf;
1929}
1930
1931/*
1932 * Function given to ExpandGeneric() to obtain the list of user defined
1933 * (global/buffer/window/built-in) variable names.
1934 */
1935 char_u *
1936get_user_var_name(expand_T *xp, int idx)
1937{
1938 static long_u gdone;
1939 static long_u bdone;
1940 static long_u wdone;
1941 static long_u tdone;
1942 static int vidx;
1943 static hashitem_T *hi;
1944 hashtab_T *ht;
1945
1946 if (idx == 0)
1947 {
1948 gdone = bdone = wdone = vidx = 0;
1949 tdone = 0;
1950 }
1951
1952 // Global variables
1953 if (gdone < globvarht.ht_used)
1954 {
1955 if (gdone++ == 0)
1956 hi = globvarht.ht_array;
1957 else
1958 ++hi;
1959 while (HASHITEM_EMPTY(hi))
1960 ++hi;
1961 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1962 return cat_prefix_varname('g', hi->hi_key);
1963 return hi->hi_key;
1964 }
1965
1966 // b: variables
1967 ht = &curbuf->b_vars->dv_hashtab;
1968 if (bdone < ht->ht_used)
1969 {
1970 if (bdone++ == 0)
1971 hi = ht->ht_array;
1972 else
1973 ++hi;
1974 while (HASHITEM_EMPTY(hi))
1975 ++hi;
1976 return cat_prefix_varname('b', hi->hi_key);
1977 }
1978
1979 // w: variables
1980 ht = &curwin->w_vars->dv_hashtab;
1981 if (wdone < ht->ht_used)
1982 {
1983 if (wdone++ == 0)
1984 hi = ht->ht_array;
1985 else
1986 ++hi;
1987 while (HASHITEM_EMPTY(hi))
1988 ++hi;
1989 return cat_prefix_varname('w', hi->hi_key);
1990 }
1991
1992 // t: variables
1993 ht = &curtab->tp_vars->dv_hashtab;
1994 if (tdone < ht->ht_used)
1995 {
1996 if (tdone++ == 0)
1997 hi = ht->ht_array;
1998 else
1999 ++hi;
2000 while (HASHITEM_EMPTY(hi))
2001 ++hi;
2002 return cat_prefix_varname('t', hi->hi_key);
2003 }
2004
2005 // v: variables
2006 if (vidx < VV_LEN)
2007 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2008
2009 VIM_CLEAR(varnamebuf);
2010 varnamebuflen = 0;
2011 return NULL;
2012}
2013
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002014 char *
2015get_var_special_name(int nr)
2016{
2017 switch (nr)
2018 {
2019 case VVAL_FALSE: return "v:false";
2020 case VVAL_TRUE: return "v:true";
2021 case VVAL_NONE: return "v:none";
2022 case VVAL_NULL: return "v:null";
2023 }
2024 internal_error("get_var_special_name()");
2025 return "42";
2026}
2027
2028/*
2029 * Returns the global variable dictionary
2030 */
2031 dict_T *
2032get_globvar_dict(void)
2033{
2034 return &globvardict;
2035}
2036
2037/*
2038 * Returns the global variable hash table
2039 */
2040 hashtab_T *
2041get_globvar_ht(void)
2042{
2043 return &globvarht;
2044}
2045
2046/*
2047 * Returns the v: variable dictionary
2048 */
2049 dict_T *
2050get_vimvar_dict(void)
2051{
2052 return &vimvardict;
2053}
2054
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002055/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002056 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002057 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 */
2059 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002060find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002061{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002062 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2063 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064
2065 if (di == NULL)
2066 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002067 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2069 return (int)(vv - vimvars);
2070}
2071
2072
2073/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002074 * Set type of v: variable to "type".
2075 */
2076 void
2077set_vim_var_type(int idx, vartype_T type)
2078{
2079 vimvars[idx].vv_type = type;
2080}
2081
2082/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002083 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002084 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002085 */
2086 void
2087set_vim_var_nr(int idx, varnumber_T val)
2088{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002089 vimvars[idx].vv_nr = val;
2090}
2091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092 char *
2093get_vim_var_name(int idx)
2094{
2095 return vimvars[idx].vv_name;
2096}
2097
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002098/*
2099 * Get typval_T v: variable value.
2100 */
2101 typval_T *
2102get_vim_var_tv(int idx)
2103{
2104 return &vimvars[idx].vv_tv;
2105}
2106
2107/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002108 * Set v: variable to "tv". Only accepts the same type.
2109 * Takes over the value of "tv".
2110 */
2111 int
2112set_vim_var_tv(int idx, typval_T *tv)
2113{
2114 if (vimvars[idx].vv_type != tv->v_type)
2115 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002116 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002117 clear_tv(tv);
2118 return FAIL;
2119 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002120 // VV_RO is also checked when compiling, but let's check here as well.
2121 if (vimvars[idx].vv_flags & VV_RO)
2122 {
2123 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2124 return FAIL;
2125 }
2126 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2127 {
2128 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2129 return FAIL;
2130 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002131 clear_tv(&vimvars[idx].vv_di.di_tv);
2132 vimvars[idx].vv_di.di_tv = *tv;
2133 return OK;
2134}
2135
2136/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002137 * Get number v: variable value.
2138 */
2139 varnumber_T
2140get_vim_var_nr(int idx)
2141{
2142 return vimvars[idx].vv_nr;
2143}
2144
2145/*
2146 * Get string v: variable value. Uses a static buffer, can only be used once.
2147 * If the String variable has never been set, return an empty string.
2148 * Never returns NULL;
2149 */
2150 char_u *
2151get_vim_var_str(int idx)
2152{
2153 return tv_get_string(&vimvars[idx].vv_tv);
2154}
2155
2156/*
2157 * Get List v: variable value. Caller must take care of reference count when
2158 * needed.
2159 */
2160 list_T *
2161get_vim_var_list(int idx)
2162{
2163 return vimvars[idx].vv_list;
2164}
2165
2166/*
2167 * Get Dict v: variable value. Caller must take care of reference count when
2168 * needed.
2169 */
2170 dict_T *
2171get_vim_var_dict(int idx)
2172{
2173 return vimvars[idx].vv_dict;
2174}
2175
2176/*
2177 * Set v:char to character "c".
2178 */
2179 void
2180set_vim_var_char(int c)
2181{
2182 char_u buf[MB_MAXBYTES + 1];
2183
2184 if (has_mbyte)
2185 buf[(*mb_char2bytes)(c, buf)] = NUL;
2186 else
2187 {
2188 buf[0] = c;
2189 buf[1] = NUL;
2190 }
2191 set_vim_var_string(VV_CHAR, buf, -1);
2192}
2193
2194/*
2195 * Set v:count to "count" and v:count1 to "count1".
2196 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2197 */
2198 void
2199set_vcount(
2200 long count,
2201 long count1,
2202 int set_prevcount)
2203{
2204 if (set_prevcount)
2205 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2206 vimvars[VV_COUNT].vv_nr = count;
2207 vimvars[VV_COUNT1].vv_nr = count1;
2208}
2209
2210/*
2211 * Save variables that might be changed as a side effect. Used when executing
2212 * a timer callback.
2213 */
2214 void
2215save_vimvars(vimvars_save_T *vvsave)
2216{
2217 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2218 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2219 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2220}
2221
2222/*
2223 * Restore variables saved by save_vimvars().
2224 */
2225 void
2226restore_vimvars(vimvars_save_T *vvsave)
2227{
2228 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2229 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2230 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2231}
2232
2233/*
2234 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2235 * value.
2236 */
2237 void
2238set_vim_var_string(
2239 int idx,
2240 char_u *val,
2241 int len) // length of "val" to use or -1 (whole string)
2242{
2243 clear_tv(&vimvars[idx].vv_di.di_tv);
2244 vimvars[idx].vv_type = VAR_STRING;
2245 if (val == NULL)
2246 vimvars[idx].vv_str = NULL;
2247 else if (len == -1)
2248 vimvars[idx].vv_str = vim_strsave(val);
2249 else
2250 vimvars[idx].vv_str = vim_strnsave(val, len);
2251}
2252
2253/*
2254 * Set List v: variable to "val".
2255 */
2256 void
2257set_vim_var_list(int idx, list_T *val)
2258{
2259 clear_tv(&vimvars[idx].vv_di.di_tv);
2260 vimvars[idx].vv_type = VAR_LIST;
2261 vimvars[idx].vv_list = val;
2262 if (val != NULL)
2263 ++val->lv_refcount;
2264}
2265
2266/*
2267 * Set Dictionary v: variable to "val".
2268 */
2269 void
2270set_vim_var_dict(int idx, dict_T *val)
2271{
2272 clear_tv(&vimvars[idx].vv_di.di_tv);
2273 vimvars[idx].vv_type = VAR_DICT;
2274 vimvars[idx].vv_dict = val;
2275 if (val != NULL)
2276 {
2277 ++val->dv_refcount;
2278 dict_set_items_ro(val);
2279 }
2280}
2281
2282/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002283 * Set the v:argv list.
2284 */
2285 void
2286set_argv_var(char **argv, int argc)
2287{
2288 list_T *l = list_alloc();
2289 int i;
2290
2291 if (l == NULL)
2292 getout(1);
2293 l->lv_lock = VAR_FIXED;
2294 for (i = 0; i < argc; ++i)
2295 {
2296 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2297 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002298 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002299 }
2300 set_vim_var_list(VV_ARGV, l);
2301}
2302
2303/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002304 * Reset v:register, taking the 'clipboard' setting into account.
2305 */
2306 void
2307reset_reg_var(void)
2308{
2309 int regname = 0;
2310
2311 // Adjust the register according to 'clipboard', so that when
2312 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2313#ifdef FEAT_CLIPBOARD
2314 adjust_clip_reg(&regname);
2315#endif
2316 set_reg_var(regname);
2317}
2318
2319/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002320 * Set v:register if needed.
2321 */
2322 void
2323set_reg_var(int c)
2324{
2325 char_u regname;
2326
2327 if (c == 0 || c == ' ')
2328 regname = '"';
2329 else
2330 regname = c;
2331 // Avoid free/alloc when the value is already right.
2332 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2333 set_vim_var_string(VV_REG, &regname, 1);
2334}
2335
2336/*
2337 * Get or set v:exception. If "oldval" == NULL, return the current value.
2338 * Otherwise, restore the value to "oldval" and return NULL.
2339 * Must always be called in pairs to save and restore v:exception! Does not
2340 * take care of memory allocations.
2341 */
2342 char_u *
2343v_exception(char_u *oldval)
2344{
2345 if (oldval == NULL)
2346 return vimvars[VV_EXCEPTION].vv_str;
2347
2348 vimvars[VV_EXCEPTION].vv_str = oldval;
2349 return NULL;
2350}
2351
2352/*
2353 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2354 * Otherwise, restore the value to "oldval" and return NULL.
2355 * Must always be called in pairs to save and restore v:throwpoint! Does not
2356 * take care of memory allocations.
2357 */
2358 char_u *
2359v_throwpoint(char_u *oldval)
2360{
2361 if (oldval == NULL)
2362 return vimvars[VV_THROWPOINT].vv_str;
2363
2364 vimvars[VV_THROWPOINT].vv_str = oldval;
2365 return NULL;
2366}
2367
2368/*
2369 * Set v:cmdarg.
2370 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2371 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2372 * Must always be called in pairs!
2373 */
2374 char_u *
2375set_cmdarg(exarg_T *eap, char_u *oldarg)
2376{
2377 char_u *oldval;
2378 char_u *newval;
2379 unsigned len;
2380
2381 oldval = vimvars[VV_CMDARG].vv_str;
2382 if (eap == NULL)
2383 {
2384 vim_free(oldval);
2385 vimvars[VV_CMDARG].vv_str = oldarg;
2386 return NULL;
2387 }
2388
2389 if (eap->force_bin == FORCE_BIN)
2390 len = 6;
2391 else if (eap->force_bin == FORCE_NOBIN)
2392 len = 8;
2393 else
2394 len = 0;
2395
2396 if (eap->read_edit)
2397 len += 7;
2398
2399 if (eap->force_ff != 0)
2400 len += 10; // " ++ff=unix"
2401 if (eap->force_enc != 0)
2402 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2403 if (eap->bad_char != 0)
2404 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2405
2406 newval = alloc(len + 1);
2407 if (newval == NULL)
2408 return NULL;
2409
2410 if (eap->force_bin == FORCE_BIN)
2411 sprintf((char *)newval, " ++bin");
2412 else if (eap->force_bin == FORCE_NOBIN)
2413 sprintf((char *)newval, " ++nobin");
2414 else
2415 *newval = NUL;
2416
2417 if (eap->read_edit)
2418 STRCAT(newval, " ++edit");
2419
2420 if (eap->force_ff != 0)
2421 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2422 eap->force_ff == 'u' ? "unix"
2423 : eap->force_ff == 'd' ? "dos"
2424 : "mac");
2425 if (eap->force_enc != 0)
2426 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2427 eap->cmd + eap->force_enc);
2428 if (eap->bad_char == BAD_KEEP)
2429 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2430 else if (eap->bad_char == BAD_DROP)
2431 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2432 else if (eap->bad_char != 0)
2433 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2434 vimvars[VV_CMDARG].vv_str = newval;
2435 return oldval;
2436}
2437
2438/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002439 * Get the value of internal variable "name".
2440 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2441 */
2442 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002443eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002444 char_u *name,
2445 int len, // length of "name"
2446 typval_T *rettv, // NULL when only checking existence
2447 dictitem_T **dip, // non-NULL when typval's dict item is needed
2448 int verbose, // may give error message
2449 int no_autoload) // do not use script autoloading
2450{
2451 int ret = OK;
2452 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002453 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002454 dictitem_T *v;
2455 int cc;
2456
2457 // truncate the name, so that we can use strcmp()
2458 cc = name[len];
2459 name[len] = NUL;
2460
2461 // Check for user-defined variables.
2462 v = find_var(name, NULL, no_autoload);
2463 if (v != NULL)
2464 {
2465 tv = &v->di_tv;
2466 if (dip != NULL)
2467 *dip = v;
2468 }
2469
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002470 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002472 imported_T *import;
2473 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2474
2475 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476
2477 // imported variable from another script
2478 if (import != NULL)
2479 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002480 if (import->imp_funcname != NULL)
2481 {
2482 foundFunc = TRUE;
2483 if (rettv != NULL)
2484 {
2485 rettv->v_type = VAR_FUNC;
2486 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2487 }
2488 }
2489 else
2490 {
2491 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002492 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002494 tv = sv->sv_tv;
2495 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002497 else if (in_vim9script())
2498 {
2499 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2500
2501 if (ufunc != NULL)
2502 {
2503 foundFunc = TRUE;
2504 if (rettv != NULL)
2505 {
2506 rettv->v_type = VAR_FUNC;
2507 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2508 }
2509 }
2510 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 }
2512
Bram Moolenaarc620c052020-07-08 15:16:19 +02002513 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002514 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002515 if (tv == NULL)
2516 {
2517 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002518 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002519 ret = FAIL;
2520 }
2521 else if (rettv != NULL)
2522 copy_tv(tv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002523 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002524
2525 name[len] = cc;
2526
2527 return ret;
2528}
2529
2530/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002531 * Check if variable "name[len]" is a local variable or an argument.
2532 * If so, "*eval_lavars_used" is set to TRUE.
2533 */
2534 void
2535check_vars(char_u *name, int len)
2536{
2537 int cc;
2538 char_u *varname;
2539 hashtab_T *ht;
2540
2541 if (eval_lavars_used == NULL)
2542 return;
2543
2544 // truncate the name, so that we can use strcmp()
2545 cc = name[len];
2546 name[len] = NUL;
2547
2548 ht = find_var_ht(name, &varname);
2549 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2550 {
2551 if (find_var(name, NULL, TRUE) != NULL)
2552 *eval_lavars_used = TRUE;
2553 }
2554
2555 name[len] = cc;
2556}
2557
2558/*
2559 * Find variable "name" in the list of variables.
2560 * Return a pointer to it if found, NULL if not found.
2561 * Careful: "a:0" variables don't have a name.
2562 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2563 * hashtab_T used.
2564 */
2565 dictitem_T *
2566find_var(char_u *name, hashtab_T **htp, int no_autoload)
2567{
2568 char_u *varname;
2569 hashtab_T *ht;
2570 dictitem_T *ret = NULL;
2571
2572 ht = find_var_ht(name, &varname);
2573 if (htp != NULL)
2574 *htp = ht;
2575 if (ht == NULL)
2576 return NULL;
2577 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2578 if (ret != NULL)
2579 return ret;
2580
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002581 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002582 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2583}
2584
2585/*
2586 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002587 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002588 * Returns NULL if not found.
2589 */
2590 dictitem_T *
2591find_var_in_ht(
2592 hashtab_T *ht,
2593 int htname,
2594 char_u *varname,
2595 int no_autoload)
2596{
2597 hashitem_T *hi;
2598
2599 if (*varname == NUL)
2600 {
2601 // Must be something like "s:", otherwise "ht" would be NULL.
2602 switch (htname)
2603 {
2604 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2605 case 'g': return &globvars_var;
2606 case 'v': return &vimvars_var;
2607 case 'b': return &curbuf->b_bufvar;
2608 case 'w': return &curwin->w_winvar;
2609 case 't': return &curtab->tp_winvar;
2610 case 'l': return get_funccal_local_var();
2611 case 'a': return get_funccal_args_var();
2612 }
2613 return NULL;
2614 }
2615
2616 hi = hash_find(ht, varname);
2617 if (HASHITEM_EMPTY(hi))
2618 {
2619 // For global variables we may try auto-loading the script. If it
2620 // worked find the variable again. Don't auto-load a script if it was
2621 // loaded already, otherwise it would be loaded every time when
2622 // checking if a function name is a Funcref variable.
2623 if (ht == &globvarht && !no_autoload)
2624 {
2625 // Note: script_autoload() may make "hi" invalid. It must either
2626 // be obtained again or not used.
2627 if (!script_autoload(varname, FALSE) || aborting())
2628 return NULL;
2629 hi = hash_find(ht, varname);
2630 }
2631 if (HASHITEM_EMPTY(hi))
2632 return NULL;
2633 }
2634 return HI2DI(hi);
2635}
2636
2637/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 * Get the script-local hashtab. NULL if not in a script context.
2639 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002640 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641get_script_local_ht(void)
2642{
2643 scid_T sid = current_sctx.sc_sid;
2644
Bram Moolenaare3d46852020-08-29 13:39:17 +02002645 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 return &SCRIPT_VARS(sid);
2647 return NULL;
2648}
2649
2650/*
2651 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002652 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002654 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2656{
2657 hashtab_T *ht = get_script_local_ht();
2658 char_u buffer[30];
2659 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002660 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 hashitem_T *hi;
2662
2663 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002664 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 if (len < sizeof(buffer) - 1)
2666 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002667 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 vim_strncpy(buffer, name, len);
2669 p = buffer;
2670 }
2671 else
2672 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002673 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002675 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 }
2677
2678 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002679 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680
2681 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002682 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2683 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684
2685 if (p != buffer)
2686 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002687 // Don't return "buffer", gcc complains.
2688 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689}
2690
2691/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002692 * Find the hashtab used for a variable name.
2693 * Return NULL if the name is not valid.
2694 * Set "varname" to the start of name without ':'.
2695 */
2696 hashtab_T *
2697find_var_ht(char_u *name, char_u **varname)
2698{
2699 hashitem_T *hi;
2700 hashtab_T *ht;
2701
2702 if (name[0] == NUL)
2703 return NULL;
2704 if (name[1] != ':')
2705 {
2706 // The name must not start with a colon or #.
2707 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2708 return NULL;
2709 *varname = name;
2710
2711 // "version" is "v:version" in all scopes if scriptversion < 3.
2712 // Same for a few other variables marked with VV_COMPAT.
2713 if (current_sctx.sc_version < 3)
2714 {
2715 hi = hash_find(&compat_hashtab, name);
2716 if (!HASHITEM_EMPTY(hi))
2717 return &compat_hashtab;
2718 }
2719
2720 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 if (ht != NULL)
2722 return ht; // local variable
2723
2724 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002725 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 {
2727 ht = get_script_local_ht();
2728 if (ht != NULL)
2729 return ht;
2730 }
2731
2732 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002733 }
2734 *varname = name + 2;
2735 if (*name == 'g') // global variable
2736 return &globvarht;
2737 // There must be no ':' or '#' in the rest of the name, unless g: is used
2738 if (vim_strchr(name + 2, ':') != NULL
2739 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2740 return NULL;
2741 if (*name == 'b') // buffer variable
2742 return &curbuf->b_vars->dv_hashtab;
2743 if (*name == 'w') // window variable
2744 return &curwin->w_vars->dv_hashtab;
2745 if (*name == 't') // tab page variable
2746 return &curtab->tp_vars->dv_hashtab;
2747 if (*name == 'v') // v: variable
2748 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002749 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002750 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002752 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 if (*name == 'a') // a: function argument
2754 return get_funccal_args_ht();
2755 if (*name == 'l') // l: local function variable
2756 return get_funccal_local_ht();
2757 }
2758 if (*name == 's') // script variable
2759 {
2760 ht = get_script_local_ht();
2761 if (ht != NULL)
2762 return ht;
2763 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002764 return NULL;
2765}
2766
2767/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002768 * Get the string value of a (global/local) variable.
2769 * Note: see tv_get_string() for how long the pointer remains valid.
2770 * Returns NULL when it doesn't exist.
2771 */
2772 char_u *
2773get_var_value(char_u *name)
2774{
2775 dictitem_T *v;
2776
2777 v = find_var(name, NULL, FALSE);
2778 if (v == NULL)
2779 return NULL;
2780 return tv_get_string(&v->di_tv);
2781}
2782
2783/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002784 * Allocate a new hashtab for a sourced script. It will be used while
2785 * sourcing this script and when executing functions defined in the script.
2786 */
2787 void
2788new_script_vars(scid_T id)
2789{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002790 scriptvar_T *sv;
2791
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002792 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2793 if (sv == NULL)
2794 return;
2795 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002796 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002797}
2798
2799/*
2800 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2801 * point to it.
2802 */
2803 void
2804init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2805{
2806 hash_init(&dict->dv_hashtab);
2807 dict->dv_lock = 0;
2808 dict->dv_scope = scope;
2809 dict->dv_refcount = DO_NOT_FREE_CNT;
2810 dict->dv_copyID = 0;
2811 dict_var->di_tv.vval.v_dict = dict;
2812 dict_var->di_tv.v_type = VAR_DICT;
2813 dict_var->di_tv.v_lock = VAR_FIXED;
2814 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2815 dict_var->di_key[0] = NUL;
2816}
2817
2818/*
2819 * Unreference a dictionary initialized by init_var_dict().
2820 */
2821 void
2822unref_var_dict(dict_T *dict)
2823{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002824 // Now the dict needs to be freed if no one else is using it, go back to
2825 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002826 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2827 dict_unref(dict);
2828}
2829
2830/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002831 * Clean up a list of internal variables.
2832 * Frees all allocated variables and the value they contain.
2833 * Clears hashtab "ht", does not free it.
2834 */
2835 void
2836vars_clear(hashtab_T *ht)
2837{
2838 vars_clear_ext(ht, TRUE);
2839}
2840
2841/*
2842 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2843 */
2844 void
2845vars_clear_ext(hashtab_T *ht, int free_val)
2846{
2847 int todo;
2848 hashitem_T *hi;
2849 dictitem_T *v;
2850
2851 hash_lock(ht);
2852 todo = (int)ht->ht_used;
2853 for (hi = ht->ht_array; todo > 0; ++hi)
2854 {
2855 if (!HASHITEM_EMPTY(hi))
2856 {
2857 --todo;
2858
2859 // Free the variable. Don't remove it from the hashtab,
2860 // ht_array might change then. hash_clear() takes care of it
2861 // later.
2862 v = HI2DI(hi);
2863 if (free_val)
2864 clear_tv(&v->di_tv);
2865 if (v->di_flags & DI_FLAGS_ALLOC)
2866 vim_free(v);
2867 }
2868 }
2869 hash_clear(ht);
2870 ht->ht_used = 0;
2871}
2872
2873/*
2874 * Delete a variable from hashtab "ht" at item "hi".
2875 * Clear the variable value and free the dictitem.
2876 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002877 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002878delete_var(hashtab_T *ht, hashitem_T *hi)
2879{
2880 dictitem_T *di = HI2DI(hi);
2881
2882 hash_remove(ht, hi);
2883 clear_tv(&di->di_tv);
2884 vim_free(di);
2885}
2886
2887/*
2888 * List the value of one internal variable.
2889 */
2890 static void
2891list_one_var(dictitem_T *v, char *prefix, int *first)
2892{
2893 char_u *tofree;
2894 char_u *s;
2895 char_u numbuf[NUMBUFLEN];
2896
2897 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2898 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2899 s == NULL ? (char_u *)"" : s, first);
2900 vim_free(tofree);
2901}
2902
2903 static void
2904list_one_var_a(
2905 char *prefix,
2906 char_u *name,
2907 int type,
2908 char_u *string,
2909 int *first) // when TRUE clear rest of screen and set to FALSE
2910{
2911 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2912 msg_start();
2913 msg_puts(prefix);
2914 if (name != NULL) // "a:" vars don't have a name stored
2915 msg_puts((char *)name);
2916 msg_putchar(' ');
2917 msg_advance(22);
2918 if (type == VAR_NUMBER)
2919 msg_putchar('#');
2920 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2921 msg_putchar('*');
2922 else if (type == VAR_LIST)
2923 {
2924 msg_putchar('[');
2925 if (*string == '[')
2926 ++string;
2927 }
2928 else if (type == VAR_DICT)
2929 {
2930 msg_putchar('{');
2931 if (*string == '{')
2932 ++string;
2933 }
2934 else
2935 msg_putchar(' ');
2936
2937 msg_outtrans(string);
2938
2939 if (type == VAR_FUNC || type == VAR_PARTIAL)
2940 msg_puts("()");
2941 if (*first)
2942 {
2943 msg_clr_eos();
2944 *first = FALSE;
2945 }
2946}
2947
2948/*
2949 * Set variable "name" to value in "tv".
2950 * If the variable already exists, the value is updated.
2951 * Otherwise the variable is created.
2952 */
2953 void
2954set_var(
2955 char_u *name,
2956 typval_T *tv,
2957 int copy) // make copy of value in "tv"
2958{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002959 set_var_const(name, NULL, tv, copy, ASSIGN_NO_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002960}
2961
2962/*
2963 * Set variable "name" to value in "tv".
2964 * If the variable already exists and "is_const" is FALSE the value is updated.
2965 * Otherwise the variable is created.
2966 */
2967 void
2968set_var_const(
2969 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002971 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002972 int copy, // make copy of value in "tv"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002973 int flags) // ASSIGN_CONST, ASSIGN_NO_DECL
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002974{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002975 typval_T *tv = tv_arg;
2976 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002978 char_u *varname;
2979 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002981 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002982
2983 ht = find_var_ht(name, &varname);
2984 if (ht == NULL || *varname == NUL)
2985 {
2986 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002987 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002989 is_script_local = ht == get_script_local_ht();
2990
Bram Moolenaardbeecb22020-09-14 18:15:09 +02002991 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002992 && !is_script_local
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002993 && (flags & ASSIGN_NO_DECL) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002994 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002995 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002996 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02002997 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02002998 }
2999
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003001
3002 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 if (di == NULL)
3004 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003005
3006 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003007 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003008 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003009
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003010 if (need_convert_to_bool(type, tv))
3011 {
3012 // Destination is a bool and the value is not, but it can be converted.
3013 CLEAR_FIELD(bool_tv);
3014 bool_tv.v_type = VAR_BOOL;
3015 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3016 tv = &bool_tv;
3017 }
3018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003020 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003022 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003023 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 {
3025 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003026 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 }
3028
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003029 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003031 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003032 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003033 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003034 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003035 }
3036
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003037 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003038 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003039 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003040 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003041
Bram Moolenaara187c432020-09-16 21:08:28 +02003042 // Check in this order for backwards compatibility:
3043 // - Whether the variable is read-only
3044 // - Whether the variable value is locked
3045 // - Whether the variable is locked
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003046 if (var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02003047 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3048 || var_check_lock(di->di_flags, name, FALSE))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003049 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003050 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 else
3052 // can only redefine once
3053 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003054
3055 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003058 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003059 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003060 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003062 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003064 if (copy || tv->v_type != VAR_STRING)
3065 {
3066 char_u *val = tv_get_string(tv);
3067
3068 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003069 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 if (di->di_tv.vval.v_string == NULL)
3071 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003072 }
3073 else
3074 {
3075 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003077 tv->vval.v_string = NULL;
3078 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003079 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003080 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003082 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003084 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086#ifdef FEAT_SEARCH_EXTRA
3087 else if (STRCMP(varname, "hlsearch") == 0)
3088 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090 redraw_all_later(SOME_VALID);
3091 }
3092#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003093 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003094 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003096 {
3097 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003098 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003099 }
3100 }
3101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003103 }
3104 else // add a new variable
3105 {
3106 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003107 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003108 {
3109 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003110 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003111 }
3112
3113 // Make sure the variable name is valid.
3114 if (!valid_varname(varname))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003115 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003116
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3118 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003119 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 STRCPY(di->di_key, varname);
3121 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003122 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003124 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003127 if (flags & ASSIGN_CONST)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003128 di->di_flags |= DI_FLAGS_LOCK;
3129
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003130 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003132 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133
3134 // Store a pointer to the typval_T, so that it can be found by
3135 // index instead of using a hastab lookup.
3136 if (ga_grow(&si->sn_var_vals, 1) == OK)
3137 {
3138 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3139 + si->sn_var_vals.ga_len;
3140 sv->sv_name = di->di_key;
3141 sv->sv_tv = &di->di_tv;
Bram Moolenaar53b29e42020-08-15 14:31:20 +02003142 if (type == NULL)
3143 sv->sv_type = typval2type(tv, &si->sn_type_list);
3144 else
3145 sv->sv_type = type;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003146 sv->sv_const = (flags & ASSIGN_CONST);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 sv->sv_export = is_export;
3148 ++si->sn_var_vals.ga_len;
3149
3150 // let ex_export() know the export worked.
3151 is_export = FALSE;
3152 }
3153 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003154 }
3155
3156 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003158 else
3159 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 di->di_tv = *tv;
3161 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162 init_tv(tv);
3163 }
3164
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003165 // ":const var = val" locks the value
3166 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003167 // Like :lockvar! name: lock the value and what it contains, but only
3168 // if the reference count is up to one. That locks only literal
3169 // values.
3170 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003171 return;
3172failed:
3173 if (!copy)
3174 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003175}
3176
3177/*
3178 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3179 * Also give an error message.
3180 */
3181 int
3182var_check_ro(int flags, char_u *name, int use_gettext)
3183{
3184 if (flags & DI_FLAGS_RO)
3185 {
3186 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3187 return TRUE;
3188 }
3189 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3190 {
3191 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3192 return TRUE;
3193 }
3194 return FALSE;
3195}
3196
3197/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003198 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3199 * Also give an error message.
3200 */
3201 int
3202var_check_lock(int flags, char_u *name, int use_gettext)
3203{
3204 if (flags & DI_FLAGS_LOCK)
3205 {
3206 semsg(_(e_variable_is_locked_str),
3207 use_gettext ? (char_u *)_(name) : name);
3208 return TRUE;
3209 }
3210 return FALSE;
3211}
3212
3213/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003214 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3215 * Also give an error message.
3216 */
3217 int
3218var_check_fixed(int flags, char_u *name, int use_gettext)
3219{
3220 if (flags & DI_FLAGS_FIX)
3221 {
3222 semsg(_("E795: Cannot delete variable %s"),
3223 use_gettext ? (char_u *)_(name) : name);
3224 return TRUE;
3225 }
3226 return FALSE;
3227}
3228
3229/*
3230 * Check if a funcref is assigned to a valid variable name.
3231 * Return TRUE and give an error if not.
3232 */
3233 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003234var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003235 char_u *name, // points to start of variable name
3236 int new_var) // TRUE when creating the variable
3237{
3238 // Allow for w: b: s: and t:.
3239 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3240 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3241 ? name[2] : name[0]))
3242 {
3243 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3244 name);
3245 return TRUE;
3246 }
3247 // Don't allow hiding a function. When "v" is not NULL we might be
3248 // assigning another function to the same var, the type is checked
3249 // below.
3250 if (new_var && function_exists(name, FALSE))
3251 {
3252 semsg(_("E705: Variable name conflicts with existing function: %s"),
3253 name);
3254 return TRUE;
3255 }
3256 return FALSE;
3257}
3258
3259/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003260 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3261 * value. Also give an error message, using "name" or _("name") when
3262 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003263 */
3264 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003265value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003266{
3267 if (lock & VAR_LOCKED)
3268 {
3269 semsg(_("E741: Value is locked: %s"),
3270 name == NULL ? (char_u *)_("Unknown")
3271 : use_gettext ? (char_u *)_(name)
3272 : name);
3273 return TRUE;
3274 }
3275 if (lock & VAR_FIXED)
3276 {
3277 semsg(_("E742: Cannot change value of %s"),
3278 name == NULL ? (char_u *)_("Unknown")
3279 : use_gettext ? (char_u *)_(name)
3280 : name);
3281 return TRUE;
3282 }
3283 return FALSE;
3284}
3285
3286/*
3287 * Check if a variable name is valid.
3288 * Return FALSE and give an error if not.
3289 */
3290 int
3291valid_varname(char_u *varname)
3292{
3293 char_u *p;
3294
3295 for (p = varname; *p != NUL; ++p)
3296 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3297 && *p != AUTOLOAD_CHAR)
3298 {
3299 semsg(_(e_illvar), varname);
3300 return FALSE;
3301 }
3302 return TRUE;
3303}
3304
3305/*
3306 * getwinvar() and gettabwinvar()
3307 */
3308 static void
3309getwinvar(
3310 typval_T *argvars,
3311 typval_T *rettv,
3312 int off) // 1 for gettabwinvar()
3313{
3314 win_T *win;
3315 char_u *varname;
3316 dictitem_T *v;
3317 tabpage_T *tp = NULL;
3318 int done = FALSE;
3319 win_T *oldcurwin;
3320 tabpage_T *oldtabpage;
3321 int need_switch_win;
3322
3323 if (off == 1)
3324 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3325 else
3326 tp = curtab;
3327 win = find_win_by_nr(&argvars[off], tp);
3328 varname = tv_get_string_chk(&argvars[off + 1]);
3329 ++emsg_off;
3330
3331 rettv->v_type = VAR_STRING;
3332 rettv->vval.v_string = NULL;
3333
3334 if (win != NULL && varname != NULL)
3335 {
3336 // Set curwin to be our win, temporarily. Also set the tabpage,
3337 // otherwise the window is not valid. Only do this when needed,
3338 // autocommands get blocked.
3339 need_switch_win = !(tp == curtab && win == curwin);
3340 if (!need_switch_win
3341 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3342 {
3343 if (*varname == '&')
3344 {
3345 if (varname[1] == NUL)
3346 {
3347 // get all window-local options in a dict
3348 dict_T *opts = get_winbuf_options(FALSE);
3349
3350 if (opts != NULL)
3351 {
3352 rettv_dict_set(rettv, opts);
3353 done = TRUE;
3354 }
3355 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003356 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003357 // window-local-option
3358 done = TRUE;
3359 }
3360 else
3361 {
3362 // Look up the variable.
3363 // Let getwinvar({nr}, "") return the "w:" dictionary.
3364 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3365 varname, FALSE);
3366 if (v != NULL)
3367 {
3368 copy_tv(&v->di_tv, rettv);
3369 done = TRUE;
3370 }
3371 }
3372 }
3373
3374 if (need_switch_win)
3375 // restore previous notion of curwin
3376 restore_win(oldcurwin, oldtabpage, TRUE);
3377 }
3378
3379 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3380 // use the default return value
3381 copy_tv(&argvars[off + 2], rettv);
3382
3383 --emsg_off;
3384}
3385
3386/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003387 * Set option "varname" to the value of "varp" for the current buffer/window.
3388 */
3389 static void
3390set_option_from_tv(char_u *varname, typval_T *varp)
3391{
3392 long numval = 0;
3393 char_u *strval;
3394 char_u nbuf[NUMBUFLEN];
3395 int error = FALSE;
3396
3397 if (!in_vim9script() || varp->v_type != VAR_STRING)
3398 numval = (long)tv_get_number_chk(varp, &error);
3399 strval = tv_get_string_buf_chk(varp, nbuf);
3400 if (!error && strval != NULL)
3401 set_option_value(varname, numval, strval, OPT_LOCAL);
3402}
3403
3404/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003405 * "setwinvar()" and "settabwinvar()" functions
3406 */
3407 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003408setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003409{
3410 win_T *win;
3411 win_T *save_curwin;
3412 tabpage_T *save_curtab;
3413 int need_switch_win;
3414 char_u *varname, *winvarname;
3415 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003416 tabpage_T *tp = NULL;
3417
3418 if (check_secure())
3419 return;
3420
3421 if (off == 1)
3422 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3423 else
3424 tp = curtab;
3425 win = find_win_by_nr(&argvars[off], tp);
3426 varname = tv_get_string_chk(&argvars[off + 1]);
3427 varp = &argvars[off + 2];
3428
3429 if (win != NULL && varname != NULL && varp != NULL)
3430 {
3431 need_switch_win = !(tp == curtab && win == curwin);
3432 if (!need_switch_win
3433 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3434 {
3435 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003436 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003437 else
3438 {
3439 winvarname = alloc(STRLEN(varname) + 3);
3440 if (winvarname != NULL)
3441 {
3442 STRCPY(winvarname, "w:");
3443 STRCPY(winvarname + 2, varname);
3444 set_var(winvarname, varp, TRUE);
3445 vim_free(winvarname);
3446 }
3447 }
3448 }
3449 if (need_switch_win)
3450 restore_win(save_curwin, save_curtab, TRUE);
3451 }
3452}
3453
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003454/*
3455 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3456 * v:option_type, and v:option_command.
3457 */
3458 void
3459reset_v_option_vars(void)
3460{
3461 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3462 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3463 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3464 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3465 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3466 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3467}
3468
3469/*
3470 * Add an assert error to v:errors.
3471 */
3472 void
3473assert_error(garray_T *gap)
3474{
3475 struct vimvar *vp = &vimvars[VV_ERRORS];
3476
3477 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003478 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003479 set_vim_var_list(VV_ERRORS, list_alloc());
3480 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3481}
3482
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003483 int
3484var_exists(char_u *var)
3485{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003486 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003487 char_u *name;
3488 char_u *tofree;
3489 typval_T tv;
3490 int len = 0;
3491 int n = FALSE;
3492
3493 // get_name_len() takes care of expanding curly braces
3494 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003495 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003496 if (len > 0)
3497 {
3498 if (tofree != NULL)
3499 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003500 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003501 if (n)
3502 {
3503 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003504 arg = skipwhite(arg);
3505 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003506 if (n)
3507 clear_tv(&tv);
3508 }
3509 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003510 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003511 n = FALSE;
3512
3513 vim_free(tofree);
3514 return n;
3515}
3516
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003517static lval_T *redir_lval = NULL;
3518#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3519static garray_T redir_ga; // only valid when redir_lval is not NULL
3520static char_u *redir_endp = NULL;
3521static char_u *redir_varname = NULL;
3522
3523/*
3524 * Start recording command output to a variable
3525 * When "append" is TRUE append to an existing variable.
3526 * Returns OK if successfully completed the setup. FAIL otherwise.
3527 */
3528 int
3529var_redir_start(char_u *name, int append)
3530{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003531 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003532 typval_T tv;
3533
3534 // Catch a bad name early.
3535 if (!eval_isnamec1(*name))
3536 {
3537 emsg(_(e_invarg));
3538 return FAIL;
3539 }
3540
3541 // Make a copy of the name, it is used in redir_lval until redir ends.
3542 redir_varname = vim_strsave(name);
3543 if (redir_varname == NULL)
3544 return FAIL;
3545
3546 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3547 if (redir_lval == NULL)
3548 {
3549 var_redir_stop();
3550 return FAIL;
3551 }
3552
3553 // The output is stored in growarray "redir_ga" until redirection ends.
3554 ga_init2(&redir_ga, (int)sizeof(char), 500);
3555
3556 // Parse the variable name (can be a dict or list entry).
3557 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3558 FNE_CHECK_START);
3559 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3560 {
3561 clear_lval(redir_lval);
3562 if (redir_endp != NULL && *redir_endp != NUL)
3563 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003564 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003565 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003566 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003567 redir_endp = NULL; // don't store a value, only cleanup
3568 var_redir_stop();
3569 return FAIL;
3570 }
3571
3572 // check if we can write to the variable: set it to or append an empty
3573 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003574 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003575 tv.v_type = VAR_STRING;
3576 tv.vval.v_string = (char_u *)"";
3577 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003579 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003581 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003582 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003583 {
3584 redir_endp = NULL; // don't store a value, only cleanup
3585 var_redir_stop();
3586 return FAIL;
3587 }
3588
3589 return OK;
3590}
3591
3592/*
3593 * Append "value[value_len]" to the variable set by var_redir_start().
3594 * The actual appending is postponed until redirection ends, because the value
3595 * appended may in fact be the string we write to, changing it may cause freed
3596 * memory to be used:
3597 * :redir => foo
3598 * :let foo
3599 * :redir END
3600 */
3601 void
3602var_redir_str(char_u *value, int value_len)
3603{
3604 int len;
3605
3606 if (redir_lval == NULL)
3607 return;
3608
3609 if (value_len == -1)
3610 len = (int)STRLEN(value); // Append the entire string
3611 else
3612 len = value_len; // Append only "value_len" characters
3613
3614 if (ga_grow(&redir_ga, len) == OK)
3615 {
3616 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3617 redir_ga.ga_len += len;
3618 }
3619 else
3620 var_redir_stop();
3621}
3622
3623/*
3624 * Stop redirecting command output to a variable.
3625 * Frees the allocated memory.
3626 */
3627 void
3628var_redir_stop(void)
3629{
3630 typval_T tv;
3631
3632 if (EVALCMD_BUSY)
3633 {
3634 redir_lval = NULL;
3635 return;
3636 }
3637
3638 if (redir_lval != NULL)
3639 {
3640 // If there was no error: assign the text to the variable.
3641 if (redir_endp != NULL)
3642 {
3643 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3644 tv.v_type = VAR_STRING;
3645 tv.vval.v_string = redir_ga.ga_data;
3646 // Call get_lval() again, if it's inside a Dict or List it may
3647 // have changed.
3648 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3649 FALSE, FALSE, 0, FNE_CHECK_START);
3650 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003652 (char_u *)".");
3653 clear_lval(redir_lval);
3654 }
3655
3656 // free the collected output
3657 VIM_CLEAR(redir_ga.ga_data);
3658
3659 VIM_CLEAR(redir_lval);
3660 }
3661 VIM_CLEAR(redir_varname);
3662}
3663
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003664/*
3665 * "gettabvar()" function
3666 */
3667 void
3668f_gettabvar(typval_T *argvars, typval_T *rettv)
3669{
3670 win_T *oldcurwin;
3671 tabpage_T *tp, *oldtabpage;
3672 dictitem_T *v;
3673 char_u *varname;
3674 int done = FALSE;
3675
3676 rettv->v_type = VAR_STRING;
3677 rettv->vval.v_string = NULL;
3678
3679 varname = tv_get_string_chk(&argvars[1]);
3680 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3681 if (tp != NULL && varname != NULL)
3682 {
3683 // Set tp to be our tabpage, temporarily. Also set the window to the
3684 // first window in the tabpage, otherwise the window is not valid.
3685 if (switch_win(&oldcurwin, &oldtabpage,
3686 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3687 : tp->tp_firstwin, tp, TRUE) == OK)
3688 {
3689 // look up the variable
3690 // Let gettabvar({nr}, "") return the "t:" dictionary.
3691 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3692 if (v != NULL)
3693 {
3694 copy_tv(&v->di_tv, rettv);
3695 done = TRUE;
3696 }
3697 }
3698
3699 // restore previous notion of curwin
3700 restore_win(oldcurwin, oldtabpage, TRUE);
3701 }
3702
3703 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3704 copy_tv(&argvars[2], rettv);
3705}
3706
3707/*
3708 * "gettabwinvar()" function
3709 */
3710 void
3711f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3712{
3713 getwinvar(argvars, rettv, 1);
3714}
3715
3716/*
3717 * "getwinvar()" function
3718 */
3719 void
3720f_getwinvar(typval_T *argvars, typval_T *rettv)
3721{
3722 getwinvar(argvars, rettv, 0);
3723}
3724
3725/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003726 * "getbufvar()" function
3727 */
3728 void
3729f_getbufvar(typval_T *argvars, typval_T *rettv)
3730{
3731 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003732 char_u *varname;
3733 dictitem_T *v;
3734 int done = FALSE;
3735
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003736 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003737 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003738
3739 rettv->v_type = VAR_STRING;
3740 rettv->vval.v_string = NULL;
3741
3742 if (buf != NULL && varname != NULL)
3743 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003744 if (*varname == '&')
3745 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003746 buf_T *save_curbuf = curbuf;
3747
3748 // set curbuf to be our buf, temporarily
3749 curbuf = buf;
3750
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003751 if (varname[1] == NUL)
3752 {
3753 // get all buffer-local options in a dict
3754 dict_T *opts = get_winbuf_options(TRUE);
3755
3756 if (opts != NULL)
3757 {
3758 rettv_dict_set(rettv, opts);
3759 done = TRUE;
3760 }
3761 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003762 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003763 // buffer-local-option
3764 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003765
3766 // restore previous notion of curbuf
3767 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003768 }
3769 else
3770 {
3771 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003772 if (*varname == NUL)
3773 // Let getbufvar({nr}, "") return the "b:" dictionary.
3774 v = &buf->b_bufvar;
3775 else
3776 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3777 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003778 if (v != NULL)
3779 {
3780 copy_tv(&v->di_tv, rettv);
3781 done = TRUE;
3782 }
3783 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003784 }
3785
3786 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3787 // use the default value
3788 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003789}
3790
3791/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003792 * "settabvar()" function
3793 */
3794 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003795f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003796{
3797 tabpage_T *save_curtab;
3798 tabpage_T *tp;
3799 char_u *varname, *tabvarname;
3800 typval_T *varp;
3801
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003802 if (check_secure())
3803 return;
3804
3805 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3806 varname = tv_get_string_chk(&argvars[1]);
3807 varp = &argvars[2];
3808
3809 if (varname != NULL && varp != NULL && tp != NULL)
3810 {
3811 save_curtab = curtab;
3812 goto_tabpage_tp(tp, FALSE, FALSE);
3813
3814 tabvarname = alloc(STRLEN(varname) + 3);
3815 if (tabvarname != NULL)
3816 {
3817 STRCPY(tabvarname, "t:");
3818 STRCPY(tabvarname + 2, varname);
3819 set_var(tabvarname, varp, TRUE);
3820 vim_free(tabvarname);
3821 }
3822
3823 // Restore current tabpage
3824 if (valid_tabpage(save_curtab))
3825 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3826 }
3827}
3828
3829/*
3830 * "settabwinvar()" function
3831 */
3832 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003833f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003834{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003835 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003836}
3837
3838/*
3839 * "setwinvar()" function
3840 */
3841 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003842f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003843{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003844 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003845}
3846
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003847/*
3848 * "setbufvar()" function
3849 */
3850 void
3851f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3852{
3853 buf_T *buf;
3854 char_u *varname, *bufvarname;
3855 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003856
3857 if (check_secure())
3858 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003859 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003860 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003861 varp = &argvars[2];
3862
3863 if (buf != NULL && varname != NULL && varp != NULL)
3864 {
3865 if (*varname == '&')
3866 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003867 aco_save_T aco;
3868
3869 // set curbuf to be our buf, temporarily
3870 aucmd_prepbuf(&aco, buf);
3871
Bram Moolenaar191929b2020-08-19 21:20:49 +02003872 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003873
3874 // reset notion of buffer
3875 aucmd_restbuf(&aco);
3876 }
3877 else
3878 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003879 bufvarname = alloc(STRLEN(varname) + 3);
3880 if (bufvarname != NULL)
3881 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003882 buf_T *save_curbuf = curbuf;
3883
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003884 curbuf = buf;
3885 STRCPY(bufvarname, "b:");
3886 STRCPY(bufvarname + 2, varname);
3887 set_var(bufvarname, varp, TRUE);
3888 vim_free(bufvarname);
3889 curbuf = save_curbuf;
3890 }
3891 }
3892 }
3893}
3894
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003895/*
3896 * Get a callback from "arg". It can be a Funcref or a function name.
3897 * When "arg" is zero return an empty string.
3898 * "cb_name" is not allocated.
3899 * "cb_name" is set to NULL for an invalid argument.
3900 */
3901 callback_T
3902get_callback(typval_T *arg)
3903{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003904 callback_T res;
3905 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003906
3907 res.cb_free_name = FALSE;
3908 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3909 {
3910 res.cb_partial = arg->vval.v_partial;
3911 ++res.cb_partial->pt_refcount;
3912 res.cb_name = partial_name(res.cb_partial);
3913 }
3914 else
3915 {
3916 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003917 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3918 && isdigit(*arg->vval.v_string))
3919 r = FAIL;
3920 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003921 {
3922 // Note that we don't make a copy of the string.
3923 res.cb_name = arg->vval.v_string;
3924 func_ref(res.cb_name);
3925 }
3926 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003927 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003928 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003929 r = FAIL;
3930
3931 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003932 {
3933 emsg(_("E921: Invalid callback argument"));
3934 res.cb_name = NULL;
3935 }
3936 }
3937 return res;
3938}
3939
3940/*
3941 * Copy a callback into a typval_T.
3942 */
3943 void
3944put_callback(callback_T *cb, typval_T *tv)
3945{
3946 if (cb->cb_partial != NULL)
3947 {
3948 tv->v_type = VAR_PARTIAL;
3949 tv->vval.v_partial = cb->cb_partial;
3950 ++tv->vval.v_partial->pt_refcount;
3951 }
3952 else
3953 {
3954 tv->v_type = VAR_FUNC;
3955 tv->vval.v_string = vim_strsave(cb->cb_name);
3956 func_ref(cb->cb_name);
3957 }
3958}
3959
3960/*
3961 * Make a copy of "src" into "dest", allocating the function name if needed,
3962 * without incrementing the refcount.
3963 */
3964 void
3965set_callback(callback_T *dest, callback_T *src)
3966{
3967 if (src->cb_partial == NULL)
3968 {
3969 // just a function name, make a copy
3970 dest->cb_name = vim_strsave(src->cb_name);
3971 dest->cb_free_name = TRUE;
3972 }
3973 else
3974 {
3975 // cb_name is a pointer into cb_partial
3976 dest->cb_name = src->cb_name;
3977 dest->cb_free_name = FALSE;
3978 }
3979 dest->cb_partial = src->cb_partial;
3980}
3981
3982/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02003983 * Copy callback from "src" to "dest", incrementing the refcounts.
3984 */
3985 void
3986copy_callback(callback_T *dest, callback_T *src)
3987{
3988 dest->cb_partial = src->cb_partial;
3989 if (dest->cb_partial != NULL)
3990 {
3991 dest->cb_name = src->cb_name;
3992 dest->cb_free_name = FALSE;
3993 ++dest->cb_partial->pt_refcount;
3994 }
3995 else
3996 {
3997 dest->cb_name = vim_strsave(src->cb_name);
3998 dest->cb_free_name = TRUE;
3999 func_ref(src->cb_name);
4000 }
4001}
4002
4003/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004004 * Unref/free "callback" returned by get_callback() or set_callback().
4005 */
4006 void
4007free_callback(callback_T *callback)
4008{
4009 if (callback->cb_partial != NULL)
4010 {
4011 partial_unref(callback->cb_partial);
4012 callback->cb_partial = NULL;
4013 }
4014 else if (callback->cb_name != NULL)
4015 func_unref(callback->cb_name);
4016 if (callback->cb_free_name)
4017 {
4018 vim_free(callback->cb_name);
4019 callback->cb_free_name = FALSE;
4020 }
4021 callback->cb_name = NULL;
4022}
4023
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004024#endif // FEAT_EVAL