blob: d359c7f44cc332cc5deaf3bd7fcbdc6c4ad37f13 [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:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020021static dict_T globvardict; // Dictionary with g: variables
22#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020023
24/*
25 * Old Vim variables such as "v:version" are also available without the "v:".
26 * Also in functions. We need a special hashtable for them.
27 */
28static hashtab_T compat_hashtab;
29
30/*
31 * Array to hold the value of v: variables.
32 * The value is in a dictitem, so that it can also be used in the v: scope.
33 * The reason to use this table anyway is for very quick access to the
34 * variables with the VV_ defines.
35 */
36
37// values for vv_flags:
38#define VV_COMPAT 1 // compatible, also used without "v:"
39#define VV_RO 2 // read-only
40#define VV_RO_SBX 4 // read-only in the sandbox
41
42#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
43
44static 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("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},
Bram Moolenaar69bf6342019-10-29 04:16:57 +0100146 {VV_NAME("argv", VAR_LIST), VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200147};
148
149// shorthand
150#define vv_type vv_di.di_tv.v_type
151#define vv_nr vv_di.di_tv.vval.v_number
152#define vv_float vv_di.di_tv.vval.v_float
153#define vv_str vv_di.di_tv.vval.v_string
154#define vv_list vv_di.di_tv.vval.v_list
155#define vv_dict vv_di.di_tv.vval.v_dict
156#define vv_blob vv_di.di_tv.vval.v_blob
157#define vv_tv vv_di.di_tv
158
159static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200160static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200161#define vimvarht vimvardict.dv_hashtab
162
163// for VIM_VERSION_ defines
164#include "version.h"
165
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100166#define SCRIPT_SV(id) (SCRIPT_ITEM(id).sn_vars)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200167#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
168
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200169static void ex_let_const(exarg_T *eap, int is_const);
170static char_u *skip_var_one(char_u *arg);
171static 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);
176static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op);
177static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
178static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
179static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
180static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200181static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200182static void list_one_var(dictitem_T *v, char *prefix, int *first);
183static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
184
185/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200186 * Initialize global and vim special variables
187 */
188 void
189evalvars_init(void)
190{
191 int i;
192 struct vimvar *p;
193
194 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
195 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
196 vimvardict.dv_lock = VAR_FIXED;
197 hash_init(&compat_hashtab);
198
199 for (i = 0; i < VV_LEN; ++i)
200 {
201 p = &vimvars[i];
202 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
203 {
204 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
205 getout(1);
206 }
207 STRCPY(p->vv_di.di_key, p->vv_name);
208 if (p->vv_flags & VV_RO)
209 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
210 else if (p->vv_flags & VV_RO_SBX)
211 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
212 else
213 p->vv_di.di_flags = DI_FLAGS_FIX;
214
215 // add to v: scope dict, unless the value is not always available
216 if (p->vv_type != VAR_UNKNOWN)
217 hash_add(&vimvarht, p->vv_di.di_key);
218 if (p->vv_flags & VV_COMPAT)
219 // add to compat scope dict
220 hash_add(&compat_hashtab, p->vv_di.di_key);
221 }
222 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
223 vimvars[VV_VERSIONLONG].vv_nr = VIM_VERSION_100 * 10000 + highest_patch();
224
225 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
226 set_vim_var_nr(VV_HLSEARCH, 1L);
227 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
228 set_vim_var_list(VV_ERRORS, list_alloc());
229 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
230
231 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
232 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
233 set_vim_var_nr(VV_NONE, VVAL_NONE);
234 set_vim_var_nr(VV_NULL, VVAL_NULL);
235
236 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
237 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
238 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
239 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
240 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
241 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
242 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
243 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
244 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
245 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
246 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
247
248 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
249
250 set_reg_var(0); // default for v:register is not 0 but '"'
251}
252
253#if defined(EXITFREE) || defined(PROTO)
254/*
255 * Free all vim variables information on exit
256 */
257 void
258evalvars_clear(void)
259{
260 int i;
261 struct vimvar *p;
262
263 for (i = 0; i < VV_LEN; ++i)
264 {
265 p = &vimvars[i];
266 if (p->vv_di.di_tv.v_type == VAR_STRING)
267 VIM_CLEAR(p->vv_str);
268 else if (p->vv_di.di_tv.v_type == VAR_LIST)
269 {
270 list_unref(p->vv_list);
271 p->vv_list = NULL;
272 }
273 }
274 hash_clear(&vimvarht);
275 hash_init(&vimvarht); // garbage_collect() will access it
276 hash_clear(&compat_hashtab);
277
278 // global variables
279 vars_clear(&globvarht);
280
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100281 // Script-local variables. Clear all the variables here.
282 // The scriptvar_T is cleared later in free_scriptnames(), because a
283 // variable in one script might hold a reference to the whole scope of
284 // another script.
285 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200286 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200287}
288#endif
289
290 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200291garbage_collect_globvars(int copyID)
292{
293 return set_ref_in_ht(&globvarht, copyID, NULL);
294}
295
296 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200297garbage_collect_vimvars(int copyID)
298{
299 return set_ref_in_ht(&vimvarht, copyID, NULL);
300}
301
302 int
303garbage_collect_scriptvars(int copyID)
304{
305 int i;
306 int abort = FALSE;
307
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100308 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200309 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
310
311 return abort;
312}
313
314/*
315 * Set an internal variable to a string value. Creates the variable if it does
316 * not already exist.
317 */
318 void
319set_internal_string_var(char_u *name, char_u *value)
320{
321 char_u *val;
322 typval_T *tvp;
323
324 val = vim_strsave(value);
325 if (val != NULL)
326 {
327 tvp = alloc_string_tv(val);
328 if (tvp != NULL)
329 {
330 set_var(name, tvp, FALSE);
331 free_tv(tvp);
332 }
333 }
334}
335
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200336 int
337eval_charconvert(
338 char_u *enc_from,
339 char_u *enc_to,
340 char_u *fname_from,
341 char_u *fname_to)
342{
343 int err = FALSE;
344
345 set_vim_var_string(VV_CC_FROM, enc_from, -1);
346 set_vim_var_string(VV_CC_TO, enc_to, -1);
347 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
348 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
349 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
350 err = TRUE;
351 set_vim_var_string(VV_CC_FROM, NULL, -1);
352 set_vim_var_string(VV_CC_TO, NULL, -1);
353 set_vim_var_string(VV_FNAME_IN, NULL, -1);
354 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
355
356 if (err)
357 return FAIL;
358 return OK;
359}
360
361# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
362 int
363eval_printexpr(char_u *fname, char_u *args)
364{
365 int err = FALSE;
366
367 set_vim_var_string(VV_FNAME_IN, fname, -1);
368 set_vim_var_string(VV_CMDARG, args, -1);
369 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
370 err = TRUE;
371 set_vim_var_string(VV_FNAME_IN, NULL, -1);
372 set_vim_var_string(VV_CMDARG, NULL, -1);
373
374 if (err)
375 {
376 mch_remove(fname);
377 return FAIL;
378 }
379 return OK;
380}
381# endif
382
383# if defined(FEAT_DIFF) || defined(PROTO)
384 void
385eval_diff(
386 char_u *origfile,
387 char_u *newfile,
388 char_u *outfile)
389{
390 int err = FALSE;
391
392 set_vim_var_string(VV_FNAME_IN, origfile, -1);
393 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
394 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
395 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
396 set_vim_var_string(VV_FNAME_IN, NULL, -1);
397 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
398 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
399}
400
401 void
402eval_patch(
403 char_u *origfile,
404 char_u *difffile,
405 char_u *outfile)
406{
407 int err;
408
409 set_vim_var_string(VV_FNAME_IN, origfile, -1);
410 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
411 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
412 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
413 set_vim_var_string(VV_FNAME_IN, NULL, -1);
414 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
415 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
416}
417# endif
418
419#if defined(FEAT_SPELL) || defined(PROTO)
420/*
421 * Evaluate an expression to a list with suggestions.
422 * For the "expr:" part of 'spellsuggest'.
423 * Returns NULL when there is an error.
424 */
425 list_T *
426eval_spell_expr(char_u *badword, char_u *expr)
427{
428 typval_T save_val;
429 typval_T rettv;
430 list_T *list = NULL;
431 char_u *p = skipwhite(expr);
432
433 // Set "v:val" to the bad word.
434 prepare_vimvar(VV_VAL, &save_val);
435 set_vim_var_string(VV_VAL, badword, -1);
436 if (p_verbose == 0)
437 ++emsg_off;
438
439 if (eval1(&p, &rettv, TRUE) == OK)
440 {
441 if (rettv.v_type != VAR_LIST)
442 clear_tv(&rettv);
443 else
444 list = rettv.vval.v_list;
445 }
446
447 if (p_verbose == 0)
448 --emsg_off;
449 clear_tv(get_vim_var_tv(VV_VAL));
450 restore_vimvar(VV_VAL, &save_val);
451
452 return list;
453}
454
455/*
456 * "list" is supposed to contain two items: a word and a number. Return the
457 * word in "pp" and the number as the return value.
458 * Return -1 if anything isn't right.
459 * Used to get the good word and score from the eval_spell_expr() result.
460 */
461 int
462get_spellword(list_T *list, char_u **pp)
463{
464 listitem_T *li;
465
466 li = list->lv_first;
467 if (li == NULL)
468 return -1;
469 *pp = tv_get_string(&li->li_tv);
470
471 li = li->li_next;
472 if (li == NULL)
473 return -1;
474 return (int)tv_get_number(&li->li_tv);
475}
476#endif
477
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200478/*
479 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200480 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200481 * When not used yet add the variable to the v: hashtable.
482 */
483 void
484prepare_vimvar(int idx, typval_T *save_tv)
485{
486 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200487 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200488 if (vimvars[idx].vv_type == VAR_UNKNOWN)
489 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
490}
491
492/*
493 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200494 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200495 * When no longer defined, remove the variable from the v: hashtable.
496 */
497 void
498restore_vimvar(int idx, typval_T *save_tv)
499{
500 hashitem_T *hi;
501
502 vimvars[idx].vv_tv = *save_tv;
503 if (vimvars[idx].vv_type == VAR_UNKNOWN)
504 {
505 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
506 if (HASHITEM_EMPTY(hi))
507 internal_error("restore_vimvar()");
508 else
509 hash_remove(&vimvarht, hi);
510 }
511}
512
513/*
514 * List Vim variables.
515 */
516 static void
517list_vim_vars(int *first)
518{
519 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
520}
521
522/*
523 * List script-local variables, if there is a script.
524 */
525 static void
526list_script_vars(int *first)
527{
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100528 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200529 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
530 "s:", FALSE, first);
531}
532
533/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200534 * Get a list of lines from a HERE document. The here document is a list of
535 * lines surrounded by a marker.
536 * cmd << {marker}
537 * {line1}
538 * {line2}
539 * ....
540 * {marker}
541 *
542 * The {marker} is a string. If the optional 'trim' word is supplied before the
543 * marker, then the leading indentation before the lines (matching the
544 * indentation in the 'cmd' line) is stripped.
545 * Returns a List with {lines} or NULL.
546 */
547 static list_T *
548heredoc_get(exarg_T *eap, char_u *cmd)
549{
550 char_u *theline;
551 char_u *marker;
552 list_T *l;
553 char_u *p;
554 int marker_indent_len = 0;
555 int text_indent_len = 0;
556 char_u *text_indent = NULL;
557
558 if (eap->getline == NULL)
559 {
560 emsg(_("E991: cannot use =<< here"));
561 return NULL;
562 }
563
564 // Check for the optional 'trim' word before the marker
565 cmd = skipwhite(cmd);
566 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
567 {
568 cmd = skipwhite(cmd + 4);
569
570 // Trim the indentation from all the lines in the here document.
571 // The amount of indentation trimmed is the same as the indentation of
572 // the first line after the :let command line. To find the end marker
573 // the indent of the :let command line is trimmed.
574 p = *eap->cmdlinep;
575 while (VIM_ISWHITE(*p))
576 {
577 p++;
578 marker_indent_len++;
579 }
580 text_indent_len = -1;
581 }
582
583 // The marker is the next word.
584 if (*cmd != NUL && *cmd != '"')
585 {
586 marker = skipwhite(cmd);
587 p = skiptowhite(marker);
588 if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
589 {
590 emsg(_(e_trailing));
591 return NULL;
592 }
593 *p = NUL;
594 if (vim_islower(*marker))
595 {
596 emsg(_("E221: Marker cannot start with lower case letter"));
597 return NULL;
598 }
599 }
600 else
601 {
602 emsg(_("E172: Missing marker"));
603 return NULL;
604 }
605
606 l = list_alloc();
607 if (l == NULL)
608 return NULL;
609
610 for (;;)
611 {
612 int mi = 0;
613 int ti = 0;
614
615 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
616 if (theline == NULL)
617 {
618 semsg(_("E990: Missing end marker '%s'"), marker);
619 break;
620 }
621
622 // with "trim": skip the indent matching the :let line to find the
623 // marker
624 if (marker_indent_len > 0
625 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
626 mi = marker_indent_len;
627 if (STRCMP(marker, theline + mi) == 0)
628 {
629 vim_free(theline);
630 break;
631 }
632
633 if (text_indent_len == -1 && *theline != NUL)
634 {
635 // set the text indent from the first line.
636 p = theline;
637 text_indent_len = 0;
638 while (VIM_ISWHITE(*p))
639 {
640 p++;
641 text_indent_len++;
642 }
643 text_indent = vim_strnsave(theline, text_indent_len);
644 }
645 // with "trim": skip the indent matching the first line
646 if (text_indent != NULL)
647 for (ti = 0; ti < text_indent_len; ++ti)
648 if (theline[ti] != text_indent[ti])
649 break;
650
651 if (list_append_string(l, theline + ti, -1) == FAIL)
652 break;
653 vim_free(theline);
654 }
655 vim_free(text_indent);
656
657 return l;
658}
659
660/*
661 * ":let" list all variable values
662 * ":let var1 var2" list variable values
663 * ":let var = expr" assignment command.
664 * ":let var += expr" assignment command.
665 * ":let var -= expr" assignment command.
666 * ":let var *= expr" assignment command.
667 * ":let var /= expr" assignment command.
668 * ":let var %= expr" assignment command.
669 * ":let var .= expr" assignment command.
670 * ":let var ..= expr" assignment command.
671 * ":let [var1, var2] = expr" unpack list.
672 */
673 void
674ex_let(exarg_T *eap)
675{
676 ex_let_const(eap, FALSE);
677}
678
679/*
680 * ":const" list all variable values
681 * ":const var1 var2" list variable values
682 * ":const var = expr" assignment command.
683 * ":const [var1, var2] = expr" unpack list.
684 */
685 void
686ex_const(exarg_T *eap)
687{
688 ex_let_const(eap, TRUE);
689}
690
691 static void
692ex_let_const(exarg_T *eap, int is_const)
693{
694 char_u *arg = eap->arg;
695 char_u *expr = NULL;
696 typval_T rettv;
697 int i;
698 int var_count = 0;
699 int semicolon = 0;
700 char_u op[2];
701 char_u *argend;
702 int first = TRUE;
703 int concat;
704
705 argend = skip_var_list(arg, &var_count, &semicolon);
706 if (argend == NULL)
707 return;
708 if (argend > arg && argend[-1] == '.') // for var.='str'
709 --argend;
710 expr = skipwhite(argend);
711 concat = expr[0] == '.'
712 && ((expr[1] == '=' && current_sctx.sc_version < 2)
713 || (expr[1] == '.' && expr[2] == '='));
714 if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL
715 && expr[1] == '=') || concat))
716 {
717 // ":let" without "=": list variables
718 if (*arg == '[')
719 emsg(_(e_invarg));
720 else if (expr[0] == '.')
721 emsg(_("E985: .= is not supported with script version 2"));
722 else if (!ends_excmd(*arg))
723 // ":let var1 var2"
724 arg = list_arg_vars(eap, arg, &first);
725 else if (!eap->skip)
726 {
727 // ":let"
728 list_glob_vars(&first);
729 list_buf_vars(&first);
730 list_win_vars(&first);
731 list_tab_vars(&first);
732 list_script_vars(&first);
733 list_func_vars(&first);
734 list_vim_vars(&first);
735 }
736 eap->nextcmd = check_nextcmd(arg);
737 }
738 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
739 {
740 list_T *l;
741
742 // HERE document
743 l = heredoc_get(eap, expr + 3);
744 if (l != NULL)
745 {
746 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200747 if (!eap->skip)
748 {
749 op[0] = '=';
750 op[1] = NUL;
751 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200752 is_const, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200753 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200754 clear_tv(&rettv);
755 }
756 }
757 else
758 {
759 op[0] = '=';
760 op[1] = NUL;
761 if (*expr != '=')
762 {
763 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
764 {
765 op[0] = *expr; // +=, -=, *=, /=, %= or .=
766 if (expr[0] == '.' && expr[1] == '.') // ..=
767 ++expr;
768 }
769 expr = skipwhite(expr + 2);
770 }
771 else
772 expr = skipwhite(expr + 1);
773
774 if (eap->skip)
775 ++emsg_skip;
776 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
777 if (eap->skip)
778 {
779 if (i != FAIL)
780 clear_tv(&rettv);
781 --emsg_skip;
782 }
783 else if (i != FAIL)
784 {
785 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
786 is_const, op);
787 clear_tv(&rettv);
788 }
789 }
790}
791
792/*
793 * Assign the typevalue "tv" to the variable or variables at "arg_start".
794 * Handles both "var" with any type and "[var, var; var]" with a list type.
795 * When "op" is not NULL it points to a string with characters that
796 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
797 * or concatenate.
798 * Returns OK or FAIL;
799 */
800 int
801ex_let_vars(
802 char_u *arg_start,
803 typval_T *tv,
804 int copy, // copy values from "tv", don't move
805 int semicolon, // from skip_var_list()
806 int var_count, // from skip_var_list()
807 int is_const, // lock variables for const
808 char_u *op)
809{
810 char_u *arg = arg_start;
811 list_T *l;
812 int i;
813 listitem_T *item;
814 typval_T ltv;
815
816 if (*arg != '[')
817 {
818 // ":let var = expr" or ":for var in list"
819 if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL)
820 return FAIL;
821 return OK;
822 }
823
824 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
825 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
826 {
827 emsg(_(e_listreq));
828 return FAIL;
829 }
830
831 i = list_len(l);
832 if (semicolon == 0 && var_count < i)
833 {
834 emsg(_("E687: Less targets than List items"));
835 return FAIL;
836 }
837 if (var_count - semicolon > i)
838 {
839 emsg(_("E688: More targets than List items"));
840 return FAIL;
841 }
842
843 item = l->lv_first;
844 while (*arg != ']')
845 {
846 arg = skipwhite(arg + 1);
847 arg = ex_let_one(arg, &item->li_tv, TRUE, is_const,
848 (char_u *)",;]", op);
849 item = item->li_next;
850 if (arg == NULL)
851 return FAIL;
852
853 arg = skipwhite(arg);
854 if (*arg == ';')
855 {
856 // Put the rest of the list (may be empty) in the var after ';'.
857 // Create a new list for this.
858 l = list_alloc();
859 if (l == NULL)
860 return FAIL;
861 while (item != NULL)
862 {
863 list_append_tv(l, &item->li_tv);
864 item = item->li_next;
865 }
866
867 ltv.v_type = VAR_LIST;
868 ltv.v_lock = 0;
869 ltv.vval.v_list = l;
870 l->lv_refcount = 1;
871
872 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE, is_const,
873 (char_u *)"]", op);
874 clear_tv(&ltv);
875 if (arg == NULL)
876 return FAIL;
877 break;
878 }
879 else if (*arg != ',' && *arg != ']')
880 {
881 internal_error("ex_let_vars()");
882 return FAIL;
883 }
884 }
885
886 return OK;
887}
888
889/*
890 * Skip over assignable variable "var" or list of variables "[var, var]".
891 * Used for ":let varvar = expr" and ":for varvar in expr".
892 * For "[var, var]" increment "*var_count" for each variable.
893 * for "[var, var; var]" set "semicolon".
894 * Return NULL for an error.
895 */
896 char_u *
897skip_var_list(
898 char_u *arg,
899 int *var_count,
900 int *semicolon)
901{
902 char_u *p, *s;
903
904 if (*arg == '[')
905 {
906 // "[var, var]": find the matching ']'.
907 p = arg;
908 for (;;)
909 {
910 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
911 s = skip_var_one(p);
912 if (s == p)
913 {
914 semsg(_(e_invarg2), p);
915 return NULL;
916 }
917 ++*var_count;
918
919 p = skipwhite(s);
920 if (*p == ']')
921 break;
922 else if (*p == ';')
923 {
924 if (*semicolon == 1)
925 {
926 emsg(_("Double ; in list of variables"));
927 return NULL;
928 }
929 *semicolon = 1;
930 }
931 else if (*p != ',')
932 {
933 semsg(_(e_invarg2), p);
934 return NULL;
935 }
936 }
937 return p + 1;
938 }
939 else
940 return skip_var_one(arg);
941}
942
943/*
944 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
945 * l[idx].
946 */
947 static char_u *
948skip_var_one(char_u *arg)
949{
950 if (*arg == '@' && arg[1] != NUL)
951 return arg + 2;
952 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
953 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
954}
955
956/*
957 * List variables for hashtab "ht" with prefix "prefix".
958 * If "empty" is TRUE also list NULL strings as empty strings.
959 */
960 void
961list_hashtable_vars(
962 hashtab_T *ht,
963 char *prefix,
964 int empty,
965 int *first)
966{
967 hashitem_T *hi;
968 dictitem_T *di;
969 int todo;
970 char_u buf[IOSIZE];
971
972 todo = (int)ht->ht_used;
973 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
974 {
975 if (!HASHITEM_EMPTY(hi))
976 {
977 --todo;
978 di = HI2DI(hi);
979
980 // apply :filter /pat/ to variable name
981 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
982 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
983 if (message_filtered(buf))
984 continue;
985
986 if (empty || di->di_tv.v_type != VAR_STRING
987 || di->di_tv.vval.v_string != NULL)
988 list_one_var(di, prefix, first);
989 }
990 }
991}
992
993/*
994 * List global variables.
995 */
996 static void
997list_glob_vars(int *first)
998{
999 list_hashtable_vars(&globvarht, "", TRUE, first);
1000}
1001
1002/*
1003 * List buffer variables.
1004 */
1005 static void
1006list_buf_vars(int *first)
1007{
1008 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1009}
1010
1011/*
1012 * List window variables.
1013 */
1014 static void
1015list_win_vars(int *first)
1016{
1017 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1018}
1019
1020/*
1021 * List tab page variables.
1022 */
1023 static void
1024list_tab_vars(int *first)
1025{
1026 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1027}
1028
1029/*
1030 * List variables in "arg".
1031 */
1032 static char_u *
1033list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1034{
1035 int error = FALSE;
1036 int len;
1037 char_u *name;
1038 char_u *name_start;
1039 char_u *arg_subsc;
1040 char_u *tofree;
1041 typval_T tv;
1042
1043 while (!ends_excmd(*arg) && !got_int)
1044 {
1045 if (error || eap->skip)
1046 {
1047 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1048 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1049 {
1050 emsg_severe = TRUE;
1051 emsg(_(e_trailing));
1052 break;
1053 }
1054 }
1055 else
1056 {
1057 // get_name_len() takes care of expanding curly braces
1058 name_start = name = arg;
1059 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1060 if (len <= 0)
1061 {
1062 // This is mainly to keep test 49 working: when expanding
1063 // curly braces fails overrule the exception error message.
1064 if (len < 0 && !aborting())
1065 {
1066 emsg_severe = TRUE;
1067 semsg(_(e_invarg2), arg);
1068 break;
1069 }
1070 error = TRUE;
1071 }
1072 else
1073 {
1074 if (tofree != NULL)
1075 name = tofree;
1076 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
1077 error = TRUE;
1078 else
1079 {
1080 // handle d.key, l[idx], f(expr)
1081 arg_subsc = arg;
1082 if (handle_subscript(&arg, &tv, TRUE, TRUE,
1083 name, &name) == FAIL)
1084 error = TRUE;
1085 else
1086 {
1087 if (arg == arg_subsc && len == 2 && name[1] == ':')
1088 {
1089 switch (*name)
1090 {
1091 case 'g': list_glob_vars(first); break;
1092 case 'b': list_buf_vars(first); break;
1093 case 'w': list_win_vars(first); break;
1094 case 't': list_tab_vars(first); break;
1095 case 'v': list_vim_vars(first); break;
1096 case 's': list_script_vars(first); break;
1097 case 'l': list_func_vars(first); break;
1098 default:
1099 semsg(_("E738: Can't list variables for %s"), name);
1100 }
1101 }
1102 else
1103 {
1104 char_u numbuf[NUMBUFLEN];
1105 char_u *tf;
1106 int c;
1107 char_u *s;
1108
1109 s = echo_string(&tv, &tf, numbuf, 0);
1110 c = *arg;
1111 *arg = NUL;
1112 list_one_var_a("",
1113 arg == arg_subsc ? name : name_start,
1114 tv.v_type,
1115 s == NULL ? (char_u *)"" : s,
1116 first);
1117 *arg = c;
1118 vim_free(tf);
1119 }
1120 clear_tv(&tv);
1121 }
1122 }
1123 }
1124
1125 vim_free(tofree);
1126 }
1127
1128 arg = skipwhite(arg);
1129 }
1130
1131 return arg;
1132}
1133
1134/*
1135 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1136 * Returns a pointer to the char just after the var name.
1137 * Returns NULL if there is an error.
1138 */
1139 static char_u *
1140ex_let_one(
1141 char_u *arg, // points to variable name
1142 typval_T *tv, // value to assign to variable
1143 int copy, // copy value from "tv"
1144 int is_const, // lock variable for const
1145 char_u *endchars, // valid chars after variable name or NULL
1146 char_u *op) // "+", "-", "." or NULL
1147{
1148 int c1;
1149 char_u *name;
1150 char_u *p;
1151 char_u *arg_end = NULL;
1152 int len;
1153 int opt_flags;
1154 char_u *tofree = NULL;
1155
1156 // ":let $VAR = expr": Set environment variable.
1157 if (*arg == '$')
1158 {
1159 if (is_const)
1160 {
1161 emsg(_("E996: Cannot lock an environment variable"));
1162 return NULL;
1163 }
1164 // Find the end of the name.
1165 ++arg;
1166 name = arg;
1167 len = get_env_len(&arg);
1168 if (len == 0)
1169 semsg(_(e_invarg2), name - 1);
1170 else
1171 {
1172 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1173 semsg(_(e_letwrong), op);
1174 else if (endchars != NULL
1175 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1176 emsg(_(e_letunexp));
1177 else if (!check_secure())
1178 {
1179 c1 = name[len];
1180 name[len] = NUL;
1181 p = tv_get_string_chk(tv);
1182 if (p != NULL && op != NULL && *op == '.')
1183 {
1184 int mustfree = FALSE;
1185 char_u *s = vim_getenv(name, &mustfree);
1186
1187 if (s != NULL)
1188 {
1189 p = tofree = concat_str(s, p);
1190 if (mustfree)
1191 vim_free(s);
1192 }
1193 }
1194 if (p != NULL)
1195 {
1196 vim_setenv(name, p);
1197 if (STRICMP(name, "HOME") == 0)
1198 init_homedir();
1199 else if (didset_vim && STRICMP(name, "VIM") == 0)
1200 didset_vim = FALSE;
1201 else if (didset_vimruntime
1202 && STRICMP(name, "VIMRUNTIME") == 0)
1203 didset_vimruntime = FALSE;
1204 arg_end = arg;
1205 }
1206 name[len] = c1;
1207 vim_free(tofree);
1208 }
1209 }
1210 }
1211
1212 // ":let &option = expr": Set option value.
1213 // ":let &l:option = expr": Set local option value.
1214 // ":let &g:option = expr": Set global option value.
1215 else if (*arg == '&')
1216 {
1217 if (is_const)
1218 {
1219 emsg(_("E996: Cannot lock an option"));
1220 return NULL;
1221 }
1222 // Find the end of the name.
1223 p = find_option_end(&arg, &opt_flags);
1224 if (p == NULL || (endchars != NULL
1225 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1226 emsg(_(e_letunexp));
1227 else
1228 {
1229 long n;
1230 int opt_type;
1231 long numval;
1232 char_u *stringval = NULL;
1233 char_u *s;
1234
1235 c1 = *p;
1236 *p = NUL;
1237
1238 n = (long)tv_get_number(tv);
1239 s = tv_get_string_chk(tv); // != NULL if number or string
1240 if (s != NULL && op != NULL && *op != '=')
1241 {
1242 opt_type = get_option_value(arg, &numval,
1243 &stringval, opt_flags);
1244 if ((opt_type == 1 && *op == '.')
1245 || (opt_type == 0 && *op != '.'))
1246 {
1247 semsg(_(e_letwrong), op);
1248 s = NULL; // don't set the value
1249 }
1250 else
1251 {
1252 if (opt_type == 1) // number
1253 {
1254 switch (*op)
1255 {
1256 case '+': n = numval + n; break;
1257 case '-': n = numval - n; break;
1258 case '*': n = numval * n; break;
1259 case '/': n = (long)num_divide(numval, n); break;
1260 case '%': n = (long)num_modulus(numval, n); break;
1261 }
1262 }
1263 else if (opt_type == 0 && stringval != NULL) // string
1264 {
1265 s = concat_str(stringval, s);
1266 vim_free(stringval);
1267 stringval = s;
1268 }
1269 }
1270 }
1271 if (s != NULL)
1272 {
1273 set_option_value(arg, n, s, opt_flags);
1274 arg_end = p;
1275 }
1276 *p = c1;
1277 vim_free(stringval);
1278 }
1279 }
1280
1281 // ":let @r = expr": Set register contents.
1282 else if (*arg == '@')
1283 {
1284 if (is_const)
1285 {
1286 emsg(_("E996: Cannot lock a register"));
1287 return NULL;
1288 }
1289 ++arg;
1290 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
1291 semsg(_(e_letwrong), op);
1292 else if (endchars != NULL
1293 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1294 emsg(_(e_letunexp));
1295 else
1296 {
1297 char_u *ptofree = NULL;
1298 char_u *s;
1299
1300 p = tv_get_string_chk(tv);
1301 if (p != NULL && op != NULL && *op == '.')
1302 {
1303 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
1304 if (s != NULL)
1305 {
1306 p = ptofree = concat_str(s, p);
1307 vim_free(s);
1308 }
1309 }
1310 if (p != NULL)
1311 {
1312 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1313 arg_end = arg + 1;
1314 }
1315 vim_free(ptofree);
1316 }
1317 }
1318
1319 // ":let var = expr": Set internal variable.
1320 // ":let {expr} = expr": Idem, name made with curly braces
1321 else if (eval_isnamec1(*arg) || *arg == '{')
1322 {
1323 lval_T lv;
1324
1325 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
1326 if (p != NULL && lv.ll_name != NULL)
1327 {
1328 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
1329 emsg(_(e_letunexp));
1330 else
1331 {
1332 set_var_lval(&lv, p, tv, copy, is_const, op);
1333 arg_end = p;
1334 }
1335 }
1336 clear_lval(&lv);
1337 }
1338
1339 else
1340 semsg(_(e_invarg2), arg);
1341
1342 return arg_end;
1343}
1344
1345/*
1346 * ":unlet[!] var1 ... " command.
1347 */
1348 void
1349ex_unlet(exarg_T *eap)
1350{
1351 ex_unletlock(eap, eap->arg, 0);
1352}
1353
1354/*
1355 * ":lockvar" and ":unlockvar" commands
1356 */
1357 void
1358ex_lockvar(exarg_T *eap)
1359{
1360 char_u *arg = eap->arg;
1361 int deep = 2;
1362
1363 if (eap->forceit)
1364 deep = -1;
1365 else if (vim_isdigit(*arg))
1366 {
1367 deep = getdigits(&arg);
1368 arg = skipwhite(arg);
1369 }
1370
1371 ex_unletlock(eap, arg, deep);
1372}
1373
1374/*
1375 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
1376 */
1377 static void
1378ex_unletlock(
1379 exarg_T *eap,
1380 char_u *argstart,
1381 int deep)
1382{
1383 char_u *arg = argstart;
1384 char_u *name_end;
1385 int error = FALSE;
1386 lval_T lv;
1387
1388 do
1389 {
1390 if (*arg == '$')
1391 {
1392 char_u *name = ++arg;
1393
1394 if (get_env_len(&arg) == 0)
1395 {
1396 semsg(_(e_invarg2), name - 1);
1397 return;
1398 }
1399 vim_unsetenv(name);
1400 arg = skipwhite(arg);
1401 continue;
1402 }
1403
1404 // Parse the name and find the end.
1405 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
1406 FNE_CHECK_START);
1407 if (lv.ll_name == NULL)
1408 error = TRUE; // error but continue parsing
1409 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1410 && !ends_excmd(*name_end)))
1411 {
1412 if (name_end != NULL)
1413 {
1414 emsg_severe = TRUE;
1415 emsg(_(e_trailing));
1416 }
1417 if (!(eap->skip || error))
1418 clear_lval(&lv);
1419 break;
1420 }
1421
1422 if (!error && !eap->skip)
1423 {
1424 if (eap->cmdidx == CMD_unlet)
1425 {
1426 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
1427 error = TRUE;
1428 }
1429 else
1430 {
1431 if (do_lock_var(&lv, name_end, deep,
1432 eap->cmdidx == CMD_lockvar) == FAIL)
1433 error = TRUE;
1434 }
1435 }
1436
1437 if (!eap->skip)
1438 clear_lval(&lv);
1439
1440 arg = skipwhite(name_end);
1441 } while (!ends_excmd(*arg));
1442
1443 eap->nextcmd = check_nextcmd(arg);
1444}
1445
1446 static int
1447do_unlet_var(
1448 lval_T *lp,
1449 char_u *name_end,
1450 int forceit)
1451{
1452 int ret = OK;
1453 int cc;
1454
1455 if (lp->ll_tv == NULL)
1456 {
1457 cc = *name_end;
1458 *name_end = NUL;
1459
1460 // Normal name or expanded name.
1461 if (do_unlet(lp->ll_name, forceit) == FAIL)
1462 ret = FAIL;
1463 *name_end = cc;
1464 }
1465 else if ((lp->ll_list != NULL
1466 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
1467 || (lp->ll_dict != NULL
1468 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
1469 return FAIL;
1470 else if (lp->ll_range)
1471 {
1472 listitem_T *li;
1473 listitem_T *ll_li = lp->ll_li;
1474 int ll_n1 = lp->ll_n1;
1475
1476 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
1477 {
1478 li = ll_li->li_next;
1479 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
1480 return FAIL;
1481 ll_li = li;
1482 ++ll_n1;
1483 }
1484
1485 // Delete a range of List items.
1486 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1487 {
1488 li = lp->ll_li->li_next;
1489 listitem_remove(lp->ll_list, lp->ll_li);
1490 lp->ll_li = li;
1491 ++lp->ll_n1;
1492 }
1493 }
1494 else
1495 {
1496 if (lp->ll_list != NULL)
1497 // unlet a List item.
1498 listitem_remove(lp->ll_list, lp->ll_li);
1499 else
1500 // unlet a Dictionary item.
1501 dictitem_remove(lp->ll_dict, lp->ll_di);
1502 }
1503
1504 return ret;
1505}
1506
1507/*
1508 * "unlet" a variable. Return OK if it existed, FAIL if not.
1509 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1510 */
1511 int
1512do_unlet(char_u *name, int forceit)
1513{
1514 hashtab_T *ht;
1515 hashitem_T *hi;
1516 char_u *varname;
1517 dict_T *d;
1518 dictitem_T *di;
1519
1520 ht = find_var_ht(name, &varname);
1521 if (ht != NULL && *varname != NUL)
1522 {
1523 d = get_current_funccal_dict(ht);
1524 if (d == NULL)
1525 {
1526 if (ht == &globvarht)
1527 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001528 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001529 d = &vimvardict;
1530 else
1531 {
1532 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1533 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1534 }
1535 if (d == NULL)
1536 {
1537 internal_error("do_unlet()");
1538 return FAIL;
1539 }
1540 }
1541 hi = hash_find(ht, varname);
1542 if (HASHITEM_EMPTY(hi))
1543 hi = find_hi_in_scoped_ht(name, &ht);
1544 if (hi != NULL && !HASHITEM_EMPTY(hi))
1545 {
1546 di = HI2DI(hi);
1547 if (var_check_fixed(di->di_flags, name, FALSE)
1548 || var_check_ro(di->di_flags, name, FALSE)
1549 || var_check_lock(d->dv_lock, name, FALSE))
1550 return FAIL;
1551
1552 delete_var(ht, hi);
1553 return OK;
1554 }
1555 }
1556 if (forceit)
1557 return OK;
1558 semsg(_("E108: No such variable: \"%s\""), name);
1559 return FAIL;
1560}
1561
1562/*
1563 * Lock or unlock variable indicated by "lp".
1564 * "deep" is the levels to go (-1 for unlimited);
1565 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1566 */
1567 static int
1568do_lock_var(
1569 lval_T *lp,
1570 char_u *name_end,
1571 int deep,
1572 int lock)
1573{
1574 int ret = OK;
1575 int cc;
1576 dictitem_T *di;
1577
1578 if (deep == 0) // nothing to do
1579 return OK;
1580
1581 if (lp->ll_tv == NULL)
1582 {
1583 cc = *name_end;
1584 *name_end = NUL;
1585
1586 // Normal name or expanded name.
1587 di = find_var(lp->ll_name, NULL, TRUE);
1588 if (di == NULL)
1589 ret = FAIL;
1590 else if ((di->di_flags & DI_FLAGS_FIX)
1591 && di->di_tv.v_type != VAR_DICT
1592 && di->di_tv.v_type != VAR_LIST)
1593 // For historic reasons this error is not given for a list or dict.
1594 // E.g., the b: dict could be locked/unlocked.
1595 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
1596 else
1597 {
1598 if (lock)
1599 di->di_flags |= DI_FLAGS_LOCK;
1600 else
1601 di->di_flags &= ~DI_FLAGS_LOCK;
1602 item_lock(&di->di_tv, deep, lock);
1603 }
1604 *name_end = cc;
1605 }
1606 else if (lp->ll_range)
1607 {
1608 listitem_T *li = lp->ll_li;
1609
1610 // (un)lock a range of List items.
1611 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1612 {
1613 item_lock(&li->li_tv, deep, lock);
1614 li = li->li_next;
1615 ++lp->ll_n1;
1616 }
1617 }
1618 else if (lp->ll_list != NULL)
1619 // (un)lock a List item.
1620 item_lock(&lp->ll_li->li_tv, deep, lock);
1621 else
1622 // (un)lock a Dictionary item.
1623 item_lock(&lp->ll_di->di_tv, deep, lock);
1624
1625 return ret;
1626}
1627
1628/*
1629 * Lock or unlock an item. "deep" is nr of levels to go.
1630 */
1631 static void
1632item_lock(typval_T *tv, int deep, int lock)
1633{
1634 static int recurse = 0;
1635 list_T *l;
1636 listitem_T *li;
1637 dict_T *d;
1638 blob_T *b;
1639 hashitem_T *hi;
1640 int todo;
1641
1642 if (recurse >= DICT_MAXNEST)
1643 {
1644 emsg(_("E743: variable nested too deep for (un)lock"));
1645 return;
1646 }
1647 if (deep == 0)
1648 return;
1649 ++recurse;
1650
1651 // lock/unlock the item itself
1652 if (lock)
1653 tv->v_lock |= VAR_LOCKED;
1654 else
1655 tv->v_lock &= ~VAR_LOCKED;
1656
1657 switch (tv->v_type)
1658 {
1659 case VAR_UNKNOWN:
1660 case VAR_NUMBER:
1661 case VAR_STRING:
1662 case VAR_FUNC:
1663 case VAR_PARTIAL:
1664 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001665 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001666 case VAR_SPECIAL:
1667 case VAR_JOB:
1668 case VAR_CHANNEL:
1669 break;
1670
1671 case VAR_BLOB:
1672 if ((b = tv->vval.v_blob) != NULL)
1673 {
1674 if (lock)
1675 b->bv_lock |= VAR_LOCKED;
1676 else
1677 b->bv_lock &= ~VAR_LOCKED;
1678 }
1679 break;
1680 case VAR_LIST:
1681 if ((l = tv->vval.v_list) != NULL)
1682 {
1683 if (lock)
1684 l->lv_lock |= VAR_LOCKED;
1685 else
1686 l->lv_lock &= ~VAR_LOCKED;
1687 if (deep < 0 || deep > 1)
1688 // recursive: lock/unlock the items the List contains
1689 for (li = l->lv_first; li != NULL; li = li->li_next)
1690 item_lock(&li->li_tv, deep - 1, lock);
1691 }
1692 break;
1693 case VAR_DICT:
1694 if ((d = tv->vval.v_dict) != NULL)
1695 {
1696 if (lock)
1697 d->dv_lock |= VAR_LOCKED;
1698 else
1699 d->dv_lock &= ~VAR_LOCKED;
1700 if (deep < 0 || deep > 1)
1701 {
1702 // recursive: lock/unlock the items the List contains
1703 todo = (int)d->dv_hashtab.ht_used;
1704 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1705 {
1706 if (!HASHITEM_EMPTY(hi))
1707 {
1708 --todo;
1709 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
1710 }
1711 }
1712 }
1713 }
1714 }
1715 --recurse;
1716}
1717
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001718#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1719/*
1720 * Delete all "menutrans_" variables.
1721 */
1722 void
1723del_menutrans_vars(void)
1724{
1725 hashitem_T *hi;
1726 int todo;
1727
1728 hash_lock(&globvarht);
1729 todo = (int)globvarht.ht_used;
1730 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
1731 {
1732 if (!HASHITEM_EMPTY(hi))
1733 {
1734 --todo;
1735 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1736 delete_var(&globvarht, hi);
1737 }
1738 }
1739 hash_unlock(&globvarht);
1740}
1741#endif
1742
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001743/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001744 * Local string buffer for the next two functions to store a variable name
1745 * with its prefix. Allocated in cat_prefix_varname(), freed later in
1746 * get_user_var_name().
1747 */
1748
1749static char_u *varnamebuf = NULL;
1750static int varnamebuflen = 0;
1751
1752/*
1753 * Function to concatenate a prefix and a variable name.
1754 */
1755 static char_u *
1756cat_prefix_varname(int prefix, char_u *name)
1757{
1758 int len;
1759
1760 len = (int)STRLEN(name) + 3;
1761 if (len > varnamebuflen)
1762 {
1763 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001764 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001765 varnamebuf = alloc(len);
1766 if (varnamebuf == NULL)
1767 {
1768 varnamebuflen = 0;
1769 return NULL;
1770 }
1771 varnamebuflen = len;
1772 }
1773 *varnamebuf = prefix;
1774 varnamebuf[1] = ':';
1775 STRCPY(varnamebuf + 2, name);
1776 return varnamebuf;
1777}
1778
1779/*
1780 * Function given to ExpandGeneric() to obtain the list of user defined
1781 * (global/buffer/window/built-in) variable names.
1782 */
1783 char_u *
1784get_user_var_name(expand_T *xp, int idx)
1785{
1786 static long_u gdone;
1787 static long_u bdone;
1788 static long_u wdone;
1789 static long_u tdone;
1790 static int vidx;
1791 static hashitem_T *hi;
1792 hashtab_T *ht;
1793
1794 if (idx == 0)
1795 {
1796 gdone = bdone = wdone = vidx = 0;
1797 tdone = 0;
1798 }
1799
1800 // Global variables
1801 if (gdone < globvarht.ht_used)
1802 {
1803 if (gdone++ == 0)
1804 hi = globvarht.ht_array;
1805 else
1806 ++hi;
1807 while (HASHITEM_EMPTY(hi))
1808 ++hi;
1809 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
1810 return cat_prefix_varname('g', hi->hi_key);
1811 return hi->hi_key;
1812 }
1813
1814 // b: variables
1815 ht = &curbuf->b_vars->dv_hashtab;
1816 if (bdone < ht->ht_used)
1817 {
1818 if (bdone++ == 0)
1819 hi = ht->ht_array;
1820 else
1821 ++hi;
1822 while (HASHITEM_EMPTY(hi))
1823 ++hi;
1824 return cat_prefix_varname('b', hi->hi_key);
1825 }
1826
1827 // w: variables
1828 ht = &curwin->w_vars->dv_hashtab;
1829 if (wdone < ht->ht_used)
1830 {
1831 if (wdone++ == 0)
1832 hi = ht->ht_array;
1833 else
1834 ++hi;
1835 while (HASHITEM_EMPTY(hi))
1836 ++hi;
1837 return cat_prefix_varname('w', hi->hi_key);
1838 }
1839
1840 // t: variables
1841 ht = &curtab->tp_vars->dv_hashtab;
1842 if (tdone < ht->ht_used)
1843 {
1844 if (tdone++ == 0)
1845 hi = ht->ht_array;
1846 else
1847 ++hi;
1848 while (HASHITEM_EMPTY(hi))
1849 ++hi;
1850 return cat_prefix_varname('t', hi->hi_key);
1851 }
1852
1853 // v: variables
1854 if (vidx < VV_LEN)
1855 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
1856
1857 VIM_CLEAR(varnamebuf);
1858 varnamebuflen = 0;
1859 return NULL;
1860}
1861
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001862 char *
1863get_var_special_name(int nr)
1864{
1865 switch (nr)
1866 {
1867 case VVAL_FALSE: return "v:false";
1868 case VVAL_TRUE: return "v:true";
1869 case VVAL_NONE: return "v:none";
1870 case VVAL_NULL: return "v:null";
1871 }
1872 internal_error("get_var_special_name()");
1873 return "42";
1874}
1875
1876/*
1877 * Returns the global variable dictionary
1878 */
1879 dict_T *
1880get_globvar_dict(void)
1881{
1882 return &globvardict;
1883}
1884
1885/*
1886 * Returns the global variable hash table
1887 */
1888 hashtab_T *
1889get_globvar_ht(void)
1890{
1891 return &globvarht;
1892}
1893
1894/*
1895 * Returns the v: variable dictionary
1896 */
1897 dict_T *
1898get_vimvar_dict(void)
1899{
1900 return &vimvardict;
1901}
1902
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001903/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02001904 * Set type of v: variable to "type".
1905 */
1906 void
1907set_vim_var_type(int idx, vartype_T type)
1908{
1909 vimvars[idx].vv_type = type;
1910}
1911
1912/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001913 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02001914 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001915 */
1916 void
1917set_vim_var_nr(int idx, varnumber_T val)
1918{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001919 vimvars[idx].vv_nr = val;
1920}
1921
1922/*
1923 * Get typval_T v: variable value.
1924 */
1925 typval_T *
1926get_vim_var_tv(int idx)
1927{
1928 return &vimvars[idx].vv_tv;
1929}
1930
1931/*
1932 * Get number v: variable value.
1933 */
1934 varnumber_T
1935get_vim_var_nr(int idx)
1936{
1937 return vimvars[idx].vv_nr;
1938}
1939
1940/*
1941 * Get string v: variable value. Uses a static buffer, can only be used once.
1942 * If the String variable has never been set, return an empty string.
1943 * Never returns NULL;
1944 */
1945 char_u *
1946get_vim_var_str(int idx)
1947{
1948 return tv_get_string(&vimvars[idx].vv_tv);
1949}
1950
1951/*
1952 * Get List v: variable value. Caller must take care of reference count when
1953 * needed.
1954 */
1955 list_T *
1956get_vim_var_list(int idx)
1957{
1958 return vimvars[idx].vv_list;
1959}
1960
1961/*
1962 * Get Dict v: variable value. Caller must take care of reference count when
1963 * needed.
1964 */
1965 dict_T *
1966get_vim_var_dict(int idx)
1967{
1968 return vimvars[idx].vv_dict;
1969}
1970
1971/*
1972 * Set v:char to character "c".
1973 */
1974 void
1975set_vim_var_char(int c)
1976{
1977 char_u buf[MB_MAXBYTES + 1];
1978
1979 if (has_mbyte)
1980 buf[(*mb_char2bytes)(c, buf)] = NUL;
1981 else
1982 {
1983 buf[0] = c;
1984 buf[1] = NUL;
1985 }
1986 set_vim_var_string(VV_CHAR, buf, -1);
1987}
1988
1989/*
1990 * Set v:count to "count" and v:count1 to "count1".
1991 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
1992 */
1993 void
1994set_vcount(
1995 long count,
1996 long count1,
1997 int set_prevcount)
1998{
1999 if (set_prevcount)
2000 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2001 vimvars[VV_COUNT].vv_nr = count;
2002 vimvars[VV_COUNT1].vv_nr = count1;
2003}
2004
2005/*
2006 * Save variables that might be changed as a side effect. Used when executing
2007 * a timer callback.
2008 */
2009 void
2010save_vimvars(vimvars_save_T *vvsave)
2011{
2012 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2013 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2014 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2015}
2016
2017/*
2018 * Restore variables saved by save_vimvars().
2019 */
2020 void
2021restore_vimvars(vimvars_save_T *vvsave)
2022{
2023 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2024 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2025 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2026}
2027
2028/*
2029 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2030 * value.
2031 */
2032 void
2033set_vim_var_string(
2034 int idx,
2035 char_u *val,
2036 int len) // length of "val" to use or -1 (whole string)
2037{
2038 clear_tv(&vimvars[idx].vv_di.di_tv);
2039 vimvars[idx].vv_type = VAR_STRING;
2040 if (val == NULL)
2041 vimvars[idx].vv_str = NULL;
2042 else if (len == -1)
2043 vimvars[idx].vv_str = vim_strsave(val);
2044 else
2045 vimvars[idx].vv_str = vim_strnsave(val, len);
2046}
2047
2048/*
2049 * Set List v: variable to "val".
2050 */
2051 void
2052set_vim_var_list(int idx, list_T *val)
2053{
2054 clear_tv(&vimvars[idx].vv_di.di_tv);
2055 vimvars[idx].vv_type = VAR_LIST;
2056 vimvars[idx].vv_list = val;
2057 if (val != NULL)
2058 ++val->lv_refcount;
2059}
2060
2061/*
2062 * Set Dictionary v: variable to "val".
2063 */
2064 void
2065set_vim_var_dict(int idx, dict_T *val)
2066{
2067 clear_tv(&vimvars[idx].vv_di.di_tv);
2068 vimvars[idx].vv_type = VAR_DICT;
2069 vimvars[idx].vv_dict = val;
2070 if (val != NULL)
2071 {
2072 ++val->dv_refcount;
2073 dict_set_items_ro(val);
2074 }
2075}
2076
2077/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002078 * Set the v:argv list.
2079 */
2080 void
2081set_argv_var(char **argv, int argc)
2082{
2083 list_T *l = list_alloc();
2084 int i;
2085
2086 if (l == NULL)
2087 getout(1);
2088 l->lv_lock = VAR_FIXED;
2089 for (i = 0; i < argc; ++i)
2090 {
2091 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2092 getout(1);
2093 l->lv_last->li_tv.v_lock = VAR_FIXED;
2094 }
2095 set_vim_var_list(VV_ARGV, l);
2096}
2097
2098/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002099 * Set v:register if needed.
2100 */
2101 void
2102set_reg_var(int c)
2103{
2104 char_u regname;
2105
2106 if (c == 0 || c == ' ')
2107 regname = '"';
2108 else
2109 regname = c;
2110 // Avoid free/alloc when the value is already right.
2111 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2112 set_vim_var_string(VV_REG, &regname, 1);
2113}
2114
2115/*
2116 * Get or set v:exception. If "oldval" == NULL, return the current value.
2117 * Otherwise, restore the value to "oldval" and return NULL.
2118 * Must always be called in pairs to save and restore v:exception! Does not
2119 * take care of memory allocations.
2120 */
2121 char_u *
2122v_exception(char_u *oldval)
2123{
2124 if (oldval == NULL)
2125 return vimvars[VV_EXCEPTION].vv_str;
2126
2127 vimvars[VV_EXCEPTION].vv_str = oldval;
2128 return NULL;
2129}
2130
2131/*
2132 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2133 * Otherwise, restore the value to "oldval" and return NULL.
2134 * Must always be called in pairs to save and restore v:throwpoint! Does not
2135 * take care of memory allocations.
2136 */
2137 char_u *
2138v_throwpoint(char_u *oldval)
2139{
2140 if (oldval == NULL)
2141 return vimvars[VV_THROWPOINT].vv_str;
2142
2143 vimvars[VV_THROWPOINT].vv_str = oldval;
2144 return NULL;
2145}
2146
2147/*
2148 * Set v:cmdarg.
2149 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2150 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2151 * Must always be called in pairs!
2152 */
2153 char_u *
2154set_cmdarg(exarg_T *eap, char_u *oldarg)
2155{
2156 char_u *oldval;
2157 char_u *newval;
2158 unsigned len;
2159
2160 oldval = vimvars[VV_CMDARG].vv_str;
2161 if (eap == NULL)
2162 {
2163 vim_free(oldval);
2164 vimvars[VV_CMDARG].vv_str = oldarg;
2165 return NULL;
2166 }
2167
2168 if (eap->force_bin == FORCE_BIN)
2169 len = 6;
2170 else if (eap->force_bin == FORCE_NOBIN)
2171 len = 8;
2172 else
2173 len = 0;
2174
2175 if (eap->read_edit)
2176 len += 7;
2177
2178 if (eap->force_ff != 0)
2179 len += 10; // " ++ff=unix"
2180 if (eap->force_enc != 0)
2181 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2182 if (eap->bad_char != 0)
2183 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2184
2185 newval = alloc(len + 1);
2186 if (newval == NULL)
2187 return NULL;
2188
2189 if (eap->force_bin == FORCE_BIN)
2190 sprintf((char *)newval, " ++bin");
2191 else if (eap->force_bin == FORCE_NOBIN)
2192 sprintf((char *)newval, " ++nobin");
2193 else
2194 *newval = NUL;
2195
2196 if (eap->read_edit)
2197 STRCAT(newval, " ++edit");
2198
2199 if (eap->force_ff != 0)
2200 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2201 eap->force_ff == 'u' ? "unix"
2202 : eap->force_ff == 'd' ? "dos"
2203 : "mac");
2204 if (eap->force_enc != 0)
2205 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2206 eap->cmd + eap->force_enc);
2207 if (eap->bad_char == BAD_KEEP)
2208 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2209 else if (eap->bad_char == BAD_DROP)
2210 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2211 else if (eap->bad_char != 0)
2212 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2213 vimvars[VV_CMDARG].vv_str = newval;
2214 return oldval;
2215}
2216
2217/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002218 * Get the value of internal variable "name".
2219 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2220 */
2221 int
2222get_var_tv(
2223 char_u *name,
2224 int len, // length of "name"
2225 typval_T *rettv, // NULL when only checking existence
2226 dictitem_T **dip, // non-NULL when typval's dict item is needed
2227 int verbose, // may give error message
2228 int no_autoload) // do not use script autoloading
2229{
2230 int ret = OK;
2231 typval_T *tv = NULL;
2232 dictitem_T *v;
2233 int cc;
2234
2235 // truncate the name, so that we can use strcmp()
2236 cc = name[len];
2237 name[len] = NUL;
2238
2239 // Check for user-defined variables.
2240 v = find_var(name, NULL, no_autoload);
2241 if (v != NULL)
2242 {
2243 tv = &v->di_tv;
2244 if (dip != NULL)
2245 *dip = v;
2246 }
2247
2248 if (tv == NULL)
2249 {
2250 if (rettv != NULL && verbose)
2251 semsg(_(e_undefvar), name);
2252 ret = FAIL;
2253 }
2254 else if (rettv != NULL)
2255 copy_tv(tv, rettv);
2256
2257 name[len] = cc;
2258
2259 return ret;
2260}
2261
2262/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002263 * Check if variable "name[len]" is a local variable or an argument.
2264 * If so, "*eval_lavars_used" is set to TRUE.
2265 */
2266 void
2267check_vars(char_u *name, int len)
2268{
2269 int cc;
2270 char_u *varname;
2271 hashtab_T *ht;
2272
2273 if (eval_lavars_used == NULL)
2274 return;
2275
2276 // truncate the name, so that we can use strcmp()
2277 cc = name[len];
2278 name[len] = NUL;
2279
2280 ht = find_var_ht(name, &varname);
2281 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2282 {
2283 if (find_var(name, NULL, TRUE) != NULL)
2284 *eval_lavars_used = TRUE;
2285 }
2286
2287 name[len] = cc;
2288}
2289
2290/*
2291 * Find variable "name" in the list of variables.
2292 * Return a pointer to it if found, NULL if not found.
2293 * Careful: "a:0" variables don't have a name.
2294 * When "htp" is not NULL we are writing to the variable, set "htp" to the
2295 * hashtab_T used.
2296 */
2297 dictitem_T *
2298find_var(char_u *name, hashtab_T **htp, int no_autoload)
2299{
2300 char_u *varname;
2301 hashtab_T *ht;
2302 dictitem_T *ret = NULL;
2303
2304 ht = find_var_ht(name, &varname);
2305 if (htp != NULL)
2306 *htp = ht;
2307 if (ht == NULL)
2308 return NULL;
2309 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
2310 if (ret != NULL)
2311 return ret;
2312
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002313 // Search in parent scope for lambda
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002314 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
2315}
2316
2317/*
2318 * Find variable "varname" in hashtab "ht" with name "htname".
2319 * Returns NULL if not found.
2320 */
2321 dictitem_T *
2322find_var_in_ht(
2323 hashtab_T *ht,
2324 int htname,
2325 char_u *varname,
2326 int no_autoload)
2327{
2328 hashitem_T *hi;
2329
2330 if (*varname == NUL)
2331 {
2332 // Must be something like "s:", otherwise "ht" would be NULL.
2333 switch (htname)
2334 {
2335 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2336 case 'g': return &globvars_var;
2337 case 'v': return &vimvars_var;
2338 case 'b': return &curbuf->b_bufvar;
2339 case 'w': return &curwin->w_winvar;
2340 case 't': return &curtab->tp_winvar;
2341 case 'l': return get_funccal_local_var();
2342 case 'a': return get_funccal_args_var();
2343 }
2344 return NULL;
2345 }
2346
2347 hi = hash_find(ht, varname);
2348 if (HASHITEM_EMPTY(hi))
2349 {
2350 // For global variables we may try auto-loading the script. If it
2351 // worked find the variable again. Don't auto-load a script if it was
2352 // loaded already, otherwise it would be loaded every time when
2353 // checking if a function name is a Funcref variable.
2354 if (ht == &globvarht && !no_autoload)
2355 {
2356 // Note: script_autoload() may make "hi" invalid. It must either
2357 // be obtained again or not used.
2358 if (!script_autoload(varname, FALSE) || aborting())
2359 return NULL;
2360 hi = hash_find(ht, varname);
2361 }
2362 if (HASHITEM_EMPTY(hi))
2363 return NULL;
2364 }
2365 return HI2DI(hi);
2366}
2367
2368/*
2369 * Find the hashtab used for a variable name.
2370 * Return NULL if the name is not valid.
2371 * Set "varname" to the start of name without ':'.
2372 */
2373 hashtab_T *
2374find_var_ht(char_u *name, char_u **varname)
2375{
2376 hashitem_T *hi;
2377 hashtab_T *ht;
2378
2379 if (name[0] == NUL)
2380 return NULL;
2381 if (name[1] != ':')
2382 {
2383 // The name must not start with a colon or #.
2384 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
2385 return NULL;
2386 *varname = name;
2387
2388 // "version" is "v:version" in all scopes if scriptversion < 3.
2389 // Same for a few other variables marked with VV_COMPAT.
2390 if (current_sctx.sc_version < 3)
2391 {
2392 hi = hash_find(&compat_hashtab, name);
2393 if (!HASHITEM_EMPTY(hi))
2394 return &compat_hashtab;
2395 }
2396
2397 ht = get_funccal_local_ht();
2398 if (ht == NULL)
2399 return &globvarht; // global variable
2400 return ht; // local variable
2401 }
2402 *varname = name + 2;
2403 if (*name == 'g') // global variable
2404 return &globvarht;
2405 // There must be no ':' or '#' in the rest of the name, unless g: is used
2406 if (vim_strchr(name + 2, ':') != NULL
2407 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
2408 return NULL;
2409 if (*name == 'b') // buffer variable
2410 return &curbuf->b_vars->dv_hashtab;
2411 if (*name == 'w') // window variable
2412 return &curwin->w_vars->dv_hashtab;
2413 if (*name == 't') // tab page variable
2414 return &curtab->tp_vars->dv_hashtab;
2415 if (*name == 'v') // v: variable
2416 return &vimvarht;
2417 if (*name == 'a') // a: function argument
2418 return get_funccal_args_ht();
2419 if (*name == 'l') // l: local function variable
2420 return get_funccal_local_ht();
2421 if (*name == 's' // script variable
2422 && current_sctx.sc_sid > 0
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002423 && current_sctx.sc_sid <= script_items.ga_len)
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002424 return &SCRIPT_VARS(current_sctx.sc_sid);
2425 return NULL;
2426}
2427
2428/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002429 * Get the string value of a (global/local) variable.
2430 * Note: see tv_get_string() for how long the pointer remains valid.
2431 * Returns NULL when it doesn't exist.
2432 */
2433 char_u *
2434get_var_value(char_u *name)
2435{
2436 dictitem_T *v;
2437
2438 v = find_var(name, NULL, FALSE);
2439 if (v == NULL)
2440 return NULL;
2441 return tv_get_string(&v->di_tv);
2442}
2443
2444/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002445 * Allocate a new hashtab for a sourced script. It will be used while
2446 * sourcing this script and when executing functions defined in the script.
2447 */
2448 void
2449new_script_vars(scid_T id)
2450{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002451 scriptvar_T *sv;
2452
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01002453 sv = ALLOC_CLEAR_ONE(scriptvar_T);
2454 if (sv == NULL)
2455 return;
2456 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
2457 SCRIPT_ITEM(id).sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002458}
2459
2460/*
2461 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
2462 * point to it.
2463 */
2464 void
2465init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
2466{
2467 hash_init(&dict->dv_hashtab);
2468 dict->dv_lock = 0;
2469 dict->dv_scope = scope;
2470 dict->dv_refcount = DO_NOT_FREE_CNT;
2471 dict->dv_copyID = 0;
2472 dict_var->di_tv.vval.v_dict = dict;
2473 dict_var->di_tv.v_type = VAR_DICT;
2474 dict_var->di_tv.v_lock = VAR_FIXED;
2475 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
2476 dict_var->di_key[0] = NUL;
2477}
2478
2479/*
2480 * Unreference a dictionary initialized by init_var_dict().
2481 */
2482 void
2483unref_var_dict(dict_T *dict)
2484{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002485 // Now the dict needs to be freed if no one else is using it, go back to
2486 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002487 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
2488 dict_unref(dict);
2489}
2490
2491/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002492 * Clean up a list of internal variables.
2493 * Frees all allocated variables and the value they contain.
2494 * Clears hashtab "ht", does not free it.
2495 */
2496 void
2497vars_clear(hashtab_T *ht)
2498{
2499 vars_clear_ext(ht, TRUE);
2500}
2501
2502/*
2503 * Like vars_clear(), but only free the value if "free_val" is TRUE.
2504 */
2505 void
2506vars_clear_ext(hashtab_T *ht, int free_val)
2507{
2508 int todo;
2509 hashitem_T *hi;
2510 dictitem_T *v;
2511
2512 hash_lock(ht);
2513 todo = (int)ht->ht_used;
2514 for (hi = ht->ht_array; todo > 0; ++hi)
2515 {
2516 if (!HASHITEM_EMPTY(hi))
2517 {
2518 --todo;
2519
2520 // Free the variable. Don't remove it from the hashtab,
2521 // ht_array might change then. hash_clear() takes care of it
2522 // later.
2523 v = HI2DI(hi);
2524 if (free_val)
2525 clear_tv(&v->di_tv);
2526 if (v->di_flags & DI_FLAGS_ALLOC)
2527 vim_free(v);
2528 }
2529 }
2530 hash_clear(ht);
2531 ht->ht_used = 0;
2532}
2533
2534/*
2535 * Delete a variable from hashtab "ht" at item "hi".
2536 * Clear the variable value and free the dictitem.
2537 */
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002538 static void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002539delete_var(hashtab_T *ht, hashitem_T *hi)
2540{
2541 dictitem_T *di = HI2DI(hi);
2542
2543 hash_remove(ht, hi);
2544 clear_tv(&di->di_tv);
2545 vim_free(di);
2546}
2547
2548/*
2549 * List the value of one internal variable.
2550 */
2551 static void
2552list_one_var(dictitem_T *v, char *prefix, int *first)
2553{
2554 char_u *tofree;
2555 char_u *s;
2556 char_u numbuf[NUMBUFLEN];
2557
2558 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
2559 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
2560 s == NULL ? (char_u *)"" : s, first);
2561 vim_free(tofree);
2562}
2563
2564 static void
2565list_one_var_a(
2566 char *prefix,
2567 char_u *name,
2568 int type,
2569 char_u *string,
2570 int *first) // when TRUE clear rest of screen and set to FALSE
2571{
2572 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
2573 msg_start();
2574 msg_puts(prefix);
2575 if (name != NULL) // "a:" vars don't have a name stored
2576 msg_puts((char *)name);
2577 msg_putchar(' ');
2578 msg_advance(22);
2579 if (type == VAR_NUMBER)
2580 msg_putchar('#');
2581 else if (type == VAR_FUNC || type == VAR_PARTIAL)
2582 msg_putchar('*');
2583 else if (type == VAR_LIST)
2584 {
2585 msg_putchar('[');
2586 if (*string == '[')
2587 ++string;
2588 }
2589 else if (type == VAR_DICT)
2590 {
2591 msg_putchar('{');
2592 if (*string == '{')
2593 ++string;
2594 }
2595 else
2596 msg_putchar(' ');
2597
2598 msg_outtrans(string);
2599
2600 if (type == VAR_FUNC || type == VAR_PARTIAL)
2601 msg_puts("()");
2602 if (*first)
2603 {
2604 msg_clr_eos();
2605 *first = FALSE;
2606 }
2607}
2608
2609/*
2610 * Set variable "name" to value in "tv".
2611 * If the variable already exists, the value is updated.
2612 * Otherwise the variable is created.
2613 */
2614 void
2615set_var(
2616 char_u *name,
2617 typval_T *tv,
2618 int copy) // make copy of value in "tv"
2619{
2620 set_var_const(name, tv, copy, FALSE);
2621}
2622
2623/*
2624 * Set variable "name" to value in "tv".
2625 * If the variable already exists and "is_const" is FALSE the value is updated.
2626 * Otherwise the variable is created.
2627 */
2628 void
2629set_var_const(
2630 char_u *name,
2631 typval_T *tv,
2632 int copy, // make copy of value in "tv"
2633 int is_const) // disallow to modify existing variable
2634{
2635 dictitem_T *v;
2636 char_u *varname;
2637 hashtab_T *ht;
2638
2639 ht = find_var_ht(name, &varname);
2640 if (ht == NULL || *varname == NUL)
2641 {
2642 semsg(_(e_illvar), name);
2643 return;
2644 }
2645 v = find_var_in_ht(ht, 0, varname, TRUE);
2646
2647 // Search in parent scope which is possible to reference from lambda
2648 if (v == NULL)
2649 v = find_var_in_scoped_ht(name, TRUE);
2650
2651 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
2652 && var_check_func_name(name, v == NULL))
2653 return;
2654
2655 if (v != NULL)
2656 {
2657 if (is_const)
2658 {
2659 emsg(_(e_cannot_mod));
2660 return;
2661 }
2662
2663 // existing variable, need to clear the value
2664 if (var_check_ro(v->di_flags, name, FALSE)
2665 || var_check_lock(v->di_tv.v_lock, name, FALSE))
2666 return;
2667
2668 // Handle setting internal v: variables separately where needed to
2669 // prevent changing the type.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002670 if (ht == &vimvarht)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002671 {
2672 if (v->di_tv.v_type == VAR_STRING)
2673 {
2674 VIM_CLEAR(v->di_tv.vval.v_string);
2675 if (copy || tv->v_type != VAR_STRING)
2676 {
2677 char_u *val = tv_get_string(tv);
2678
2679 // Careful: when assigning to v:errmsg and tv_get_string()
2680 // causes an error message the variable will alrady be set.
2681 if (v->di_tv.vval.v_string == NULL)
2682 v->di_tv.vval.v_string = vim_strsave(val);
2683 }
2684 else
2685 {
2686 // Take over the string to avoid an extra alloc/free.
2687 v->di_tv.vval.v_string = tv->vval.v_string;
2688 tv->vval.v_string = NULL;
2689 }
2690 return;
2691 }
2692 else if (v->di_tv.v_type == VAR_NUMBER)
2693 {
2694 v->di_tv.vval.v_number = tv_get_number(tv);
2695 if (STRCMP(varname, "searchforward") == 0)
2696 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
2697#ifdef FEAT_SEARCH_EXTRA
2698 else if (STRCMP(varname, "hlsearch") == 0)
2699 {
2700 no_hlsearch = !v->di_tv.vval.v_number;
2701 redraw_all_later(SOME_VALID);
2702 }
2703#endif
2704 return;
2705 }
2706 else if (v->di_tv.v_type != tv->v_type)
2707 {
2708 semsg(_("E963: setting %s to value with wrong type"), name);
2709 return;
2710 }
2711 }
2712
2713 clear_tv(&v->di_tv);
2714 }
2715 else // add a new variable
2716 {
2717 // Can't add "v:" or "a:" variable.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002718 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002719 {
2720 semsg(_(e_illvar), name);
2721 return;
2722 }
2723
2724 // Make sure the variable name is valid.
2725 if (!valid_varname(varname))
2726 return;
2727
2728 v = alloc(sizeof(dictitem_T) + STRLEN(varname));
2729 if (v == NULL)
2730 return;
2731 STRCPY(v->di_key, varname);
2732 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
2733 {
2734 vim_free(v);
2735 return;
2736 }
2737 v->di_flags = DI_FLAGS_ALLOC;
2738 if (is_const)
2739 v->di_flags |= DI_FLAGS_LOCK;
2740 }
2741
2742 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
2743 copy_tv(tv, &v->di_tv);
2744 else
2745 {
2746 v->di_tv = *tv;
2747 v->di_tv.v_lock = 0;
2748 init_tv(tv);
2749 }
2750
2751 if (is_const)
2752 v->di_tv.v_lock |= VAR_LOCKED;
2753}
2754
2755/*
2756 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
2757 * Also give an error message.
2758 */
2759 int
2760var_check_ro(int flags, char_u *name, int use_gettext)
2761{
2762 if (flags & DI_FLAGS_RO)
2763 {
2764 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
2765 return TRUE;
2766 }
2767 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
2768 {
2769 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
2770 return TRUE;
2771 }
2772 return FALSE;
2773}
2774
2775/*
2776 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
2777 * Also give an error message.
2778 */
2779 int
2780var_check_fixed(int flags, char_u *name, int use_gettext)
2781{
2782 if (flags & DI_FLAGS_FIX)
2783 {
2784 semsg(_("E795: Cannot delete variable %s"),
2785 use_gettext ? (char_u *)_(name) : name);
2786 return TRUE;
2787 }
2788 return FALSE;
2789}
2790
2791/*
2792 * Check if a funcref is assigned to a valid variable name.
2793 * Return TRUE and give an error if not.
2794 */
2795 int
2796var_check_func_name(
2797 char_u *name, // points to start of variable name
2798 int new_var) // TRUE when creating the variable
2799{
2800 // Allow for w: b: s: and t:.
2801 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
2802 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
2803 ? name[2] : name[0]))
2804 {
2805 semsg(_("E704: Funcref variable name must start with a capital: %s"),
2806 name);
2807 return TRUE;
2808 }
2809 // Don't allow hiding a function. When "v" is not NULL we might be
2810 // assigning another function to the same var, the type is checked
2811 // below.
2812 if (new_var && function_exists(name, FALSE))
2813 {
2814 semsg(_("E705: Variable name conflicts with existing function: %s"),
2815 name);
2816 return TRUE;
2817 }
2818 return FALSE;
2819}
2820
2821/*
2822 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
2823 * Also give an error message, using "name" or _("name") when use_gettext is
2824 * TRUE.
2825 */
2826 int
2827var_check_lock(int lock, char_u *name, int use_gettext)
2828{
2829 if (lock & VAR_LOCKED)
2830 {
2831 semsg(_("E741: Value is locked: %s"),
2832 name == NULL ? (char_u *)_("Unknown")
2833 : use_gettext ? (char_u *)_(name)
2834 : name);
2835 return TRUE;
2836 }
2837 if (lock & VAR_FIXED)
2838 {
2839 semsg(_("E742: Cannot change value of %s"),
2840 name == NULL ? (char_u *)_("Unknown")
2841 : use_gettext ? (char_u *)_(name)
2842 : name);
2843 return TRUE;
2844 }
2845 return FALSE;
2846}
2847
2848/*
2849 * Check if a variable name is valid.
2850 * Return FALSE and give an error if not.
2851 */
2852 int
2853valid_varname(char_u *varname)
2854{
2855 char_u *p;
2856
2857 for (p = varname; *p != NUL; ++p)
2858 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
2859 && *p != AUTOLOAD_CHAR)
2860 {
2861 semsg(_(e_illvar), varname);
2862 return FALSE;
2863 }
2864 return TRUE;
2865}
2866
2867/*
2868 * getwinvar() and gettabwinvar()
2869 */
2870 static void
2871getwinvar(
2872 typval_T *argvars,
2873 typval_T *rettv,
2874 int off) // 1 for gettabwinvar()
2875{
2876 win_T *win;
2877 char_u *varname;
2878 dictitem_T *v;
2879 tabpage_T *tp = NULL;
2880 int done = FALSE;
2881 win_T *oldcurwin;
2882 tabpage_T *oldtabpage;
2883 int need_switch_win;
2884
2885 if (off == 1)
2886 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
2887 else
2888 tp = curtab;
2889 win = find_win_by_nr(&argvars[off], tp);
2890 varname = tv_get_string_chk(&argvars[off + 1]);
2891 ++emsg_off;
2892
2893 rettv->v_type = VAR_STRING;
2894 rettv->vval.v_string = NULL;
2895
2896 if (win != NULL && varname != NULL)
2897 {
2898 // Set curwin to be our win, temporarily. Also set the tabpage,
2899 // otherwise the window is not valid. Only do this when needed,
2900 // autocommands get blocked.
2901 need_switch_win = !(tp == curtab && win == curwin);
2902 if (!need_switch_win
2903 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
2904 {
2905 if (*varname == '&')
2906 {
2907 if (varname[1] == NUL)
2908 {
2909 // get all window-local options in a dict
2910 dict_T *opts = get_winbuf_options(FALSE);
2911
2912 if (opts != NULL)
2913 {
2914 rettv_dict_set(rettv, opts);
2915 done = TRUE;
2916 }
2917 }
2918 else if (get_option_tv(&varname, rettv, 1) == OK)
2919 // window-local-option
2920 done = TRUE;
2921 }
2922 else
2923 {
2924 // Look up the variable.
2925 // Let getwinvar({nr}, "") return the "w:" dictionary.
2926 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
2927 varname, FALSE);
2928 if (v != NULL)
2929 {
2930 copy_tv(&v->di_tv, rettv);
2931 done = TRUE;
2932 }
2933 }
2934 }
2935
2936 if (need_switch_win)
2937 // restore previous notion of curwin
2938 restore_win(oldcurwin, oldtabpage, TRUE);
2939 }
2940
2941 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
2942 // use the default return value
2943 copy_tv(&argvars[off + 2], rettv);
2944
2945 --emsg_off;
2946}
2947
2948/*
2949 * "setwinvar()" and "settabwinvar()" functions
2950 */
2951 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01002952setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002953{
2954 win_T *win;
2955 win_T *save_curwin;
2956 tabpage_T *save_curtab;
2957 int need_switch_win;
2958 char_u *varname, *winvarname;
2959 typval_T *varp;
2960 char_u nbuf[NUMBUFLEN];
2961 tabpage_T *tp = NULL;
2962
2963 if (check_secure())
2964 return;
2965
2966 if (off == 1)
2967 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
2968 else
2969 tp = curtab;
2970 win = find_win_by_nr(&argvars[off], tp);
2971 varname = tv_get_string_chk(&argvars[off + 1]);
2972 varp = &argvars[off + 2];
2973
2974 if (win != NULL && varname != NULL && varp != NULL)
2975 {
2976 need_switch_win = !(tp == curtab && win == curwin);
2977 if (!need_switch_win
2978 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
2979 {
2980 if (*varname == '&')
2981 {
2982 long numval;
2983 char_u *strval;
2984 int error = FALSE;
2985
2986 ++varname;
2987 numval = (long)tv_get_number_chk(varp, &error);
2988 strval = tv_get_string_buf_chk(varp, nbuf);
2989 if (!error && strval != NULL)
2990 set_option_value(varname, numval, strval, OPT_LOCAL);
2991 }
2992 else
2993 {
2994 winvarname = alloc(STRLEN(varname) + 3);
2995 if (winvarname != NULL)
2996 {
2997 STRCPY(winvarname, "w:");
2998 STRCPY(winvarname + 2, varname);
2999 set_var(winvarname, varp, TRUE);
3000 vim_free(winvarname);
3001 }
3002 }
3003 }
3004 if (need_switch_win)
3005 restore_win(save_curwin, save_curtab, TRUE);
3006 }
3007}
3008
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003009/*
3010 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
3011 * v:option_type, and v:option_command.
3012 */
3013 void
3014reset_v_option_vars(void)
3015{
3016 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
3017 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
3018 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
3019 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
3020 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
3021 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
3022}
3023
3024/*
3025 * Add an assert error to v:errors.
3026 */
3027 void
3028assert_error(garray_T *gap)
3029{
3030 struct vimvar *vp = &vimvars[VV_ERRORS];
3031
3032 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003033 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003034 set_vim_var_list(VV_ERRORS, list_alloc());
3035 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
3036}
3037
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003038 int
3039var_exists(char_u *var)
3040{
3041 char_u *name;
3042 char_u *tofree;
3043 typval_T tv;
3044 int len = 0;
3045 int n = FALSE;
3046
3047 // get_name_len() takes care of expanding curly braces
3048 name = var;
3049 len = get_name_len(&var, &tofree, TRUE, FALSE);
3050 if (len > 0)
3051 {
3052 if (tofree != NULL)
3053 name = tofree;
3054 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
3055 if (n)
3056 {
3057 // handle d.key, l[idx], f(expr)
3058 n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
3059 if (n)
3060 clear_tv(&tv);
3061 }
3062 }
3063 if (*var != NUL)
3064 n = FALSE;
3065
3066 vim_free(tofree);
3067 return n;
3068}
3069
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003070static lval_T *redir_lval = NULL;
3071#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
3072static garray_T redir_ga; // only valid when redir_lval is not NULL
3073static char_u *redir_endp = NULL;
3074static char_u *redir_varname = NULL;
3075
3076/*
3077 * Start recording command output to a variable
3078 * When "append" is TRUE append to an existing variable.
3079 * Returns OK if successfully completed the setup. FAIL otherwise.
3080 */
3081 int
3082var_redir_start(char_u *name, int append)
3083{
3084 int save_emsg;
3085 int err;
3086 typval_T tv;
3087
3088 // Catch a bad name early.
3089 if (!eval_isnamec1(*name))
3090 {
3091 emsg(_(e_invarg));
3092 return FAIL;
3093 }
3094
3095 // Make a copy of the name, it is used in redir_lval until redir ends.
3096 redir_varname = vim_strsave(name);
3097 if (redir_varname == NULL)
3098 return FAIL;
3099
3100 redir_lval = ALLOC_CLEAR_ONE(lval_T);
3101 if (redir_lval == NULL)
3102 {
3103 var_redir_stop();
3104 return FAIL;
3105 }
3106
3107 // The output is stored in growarray "redir_ga" until redirection ends.
3108 ga_init2(&redir_ga, (int)sizeof(char), 500);
3109
3110 // Parse the variable name (can be a dict or list entry).
3111 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
3112 FNE_CHECK_START);
3113 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
3114 {
3115 clear_lval(redir_lval);
3116 if (redir_endp != NULL && *redir_endp != NUL)
3117 // Trailing characters are present after the variable name
3118 emsg(_(e_trailing));
3119 else
3120 emsg(_(e_invarg));
3121 redir_endp = NULL; // don't store a value, only cleanup
3122 var_redir_stop();
3123 return FAIL;
3124 }
3125
3126 // check if we can write to the variable: set it to or append an empty
3127 // string
3128 save_emsg = did_emsg;
3129 did_emsg = FALSE;
3130 tv.v_type = VAR_STRING;
3131 tv.vval.v_string = (char_u *)"";
3132 if (append)
3133 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
3134 else
3135 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
3136 clear_lval(redir_lval);
3137 err = did_emsg;
3138 did_emsg |= save_emsg;
3139 if (err)
3140 {
3141 redir_endp = NULL; // don't store a value, only cleanup
3142 var_redir_stop();
3143 return FAIL;
3144 }
3145
3146 return OK;
3147}
3148
3149/*
3150 * Append "value[value_len]" to the variable set by var_redir_start().
3151 * The actual appending is postponed until redirection ends, because the value
3152 * appended may in fact be the string we write to, changing it may cause freed
3153 * memory to be used:
3154 * :redir => foo
3155 * :let foo
3156 * :redir END
3157 */
3158 void
3159var_redir_str(char_u *value, int value_len)
3160{
3161 int len;
3162
3163 if (redir_lval == NULL)
3164 return;
3165
3166 if (value_len == -1)
3167 len = (int)STRLEN(value); // Append the entire string
3168 else
3169 len = value_len; // Append only "value_len" characters
3170
3171 if (ga_grow(&redir_ga, len) == OK)
3172 {
3173 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
3174 redir_ga.ga_len += len;
3175 }
3176 else
3177 var_redir_stop();
3178}
3179
3180/*
3181 * Stop redirecting command output to a variable.
3182 * Frees the allocated memory.
3183 */
3184 void
3185var_redir_stop(void)
3186{
3187 typval_T tv;
3188
3189 if (EVALCMD_BUSY)
3190 {
3191 redir_lval = NULL;
3192 return;
3193 }
3194
3195 if (redir_lval != NULL)
3196 {
3197 // If there was no error: assign the text to the variable.
3198 if (redir_endp != NULL)
3199 {
3200 ga_append(&redir_ga, NUL); // Append the trailing NUL.
3201 tv.v_type = VAR_STRING;
3202 tv.vval.v_string = redir_ga.ga_data;
3203 // Call get_lval() again, if it's inside a Dict or List it may
3204 // have changed.
3205 redir_endp = get_lval(redir_varname, NULL, redir_lval,
3206 FALSE, FALSE, 0, FNE_CHECK_START);
3207 if (redir_endp != NULL && redir_lval->ll_name != NULL)
3208 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
3209 (char_u *)".");
3210 clear_lval(redir_lval);
3211 }
3212
3213 // free the collected output
3214 VIM_CLEAR(redir_ga.ga_data);
3215
3216 VIM_CLEAR(redir_lval);
3217 }
3218 VIM_CLEAR(redir_varname);
3219}
3220
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003221/*
3222 * "gettabvar()" function
3223 */
3224 void
3225f_gettabvar(typval_T *argvars, typval_T *rettv)
3226{
3227 win_T *oldcurwin;
3228 tabpage_T *tp, *oldtabpage;
3229 dictitem_T *v;
3230 char_u *varname;
3231 int done = FALSE;
3232
3233 rettv->v_type = VAR_STRING;
3234 rettv->vval.v_string = NULL;
3235
3236 varname = tv_get_string_chk(&argvars[1]);
3237 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3238 if (tp != NULL && varname != NULL)
3239 {
3240 // Set tp to be our tabpage, temporarily. Also set the window to the
3241 // first window in the tabpage, otherwise the window is not valid.
3242 if (switch_win(&oldcurwin, &oldtabpage,
3243 tp == curtab || tp->tp_firstwin == NULL ? firstwin
3244 : tp->tp_firstwin, tp, TRUE) == OK)
3245 {
3246 // look up the variable
3247 // Let gettabvar({nr}, "") return the "t:" dictionary.
3248 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
3249 if (v != NULL)
3250 {
3251 copy_tv(&v->di_tv, rettv);
3252 done = TRUE;
3253 }
3254 }
3255
3256 // restore previous notion of curwin
3257 restore_win(oldcurwin, oldtabpage, TRUE);
3258 }
3259
3260 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3261 copy_tv(&argvars[2], rettv);
3262}
3263
3264/*
3265 * "gettabwinvar()" function
3266 */
3267 void
3268f_gettabwinvar(typval_T *argvars, typval_T *rettv)
3269{
3270 getwinvar(argvars, rettv, 1);
3271}
3272
3273/*
3274 * "getwinvar()" function
3275 */
3276 void
3277f_getwinvar(typval_T *argvars, typval_T *rettv)
3278{
3279 getwinvar(argvars, rettv, 0);
3280}
3281
3282/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003283 * "getbufvar()" function
3284 */
3285 void
3286f_getbufvar(typval_T *argvars, typval_T *rettv)
3287{
3288 buf_T *buf;
3289 buf_T *save_curbuf;
3290 char_u *varname;
3291 dictitem_T *v;
3292 int done = FALSE;
3293
3294 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3295 varname = tv_get_string_chk(&argvars[1]);
3296 ++emsg_off;
3297 buf = tv_get_buf(&argvars[0], FALSE);
3298
3299 rettv->v_type = VAR_STRING;
3300 rettv->vval.v_string = NULL;
3301
3302 if (buf != NULL && varname != NULL)
3303 {
3304 // set curbuf to be our buf, temporarily
3305 save_curbuf = curbuf;
3306 curbuf = buf;
3307
3308 if (*varname == '&')
3309 {
3310 if (varname[1] == NUL)
3311 {
3312 // get all buffer-local options in a dict
3313 dict_T *opts = get_winbuf_options(TRUE);
3314
3315 if (opts != NULL)
3316 {
3317 rettv_dict_set(rettv, opts);
3318 done = TRUE;
3319 }
3320 }
3321 else if (get_option_tv(&varname, rettv, TRUE) == OK)
3322 // buffer-local-option
3323 done = TRUE;
3324 }
3325 else
3326 {
3327 // Look up the variable.
3328 // Let getbufvar({nr}, "") return the "b:" dictionary.
3329 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
3330 'b', varname, FALSE);
3331 if (v != NULL)
3332 {
3333 copy_tv(&v->di_tv, rettv);
3334 done = TRUE;
3335 }
3336 }
3337
3338 // restore previous notion of curbuf
3339 curbuf = save_curbuf;
3340 }
3341
3342 if (!done && argvars[2].v_type != VAR_UNKNOWN)
3343 // use the default value
3344 copy_tv(&argvars[2], rettv);
3345
3346 --emsg_off;
3347}
3348
3349/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003350 * "settabvar()" function
3351 */
3352 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003353f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003354{
3355 tabpage_T *save_curtab;
3356 tabpage_T *tp;
3357 char_u *varname, *tabvarname;
3358 typval_T *varp;
3359
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003360 if (check_secure())
3361 return;
3362
3363 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3364 varname = tv_get_string_chk(&argvars[1]);
3365 varp = &argvars[2];
3366
3367 if (varname != NULL && varp != NULL && tp != NULL)
3368 {
3369 save_curtab = curtab;
3370 goto_tabpage_tp(tp, FALSE, FALSE);
3371
3372 tabvarname = alloc(STRLEN(varname) + 3);
3373 if (tabvarname != NULL)
3374 {
3375 STRCPY(tabvarname, "t:");
3376 STRCPY(tabvarname + 2, varname);
3377 set_var(tabvarname, varp, TRUE);
3378 vim_free(tabvarname);
3379 }
3380
3381 // Restore current tabpage
3382 if (valid_tabpage(save_curtab))
3383 goto_tabpage_tp(save_curtab, FALSE, FALSE);
3384 }
3385}
3386
3387/*
3388 * "settabwinvar()" function
3389 */
3390 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003391f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003392{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003393 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003394}
3395
3396/*
3397 * "setwinvar()" function
3398 */
3399 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003400f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003401{
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003402 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003403}
3404
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003405/*
3406 * "setbufvar()" function
3407 */
3408 void
3409f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
3410{
3411 buf_T *buf;
3412 char_u *varname, *bufvarname;
3413 typval_T *varp;
3414 char_u nbuf[NUMBUFLEN];
3415
3416 if (check_secure())
3417 return;
3418 (void)tv_get_number(&argvars[0]); // issue errmsg if type error
3419 varname = tv_get_string_chk(&argvars[1]);
3420 buf = tv_get_buf(&argvars[0], FALSE);
3421 varp = &argvars[2];
3422
3423 if (buf != NULL && varname != NULL && varp != NULL)
3424 {
3425 if (*varname == '&')
3426 {
3427 long numval;
3428 char_u *strval;
3429 int error = FALSE;
3430 aco_save_T aco;
3431
3432 // set curbuf to be our buf, temporarily
3433 aucmd_prepbuf(&aco, buf);
3434
3435 ++varname;
3436 numval = (long)tv_get_number_chk(varp, &error);
3437 strval = tv_get_string_buf_chk(varp, nbuf);
3438 if (!error && strval != NULL)
3439 set_option_value(varname, numval, strval, OPT_LOCAL);
3440
3441 // reset notion of buffer
3442 aucmd_restbuf(&aco);
3443 }
3444 else
3445 {
3446 buf_T *save_curbuf = curbuf;
3447
3448 bufvarname = alloc(STRLEN(varname) + 3);
3449 if (bufvarname != NULL)
3450 {
3451 curbuf = buf;
3452 STRCPY(bufvarname, "b:");
3453 STRCPY(bufvarname + 2, varname);
3454 set_var(bufvarname, varp, TRUE);
3455 vim_free(bufvarname);
3456 curbuf = save_curbuf;
3457 }
3458 }
3459 }
3460}
3461
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02003462/*
3463 * Get a callback from "arg". It can be a Funcref or a function name.
3464 * When "arg" is zero return an empty string.
3465 * "cb_name" is not allocated.
3466 * "cb_name" is set to NULL for an invalid argument.
3467 */
3468 callback_T
3469get_callback(typval_T *arg)
3470{
3471 callback_T res;
3472
3473 res.cb_free_name = FALSE;
3474 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
3475 {
3476 res.cb_partial = arg->vval.v_partial;
3477 ++res.cb_partial->pt_refcount;
3478 res.cb_name = partial_name(res.cb_partial);
3479 }
3480 else
3481 {
3482 res.cb_partial = NULL;
3483 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
3484 {
3485 // Note that we don't make a copy of the string.
3486 res.cb_name = arg->vval.v_string;
3487 func_ref(res.cb_name);
3488 }
3489 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
3490 {
3491 res.cb_name = (char_u *)"";
3492 }
3493 else
3494 {
3495 emsg(_("E921: Invalid callback argument"));
3496 res.cb_name = NULL;
3497 }
3498 }
3499 return res;
3500}
3501
3502/*
3503 * Copy a callback into a typval_T.
3504 */
3505 void
3506put_callback(callback_T *cb, typval_T *tv)
3507{
3508 if (cb->cb_partial != NULL)
3509 {
3510 tv->v_type = VAR_PARTIAL;
3511 tv->vval.v_partial = cb->cb_partial;
3512 ++tv->vval.v_partial->pt_refcount;
3513 }
3514 else
3515 {
3516 tv->v_type = VAR_FUNC;
3517 tv->vval.v_string = vim_strsave(cb->cb_name);
3518 func_ref(cb->cb_name);
3519 }
3520}
3521
3522/*
3523 * Make a copy of "src" into "dest", allocating the function name if needed,
3524 * without incrementing the refcount.
3525 */
3526 void
3527set_callback(callback_T *dest, callback_T *src)
3528{
3529 if (src->cb_partial == NULL)
3530 {
3531 // just a function name, make a copy
3532 dest->cb_name = vim_strsave(src->cb_name);
3533 dest->cb_free_name = TRUE;
3534 }
3535 else
3536 {
3537 // cb_name is a pointer into cb_partial
3538 dest->cb_name = src->cb_name;
3539 dest->cb_free_name = FALSE;
3540 }
3541 dest->cb_partial = src->cb_partial;
3542}
3543
3544/*
3545 * Unref/free "callback" returned by get_callback() or set_callback().
3546 */
3547 void
3548free_callback(callback_T *callback)
3549{
3550 if (callback->cb_partial != NULL)
3551 {
3552 partial_unref(callback->cb_partial);
3553 callback->cb_partial = NULL;
3554 }
3555 else if (callback->cb_name != NULL)
3556 func_unref(callback->cb_name);
3557 if (callback->cb_free_name)
3558 {
3559 vim_free(callback->cb_name);
3560 callback->cb_free_name = FALSE;
3561 }
3562 callback->cb_name = NULL;
3563}
3564
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003565#endif // FEAT_EVAL