blob: 901d406a00c55708e577e55001bf6fedca64c3ae [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 Moolenaar0522ba02019-08-27 22:48:30 +0200176static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200177static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void list_one_var(dictitem_T *v, char *prefix, int *first);
179static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
180
181/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200182 * Initialize global and vim special variables
183 */
184 void
185evalvars_init(void)
186{
187 int i;
188 struct vimvar *p;
189
190 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
191 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
192 vimvardict.dv_lock = VAR_FIXED;
193 hash_init(&compat_hashtab);
194
195 for (i = 0; i < VV_LEN; ++i)
196 {
197 p = &vimvars[i];
198 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
199 {
200 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
201 getout(1);
202 }
203 STRCPY(p->vv_di.di_key, p->vv_name);
204 if (p->vv_flags & VV_RO)
205 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
206 else if (p->vv_flags & VV_RO_SBX)
207 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
208 else
209 p->vv_di.di_flags = DI_FLAGS_FIX;
210
211 // add to v: scope dict, unless the value is not always available
212 if (p->vv_type != VAR_UNKNOWN)
213 hash_add(&vimvarht, p->vv_di.di_key);
214 if (p->vv_flags & VV_COMPAT)
215 // add to compat scope dict
216 hash_add(&compat_hashtab, p->vv_di.di_key);
217 }
218 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
219 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
220
221 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
222 set_vim_var_nr(VV_HLSEARCH, 1L);
223 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
224 set_vim_var_list(VV_ERRORS, list_alloc());
225 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
226
227 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
228 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
229 set_vim_var_nr(VV_NONE, VVAL_NONE);
230 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100231 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200232
233 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
234 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
235 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
236 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
237 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
238 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
239 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
240 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
241 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
242 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
243 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
244
245 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
246
Bram Moolenaar439c0362020-06-06 15:58:03 +0200247 // Default for v:register is not 0 but '"'. This is adjusted once the
248 // clipboard has been setup by calling reset_reg_var().
249 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200250}
251
252#if defined(EXITFREE) || defined(PROTO)
253/*
254 * Free all vim variables information on exit
255 */
256 void
257evalvars_clear(void)
258{
259 int i;
260 struct vimvar *p;
261
262 for (i = 0; i < VV_LEN; ++i)
263 {
264 p = &vimvars[i];
265 if (p->vv_di.di_tv.v_type == VAR_STRING)
266 VIM_CLEAR(p->vv_str);
267 else if (p->vv_di.di_tv.v_type == VAR_LIST)
268 {
269 list_unref(p->vv_list);
270 p->vv_list = NULL;
271 }
272 }
273 hash_clear(&vimvarht);
274 hash_init(&vimvarht); // garbage_collect() will access it
275 hash_clear(&compat_hashtab);
276
277 // global variables
278 vars_clear(&globvarht);
279
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100280 // Script-local variables. Clear all the variables here.
281 // The scriptvar_T is cleared later in free_scriptnames(), because a
282 // variable in one script might hold a reference to the whole scope of
283 // another script.
284 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200285 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286}
287#endif
288
289 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200290garbage_collect_globvars(int copyID)
291{
292 return set_ref_in_ht(&globvarht, copyID, NULL);
293}
294
295 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200296garbage_collect_vimvars(int copyID)
297{
298 return set_ref_in_ht(&vimvarht, copyID, NULL);
299}
300
301 int
302garbage_collect_scriptvars(int copyID)
303{
304 int i;
305 int abort = FALSE;
306
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100307 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200308 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
309
310 return abort;
311}
312
313/*
314 * Set an internal variable to a string value. Creates the variable if it does
315 * not already exist.
316 */
317 void
318set_internal_string_var(char_u *name, char_u *value)
319{
320 char_u *val;
321 typval_T *tvp;
322
323 val = vim_strsave(value);
324 if (val != NULL)
325 {
326 tvp = alloc_string_tv(val);
327 if (tvp != NULL)
328 {
329 set_var(name, tvp, FALSE);
330 free_tv(tvp);
331 }
332 }
333}
334
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200335 int
336eval_charconvert(
337 char_u *enc_from,
338 char_u *enc_to,
339 char_u *fname_from,
340 char_u *fname_to)
341{
342 int err = FALSE;
343
344 set_vim_var_string(VV_CC_FROM, enc_from, -1);
345 set_vim_var_string(VV_CC_TO, enc_to, -1);
346 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
347 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
348 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
349 err = TRUE;
350 set_vim_var_string(VV_CC_FROM, NULL, -1);
351 set_vim_var_string(VV_CC_TO, NULL, -1);
352 set_vim_var_string(VV_FNAME_IN, NULL, -1);
353 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
354
355 if (err)
356 return FAIL;
357 return OK;
358}
359
360# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
361 int
362eval_printexpr(char_u *fname, char_u *args)
363{
364 int err = FALSE;
365
366 set_vim_var_string(VV_FNAME_IN, fname, -1);
367 set_vim_var_string(VV_CMDARG, args, -1);
368 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
369 err = TRUE;
370 set_vim_var_string(VV_FNAME_IN, NULL, -1);
371 set_vim_var_string(VV_CMDARG, NULL, -1);
372
373 if (err)
374 {
375 mch_remove(fname);
376 return FAIL;
377 }
378 return OK;
379}
380# endif
381
382# if defined(FEAT_DIFF) || defined(PROTO)
383 void
384eval_diff(
385 char_u *origfile,
386 char_u *newfile,
387 char_u *outfile)
388{
389 int err = FALSE;
390
391 set_vim_var_string(VV_FNAME_IN, origfile, -1);
392 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
393 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
394 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
395 set_vim_var_string(VV_FNAME_IN, NULL, -1);
396 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
397 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
398}
399
400 void
401eval_patch(
402 char_u *origfile,
403 char_u *difffile,
404 char_u *outfile)
405{
406 int err;
407
408 set_vim_var_string(VV_FNAME_IN, origfile, -1);
409 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
410 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
411 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
412 set_vim_var_string(VV_FNAME_IN, NULL, -1);
413 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
414 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
415}
416# endif
417
418#if defined(FEAT_SPELL) || defined(PROTO)
419/*
420 * Evaluate an expression to a list with suggestions.
421 * For the "expr:" part of 'spellsuggest'.
422 * Returns NULL when there is an error.
423 */
424 list_T *
425eval_spell_expr(char_u *badword, char_u *expr)
426{
427 typval_T save_val;
428 typval_T rettv;
429 list_T *list = NULL;
430 char_u *p = skipwhite(expr);
431
432 // Set "v:val" to the bad word.
433 prepare_vimvar(VV_VAL, &save_val);
434 set_vim_var_string(VV_VAL, badword, -1);
435 if (p_verbose == 0)
436 ++emsg_off;
437
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200438 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200439 {
440 if (rettv.v_type != VAR_LIST)
441 clear_tv(&rettv);
442 else
443 list = rettv.vval.v_list;
444 }
445
446 if (p_verbose == 0)
447 --emsg_off;
448 clear_tv(get_vim_var_tv(VV_VAL));
449 restore_vimvar(VV_VAL, &save_val);
450
451 return list;
452}
453
454/*
455 * "list" is supposed to contain two items: a word and a number. Return the
456 * word in "pp" and the number as the return value.
457 * Return -1 if anything isn't right.
458 * Used to get the good word and score from the eval_spell_expr() result.
459 */
460 int
461get_spellword(list_T *list, char_u **pp)
462{
463 listitem_T *li;
464
465 li = list->lv_first;
466 if (li == NULL)
467 return -1;
468 *pp = tv_get_string(&li->li_tv);
469
470 li = li->li_next;
471 if (li == NULL)
472 return -1;
473 return (int)tv_get_number(&li->li_tv);
474}
475#endif
476
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200477/*
478 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200479 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200480 * When not used yet add the variable to the v: hashtable.
481 */
482 void
483prepare_vimvar(int idx, typval_T *save_tv)
484{
485 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200486 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200487 if (vimvars[idx].vv_type == VAR_UNKNOWN)
488 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
489}
490
491/*
492 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200493 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200494 * When no longer defined, remove the variable from the v: hashtable.
495 */
496 void
497restore_vimvar(int idx, typval_T *save_tv)
498{
499 hashitem_T *hi;
500
501 vimvars[idx].vv_tv = *save_tv;
502 if (vimvars[idx].vv_type == VAR_UNKNOWN)
503 {
504 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
505 if (HASHITEM_EMPTY(hi))
506 internal_error("restore_vimvar()");
507 else
508 hash_remove(&vimvarht, hi);
509 }
510}
511
512/*
513 * List Vim variables.
514 */
515 static void
516list_vim_vars(int *first)
517{
518 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
519}
520
521/*
522 * List script-local variables, if there is a script.
523 */
524 static void
525list_script_vars(int *first)
526{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100527 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200528 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
529 "s:", FALSE, first);
530}
531
532/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200533 * Get a list of lines from a HERE document. The here document is a list of
534 * lines surrounded by a marker.
535 * cmd << {marker}
536 * {line1}
537 * {line2}
538 * ....
539 * {marker}
540 *
541 * The {marker} is a string. If the optional 'trim' word is supplied before the
542 * marker, then the leading indentation before the lines (matching the
543 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200544 *
545 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
546 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
547 * missing, then '.' is accepted as a marker.
548 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200549 * Returns a List with {lines} or NULL.
550 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200552heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200553{
554 char_u *theline;
555 char_u *marker;
556 list_T *l;
557 char_u *p;
558 int marker_indent_len = 0;
559 int text_indent_len = 0;
560 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200561 char_u dot[] = ".";
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200562
563 if (eap->getline == NULL)
564 {
565 emsg(_("E991: cannot use =<< here"));
566 return NULL;
567 }
568
569 // Check for the optional 'trim' word before the marker
570 cmd = skipwhite(cmd);
571 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
572 {
573 cmd = skipwhite(cmd + 4);
574
575 // Trim the indentation from all the lines in the here document.
576 // The amount of indentation trimmed is the same as the indentation of
577 // the first line after the :let command line. To find the end marker
578 // the indent of the :let command line is trimmed.
579 p = *eap->cmdlinep;
580 while (VIM_ISWHITE(*p))
581 {
582 p++;
583 marker_indent_len++;
584 }
585 text_indent_len = -1;
586 }
587
588 // The marker is the next word.
589 if (*cmd != NUL && *cmd != '"')
590 {
591 marker = skipwhite(cmd);
592 p = skiptowhite(marker);
593 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
594 {
595 emsg(_(e_trailing));
596 return NULL;
597 }
598 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200599 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200600 {
601 emsg(_("E221: Marker cannot start with lower case letter"));
602 return NULL;
603 }
604 }
605 else
606 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200607 // When getting lines for an embedded script, if the marker is missing,
608 // accept '.' as the marker.
609 if (script_get)
610 marker = dot;
611 else
612 {
613 emsg(_("E172: Missing marker"));
614 return NULL;
615 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200616 }
617
618 l = list_alloc();
619 if (l == NULL)
620 return NULL;
621
622 for (;;)
623 {
624 int mi = 0;
625 int ti = 0;
626
627 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
628 if (theline == NULL)
629 {
630 semsg(_("E990: Missing end marker '%s'"), marker);
631 break;
632 }
633
634 // with "trim": skip the indent matching the :let line to find the
635 // marker
636 if (marker_indent_len > 0
637 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
638 mi = marker_indent_len;
639 if (STRCMP(marker, theline + mi) == 0)
640 {
641 vim_free(theline);
642 break;
643 }
644
645 if (text_indent_len == -1 && *theline != NUL)
646 {
647 // set the text indent from the first line.
648 p = theline;
649 text_indent_len = 0;
650 while (VIM_ISWHITE(*p))
651 {
652 p++;
653 text_indent_len++;
654 }
655 text_indent = vim_strnsave(theline, text_indent_len);
656 }
657 // with "trim": skip the indent matching the first line
658 if (text_indent != NULL)
659 for (ti = 0; ti < text_indent_len; ++ti)
660 if (theline[ti] != text_indent[ti])
661 break;
662
663 if (list_append_string(l, theline + ti, -1) == FAIL)
664 break;
665 vim_free(theline);
666 }
667 vim_free(text_indent);
668
669 return l;
670}
671
672/*
673 * ":let" list all variable values
674 * ":let var1 var2" list variable values
675 * ":let var = expr" assignment command.
676 * ":let var += expr" assignment command.
677 * ":let var -= expr" assignment command.
678 * ":let var *= expr" assignment command.
679 * ":let var /= expr" assignment command.
680 * ":let var %= expr" assignment command.
681 * ":let var .= expr" assignment command.
682 * ":let var ..= expr" assignment command.
683 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100685 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200686 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200687 * ":const" list all variable values
688 * ":const var1 var2" list variable values
689 * ":const var = expr" assignment command.
690 * ":const [var1, var2] = expr" unpack list.
691 */
692 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200693ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200694{
695 char_u *arg = eap->arg;
696 char_u *expr = NULL;
697 typval_T rettv;
698 int i;
699 int var_count = 0;
700 int semicolon = 0;
701 char_u op[2];
702 char_u *argend;
703 int first = TRUE;
704 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200705 int has_assign;
Bram Moolenaar09689a02020-05-09 22:50:08 +0200706 int flags = eap->cmdidx == CMD_const ? LET_IS_CONST : 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 // detect Vim9 assignment without ":let" or ":const"
709 if (eap->arg == eap->cmd)
710 flags |= LET_NO_COMMAND;
711
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200712 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200713 if (argend == NULL)
714 return;
715 if (argend > arg && argend[-1] == '.') // for var.='str'
716 --argend;
717 expr = skipwhite(argend);
718 concat = expr[0] == '.'
719 && ((expr[1] == '=' && current_sctx.sc_version < 2)
720 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200721 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
722 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200723 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200724 {
725 // ":let" without "=": list variables
726 if (*arg == '[')
727 emsg(_(e_invarg));
728 else if (expr[0] == '.')
729 emsg(_("E985: .= is not supported with script version 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200730 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200731 {
732 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
733 {
734 // Vim9 declaration ":let var: type"
735 arg = vim9_declare_scriptvar(eap, arg);
736 }
737 else
738 {
739 // ":let var1 var2" - list values
740 arg = list_arg_vars(eap, arg, &first);
741 }
742 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743 else if (!eap->skip)
744 {
745 // ":let"
746 list_glob_vars(&first);
747 list_buf_vars(&first);
748 list_win_vars(&first);
749 list_tab_vars(&first);
750 list_script_vars(&first);
751 list_func_vars(&first);
752 list_vim_vars(&first);
753 }
754 eap->nextcmd = check_nextcmd(arg);
755 }
756 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
757 {
758 list_T *l;
759
760 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200761 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200762 if (l != NULL)
763 {
764 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200765 if (!eap->skip)
766 {
767 op[0] = '=';
768 op[1] = NUL;
769 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200771 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200772 clear_tv(&rettv);
773 }
774 }
775 else
776 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200777 evalarg_T evalarg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200778
Bram Moolenaar32e35112020-05-14 22:41:15 +0200779 rettv.v_type = VAR_UNKNOWN;
780 i = FAIL;
781 if (has_assign || concat)
782 {
783 op[0] = '=';
784 op[1] = NUL;
785 if (*expr != '=')
786 {
787 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
788 {
789 op[0] = *expr; // +=, -=, *=, /=, %= or .=
790 if (expr[0] == '.' && expr[1] == '.') // ..=
791 ++expr;
792 }
793 expr = skipwhite(expr + 2);
794 }
795 else
796 expr = skipwhite(expr + 1);
797
798 if (eap->skip)
799 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200800 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200801 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200802 if (getline_equal(eap->getline, eap->cookie, getsourceline))
803 {
804 evalarg.eval_getline = eap->getline;
805 evalarg.eval_cookie = eap->cookie;
806 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200807 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200808 if (eap->skip)
809 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200810 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200811 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200812 if (eap->skip)
813 {
814 if (i != FAIL)
815 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200816 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200817 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200818 {
819 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200821 clear_tv(&rettv);
822 }
823 }
824}
825
826/*
827 * Assign the typevalue "tv" to the variable or variables at "arg_start".
828 * Handles both "var" with any type and "[var, var; var]" with a list type.
829 * When "op" is not NULL it points to a string with characters that
830 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
831 * or concatenate.
832 * Returns OK or FAIL;
833 */
834 int
835ex_let_vars(
836 char_u *arg_start,
837 typval_T *tv,
838 int copy, // copy values from "tv", don't move
839 int semicolon, // from skip_var_list()
840 int var_count, // from skip_var_list()
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200842 char_u *op)
843{
844 char_u *arg = arg_start;
845 list_T *l;
846 int i;
847 listitem_T *item;
848 typval_T ltv;
849
850 if (*arg != '[')
851 {
852 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200854 return FAIL;
855 return OK;
856 }
857
858 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
859 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
860 {
861 emsg(_(e_listreq));
862 return FAIL;
863 }
864
865 i = list_len(l);
866 if (semicolon == 0 && var_count < i)
867 {
868 emsg(_("E687: Less targets than List items"));
869 return FAIL;
870 }
871 if (var_count - semicolon > i)
872 {
873 emsg(_("E688: More targets than List items"));
874 return FAIL;
875 }
876
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200877 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200878 item = l->lv_first;
879 while (*arg != ']')
880 {
881 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200883 item = item->li_next;
884 if (arg == NULL)
885 return FAIL;
886
887 arg = skipwhite(arg);
888 if (*arg == ';')
889 {
890 // Put the rest of the list (may be empty) in the var after ';'.
891 // Create a new list for this.
892 l = list_alloc();
893 if (l == NULL)
894 return FAIL;
895 while (item != NULL)
896 {
897 list_append_tv(l, &item->li_tv);
898 item = item->li_next;
899 }
900
901 ltv.v_type = VAR_LIST;
902 ltv.v_lock = 0;
903 ltv.vval.v_list = l;
904 l->lv_refcount = 1;
905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200907 (char_u *)"]", op);
908 clear_tv(&ltv);
909 if (arg == NULL)
910 return FAIL;
911 break;
912 }
913 else if (*arg != ',' && *arg != ']')
914 {
915 internal_error("ex_let_vars()");
916 return FAIL;
917 }
918 }
919
920 return OK;
921}
922
923/*
924 * Skip over assignable variable "var" or list of variables "[var, var]".
925 * Used for ":let varvar = expr" and ":for varvar in expr".
926 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200927 * for "[var, var; var]" set "semicolon" to 1.
928 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200929 * Return NULL for an error.
930 */
931 char_u *
932skip_var_list(
933 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200935 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200936 int *semicolon,
937 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200938{
939 char_u *p, *s;
940
941 if (*arg == '[')
942 {
943 // "[var, var]": find the matching ']'.
944 p = arg;
945 for (;;)
946 {
947 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200948 s = skip_var_one(p, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200949 if (s == p)
950 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200951 if (!silent)
952 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200953 return NULL;
954 }
955 ++*var_count;
956
957 p = skipwhite(s);
958 if (*p == ']')
959 break;
960 else if (*p == ';')
961 {
962 if (*semicolon == 1)
963 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +0200964 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200965 return NULL;
966 }
967 *semicolon = 1;
968 }
969 else if (*p != ',')
970 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200971 if (!silent)
972 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200973 return NULL;
974 }
975 }
976 return p + 1;
977 }
978 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200980}
981
982/*
983 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
984 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200986 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200987 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200989{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 char_u *end;
991
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200992 if (*arg == '@' && arg[1] != NUL)
993 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200996 if (include_type && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200998 // "a: type" is declaring variable "a" with a type, not "a:".
999 if (end == arg + 2 && end[-1] == ':')
1000 --end;
1001 if (*end == ':')
1002 end = skip_type(skipwhite(end + 1));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001003 }
1004 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001005}
1006
1007/*
1008 * List variables for hashtab "ht" with prefix "prefix".
1009 * If "empty" is TRUE also list NULL strings as empty strings.
1010 */
1011 void
1012list_hashtable_vars(
1013 hashtab_T *ht,
1014 char *prefix,
1015 int empty,
1016 int *first)
1017{
1018 hashitem_T *hi;
1019 dictitem_T *di;
1020 int todo;
1021 char_u buf[IOSIZE];
1022
1023 todo = (int)ht->ht_used;
1024 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1025 {
1026 if (!HASHITEM_EMPTY(hi))
1027 {
1028 --todo;
1029 di = HI2DI(hi);
1030
1031 // apply :filter /pat/ to variable name
1032 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1033 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1034 if (message_filtered(buf))
1035 continue;
1036
1037 if (empty || di->di_tv.v_type != VAR_STRING
1038 || di->di_tv.vval.v_string != NULL)
1039 list_one_var(di, prefix, first);
1040 }
1041 }
1042}
1043
1044/*
1045 * List global variables.
1046 */
1047 static void
1048list_glob_vars(int *first)
1049{
1050 list_hashtable_vars(&globvarht, "", TRUE, first);
1051}
1052
1053/*
1054 * List buffer variables.
1055 */
1056 static void
1057list_buf_vars(int *first)
1058{
1059 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1060}
1061
1062/*
1063 * List window variables.
1064 */
1065 static void
1066list_win_vars(int *first)
1067{
1068 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1069}
1070
1071/*
1072 * List tab page variables.
1073 */
1074 static void
1075list_tab_vars(int *first)
1076{
1077 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1078}
1079
1080/*
1081 * List variables in "arg".
1082 */
1083 static char_u *
1084list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1085{
1086 int error = FALSE;
1087 int len;
1088 char_u *name;
1089 char_u *name_start;
1090 char_u *arg_subsc;
1091 char_u *tofree;
1092 typval_T tv;
1093
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001094 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001095 {
1096 if (error || eap->skip)
1097 {
1098 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1099 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1100 {
1101 emsg_severe = TRUE;
1102 emsg(_(e_trailing));
1103 break;
1104 }
1105 }
1106 else
1107 {
1108 // get_name_len() takes care of expanding curly braces
1109 name_start = name = arg;
1110 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1111 if (len <= 0)
1112 {
1113 // This is mainly to keep test 49 working: when expanding
1114 // curly braces fails overrule the exception error message.
1115 if (len < 0 && !aborting())
1116 {
1117 emsg_severe = TRUE;
1118 semsg(_(e_invarg2), arg);
1119 break;
1120 }
1121 error = TRUE;
1122 }
1123 else
1124 {
1125 if (tofree != NULL)
1126 name = tofree;
1127 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1128 error = TRUE;
1129 else
1130 {
1131 // handle d.key, l[idx], f(expr)
1132 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001133 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001134 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001135 error = TRUE;
1136 else
1137 {
1138 if (arg == arg_subsc && len == 2 && name[1] == ':')
1139 {
1140 switch (*name)
1141 {
1142 case 'g': list_glob_vars(first); break;
1143 case 'b': list_buf_vars(first); break;
1144 case 'w': list_win_vars(first); break;
1145 case 't': list_tab_vars(first); break;
1146 case 'v': list_vim_vars(first); break;
1147 case 's': list_script_vars(first); break;
1148 case 'l': list_func_vars(first); break;
1149 default:
1150 semsg(_("E738: Can't list variables for %s"), name);
1151 }
1152 }
1153 else
1154 {
1155 char_u numbuf[NUMBUFLEN];
1156 char_u *tf;
1157 int c;
1158 char_u *s;
1159
1160 s = echo_string(&tv, &tf, numbuf, 0);
1161 c = *arg;
1162 *arg = NUL;
1163 list_one_var_a("",
1164 arg == arg_subsc ? name : name_start,
1165 tv.v_type,
1166 s == NULL ? (char_u *)"" : s,
1167 first);
1168 *arg = c;
1169 vim_free(tf);
1170 }
1171 clear_tv(&tv);
1172 }
1173 }
1174 }
1175
1176 vim_free(tofree);
1177 }
1178
1179 arg = skipwhite(arg);
1180 }
1181
1182 return arg;
1183}
1184
1185/*
1186 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1187 * Returns a pointer to the char just after the var name.
1188 * Returns NULL if there is an error.
1189 */
1190 static char_u *
1191ex_let_one(
1192 char_u *arg, // points to variable name
1193 typval_T *tv, // value to assign to variable
1194 int copy, // copy value from "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001196 char_u *endchars, // valid chars after variable name or NULL
1197 char_u *op) // "+", "-", "." or NULL
1198{
1199 int c1;
1200 char_u *name;
1201 char_u *p;
1202 char_u *arg_end = NULL;
1203 int len;
1204 int opt_flags;
1205 char_u *tofree = NULL;
1206
1207 // ":let $VAR = expr": Set environment variable.
1208 if (*arg == '$')
1209 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001211 {
1212 emsg(_("E996: Cannot lock an environment variable"));
1213 return NULL;
1214 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001215 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1216 && (flags & LET_NO_COMMAND) == 0)
1217 {
1218 vim9_declare_error(arg);
1219 return NULL;
1220 }
1221
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001222 // Find the end of the name.
1223 ++arg;
1224 name = arg;
1225 len = get_env_len(&arg);
1226 if (len == 0)
1227 semsg(_(e_invarg2), name - 1);
1228 else
1229 {
1230 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1231 semsg(_(e_letwrong), op);
1232 else if (endchars != NULL
1233 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1234 emsg(_(e_letunexp));
1235 else if (!check_secure())
1236 {
1237 c1 = name[len];
1238 name[len] = NUL;
1239 p = tv_get_string_chk(tv);
1240 if (p != NULL && op != NULL && *op == '.')
1241 {
1242 int mustfree = FALSE;
1243 char_u *s = vim_getenv(name, &mustfree);
1244
1245 if (s != NULL)
1246 {
1247 p = tofree = concat_str(s, p);
1248 if (mustfree)
1249 vim_free(s);
1250 }
1251 }
1252 if (p != NULL)
1253 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001254 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001255 arg_end = arg;
1256 }
1257 name[len] = c1;
1258 vim_free(tofree);
1259 }
1260 }
1261 }
1262
1263 // ":let &option = expr": Set option value.
1264 // ":let &l:option = expr": Set local option value.
1265 // ":let &g:option = expr": Set global option value.
1266 else if (*arg == '&')
1267 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001269 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 return NULL;
1272 }
1273 // Find the end of the name.
1274 p = find_option_end(&arg, &opt_flags);
1275 if (p == NULL || (endchars != NULL
1276 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1277 emsg(_(e_letunexp));
1278 else
1279 {
1280 long n;
1281 int opt_type;
1282 long numval;
1283 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001284 char_u *s = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001285
1286 c1 = *p;
1287 *p = NUL;
1288
1289 n = (long)tv_get_number(tv);
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001290 // avoid setting a string option to the text "v:false" or similar.
1291 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL)
1292 s = tv_get_string_chk(tv); // != NULL if number or string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001293 if (s != NULL && op != NULL && *op != '=')
1294 {
1295 opt_type = get_option_value(arg, &numval,
1296 &stringval, opt_flags);
1297 if ((opt_type == 1 && *op == '.')
1298 || (opt_type == 0 && *op != '.'))
1299 {
1300 semsg(_(e_letwrong), op);
1301 s = NULL; // don't set the value
1302 }
1303 else
1304 {
1305 if (opt_type == 1) // number
1306 {
1307 switch (*op)
1308 {
1309 case '+': n = numval + n; break;
1310 case '-': n = numval - n; break;
1311 case '*': n = numval * n; break;
1312 case '/': n = (long)num_divide(numval, n); break;
1313 case '%': n = (long)num_modulus(numval, n); break;
1314 }
1315 }
1316 else if (opt_type == 0 && stringval != NULL) // string
1317 {
1318 s = concat_str(stringval, s);
1319 vim_free(stringval);
1320 stringval = s;
1321 }
1322 }
1323 }
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001324 if (s != NULL || tv->v_type == VAR_BOOL
1325 || tv->v_type == VAR_SPECIAL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001326 {
1327 set_option_value(arg, n, s, opt_flags);
1328 arg_end = p;
1329 }
1330 *p = c1;
1331 vim_free(stringval);
1332 }
1333 }
1334
1335 // ":let @r = expr": Set register contents.
1336 else if (*arg == '@')
1337 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 if (flags & LET_IS_CONST)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001339 {
1340 emsg(_("E996: Cannot lock a register"));
1341 return NULL;
1342 }
1343 ++arg;
1344 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1345 semsg(_(e_letwrong), op);
1346 else if (endchars != NULL
1347 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1348 emsg(_(e_letunexp));
1349 else
1350 {
1351 char_u *ptofree = NULL;
1352 char_u *s;
1353
1354 p = tv_get_string_chk(tv);
1355 if (p != NULL && op != NULL && *op == '.')
1356 {
1357 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1358 if (s != NULL)
1359 {
1360 p = ptofree = concat_str(s, p);
1361 vim_free(s);
1362 }
1363 }
1364 if (p != NULL)
1365 {
1366 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1367 arg_end = arg + 1;
1368 }
1369 vim_free(ptofree);
1370 }
1371 }
1372
1373 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001375 // ":let {expr} = expr": Idem, name made with curly braces
1376 else if (eval_isnamec1(*arg) || *arg == '{')
1377 {
1378 lval_T lv;
1379
1380 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001381 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001382 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 if (endchars != NULL && vim_strchr(endchars,
1384 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001385 emsg(_(e_letunexp));
1386 else
1387 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001389 arg_end = p;
1390 }
1391 }
1392 clear_lval(&lv);
1393 }
1394
1395 else
1396 semsg(_(e_invarg2), arg);
1397
1398 return arg_end;
1399}
1400
1401/*
1402 * ":unlet[!] var1 ... " command.
1403 */
1404 void
1405ex_unlet(exarg_T *eap)
1406{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001407 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001408}
1409
1410/*
1411 * ":lockvar" and ":unlockvar" commands
1412 */
1413 void
1414ex_lockvar(exarg_T *eap)
1415{
1416 char_u *arg = eap->arg;
1417 int deep = 2;
1418
1419 if (eap->forceit)
1420 deep = -1;
1421 else if (vim_isdigit(*arg))
1422 {
1423 deep = getdigits(&arg);
1424 arg = skipwhite(arg);
1425 }
1426
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001427 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001428}
1429
1430/*
1431 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001432 * Also used for Vim9 script. "callback" is invoked as:
1433 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001434 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001435 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001436ex_unletlock(
1437 exarg_T *eap,
1438 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001439 int deep,
1440 int glv_flags,
1441 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1442 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443{
1444 char_u *arg = argstart;
1445 char_u *name_end;
1446 int error = FALSE;
1447 lval_T lv;
1448
1449 do
1450 {
1451 if (*arg == '$')
1452 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001453 lv.ll_name = arg;
1454 lv.ll_tv = NULL;
1455 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001456 if (get_env_len(&arg) == 0)
1457 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001458 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001459 return;
1460 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001461 if (!error && !eap->skip
1462 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1463 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001464 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001465 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001466 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001467 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001468 // Parse the name and find the end.
1469 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
1470 glv_flags, FNE_CHECK_START);
1471 if (lv.ll_name == NULL)
1472 error = TRUE; // error but continue parsing
1473 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1474 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001475 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001476 if (name_end != NULL)
1477 {
1478 emsg_severe = TRUE;
1479 emsg(_(e_trailing));
1480 }
1481 if (!(eap->skip || error))
1482 clear_lval(&lv);
1483 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001484 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001485
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001486 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001487 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001488 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001489
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001490 if (!eap->skip)
1491 clear_lval(&lv);
1492 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001493
1494 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001495 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001496
1497 eap->nextcmd = check_nextcmd(arg);
1498}
1499
1500 static int
1501do_unlet_var(
1502 lval_T *lp,
1503 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001504 exarg_T *eap,
1505 int deep UNUSED,
1506 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001507{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001508 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001509 int ret = OK;
1510 int cc;
1511
1512 if (lp->ll_tv == NULL)
1513 {
1514 cc = *name_end;
1515 *name_end = NUL;
1516
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001517 // Environment variable, normal name or expanded name.
1518 if (*lp->ll_name == '$')
1519 vim_unsetenv(lp->ll_name + 1);
1520 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001521 ret = FAIL;
1522 *name_end = cc;
1523 }
1524 else if ((lp->ll_list != NULL
1525 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1526 || (lp->ll_dict != NULL
1527 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1528 return FAIL;
1529 else if (lp->ll_range)
1530 {
1531 listitem_T *li;
1532 listitem_T *ll_li = lp->ll_li;
1533 int ll_n1 = lp->ll_n1;
1534
1535 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1536 {
1537 li = ll_li->li_next;
1538 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1539 return FAIL;
1540 ll_li = li;
1541 ++ll_n1;
1542 }
1543
1544 // Delete a range of List items.
1545 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1546 {
1547 li = lp->ll_li->li_next;
1548 listitem_remove(lp->ll_list, lp->ll_li);
1549 lp->ll_li = li;
1550 ++lp->ll_n1;
1551 }
1552 }
1553 else
1554 {
1555 if (lp->ll_list != NULL)
1556 // unlet a List item.
1557 listitem_remove(lp->ll_list, lp->ll_li);
1558 else
1559 // unlet a Dictionary item.
1560 dictitem_remove(lp->ll_dict, lp->ll_di);
1561 }
1562
1563 return ret;
1564}
1565
1566/*
1567 * "unlet" a variable. Return OK if it existed, FAIL if not.
1568 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1569 */
1570 int
1571do_unlet(char_u *name, int forceit)
1572{
1573 hashtab_T *ht;
1574 hashitem_T *hi;
1575 char_u *varname;
1576 dict_T *d;
1577 dictitem_T *di;
1578
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001579 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
1580 && check_vim9_unlet(name) == FAIL)
1581 return FAIL;
1582
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583 ht = find_var_ht(name, &varname);
1584 if (ht != NULL && *varname != NUL)
1585 {
1586 d = get_current_funccal_dict(ht);
1587 if (d == NULL)
1588 {
1589 if (ht == &globvarht)
1590 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001591 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001592 d = &vimvardict;
1593 else
1594 {
1595 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1596 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1597 }
1598 if (d == NULL)
1599 {
1600 internal_error("do_unlet()");
1601 return FAIL;
1602 }
1603 }
1604 hi = hash_find(ht, varname);
1605 if (HASHITEM_EMPTY(hi))
1606 hi = find_hi_in_scoped_ht(name, &ht);
1607 if (hi != NULL && !HASHITEM_EMPTY(hi))
1608 {
1609 di = HI2DI(hi);
1610 if (var_check_fixed(di->di_flags, name, FALSE)
1611 || var_check_ro(di->di_flags, name, FALSE)
1612 || var_check_lock(d->dv_lock, name, FALSE))
1613 return FAIL;
1614
1615 delete_var(ht, hi);
1616 return OK;
1617 }
1618 }
1619 if (forceit)
1620 return OK;
1621 semsg(_("E108: No such variable: \"%s\""), name);
1622 return FAIL;
1623}
1624
1625/*
1626 * Lock or unlock variable indicated by "lp".
1627 * "deep" is the levels to go (-1 for unlimited);
1628 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1629 */
1630 static int
1631do_lock_var(
1632 lval_T *lp,
1633 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001634 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001635 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001636 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001638 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001639 int ret = OK;
1640 int cc;
1641 dictitem_T *di;
1642
1643 if (deep == 0) // nothing to do
1644 return OK;
1645
1646 if (lp->ll_tv == NULL)
1647 {
1648 cc = *name_end;
1649 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001650 if (*lp->ll_name == '$')
1651 {
1652 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001653 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001654 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001655 else
1656 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001657 // Normal name or expanded name.
1658 di = find_var(lp->ll_name, NULL, TRUE);
1659 if (di == NULL)
1660 ret = FAIL;
1661 else if ((di->di_flags & DI_FLAGS_FIX)
1662 && di->di_tv.v_type != VAR_DICT
1663 && di->di_tv.v_type != VAR_LIST)
1664 {
1665 // For historic reasons this error is not given for a list or
1666 // dict. E.g., the b: dict could be locked/unlocked.
1667 semsg(_(e_lock_unlock), lp->ll_name);
1668 ret = FAIL;
1669 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001670 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001671 {
1672 if (lock)
1673 di->di_flags |= DI_FLAGS_LOCK;
1674 else
1675 di->di_flags &= ~DI_FLAGS_LOCK;
1676 item_lock(&di->di_tv, deep, lock);
1677 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001678 }
1679 *name_end = cc;
1680 }
1681 else if (lp->ll_range)
1682 {
1683 listitem_T *li = lp->ll_li;
1684
1685 // (un)lock a range of List items.
1686 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1687 {
1688 item_lock(&li->li_tv, deep, lock);
1689 li = li->li_next;
1690 ++lp->ll_n1;
1691 }
1692 }
1693 else if (lp->ll_list != NULL)
1694 // (un)lock a List item.
1695 item_lock(&lp->ll_li->li_tv, deep, lock);
1696 else
1697 // (un)lock a Dictionary item.
1698 item_lock(&lp->ll_di->di_tv, deep, lock);
1699
1700 return ret;
1701}
1702
1703/*
1704 * Lock or unlock an item. "deep" is nr of levels to go.
1705 */
1706 static void
1707item_lock(typval_T *tv, int deep, int lock)
1708{
1709 static int recurse = 0;
1710 list_T *l;
1711 listitem_T *li;
1712 dict_T *d;
1713 blob_T *b;
1714 hashitem_T *hi;
1715 int todo;
1716
1717 if (recurse >= DICT_MAXNEST)
1718 {
1719 emsg(_("E743: variable nested too deep for (un)lock"));
1720 return;
1721 }
1722 if (deep == 0)
1723 return;
1724 ++recurse;
1725
1726 // lock/unlock the item itself
1727 if (lock)
1728 tv->v_lock |= VAR_LOCKED;
1729 else
1730 tv->v_lock &= ~VAR_LOCKED;
1731
1732 switch (tv->v_type)
1733 {
1734 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001735 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001737 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001739 case VAR_STRING:
1740 case VAR_FUNC:
1741 case VAR_PARTIAL:
1742 case VAR_FLOAT:
1743 case VAR_SPECIAL:
1744 case VAR_JOB:
1745 case VAR_CHANNEL:
1746 break;
1747
1748 case VAR_BLOB:
1749 if ((b = tv->vval.v_blob) != NULL)
1750 {
1751 if (lock)
1752 b->bv_lock |= VAR_LOCKED;
1753 else
1754 b->bv_lock &= ~VAR_LOCKED;
1755 }
1756 break;
1757 case VAR_LIST:
1758 if ((l = tv->vval.v_list) != NULL)
1759 {
1760 if (lock)
1761 l->lv_lock |= VAR_LOCKED;
1762 else
1763 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001764 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001765 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001766 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001767 item_lock(&li->li_tv, deep - 1, lock);
1768 }
1769 break;
1770 case VAR_DICT:
1771 if ((d = tv->vval.v_dict) != NULL)
1772 {
1773 if (lock)
1774 d->dv_lock |= VAR_LOCKED;
1775 else
1776 d->dv_lock &= ~VAR_LOCKED;
1777 if (deep < 0 || deep > 1)
1778 {
1779 // recursive: lock/unlock the items the List contains
1780 todo = (int)d->dv_hashtab.ht_used;
1781 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1782 {
1783 if (!HASHITEM_EMPTY(hi))
1784 {
1785 --todo;
1786 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1787 }
1788 }
1789 }
1790 }
1791 }
1792 --recurse;
1793}
1794
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001795#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1796/*
1797 * Delete all "menutrans_" variables.
1798 */
1799 void
1800del_menutrans_vars(void)
1801{
1802 hashitem_T *hi;
1803 int todo;
1804
1805 hash_lock(&globvarht);
1806 todo = (int)globvarht.ht_used;
1807 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1808 {
1809 if (!HASHITEM_EMPTY(hi))
1810 {
1811 --todo;
1812 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1813 delete_var(&globvarht, hi);
1814 }
1815 }
1816 hash_unlock(&globvarht);
1817}
1818#endif
1819
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001820/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001821 * Local string buffer for the next two functions to store a variable name
1822 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1823 * get_user_var_name().
1824 */
1825
1826static char_u *varnamebuf = NULL;
1827static int varnamebuflen = 0;
1828
1829/*
1830 * Function to concatenate a prefix and a variable name.
1831 */
1832 static char_u *
1833cat_prefix_varname(int prefix, char_u *name)
1834{
1835 int len;
1836
1837 len = (int)STRLEN(name) + 3;
1838 if (len > varnamebuflen)
1839 {
1840 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001841 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001842 varnamebuf = alloc(len);
1843 if (varnamebuf == NULL)
1844 {
1845 varnamebuflen = 0;
1846 return NULL;
1847 }
1848 varnamebuflen = len;
1849 }
1850 *varnamebuf = prefix;
1851 varnamebuf[1] = ':';
1852 STRCPY(varnamebuf + 2, name);
1853 return varnamebuf;
1854}
1855
1856/*
1857 * Function given to ExpandGeneric() to obtain the list of user defined
1858 * (global/buffer/window/built-in) variable names.
1859 */
1860 char_u *
1861get_user_var_name(expand_T *xp, int idx)
1862{
1863 static long_u gdone;
1864 static long_u bdone;
1865 static long_u wdone;
1866 static long_u tdone;
1867 static int vidx;
1868 static hashitem_T *hi;
1869 hashtab_T *ht;
1870
1871 if (idx == 0)
1872 {
1873 gdone = bdone = wdone = vidx = 0;
1874 tdone = 0;
1875 }
1876
1877 // Global variables
1878 if (gdone < globvarht.ht_used)
1879 {
1880 if (gdone++ == 0)
1881 hi = globvarht.ht_array;
1882 else
1883 ++hi;
1884 while (HASHITEM_EMPTY(hi))
1885 ++hi;
1886 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1887 return cat_prefix_varname('g', hi->hi_key);
1888 return hi->hi_key;
1889 }
1890
1891 // b: variables
1892 ht = &curbuf->b_vars->dv_hashtab;
1893 if (bdone < ht->ht_used)
1894 {
1895 if (bdone++ == 0)
1896 hi = ht->ht_array;
1897 else
1898 ++hi;
1899 while (HASHITEM_EMPTY(hi))
1900 ++hi;
1901 return cat_prefix_varname('b', hi->hi_key);
1902 }
1903
1904 // w: variables
1905 ht = &curwin->w_vars->dv_hashtab;
1906 if (wdone < ht->ht_used)
1907 {
1908 if (wdone++ == 0)
1909 hi = ht->ht_array;
1910 else
1911 ++hi;
1912 while (HASHITEM_EMPTY(hi))
1913 ++hi;
1914 return cat_prefix_varname('w', hi->hi_key);
1915 }
1916
1917 // t: variables
1918 ht = &curtab->tp_vars->dv_hashtab;
1919 if (tdone < ht->ht_used)
1920 {
1921 if (tdone++ == 0)
1922 hi = ht->ht_array;
1923 else
1924 ++hi;
1925 while (HASHITEM_EMPTY(hi))
1926 ++hi;
1927 return cat_prefix_varname('t', hi->hi_key);
1928 }
1929
1930 // v: variables
1931 if (vidx < VV_LEN)
1932 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1933
1934 VIM_CLEAR(varnamebuf);
1935 varnamebuflen = 0;
1936 return NULL;
1937}
1938
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001939 char *
1940get_var_special_name(int nr)
1941{
1942 switch (nr)
1943 {
1944 case VVAL_FALSE: return "v:false";
1945 case VVAL_TRUE: return "v:true";
1946 case VVAL_NONE: return "v:none";
1947 case VVAL_NULL: return "v:null";
1948 }
1949 internal_error("get_var_special_name()");
1950 return "42";
1951}
1952
1953/*
1954 * Returns the global variable dictionary
1955 */
1956 dict_T *
1957get_globvar_dict(void)
1958{
1959 return &globvardict;
1960}
1961
1962/*
1963 * Returns the global variable hash table
1964 */
1965 hashtab_T *
1966get_globvar_ht(void)
1967{
1968 return &globvarht;
1969}
1970
1971/*
1972 * Returns the v: variable dictionary
1973 */
1974 dict_T *
1975get_vimvar_dict(void)
1976{
1977 return &vimvardict;
1978}
1979
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001980/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001982 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 */
1984 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001985find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001987 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
1988 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989
1990 if (di == NULL)
1991 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02001992 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
1994 return (int)(vv - vimvars);
1995}
1996
1997
1998/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001999 * Set type of v: variable to "type".
2000 */
2001 void
2002set_vim_var_type(int idx, vartype_T type)
2003{
2004 vimvars[idx].vv_type = type;
2005}
2006
2007/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002008 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002009 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002010 */
2011 void
2012set_vim_var_nr(int idx, varnumber_T val)
2013{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002014 vimvars[idx].vv_nr = val;
2015}
2016
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 char *
2018get_vim_var_name(int idx)
2019{
2020 return vimvars[idx].vv_name;
2021}
2022
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002023/*
2024 * Get typval_T v: variable value.
2025 */
2026 typval_T *
2027get_vim_var_tv(int idx)
2028{
2029 return &vimvars[idx].vv_tv;
2030}
2031
2032/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002033 * Set v: variable to "tv". Only accepts the same type.
2034 * Takes over the value of "tv".
2035 */
2036 int
2037set_vim_var_tv(int idx, typval_T *tv)
2038{
2039 if (vimvars[idx].vv_type != tv->v_type)
2040 {
2041 emsg(_("E1063: type mismatch for v: variable"));
2042 clear_tv(tv);
2043 return FAIL;
2044 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002045 // VV_RO is also checked when compiling, but let's check here as well.
2046 if (vimvars[idx].vv_flags & VV_RO)
2047 {
2048 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2049 return FAIL;
2050 }
2051 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2052 {
2053 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2054 return FAIL;
2055 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002056 clear_tv(&vimvars[idx].vv_di.di_tv);
2057 vimvars[idx].vv_di.di_tv = *tv;
2058 return OK;
2059}
2060
2061/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002062 * Get number v: variable value.
2063 */
2064 varnumber_T
2065get_vim_var_nr(int idx)
2066{
2067 return vimvars[idx].vv_nr;
2068}
2069
2070/*
2071 * Get string v: variable value. Uses a static buffer, can only be used once.
2072 * If the String variable has never been set, return an empty string.
2073 * Never returns NULL;
2074 */
2075 char_u *
2076get_vim_var_str(int idx)
2077{
2078 return tv_get_string(&vimvars[idx].vv_tv);
2079}
2080
2081/*
2082 * Get List v: variable value. Caller must take care of reference count when
2083 * needed.
2084 */
2085 list_T *
2086get_vim_var_list(int idx)
2087{
2088 return vimvars[idx].vv_list;
2089}
2090
2091/*
2092 * Get Dict v: variable value. Caller must take care of reference count when
2093 * needed.
2094 */
2095 dict_T *
2096get_vim_var_dict(int idx)
2097{
2098 return vimvars[idx].vv_dict;
2099}
2100
2101/*
2102 * Set v:char to character "c".
2103 */
2104 void
2105set_vim_var_char(int c)
2106{
2107 char_u buf[MB_MAXBYTES + 1];
2108
2109 if (has_mbyte)
2110 buf[(*mb_char2bytes)(c, buf)] = NUL;
2111 else
2112 {
2113 buf[0] = c;
2114 buf[1] = NUL;
2115 }
2116 set_vim_var_string(VV_CHAR, buf, -1);
2117}
2118
2119/*
2120 * Set v:count to "count" and v:count1 to "count1".
2121 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2122 */
2123 void
2124set_vcount(
2125 long count,
2126 long count1,
2127 int set_prevcount)
2128{
2129 if (set_prevcount)
2130 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2131 vimvars[VV_COUNT].vv_nr = count;
2132 vimvars[VV_COUNT1].vv_nr = count1;
2133}
2134
2135/*
2136 * Save variables that might be changed as a side effect. Used when executing
2137 * a timer callback.
2138 */
2139 void
2140save_vimvars(vimvars_save_T *vvsave)
2141{
2142 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2143 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2144 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2145}
2146
2147/*
2148 * Restore variables saved by save_vimvars().
2149 */
2150 void
2151restore_vimvars(vimvars_save_T *vvsave)
2152{
2153 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2154 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2155 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2156}
2157
2158/*
2159 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2160 * value.
2161 */
2162 void
2163set_vim_var_string(
2164 int idx,
2165 char_u *val,
2166 int len) // length of "val" to use or -1 (whole string)
2167{
2168 clear_tv(&vimvars[idx].vv_di.di_tv);
2169 vimvars[idx].vv_type = VAR_STRING;
2170 if (val == NULL)
2171 vimvars[idx].vv_str = NULL;
2172 else if (len == -1)
2173 vimvars[idx].vv_str = vim_strsave(val);
2174 else
2175 vimvars[idx].vv_str = vim_strnsave(val, len);
2176}
2177
2178/*
2179 * Set List v: variable to "val".
2180 */
2181 void
2182set_vim_var_list(int idx, list_T *val)
2183{
2184 clear_tv(&vimvars[idx].vv_di.di_tv);
2185 vimvars[idx].vv_type = VAR_LIST;
2186 vimvars[idx].vv_list = val;
2187 if (val != NULL)
2188 ++val->lv_refcount;
2189}
2190
2191/*
2192 * Set Dictionary v: variable to "val".
2193 */
2194 void
2195set_vim_var_dict(int idx, dict_T *val)
2196{
2197 clear_tv(&vimvars[idx].vv_di.di_tv);
2198 vimvars[idx].vv_type = VAR_DICT;
2199 vimvars[idx].vv_dict = val;
2200 if (val != NULL)
2201 {
2202 ++val->dv_refcount;
2203 dict_set_items_ro(val);
2204 }
2205}
2206
2207/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002208 * Set the v:argv list.
2209 */
2210 void
2211set_argv_var(char **argv, int argc)
2212{
2213 list_T *l = list_alloc();
2214 int i;
2215
2216 if (l == NULL)
2217 getout(1);
2218 l->lv_lock = VAR_FIXED;
2219 for (i = 0; i < argc; ++i)
2220 {
2221 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2222 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002223 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002224 }
2225 set_vim_var_list(VV_ARGV, l);
2226}
2227
2228/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002229 * Reset v:register, taking the 'clipboard' setting into account.
2230 */
2231 void
2232reset_reg_var(void)
2233{
2234 int regname = 0;
2235
2236 // Adjust the register according to 'clipboard', so that when
2237 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2238#ifdef FEAT_CLIPBOARD
2239 adjust_clip_reg(&regname);
2240#endif
2241 set_reg_var(regname);
2242}
2243
2244/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002245 * Set v:register if needed.
2246 */
2247 void
2248set_reg_var(int c)
2249{
2250 char_u regname;
2251
2252 if (c == 0 || c == ' ')
2253 regname = '"';
2254 else
2255 regname = c;
2256 // Avoid free/alloc when the value is already right.
2257 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2258 set_vim_var_string(VV_REG, &regname, 1);
2259}
2260
2261/*
2262 * Get or set v:exception. If "oldval" == NULL, return the current value.
2263 * Otherwise, restore the value to "oldval" and return NULL.
2264 * Must always be called in pairs to save and restore v:exception! Does not
2265 * take care of memory allocations.
2266 */
2267 char_u *
2268v_exception(char_u *oldval)
2269{
2270 if (oldval == NULL)
2271 return vimvars[VV_EXCEPTION].vv_str;
2272
2273 vimvars[VV_EXCEPTION].vv_str = oldval;
2274 return NULL;
2275}
2276
2277/*
2278 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2279 * Otherwise, restore the value to "oldval" and return NULL.
2280 * Must always be called in pairs to save and restore v:throwpoint! Does not
2281 * take care of memory allocations.
2282 */
2283 char_u *
2284v_throwpoint(char_u *oldval)
2285{
2286 if (oldval == NULL)
2287 return vimvars[VV_THROWPOINT].vv_str;
2288
2289 vimvars[VV_THROWPOINT].vv_str = oldval;
2290 return NULL;
2291}
2292
2293/*
2294 * Set v:cmdarg.
2295 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2296 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2297 * Must always be called in pairs!
2298 */
2299 char_u *
2300set_cmdarg(exarg_T *eap, char_u *oldarg)
2301{
2302 char_u *oldval;
2303 char_u *newval;
2304 unsigned len;
2305
2306 oldval = vimvars[VV_CMDARG].vv_str;
2307 if (eap == NULL)
2308 {
2309 vim_free(oldval);
2310 vimvars[VV_CMDARG].vv_str = oldarg;
2311 return NULL;
2312 }
2313
2314 if (eap->force_bin == FORCE_BIN)
2315 len = 6;
2316 else if (eap->force_bin == FORCE_NOBIN)
2317 len = 8;
2318 else
2319 len = 0;
2320
2321 if (eap->read_edit)
2322 len += 7;
2323
2324 if (eap->force_ff != 0)
2325 len += 10; // " ++ff=unix"
2326 if (eap->force_enc != 0)
2327 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2328 if (eap->bad_char != 0)
2329 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2330
2331 newval = alloc(len + 1);
2332 if (newval == NULL)
2333 return NULL;
2334
2335 if (eap->force_bin == FORCE_BIN)
2336 sprintf((char *)newval, " ++bin");
2337 else if (eap->force_bin == FORCE_NOBIN)
2338 sprintf((char *)newval, " ++nobin");
2339 else
2340 *newval = NUL;
2341
2342 if (eap->read_edit)
2343 STRCAT(newval, " ++edit");
2344
2345 if (eap->force_ff != 0)
2346 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2347 eap->force_ff == 'u' ? "unix"
2348 : eap->force_ff == 'd' ? "dos"
2349 : "mac");
2350 if (eap->force_enc != 0)
2351 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2352 eap->cmd + eap->force_enc);
2353 if (eap->bad_char == BAD_KEEP)
2354 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2355 else if (eap->bad_char == BAD_DROP)
2356 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2357 else if (eap->bad_char != 0)
2358 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2359 vimvars[VV_CMDARG].vv_str = newval;
2360 return oldval;
2361}
2362
2363/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002364 * Get the value of internal variable "name".
2365 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2366 */
2367 int
2368get_var_tv(
2369 char_u *name,
2370 int len, // length of "name"
2371 typval_T *rettv, // NULL when only checking existence
2372 dictitem_T **dip, // non-NULL when typval's dict item is needed
2373 int verbose, // may give error message
2374 int no_autoload) // do not use script autoloading
2375{
2376 int ret = OK;
2377 typval_T *tv = NULL;
2378 dictitem_T *v;
2379 int cc;
2380
2381 // truncate the name, so that we can use strcmp()
2382 cc = name[len];
2383 name[len] = NUL;
2384
2385 // Check for user-defined variables.
2386 v = find_var(name, NULL, no_autoload);
2387 if (v != NULL)
2388 {
2389 tv = &v->di_tv;
2390 if (dip != NULL)
2391 *dip = v;
2392 }
2393
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002394 if (tv == NULL && (current_sctx.sc_version == SCRIPT_VERSION_VIM9
2395 || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002397 imported_T *import;
2398 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2399
2400 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
2402 // imported variable from another script
2403 if (import != NULL)
2404 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002405 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2407 + import->imp_var_vals_idx;
2408 tv = sv->sv_tv;
2409 }
2410 }
2411
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002412 if (tv == NULL)
2413 {
2414 if (rettv != NULL && verbose)
2415 semsg(_(e_undefvar), name);
2416 ret = FAIL;
2417 }
2418 else if (rettv != NULL)
2419 copy_tv(tv, rettv);
2420
2421 name[len] = cc;
2422
2423 return ret;
2424}
2425
2426/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002427 * Check if variable "name[len]" is a local variable or an argument.
2428 * If so, "*eval_lavars_used" is set to TRUE.
2429 */
2430 void
2431check_vars(char_u *name, int len)
2432{
2433 int cc;
2434 char_u *varname;
2435 hashtab_T *ht;
2436
2437 if (eval_lavars_used == NULL)
2438 return;
2439
2440 // truncate the name, so that we can use strcmp()
2441 cc = name[len];
2442 name[len] = NUL;
2443
2444 ht = find_var_ht(name, &varname);
2445 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2446 {
2447 if (find_var(name, NULL, TRUE) != NULL)
2448 *eval_lavars_used = TRUE;
2449 }
2450
2451 name[len] = cc;
2452}
2453
2454/*
2455 * Find variable "name" in the list of variables.
2456 * Return a pointer to it if found, NULL if not found.
2457 * Careful: "a:0" variables don't have a name.
2458 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2459 * hashtab_T used.
2460 */
2461 dictitem_T *
2462find_var(char_u *name, hashtab_T **htp, int no_autoload)
2463{
2464 char_u *varname;
2465 hashtab_T *ht;
2466 dictitem_T *ret = NULL;
2467
2468 ht = find_var_ht(name, &varname);
2469 if (htp != NULL)
2470 *htp = ht;
2471 if (ht == NULL)
2472 return NULL;
2473 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2474 if (ret != NULL)
2475 return ret;
2476
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002477 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002478 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2479}
2480
2481/*
2482 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002483 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002484 * Returns NULL if not found.
2485 */
2486 dictitem_T *
2487find_var_in_ht(
2488 hashtab_T *ht,
2489 int htname,
2490 char_u *varname,
2491 int no_autoload)
2492{
2493 hashitem_T *hi;
2494
2495 if (*varname == NUL)
2496 {
2497 // Must be something like "s:", otherwise "ht" would be NULL.
2498 switch (htname)
2499 {
2500 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2501 case 'g': return &globvars_var;
2502 case 'v': return &vimvars_var;
2503 case 'b': return &curbuf->b_bufvar;
2504 case 'w': return &curwin->w_winvar;
2505 case 't': return &curtab->tp_winvar;
2506 case 'l': return get_funccal_local_var();
2507 case 'a': return get_funccal_args_var();
2508 }
2509 return NULL;
2510 }
2511
2512 hi = hash_find(ht, varname);
2513 if (HASHITEM_EMPTY(hi))
2514 {
2515 // For global variables we may try auto-loading the script. If it
2516 // worked find the variable again. Don't auto-load a script if it was
2517 // loaded already, otherwise it would be loaded every time when
2518 // checking if a function name is a Funcref variable.
2519 if (ht == &globvarht && !no_autoload)
2520 {
2521 // Note: script_autoload() may make "hi" invalid. It must either
2522 // be obtained again or not used.
2523 if (!script_autoload(varname, FALSE) || aborting())
2524 return NULL;
2525 hi = hash_find(ht, varname);
2526 }
2527 if (HASHITEM_EMPTY(hi))
2528 return NULL;
2529 }
2530 return HI2DI(hi);
2531}
2532
2533/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 * Get the script-local hashtab. NULL if not in a script context.
2535 */
Bram Moolenaarbdff0122020-04-05 18:56:05 +02002536 static hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537get_script_local_ht(void)
2538{
2539 scid_T sid = current_sctx.sc_sid;
2540
2541 if (sid > 0 && sid <= script_items.ga_len)
2542 return &SCRIPT_VARS(sid);
2543 return NULL;
2544}
2545
2546/*
2547 * Look for "name[len]" in script-local variables.
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002548 * Return a non-NULL pointer when found, NULL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 */
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002550 void *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551lookup_scriptvar(char_u *name, size_t len, cctx_T *dummy UNUSED)
2552{
2553 hashtab_T *ht = get_script_local_ht();
2554 char_u buffer[30];
2555 char_u *p;
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002556 void *res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 hashitem_T *hi;
2558
2559 if (ht == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002560 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 if (len < sizeof(buffer) - 1)
2562 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002563 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 vim_strncpy(buffer, name, len);
2565 p = buffer;
2566 }
2567 else
2568 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002569 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 if (p == NULL)
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002571 return NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
2573
2574 hi = hash_find(ht, p);
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002575 res = HASHITEM_EMPTY(hi) ? NULL : hi;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576
2577 // if not script-local, then perhaps imported
Bram Moolenaarb84a3812020-05-01 15:44:29 +02002578 if (res == NULL && find_imported(p, 0, NULL) != NULL)
2579 res = p;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580
2581 if (p != buffer)
2582 vim_free(p);
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002583 // Don't return "buffer", gcc complains.
2584 return res == NULL ? NULL : IObuff;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585}
2586
2587/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002588 * Find the hashtab used for a variable name.
2589 * Return NULL if the name is not valid.
2590 * Set "varname" to the start of name without ':'.
2591 */
2592 hashtab_T *
2593find_var_ht(char_u *name, char_u **varname)
2594{
2595 hashitem_T *hi;
2596 hashtab_T *ht;
2597
2598 if (name[0] == NUL)
2599 return NULL;
2600 if (name[1] != ':')
2601 {
2602 // The name must not start with a colon or #.
2603 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2604 return NULL;
2605 *varname = name;
2606
2607 // "version" is "v:version" in all scopes if scriptversion < 3.
2608 // Same for a few other variables marked with VV_COMPAT.
2609 if (current_sctx.sc_version < 3)
2610 {
2611 hi = hash_find(&compat_hashtab, name);
2612 if (!HASHITEM_EMPTY(hi))
2613 return &compat_hashtab;
2614 }
2615
2616 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 if (ht != NULL)
2618 return ht; // local variable
2619
2620 // in Vim9 script items at the script level are script-local
2621 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
2622 {
2623 ht = get_script_local_ht();
2624 if (ht != NULL)
2625 return ht;
2626 }
2627
2628 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002629 }
2630 *varname = name + 2;
2631 if (*name == 'g') // global variable
2632 return &globvarht;
2633 // There must be no ':' or '#' in the rest of the name, unless g: is used
2634 if (vim_strchr(name + 2, ':') != NULL
2635 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2636 return NULL;
2637 if (*name == 'b') // buffer variable
2638 return &curbuf->b_vars->dv_hashtab;
2639 if (*name == 'w') // window variable
2640 return &curwin->w_vars->dv_hashtab;
2641 if (*name == 't') // tab page variable
2642 return &curtab->tp_vars->dv_hashtab;
2643 if (*name == 'v') // v: variable
2644 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002645 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002646 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002648 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 if (*name == 'a') // a: function argument
2650 return get_funccal_args_ht();
2651 if (*name == 'l') // l: local function variable
2652 return get_funccal_local_ht();
2653 }
2654 if (*name == 's') // script variable
2655 {
2656 ht = get_script_local_ht();
2657 if (ht != NULL)
2658 return ht;
2659 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002660 return NULL;
2661}
2662
2663/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002664 * Get the string value of a (global/local) variable.
2665 * Note: see tv_get_string() for how long the pointer remains valid.
2666 * Returns NULL when it doesn't exist.
2667 */
2668 char_u *
2669get_var_value(char_u *name)
2670{
2671 dictitem_T *v;
2672
2673 v = find_var(name, NULL, FALSE);
2674 if (v == NULL)
2675 return NULL;
2676 return tv_get_string(&v->di_tv);
2677}
2678
2679/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002680 * Allocate a new hashtab for a sourced script. It will be used while
2681 * sourcing this script and when executing functions defined in the script.
2682 */
2683 void
2684new_script_vars(scid_T id)
2685{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002686 scriptvar_T *sv;
2687
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002688 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2689 if (sv == NULL)
2690 return;
2691 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002692 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002693}
2694
2695/*
2696 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2697 * point to it.
2698 */
2699 void
2700init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2701{
2702 hash_init(&dict->dv_hashtab);
2703 dict->dv_lock = 0;
2704 dict->dv_scope = scope;
2705 dict->dv_refcount = DO_NOT_FREE_CNT;
2706 dict->dv_copyID = 0;
2707 dict_var->di_tv.vval.v_dict = dict;
2708 dict_var->di_tv.v_type = VAR_DICT;
2709 dict_var->di_tv.v_lock = VAR_FIXED;
2710 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2711 dict_var->di_key[0] = NUL;
2712}
2713
2714/*
2715 * Unreference a dictionary initialized by init_var_dict().
2716 */
2717 void
2718unref_var_dict(dict_T *dict)
2719{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002720 // Now the dict needs to be freed if no one else is using it, go back to
2721 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002722 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2723 dict_unref(dict);
2724}
2725
2726/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002727 * Clean up a list of internal variables.
2728 * Frees all allocated variables and the value they contain.
2729 * Clears hashtab "ht", does not free it.
2730 */
2731 void
2732vars_clear(hashtab_T *ht)
2733{
2734 vars_clear_ext(ht, TRUE);
2735}
2736
2737/*
2738 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2739 */
2740 void
2741vars_clear_ext(hashtab_T *ht, int free_val)
2742{
2743 int todo;
2744 hashitem_T *hi;
2745 dictitem_T *v;
2746
2747 hash_lock(ht);
2748 todo = (int)ht->ht_used;
2749 for (hi = ht->ht_array; todo > 0; ++hi)
2750 {
2751 if (!HASHITEM_EMPTY(hi))
2752 {
2753 --todo;
2754
2755 // Free the variable. Don't remove it from the hashtab,
2756 // ht_array might change then. hash_clear() takes care of it
2757 // later.
2758 v = HI2DI(hi);
2759 if (free_val)
2760 clear_tv(&v->di_tv);
2761 if (v->di_flags & DI_FLAGS_ALLOC)
2762 vim_free(v);
2763 }
2764 }
2765 hash_clear(ht);
2766 ht->ht_used = 0;
2767}
2768
2769/*
2770 * Delete a variable from hashtab "ht" at item "hi".
2771 * Clear the variable value and free the dictitem.
2772 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002773 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002774delete_var(hashtab_T *ht, hashitem_T *hi)
2775{
2776 dictitem_T *di = HI2DI(hi);
2777
2778 hash_remove(ht, hi);
2779 clear_tv(&di->di_tv);
2780 vim_free(di);
2781}
2782
2783/*
2784 * List the value of one internal variable.
2785 */
2786 static void
2787list_one_var(dictitem_T *v, char *prefix, int *first)
2788{
2789 char_u *tofree;
2790 char_u *s;
2791 char_u numbuf[NUMBUFLEN];
2792
2793 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2794 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2795 s == NULL ? (char_u *)"" : s, first);
2796 vim_free(tofree);
2797}
2798
2799 static void
2800list_one_var_a(
2801 char *prefix,
2802 char_u *name,
2803 int type,
2804 char_u *string,
2805 int *first) // when TRUE clear rest of screen and set to FALSE
2806{
2807 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2808 msg_start();
2809 msg_puts(prefix);
2810 if (name != NULL) // "a:" vars don't have a name stored
2811 msg_puts((char *)name);
2812 msg_putchar(' ');
2813 msg_advance(22);
2814 if (type == VAR_NUMBER)
2815 msg_putchar('#');
2816 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2817 msg_putchar('*');
2818 else if (type == VAR_LIST)
2819 {
2820 msg_putchar('[');
2821 if (*string == '[')
2822 ++string;
2823 }
2824 else if (type == VAR_DICT)
2825 {
2826 msg_putchar('{');
2827 if (*string == '{')
2828 ++string;
2829 }
2830 else
2831 msg_putchar(' ');
2832
2833 msg_outtrans(string);
2834
2835 if (type == VAR_FUNC || type == VAR_PARTIAL)
2836 msg_puts("()");
2837 if (*first)
2838 {
2839 msg_clr_eos();
2840 *first = FALSE;
2841 }
2842}
2843
2844/*
2845 * Set variable "name" to value in "tv".
2846 * If the variable already exists, the value is updated.
2847 * Otherwise the variable is created.
2848 */
2849 void
2850set_var(
2851 char_u *name,
2852 typval_T *tv,
2853 int copy) // make copy of value in "tv"
2854{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 set_var_const(name, NULL, tv, copy, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002856}
2857
2858/*
2859 * Set variable "name" to value in "tv".
2860 * If the variable already exists and "is_const" is FALSE the value is updated.
2861 * Otherwise the variable is created.
2862 */
2863 void
2864set_var_const(
2865 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 type_T *type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002867 typval_T *tv,
2868 int copy, // make copy of value in "tv"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 int flags) // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002870{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002872 char_u *varname;
2873 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 int is_script_local;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002875
2876 ht = find_var_ht(name, &varname);
2877 if (ht == NULL || *varname == NUL)
2878 {
2879 semsg(_(e_illvar), name);
2880 return;
2881 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002882 is_script_local = ht == get_script_local_ht();
2883
Bram Moolenaar67979662020-06-20 22:50:47 +02002884 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002885 && !is_script_local
2886 && (flags & LET_NO_COMMAND) == 0
2887 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02002888 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02002889 vim9_declare_error(name);
Bram Moolenaar67979662020-06-20 22:50:47 +02002890 return;
2891 }
2892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002894
2895 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 if (di == NULL)
2897 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002898
2899 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002900 && var_check_func_name(name, di == NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002901 return;
2902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002904 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002905 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002906 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 if (flags & LET_IS_CONST)
2908 {
2909 emsg(_(e_cannot_mod));
2910 return;
2911 }
2912
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002913 if (is_script_local
2914 && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 {
Bram Moolenaar34db91f2020-06-13 19:00:10 +02002916 if ((flags & LET_NO_COMMAND) == 0)
2917 {
2918 semsg(_("E1041: Redefining script item %s"), name);
2919 return;
2920 }
2921
2922 // check the type
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002923 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
2924 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002926
2927 if (var_check_ro(di->di_flags, name, FALSE)
2928 || var_check_lock(di->di_tv.v_lock, name, FALSE))
2929 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002930 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 else
2932 // can only redefine once
2933 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002934
2935 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002938 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002939 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002940 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002942 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002944 if (copy || tv->v_type != VAR_STRING)
2945 {
2946 char_u *val = tv_get_string(tv);
2947
2948 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01002949 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950 if (di->di_tv.vval.v_string == NULL)
2951 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002952 }
2953 else
2954 {
2955 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002957 tv->vval.v_string = NULL;
2958 }
2959 return;
2960 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002962 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002964 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002966#ifdef FEAT_SEARCH_EXTRA
2967 else if (STRCMP(varname, "hlsearch") == 0)
2968 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002970 redraw_all_later(SOME_VALID);
2971 }
2972#endif
2973 return;
2974 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002976 {
2977 semsg(_("E963: setting %s to value with wrong type"), name);
2978 return;
2979 }
2980 }
2981
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002983 }
2984 else // add a new variable
2985 {
2986 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002987 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002988 {
2989 semsg(_(e_illvar), name);
2990 return;
2991 }
2992
2993 // Make sure the variable name is valid.
2994 if (!valid_varname(varname))
2995 return;
2996
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
2998 if (di == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002999 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000 STRCPY(di->di_key, varname);
3001 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003002 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003004 return;
3005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 di->di_flags = DI_FLAGS_ALLOC;
3007 if (flags & LET_IS_CONST)
3008 di->di_flags |= DI_FLAGS_LOCK;
3009
3010 if (is_script_local && current_sctx.sc_version == SCRIPT_VERSION_VIM9)
3011 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003012 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
3014 // Store a pointer to the typval_T, so that it can be found by
3015 // index instead of using a hastab lookup.
3016 if (ga_grow(&si->sn_var_vals, 1) == OK)
3017 {
3018 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3019 + si->sn_var_vals.ga_len;
3020 sv->sv_name = di->di_key;
3021 sv->sv_tv = &di->di_tv;
3022 sv->sv_type = type == NULL ? &t_any : type;
3023 sv->sv_const = (flags & LET_IS_CONST);
3024 sv->sv_export = is_export;
3025 ++si->sn_var_vals.ga_len;
3026
3027 // let ex_export() know the export worked.
3028 is_export = FALSE;
3029 }
3030 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003031 }
3032
3033 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003035 else
3036 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 di->di_tv = *tv;
3038 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003039 init_tv(tv);
3040 }
3041
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 if (flags & LET_IS_CONST)
3043 di->di_tv.v_lock |= VAR_LOCKED;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003044}
3045
3046/*
3047 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3048 * Also give an error message.
3049 */
3050 int
3051var_check_ro(int flags, char_u *name, int use_gettext)
3052{
3053 if (flags & DI_FLAGS_RO)
3054 {
3055 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3056 return TRUE;
3057 }
3058 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3059 {
3060 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3061 return TRUE;
3062 }
3063 return FALSE;
3064}
3065
3066/*
3067 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3068 * Also give an error message.
3069 */
3070 int
3071var_check_fixed(int flags, char_u *name, int use_gettext)
3072{
3073 if (flags & DI_FLAGS_FIX)
3074 {
3075 semsg(_("E795: Cannot delete variable %s"),
3076 use_gettext ? (char_u *)_(name) : name);
3077 return TRUE;
3078 }
3079 return FALSE;
3080}
3081
3082/*
3083 * Check if a funcref is assigned to a valid variable name.
3084 * Return TRUE and give an error if not.
3085 */
3086 int
3087var_check_func_name(
3088 char_u *name, // points to start of variable name
3089 int new_var) // TRUE when creating the variable
3090{
3091 // Allow for w: b: s: and t:.
3092 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3093 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3094 ? name[2] : name[0]))
3095 {
3096 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3097 name);
3098 return TRUE;
3099 }
3100 // Don't allow hiding a function. When "v" is not NULL we might be
3101 // assigning another function to the same var, the type is checked
3102 // below.
3103 if (new_var && function_exists(name, FALSE))
3104 {
3105 semsg(_("E705: Variable name conflicts with existing function: %s"),
3106 name);
3107 return TRUE;
3108 }
3109 return FALSE;
3110}
3111
3112/*
3113 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
3114 * Also give an error message, using "name" or _("name") when use_gettext is
3115 * TRUE.
3116 */
3117 int
3118var_check_lock(int lock, char_u *name, int use_gettext)
3119{
3120 if (lock & VAR_LOCKED)
3121 {
3122 semsg(_("E741: Value is locked: %s"),
3123 name == NULL ? (char_u *)_("Unknown")
3124 : use_gettext ? (char_u *)_(name)
3125 : name);
3126 return TRUE;
3127 }
3128 if (lock & VAR_FIXED)
3129 {
3130 semsg(_("E742: Cannot change value of %s"),
3131 name == NULL ? (char_u *)_("Unknown")
3132 : use_gettext ? (char_u *)_(name)
3133 : name);
3134 return TRUE;
3135 }
3136 return FALSE;
3137}
3138
3139/*
3140 * Check if a variable name is valid.
3141 * Return FALSE and give an error if not.
3142 */
3143 int
3144valid_varname(char_u *varname)
3145{
3146 char_u *p;
3147
3148 for (p = varname; *p != NUL; ++p)
3149 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
3150 && *p != AUTOLOAD_CHAR)
3151 {
3152 semsg(_(e_illvar), varname);
3153 return FALSE;
3154 }
3155 return TRUE;
3156}
3157
3158/*
3159 * getwinvar() and gettabwinvar()
3160 */
3161 static void
3162getwinvar(
3163 typval_T *argvars,
3164 typval_T *rettv,
3165 int off) // 1 for gettabwinvar()
3166{
3167 win_T *win;
3168 char_u *varname;
3169 dictitem_T *v;
3170 tabpage_T *tp = NULL;
3171 int done = FALSE;
3172 win_T *oldcurwin;
3173 tabpage_T *oldtabpage;
3174 int need_switch_win;
3175
3176 if (off == 1)
3177 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3178 else
3179 tp = curtab;
3180 win = find_win_by_nr(&argvars[off], tp);
3181 varname = tv_get_string_chk(&argvars[off + 1]);
3182 ++emsg_off;
3183
3184 rettv->v_type = VAR_STRING;
3185 rettv->vval.v_string = NULL;
3186
3187 if (win != NULL && varname != NULL)
3188 {
3189 // Set curwin to be our win, temporarily. Also set the tabpage,
3190 // otherwise the window is not valid. Only do this when needed,
3191 // autocommands get blocked.
3192 need_switch_win = !(tp == curtab && win == curwin);
3193 if (!need_switch_win
3194 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3195 {
3196 if (*varname == '&')
3197 {
3198 if (varname[1] == NUL)
3199 {
3200 // get all window-local options in a dict
3201 dict_T *opts = get_winbuf_options(FALSE);
3202
3203 if (opts != NULL)
3204 {
3205 rettv_dict_set(rettv, opts);
3206 done = TRUE;
3207 }
3208 }
3209 else if (get_option_tv(&varname, rettv, 1) == OK)
3210 // window-local-option
3211 done = TRUE;
3212 }
3213 else
3214 {
3215 // Look up the variable.
3216 // Let getwinvar({nr}, "") return the "w:" dictionary.
3217 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3218 varname, FALSE);
3219 if (v != NULL)
3220 {
3221 copy_tv(&v->di_tv, rettv);
3222 done = TRUE;
3223 }
3224 }
3225 }
3226
3227 if (need_switch_win)
3228 // restore previous notion of curwin
3229 restore_win(oldcurwin, oldtabpage, TRUE);
3230 }
3231
3232 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3233 // use the default return value
3234 copy_tv(&argvars[off + 2], rettv);
3235
3236 --emsg_off;
3237}
3238
3239/*
3240 * "setwinvar()" and "settabwinvar()" functions
3241 */
3242 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003243setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003244{
3245 win_T *win;
3246 win_T *save_curwin;
3247 tabpage_T *save_curtab;
3248 int need_switch_win;
3249 char_u *varname, *winvarname;
3250 typval_T *varp;
3251 char_u nbuf[NUMBUFLEN];
3252 tabpage_T *tp = NULL;
3253
3254 if (check_secure())
3255 return;
3256
3257 if (off == 1)
3258 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3259 else
3260 tp = curtab;
3261 win = find_win_by_nr(&argvars[off], tp);
3262 varname = tv_get_string_chk(&argvars[off + 1]);
3263 varp = &argvars[off + 2];
3264
3265 if (win != NULL && varname != NULL && varp != NULL)
3266 {
3267 need_switch_win = !(tp == curtab && win == curwin);
3268 if (!need_switch_win
3269 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3270 {
3271 if (*varname == '&')
3272 {
3273 long numval;
3274 char_u *strval;
3275 int error = FALSE;
3276
3277 ++varname;
3278 numval = (long)tv_get_number_chk(varp, &error);
3279 strval = tv_get_string_buf_chk(varp, nbuf);
3280 if (!error && strval != NULL)
3281 set_option_value(varname, numval, strval, OPT_LOCAL);
3282 }
3283 else
3284 {
3285 winvarname = alloc(STRLEN(varname) + 3);
3286 if (winvarname != NULL)
3287 {
3288 STRCPY(winvarname, "w:");
3289 STRCPY(winvarname + 2, varname);
3290 set_var(winvarname, varp, TRUE);
3291 vim_free(winvarname);
3292 }
3293 }
3294 }
3295 if (need_switch_win)
3296 restore_win(save_curwin, save_curtab, TRUE);
3297 }
3298}
3299
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003300/*
3301 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3302 * v:option_type, and v:option_command.
3303 */
3304 void
3305reset_v_option_vars(void)
3306{
3307 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3308 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3309 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3310 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3311 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3312 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3313}
3314
3315/*
3316 * Add an assert error to v:errors.
3317 */
3318 void
3319assert_error(garray_T *gap)
3320{
3321 struct vimvar *vp = &vimvars[VV_ERRORS];
3322
3323 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003324 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003325 set_vim_var_list(VV_ERRORS, list_alloc());
3326 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3327}
3328
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003329 int
3330var_exists(char_u *var)
3331{
3332 char_u *name;
3333 char_u *tofree;
3334 typval_T tv;
3335 int len = 0;
3336 int n = FALSE;
3337
3338 // get_name_len() takes care of expanding curly braces
3339 name = var;
3340 len = get_name_len(&var, &tofree, TRUE, FALSE);
3341 if (len > 0)
3342 {
3343 if (tofree != NULL)
3344 name = tofree;
3345 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3346 if (n)
3347 {
3348 // handle d.key, l[idx], f(expr)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02003349 n = (handle_subscript(&var, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003350 if (n)
3351 clear_tv(&tv);
3352 }
3353 }
3354 if (*var != NUL)
3355 n = FALSE;
3356
3357 vim_free(tofree);
3358 return n;
3359}
3360
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003361static lval_T *redir_lval = NULL;
3362#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3363static garray_T redir_ga; // only valid when redir_lval is not NULL
3364static char_u *redir_endp = NULL;
3365static char_u *redir_varname = NULL;
3366
3367/*
3368 * Start recording command output to a variable
3369 * When "append" is TRUE append to an existing variable.
3370 * Returns OK if successfully completed the setup. FAIL otherwise.
3371 */
3372 int
3373var_redir_start(char_u *name, int append)
3374{
3375 int save_emsg;
3376 int err;
3377 typval_T tv;
3378
3379 // Catch a bad name early.
3380 if (!eval_isnamec1(*name))
3381 {
3382 emsg(_(e_invarg));
3383 return FAIL;
3384 }
3385
3386 // Make a copy of the name, it is used in redir_lval until redir ends.
3387 redir_varname = vim_strsave(name);
3388 if (redir_varname == NULL)
3389 return FAIL;
3390
3391 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3392 if (redir_lval == NULL)
3393 {
3394 var_redir_stop();
3395 return FAIL;
3396 }
3397
3398 // The output is stored in growarray "redir_ga" until redirection ends.
3399 ga_init2(&redir_ga, (int)sizeof(char), 500);
3400
3401 // Parse the variable name (can be a dict or list entry).
3402 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3403 FNE_CHECK_START);
3404 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3405 {
3406 clear_lval(redir_lval);
3407 if (redir_endp != NULL && *redir_endp != NUL)
3408 // Trailing characters are present after the variable name
3409 emsg(_(e_trailing));
3410 else
3411 emsg(_(e_invarg));
3412 redir_endp = NULL; // don't store a value, only cleanup
3413 var_redir_stop();
3414 return FAIL;
3415 }
3416
3417 // check if we can write to the variable: set it to or append an empty
3418 // string
3419 save_emsg = did_emsg;
3420 did_emsg = FALSE;
3421 tv.v_type = VAR_STRING;
3422 tv.vval.v_string = (char_u *)"";
3423 if (append)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003424 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003425 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 set_var_lval(redir_lval, redir_endp, &tv, TRUE, 0, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003427 clear_lval(redir_lval);
3428 err = did_emsg;
3429 did_emsg |= save_emsg;
3430 if (err)
3431 {
3432 redir_endp = NULL; // don't store a value, only cleanup
3433 var_redir_stop();
3434 return FAIL;
3435 }
3436
3437 return OK;
3438}
3439
3440/*
3441 * Append "value[value_len]" to the variable set by var_redir_start().
3442 * The actual appending is postponed until redirection ends, because the value
3443 * appended may in fact be the string we write to, changing it may cause freed
3444 * memory to be used:
3445 * :redir => foo
3446 * :let foo
3447 * :redir END
3448 */
3449 void
3450var_redir_str(char_u *value, int value_len)
3451{
3452 int len;
3453
3454 if (redir_lval == NULL)
3455 return;
3456
3457 if (value_len == -1)
3458 len = (int)STRLEN(value); // Append the entire string
3459 else
3460 len = value_len; // Append only "value_len" characters
3461
3462 if (ga_grow(&redir_ga, len) == OK)
3463 {
3464 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3465 redir_ga.ga_len += len;
3466 }
3467 else
3468 var_redir_stop();
3469}
3470
3471/*
3472 * Stop redirecting command output to a variable.
3473 * Frees the allocated memory.
3474 */
3475 void
3476var_redir_stop(void)
3477{
3478 typval_T tv;
3479
3480 if (EVALCMD_BUSY)
3481 {
3482 redir_lval = NULL;
3483 return;
3484 }
3485
3486 if (redir_lval != NULL)
3487 {
3488 // If there was no error: assign the text to the variable.
3489 if (redir_endp != NULL)
3490 {
3491 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3492 tv.v_type = VAR_STRING;
3493 tv.vval.v_string = redir_ga.ga_data;
3494 // Call get_lval() again, if it's inside a Dict or List it may
3495 // have changed.
3496 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3497 FALSE, FALSE, 0, FNE_CHECK_START);
3498 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003500 (char_u *)".");
3501 clear_lval(redir_lval);
3502 }
3503
3504 // free the collected output
3505 VIM_CLEAR(redir_ga.ga_data);
3506
3507 VIM_CLEAR(redir_lval);
3508 }
3509 VIM_CLEAR(redir_varname);
3510}
3511
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003512/*
3513 * "gettabvar()" function
3514 */
3515 void
3516f_gettabvar(typval_T *argvars, typval_T *rettv)
3517{
3518 win_T *oldcurwin;
3519 tabpage_T *tp, *oldtabpage;
3520 dictitem_T *v;
3521 char_u *varname;
3522 int done = FALSE;
3523
3524 rettv->v_type = VAR_STRING;
3525 rettv->vval.v_string = NULL;
3526
3527 varname = tv_get_string_chk(&argvars[1]);
3528 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3529 if (tp != NULL && varname != NULL)
3530 {
3531 // Set tp to be our tabpage, temporarily. Also set the window to the
3532 // first window in the tabpage, otherwise the window is not valid.
3533 if (switch_win(&oldcurwin, &oldtabpage,
3534 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3535 : tp->tp_firstwin, tp, TRUE) == OK)
3536 {
3537 // look up the variable
3538 // Let gettabvar({nr}, "") return the "t:" dictionary.
3539 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3540 if (v != NULL)
3541 {
3542 copy_tv(&v->di_tv, rettv);
3543 done = TRUE;
3544 }
3545 }
3546
3547 // restore previous notion of curwin
3548 restore_win(oldcurwin, oldtabpage, TRUE);
3549 }
3550
3551 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3552 copy_tv(&argvars[2], rettv);
3553}
3554
3555/*
3556 * "gettabwinvar()" function
3557 */
3558 void
3559f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3560{
3561 getwinvar(argvars, rettv, 1);
3562}
3563
3564/*
3565 * "getwinvar()" function
3566 */
3567 void
3568f_getwinvar(typval_T *argvars, typval_T *rettv)
3569{
3570 getwinvar(argvars, rettv, 0);
3571}
3572
3573/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003574 * "getbufvar()" function
3575 */
3576 void
3577f_getbufvar(typval_T *argvars, typval_T *rettv)
3578{
3579 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003580 char_u *varname;
3581 dictitem_T *v;
3582 int done = FALSE;
3583
3584 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3585 varname = tv_get_string_chk(&argvars[1]);
3586 ++emsg_off;
3587 buf = tv_get_buf(&argvars[0], FALSE);
3588
3589 rettv->v_type = VAR_STRING;
3590 rettv->vval.v_string = NULL;
3591
3592 if (buf != NULL && varname != NULL)
3593 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003594 if (*varname == '&')
3595 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003596 buf_T *save_curbuf = curbuf;
3597
3598 // set curbuf to be our buf, temporarily
3599 curbuf = buf;
3600
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003601 if (varname[1] == NUL)
3602 {
3603 // get all buffer-local options in a dict
3604 dict_T *opts = get_winbuf_options(TRUE);
3605
3606 if (opts != NULL)
3607 {
3608 rettv_dict_set(rettv, opts);
3609 done = TRUE;
3610 }
3611 }
3612 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3613 // buffer-local-option
3614 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003615
3616 // restore previous notion of curbuf
3617 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003618 }
3619 else
3620 {
3621 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003622 if (*varname == NUL)
3623 // Let getbufvar({nr}, "") return the "b:" dictionary.
3624 v = &buf->b_bufvar;
3625 else
3626 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3627 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003628 if (v != NULL)
3629 {
3630 copy_tv(&v->di_tv, rettv);
3631 done = TRUE;
3632 }
3633 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003634 }
3635
3636 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3637 // use the default value
3638 copy_tv(&argvars[2], rettv);
3639
3640 --emsg_off;
3641}
3642
3643/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003644 * "settabvar()" function
3645 */
3646 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003647f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003648{
3649 tabpage_T *save_curtab;
3650 tabpage_T *tp;
3651 char_u *varname, *tabvarname;
3652 typval_T *varp;
3653
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003654 if (check_secure())
3655 return;
3656
3657 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3658 varname = tv_get_string_chk(&argvars[1]);
3659 varp = &argvars[2];
3660
3661 if (varname != NULL && varp != NULL && tp != NULL)
3662 {
3663 save_curtab = curtab;
3664 goto_tabpage_tp(tp, FALSE, FALSE);
3665
3666 tabvarname = alloc(STRLEN(varname) + 3);
3667 if (tabvarname != NULL)
3668 {
3669 STRCPY(tabvarname, "t:");
3670 STRCPY(tabvarname + 2, varname);
3671 set_var(tabvarname, varp, TRUE);
3672 vim_free(tabvarname);
3673 }
3674
3675 // Restore current tabpage
3676 if (valid_tabpage(save_curtab))
3677 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3678 }
3679}
3680
3681/*
3682 * "settabwinvar()" function
3683 */
3684 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003685f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003686{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003687 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003688}
3689
3690/*
3691 * "setwinvar()" function
3692 */
3693 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003694f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003695{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003696 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003697}
3698
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003699/*
3700 * "setbufvar()" function
3701 */
3702 void
3703f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3704{
3705 buf_T *buf;
3706 char_u *varname, *bufvarname;
3707 typval_T *varp;
3708 char_u nbuf[NUMBUFLEN];
3709
3710 if (check_secure())
3711 return;
3712 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3713 varname = tv_get_string_chk(&argvars[1]);
3714 buf = tv_get_buf(&argvars[0], FALSE);
3715 varp = &argvars[2];
3716
3717 if (buf != NULL && varname != NULL && varp != NULL)
3718 {
3719 if (*varname == '&')
3720 {
3721 long numval;
3722 char_u *strval;
3723 int error = FALSE;
3724 aco_save_T aco;
3725
3726 // set curbuf to be our buf, temporarily
3727 aucmd_prepbuf(&aco, buf);
3728
3729 ++varname;
3730 numval = (long)tv_get_number_chk(varp, &error);
3731 strval = tv_get_string_buf_chk(varp, nbuf);
3732 if (!error && strval != NULL)
3733 set_option_value(varname, numval, strval, OPT_LOCAL);
3734
3735 // reset notion of buffer
3736 aucmd_restbuf(&aco);
3737 }
3738 else
3739 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003740 bufvarname = alloc(STRLEN(varname) + 3);
3741 if (bufvarname != NULL)
3742 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003743 buf_T *save_curbuf = curbuf;
3744
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003745 curbuf = buf;
3746 STRCPY(bufvarname, "b:");
3747 STRCPY(bufvarname + 2, varname);
3748 set_var(bufvarname, varp, TRUE);
3749 vim_free(bufvarname);
3750 curbuf = save_curbuf;
3751 }
3752 }
3753 }
3754}
3755
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003756/*
3757 * Get a callback from "arg". It can be a Funcref or a function name.
3758 * When "arg" is zero return an empty string.
3759 * "cb_name" is not allocated.
3760 * "cb_name" is set to NULL for an invalid argument.
3761 */
3762 callback_T
3763get_callback(typval_T *arg)
3764{
Bram Moolenaar14e579092020-03-07 16:59:25 +01003765 callback_T res;
3766 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003767
3768 res.cb_free_name = FALSE;
3769 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3770 {
3771 res.cb_partial = arg->vval.v_partial;
3772 ++res.cb_partial->pt_refcount;
3773 res.cb_name = partial_name(res.cb_partial);
3774 }
3775 else
3776 {
3777 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01003778 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
3779 && isdigit(*arg->vval.v_string))
3780 r = FAIL;
3781 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003782 {
3783 // Note that we don't make a copy of the string.
3784 res.cb_name = arg->vval.v_string;
3785 func_ref(res.cb_name);
3786 }
3787 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003788 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003789 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01003790 r = FAIL;
3791
3792 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003793 {
3794 emsg(_("E921: Invalid callback argument"));
3795 res.cb_name = NULL;
3796 }
3797 }
3798 return res;
3799}
3800
3801/*
3802 * Copy a callback into a typval_T.
3803 */
3804 void
3805put_callback(callback_T *cb, typval_T *tv)
3806{
3807 if (cb->cb_partial != NULL)
3808 {
3809 tv->v_type = VAR_PARTIAL;
3810 tv->vval.v_partial = cb->cb_partial;
3811 ++tv->vval.v_partial->pt_refcount;
3812 }
3813 else
3814 {
3815 tv->v_type = VAR_FUNC;
3816 tv->vval.v_string = vim_strsave(cb->cb_name);
3817 func_ref(cb->cb_name);
3818 }
3819}
3820
3821/*
3822 * Make a copy of "src" into "dest", allocating the function name if needed,
3823 * without incrementing the refcount.
3824 */
3825 void
3826set_callback(callback_T *dest, callback_T *src)
3827{
3828 if (src->cb_partial == NULL)
3829 {
3830 // just a function name, make a copy
3831 dest->cb_name = vim_strsave(src->cb_name);
3832 dest->cb_free_name = TRUE;
3833 }
3834 else
3835 {
3836 // cb_name is a pointer into cb_partial
3837 dest->cb_name = src->cb_name;
3838 dest->cb_free_name = FALSE;
3839 }
3840 dest->cb_partial = src->cb_partial;
3841}
3842
3843/*
3844 * Unref/free "callback" returned by get_callback() or set_callback().
3845 */
3846 void
3847free_callback(callback_T *callback)
3848{
3849 if (callback->cb_partial != NULL)
3850 {
3851 partial_unref(callback->cb_partial);
3852 callback->cb_partial = NULL;
3853 }
3854 else if (callback->cb_name != NULL)
3855 func_unref(callback->cb_name);
3856 if (callback->cb_free_name)
3857 {
3858 vim_free(callback->cb_name);
3859 callback->cb_free_name = FALSE;
3860 }
3861 callback->cb_name = NULL;
3862}
3863
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003864#endif // FEAT_EVAL