blob: dfc8e63f5a7aac27802b933c48468d28a9e2fbdc [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},
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100125 {VV_NAME("numbermax", VAR_NUMBER), VV_RO},
126 {VV_NAME("numbermin", VAR_NUMBER), VV_RO},
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100127 {VV_NAME("numbersize", VAR_NUMBER), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200128 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
129 {VV_NAME("testing", VAR_NUMBER), 0},
130 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
138 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
139 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
140 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
141 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
142 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
143 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
144 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
145 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
146 {VV_NAME("event", VAR_DICT), VV_RO},
147 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
148 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100149 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaar84cf6bd2020-06-16 20:03:43 +0200150 {VV_NAME("collate", VAR_STRING), VV_RO},
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100151 {VV_NAME("exiting", VAR_SPECIAL), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200152};
153
154// shorthand
155#define vv_type vv_di.di_tv.v_type
156#define vv_nr vv_di.di_tv.vval.v_number
157#define vv_float vv_di.di_tv.vval.v_float
158#define vv_str vv_di.di_tv.vval.v_string
159#define vv_list vv_di.di_tv.vval.v_list
160#define vv_dict vv_di.di_tv.vval.v_dict
161#define vv_blob vv_di.di_tv.vval.v_blob
162#define vv_tv vv_di.di_tv
163
164static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200165static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200166#define vimvarht vimvardict.dv_hashtab
167
168// for VIM_VERSION_ defines
169#include "version.h"
170
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200171static void list_glob_vars(int *first);
172static void list_buf_vars(int *first);
173static void list_win_vars(int *first);
174static void list_tab_vars(int *first);
175static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176static 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 +0200177static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
178static 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 +0200179static void list_one_var(dictitem_T *v, char *prefix, int *first);
180static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
181
182/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200183 * Initialize global and vim special variables
184 */
185 void
186evalvars_init(void)
187{
188 int i;
189 struct vimvar *p;
190
191 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
192 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
193 vimvardict.dv_lock = VAR_FIXED;
194 hash_init(&compat_hashtab);
195
196 for (i = 0; i < VV_LEN; ++i)
197 {
198 p = &vimvars[i];
199 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
200 {
201 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
202 getout(1);
203 }
204 STRCPY(p->vv_di.di_key, p->vv_name);
205 if (p->vv_flags & VV_RO)
206 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
207 else if (p->vv_flags & VV_RO_SBX)
208 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
209 else
210 p->vv_di.di_flags = DI_FLAGS_FIX;
211
212 // add to v: scope dict, unless the value is not always available
213 if (p->vv_type != VAR_UNKNOWN)
214 hash_add(&vimvarht, p->vv_di.di_key);
215 if (p->vv_flags & VV_COMPAT)
216 // add to compat scope dict
217 hash_add(&compat_hashtab, p->vv_di.di_key);
218 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200219 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
220 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200221
222 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
223 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100224 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200225 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
226 set_vim_var_list(VV_ERRORS, list_alloc());
227 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
228
229 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
230 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
231 set_vim_var_nr(VV_NONE, VVAL_NONE);
232 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100233 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
234 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100235 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200236
237 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
238 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
239 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
240 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
241 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
242 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
243 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
244 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
245 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
246 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
247 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
248
249 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
250
Bram Moolenaar439c0362020-06-06 15:58:03 +0200251 // Default for v:register is not 0 but '"'. This is adjusted once the
252 // clipboard has been setup by calling reset_reg_var().
253 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200254}
255
256#if defined(EXITFREE) || defined(PROTO)
257/*
258 * Free all vim variables information on exit
259 */
260 void
261evalvars_clear(void)
262{
263 int i;
264 struct vimvar *p;
265
266 for (i = 0; i < VV_LEN; ++i)
267 {
268 p = &vimvars[i];
269 if (p->vv_di.di_tv.v_type == VAR_STRING)
270 VIM_CLEAR(p->vv_str);
271 else if (p->vv_di.di_tv.v_type == VAR_LIST)
272 {
273 list_unref(p->vv_list);
274 p->vv_list = NULL;
275 }
276 }
277 hash_clear(&vimvarht);
278 hash_init(&vimvarht); // garbage_collect() will access it
279 hash_clear(&compat_hashtab);
280
281 // global variables
282 vars_clear(&globvarht);
283
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100284 // Script-local variables. Clear all the variables here.
285 // The scriptvar_T is cleared later in free_scriptnames(), because a
286 // variable in one script might hold a reference to the whole scope of
287 // another script.
288 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200289 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200290}
291#endif
292
293 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200294garbage_collect_globvars(int copyID)
295{
296 return set_ref_in_ht(&globvarht, copyID, NULL);
297}
298
299 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200300garbage_collect_vimvars(int copyID)
301{
302 return set_ref_in_ht(&vimvarht, copyID, NULL);
303}
304
305 int
306garbage_collect_scriptvars(int copyID)
307{
Bram Moolenaared234f22020-10-15 20:42:20 +0200308 int i;
309 int idx;
310 int abort = FALSE;
311 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100313 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200314 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200315 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
316
Bram Moolenaared234f22020-10-15 20:42:20 +0200317 si = SCRIPT_ITEM(i);
318 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
319 {
320 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
321
322 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
323 }
324 }
325
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200326 return abort;
327}
328
329/*
330 * Set an internal variable to a string value. Creates the variable if it does
331 * not already exist.
332 */
333 void
334set_internal_string_var(char_u *name, char_u *value)
335{
336 char_u *val;
337 typval_T *tvp;
338
339 val = vim_strsave(value);
340 if (val != NULL)
341 {
342 tvp = alloc_string_tv(val);
343 if (tvp != NULL)
344 {
345 set_var(name, tvp, FALSE);
346 free_tv(tvp);
347 }
348 }
349}
350
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200351 int
352eval_charconvert(
353 char_u *enc_from,
354 char_u *enc_to,
355 char_u *fname_from,
356 char_u *fname_to)
357{
358 int err = FALSE;
359
360 set_vim_var_string(VV_CC_FROM, enc_from, -1);
361 set_vim_var_string(VV_CC_TO, enc_to, -1);
362 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
363 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
364 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
365 err = TRUE;
366 set_vim_var_string(VV_CC_FROM, NULL, -1);
367 set_vim_var_string(VV_CC_TO, NULL, -1);
368 set_vim_var_string(VV_FNAME_IN, NULL, -1);
369 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
370
371 if (err)
372 return FAIL;
373 return OK;
374}
375
376# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
377 int
378eval_printexpr(char_u *fname, char_u *args)
379{
380 int err = FALSE;
381
382 set_vim_var_string(VV_FNAME_IN, fname, -1);
383 set_vim_var_string(VV_CMDARG, args, -1);
384 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
385 err = TRUE;
386 set_vim_var_string(VV_FNAME_IN, NULL, -1);
387 set_vim_var_string(VV_CMDARG, NULL, -1);
388
389 if (err)
390 {
391 mch_remove(fname);
392 return FAIL;
393 }
394 return OK;
395}
396# endif
397
398# if defined(FEAT_DIFF) || defined(PROTO)
399 void
400eval_diff(
401 char_u *origfile,
402 char_u *newfile,
403 char_u *outfile)
404{
405 int err = FALSE;
406
407 set_vim_var_string(VV_FNAME_IN, origfile, -1);
408 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
409 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
410 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
411 set_vim_var_string(VV_FNAME_IN, NULL, -1);
412 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
413 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
414}
415
416 void
417eval_patch(
418 char_u *origfile,
419 char_u *difffile,
420 char_u *outfile)
421{
422 int err;
423
424 set_vim_var_string(VV_FNAME_IN, origfile, -1);
425 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
426 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
427 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
428 set_vim_var_string(VV_FNAME_IN, NULL, -1);
429 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
430 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
431}
432# endif
433
434#if defined(FEAT_SPELL) || defined(PROTO)
435/*
436 * Evaluate an expression to a list with suggestions.
437 * For the "expr:" part of 'spellsuggest'.
438 * Returns NULL when there is an error.
439 */
440 list_T *
441eval_spell_expr(char_u *badword, char_u *expr)
442{
443 typval_T save_val;
444 typval_T rettv;
445 list_T *list = NULL;
446 char_u *p = skipwhite(expr);
447
448 // Set "v:val" to the bad word.
449 prepare_vimvar(VV_VAL, &save_val);
450 set_vim_var_string(VV_VAL, badword, -1);
451 if (p_verbose == 0)
452 ++emsg_off;
453
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200454 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200455 {
456 if (rettv.v_type != VAR_LIST)
457 clear_tv(&rettv);
458 else
459 list = rettv.vval.v_list;
460 }
461
462 if (p_verbose == 0)
463 --emsg_off;
464 clear_tv(get_vim_var_tv(VV_VAL));
465 restore_vimvar(VV_VAL, &save_val);
466
467 return list;
468}
469
470/*
471 * "list" is supposed to contain two items: a word and a number. Return the
472 * word in "pp" and the number as the return value.
473 * Return -1 if anything isn't right.
474 * Used to get the good word and score from the eval_spell_expr() result.
475 */
476 int
477get_spellword(list_T *list, char_u **pp)
478{
479 listitem_T *li;
480
481 li = list->lv_first;
482 if (li == NULL)
483 return -1;
484 *pp = tv_get_string(&li->li_tv);
485
486 li = li->li_next;
487 if (li == NULL)
488 return -1;
489 return (int)tv_get_number(&li->li_tv);
490}
491#endif
492
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200493/*
494 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200495 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200496 * When not used yet add the variable to the v: hashtable.
497 */
498 void
499prepare_vimvar(int idx, typval_T *save_tv)
500{
501 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200502 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200503 if (vimvars[idx].vv_type == VAR_UNKNOWN)
504 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
505}
506
507/*
508 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200509 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200510 * When no longer defined, remove the variable from the v: hashtable.
511 */
512 void
513restore_vimvar(int idx, typval_T *save_tv)
514{
515 hashitem_T *hi;
516
517 vimvars[idx].vv_tv = *save_tv;
518 if (vimvars[idx].vv_type == VAR_UNKNOWN)
519 {
520 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
521 if (HASHITEM_EMPTY(hi))
522 internal_error("restore_vimvar()");
523 else
524 hash_remove(&vimvarht, hi);
525 }
526}
527
528/*
529 * List Vim variables.
530 */
531 static void
532list_vim_vars(int *first)
533{
534 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
535}
536
537/*
538 * List script-local variables, if there is a script.
539 */
540 static void
541list_script_vars(int *first)
542{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200543 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200544 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
545 "s:", FALSE, first);
546}
547
548/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200549 * Get a list of lines from a HERE document. The here document is a list of
550 * lines surrounded by a marker.
551 * cmd << {marker}
552 * {line1}
553 * {line2}
554 * ....
555 * {marker}
556 *
557 * The {marker} is a string. If the optional 'trim' word is supplied before the
558 * marker, then the leading indentation before the lines (matching the
559 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200560 *
561 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
562 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
563 * missing, then '.' is accepted as a marker.
564 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200565 * Returns a List with {lines} or NULL.
566 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200568heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200569{
570 char_u *theline;
571 char_u *marker;
572 list_T *l;
573 char_u *p;
574 int marker_indent_len = 0;
575 int text_indent_len = 0;
576 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200577 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200578 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200579
580 if (eap->getline == NULL)
581 {
582 emsg(_("E991: cannot use =<< here"));
583 return NULL;
584 }
585
586 // Check for the optional 'trim' word before the marker
587 cmd = skipwhite(cmd);
588 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
589 {
590 cmd = skipwhite(cmd + 4);
591
592 // Trim the indentation from all the lines in the here document.
593 // The amount of indentation trimmed is the same as the indentation of
594 // the first line after the :let command line. To find the end marker
595 // the indent of the :let command line is trimmed.
596 p = *eap->cmdlinep;
597 while (VIM_ISWHITE(*p))
598 {
599 p++;
600 marker_indent_len++;
601 }
602 text_indent_len = -1;
603 }
604
605 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200606 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 {
608 marker = skipwhite(cmd);
609 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200610 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200611 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +0200612 semsg(_(e_trailing_arg), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200613 return NULL;
614 }
615 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200616 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200617 {
618 emsg(_("E221: Marker cannot start with lower case letter"));
619 return NULL;
620 }
621 }
622 else
623 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200624 // When getting lines for an embedded script, if the marker is missing,
625 // accept '.' as the marker.
626 if (script_get)
627 marker = dot;
628 else
629 {
630 emsg(_("E172: Missing marker"));
631 return NULL;
632 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200633 }
634
635 l = list_alloc();
636 if (l == NULL)
637 return NULL;
638
639 for (;;)
640 {
641 int mi = 0;
642 int ti = 0;
643
644 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
645 if (theline == NULL)
646 {
647 semsg(_("E990: Missing end marker '%s'"), marker);
648 break;
649 }
650
651 // with "trim": skip the indent matching the :let line to find the
652 // marker
653 if (marker_indent_len > 0
654 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
655 mi = marker_indent_len;
656 if (STRCMP(marker, theline + mi) == 0)
657 {
658 vim_free(theline);
659 break;
660 }
661
662 if (text_indent_len == -1 && *theline != NUL)
663 {
664 // set the text indent from the first line.
665 p = theline;
666 text_indent_len = 0;
667 while (VIM_ISWHITE(*p))
668 {
669 p++;
670 text_indent_len++;
671 }
672 text_indent = vim_strnsave(theline, text_indent_len);
673 }
674 // with "trim": skip the indent matching the first line
675 if (text_indent != NULL)
676 for (ti = 0; ti < text_indent_len; ++ti)
677 if (theline[ti] != text_indent[ti])
678 break;
679
680 if (list_append_string(l, theline + ti, -1) == FAIL)
681 break;
682 vim_free(theline);
683 }
684 vim_free(text_indent);
685
686 return l;
687}
688
689/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200690 * Vim9 variable declaration:
691 * ":var name"
692 * ":var name: type"
693 * ":var name = expr"
694 * ":var name: type = expr"
695 * etc.
696 */
697 void
698ex_var(exarg_T *eap)
699{
700 if (!in_vim9script())
701 {
702 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
703 return;
704 }
705 ex_let(eap);
706}
707
708/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200709 * ":let" list all variable values
710 * ":let var1 var2" list variable values
711 * ":let var = expr" assignment command.
712 * ":let var += expr" assignment command.
713 * ":let var -= expr" assignment command.
714 * ":let var *= expr" assignment command.
715 * ":let var /= expr" assignment command.
716 * ":let var %= expr" assignment command.
717 * ":let var .= expr" assignment command.
718 * ":let var ..= expr" assignment command.
719 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100721 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200722 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200723 * ":final var = expr" assignment command.
724 * ":final [var1, var2] = expr" unpack list.
725 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200726 * ":const" list all variable values
727 * ":const var1 var2" list variable values
728 * ":const var = expr" assignment command.
729 * ":const [var1, var2] = expr" unpack list.
730 */
731 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200732ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200733{
734 char_u *arg = eap->arg;
735 char_u *expr = NULL;
736 typval_T rettv;
737 int i;
738 int var_count = 0;
739 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200740 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200741 char_u *argend;
742 int first = TRUE;
743 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200744 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100745 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200746 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200747
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200748 if (eap->cmdidx == CMD_final && !vim9script)
749 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100750 // In legacy Vim script ":final" is short for ":finally".
751 ex_finally(eap);
752 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200753 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200754 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200755 {
756 emsg(_(e_cannot_use_let_in_vim9_script));
757 return;
758 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200759
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100760 if (eap->cmdidx == CMD_const)
761 flags |= ASSIGN_CONST;
762 else if (eap->cmdidx == CMD_final)
763 flags |= ASSIGN_FINAL;
764
765 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200767 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200769 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200770 if (argend == NULL)
771 return;
772 if (argend > arg && argend[-1] == '.') // for var.='str'
773 --argend;
774 expr = skipwhite(argend);
775 concat = expr[0] == '.'
776 && ((expr[1] == '=' && current_sctx.sc_version < 2)
777 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200778 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
779 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200780 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200781 {
782 // ":let" without "=": list variables
783 if (*arg == '[')
784 emsg(_(e_invarg));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200785 else if (expr[0] == '.' && expr[1] == '=')
786 emsg(_("E985: .= is not supported with script version >= 2"));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200787 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200788 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200789 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200790 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100791 // Vim9 declaration ":var name: type"
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200792 arg = vim9_declare_scriptvar(eap, arg);
793 }
794 else
795 {
796 // ":let var1 var2" - list values
797 arg = list_arg_vars(eap, arg, &first);
798 }
799 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200800 else if (!eap->skip)
801 {
802 // ":let"
803 list_glob_vars(&first);
804 list_buf_vars(&first);
805 list_win_vars(&first);
806 list_tab_vars(&first);
807 list_script_vars(&first);
808 list_func_vars(&first);
809 list_vim_vars(&first);
810 }
811 eap->nextcmd = check_nextcmd(arg);
812 }
813 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
814 {
815 list_T *l;
816
817 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200818 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200819 if (l != NULL)
820 {
821 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200822 if (!eap->skip)
823 {
824 op[0] = '=';
825 op[1] = NUL;
826 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200828 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200829 clear_tv(&rettv);
830 }
831 }
832 else
833 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200834 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200835 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200836
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200837 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200838 i = FAIL;
839 if (has_assign || concat)
840 {
841 op[0] = '=';
842 op[1] = NUL;
843 if (*expr != '=')
844 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200845 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200846 {
847 // +=, /=, etc. require an existing variable
848 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
849 i = FAIL;
850 }
851 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200852 {
853 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200854 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200855 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200856 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200857 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200858 ++len;
859 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200860 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200861 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200862 }
863 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200864 ++expr;
865
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200866 if (vim9script && (!VIM_ISWHITE(*argend)
867 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200868 {
869 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100870 semsg(_(e_white_space_required_before_and_after_str_at_str),
871 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200872 i = FAIL;
873 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200874
875 if (eap->skip)
876 ++emsg_skip;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200877 CLEAR_FIELD(evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200878 evalarg.eval_flags = eap->skip ? 0 : EVAL_EVALUATE;
Bram Moolenaard5053d02020-06-28 15:51:16 +0200879 if (getline_equal(eap->getline, eap->cookie, getsourceline))
880 {
881 evalarg.eval_getline = eap->getline;
882 evalarg.eval_cookie = eap->cookie;
883 }
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200884 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200885 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200886 if (eap->skip)
887 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200888 clear_evalarg(&evalarg, eap);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200889 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200890 if (eap->skip)
891 {
892 if (i != FAIL)
893 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200894 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200895 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200896 {
897 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200898 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200899 clear_tv(&rettv);
900 }
901 }
902}
903
904/*
905 * Assign the typevalue "tv" to the variable or variables at "arg_start".
906 * Handles both "var" with any type and "[var, var; var]" with a list type.
907 * When "op" is not NULL it points to a string with characters that
908 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
909 * or concatenate.
910 * Returns OK or FAIL;
911 */
912 int
913ex_let_vars(
914 char_u *arg_start,
915 typval_T *tv,
916 int copy, // copy values from "tv", don't move
917 int semicolon, // from skip_var_list()
918 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100919 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200920 char_u *op)
921{
922 char_u *arg = arg_start;
923 list_T *l;
924 int i;
925 listitem_T *item;
926 typval_T ltv;
927
928 if (*arg != '[')
929 {
930 // ":let var = expr" or ":for var in list"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 if (ex_let_one(arg, tv, copy, flags, op, op) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200932 return FAIL;
933 return OK;
934 }
935
936 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
937 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
938 {
939 emsg(_(e_listreq));
940 return FAIL;
941 }
942
943 i = list_len(l);
944 if (semicolon == 0 && var_count < i)
945 {
946 emsg(_("E687: Less targets than List items"));
947 return FAIL;
948 }
949 if (var_count - semicolon > i)
950 {
951 emsg(_("E688: More targets than List items"));
952 return FAIL;
953 }
954
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200955 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200956 item = l->lv_first;
957 while (*arg != ']')
958 {
959 arg = skipwhite(arg + 1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 arg = ex_let_one(arg, &item->li_tv, TRUE, flags, (char_u *)",;]", op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200961 item = item->li_next;
962 if (arg == NULL)
963 return FAIL;
964
965 arg = skipwhite(arg);
966 if (*arg == ';')
967 {
968 // Put the rest of the list (may be empty) in the var after ';'.
969 // Create a new list for this.
970 l = list_alloc();
971 if (l == NULL)
972 return FAIL;
973 while (item != NULL)
974 {
975 list_append_tv(l, &item->li_tv);
976 item = item->li_next;
977 }
978
979 ltv.v_type = VAR_LIST;
980 ltv.v_lock = 0;
981 ltv.vval.v_list = l;
982 l->lv_refcount = 1;
983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, flags,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200985 (char_u *)"]", op);
986 clear_tv(&ltv);
987 if (arg == NULL)
988 return FAIL;
989 break;
990 }
991 else if (*arg != ',' && *arg != ']')
992 {
993 internal_error("ex_let_vars()");
994 return FAIL;
995 }
996 }
997
998 return OK;
999}
1000
1001/*
1002 * Skip over assignable variable "var" or list of variables "[var, var]".
1003 * Used for ":let varvar = expr" and ":for varvar in expr".
1004 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001005 * for "[var, var; var]" set "semicolon" to 1.
1006 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001007 * Return NULL for an error.
1008 */
1009 char_u *
1010skip_var_list(
1011 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001013 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001014 int *semicolon,
1015 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001016{
1017 char_u *p, *s;
1018
1019 if (*arg == '[')
1020 {
1021 // "[var, var]": find the matching ']'.
1022 p = arg;
1023 for (;;)
1024 {
1025 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001026 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001027 if (s == p)
1028 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001029 if (!silent)
1030 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001031 return NULL;
1032 }
1033 ++*var_count;
1034
1035 p = skipwhite(s);
1036 if (*p == ']')
1037 break;
1038 else if (*p == ';')
1039 {
1040 if (*semicolon == 1)
1041 {
Bram Moolenaar9be61bb2020-03-30 22:51:24 +02001042 emsg(_("E452: Double ; in list of variables"));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001043 return NULL;
1044 }
1045 *semicolon = 1;
1046 }
1047 else if (*p != ',')
1048 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001049 if (!silent)
1050 semsg(_(e_invarg2), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051 return NULL;
1052 }
1053 }
1054 return p + 1;
1055 }
1056 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001058}
1059
1060/*
1061 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1062 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001064 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001065 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001067{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001068 char_u *end;
1069 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001071 if (*arg == '@' && arg[1] != NUL)
1072 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001074 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001075
1076 // "a: type" is declaring variable "a" with a type, not "a:".
1077 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001078 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001079 --end;
1080
Bram Moolenaar585587d2021-01-17 20:52:13 +01001081 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001083 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001084 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 }
1086 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001087}
1088
1089/*
1090 * List variables for hashtab "ht" with prefix "prefix".
1091 * If "empty" is TRUE also list NULL strings as empty strings.
1092 */
1093 void
1094list_hashtable_vars(
1095 hashtab_T *ht,
1096 char *prefix,
1097 int empty,
1098 int *first)
1099{
1100 hashitem_T *hi;
1101 dictitem_T *di;
1102 int todo;
1103 char_u buf[IOSIZE];
1104
1105 todo = (int)ht->ht_used;
1106 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1107 {
1108 if (!HASHITEM_EMPTY(hi))
1109 {
1110 --todo;
1111 di = HI2DI(hi);
1112
1113 // apply :filter /pat/ to variable name
1114 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1115 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1116 if (message_filtered(buf))
1117 continue;
1118
1119 if (empty || di->di_tv.v_type != VAR_STRING
1120 || di->di_tv.vval.v_string != NULL)
1121 list_one_var(di, prefix, first);
1122 }
1123 }
1124}
1125
1126/*
1127 * List global variables.
1128 */
1129 static void
1130list_glob_vars(int *first)
1131{
1132 list_hashtable_vars(&globvarht, "", TRUE, first);
1133}
1134
1135/*
1136 * List buffer variables.
1137 */
1138 static void
1139list_buf_vars(int *first)
1140{
1141 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1142}
1143
1144/*
1145 * List window variables.
1146 */
1147 static void
1148list_win_vars(int *first)
1149{
1150 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1151}
1152
1153/*
1154 * List tab page variables.
1155 */
1156 static void
1157list_tab_vars(int *first)
1158{
1159 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1160}
1161
1162/*
1163 * List variables in "arg".
1164 */
1165 static char_u *
1166list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1167{
1168 int error = FALSE;
1169 int len;
1170 char_u *name;
1171 char_u *name_start;
1172 char_u *arg_subsc;
1173 char_u *tofree;
1174 typval_T tv;
1175
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001176 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001177 {
1178 if (error || eap->skip)
1179 {
1180 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1181 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1182 {
1183 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001184 semsg(_(e_trailing_arg), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001185 break;
1186 }
1187 }
1188 else
1189 {
1190 // get_name_len() takes care of expanding curly braces
1191 name_start = name = arg;
1192 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1193 if (len <= 0)
1194 {
1195 // This is mainly to keep test 49 working: when expanding
1196 // curly braces fails overrule the exception error message.
1197 if (len < 0 && !aborting())
1198 {
1199 emsg_severe = TRUE;
1200 semsg(_(e_invarg2), arg);
1201 break;
1202 }
1203 error = TRUE;
1204 }
1205 else
1206 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001207 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001208 if (tofree != NULL)
1209 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001210 if (eval_variable(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001211 error = TRUE;
1212 else
1213 {
1214 // handle d.key, l[idx], f(expr)
1215 arg_subsc = arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02001216 if (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, TRUE)
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02001217 == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001218 error = TRUE;
1219 else
1220 {
1221 if (arg == arg_subsc && len == 2 && name[1] == ':')
1222 {
1223 switch (*name)
1224 {
1225 case 'g': list_glob_vars(first); break;
1226 case 'b': list_buf_vars(first); break;
1227 case 'w': list_win_vars(first); break;
1228 case 't': list_tab_vars(first); break;
1229 case 'v': list_vim_vars(first); break;
1230 case 's': list_script_vars(first); break;
1231 case 'l': list_func_vars(first); break;
1232 default:
1233 semsg(_("E738: Can't list variables for %s"), name);
1234 }
1235 }
1236 else
1237 {
1238 char_u numbuf[NUMBUFLEN];
1239 char_u *tf;
1240 int c;
1241 char_u *s;
1242
1243 s = echo_string(&tv, &tf, numbuf, 0);
1244 c = *arg;
1245 *arg = NUL;
1246 list_one_var_a("",
1247 arg == arg_subsc ? name : name_start,
1248 tv.v_type,
1249 s == NULL ? (char_u *)"" : s,
1250 first);
1251 *arg = c;
1252 vim_free(tf);
1253 }
1254 clear_tv(&tv);
1255 }
1256 }
1257 }
1258
1259 vim_free(tofree);
1260 }
1261
1262 arg = skipwhite(arg);
1263 }
1264
1265 return arg;
1266}
1267
1268/*
1269 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1270 * Returns a pointer to the char just after the var name.
1271 * Returns NULL if there is an error.
1272 */
1273 static char_u *
1274ex_let_one(
1275 char_u *arg, // points to variable name
1276 typval_T *tv, // value to assign to variable
1277 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001278 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001279 char_u *endchars, // valid chars after variable name or NULL
1280 char_u *op) // "+", "-", "." or NULL
1281{
1282 int c1;
1283 char_u *name;
1284 char_u *p;
1285 char_u *arg_end = NULL;
1286 int len;
1287 int opt_flags;
1288 char_u *tofree = NULL;
1289
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001290 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001291 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001292 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1293 {
1294 vim9_declare_error(arg);
1295 return NULL;
1296 }
1297
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001298 // ":let $VAR = expr": Set environment variable.
1299 if (*arg == '$')
1300 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001301 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001302 {
1303 emsg(_("E996: Cannot lock an environment variable"));
1304 return NULL;
1305 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02001306
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001307 // Find the end of the name.
1308 ++arg;
1309 name = arg;
1310 len = get_env_len(&arg);
1311 if (len == 0)
1312 semsg(_(e_invarg2), name - 1);
1313 else
1314 {
1315 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1316 semsg(_(e_letwrong), op);
1317 else if (endchars != NULL
1318 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1319 emsg(_(e_letunexp));
1320 else if (!check_secure())
1321 {
1322 c1 = name[len];
1323 name[len] = NUL;
1324 p = tv_get_string_chk(tv);
1325 if (p != NULL && op != NULL && *op == '.')
1326 {
1327 int mustfree = FALSE;
1328 char_u *s = vim_getenv(name, &mustfree);
1329
1330 if (s != NULL)
1331 {
1332 p = tofree = concat_str(s, p);
1333 if (mustfree)
1334 vim_free(s);
1335 }
1336 }
1337 if (p != NULL)
1338 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001339 vim_setenv_ext(name, p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001340 arg_end = arg;
1341 }
1342 name[len] = c1;
1343 vim_free(tofree);
1344 }
1345 }
1346 }
1347
1348 // ":let &option = expr": Set option value.
1349 // ":let &l:option = expr": Set local option value.
1350 // ":let &g:option = expr": Set global option value.
1351 else if (*arg == '&')
1352 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001353 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001354 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 emsg(_(e_const_option));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001356 return NULL;
1357 }
1358 // Find the end of the name.
1359 p = find_option_end(&arg, &opt_flags);
1360 if (p == NULL || (endchars != NULL
1361 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1362 emsg(_(e_letunexp));
1363 else
1364 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001365 long n = 0;
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001366 getoption_T opt_type;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001367 long numval;
1368 char_u *stringval = NULL;
Bram Moolenaar65d032c2020-04-24 20:57:01 +02001369 char_u *s = NULL;
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001370 int failed = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001371
1372 c1 = *p;
1373 *p = NUL;
1374
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001375 opt_type = get_option_value(arg, &numval, &stringval, opt_flags);
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001376 if ((opt_type == gov_bool
1377 || opt_type == gov_number
1378 || opt_type == gov_hidden_bool
1379 || opt_type == gov_hidden_number)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001380 && (tv->v_type != VAR_STRING || !in_vim9script()))
Bram Moolenaar0ea04402021-01-04 13:37:54 +01001381 {
1382 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1383 // bool, possibly hidden
1384 n = (long)tv_get_bool(tv);
1385 else
1386 // number, possibly hidden
1387 n = (long)tv_get_number(tv);
1388 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001389
1390 // Avoid setting a string option to the text "v:false" or similar.
1391 // In Vim9 script also don't convert a number to string.
1392 if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1393 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1394 s = tv_get_string_chk(tv);
1395
1396 if (op != NULL && *op != '=')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001397 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001398 if (((opt_type == gov_bool || opt_type == gov_number)
1399 && *op == '.')
1400 || (opt_type == gov_string && *op != '.'))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001401 {
1402 semsg(_(e_letwrong), op);
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001403 failed = TRUE; // don't set the value
1404
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001405 }
1406 else
1407 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001408 // number, in legacy script also bool
1409 if (opt_type == gov_number
1410 || (opt_type == gov_bool && !in_vim9script()))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001411 {
1412 switch (*op)
1413 {
1414 case '+': n = numval + n; break;
1415 case '-': n = numval - n; break;
1416 case '*': n = numval * n; break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001417 case '/': n = (long)num_divide(numval, n,
1418 &failed); break;
1419 case '%': n = (long)num_modulus(numval, n,
1420 &failed); break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001421 }
1422 }
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001423 else if (opt_type == gov_string
1424 && stringval != NULL && s != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001425 {
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001426 // string
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001427 s = concat_str(stringval, s);
1428 vim_free(stringval);
1429 stringval = s;
1430 }
1431 }
1432 }
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001433
1434 if (!failed)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001435 {
Bram Moolenaardd1f4262020-12-31 17:41:01 +01001436 if (opt_type != gov_string || s != NULL)
Bram Moolenaar0aae4802020-08-16 21:29:05 +02001437 {
1438 set_option_value(arg, n, s, opt_flags);
1439 arg_end = p;
1440 }
1441 else
1442 emsg(_(e_stringreq));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001443 }
1444 *p = c1;
1445 vim_free(stringval);
1446 }
1447 }
1448
1449 // ":let @r = expr": Set register contents.
1450 else if (*arg == '@')
1451 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001452 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001453 {
1454 emsg(_("E996: Cannot lock a register"));
1455 return NULL;
1456 }
1457 ++arg;
1458 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1459 semsg(_(e_letwrong), op);
1460 else if (endchars != NULL
1461 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1462 emsg(_(e_letunexp));
1463 else
1464 {
1465 char_u *ptofree = NULL;
1466 char_u *s;
1467
1468 p = tv_get_string_chk(tv);
1469 if (p != NULL && op != NULL && *op == '.')
1470 {
1471 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1472 if (s != NULL)
1473 {
1474 p = ptofree = concat_str(s, p);
1475 vim_free(s);
1476 }
1477 }
1478 if (p != NULL)
1479 {
1480 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1481 arg_end = arg + 1;
1482 }
1483 vim_free(ptofree);
1484 }
1485 }
1486
1487 // ":let var = expr": Set internal variable.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 // ":let var: type = expr": Set internal variable with type.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001489 // ":let {expr} = expr": Idem, name made with curly braces
1490 else if (eval_isnamec1(*arg) || *arg == '{')
1491 {
1492 lval_T lv;
1493
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001494 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001495 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1496 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001497 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001498 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 if (endchars != NULL && vim_strchr(endchars,
1500 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001501 emsg(_(e_letunexp));
1502 else
1503 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 set_var_lval(&lv, p, tv, copy, flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001505 arg_end = p;
1506 }
1507 }
1508 clear_lval(&lv);
1509 }
1510
1511 else
1512 semsg(_(e_invarg2), arg);
1513
1514 return arg_end;
1515}
1516
1517/*
1518 * ":unlet[!] var1 ... " command.
1519 */
1520 void
1521ex_unlet(exarg_T *eap)
1522{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001523 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001524}
1525
1526/*
1527 * ":lockvar" and ":unlockvar" commands
1528 */
1529 void
1530ex_lockvar(exarg_T *eap)
1531{
1532 char_u *arg = eap->arg;
1533 int deep = 2;
1534
1535 if (eap->forceit)
1536 deep = -1;
1537 else if (vim_isdigit(*arg))
1538 {
1539 deep = getdigits(&arg);
1540 arg = skipwhite(arg);
1541 }
1542
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001543 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001544}
1545
1546/*
1547 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001548 * Also used for Vim9 script. "callback" is invoked as:
1549 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001550 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001551 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001552ex_unletlock(
1553 exarg_T *eap,
1554 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001555 int deep,
1556 int glv_flags,
1557 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1558 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001559{
1560 char_u *arg = argstart;
1561 char_u *name_end;
1562 int error = FALSE;
1563 lval_T lv;
1564
1565 do
1566 {
1567 if (*arg == '$')
1568 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001569 lv.ll_name = arg;
1570 lv.ll_tv = NULL;
1571 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001572 if (get_env_len(&arg) == 0)
1573 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001574 semsg(_(e_invarg2), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001575 return;
1576 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001577 if (!error && !eap->skip
1578 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1579 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001580 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001582 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001583 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001584 // Parse the name and find the end.
1585 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001586 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001587 if (lv.ll_name == NULL)
1588 error = TRUE; // error but continue parsing
1589 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1590 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001591 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001592 if (name_end != NULL)
1593 {
1594 emsg_severe = TRUE;
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001595 semsg(_(e_trailing_arg), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001596 }
1597 if (!(eap->skip || error))
1598 clear_lval(&lv);
1599 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001600 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001601
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001602 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001603 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001604 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001605
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001606 if (!eap->skip)
1607 clear_lval(&lv);
1608 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001609
1610 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001611 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001612
1613 eap->nextcmd = check_nextcmd(arg);
1614}
1615
1616 static int
1617do_unlet_var(
1618 lval_T *lp,
1619 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001620 exarg_T *eap,
1621 int deep UNUSED,
1622 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001624 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001625 int ret = OK;
1626 int cc;
1627
1628 if (lp->ll_tv == NULL)
1629 {
1630 cc = *name_end;
1631 *name_end = NUL;
1632
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001633 // Environment variable, normal name or expanded name.
1634 if (*lp->ll_name == '$')
1635 vim_unsetenv(lp->ll_name + 1);
1636 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001637 ret = FAIL;
1638 *name_end = cc;
1639 }
1640 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001641 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001642 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001643 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001644 return FAIL;
1645 else if (lp->ll_range)
1646 {
1647 listitem_T *li;
1648 listitem_T *ll_li = lp->ll_li;
1649 int ll_n1 = lp->ll_n1;
1650
1651 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1652 {
1653 li = ll_li->li_next;
Bram Moolenaara187c432020-09-16 21:08:28 +02001654 if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001655 return FAIL;
1656 ll_li = li;
1657 ++ll_n1;
1658 }
1659
1660 // Delete a range of List items.
1661 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1662 {
1663 li = lp->ll_li->li_next;
1664 listitem_remove(lp->ll_list, lp->ll_li);
1665 lp->ll_li = li;
1666 ++lp->ll_n1;
1667 }
1668 }
1669 else
1670 {
1671 if (lp->ll_list != NULL)
1672 // unlet a List item.
1673 listitem_remove(lp->ll_list, lp->ll_li);
1674 else
1675 // unlet a Dictionary item.
1676 dictitem_remove(lp->ll_dict, lp->ll_di);
1677 }
1678
1679 return ret;
1680}
1681
1682/*
1683 * "unlet" a variable. Return OK if it existed, FAIL if not.
1684 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1685 */
1686 int
1687do_unlet(char_u *name, int forceit)
1688{
1689 hashtab_T *ht;
1690 hashitem_T *hi;
1691 char_u *varname;
1692 dict_T *d;
1693 dictitem_T *di;
1694
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001695 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001696 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001697 return FAIL;
1698
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001699 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001700
1701 // can't :unlet a script variable in Vim9 script from a function
1702 if (ht == get_script_local_ht()
1703 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1704 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1705 == SCRIPT_VERSION_VIM9
1706 && check_vim9_unlet(name) == FAIL)
1707 return FAIL;
1708
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001709 if (ht != NULL && *varname != NUL)
1710 {
1711 d = get_current_funccal_dict(ht);
1712 if (d == NULL)
1713 {
1714 if (ht == &globvarht)
1715 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001716 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001717 d = &vimvardict;
1718 else
1719 {
1720 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1721 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1722 }
1723 if (d == NULL)
1724 {
1725 internal_error("do_unlet()");
1726 return FAIL;
1727 }
1728 }
1729 hi = hash_find(ht, varname);
1730 if (HASHITEM_EMPTY(hi))
1731 hi = find_hi_in_scoped_ht(name, &ht);
1732 if (hi != NULL && !HASHITEM_EMPTY(hi))
1733 {
1734 di = HI2DI(hi);
1735 if (var_check_fixed(di->di_flags, name, FALSE)
1736 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001737 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001738 return FAIL;
1739
1740 delete_var(ht, hi);
1741 return OK;
1742 }
1743 }
1744 if (forceit)
1745 return OK;
1746 semsg(_("E108: No such variable: \"%s\""), name);
1747 return FAIL;
1748}
1749
1750/*
1751 * Lock or unlock variable indicated by "lp".
1752 * "deep" is the levels to go (-1 for unlimited);
1753 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1754 */
1755 static int
1756do_lock_var(
1757 lval_T *lp,
1758 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001759 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001761 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001762{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001763 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001764 int ret = OK;
1765 int cc;
1766 dictitem_T *di;
1767
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001768 if (lp->ll_tv == NULL)
1769 {
1770 cc = *name_end;
1771 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001772 if (*lp->ll_name == '$')
1773 {
1774 semsg(_(e_lock_unlock), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001775 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001776 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001777 else
1778 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001779 // Normal name or expanded name.
1780 di = find_var(lp->ll_name, NULL, TRUE);
1781 if (di == NULL)
1782 ret = FAIL;
1783 else if ((di->di_flags & DI_FLAGS_FIX)
1784 && di->di_tv.v_type != VAR_DICT
1785 && di->di_tv.v_type != VAR_LIST)
1786 {
1787 // For historic reasons this error is not given for a list or
1788 // dict. E.g., the b: dict could be locked/unlocked.
1789 semsg(_(e_lock_unlock), lp->ll_name);
1790 ret = FAIL;
1791 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001792 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001793 {
1794 if (lock)
1795 di->di_flags |= DI_FLAGS_LOCK;
1796 else
1797 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001798 if (deep != 0)
1799 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001800 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001801 }
1802 *name_end = cc;
1803 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001804 else if (deep == 0)
1805 {
1806 // nothing to do
1807 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001808 else if (lp->ll_range)
1809 {
1810 listitem_T *li = lp->ll_li;
1811
1812 // (un)lock a range of List items.
1813 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1814 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001815 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001816 li = li->li_next;
1817 ++lp->ll_n1;
1818 }
1819 }
1820 else if (lp->ll_list != NULL)
1821 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001822 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001823 else
1824 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001825 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001826
1827 return ret;
1828}
1829
1830/*
1831 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001832 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1833 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001834 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001835 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001836item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001837{
1838 static int recurse = 0;
1839 list_T *l;
1840 listitem_T *li;
1841 dict_T *d;
1842 blob_T *b;
1843 hashitem_T *hi;
1844 int todo;
1845
1846 if (recurse >= DICT_MAXNEST)
1847 {
1848 emsg(_("E743: variable nested too deep for (un)lock"));
1849 return;
1850 }
1851 if (deep == 0)
1852 return;
1853 ++recurse;
1854
1855 // lock/unlock the item itself
1856 if (lock)
1857 tv->v_lock |= VAR_LOCKED;
1858 else
1859 tv->v_lock &= ~VAR_LOCKED;
1860
1861 switch (tv->v_type)
1862 {
1863 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001864 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001866 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001868 case VAR_STRING:
1869 case VAR_FUNC:
1870 case VAR_PARTIAL:
1871 case VAR_FLOAT:
1872 case VAR_SPECIAL:
1873 case VAR_JOB:
1874 case VAR_CHANNEL:
1875 break;
1876
1877 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001878 if ((b = tv->vval.v_blob) != NULL
1879 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001880 {
1881 if (lock)
1882 b->bv_lock |= VAR_LOCKED;
1883 else
1884 b->bv_lock &= ~VAR_LOCKED;
1885 }
1886 break;
1887 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001888 if ((l = tv->vval.v_list) != NULL
1889 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001890 {
1891 if (lock)
1892 l->lv_lock |= VAR_LOCKED;
1893 else
1894 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01001895 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001896 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02001897 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02001898 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001899 }
1900 break;
1901 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02001902 if ((d = tv->vval.v_dict) != NULL
1903 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001904 {
1905 if (lock)
1906 d->dv_lock |= VAR_LOCKED;
1907 else
1908 d->dv_lock &= ~VAR_LOCKED;
1909 if (deep < 0 || deep > 1)
1910 {
1911 // recursive: lock/unlock the items the List contains
1912 todo = (int)d->dv_hashtab.ht_used;
1913 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1914 {
1915 if (!HASHITEM_EMPTY(hi))
1916 {
1917 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02001918 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
1919 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001920 }
1921 }
1922 }
1923 }
1924 }
1925 --recurse;
1926}
1927
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001928#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1929/*
1930 * Delete all "menutrans_" variables.
1931 */
1932 void
1933del_menutrans_vars(void)
1934{
1935 hashitem_T *hi;
1936 int todo;
1937
1938 hash_lock(&globvarht);
1939 todo = (int)globvarht.ht_used;
1940 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1941 {
1942 if (!HASHITEM_EMPTY(hi))
1943 {
1944 --todo;
1945 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1946 delete_var(&globvarht, hi);
1947 }
1948 }
1949 hash_unlock(&globvarht);
1950}
1951#endif
1952
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001953/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001954 * Local string buffer for the next two functions to store a variable name
1955 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1956 * get_user_var_name().
1957 */
1958
1959static char_u *varnamebuf = NULL;
1960static int varnamebuflen = 0;
1961
1962/*
1963 * Function to concatenate a prefix and a variable name.
1964 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01001965 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001966cat_prefix_varname(int prefix, char_u *name)
1967{
1968 int len;
1969
1970 len = (int)STRLEN(name) + 3;
1971 if (len > varnamebuflen)
1972 {
1973 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001974 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001975 varnamebuf = alloc(len);
1976 if (varnamebuf == NULL)
1977 {
1978 varnamebuflen = 0;
1979 return NULL;
1980 }
1981 varnamebuflen = len;
1982 }
1983 *varnamebuf = prefix;
1984 varnamebuf[1] = ':';
1985 STRCPY(varnamebuf + 2, name);
1986 return varnamebuf;
1987}
1988
1989/*
1990 * Function given to ExpandGeneric() to obtain the list of user defined
1991 * (global/buffer/window/built-in) variable names.
1992 */
1993 char_u *
1994get_user_var_name(expand_T *xp, int idx)
1995{
1996 static long_u gdone;
1997 static long_u bdone;
1998 static long_u wdone;
1999 static long_u tdone;
2000 static int vidx;
2001 static hashitem_T *hi;
2002 hashtab_T *ht;
2003
2004 if (idx == 0)
2005 {
2006 gdone = bdone = wdone = vidx = 0;
2007 tdone = 0;
2008 }
2009
2010 // Global variables
2011 if (gdone < globvarht.ht_used)
2012 {
2013 if (gdone++ == 0)
2014 hi = globvarht.ht_array;
2015 else
2016 ++hi;
2017 while (HASHITEM_EMPTY(hi))
2018 ++hi;
2019 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2020 return cat_prefix_varname('g', hi->hi_key);
2021 return hi->hi_key;
2022 }
2023
2024 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002025 ht =
2026#ifdef FEAT_CMDWIN
2027 // In cmdwin, the alternative buffer should be used.
2028 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2029 &prevwin->w_buffer->b_vars->dv_hashtab :
2030#endif
2031 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002032 if (bdone < ht->ht_used)
2033 {
2034 if (bdone++ == 0)
2035 hi = ht->ht_array;
2036 else
2037 ++hi;
2038 while (HASHITEM_EMPTY(hi))
2039 ++hi;
2040 return cat_prefix_varname('b', hi->hi_key);
2041 }
2042
2043 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002044 ht =
2045#ifdef FEAT_CMDWIN
2046 // In cmdwin, the alternative window should be used.
2047 (cmdwin_type != 0 && get_cmdline_type() == NUL) ?
2048 &prevwin->w_vars->dv_hashtab :
2049#endif
2050 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002051 if (wdone < ht->ht_used)
2052 {
2053 if (wdone++ == 0)
2054 hi = ht->ht_array;
2055 else
2056 ++hi;
2057 while (HASHITEM_EMPTY(hi))
2058 ++hi;
2059 return cat_prefix_varname('w', hi->hi_key);
2060 }
2061
2062 // t: variables
2063 ht = &curtab->tp_vars->dv_hashtab;
2064 if (tdone < ht->ht_used)
2065 {
2066 if (tdone++ == 0)
2067 hi = ht->ht_array;
2068 else
2069 ++hi;
2070 while (HASHITEM_EMPTY(hi))
2071 ++hi;
2072 return cat_prefix_varname('t', hi->hi_key);
2073 }
2074
2075 // v: variables
2076 if (vidx < VV_LEN)
2077 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2078
2079 VIM_CLEAR(varnamebuf);
2080 varnamebuflen = 0;
2081 return NULL;
2082}
2083
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002084 char *
2085get_var_special_name(int nr)
2086{
2087 switch (nr)
2088 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002089 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2090 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002091 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002092 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002093 }
2094 internal_error("get_var_special_name()");
2095 return "42";
2096}
2097
2098/*
2099 * Returns the global variable dictionary
2100 */
2101 dict_T *
2102get_globvar_dict(void)
2103{
2104 return &globvardict;
2105}
2106
2107/*
2108 * Returns the global variable hash table
2109 */
2110 hashtab_T *
2111get_globvar_ht(void)
2112{
2113 return &globvarht;
2114}
2115
2116/*
2117 * Returns the v: variable dictionary
2118 */
2119 dict_T *
2120get_vimvar_dict(void)
2121{
2122 return &vimvardict;
2123}
2124
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002125/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002127 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 */
2129 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002130find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002132 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2133 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134
2135 if (di == NULL)
2136 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002137 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2139 return (int)(vv - vimvars);
2140}
2141
2142
2143/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002144 * Set type of v: variable to "type".
2145 */
2146 void
2147set_vim_var_type(int idx, vartype_T type)
2148{
2149 vimvars[idx].vv_type = type;
2150}
2151
2152/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002153 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002154 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002155 */
2156 void
2157set_vim_var_nr(int idx, varnumber_T val)
2158{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002159 vimvars[idx].vv_nr = val;
2160}
2161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 char *
2163get_vim_var_name(int idx)
2164{
2165 return vimvars[idx].vv_name;
2166}
2167
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002168/*
2169 * Get typval_T v: variable value.
2170 */
2171 typval_T *
2172get_vim_var_tv(int idx)
2173{
2174 return &vimvars[idx].vv_tv;
2175}
2176
2177/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002178 * Set v: variable to "tv". Only accepts the same type.
2179 * Takes over the value of "tv".
2180 */
2181 int
2182set_vim_var_tv(int idx, typval_T *tv)
2183{
2184 if (vimvars[idx].vv_type != tv->v_type)
2185 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002186 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002187 clear_tv(tv);
2188 return FAIL;
2189 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002190 // VV_RO is also checked when compiling, but let's check here as well.
2191 if (vimvars[idx].vv_flags & VV_RO)
2192 {
2193 semsg(_(e_readonlyvar), vimvars[idx].vv_name);
2194 return FAIL;
2195 }
2196 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2197 {
2198 semsg(_(e_readonlysbx), vimvars[idx].vv_name);
2199 return FAIL;
2200 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002201 clear_tv(&vimvars[idx].vv_di.di_tv);
2202 vimvars[idx].vv_di.di_tv = *tv;
2203 return OK;
2204}
2205
2206/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002207 * Get number v: variable value.
2208 */
2209 varnumber_T
2210get_vim_var_nr(int idx)
2211{
2212 return vimvars[idx].vv_nr;
2213}
2214
2215/*
2216 * Get string v: variable value. Uses a static buffer, can only be used once.
2217 * If the String variable has never been set, return an empty string.
2218 * Never returns NULL;
2219 */
2220 char_u *
2221get_vim_var_str(int idx)
2222{
2223 return tv_get_string(&vimvars[idx].vv_tv);
2224}
2225
2226/*
2227 * Get List v: variable value. Caller must take care of reference count when
2228 * needed.
2229 */
2230 list_T *
2231get_vim_var_list(int idx)
2232{
2233 return vimvars[idx].vv_list;
2234}
2235
2236/*
2237 * Get Dict v: variable value. Caller must take care of reference count when
2238 * needed.
2239 */
2240 dict_T *
2241get_vim_var_dict(int idx)
2242{
2243 return vimvars[idx].vv_dict;
2244}
2245
2246/*
2247 * Set v:char to character "c".
2248 */
2249 void
2250set_vim_var_char(int c)
2251{
2252 char_u buf[MB_MAXBYTES + 1];
2253
2254 if (has_mbyte)
2255 buf[(*mb_char2bytes)(c, buf)] = NUL;
2256 else
2257 {
2258 buf[0] = c;
2259 buf[1] = NUL;
2260 }
2261 set_vim_var_string(VV_CHAR, buf, -1);
2262}
2263
2264/*
2265 * Set v:count to "count" and v:count1 to "count1".
2266 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2267 */
2268 void
2269set_vcount(
2270 long count,
2271 long count1,
2272 int set_prevcount)
2273{
2274 if (set_prevcount)
2275 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2276 vimvars[VV_COUNT].vv_nr = count;
2277 vimvars[VV_COUNT1].vv_nr = count1;
2278}
2279
2280/*
2281 * Save variables that might be changed as a side effect. Used when executing
2282 * a timer callback.
2283 */
2284 void
2285save_vimvars(vimvars_save_T *vvsave)
2286{
2287 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2288 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2289 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2290}
2291
2292/*
2293 * Restore variables saved by save_vimvars().
2294 */
2295 void
2296restore_vimvars(vimvars_save_T *vvsave)
2297{
2298 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2299 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2300 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2301}
2302
2303/*
2304 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2305 * value.
2306 */
2307 void
2308set_vim_var_string(
2309 int idx,
2310 char_u *val,
2311 int len) // length of "val" to use or -1 (whole string)
2312{
2313 clear_tv(&vimvars[idx].vv_di.di_tv);
2314 vimvars[idx].vv_type = VAR_STRING;
2315 if (val == NULL)
2316 vimvars[idx].vv_str = NULL;
2317 else if (len == -1)
2318 vimvars[idx].vv_str = vim_strsave(val);
2319 else
2320 vimvars[idx].vv_str = vim_strnsave(val, len);
2321}
2322
2323/*
2324 * Set List v: variable to "val".
2325 */
2326 void
2327set_vim_var_list(int idx, list_T *val)
2328{
2329 clear_tv(&vimvars[idx].vv_di.di_tv);
2330 vimvars[idx].vv_type = VAR_LIST;
2331 vimvars[idx].vv_list = val;
2332 if (val != NULL)
2333 ++val->lv_refcount;
2334}
2335
2336/*
2337 * Set Dictionary v: variable to "val".
2338 */
2339 void
2340set_vim_var_dict(int idx, dict_T *val)
2341{
2342 clear_tv(&vimvars[idx].vv_di.di_tv);
2343 vimvars[idx].vv_type = VAR_DICT;
2344 vimvars[idx].vv_dict = val;
2345 if (val != NULL)
2346 {
2347 ++val->dv_refcount;
2348 dict_set_items_ro(val);
2349 }
2350}
2351
2352/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002353 * Set the v:argv list.
2354 */
2355 void
2356set_argv_var(char **argv, int argc)
2357{
2358 list_T *l = list_alloc();
2359 int i;
2360
2361 if (l == NULL)
2362 getout(1);
2363 l->lv_lock = VAR_FIXED;
2364 for (i = 0; i < argc; ++i)
2365 {
2366 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2367 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002368 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002369 }
2370 set_vim_var_list(VV_ARGV, l);
2371}
2372
2373/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002374 * Reset v:register, taking the 'clipboard' setting into account.
2375 */
2376 void
2377reset_reg_var(void)
2378{
2379 int regname = 0;
2380
2381 // Adjust the register according to 'clipboard', so that when
2382 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2383#ifdef FEAT_CLIPBOARD
2384 adjust_clip_reg(&regname);
2385#endif
2386 set_reg_var(regname);
2387}
2388
2389/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002390 * Set v:register if needed.
2391 */
2392 void
2393set_reg_var(int c)
2394{
2395 char_u regname;
2396
2397 if (c == 0 || c == ' ')
2398 regname = '"';
2399 else
2400 regname = c;
2401 // Avoid free/alloc when the value is already right.
2402 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2403 set_vim_var_string(VV_REG, &regname, 1);
2404}
2405
2406/*
2407 * Get or set v:exception. If "oldval" == NULL, return the current value.
2408 * Otherwise, restore the value to "oldval" and return NULL.
2409 * Must always be called in pairs to save and restore v:exception! Does not
2410 * take care of memory allocations.
2411 */
2412 char_u *
2413v_exception(char_u *oldval)
2414{
2415 if (oldval == NULL)
2416 return vimvars[VV_EXCEPTION].vv_str;
2417
2418 vimvars[VV_EXCEPTION].vv_str = oldval;
2419 return NULL;
2420}
2421
2422/*
2423 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2424 * Otherwise, restore the value to "oldval" and return NULL.
2425 * Must always be called in pairs to save and restore v:throwpoint! Does not
2426 * take care of memory allocations.
2427 */
2428 char_u *
2429v_throwpoint(char_u *oldval)
2430{
2431 if (oldval == NULL)
2432 return vimvars[VV_THROWPOINT].vv_str;
2433
2434 vimvars[VV_THROWPOINT].vv_str = oldval;
2435 return NULL;
2436}
2437
2438/*
2439 * Set v:cmdarg.
2440 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2441 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2442 * Must always be called in pairs!
2443 */
2444 char_u *
2445set_cmdarg(exarg_T *eap, char_u *oldarg)
2446{
2447 char_u *oldval;
2448 char_u *newval;
2449 unsigned len;
2450
2451 oldval = vimvars[VV_CMDARG].vv_str;
2452 if (eap == NULL)
2453 {
2454 vim_free(oldval);
2455 vimvars[VV_CMDARG].vv_str = oldarg;
2456 return NULL;
2457 }
2458
2459 if (eap->force_bin == FORCE_BIN)
2460 len = 6;
2461 else if (eap->force_bin == FORCE_NOBIN)
2462 len = 8;
2463 else
2464 len = 0;
2465
2466 if (eap->read_edit)
2467 len += 7;
2468
2469 if (eap->force_ff != 0)
2470 len += 10; // " ++ff=unix"
2471 if (eap->force_enc != 0)
2472 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2473 if (eap->bad_char != 0)
2474 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2475
2476 newval = alloc(len + 1);
2477 if (newval == NULL)
2478 return NULL;
2479
2480 if (eap->force_bin == FORCE_BIN)
2481 sprintf((char *)newval, " ++bin");
2482 else if (eap->force_bin == FORCE_NOBIN)
2483 sprintf((char *)newval, " ++nobin");
2484 else
2485 *newval = NUL;
2486
2487 if (eap->read_edit)
2488 STRCAT(newval, " ++edit");
2489
2490 if (eap->force_ff != 0)
2491 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2492 eap->force_ff == 'u' ? "unix"
2493 : eap->force_ff == 'd' ? "dos"
2494 : "mac");
2495 if (eap->force_enc != 0)
2496 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2497 eap->cmd + eap->force_enc);
2498 if (eap->bad_char == BAD_KEEP)
2499 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2500 else if (eap->bad_char == BAD_DROP)
2501 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2502 else if (eap->bad_char != 0)
2503 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2504 vimvars[VV_CMDARG].vv_str = newval;
2505 return oldval;
2506}
2507
2508/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002509 * Get the value of internal variable "name".
2510 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2511 */
2512 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002513eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002514 char_u *name,
2515 int len, // length of "name"
2516 typval_T *rettv, // NULL when only checking existence
2517 dictitem_T **dip, // non-NULL when typval's dict item is needed
2518 int verbose, // may give error message
2519 int no_autoload) // do not use script autoloading
2520{
2521 int ret = OK;
2522 typval_T *tv = NULL;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002523 int foundFunc = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002524 dictitem_T *v;
2525 int cc;
2526
2527 // truncate the name, so that we can use strcmp()
2528 cc = name[len];
2529 name[len] = NUL;
2530
2531 // Check for user-defined variables.
2532 v = find_var(name, NULL, no_autoload);
2533 if (v != NULL)
2534 {
2535 tv = &v->di_tv;
2536 if (dip != NULL)
2537 *dip = v;
2538 }
2539
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002540 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 {
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002542 imported_T *import;
2543 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2544
2545 import = find_imported(p, 0, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546
2547 // imported variable from another script
2548 if (import != NULL)
2549 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002550 if (import->imp_funcname != NULL)
2551 {
2552 foundFunc = TRUE;
2553 if (rettv != NULL)
2554 {
2555 rettv->v_type = VAR_FUNC;
2556 rettv->vval.v_string = vim_strsave(import->imp_funcname);
2557 }
2558 }
Bram Moolenaara6294952020-12-27 13:39:50 +01002559 else if (import->imp_flags & IMP_FLAGS_STAR)
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002560 {
2561 emsg("Sorry, 'import * as X' not implemented yet");
2562 ret = FAIL;
2563 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002564 else
2565 {
2566 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02002567 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 + import->imp_var_vals_idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002569 tv = sv->sv_tv;
2570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 }
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002572 else if (in_vim9script())
2573 {
2574 ufunc_T *ufunc = find_func(name, FALSE, NULL);
2575
2576 if (ufunc != NULL)
2577 {
2578 foundFunc = TRUE;
2579 if (rettv != NULL)
2580 {
2581 rettv->v_type = VAR_FUNC;
2582 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
2583 }
2584 }
2585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 }
2587
Bram Moolenaarc620c052020-07-08 15:16:19 +02002588 if (!foundFunc)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002589 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002590 if (tv == NULL)
2591 {
2592 if (rettv != NULL && verbose)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002593 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002594 ret = FAIL;
2595 }
2596 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002597 {
2598 // If a list or dict variable wasn't initialized, do it now.
2599 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2600 {
2601 tv->vval.v_dict = dict_alloc();
2602 if (tv->vval.v_dict != NULL)
2603 ++tv->vval.v_dict->dv_refcount;
2604 }
2605 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2606 {
2607 tv->vval.v_list = list_alloc();
2608 if (tv->vval.v_list != NULL)
2609 ++tv->vval.v_list->lv_refcount;
2610 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002611 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002612 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002613 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002614
2615 name[len] = cc;
2616
2617 return ret;
2618}
2619
2620/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002621 * Check if variable "name[len]" is a local variable or an argument.
2622 * If so, "*eval_lavars_used" is set to TRUE.
2623 */
2624 void
2625check_vars(char_u *name, int len)
2626{
2627 int cc;
2628 char_u *varname;
2629 hashtab_T *ht;
2630
2631 if (eval_lavars_used == NULL)
2632 return;
2633
2634 // truncate the name, so that we can use strcmp()
2635 cc = name[len];
2636 name[len] = NUL;
2637
2638 ht = find_var_ht(name, &varname);
2639 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2640 {
2641 if (find_var(name, NULL, TRUE) != NULL)
2642 *eval_lavars_used = TRUE;
2643 }
2644
2645 name[len] = cc;
2646}
2647
2648/*
2649 * Find variable "name" in the list of variables.
2650 * Return a pointer to it if found, NULL if not found.
2651 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002652 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002653 */
2654 dictitem_T *
2655find_var(char_u *name, hashtab_T **htp, int no_autoload)
2656{
2657 char_u *varname;
2658 hashtab_T *ht;
2659 dictitem_T *ret = NULL;
2660
2661 ht = find_var_ht(name, &varname);
2662 if (htp != NULL)
2663 *htp = ht;
2664 if (ht == NULL)
2665 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002666 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002667 if (ret != NULL)
2668 return ret;
2669
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002670 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002671 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002672 if (ret != NULL)
2673 return ret;
2674
2675 // in Vim9 script items without a scope can be script-local
2676 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2677 {
2678 ht = get_script_local_ht();
2679 if (ht != NULL)
2680 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002681 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002682 if (ret != NULL)
2683 {
2684 if (htp != NULL)
2685 *htp = ht;
2686 return ret;
2687 }
2688 }
2689 }
2690
2691 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002692}
2693
2694/*
2695 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002696 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002697 * Returns NULL if not found.
2698 */
2699 dictitem_T *
2700find_var_in_ht(
2701 hashtab_T *ht,
2702 int htname,
2703 char_u *varname,
2704 int no_autoload)
2705{
2706 hashitem_T *hi;
2707
2708 if (*varname == NUL)
2709 {
2710 // Must be something like "s:", otherwise "ht" would be NULL.
2711 switch (htname)
2712 {
2713 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2714 case 'g': return &globvars_var;
2715 case 'v': return &vimvars_var;
2716 case 'b': return &curbuf->b_bufvar;
2717 case 'w': return &curwin->w_winvar;
2718 case 't': return &curtab->tp_winvar;
2719 case 'l': return get_funccal_local_var();
2720 case 'a': return get_funccal_args_var();
2721 }
2722 return NULL;
2723 }
2724
2725 hi = hash_find(ht, varname);
2726 if (HASHITEM_EMPTY(hi))
2727 {
2728 // For global variables we may try auto-loading the script. If it
2729 // worked find the variable again. Don't auto-load a script if it was
2730 // loaded already, otherwise it would be loaded every time when
2731 // checking if a function name is a Funcref variable.
2732 if (ht == &globvarht && !no_autoload)
2733 {
2734 // Note: script_autoload() may make "hi" invalid. It must either
2735 // be obtained again or not used.
2736 if (!script_autoload(varname, FALSE) || aborting())
2737 return NULL;
2738 hi = hash_find(ht, varname);
2739 }
2740 if (HASHITEM_EMPTY(hi))
2741 return NULL;
2742 }
2743 return HI2DI(hi);
2744}
2745
2746/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 * Get the script-local hashtab. NULL if not in a script context.
2748 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02002749 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750get_script_local_ht(void)
2751{
2752 scid_T sid = current_sctx.sc_sid;
2753
Bram Moolenaare3d46852020-08-29 13:39:17 +02002754 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 return &SCRIPT_VARS(sid);
2756 return NULL;
2757}
2758
2759/*
2760 * Look for "name[len]" in script-local variables.
Bram Moolenaar709664c2020-12-12 14:33:41 +01002761 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01002763 int
2764lookup_scriptvar(
2765 char_u *name,
2766 size_t len,
2767 void *lvar UNUSED,
2768 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769{
2770 hashtab_T *ht = get_script_local_ht();
2771 char_u buffer[30];
2772 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01002773 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002774 hashitem_T *hi;
2775
2776 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002777 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 if (len < sizeof(buffer) - 1)
2779 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02002780 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 vim_strncpy(buffer, name, len);
2782 p = buffer;
2783 }
2784 else
2785 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02002786 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01002788 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790
2791 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002792 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793
2794 // if not script-local, then perhaps imported
Bram Moolenaar709664c2020-12-12 14:33:41 +01002795 if (res == FAIL && find_imported(p, 0, NULL) != NULL)
2796 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797
2798 if (p != buffer)
2799 vim_free(p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01002800 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801}
2802
2803/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002804 * Find the hashtab used for a variable name.
2805 * Return NULL if the name is not valid.
2806 * Set "varname" to the start of name without ':'.
2807 */
2808 hashtab_T *
2809find_var_ht(char_u *name, char_u **varname)
2810{
2811 hashitem_T *hi;
2812 hashtab_T *ht;
2813
2814 if (name[0] == NUL)
2815 return NULL;
2816 if (name[1] != ':')
2817 {
2818 // The name must not start with a colon or #.
2819 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2820 return NULL;
2821 *varname = name;
2822
2823 // "version" is "v:version" in all scopes if scriptversion < 3.
2824 // Same for a few other variables marked with VV_COMPAT.
2825 if (current_sctx.sc_version < 3)
2826 {
2827 hi = hash_find(&compat_hashtab, name);
2828 if (!HASHITEM_EMPTY(hi))
2829 return &compat_hashtab;
2830 }
2831
2832 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002833 if (ht != NULL)
2834 return ht; // local variable
2835
2836 // in Vim9 script items at the script level are script-local
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002837 if (in_vim9script())
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 {
2839 ht = get_script_local_ht();
2840 if (ht != NULL)
2841 return ht;
2842 }
2843
2844 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002845 }
2846 *varname = name + 2;
2847 if (*name == 'g') // global variable
2848 return &globvarht;
2849 // There must be no ':' or '#' in the rest of the name, unless g: is used
2850 if (vim_strchr(name + 2, ':') != NULL
2851 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2852 return NULL;
2853 if (*name == 'b') // buffer variable
2854 return &curbuf->b_vars->dv_hashtab;
2855 if (*name == 'w') // window variable
2856 return &curwin->w_vars->dv_hashtab;
2857 if (*name == 't') // tab page variable
2858 return &curtab->tp_vars->dv_hashtab;
2859 if (*name == 'v') // v: variable
2860 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002861 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002862 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01002864 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 if (*name == 'a') // a: function argument
2866 return get_funccal_args_ht();
2867 if (*name == 'l') // l: local function variable
2868 return get_funccal_local_ht();
2869 }
2870 if (*name == 's') // script variable
2871 {
2872 ht = get_script_local_ht();
2873 if (ht != NULL)
2874 return ht;
2875 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002876 return NULL;
2877}
2878
2879/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002880 * Get the string value of a (global/local) variable.
2881 * Note: see tv_get_string() for how long the pointer remains valid.
2882 * Returns NULL when it doesn't exist.
2883 */
2884 char_u *
2885get_var_value(char_u *name)
2886{
2887 dictitem_T *v;
2888
2889 v = find_var(name, NULL, FALSE);
2890 if (v == NULL)
2891 return NULL;
2892 return tv_get_string(&v->di_tv);
2893}
2894
2895/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002896 * Allocate a new hashtab for a sourced script. It will be used while
2897 * sourcing this script and when executing functions defined in the script.
2898 */
2899 void
2900new_script_vars(scid_T id)
2901{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002902 scriptvar_T *sv;
2903
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002904 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2905 if (sv == NULL)
2906 return;
2907 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002908 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002909}
2910
2911/*
2912 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2913 * point to it.
2914 */
2915 void
2916init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2917{
2918 hash_init(&dict->dv_hashtab);
2919 dict->dv_lock = 0;
2920 dict->dv_scope = scope;
2921 dict->dv_refcount = DO_NOT_FREE_CNT;
2922 dict->dv_copyID = 0;
2923 dict_var->di_tv.vval.v_dict = dict;
2924 dict_var->di_tv.v_type = VAR_DICT;
2925 dict_var->di_tv.v_lock = VAR_FIXED;
2926 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2927 dict_var->di_key[0] = NUL;
2928}
2929
2930/*
2931 * Unreference a dictionary initialized by init_var_dict().
2932 */
2933 void
2934unref_var_dict(dict_T *dict)
2935{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002936 // Now the dict needs to be freed if no one else is using it, go back to
2937 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002938 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2939 dict_unref(dict);
2940}
2941
2942/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002943 * Clean up a list of internal variables.
2944 * Frees all allocated variables and the value they contain.
2945 * Clears hashtab "ht", does not free it.
2946 */
2947 void
2948vars_clear(hashtab_T *ht)
2949{
2950 vars_clear_ext(ht, TRUE);
2951}
2952
2953/*
2954 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2955 */
2956 void
2957vars_clear_ext(hashtab_T *ht, int free_val)
2958{
2959 int todo;
2960 hashitem_T *hi;
2961 dictitem_T *v;
2962
2963 hash_lock(ht);
2964 todo = (int)ht->ht_used;
2965 for (hi = ht->ht_array; todo > 0; ++hi)
2966 {
2967 if (!HASHITEM_EMPTY(hi))
2968 {
2969 --todo;
2970
2971 // Free the variable. Don't remove it from the hashtab,
2972 // ht_array might change then. hash_clear() takes care of it
2973 // later.
2974 v = HI2DI(hi);
2975 if (free_val)
2976 clear_tv(&v->di_tv);
2977 if (v->di_flags & DI_FLAGS_ALLOC)
2978 vim_free(v);
2979 }
2980 }
2981 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02002982 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002983}
2984
2985/*
2986 * Delete a variable from hashtab "ht" at item "hi".
2987 * Clear the variable value and free the dictitem.
2988 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02002989 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002990delete_var(hashtab_T *ht, hashitem_T *hi)
2991{
2992 dictitem_T *di = HI2DI(hi);
2993
2994 hash_remove(ht, hi);
2995 clear_tv(&di->di_tv);
2996 vim_free(di);
2997}
2998
2999/*
3000 * List the value of one internal variable.
3001 */
3002 static void
3003list_one_var(dictitem_T *v, char *prefix, int *first)
3004{
3005 char_u *tofree;
3006 char_u *s;
3007 char_u numbuf[NUMBUFLEN];
3008
3009 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3010 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3011 s == NULL ? (char_u *)"" : s, first);
3012 vim_free(tofree);
3013}
3014
3015 static void
3016list_one_var_a(
3017 char *prefix,
3018 char_u *name,
3019 int type,
3020 char_u *string,
3021 int *first) // when TRUE clear rest of screen and set to FALSE
3022{
3023 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3024 msg_start();
3025 msg_puts(prefix);
3026 if (name != NULL) // "a:" vars don't have a name stored
3027 msg_puts((char *)name);
3028 msg_putchar(' ');
3029 msg_advance(22);
3030 if (type == VAR_NUMBER)
3031 msg_putchar('#');
3032 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3033 msg_putchar('*');
3034 else if (type == VAR_LIST)
3035 {
3036 msg_putchar('[');
3037 if (*string == '[')
3038 ++string;
3039 }
3040 else if (type == VAR_DICT)
3041 {
3042 msg_putchar('{');
3043 if (*string == '{')
3044 ++string;
3045 }
3046 else
3047 msg_putchar(' ');
3048
3049 msg_outtrans(string);
3050
3051 if (type == VAR_FUNC || type == VAR_PARTIAL)
3052 msg_puts("()");
3053 if (*first)
3054 {
3055 msg_clr_eos();
3056 *first = FALSE;
3057 }
3058}
3059
3060/*
3061 * Set variable "name" to value in "tv".
3062 * If the variable already exists, the value is updated.
3063 * Otherwise the variable is created.
3064 */
3065 void
3066set_var(
3067 char_u *name,
3068 typval_T *tv,
3069 int copy) // make copy of value in "tv"
3070{
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003071 set_var_const(name, NULL, tv, copy, ASSIGN_DECL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003072}
3073
3074/*
3075 * Set variable "name" to value in "tv".
3076 * If the variable already exists and "is_const" is FALSE the value is updated.
3077 * Otherwise the variable is created.
3078 */
3079 void
3080set_var_const(
3081 char_u *name,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 type_T *type,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003083 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003084 int copy, // make copy of value in "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003085 int flags) // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003086{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003087 typval_T *tv = tv_arg;
3088 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 dictitem_T *di;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003090 char_u *varname;
3091 hashtab_T *ht;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003093 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003094
3095 ht = find_var_ht(name, &varname);
3096 if (ht == NULL || *varname == NUL)
3097 {
3098 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003099 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003100 }
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003101 is_script_local = ht == get_script_local_ht();
3102
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003103 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003104 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003105 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003106 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003107 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003108 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003109 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003110 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003111 }
3112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003114
3115 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 if (di == NULL)
3117 di = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003118
3119 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003120 && var_wrong_func_name(name, di == NULL))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003121 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003122
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003123 if (need_convert_to_bool(type, tv))
3124 {
3125 // Destination is a bool and the value is not, but it can be converted.
3126 CLEAR_FIELD(bool_tv);
3127 bool_tv.v_type = VAR_BOOL;
3128 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3129 tv = &bool_tv;
3130 }
3131
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 if (di != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003133 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003135 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003136 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137 {
3138 emsg(_(e_cannot_mod));
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003139 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 }
3141
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003142 if (is_script_local && vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003144 if ((flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003145 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003146 semsg(_(e_redefining_script_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003147 goto failed;
Bram Moolenaar34db91f2020-06-13 19:00:10 +02003148 }
3149
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003150 // check the type and adjust to bool if needed
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003151 if (check_script_var_type(&di->di_tv, tv, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003152 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 }
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003154
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003155 if (var_check_permission(di, name) == FAIL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003156 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003157 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 else
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003159 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 // can only redefine once
3161 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003162
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003163 // A Vim9 script-local variable is also present in sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003164 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003165 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003166 update_vim9_script_var(FALSE, di, flags, tv, &type);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003167 }
3168
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003169 // existing variable, need to clear the value
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 // Handle setting internal di: variables separately where needed to
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003172 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003173 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003174 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 if (di->di_tv.v_type == VAR_STRING)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003176 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 VIM_CLEAR(di->di_tv.vval.v_string);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003178 if (copy || tv->v_type != VAR_STRING)
3179 {
3180 char_u *val = tv_get_string(tv);
3181
3182 // Careful: when assigning to v:errmsg and tv_get_string()
Bram Moolenaar4b96df52020-01-26 22:00:26 +01003183 // causes an error message the variable will already be set.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 if (di->di_tv.vval.v_string == NULL)
3185 di->di_tv.vval.v_string = vim_strsave(val);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003186 }
3187 else
3188 {
3189 // Take over the string to avoid an extra alloc/free.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 di->di_tv.vval.v_string = tv->vval.v_string;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003191 tv->vval.v_string = NULL;
3192 }
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003193 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 else if (di->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003196 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 di->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003198 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003200#ifdef FEAT_SEARCH_EXTRA
3201 else if (STRCMP(varname, "hlsearch") == 0)
3202 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 no_hlsearch = !di->di_tv.vval.v_number;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003204 redraw_all_later(SOME_VALID);
3205 }
3206#endif
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003207 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003208 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 else if (di->di_tv.v_type != tv->v_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003210 {
3211 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003212 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003213 }
3214 }
3215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 clear_tv(&di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003217 }
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003218 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003219 {
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003220 // add a new variable
3221 if (vim9script && is_script_local && (flags & ASSIGN_NO_DECL))
3222 {
3223 semsg(_(e_unknown_variable_str), name);
3224 goto failed;
3225 }
3226
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003227 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003228 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003229 {
3230 semsg(_(e_illvar), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003231 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003232 }
3233
Bram Moolenaar03290b82020-12-19 16:30:44 +01003234 // Make sure the variable name is valid. In Vim9 script an autoload
3235 // variable must be prefixed with "g:".
3236 if (!valid_varname(varname, !vim9script
3237 || STRNCMP(name, "g:", 2) == 0))
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003238 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003239
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3241 if (di == NULL)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003242 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 STRCPY(di->di_key, varname);
3244 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003245 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 vim_free(di);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003247 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003250 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 di->di_flags |= DI_FLAGS_LOCK;
3252
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003253 // A Vim9 script-local variable is also added to sn_all_vars and
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003254 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003255 if (is_script_local && vim9script)
Bram Moolenaar08251752021-01-11 21:20:18 +01003256 update_vim9_script_var(TRUE, di, flags, tv, &type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003257 }
3258
3259 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 copy_tv(tv, &di->di_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003261 else
3262 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 di->di_tv = *tv;
3264 di->di_tv.v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003265 init_tv(tv);
3266 }
3267
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003268 if (vim9script && type != NULL)
3269 {
3270 if (type->tt_type == VAR_DICT && di->di_tv.vval.v_dict != NULL)
3271 di->di_tv.vval.v_dict->dv_type = alloc_type(type);
3272 else if (type->tt_type == VAR_LIST && di->di_tv.vval.v_list != NULL)
3273 di->di_tv.vval.v_list->lv_type = alloc_type(type);
3274 }
3275
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003276 // ":const var = value" locks the value
3277 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003278 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003279 // Like :lockvar! name: lock the value and what it contains, but only
3280 // if the reference count is up to one. That locks only literal
3281 // values.
3282 item_lock(&di->di_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003283 return;
3284failed:
3285 if (!copy)
3286 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003287}
3288
3289/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003290 * Check in this order for backwards compatibility:
3291 * - Whether the variable is read-only
3292 * - Whether the variable value is locked
3293 * - Whether the variable is locked
3294 */
3295 int
3296var_check_permission(dictitem_T *di, char_u *name)
3297{
3298 if (var_check_ro(di->di_flags, name, FALSE)
3299 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3300 || var_check_lock(di->di_flags, name, FALSE))
3301 return FAIL;
3302 return OK;
3303}
3304
3305/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003306 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3307 * Also give an error message.
3308 */
3309 int
3310var_check_ro(int flags, char_u *name, int use_gettext)
3311{
3312 if (flags & DI_FLAGS_RO)
3313 {
3314 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
3315 return TRUE;
3316 }
3317 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3318 {
3319 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
3320 return TRUE;
3321 }
3322 return FALSE;
3323}
3324
3325/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003326 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3327 * Also give an error message.
3328 */
3329 int
3330var_check_lock(int flags, char_u *name, int use_gettext)
3331{
3332 if (flags & DI_FLAGS_LOCK)
3333 {
3334 semsg(_(e_variable_is_locked_str),
3335 use_gettext ? (char_u *)_(name) : name);
3336 return TRUE;
3337 }
3338 return FALSE;
3339}
3340
3341/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003342 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3343 * Also give an error message.
3344 */
3345 int
3346var_check_fixed(int flags, char_u *name, int use_gettext)
3347{
3348 if (flags & DI_FLAGS_FIX)
3349 {
3350 semsg(_("E795: Cannot delete variable %s"),
3351 use_gettext ? (char_u *)_(name) : name);
3352 return TRUE;
3353 }
3354 return FALSE;
3355}
3356
3357/*
3358 * Check if a funcref is assigned to a valid variable name.
3359 * Return TRUE and give an error if not.
3360 */
3361 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003362var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003363 char_u *name, // points to start of variable name
3364 int new_var) // TRUE when creating the variable
3365{
3366 // Allow for w: b: s: and t:.
3367 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
3368 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3369 ? name[2] : name[0]))
3370 {
3371 semsg(_("E704: Funcref variable name must start with a capital: %s"),
3372 name);
3373 return TRUE;
3374 }
3375 // Don't allow hiding a function. When "v" is not NULL we might be
3376 // assigning another function to the same var, the type is checked
3377 // below.
3378 if (new_var && function_exists(name, FALSE))
3379 {
3380 semsg(_("E705: Variable name conflicts with existing function: %s"),
3381 name);
3382 return TRUE;
3383 }
3384 return FALSE;
3385}
3386
3387/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003388 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3389 * value. Also give an error message, using "name" or _("name") when
3390 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003391 */
3392 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003393value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003394{
3395 if (lock & VAR_LOCKED)
3396 {
3397 semsg(_("E741: Value is locked: %s"),
3398 name == NULL ? (char_u *)_("Unknown")
3399 : use_gettext ? (char_u *)_(name)
3400 : name);
3401 return TRUE;
3402 }
3403 if (lock & VAR_FIXED)
3404 {
3405 semsg(_("E742: Cannot change value of %s"),
3406 name == NULL ? (char_u *)_("Unknown")
3407 : use_gettext ? (char_u *)_(name)
3408 : name);
3409 return TRUE;
3410 }
3411 return FALSE;
3412}
3413
3414/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003415 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003416 * Return FALSE and give an error if not.
3417 */
3418 int
Bram Moolenaar03290b82020-12-19 16:30:44 +01003419valid_varname(char_u *varname, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003420{
3421 char_u *p;
3422
3423 for (p = varname; *p != NUL; ++p)
3424 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003425 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003426 {
3427 semsg(_(e_illvar), varname);
3428 return FALSE;
3429 }
3430 return TRUE;
3431}
3432
3433/*
3434 * getwinvar() and gettabwinvar()
3435 */
3436 static void
3437getwinvar(
3438 typval_T *argvars,
3439 typval_T *rettv,
3440 int off) // 1 for gettabwinvar()
3441{
3442 win_T *win;
3443 char_u *varname;
3444 dictitem_T *v;
3445 tabpage_T *tp = NULL;
3446 int done = FALSE;
3447 win_T *oldcurwin;
3448 tabpage_T *oldtabpage;
3449 int need_switch_win;
3450
3451 if (off == 1)
3452 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3453 else
3454 tp = curtab;
3455 win = find_win_by_nr(&argvars[off], tp);
3456 varname = tv_get_string_chk(&argvars[off + 1]);
3457 ++emsg_off;
3458
3459 rettv->v_type = VAR_STRING;
3460 rettv->vval.v_string = NULL;
3461
3462 if (win != NULL && varname != NULL)
3463 {
3464 // Set curwin to be our win, temporarily. Also set the tabpage,
3465 // otherwise the window is not valid. Only do this when needed,
3466 // autocommands get blocked.
3467 need_switch_win = !(tp == curtab && win == curwin);
3468 if (!need_switch_win
3469 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
3470 {
3471 if (*varname == '&')
3472 {
3473 if (varname[1] == NUL)
3474 {
3475 // get all window-local options in a dict
3476 dict_T *opts = get_winbuf_options(FALSE);
3477
3478 if (opts != NULL)
3479 {
3480 rettv_dict_set(rettv, opts);
3481 done = TRUE;
3482 }
3483 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003484 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003485 // window-local-option
3486 done = TRUE;
3487 }
3488 else
3489 {
3490 // Look up the variable.
3491 // Let getwinvar({nr}, "") return the "w:" dictionary.
3492 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3493 varname, FALSE);
3494 if (v != NULL)
3495 {
3496 copy_tv(&v->di_tv, rettv);
3497 done = TRUE;
3498 }
3499 }
3500 }
3501
3502 if (need_switch_win)
3503 // restore previous notion of curwin
3504 restore_win(oldcurwin, oldtabpage, TRUE);
3505 }
3506
3507 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3508 // use the default return value
3509 copy_tv(&argvars[off + 2], rettv);
3510
3511 --emsg_off;
3512}
3513
3514/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003515 * Set option "varname" to the value of "varp" for the current buffer/window.
3516 */
3517 static void
3518set_option_from_tv(char_u *varname, typval_T *varp)
3519{
3520 long numval = 0;
3521 char_u *strval;
3522 char_u nbuf[NUMBUFLEN];
3523 int error = FALSE;
3524
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003525 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003526 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003527 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003528 strval = (char_u *)"0"; // avoid using "false"
3529 }
3530 else
3531 {
3532 if (!in_vim9script() || varp->v_type != VAR_STRING)
3533 numval = (long)tv_get_number_chk(varp, &error);
3534 strval = tv_get_string_buf_chk(varp, nbuf);
3535 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003536 if (!error && strval != NULL)
3537 set_option_value(varname, numval, strval, OPT_LOCAL);
3538}
3539
3540/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003541 * "setwinvar()" and "settabwinvar()" functions
3542 */
3543 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003544setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003545{
3546 win_T *win;
3547 win_T *save_curwin;
3548 tabpage_T *save_curtab;
3549 int need_switch_win;
3550 char_u *varname, *winvarname;
3551 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003552 tabpage_T *tp = NULL;
3553
3554 if (check_secure())
3555 return;
3556
3557 if (off == 1)
3558 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3559 else
3560 tp = curtab;
3561 win = find_win_by_nr(&argvars[off], tp);
3562 varname = tv_get_string_chk(&argvars[off + 1]);
3563 varp = &argvars[off + 2];
3564
3565 if (win != NULL && varname != NULL && varp != NULL)
3566 {
3567 need_switch_win = !(tp == curtab && win == curwin);
3568 if (!need_switch_win
3569 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
3570 {
3571 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003572 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003573 else
3574 {
3575 winvarname = alloc(STRLEN(varname) + 3);
3576 if (winvarname != NULL)
3577 {
3578 STRCPY(winvarname, "w:");
3579 STRCPY(winvarname + 2, varname);
3580 set_var(winvarname, varp, TRUE);
3581 vim_free(winvarname);
3582 }
3583 }
3584 }
3585 if (need_switch_win)
3586 restore_win(save_curwin, save_curtab, TRUE);
3587 }
3588}
3589
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003590/*
3591 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3592 * v:option_type, and v:option_command.
3593 */
3594 void
3595reset_v_option_vars(void)
3596{
3597 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3598 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3599 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3600 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3601 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3602 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3603}
3604
3605/*
3606 * Add an assert error to v:errors.
3607 */
3608 void
3609assert_error(garray_T *gap)
3610{
3611 struct vimvar *vp = &vimvars[VV_ERRORS];
3612
3613 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003614 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003615 set_vim_var_list(VV_ERRORS, list_alloc());
3616 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3617}
3618
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003619 int
3620var_exists(char_u *var)
3621{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003622 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003623 char_u *name;
3624 char_u *tofree;
3625 typval_T tv;
3626 int len = 0;
3627 int n = FALSE;
3628
3629 // get_name_len() takes care of expanding curly braces
3630 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003631 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003632 if (len > 0)
3633 {
3634 if (tofree != NULL)
3635 name = tofree;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003636 n = (eval_variable(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003637 if (n)
3638 {
3639 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003640 arg = skipwhite(arg);
3641 n = (handle_subscript(&arg, &tv, &EVALARG_EVALUATE, FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003642 if (n)
3643 clear_tv(&tv);
3644 }
3645 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003646 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003647 n = FALSE;
3648
3649 vim_free(tofree);
3650 return n;
3651}
3652
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003653static lval_T *redir_lval = NULL;
3654#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3655static garray_T redir_ga; // only valid when redir_lval is not NULL
3656static char_u *redir_endp = NULL;
3657static char_u *redir_varname = NULL;
3658
3659/*
3660 * Start recording command output to a variable
3661 * When "append" is TRUE append to an existing variable.
3662 * Returns OK if successfully completed the setup. FAIL otherwise.
3663 */
3664 int
3665var_redir_start(char_u *name, int append)
3666{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003667 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003668 typval_T tv;
3669
3670 // Catch a bad name early.
3671 if (!eval_isnamec1(*name))
3672 {
3673 emsg(_(e_invarg));
3674 return FAIL;
3675 }
3676
3677 // Make a copy of the name, it is used in redir_lval until redir ends.
3678 redir_varname = vim_strsave(name);
3679 if (redir_varname == NULL)
3680 return FAIL;
3681
3682 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3683 if (redir_lval == NULL)
3684 {
3685 var_redir_stop();
3686 return FAIL;
3687 }
3688
3689 // The output is stored in growarray "redir_ga" until redirection ends.
3690 ga_init2(&redir_ga, (int)sizeof(char), 500);
3691
3692 // Parse the variable name (can be a dict or list entry).
3693 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3694 FNE_CHECK_START);
3695 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3696 {
3697 clear_lval(redir_lval);
3698 if (redir_endp != NULL && *redir_endp != NUL)
3699 // Trailing characters are present after the variable name
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003700 semsg(_(e_trailing_arg), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003701 else
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02003702 semsg(_(e_invarg2), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003703 redir_endp = NULL; // don't store a value, only cleanup
3704 var_redir_stop();
3705 return FAIL;
3706 }
3707
3708 // check if we can write to the variable: set it to or append an empty
3709 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003710 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003711 tv.v_type = VAR_STRING;
3712 tv.vval.v_string = (char_u *)"";
3713 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003714 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3715 ASSIGN_NO_DECL, (char_u *)".");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003716 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003717 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
3718 ASSIGN_NO_DECL, (char_u *)"=");
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003719 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02003720 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003721 {
3722 redir_endp = NULL; // don't store a value, only cleanup
3723 var_redir_stop();
3724 return FAIL;
3725 }
3726
3727 return OK;
3728}
3729
3730/*
3731 * Append "value[value_len]" to the variable set by var_redir_start().
3732 * The actual appending is postponed until redirection ends, because the value
3733 * appended may in fact be the string we write to, changing it may cause freed
3734 * memory to be used:
3735 * :redir => foo
3736 * :let foo
3737 * :redir END
3738 */
3739 void
3740var_redir_str(char_u *value, int value_len)
3741{
3742 int len;
3743
3744 if (redir_lval == NULL)
3745 return;
3746
3747 if (value_len == -1)
3748 len = (int)STRLEN(value); // Append the entire string
3749 else
3750 len = value_len; // Append only "value_len" characters
3751
3752 if (ga_grow(&redir_ga, len) == OK)
3753 {
3754 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3755 redir_ga.ga_len += len;
3756 }
3757 else
3758 var_redir_stop();
3759}
3760
3761/*
3762 * Stop redirecting command output to a variable.
3763 * Frees the allocated memory.
3764 */
3765 void
3766var_redir_stop(void)
3767{
3768 typval_T tv;
3769
3770 if (EVALCMD_BUSY)
3771 {
3772 redir_lval = NULL;
3773 return;
3774 }
3775
3776 if (redir_lval != NULL)
3777 {
3778 // If there was no error: assign the text to the variable.
3779 if (redir_endp != NULL)
3780 {
3781 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3782 tv.v_type = VAR_STRING;
3783 tv.vval.v_string = redir_ga.ga_data;
3784 // Call get_lval() again, if it's inside a Dict or List it may
3785 // have changed.
3786 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3787 FALSE, FALSE, 0, FNE_CHECK_START);
3788 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003790 (char_u *)".");
3791 clear_lval(redir_lval);
3792 }
3793
3794 // free the collected output
3795 VIM_CLEAR(redir_ga.ga_data);
3796
3797 VIM_CLEAR(redir_lval);
3798 }
3799 VIM_CLEAR(redir_varname);
3800}
3801
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003802/*
3803 * "gettabvar()" function
3804 */
3805 void
3806f_gettabvar(typval_T *argvars, typval_T *rettv)
3807{
3808 win_T *oldcurwin;
3809 tabpage_T *tp, *oldtabpage;
3810 dictitem_T *v;
3811 char_u *varname;
3812 int done = FALSE;
3813
3814 rettv->v_type = VAR_STRING;
3815 rettv->vval.v_string = NULL;
3816
3817 varname = tv_get_string_chk(&argvars[1]);
3818 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3819 if (tp != NULL && varname != NULL)
3820 {
3821 // Set tp to be our tabpage, temporarily. Also set the window to the
3822 // first window in the tabpage, otherwise the window is not valid.
3823 if (switch_win(&oldcurwin, &oldtabpage,
3824 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3825 : tp->tp_firstwin, tp, TRUE) == OK)
3826 {
3827 // look up the variable
3828 // Let gettabvar({nr}, "") return the "t:" dictionary.
3829 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3830 if (v != NULL)
3831 {
3832 copy_tv(&v->di_tv, rettv);
3833 done = TRUE;
3834 }
3835 }
3836
3837 // restore previous notion of curwin
3838 restore_win(oldcurwin, oldtabpage, TRUE);
3839 }
3840
3841 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3842 copy_tv(&argvars[2], rettv);
3843}
3844
3845/*
3846 * "gettabwinvar()" function
3847 */
3848 void
3849f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3850{
3851 getwinvar(argvars, rettv, 1);
3852}
3853
3854/*
3855 * "getwinvar()" function
3856 */
3857 void
3858f_getwinvar(typval_T *argvars, typval_T *rettv)
3859{
3860 getwinvar(argvars, rettv, 0);
3861}
3862
3863/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003864 * "getbufvar()" function
3865 */
3866 void
3867f_getbufvar(typval_T *argvars, typval_T *rettv)
3868{
3869 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003870 char_u *varname;
3871 dictitem_T *v;
3872 int done = FALSE;
3873
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003874 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003875 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003876
3877 rettv->v_type = VAR_STRING;
3878 rettv->vval.v_string = NULL;
3879
3880 if (buf != NULL && varname != NULL)
3881 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003882 if (*varname == '&')
3883 {
Bram Moolenaar86015452020-03-29 15:12:15 +02003884 buf_T *save_curbuf = curbuf;
3885
3886 // set curbuf to be our buf, temporarily
3887 curbuf = buf;
3888
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003889 if (varname[1] == NUL)
3890 {
3891 // get all buffer-local options in a dict
3892 dict_T *opts = get_winbuf_options(TRUE);
3893
3894 if (opts != NULL)
3895 {
3896 rettv_dict_set(rettv, opts);
3897 done = TRUE;
3898 }
3899 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003900 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003901 // buffer-local-option
3902 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02003903
3904 // restore previous notion of curbuf
3905 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003906 }
3907 else
3908 {
3909 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02003910 if (*varname == NUL)
3911 // Let getbufvar({nr}, "") return the "b:" dictionary.
3912 v = &buf->b_bufvar;
3913 else
3914 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
3915 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003916 if (v != NULL)
3917 {
3918 copy_tv(&v->di_tv, rettv);
3919 done = TRUE;
3920 }
3921 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003922 }
3923
3924 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3925 // use the default value
3926 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003927}
3928
3929/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003930 * "settabvar()" function
3931 */
3932 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003933f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003934{
3935 tabpage_T *save_curtab;
3936 tabpage_T *tp;
3937 char_u *varname, *tabvarname;
3938 typval_T *varp;
3939
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003940 if (check_secure())
3941 return;
3942
3943 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3944 varname = tv_get_string_chk(&argvars[1]);
3945 varp = &argvars[2];
3946
3947 if (varname != NULL && varp != NULL && tp != NULL)
3948 {
3949 save_curtab = curtab;
3950 goto_tabpage_tp(tp, FALSE, FALSE);
3951
3952 tabvarname = alloc(STRLEN(varname) + 3);
3953 if (tabvarname != NULL)
3954 {
3955 STRCPY(tabvarname, "t:");
3956 STRCPY(tabvarname + 2, varname);
3957 set_var(tabvarname, varp, TRUE);
3958 vim_free(tabvarname);
3959 }
3960
3961 // Restore current tabpage
3962 if (valid_tabpage(save_curtab))
3963 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3964 }
3965}
3966
3967/*
3968 * "settabwinvar()" function
3969 */
3970 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003971f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003972{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003973 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003974}
3975
3976/*
3977 * "setwinvar()" function
3978 */
3979 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003980f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003981{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003982 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003983}
3984
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003985/*
3986 * "setbufvar()" function
3987 */
3988 void
3989f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3990{
3991 buf_T *buf;
3992 char_u *varname, *bufvarname;
3993 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003994
3995 if (check_secure())
3996 return;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003997 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02003998 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003999 varp = &argvars[2];
4000
4001 if (buf != NULL && varname != NULL && varp != NULL)
4002 {
4003 if (*varname == '&')
4004 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004005 aco_save_T aco;
4006
4007 // set curbuf to be our buf, temporarily
4008 aucmd_prepbuf(&aco, buf);
4009
Bram Moolenaar191929b2020-08-19 21:20:49 +02004010 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004011
4012 // reset notion of buffer
4013 aucmd_restbuf(&aco);
4014 }
4015 else
4016 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004017 bufvarname = alloc(STRLEN(varname) + 3);
4018 if (bufvarname != NULL)
4019 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004020 buf_T *save_curbuf = curbuf;
4021
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004022 curbuf = buf;
4023 STRCPY(bufvarname, "b:");
4024 STRCPY(bufvarname + 2, varname);
4025 set_var(bufvarname, varp, TRUE);
4026 vim_free(bufvarname);
4027 curbuf = save_curbuf;
4028 }
4029 }
4030 }
4031}
4032
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004033/*
4034 * Get a callback from "arg". It can be a Funcref or a function name.
4035 * When "arg" is zero return an empty string.
4036 * "cb_name" is not allocated.
4037 * "cb_name" is set to NULL for an invalid argument.
4038 */
4039 callback_T
4040get_callback(typval_T *arg)
4041{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004042 callback_T res;
4043 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004044
4045 res.cb_free_name = FALSE;
4046 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4047 {
4048 res.cb_partial = arg->vval.v_partial;
4049 ++res.cb_partial->pt_refcount;
4050 res.cb_name = partial_name(res.cb_partial);
4051 }
4052 else
4053 {
4054 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004055 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4056 && isdigit(*arg->vval.v_string))
4057 r = FAIL;
4058 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004059 {
4060 // Note that we don't make a copy of the string.
4061 res.cb_name = arg->vval.v_string;
4062 func_ref(res.cb_name);
4063 }
4064 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004065 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004066 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004067 r = FAIL;
4068
4069 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004070 {
4071 emsg(_("E921: Invalid callback argument"));
4072 res.cb_name = NULL;
4073 }
4074 }
4075 return res;
4076}
4077
4078/*
4079 * Copy a callback into a typval_T.
4080 */
4081 void
4082put_callback(callback_T *cb, typval_T *tv)
4083{
4084 if (cb->cb_partial != NULL)
4085 {
4086 tv->v_type = VAR_PARTIAL;
4087 tv->vval.v_partial = cb->cb_partial;
4088 ++tv->vval.v_partial->pt_refcount;
4089 }
4090 else
4091 {
4092 tv->v_type = VAR_FUNC;
4093 tv->vval.v_string = vim_strsave(cb->cb_name);
4094 func_ref(cb->cb_name);
4095 }
4096}
4097
4098/*
4099 * Make a copy of "src" into "dest", allocating the function name if needed,
4100 * without incrementing the refcount.
4101 */
4102 void
4103set_callback(callback_T *dest, callback_T *src)
4104{
4105 if (src->cb_partial == NULL)
4106 {
4107 // just a function name, make a copy
4108 dest->cb_name = vim_strsave(src->cb_name);
4109 dest->cb_free_name = TRUE;
4110 }
4111 else
4112 {
4113 // cb_name is a pointer into cb_partial
4114 dest->cb_name = src->cb_name;
4115 dest->cb_free_name = FALSE;
4116 }
4117 dest->cb_partial = src->cb_partial;
4118}
4119
4120/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004121 * Copy callback from "src" to "dest", incrementing the refcounts.
4122 */
4123 void
4124copy_callback(callback_T *dest, callback_T *src)
4125{
4126 dest->cb_partial = src->cb_partial;
4127 if (dest->cb_partial != NULL)
4128 {
4129 dest->cb_name = src->cb_name;
4130 dest->cb_free_name = FALSE;
4131 ++dest->cb_partial->pt_refcount;
4132 }
4133 else
4134 {
4135 dest->cb_name = vim_strsave(src->cb_name);
4136 dest->cb_free_name = TRUE;
4137 func_ref(src->cb_name);
4138 }
4139}
4140
4141/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004142 * Unref/free "callback" returned by get_callback() or set_callback().
4143 */
4144 void
4145free_callback(callback_T *callback)
4146{
4147 if (callback->cb_partial != NULL)
4148 {
4149 partial_unref(callback->cb_partial);
4150 callback->cb_partial = NULL;
4151 }
4152 else if (callback->cb_name != NULL)
4153 func_unref(callback->cb_name);
4154 if (callback->cb_free_name)
4155 {
4156 vim_free(callback->cb_name);
4157 callback->cb_free_name = FALSE;
4158 }
4159 callback->cb_name = NULL;
4160}
4161
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004162#endif // FEAT_EVAL