blob: c087d85cc5237b69f4252dd012b564de5747661b [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
18static char *e_letunexp = N_("E18: Unexpected characters in :let");
19
Bram Moolenaare5cdf152019-08-29 22:09:46 +020020static dictitem_T globvars_var; // variable used for g:
21
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
42static struct vimvar
43{
44 char *vv_name; // name of variable, without v:
45 dictitem16_T vv_di; // value and name for key (max 16 chars!)
46 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
47} vimvars[VV_LEN] =
48{
49 /*
50 * The order here must match the VV_ defines in vim.h!
51 * Initializing a union does not work, leave tv.vval empty to get zero's.
52 */
53 {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},
121 {VV_NAME("false", VAR_SPECIAL), VV_RO},
122 {VV_NAME("true", VAR_SPECIAL), VV_RO},
123 {VV_NAME("null", VAR_SPECIAL), VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), VV_RO},
125 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
126 {VV_NAME("testing", VAR_NUMBER), 0},
127 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
128 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
129 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
130 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
131 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
132 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
133 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
134 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
135 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
136 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
137 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
138 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
139 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
140 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
141 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
142 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
143 {VV_NAME("event", VAR_DICT), VV_RO},
144 {VV_NAME("versionlong", VAR_NUMBER), VV_RO},
145 {VV_NAME("echospace", VAR_NUMBER), VV_RO},
146};
147
148// shorthand
149#define vv_type vv_di.di_tv.v_type
150#define vv_nr vv_di.di_tv.vval.v_number
151#define vv_float vv_di.di_tv.vval.v_float
152#define vv_str vv_di.di_tv.vval.v_string
153#define vv_list vv_di.di_tv.vval.v_list
154#define vv_dict vv_di.di_tv.vval.v_dict
155#define vv_blob vv_di.di_tv.vval.v_blob
156#define vv_tv vv_di.di_tv
157
158static dictitem_T vimvars_var; // variable used for v:
159#define vimvarht vimvardict.dv_hashtab
160
161// for VIM_VERSION_ defines
162#include "version.h"
163
164/*
165 * Array to hold the hashtab with variables local to each sourced script.
166 * Each item holds a variable (nameless) that points to the dict_T.
167 */
168typedef struct
169{
170 dictitem_T sv_var;
171 dict_T sv_dict;
172} scriptvar_T;
173
174static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
175#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
176#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
177
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200178static void ex_let_const(exarg_T *eap, int is_const);
179static char_u *skip_var_one(char_u *arg);
180static void list_glob_vars(int *first);
181static void list_buf_vars(int *first);
182static void list_win_vars(int *first);
183static void list_tab_vars(int *first);
184static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
185static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
186static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
187static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
188static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
189static void item_lock(typval_T *tv, int deep, int lock);
190static void list_one_var(dictitem_T *v, char *prefix, int *first);
191static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
192
193/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200194 * Initialize global and vim special variables
195 */
196 void
197evalvars_init(void)
198{
199 int i;
200 struct vimvar *p;
201
202 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
203 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
204 vimvardict.dv_lock = VAR_FIXED;
205 hash_init(&compat_hashtab);
206
207 for (i = 0; i < VV_LEN; ++i)
208 {
209 p = &vimvars[i];
210 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
211 {
212 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
213 getout(1);
214 }
215 STRCPY(p->vv_di.di_key, p->vv_name);
216 if (p->vv_flags & VV_RO)
217 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
218 else if (p->vv_flags & VV_RO_SBX)
219 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
220 else
221 p->vv_di.di_flags = DI_FLAGS_FIX;
222
223 // add to v: scope dict, unless the value is not always available
224 if (p->vv_type != VAR_UNKNOWN)
225 hash_add(&vimvarht, p->vv_di.di_key);
226 if (p->vv_flags & VV_COMPAT)
227 // add to compat scope dict
228 hash_add(&compat_hashtab, p->vv_di.di_key);
229 }
230 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
231 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
232
233 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
234 set_vim_var_nr(VV_HLSEARCH, 1L);
235 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
236 set_vim_var_list(VV_ERRORS, list_alloc());
237 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
238
239 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
240 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
241 set_vim_var_nr(VV_NONE, VVAL_NONE);
242 set_vim_var_nr(VV_NULL, VVAL_NULL);
243
244 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
245 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
246 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
247 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
248 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
249 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
250 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
251 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
252 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
253 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
254 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
255
256 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
257
258 set_reg_var(0); // default for v:register is not 0 but '"'
259}
260
261#if defined(EXITFREE) || defined(PROTO)
262/*
263 * Free all vim variables information on exit
264 */
265 void
266evalvars_clear(void)
267{
268 int i;
269 struct vimvar *p;
270
271 for (i = 0; i < VV_LEN; ++i)
272 {
273 p = &vimvars[i];
274 if (p->vv_di.di_tv.v_type == VAR_STRING)
275 VIM_CLEAR(p->vv_str);
276 else if (p->vv_di.di_tv.v_type == VAR_LIST)
277 {
278 list_unref(p->vv_list);
279 p->vv_list = NULL;
280 }
281 }
282 hash_clear(&vimvarht);
283 hash_init(&vimvarht); // garbage_collect() will access it
284 hash_clear(&compat_hashtab);
285
286 // global variables
287 vars_clear(&globvarht);
288
289 // Script-local variables. First clear all the variables and in a second
290 // loop free the scriptvar_T, because a variable in one script might hold
291 // a reference to the whole scope of another script.
292 for (i = 1; i <= ga_scripts.ga_len; ++i)
293 vars_clear(&SCRIPT_VARS(i));
294 for (i = 1; i <= ga_scripts.ga_len; ++i)
295 vim_free(SCRIPT_SV(i));
296 ga_clear(&ga_scripts);
297}
298#endif
299
300 int
301garbage_collect_vimvars(int copyID)
302{
303 return set_ref_in_ht(&vimvarht, copyID, NULL);
304}
305
306 int
307garbage_collect_scriptvars(int copyID)
308{
309 int i;
310 int abort = FALSE;
311
312 for (i = 1; i <= ga_scripts.ga_len; ++i)
313 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
314
315 return abort;
316}
317
318/*
319 * Set an internal variable to a string value. Creates the variable if it does
320 * not already exist.
321 */
322 void
323set_internal_string_var(char_u *name, char_u *value)
324{
325 char_u *val;
326 typval_T *tvp;
327
328 val = vim_strsave(value);
329 if (val != NULL)
330 {
331 tvp = alloc_string_tv(val);
332 if (tvp != NULL)
333 {
334 set_var(name, tvp, FALSE);
335 free_tv(tvp);
336 }
337 }
338}
339
340/*
341 * Prepare v: variable "idx" to be used.
342 * Save the current typeval in "save_tv".
343 * When not used yet add the variable to the v: hashtable.
344 */
345 void
346prepare_vimvar(int idx, typval_T *save_tv)
347{
348 *save_tv = vimvars[idx].vv_tv;
349 if (vimvars[idx].vv_type == VAR_UNKNOWN)
350 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
351}
352
353/*
354 * Restore v: variable "idx" to typeval "save_tv".
355 * When no longer defined, remove the variable from the v: hashtable.
356 */
357 void
358restore_vimvar(int idx, typval_T *save_tv)
359{
360 hashitem_T *hi;
361
362 vimvars[idx].vv_tv = *save_tv;
363 if (vimvars[idx].vv_type == VAR_UNKNOWN)
364 {
365 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
366 if (HASHITEM_EMPTY(hi))
367 internal_error("restore_vimvar()");
368 else
369 hash_remove(&vimvarht, hi);
370 }
371}
372
373/*
374 * List Vim variables.
375 */
376 static void
377list_vim_vars(int *first)
378{
379 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
380}
381
382/*
383 * List script-local variables, if there is a script.
384 */
385 static void
386list_script_vars(int *first)
387{
388 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
389 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
390 "s:", FALSE, first);
391}
392
393/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200394 * Get a list of lines from a HERE document. The here document is a list of
395 * lines surrounded by a marker.
396 * cmd << {marker}
397 * {line1}
398 * {line2}
399 * ....
400 * {marker}
401 *
402 * The {marker} is a string. If the optional 'trim' word is supplied before the
403 * marker, then the leading indentation before the lines (matching the
404 * indentation in the 'cmd' line) is stripped.
405 * Returns a List with {lines} or NULL.
406 */
407 static list_T *
408heredoc_get(exarg_T *eap, char_u *cmd)
409{
410 char_u *theline;
411 char_u *marker;
412 list_T *l;
413 char_u *p;
414 int marker_indent_len = 0;
415 int text_indent_len = 0;
416 char_u *text_indent = NULL;
417
418 if (eap->getline == NULL)
419 {
420 emsg(_("E991: cannot use =<< here"));
421 return NULL;
422 }
423
424 // Check for the optional 'trim' word before the marker
425 cmd = skipwhite(cmd);
426 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
427 {
428 cmd = skipwhite(cmd + 4);
429
430 // Trim the indentation from all the lines in the here document.
431 // The amount of indentation trimmed is the same as the indentation of
432 // the first line after the :let command line. To find the end marker
433 // the indent of the :let command line is trimmed.
434 p = *eap->cmdlinep;
435 while (VIM_ISWHITE(*p))
436 {
437 p++;
438 marker_indent_len++;
439 }
440 text_indent_len = -1;
441 }
442
443 // The marker is the next word.
444 if (*cmd != NUL && *cmd != '"')
445 {
446 marker = skipwhite(cmd);
447 p = skiptowhite(marker);
448 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
449 {
450 emsg(_(e_trailing));
451 return NULL;
452 }
453 *p = NUL;
454 if (vim_islower(*marker))
455 {
456 emsg(_("E221: Marker cannot start with lower case letter"));
457 return NULL;
458 }
459 }
460 else
461 {
462 emsg(_("E172: Missing marker"));
463 return NULL;
464 }
465
466 l = list_alloc();
467 if (l == NULL)
468 return NULL;
469
470 for (;;)
471 {
472 int mi = 0;
473 int ti = 0;
474
475 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
476 if (theline == NULL)
477 {
478 semsg(_("E990: Missing end marker '%s'"), marker);
479 break;
480 }
481
482 // with "trim": skip the indent matching the :let line to find the
483 // marker
484 if (marker_indent_len > 0
485 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
486 mi = marker_indent_len;
487 if (STRCMP(marker, theline + mi) == 0)
488 {
489 vim_free(theline);
490 break;
491 }
492
493 if (text_indent_len == -1 && *theline != NUL)
494 {
495 // set the text indent from the first line.
496 p = theline;
497 text_indent_len = 0;
498 while (VIM_ISWHITE(*p))
499 {
500 p++;
501 text_indent_len++;
502 }
503 text_indent = vim_strnsave(theline, text_indent_len);
504 }
505 // with "trim": skip the indent matching the first line
506 if (text_indent != NULL)
507 for (ti = 0; ti < text_indent_len; ++ti)
508 if (theline[ti] != text_indent[ti])
509 break;
510
511 if (list_append_string(l, theline + ti, -1) == FAIL)
512 break;
513 vim_free(theline);
514 }
515 vim_free(text_indent);
516
517 return l;
518}
519
520/*
521 * ":let" list all variable values
522 * ":let var1 var2" list variable values
523 * ":let var = expr" assignment command.
524 * ":let var += expr" assignment command.
525 * ":let var -= expr" assignment command.
526 * ":let var *= expr" assignment command.
527 * ":let var /= expr" assignment command.
528 * ":let var %= expr" assignment command.
529 * ":let var .= expr" assignment command.
530 * ":let var ..= expr" assignment command.
531 * ":let [var1, var2] = expr" unpack list.
532 */
533 void
534ex_let(exarg_T *eap)
535{
536 ex_let_const(eap, FALSE);
537}
538
539/*
540 * ":const" list all variable values
541 * ":const var1 var2" list variable values
542 * ":const var = expr" assignment command.
543 * ":const [var1, var2] = expr" unpack list.
544 */
545 void
546ex_const(exarg_T *eap)
547{
548 ex_let_const(eap, TRUE);
549}
550
551 static void
552ex_let_const(exarg_T *eap, int is_const)
553{
554 char_u *arg = eap->arg;
555 char_u *expr = NULL;
556 typval_T rettv;
557 int i;
558 int var_count = 0;
559 int semicolon = 0;
560 char_u op[2];
561 char_u *argend;
562 int first = TRUE;
563 int concat;
564
565 argend = skip_var_list(arg, &var_count, &semicolon);
566 if (argend == NULL)
567 return;
568 if (argend > arg && argend[-1] == '.') // for var.='str'
569 --argend;
570 expr = skipwhite(argend);
571 concat = expr[0] == '.'
572 && ((expr[1] == '=' && current_sctx.sc_version < 2)
573 || (expr[1] == '.' && expr[2] == '='));
574 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
575 && expr[1] == '=') || concat))
576 {
577 // ":let" without "=": list variables
578 if (*arg == '[')
579 emsg(_(e_invarg));
580 else if (expr[0] == '.')
581 emsg(_("E985: .= is not supported with script version 2"));
582 else if (!ends_excmd(*arg))
583 // ":let var1 var2"
584 arg = list_arg_vars(eap, arg, &first);
585 else if (!eap->skip)
586 {
587 // ":let"
588 list_glob_vars(&first);
589 list_buf_vars(&first);
590 list_win_vars(&first);
591 list_tab_vars(&first);
592 list_script_vars(&first);
593 list_func_vars(&first);
594 list_vim_vars(&first);
595 }
596 eap->nextcmd = check_nextcmd(arg);
597 }
598 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
599 {
600 list_T *l;
601
602 // HERE document
603 l = heredoc_get(eap, expr + 3);
604 if (l != NULL)
605 {
606 rettv_list_set(&rettv, l);
607 op[0] = '=';
608 op[1] = NUL;
609 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
610 is_const, op);
611 clear_tv(&rettv);
612 }
613 }
614 else
615 {
616 op[0] = '=';
617 op[1] = NUL;
618 if (*expr != '=')
619 {
620 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
621 {
622 op[0] = *expr; // +=, -=, *=, /=, %= or .=
623 if (expr[0] == '.' && expr[1] == '.') // ..=
624 ++expr;
625 }
626 expr = skipwhite(expr + 2);
627 }
628 else
629 expr = skipwhite(expr + 1);
630
631 if (eap->skip)
632 ++emsg_skip;
633 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
634 if (eap->skip)
635 {
636 if (i != FAIL)
637 clear_tv(&rettv);
638 --emsg_skip;
639 }
640 else if (i != FAIL)
641 {
642 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
643 is_const, op);
644 clear_tv(&rettv);
645 }
646 }
647}
648
649/*
650 * Assign the typevalue "tv" to the variable or variables at "arg_start".
651 * Handles both "var" with any type and "[var, var; var]" with a list type.
652 * When "op" is not NULL it points to a string with characters that
653 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
654 * or concatenate.
655 * Returns OK or FAIL;
656 */
657 int
658ex_let_vars(
659 char_u *arg_start,
660 typval_T *tv,
661 int copy, // copy values from "tv", don't move
662 int semicolon, // from skip_var_list()
663 int var_count, // from skip_var_list()
664 int is_const, // lock variables for const
665 char_u *op)
666{
667 char_u *arg = arg_start;
668 list_T *l;
669 int i;
670 listitem_T *item;
671 typval_T ltv;
672
673 if (*arg != '[')
674 {
675 // ":let var = expr" or ":for var in list"
676 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
677 return FAIL;
678 return OK;
679 }
680
681 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
682 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
683 {
684 emsg(_(e_listreq));
685 return FAIL;
686 }
687
688 i = list_len(l);
689 if (semicolon == 0 && var_count < i)
690 {
691 emsg(_("E687: Less targets than List items"));
692 return FAIL;
693 }
694 if (var_count - semicolon > i)
695 {
696 emsg(_("E688: More targets than List items"));
697 return FAIL;
698 }
699
700 item = l->lv_first;
701 while (*arg != ']')
702 {
703 arg = skipwhite(arg + 1);
704 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
705 (char_u *)",;]", op);
706 item = item->li_next;
707 if (arg == NULL)
708 return FAIL;
709
710 arg = skipwhite(arg);
711 if (*arg == ';')
712 {
713 // Put the rest of the list (may be empty) in the var after ';'.
714 // Create a new list for this.
715 l = list_alloc();
716 if (l == NULL)
717 return FAIL;
718 while (item != NULL)
719 {
720 list_append_tv(l, &item->li_tv);
721 item = item->li_next;
722 }
723
724 ltv.v_type = VAR_LIST;
725 ltv.v_lock = 0;
726 ltv.vval.v_list = l;
727 l->lv_refcount = 1;
728
729 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
730 (char_u *)"]", op);
731 clear_tv(&ltv);
732 if (arg == NULL)
733 return FAIL;
734 break;
735 }
736 else if (*arg != ',' && *arg != ']')
737 {
738 internal_error("ex_let_vars()");
739 return FAIL;
740 }
741 }
742
743 return OK;
744}
745
746/*
747 * Skip over assignable variable "var" or list of variables "[var, var]".
748 * Used for ":let varvar = expr" and ":for varvar in expr".
749 * For "[var, var]" increment "*var_count" for each variable.
750 * for "[var, var; var]" set "semicolon".
751 * Return NULL for an error.
752 */
753 char_u *
754skip_var_list(
755 char_u *arg,
756 int *var_count,
757 int *semicolon)
758{
759 char_u *p, *s;
760
761 if (*arg == '[')
762 {
763 // "[var, var]": find the matching ']'.
764 p = arg;
765 for (;;)
766 {
767 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
768 s = skip_var_one(p);
769 if (s == p)
770 {
771 semsg(_(e_invarg2), p);
772 return NULL;
773 }
774 ++*var_count;
775
776 p = skipwhite(s);
777 if (*p == ']')
778 break;
779 else if (*p == ';')
780 {
781 if (*semicolon == 1)
782 {
783 emsg(_("Double ; in list of variables"));
784 return NULL;
785 }
786 *semicolon = 1;
787 }
788 else if (*p != ',')
789 {
790 semsg(_(e_invarg2), p);
791 return NULL;
792 }
793 }
794 return p + 1;
795 }
796 else
797 return skip_var_one(arg);
798}
799
800/*
801 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
802 * l[idx].
803 */
804 static char_u *
805skip_var_one(char_u *arg)
806{
807 if (*arg == '@' && arg[1] != NUL)
808 return arg + 2;
809 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
810 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
811}
812
813/*
814 * List variables for hashtab "ht" with prefix "prefix".
815 * If "empty" is TRUE also list NULL strings as empty strings.
816 */
817 void
818list_hashtable_vars(
819 hashtab_T *ht,
820 char *prefix,
821 int empty,
822 int *first)
823{
824 hashitem_T *hi;
825 dictitem_T *di;
826 int todo;
827 char_u buf[IOSIZE];
828
829 todo = (int)ht->ht_used;
830 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
831 {
832 if (!HASHITEM_EMPTY(hi))
833 {
834 --todo;
835 di = HI2DI(hi);
836
837 // apply :filter /pat/ to variable name
838 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
839 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
840 if (message_filtered(buf))
841 continue;
842
843 if (empty || di->di_tv.v_type != VAR_STRING
844 || di->di_tv.vval.v_string != NULL)
845 list_one_var(di, prefix, first);
846 }
847 }
848}
849
850/*
851 * List global variables.
852 */
853 static void
854list_glob_vars(int *first)
855{
856 list_hashtable_vars(&globvarht, "", TRUE, first);
857}
858
859/*
860 * List buffer variables.
861 */
862 static void
863list_buf_vars(int *first)
864{
865 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
866}
867
868/*
869 * List window variables.
870 */
871 static void
872list_win_vars(int *first)
873{
874 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
875}
876
877/*
878 * List tab page variables.
879 */
880 static void
881list_tab_vars(int *first)
882{
883 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
884}
885
886/*
887 * List variables in "arg".
888 */
889 static char_u *
890list_arg_vars(exarg_T *eap, char_u *arg, int *first)
891{
892 int error = FALSE;
893 int len;
894 char_u *name;
895 char_u *name_start;
896 char_u *arg_subsc;
897 char_u *tofree;
898 typval_T tv;
899
900 while (!ends_excmd(*arg) && !got_int)
901 {
902 if (error || eap->skip)
903 {
904 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
905 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
906 {
907 emsg_severe = TRUE;
908 emsg(_(e_trailing));
909 break;
910 }
911 }
912 else
913 {
914 // get_name_len() takes care of expanding curly braces
915 name_start = name = arg;
916 len = get_name_len(&arg, &tofree, TRUE, TRUE);
917 if (len <= 0)
918 {
919 // This is mainly to keep test 49 working: when expanding
920 // curly braces fails overrule the exception error message.
921 if (len < 0 && !aborting())
922 {
923 emsg_severe = TRUE;
924 semsg(_(e_invarg2), arg);
925 break;
926 }
927 error = TRUE;
928 }
929 else
930 {
931 if (tofree != NULL)
932 name = tofree;
933 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
934 error = TRUE;
935 else
936 {
937 // handle d.key, l[idx], f(expr)
938 arg_subsc = arg;
939 if (handle_subscript(&arg, &tv, TRUE, TRUE,
940 name, &name) == FAIL)
941 error = TRUE;
942 else
943 {
944 if (arg == arg_subsc && len == 2 && name[1] == ':')
945 {
946 switch (*name)
947 {
948 case 'g': list_glob_vars(first); break;
949 case 'b': list_buf_vars(first); break;
950 case 'w': list_win_vars(first); break;
951 case 't': list_tab_vars(first); break;
952 case 'v': list_vim_vars(first); break;
953 case 's': list_script_vars(first); break;
954 case 'l': list_func_vars(first); break;
955 default:
956 semsg(_("E738: Can't list variables for %s"), name);
957 }
958 }
959 else
960 {
961 char_u numbuf[NUMBUFLEN];
962 char_u *tf;
963 int c;
964 char_u *s;
965
966 s = echo_string(&tv, &tf, numbuf, 0);
967 c = *arg;
968 *arg = NUL;
969 list_one_var_a("",
970 arg == arg_subsc ? name : name_start,
971 tv.v_type,
972 s == NULL ? (char_u *)"" : s,
973 first);
974 *arg = c;
975 vim_free(tf);
976 }
977 clear_tv(&tv);
978 }
979 }
980 }
981
982 vim_free(tofree);
983 }
984
985 arg = skipwhite(arg);
986 }
987
988 return arg;
989}
990
991/*
992 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
993 * Returns a pointer to the char just after the var name.
994 * Returns NULL if there is an error.
995 */
996 static char_u *
997ex_let_one(
998 char_u *arg, // points to variable name
999 typval_T *tv, // value to assign to variable
1000 int copy, // copy value from "tv"
1001 int is_const, // lock variable for const
1002 char_u *endchars, // valid chars after variable name or NULL
1003 char_u *op) // "+", "-", "." or NULL
1004{
1005 int c1;
1006 char_u *name;
1007 char_u *p;
1008 char_u *arg_end = NULL;
1009 int len;
1010 int opt_flags;
1011 char_u *tofree = NULL;
1012
1013 // ":let $VAR = expr": Set environment variable.
1014 if (*arg == '$')
1015 {
1016 if (is_const)
1017 {
1018 emsg(_("E996: Cannot lock an environment variable"));
1019 return NULL;
1020 }
1021 // Find the end of the name.
1022 ++arg;
1023 name = arg;
1024 len = get_env_len(&arg);
1025 if (len == 0)
1026 semsg(_(e_invarg2), name - 1);
1027 else
1028 {
1029 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1030 semsg(_(e_letwrong), op);
1031 else if (endchars != NULL
1032 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1033 emsg(_(e_letunexp));
1034 else if (!check_secure())
1035 {
1036 c1 = name[len];
1037 name[len] = NUL;
1038 p = tv_get_string_chk(tv);
1039 if (p != NULL && op != NULL && *op == '.')
1040 {
1041 int mustfree = FALSE;
1042 char_u *s = vim_getenv(name, &mustfree);
1043
1044 if (s != NULL)
1045 {
1046 p = tofree = concat_str(s, p);
1047 if (mustfree)
1048 vim_free(s);
1049 }
1050 }
1051 if (p != NULL)
1052 {
1053 vim_setenv(name, p);
1054 if (STRICMP(name, "HOME") == 0)
1055 init_homedir();
1056 else if (didset_vim && STRICMP(name, "VIM") == 0)
1057 didset_vim = FALSE;
1058 else if (didset_vimruntime
1059 && STRICMP(name, "VIMRUNTIME") == 0)
1060 didset_vimruntime = FALSE;
1061 arg_end = arg;
1062 }
1063 name[len] = c1;
1064 vim_free(tofree);
1065 }
1066 }
1067 }
1068
1069 // ":let &option = expr": Set option value.
1070 // ":let &l:option = expr": Set local option value.
1071 // ":let &g:option = expr": Set global option value.
1072 else if (*arg == '&')
1073 {
1074 if (is_const)
1075 {
1076 emsg(_("E996: Cannot lock an option"));
1077 return NULL;
1078 }
1079 // Find the end of the name.
1080 p = find_option_end(&arg, &opt_flags);
1081 if (p == NULL || (endchars != NULL
1082 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1083 emsg(_(e_letunexp));
1084 else
1085 {
1086 long n;
1087 int opt_type;
1088 long numval;
1089 char_u *stringval = NULL;
1090 char_u *s;
1091
1092 c1 = *p;
1093 *p = NUL;
1094
1095 n = (long)tv_get_number(tv);
1096 s = tv_get_string_chk(tv); // != NULL if number or string
1097 if (s != NULL && op != NULL && *op != '=')
1098 {
1099 opt_type = get_option_value(arg, &numval,
1100 &stringval, opt_flags);
1101 if ((opt_type == 1 && *op == '.')
1102 || (opt_type == 0 && *op != '.'))
1103 {
1104 semsg(_(e_letwrong), op);
1105 s = NULL; // don't set the value
1106 }
1107 else
1108 {
1109 if (opt_type == 1) // number
1110 {
1111 switch (*op)
1112 {
1113 case '+': n = numval + n; break;
1114 case '-': n = numval - n; break;
1115 case '*': n = numval * n; break;
1116 case '/': n = (long)num_divide(numval, n); break;
1117 case '%': n = (long)num_modulus(numval, n); break;
1118 }
1119 }
1120 else if (opt_type == 0 && stringval != NULL) // string
1121 {
1122 s = concat_str(stringval, s);
1123 vim_free(stringval);
1124 stringval = s;
1125 }
1126 }
1127 }
1128 if (s != NULL)
1129 {
1130 set_option_value(arg, n, s, opt_flags);
1131 arg_end = p;
1132 }
1133 *p = c1;
1134 vim_free(stringval);
1135 }
1136 }
1137
1138 // ":let @r = expr": Set register contents.
1139 else if (*arg == '@')
1140 {
1141 if (is_const)
1142 {
1143 emsg(_("E996: Cannot lock a register"));
1144 return NULL;
1145 }
1146 ++arg;
1147 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1148 semsg(_(e_letwrong), op);
1149 else if (endchars != NULL
1150 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1151 emsg(_(e_letunexp));
1152 else
1153 {
1154 char_u *ptofree = NULL;
1155 char_u *s;
1156
1157 p = tv_get_string_chk(tv);
1158 if (p != NULL && op != NULL && *op == '.')
1159 {
1160 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1161 if (s != NULL)
1162 {
1163 p = ptofree = concat_str(s, p);
1164 vim_free(s);
1165 }
1166 }
1167 if (p != NULL)
1168 {
1169 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1170 arg_end = arg + 1;
1171 }
1172 vim_free(ptofree);
1173 }
1174 }
1175
1176 // ":let var = expr": Set internal variable.
1177 // ":let {expr} = expr": Idem, name made with curly braces
1178 else if (eval_isnamec1(*arg) || *arg == '{')
1179 {
1180 lval_T lv;
1181
1182 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
1183 if (p != NULL && lv.ll_name != NULL)
1184 {
1185 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1186 emsg(_(e_letunexp));
1187 else
1188 {
1189 set_var_lval(&lv, p, tv, copy, is_const, op);
1190 arg_end = p;
1191 }
1192 }
1193 clear_lval(&lv);
1194 }
1195
1196 else
1197 semsg(_(e_invarg2), arg);
1198
1199 return arg_end;
1200}
1201
1202/*
1203 * ":unlet[!] var1 ... " command.
1204 */
1205 void
1206ex_unlet(exarg_T *eap)
1207{
1208 ex_unletlock(eap, eap->arg, 0);
1209}
1210
1211/*
1212 * ":lockvar" and ":unlockvar" commands
1213 */
1214 void
1215ex_lockvar(exarg_T *eap)
1216{
1217 char_u *arg = eap->arg;
1218 int deep = 2;
1219
1220 if (eap->forceit)
1221 deep = -1;
1222 else if (vim_isdigit(*arg))
1223 {
1224 deep = getdigits(&arg);
1225 arg = skipwhite(arg);
1226 }
1227
1228 ex_unletlock(eap, arg, deep);
1229}
1230
1231/*
1232 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
1233 */
1234 static void
1235ex_unletlock(
1236 exarg_T *eap,
1237 char_u *argstart,
1238 int deep)
1239{
1240 char_u *arg = argstart;
1241 char_u *name_end;
1242 int error = FALSE;
1243 lval_T lv;
1244
1245 do
1246 {
1247 if (*arg == '$')
1248 {
1249 char_u *name = ++arg;
1250
1251 if (get_env_len(&arg) == 0)
1252 {
1253 semsg(_(e_invarg2), name - 1);
1254 return;
1255 }
1256 vim_unsetenv(name);
1257 arg = skipwhite(arg);
1258 continue;
1259 }
1260
1261 // Parse the name and find the end.
1262 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
1263 FNE_CHECK_START);
1264 if (lv.ll_name == NULL)
1265 error = TRUE; // error but continue parsing
1266 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1267 && !ends_excmd(*name_end)))
1268 {
1269 if (name_end != NULL)
1270 {
1271 emsg_severe = TRUE;
1272 emsg(_(e_trailing));
1273 }
1274 if (!(eap->skip || error))
1275 clear_lval(&lv);
1276 break;
1277 }
1278
1279 if (!error && !eap->skip)
1280 {
1281 if (eap->cmdidx == CMD_unlet)
1282 {
1283 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
1284 error = TRUE;
1285 }
1286 else
1287 {
1288 if (do_lock_var(&lv, name_end, deep,
1289 eap->cmdidx == CMD_lockvar) == FAIL)
1290 error = TRUE;
1291 }
1292 }
1293
1294 if (!eap->skip)
1295 clear_lval(&lv);
1296
1297 arg = skipwhite(name_end);
1298 } while (!ends_excmd(*arg));
1299
1300 eap->nextcmd = check_nextcmd(arg);
1301}
1302
1303 static int
1304do_unlet_var(
1305 lval_T *lp,
1306 char_u *name_end,
1307 int forceit)
1308{
1309 int ret = OK;
1310 int cc;
1311
1312 if (lp->ll_tv == NULL)
1313 {
1314 cc = *name_end;
1315 *name_end = NUL;
1316
1317 // Normal name or expanded name.
1318 if (do_unlet(lp->ll_name, forceit) == FAIL)
1319 ret = FAIL;
1320 *name_end = cc;
1321 }
1322 else if ((lp->ll_list != NULL
1323 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1324 || (lp->ll_dict != NULL
1325 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1326 return FAIL;
1327 else if (lp->ll_range)
1328 {
1329 listitem_T *li;
1330 listitem_T *ll_li = lp->ll_li;
1331 int ll_n1 = lp->ll_n1;
1332
1333 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1334 {
1335 li = ll_li->li_next;
1336 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1337 return FAIL;
1338 ll_li = li;
1339 ++ll_n1;
1340 }
1341
1342 // Delete a range of List items.
1343 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1344 {
1345 li = lp->ll_li->li_next;
1346 listitem_remove(lp->ll_list, lp->ll_li);
1347 lp->ll_li = li;
1348 ++lp->ll_n1;
1349 }
1350 }
1351 else
1352 {
1353 if (lp->ll_list != NULL)
1354 // unlet a List item.
1355 listitem_remove(lp->ll_list, lp->ll_li);
1356 else
1357 // unlet a Dictionary item.
1358 dictitem_remove(lp->ll_dict, lp->ll_di);
1359 }
1360
1361 return ret;
1362}
1363
1364/*
1365 * "unlet" a variable. Return OK if it existed, FAIL if not.
1366 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1367 */
1368 int
1369do_unlet(char_u *name, int forceit)
1370{
1371 hashtab_T *ht;
1372 hashitem_T *hi;
1373 char_u *varname;
1374 dict_T *d;
1375 dictitem_T *di;
1376
1377 ht = find_var_ht(name, &varname);
1378 if (ht != NULL && *varname != NUL)
1379 {
1380 d = get_current_funccal_dict(ht);
1381 if (d == NULL)
1382 {
1383 if (ht == &globvarht)
1384 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001385 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001386 d = &vimvardict;
1387 else
1388 {
1389 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1390 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1391 }
1392 if (d == NULL)
1393 {
1394 internal_error("do_unlet()");
1395 return FAIL;
1396 }
1397 }
1398 hi = hash_find(ht, varname);
1399 if (HASHITEM_EMPTY(hi))
1400 hi = find_hi_in_scoped_ht(name, &ht);
1401 if (hi != NULL && !HASHITEM_EMPTY(hi))
1402 {
1403 di = HI2DI(hi);
1404 if (var_check_fixed(di->di_flags, name, FALSE)
1405 || var_check_ro(di->di_flags, name, FALSE)
1406 || var_check_lock(d->dv_lock, name, FALSE))
1407 return FAIL;
1408
1409 delete_var(ht, hi);
1410 return OK;
1411 }
1412 }
1413 if (forceit)
1414 return OK;
1415 semsg(_("E108: No such variable: \"%s\""), name);
1416 return FAIL;
1417}
1418
1419/*
1420 * Lock or unlock variable indicated by "lp".
1421 * "deep" is the levels to go (-1 for unlimited);
1422 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1423 */
1424 static int
1425do_lock_var(
1426 lval_T *lp,
1427 char_u *name_end,
1428 int deep,
1429 int lock)
1430{
1431 int ret = OK;
1432 int cc;
1433 dictitem_T *di;
1434
1435 if (deep == 0) // nothing to do
1436 return OK;
1437
1438 if (lp->ll_tv == NULL)
1439 {
1440 cc = *name_end;
1441 *name_end = NUL;
1442
1443 // Normal name or expanded name.
1444 di = find_var(lp->ll_name, NULL, TRUE);
1445 if (di == NULL)
1446 ret = FAIL;
1447 else if ((di->di_flags & DI_FLAGS_FIX)
1448 && di->di_tv.v_type != VAR_DICT
1449 && di->di_tv.v_type != VAR_LIST)
1450 // For historic reasons this error is not given for a list or dict.
1451 // E.g., the b: dict could be locked/unlocked.
1452 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
1453 else
1454 {
1455 if (lock)
1456 di->di_flags |= DI_FLAGS_LOCK;
1457 else
1458 di->di_flags &= ~DI_FLAGS_LOCK;
1459 item_lock(&di->di_tv, deep, lock);
1460 }
1461 *name_end = cc;
1462 }
1463 else if (lp->ll_range)
1464 {
1465 listitem_T *li = lp->ll_li;
1466
1467 // (un)lock a range of List items.
1468 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1469 {
1470 item_lock(&li->li_tv, deep, lock);
1471 li = li->li_next;
1472 ++lp->ll_n1;
1473 }
1474 }
1475 else if (lp->ll_list != NULL)
1476 // (un)lock a List item.
1477 item_lock(&lp->ll_li->li_tv, deep, lock);
1478 else
1479 // (un)lock a Dictionary item.
1480 item_lock(&lp->ll_di->di_tv, deep, lock);
1481
1482 return ret;
1483}
1484
1485/*
1486 * Lock or unlock an item. "deep" is nr of levels to go.
1487 */
1488 static void
1489item_lock(typval_T *tv, int deep, int lock)
1490{
1491 static int recurse = 0;
1492 list_T *l;
1493 listitem_T *li;
1494 dict_T *d;
1495 blob_T *b;
1496 hashitem_T *hi;
1497 int todo;
1498
1499 if (recurse >= DICT_MAXNEST)
1500 {
1501 emsg(_("E743: variable nested too deep for (un)lock"));
1502 return;
1503 }
1504 if (deep == 0)
1505 return;
1506 ++recurse;
1507
1508 // lock/unlock the item itself
1509 if (lock)
1510 tv->v_lock |= VAR_LOCKED;
1511 else
1512 tv->v_lock &= ~VAR_LOCKED;
1513
1514 switch (tv->v_type)
1515 {
1516 case VAR_UNKNOWN:
1517 case VAR_NUMBER:
1518 case VAR_STRING:
1519 case VAR_FUNC:
1520 case VAR_PARTIAL:
1521 case VAR_FLOAT:
1522 case VAR_SPECIAL:
1523 case VAR_JOB:
1524 case VAR_CHANNEL:
1525 break;
1526
1527 case VAR_BLOB:
1528 if ((b = tv->vval.v_blob) != NULL)
1529 {
1530 if (lock)
1531 b->bv_lock |= VAR_LOCKED;
1532 else
1533 b->bv_lock &= ~VAR_LOCKED;
1534 }
1535 break;
1536 case VAR_LIST:
1537 if ((l = tv->vval.v_list) != NULL)
1538 {
1539 if (lock)
1540 l->lv_lock |= VAR_LOCKED;
1541 else
1542 l->lv_lock &= ~VAR_LOCKED;
1543 if (deep < 0 || deep > 1)
1544 // recursive: lock/unlock the items the List contains
1545 for (li = l->lv_first; li != NULL; li = li->li_next)
1546 item_lock(&li->li_tv, deep - 1, lock);
1547 }
1548 break;
1549 case VAR_DICT:
1550 if ((d = tv->vval.v_dict) != NULL)
1551 {
1552 if (lock)
1553 d->dv_lock |= VAR_LOCKED;
1554 else
1555 d->dv_lock &= ~VAR_LOCKED;
1556 if (deep < 0 || deep > 1)
1557 {
1558 // recursive: lock/unlock the items the List contains
1559 todo = (int)d->dv_hashtab.ht_used;
1560 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1561 {
1562 if (!HASHITEM_EMPTY(hi))
1563 {
1564 --todo;
1565 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1566 }
1567 }
1568 }
1569 }
1570 }
1571 --recurse;
1572}
1573
1574/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001575 * Local string buffer for the next two functions to store a variable name
1576 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1577 * get_user_var_name().
1578 */
1579
1580static char_u *varnamebuf = NULL;
1581static int varnamebuflen = 0;
1582
1583/*
1584 * Function to concatenate a prefix and a variable name.
1585 */
1586 static char_u *
1587cat_prefix_varname(int prefix, char_u *name)
1588{
1589 int len;
1590
1591 len = (int)STRLEN(name) + 3;
1592 if (len > varnamebuflen)
1593 {
1594 vim_free(varnamebuf);
1595 len += 10; /* some additional space */
1596 varnamebuf = alloc(len);
1597 if (varnamebuf == NULL)
1598 {
1599 varnamebuflen = 0;
1600 return NULL;
1601 }
1602 varnamebuflen = len;
1603 }
1604 *varnamebuf = prefix;
1605 varnamebuf[1] = ':';
1606 STRCPY(varnamebuf + 2, name);
1607 return varnamebuf;
1608}
1609
1610/*
1611 * Function given to ExpandGeneric() to obtain the list of user defined
1612 * (global/buffer/window/built-in) variable names.
1613 */
1614 char_u *
1615get_user_var_name(expand_T *xp, int idx)
1616{
1617 static long_u gdone;
1618 static long_u bdone;
1619 static long_u wdone;
1620 static long_u tdone;
1621 static int vidx;
1622 static hashitem_T *hi;
1623 hashtab_T *ht;
1624
1625 if (idx == 0)
1626 {
1627 gdone = bdone = wdone = vidx = 0;
1628 tdone = 0;
1629 }
1630
1631 // Global variables
1632 if (gdone < globvarht.ht_used)
1633 {
1634 if (gdone++ == 0)
1635 hi = globvarht.ht_array;
1636 else
1637 ++hi;
1638 while (HASHITEM_EMPTY(hi))
1639 ++hi;
1640 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1641 return cat_prefix_varname('g', hi->hi_key);
1642 return hi->hi_key;
1643 }
1644
1645 // b: variables
1646 ht = &curbuf->b_vars->dv_hashtab;
1647 if (bdone < ht->ht_used)
1648 {
1649 if (bdone++ == 0)
1650 hi = ht->ht_array;
1651 else
1652 ++hi;
1653 while (HASHITEM_EMPTY(hi))
1654 ++hi;
1655 return cat_prefix_varname('b', hi->hi_key);
1656 }
1657
1658 // w: variables
1659 ht = &curwin->w_vars->dv_hashtab;
1660 if (wdone < ht->ht_used)
1661 {
1662 if (wdone++ == 0)
1663 hi = ht->ht_array;
1664 else
1665 ++hi;
1666 while (HASHITEM_EMPTY(hi))
1667 ++hi;
1668 return cat_prefix_varname('w', hi->hi_key);
1669 }
1670
1671 // t: variables
1672 ht = &curtab->tp_vars->dv_hashtab;
1673 if (tdone < ht->ht_used)
1674 {
1675 if (tdone++ == 0)
1676 hi = ht->ht_array;
1677 else
1678 ++hi;
1679 while (HASHITEM_EMPTY(hi))
1680 ++hi;
1681 return cat_prefix_varname('t', hi->hi_key);
1682 }
1683
1684 // v: variables
1685 if (vidx < VV_LEN)
1686 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1687
1688 VIM_CLEAR(varnamebuf);
1689 varnamebuflen = 0;
1690 return NULL;
1691}
1692
1693/*
1694 * Set number v: variable to "val".
1695 */
1696 void
1697set_vim_var_nr(int idx, varnumber_T val)
1698{
1699 vimvars[idx].vv_type = VAR_NUMBER;
1700 vimvars[idx].vv_nr = val;
1701}
1702
1703/*
1704 * Get typval_T v: variable value.
1705 */
1706 typval_T *
1707get_vim_var_tv(int idx)
1708{
1709 return &vimvars[idx].vv_tv;
1710}
1711
1712/*
1713 * Get number v: variable value.
1714 */
1715 varnumber_T
1716get_vim_var_nr(int idx)
1717{
1718 return vimvars[idx].vv_nr;
1719}
1720
1721/*
1722 * Get string v: variable value. Uses a static buffer, can only be used once.
1723 * If the String variable has never been set, return an empty string.
1724 * Never returns NULL;
1725 */
1726 char_u *
1727get_vim_var_str(int idx)
1728{
1729 return tv_get_string(&vimvars[idx].vv_tv);
1730}
1731
1732/*
1733 * Get List v: variable value. Caller must take care of reference count when
1734 * needed.
1735 */
1736 list_T *
1737get_vim_var_list(int idx)
1738{
1739 return vimvars[idx].vv_list;
1740}
1741
1742/*
1743 * Get Dict v: variable value. Caller must take care of reference count when
1744 * needed.
1745 */
1746 dict_T *
1747get_vim_var_dict(int idx)
1748{
1749 return vimvars[idx].vv_dict;
1750}
1751
1752/*
1753 * Set v:char to character "c".
1754 */
1755 void
1756set_vim_var_char(int c)
1757{
1758 char_u buf[MB_MAXBYTES + 1];
1759
1760 if (has_mbyte)
1761 buf[(*mb_char2bytes)(c, buf)] = NUL;
1762 else
1763 {
1764 buf[0] = c;
1765 buf[1] = NUL;
1766 }
1767 set_vim_var_string(VV_CHAR, buf, -1);
1768}
1769
1770/*
1771 * Set v:count to "count" and v:count1 to "count1".
1772 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
1773 */
1774 void
1775set_vcount(
1776 long count,
1777 long count1,
1778 int set_prevcount)
1779{
1780 if (set_prevcount)
1781 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
1782 vimvars[VV_COUNT].vv_nr = count;
1783 vimvars[VV_COUNT1].vv_nr = count1;
1784}
1785
1786/*
1787 * Save variables that might be changed as a side effect. Used when executing
1788 * a timer callback.
1789 */
1790 void
1791save_vimvars(vimvars_save_T *vvsave)
1792{
1793 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
1794 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
1795 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
1796}
1797
1798/*
1799 * Restore variables saved by save_vimvars().
1800 */
1801 void
1802restore_vimvars(vimvars_save_T *vvsave)
1803{
1804 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
1805 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
1806 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
1807}
1808
1809/*
1810 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
1811 * value.
1812 */
1813 void
1814set_vim_var_string(
1815 int idx,
1816 char_u *val,
1817 int len) // length of "val" to use or -1 (whole string)
1818{
1819 clear_tv(&vimvars[idx].vv_di.di_tv);
1820 vimvars[idx].vv_type = VAR_STRING;
1821 if (val == NULL)
1822 vimvars[idx].vv_str = NULL;
1823 else if (len == -1)
1824 vimvars[idx].vv_str = vim_strsave(val);
1825 else
1826 vimvars[idx].vv_str = vim_strnsave(val, len);
1827}
1828
1829/*
1830 * Set List v: variable to "val".
1831 */
1832 void
1833set_vim_var_list(int idx, list_T *val)
1834{
1835 clear_tv(&vimvars[idx].vv_di.di_tv);
1836 vimvars[idx].vv_type = VAR_LIST;
1837 vimvars[idx].vv_list = val;
1838 if (val != NULL)
1839 ++val->lv_refcount;
1840}
1841
1842/*
1843 * Set Dictionary v: variable to "val".
1844 */
1845 void
1846set_vim_var_dict(int idx, dict_T *val)
1847{
1848 clear_tv(&vimvars[idx].vv_di.di_tv);
1849 vimvars[idx].vv_type = VAR_DICT;
1850 vimvars[idx].vv_dict = val;
1851 if (val != NULL)
1852 {
1853 ++val->dv_refcount;
1854 dict_set_items_ro(val);
1855 }
1856}
1857
1858/*
1859 * Set v:register if needed.
1860 */
1861 void
1862set_reg_var(int c)
1863{
1864 char_u regname;
1865
1866 if (c == 0 || c == ' ')
1867 regname = '"';
1868 else
1869 regname = c;
1870 // Avoid free/alloc when the value is already right.
1871 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
1872 set_vim_var_string(VV_REG, &regname, 1);
1873}
1874
1875/*
1876 * Get or set v:exception. If "oldval" == NULL, return the current value.
1877 * Otherwise, restore the value to "oldval" and return NULL.
1878 * Must always be called in pairs to save and restore v:exception! Does not
1879 * take care of memory allocations.
1880 */
1881 char_u *
1882v_exception(char_u *oldval)
1883{
1884 if (oldval == NULL)
1885 return vimvars[VV_EXCEPTION].vv_str;
1886
1887 vimvars[VV_EXCEPTION].vv_str = oldval;
1888 return NULL;
1889}
1890
1891/*
1892 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
1893 * Otherwise, restore the value to "oldval" and return NULL.
1894 * Must always be called in pairs to save and restore v:throwpoint! Does not
1895 * take care of memory allocations.
1896 */
1897 char_u *
1898v_throwpoint(char_u *oldval)
1899{
1900 if (oldval == NULL)
1901 return vimvars[VV_THROWPOINT].vv_str;
1902
1903 vimvars[VV_THROWPOINT].vv_str = oldval;
1904 return NULL;
1905}
1906
1907/*
1908 * Set v:cmdarg.
1909 * If "eap" != NULL, use "eap" to generate the value and return the old value.
1910 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
1911 * Must always be called in pairs!
1912 */
1913 char_u *
1914set_cmdarg(exarg_T *eap, char_u *oldarg)
1915{
1916 char_u *oldval;
1917 char_u *newval;
1918 unsigned len;
1919
1920 oldval = vimvars[VV_CMDARG].vv_str;
1921 if (eap == NULL)
1922 {
1923 vim_free(oldval);
1924 vimvars[VV_CMDARG].vv_str = oldarg;
1925 return NULL;
1926 }
1927
1928 if (eap->force_bin == FORCE_BIN)
1929 len = 6;
1930 else if (eap->force_bin == FORCE_NOBIN)
1931 len = 8;
1932 else
1933 len = 0;
1934
1935 if (eap->read_edit)
1936 len += 7;
1937
1938 if (eap->force_ff != 0)
1939 len += 10; // " ++ff=unix"
1940 if (eap->force_enc != 0)
1941 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
1942 if (eap->bad_char != 0)
1943 len += 7 + 4; // " ++bad=" + "keep" or "drop"
1944
1945 newval = alloc(len + 1);
1946 if (newval == NULL)
1947 return NULL;
1948
1949 if (eap->force_bin == FORCE_BIN)
1950 sprintf((char *)newval, " ++bin");
1951 else if (eap->force_bin == FORCE_NOBIN)
1952 sprintf((char *)newval, " ++nobin");
1953 else
1954 *newval = NUL;
1955
1956 if (eap->read_edit)
1957 STRCAT(newval, " ++edit");
1958
1959 if (eap->force_ff != 0)
1960 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
1961 eap->force_ff == 'u' ? "unix"
1962 : eap->force_ff == 'd' ? "dos"
1963 : "mac");
1964 if (eap->force_enc != 0)
1965 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
1966 eap->cmd + eap->force_enc);
1967 if (eap->bad_char == BAD_KEEP)
1968 STRCPY(newval + STRLEN(newval), " ++bad=keep");
1969 else if (eap->bad_char == BAD_DROP)
1970 STRCPY(newval + STRLEN(newval), " ++bad=drop");
1971 else if (eap->bad_char != 0)
1972 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
1973 vimvars[VV_CMDARG].vv_str = newval;
1974 return oldval;
1975}
1976
1977/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001978 * Get the value of internal variable "name".
1979 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
1980 */
1981 int
1982get_var_tv(
1983 char_u *name,
1984 int len, // length of "name"
1985 typval_T *rettv, // NULL when only checking existence
1986 dictitem_T **dip, // non-NULL when typval's dict item is needed
1987 int verbose, // may give error message
1988 int no_autoload) // do not use script autoloading
1989{
1990 int ret = OK;
1991 typval_T *tv = NULL;
1992 dictitem_T *v;
1993 int cc;
1994
1995 // truncate the name, so that we can use strcmp()
1996 cc = name[len];
1997 name[len] = NUL;
1998
1999 // Check for user-defined variables.
2000 v = find_var(name, NULL, no_autoload);
2001 if (v != NULL)
2002 {
2003 tv = &v->di_tv;
2004 if (dip != NULL)
2005 *dip = v;
2006 }
2007
2008 if (tv == NULL)
2009 {
2010 if (rettv != NULL && verbose)
2011 semsg(_(e_undefvar), name);
2012 ret = FAIL;
2013 }
2014 else if (rettv != NULL)
2015 copy_tv(tv, rettv);
2016
2017 name[len] = cc;
2018
2019 return ret;
2020}
2021
2022/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002023 * Check if variable "name[len]" is a local variable or an argument.
2024 * If so, "*eval_lavars_used" is set to TRUE.
2025 */
2026 void
2027check_vars(char_u *name, int len)
2028{
2029 int cc;
2030 char_u *varname;
2031 hashtab_T *ht;
2032
2033 if (eval_lavars_used == NULL)
2034 return;
2035
2036 // truncate the name, so that we can use strcmp()
2037 cc = name[len];
2038 name[len] = NUL;
2039
2040 ht = find_var_ht(name, &varname);
2041 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2042 {
2043 if (find_var(name, NULL, TRUE) != NULL)
2044 *eval_lavars_used = TRUE;
2045 }
2046
2047 name[len] = cc;
2048}
2049
2050/*
2051 * Find variable "name" in the list of variables.
2052 * Return a pointer to it if found, NULL if not found.
2053 * Careful: "a:0" variables don't have a name.
2054 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2055 * hashtab_T used.
2056 */
2057 dictitem_T *
2058find_var(char_u *name, hashtab_T **htp, int no_autoload)
2059{
2060 char_u *varname;
2061 hashtab_T *ht;
2062 dictitem_T *ret = NULL;
2063
2064 ht = find_var_ht(name, &varname);
2065 if (htp != NULL)
2066 *htp = ht;
2067 if (ht == NULL)
2068 return NULL;
2069 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2070 if (ret != NULL)
2071 return ret;
2072
2073 /* Search in parent scope for lambda */
2074 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2075}
2076
2077/*
2078 * Find variable "varname" in hashtab "ht" with name "htname".
2079 * Returns NULL if not found.
2080 */
2081 dictitem_T *
2082find_var_in_ht(
2083 hashtab_T *ht,
2084 int htname,
2085 char_u *varname,
2086 int no_autoload)
2087{
2088 hashitem_T *hi;
2089
2090 if (*varname == NUL)
2091 {
2092 // Must be something like "s:", otherwise "ht" would be NULL.
2093 switch (htname)
2094 {
2095 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2096 case 'g': return &globvars_var;
2097 case 'v': return &vimvars_var;
2098 case 'b': return &curbuf->b_bufvar;
2099 case 'w': return &curwin->w_winvar;
2100 case 't': return &curtab->tp_winvar;
2101 case 'l': return get_funccal_local_var();
2102 case 'a': return get_funccal_args_var();
2103 }
2104 return NULL;
2105 }
2106
2107 hi = hash_find(ht, varname);
2108 if (HASHITEM_EMPTY(hi))
2109 {
2110 // For global variables we may try auto-loading the script. If it
2111 // worked find the variable again. Don't auto-load a script if it was
2112 // loaded already, otherwise it would be loaded every time when
2113 // checking if a function name is a Funcref variable.
2114 if (ht == &globvarht && !no_autoload)
2115 {
2116 // Note: script_autoload() may make "hi" invalid. It must either
2117 // be obtained again or not used.
2118 if (!script_autoload(varname, FALSE) || aborting())
2119 return NULL;
2120 hi = hash_find(ht, varname);
2121 }
2122 if (HASHITEM_EMPTY(hi))
2123 return NULL;
2124 }
2125 return HI2DI(hi);
2126}
2127
2128/*
2129 * Find the hashtab used for a variable name.
2130 * Return NULL if the name is not valid.
2131 * Set "varname" to the start of name without ':'.
2132 */
2133 hashtab_T *
2134find_var_ht(char_u *name, char_u **varname)
2135{
2136 hashitem_T *hi;
2137 hashtab_T *ht;
2138
2139 if (name[0] == NUL)
2140 return NULL;
2141 if (name[1] != ':')
2142 {
2143 // The name must not start with a colon or #.
2144 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2145 return NULL;
2146 *varname = name;
2147
2148 // "version" is "v:version" in all scopes if scriptversion < 3.
2149 // Same for a few other variables marked with VV_COMPAT.
2150 if (current_sctx.sc_version < 3)
2151 {
2152 hi = hash_find(&compat_hashtab, name);
2153 if (!HASHITEM_EMPTY(hi))
2154 return &compat_hashtab;
2155 }
2156
2157 ht = get_funccal_local_ht();
2158 if (ht == NULL)
2159 return &globvarht; // global variable
2160 return ht; // local variable
2161 }
2162 *varname = name + 2;
2163 if (*name == 'g') // global variable
2164 return &globvarht;
2165 // There must be no ':' or '#' in the rest of the name, unless g: is used
2166 if (vim_strchr(name + 2, ':') != NULL
2167 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2168 return NULL;
2169 if (*name == 'b') // buffer variable
2170 return &curbuf->b_vars->dv_hashtab;
2171 if (*name == 'w') // window variable
2172 return &curwin->w_vars->dv_hashtab;
2173 if (*name == 't') // tab page variable
2174 return &curtab->tp_vars->dv_hashtab;
2175 if (*name == 'v') // v: variable
2176 return &vimvarht;
2177 if (*name == 'a') // a: function argument
2178 return get_funccal_args_ht();
2179 if (*name == 'l') // l: local function variable
2180 return get_funccal_local_ht();
2181 if (*name == 's' // script variable
2182 && current_sctx.sc_sid > 0
2183 && current_sctx.sc_sid <= ga_scripts.ga_len)
2184 return &SCRIPT_VARS(current_sctx.sc_sid);
2185 return NULL;
2186}
2187
2188/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002189 * Get the string value of a (global/local) variable.
2190 * Note: see tv_get_string() for how long the pointer remains valid.
2191 * Returns NULL when it doesn't exist.
2192 */
2193 char_u *
2194get_var_value(char_u *name)
2195{
2196 dictitem_T *v;
2197
2198 v = find_var(name, NULL, FALSE);
2199 if (v == NULL)
2200 return NULL;
2201 return tv_get_string(&v->di_tv);
2202}
2203
2204/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002205 * Allocate a new hashtab for a sourced script. It will be used while
2206 * sourcing this script and when executing functions defined in the script.
2207 */
2208 void
2209new_script_vars(scid_T id)
2210{
2211 int i;
2212 hashtab_T *ht;
2213 scriptvar_T *sv;
2214
2215 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
2216 {
2217 /* Re-allocating ga_data means that an ht_array pointing to
2218 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
2219 * at its init value. Also reset "v_dict", it's always the same. */
2220 for (i = 1; i <= ga_scripts.ga_len; ++i)
2221 {
2222 ht = &SCRIPT_VARS(i);
2223 if (ht->ht_mask == HT_INIT_SIZE - 1)
2224 ht->ht_array = ht->ht_smallarray;
2225 sv = SCRIPT_SV(i);
2226 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
2227 }
2228
2229 while (ga_scripts.ga_len < id)
2230 {
2231 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
2232 ALLOC_CLEAR_ONE(scriptvar_T);
2233 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
2234 ++ga_scripts.ga_len;
2235 }
2236 }
2237}
2238
2239/*
2240 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2241 * point to it.
2242 */
2243 void
2244init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2245{
2246 hash_init(&dict->dv_hashtab);
2247 dict->dv_lock = 0;
2248 dict->dv_scope = scope;
2249 dict->dv_refcount = DO_NOT_FREE_CNT;
2250 dict->dv_copyID = 0;
2251 dict_var->di_tv.vval.v_dict = dict;
2252 dict_var->di_tv.v_type = VAR_DICT;
2253 dict_var->di_tv.v_lock = VAR_FIXED;
2254 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2255 dict_var->di_key[0] = NUL;
2256}
2257
2258/*
2259 * Unreference a dictionary initialized by init_var_dict().
2260 */
2261 void
2262unref_var_dict(dict_T *dict)
2263{
2264 /* Now the dict needs to be freed if no one else is using it, go back to
2265 * normal reference counting. */
2266 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2267 dict_unref(dict);
2268}
2269
2270/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002271 * Clean up a list of internal variables.
2272 * Frees all allocated variables and the value they contain.
2273 * Clears hashtab "ht", does not free it.
2274 */
2275 void
2276vars_clear(hashtab_T *ht)
2277{
2278 vars_clear_ext(ht, TRUE);
2279}
2280
2281/*
2282 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2283 */
2284 void
2285vars_clear_ext(hashtab_T *ht, int free_val)
2286{
2287 int todo;
2288 hashitem_T *hi;
2289 dictitem_T *v;
2290
2291 hash_lock(ht);
2292 todo = (int)ht->ht_used;
2293 for (hi = ht->ht_array; todo > 0; ++hi)
2294 {
2295 if (!HASHITEM_EMPTY(hi))
2296 {
2297 --todo;
2298
2299 // Free the variable. Don't remove it from the hashtab,
2300 // ht_array might change then. hash_clear() takes care of it
2301 // later.
2302 v = HI2DI(hi);
2303 if (free_val)
2304 clear_tv(&v->di_tv);
2305 if (v->di_flags & DI_FLAGS_ALLOC)
2306 vim_free(v);
2307 }
2308 }
2309 hash_clear(ht);
2310 ht->ht_used = 0;
2311}
2312
2313/*
2314 * Delete a variable from hashtab "ht" at item "hi".
2315 * Clear the variable value and free the dictitem.
2316 */
2317 void
2318delete_var(hashtab_T *ht, hashitem_T *hi)
2319{
2320 dictitem_T *di = HI2DI(hi);
2321
2322 hash_remove(ht, hi);
2323 clear_tv(&di->di_tv);
2324 vim_free(di);
2325}
2326
2327/*
2328 * List the value of one internal variable.
2329 */
2330 static void
2331list_one_var(dictitem_T *v, char *prefix, int *first)
2332{
2333 char_u *tofree;
2334 char_u *s;
2335 char_u numbuf[NUMBUFLEN];
2336
2337 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2338 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2339 s == NULL ? (char_u *)"" : s, first);
2340 vim_free(tofree);
2341}
2342
2343 static void
2344list_one_var_a(
2345 char *prefix,
2346 char_u *name,
2347 int type,
2348 char_u *string,
2349 int *first) // when TRUE clear rest of screen and set to FALSE
2350{
2351 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2352 msg_start();
2353 msg_puts(prefix);
2354 if (name != NULL) // "a:" vars don't have a name stored
2355 msg_puts((char *)name);
2356 msg_putchar(' ');
2357 msg_advance(22);
2358 if (type == VAR_NUMBER)
2359 msg_putchar('#');
2360 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2361 msg_putchar('*');
2362 else if (type == VAR_LIST)
2363 {
2364 msg_putchar('[');
2365 if (*string == '[')
2366 ++string;
2367 }
2368 else if (type == VAR_DICT)
2369 {
2370 msg_putchar('{');
2371 if (*string == '{')
2372 ++string;
2373 }
2374 else
2375 msg_putchar(' ');
2376
2377 msg_outtrans(string);
2378
2379 if (type == VAR_FUNC || type == VAR_PARTIAL)
2380 msg_puts("()");
2381 if (*first)
2382 {
2383 msg_clr_eos();
2384 *first = FALSE;
2385 }
2386}
2387
2388/*
2389 * Set variable "name" to value in "tv".
2390 * If the variable already exists, the value is updated.
2391 * Otherwise the variable is created.
2392 */
2393 void
2394set_var(
2395 char_u *name,
2396 typval_T *tv,
2397 int copy) // make copy of value in "tv"
2398{
2399 set_var_const(name, tv, copy, FALSE);
2400}
2401
2402/*
2403 * Set variable "name" to value in "tv".
2404 * If the variable already exists and "is_const" is FALSE the value is updated.
2405 * Otherwise the variable is created.
2406 */
2407 void
2408set_var_const(
2409 char_u *name,
2410 typval_T *tv,
2411 int copy, // make copy of value in "tv"
2412 int is_const) // disallow to modify existing variable
2413{
2414 dictitem_T *v;
2415 char_u *varname;
2416 hashtab_T *ht;
2417
2418 ht = find_var_ht(name, &varname);
2419 if (ht == NULL || *varname == NUL)
2420 {
2421 semsg(_(e_illvar), name);
2422 return;
2423 }
2424 v = find_var_in_ht(ht, 0, varname, TRUE);
2425
2426 // Search in parent scope which is possible to reference from lambda
2427 if (v == NULL)
2428 v = find_var_in_scoped_ht(name, TRUE);
2429
2430 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
2431 && var_check_func_name(name, v == NULL))
2432 return;
2433
2434 if (v != NULL)
2435 {
2436 if (is_const)
2437 {
2438 emsg(_(e_cannot_mod));
2439 return;
2440 }
2441
2442 // existing variable, need to clear the value
2443 if (var_check_ro(v->di_flags, name, FALSE)
2444 || var_check_lock(v->di_tv.v_lock, name, FALSE))
2445 return;
2446
2447 // Handle setting internal v: variables separately where needed to
2448 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002449 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002450 {
2451 if (v->di_tv.v_type == VAR_STRING)
2452 {
2453 VIM_CLEAR(v->di_tv.vval.v_string);
2454 if (copy || tv->v_type != VAR_STRING)
2455 {
2456 char_u *val = tv_get_string(tv);
2457
2458 // Careful: when assigning to v:errmsg and tv_get_string()
2459 // causes an error message the variable will alrady be set.
2460 if (v->di_tv.vval.v_string == NULL)
2461 v->di_tv.vval.v_string = vim_strsave(val);
2462 }
2463 else
2464 {
2465 // Take over the string to avoid an extra alloc/free.
2466 v->di_tv.vval.v_string = tv->vval.v_string;
2467 tv->vval.v_string = NULL;
2468 }
2469 return;
2470 }
2471 else if (v->di_tv.v_type == VAR_NUMBER)
2472 {
2473 v->di_tv.vval.v_number = tv_get_number(tv);
2474 if (STRCMP(varname, "searchforward") == 0)
2475 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
2476#ifdef FEAT_SEARCH_EXTRA
2477 else if (STRCMP(varname, "hlsearch") == 0)
2478 {
2479 no_hlsearch = !v->di_tv.vval.v_number;
2480 redraw_all_later(SOME_VALID);
2481 }
2482#endif
2483 return;
2484 }
2485 else if (v->di_tv.v_type != tv->v_type)
2486 {
2487 semsg(_("E963: setting %s to value with wrong type"), name);
2488 return;
2489 }
2490 }
2491
2492 clear_tv(&v->di_tv);
2493 }
2494 else // add a new variable
2495 {
2496 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002497 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002498 {
2499 semsg(_(e_illvar), name);
2500 return;
2501 }
2502
2503 // Make sure the variable name is valid.
2504 if (!valid_varname(varname))
2505 return;
2506
2507 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
2508 if (v == NULL)
2509 return;
2510 STRCPY(v->di_key, varname);
2511 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
2512 {
2513 vim_free(v);
2514 return;
2515 }
2516 v->di_flags = DI_FLAGS_ALLOC;
2517 if (is_const)
2518 v->di_flags |= DI_FLAGS_LOCK;
2519 }
2520
2521 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
2522 copy_tv(tv, &v->di_tv);
2523 else
2524 {
2525 v->di_tv = *tv;
2526 v->di_tv.v_lock = 0;
2527 init_tv(tv);
2528 }
2529
2530 if (is_const)
2531 v->di_tv.v_lock |= VAR_LOCKED;
2532}
2533
2534/*
2535 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2536 * Also give an error message.
2537 */
2538 int
2539var_check_ro(int flags, char_u *name, int use_gettext)
2540{
2541 if (flags & DI_FLAGS_RO)
2542 {
2543 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
2544 return TRUE;
2545 }
2546 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
2547 {
2548 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
2549 return TRUE;
2550 }
2551 return FALSE;
2552}
2553
2554/*
2555 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
2556 * Also give an error message.
2557 */
2558 int
2559var_check_fixed(int flags, char_u *name, int use_gettext)
2560{
2561 if (flags & DI_FLAGS_FIX)
2562 {
2563 semsg(_("E795: Cannot delete variable %s"),
2564 use_gettext ? (char_u *)_(name) : name);
2565 return TRUE;
2566 }
2567 return FALSE;
2568}
2569
2570/*
2571 * Check if a funcref is assigned to a valid variable name.
2572 * Return TRUE and give an error if not.
2573 */
2574 int
2575var_check_func_name(
2576 char_u *name, // points to start of variable name
2577 int new_var) // TRUE when creating the variable
2578{
2579 // Allow for w: b: s: and t:.
2580 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
2581 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
2582 ? name[2] : name[0]))
2583 {
2584 semsg(_("E704: Funcref variable name must start with a capital: %s"),
2585 name);
2586 return TRUE;
2587 }
2588 // Don't allow hiding a function. When "v" is not NULL we might be
2589 // assigning another function to the same var, the type is checked
2590 // below.
2591 if (new_var && function_exists(name, FALSE))
2592 {
2593 semsg(_("E705: Variable name conflicts with existing function: %s"),
2594 name);
2595 return TRUE;
2596 }
2597 return FALSE;
2598}
2599
2600/*
2601 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
2602 * Also give an error message, using "name" or _("name") when use_gettext is
2603 * TRUE.
2604 */
2605 int
2606var_check_lock(int lock, char_u *name, int use_gettext)
2607{
2608 if (lock & VAR_LOCKED)
2609 {
2610 semsg(_("E741: Value is locked: %s"),
2611 name == NULL ? (char_u *)_("Unknown")
2612 : use_gettext ? (char_u *)_(name)
2613 : name);
2614 return TRUE;
2615 }
2616 if (lock & VAR_FIXED)
2617 {
2618 semsg(_("E742: Cannot change value of %s"),
2619 name == NULL ? (char_u *)_("Unknown")
2620 : use_gettext ? (char_u *)_(name)
2621 : name);
2622 return TRUE;
2623 }
2624 return FALSE;
2625}
2626
2627/*
2628 * Check if a variable name is valid.
2629 * Return FALSE and give an error if not.
2630 */
2631 int
2632valid_varname(char_u *varname)
2633{
2634 char_u *p;
2635
2636 for (p = varname; *p != NUL; ++p)
2637 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
2638 && *p != AUTOLOAD_CHAR)
2639 {
2640 semsg(_(e_illvar), varname);
2641 return FALSE;
2642 }
2643 return TRUE;
2644}
2645
2646/*
2647 * getwinvar() and gettabwinvar()
2648 */
2649 static void
2650getwinvar(
2651 typval_T *argvars,
2652 typval_T *rettv,
2653 int off) // 1 for gettabwinvar()
2654{
2655 win_T *win;
2656 char_u *varname;
2657 dictitem_T *v;
2658 tabpage_T *tp = NULL;
2659 int done = FALSE;
2660 win_T *oldcurwin;
2661 tabpage_T *oldtabpage;
2662 int need_switch_win;
2663
2664 if (off == 1)
2665 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
2666 else
2667 tp = curtab;
2668 win = find_win_by_nr(&argvars[off], tp);
2669 varname = tv_get_string_chk(&argvars[off + 1]);
2670 ++emsg_off;
2671
2672 rettv->v_type = VAR_STRING;
2673 rettv->vval.v_string = NULL;
2674
2675 if (win != NULL && varname != NULL)
2676 {
2677 // Set curwin to be our win, temporarily. Also set the tabpage,
2678 // otherwise the window is not valid. Only do this when needed,
2679 // autocommands get blocked.
2680 need_switch_win = !(tp == curtab && win == curwin);
2681 if (!need_switch_win
2682 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
2683 {
2684 if (*varname == '&')
2685 {
2686 if (varname[1] == NUL)
2687 {
2688 // get all window-local options in a dict
2689 dict_T *opts = get_winbuf_options(FALSE);
2690
2691 if (opts != NULL)
2692 {
2693 rettv_dict_set(rettv, opts);
2694 done = TRUE;
2695 }
2696 }
2697 else if (get_option_tv(&varname, rettv, 1) == OK)
2698 // window-local-option
2699 done = TRUE;
2700 }
2701 else
2702 {
2703 // Look up the variable.
2704 // Let getwinvar({nr}, "") return the "w:" dictionary.
2705 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
2706 varname, FALSE);
2707 if (v != NULL)
2708 {
2709 copy_tv(&v->di_tv, rettv);
2710 done = TRUE;
2711 }
2712 }
2713 }
2714
2715 if (need_switch_win)
2716 // restore previous notion of curwin
2717 restore_win(oldcurwin, oldtabpage, TRUE);
2718 }
2719
2720 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
2721 // use the default return value
2722 copy_tv(&argvars[off + 2], rettv);
2723
2724 --emsg_off;
2725}
2726
2727/*
2728 * "setwinvar()" and "settabwinvar()" functions
2729 */
2730 static void
2731setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
2732{
2733 win_T *win;
2734 win_T *save_curwin;
2735 tabpage_T *save_curtab;
2736 int need_switch_win;
2737 char_u *varname, *winvarname;
2738 typval_T *varp;
2739 char_u nbuf[NUMBUFLEN];
2740 tabpage_T *tp = NULL;
2741
2742 if (check_secure())
2743 return;
2744
2745 if (off == 1)
2746 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
2747 else
2748 tp = curtab;
2749 win = find_win_by_nr(&argvars[off], tp);
2750 varname = tv_get_string_chk(&argvars[off + 1]);
2751 varp = &argvars[off + 2];
2752
2753 if (win != NULL && varname != NULL && varp != NULL)
2754 {
2755 need_switch_win = !(tp == curtab && win == curwin);
2756 if (!need_switch_win
2757 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
2758 {
2759 if (*varname == '&')
2760 {
2761 long numval;
2762 char_u *strval;
2763 int error = FALSE;
2764
2765 ++varname;
2766 numval = (long)tv_get_number_chk(varp, &error);
2767 strval = tv_get_string_buf_chk(varp, nbuf);
2768 if (!error && strval != NULL)
2769 set_option_value(varname, numval, strval, OPT_LOCAL);
2770 }
2771 else
2772 {
2773 winvarname = alloc(STRLEN(varname) + 3);
2774 if (winvarname != NULL)
2775 {
2776 STRCPY(winvarname, "w:");
2777 STRCPY(winvarname + 2, varname);
2778 set_var(winvarname, varp, TRUE);
2779 vim_free(winvarname);
2780 }
2781 }
2782 }
2783 if (need_switch_win)
2784 restore_win(save_curwin, save_curtab, TRUE);
2785 }
2786}
2787
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002788/*
2789 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
2790 * v:option_type, and v:option_command.
2791 */
2792 void
2793reset_v_option_vars(void)
2794{
2795 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
2796 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
2797 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
2798 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
2799 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
2800 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
2801}
2802
2803/*
2804 * Add an assert error to v:errors.
2805 */
2806 void
2807assert_error(garray_T *gap)
2808{
2809 struct vimvar *vp = &vimvars[VV_ERRORS];
2810
2811 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
2812 /* Make sure v:errors is a list. */
2813 set_vim_var_list(VV_ERRORS, list_alloc());
2814 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
2815}
2816
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002817 int
2818var_exists(char_u *var)
2819{
2820 char_u *name;
2821 char_u *tofree;
2822 typval_T tv;
2823 int len = 0;
2824 int n = FALSE;
2825
2826 // get_name_len() takes care of expanding curly braces
2827 name = var;
2828 len = get_name_len(&var, &tofree, TRUE, FALSE);
2829 if (len > 0)
2830 {
2831 if (tofree != NULL)
2832 name = tofree;
2833 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
2834 if (n)
2835 {
2836 // handle d.key, l[idx], f(expr)
2837 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
2838 if (n)
2839 clear_tv(&tv);
2840 }
2841 }
2842 if (*var != NUL)
2843 n = FALSE;
2844
2845 vim_free(tofree);
2846 return n;
2847}
2848
2849/*
2850 * "gettabvar()" function
2851 */
2852 void
2853f_gettabvar(typval_T *argvars, typval_T *rettv)
2854{
2855 win_T *oldcurwin;
2856 tabpage_T *tp, *oldtabpage;
2857 dictitem_T *v;
2858 char_u *varname;
2859 int done = FALSE;
2860
2861 rettv->v_type = VAR_STRING;
2862 rettv->vval.v_string = NULL;
2863
2864 varname = tv_get_string_chk(&argvars[1]);
2865 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
2866 if (tp != NULL && varname != NULL)
2867 {
2868 // Set tp to be our tabpage, temporarily. Also set the window to the
2869 // first window in the tabpage, otherwise the window is not valid.
2870 if (switch_win(&oldcurwin, &oldtabpage,
2871 tp == curtab || tp->tp_firstwin == NULL ? firstwin
2872 : tp->tp_firstwin, tp, TRUE) == OK)
2873 {
2874 // look up the variable
2875 // Let gettabvar({nr}, "") return the "t:" dictionary.
2876 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
2877 if (v != NULL)
2878 {
2879 copy_tv(&v->di_tv, rettv);
2880 done = TRUE;
2881 }
2882 }
2883
2884 // restore previous notion of curwin
2885 restore_win(oldcurwin, oldtabpage, TRUE);
2886 }
2887
2888 if (!done && argvars[2].v_type != VAR_UNKNOWN)
2889 copy_tv(&argvars[2], rettv);
2890}
2891
2892/*
2893 * "gettabwinvar()" function
2894 */
2895 void
2896f_gettabwinvar(typval_T *argvars, typval_T *rettv)
2897{
2898 getwinvar(argvars, rettv, 1);
2899}
2900
2901/*
2902 * "getwinvar()" function
2903 */
2904 void
2905f_getwinvar(typval_T *argvars, typval_T *rettv)
2906{
2907 getwinvar(argvars, rettv, 0);
2908}
2909
2910/*
2911 * "settabvar()" function
2912 */
2913 void
2914f_settabvar(typval_T *argvars, typval_T *rettv)
2915{
2916 tabpage_T *save_curtab;
2917 tabpage_T *tp;
2918 char_u *varname, *tabvarname;
2919 typval_T *varp;
2920
2921 rettv->vval.v_number = 0;
2922
2923 if (check_secure())
2924 return;
2925
2926 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
2927 varname = tv_get_string_chk(&argvars[1]);
2928 varp = &argvars[2];
2929
2930 if (varname != NULL && varp != NULL && tp != NULL)
2931 {
2932 save_curtab = curtab;
2933 goto_tabpage_tp(tp, FALSE, FALSE);
2934
2935 tabvarname = alloc(STRLEN(varname) + 3);
2936 if (tabvarname != NULL)
2937 {
2938 STRCPY(tabvarname, "t:");
2939 STRCPY(tabvarname + 2, varname);
2940 set_var(tabvarname, varp, TRUE);
2941 vim_free(tabvarname);
2942 }
2943
2944 // Restore current tabpage
2945 if (valid_tabpage(save_curtab))
2946 goto_tabpage_tp(save_curtab, FALSE, FALSE);
2947 }
2948}
2949
2950/*
2951 * "settabwinvar()" function
2952 */
2953 void
2954f_settabwinvar(typval_T *argvars, typval_T *rettv)
2955{
2956 setwinvar(argvars, rettv, 1);
2957}
2958
2959/*
2960 * "setwinvar()" function
2961 */
2962 void
2963f_setwinvar(typval_T *argvars, typval_T *rettv)
2964{
2965 setwinvar(argvars, rettv, 0);
2966}
2967
2968#endif // FEAT_EVAL