blob: 9dbdc75c0c99a241d8b4116c94f33d7a91a8130f [file] [log] [blame]
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * evalvars.c: functions for dealing with variables
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaare5cdf152019-08-29 22:09:46 +020018static dictitem_T globvars_var; // variable used for g:
Bram Moolenaarda6c0332019-09-01 16:01:30 +020019static dict_T globvardict; // Dictionary with g: variables
20#define globvarht globvardict.dv_hashtab
Bram Moolenaare5cdf152019-08-29 22:09:46 +020021
22/*
23 * Old Vim variables such as "v:version" are also available without the "v:".
24 * Also in functions. We need a special hashtable for them.
25 */
26static hashtab_T compat_hashtab;
27
28/*
29 * Array to hold the value of v: variables.
30 * The value is in a dictitem, so that it can also be used in the v: scope.
31 * The reason to use this table anyway is for very quick access to the
32 * variables with the VV_ defines.
33 */
34
35// values for vv_flags:
36#define VV_COMPAT 1 // compatible, also used without "v:"
37#define VV_RO 2 // read-only
38#define VV_RO_SBX 4 // read-only in the sandbox
39
40#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
41
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010042typedef struct vimvar vimvar_T;
43
Bram Moolenaare5cdf152019-08-29 22:09:46 +020044static struct vimvar
45{
46 char *vv_name; // name of variable, without v:
47 dictitem16_T vv_di; // value and name for key (max 16 chars!)
Bram Moolenaard787e402021-12-24 21:36:12 +000048 type_T *vv_type; // type or NULL
Bram Moolenaare5cdf152019-08-29 22:09:46 +020049 char vv_flags; // VV_COMPAT, VV_RO, VV_RO_SBX
50} vimvars[VV_LEN] =
51{
Bram Moolenaar8d71b542019-08-30 15:46:30 +020052 // The order here must match the VV_ defines in vim.h!
53 // Initializing a union does not work, leave tv.vval empty to get zero's.
Bram Moolenaard787e402021-12-24 21:36:12 +000054 {VV_NAME("count", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
55 {VV_NAME("count1", VAR_NUMBER), NULL, VV_RO},
56 {VV_NAME("prevcount", VAR_NUMBER), NULL, VV_RO},
57 {VV_NAME("errmsg", VAR_STRING), NULL, VV_COMPAT},
58 {VV_NAME("warningmsg", VAR_STRING), NULL, 0},
59 {VV_NAME("statusmsg", VAR_STRING), NULL, 0},
60 {VV_NAME("shell_error", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
61 {VV_NAME("this_session", VAR_STRING), NULL, VV_COMPAT},
62 {VV_NAME("version", VAR_NUMBER), NULL, VV_COMPAT+VV_RO},
63 {VV_NAME("lnum", VAR_NUMBER), NULL, VV_RO_SBX},
64 {VV_NAME("termresponse", VAR_STRING), NULL, VV_RO},
65 {VV_NAME("fname", VAR_STRING), NULL, VV_RO},
66 {VV_NAME("lang", VAR_STRING), NULL, VV_RO},
67 {VV_NAME("lc_time", VAR_STRING), NULL, VV_RO},
68 {VV_NAME("ctype", VAR_STRING), NULL, VV_RO},
69 {VV_NAME("charconvert_from", VAR_STRING), NULL, VV_RO},
70 {VV_NAME("charconvert_to", VAR_STRING), NULL, VV_RO},
71 {VV_NAME("fname_in", VAR_STRING), NULL, VV_RO},
72 {VV_NAME("fname_out", VAR_STRING), NULL, VV_RO},
73 {VV_NAME("fname_new", VAR_STRING), NULL, VV_RO},
74 {VV_NAME("fname_diff", VAR_STRING), NULL, VV_RO},
75 {VV_NAME("cmdarg", VAR_STRING), NULL, VV_RO},
76 {VV_NAME("foldstart", VAR_NUMBER), NULL, VV_RO_SBX},
77 {VV_NAME("foldend", VAR_NUMBER), NULL, VV_RO_SBX},
78 {VV_NAME("folddashes", VAR_STRING), NULL, VV_RO_SBX},
79 {VV_NAME("foldlevel", VAR_NUMBER), NULL, VV_RO_SBX},
80 {VV_NAME("progname", VAR_STRING), NULL, VV_RO},
81 {VV_NAME("servername", VAR_STRING), NULL, VV_RO},
82 {VV_NAME("dying", VAR_NUMBER), NULL, VV_RO},
83 {VV_NAME("exception", VAR_STRING), NULL, VV_RO},
84 {VV_NAME("throwpoint", VAR_STRING), NULL, VV_RO},
85 {VV_NAME("register", VAR_STRING), NULL, VV_RO},
86 {VV_NAME("cmdbang", VAR_NUMBER), NULL, VV_RO},
87 {VV_NAME("insertmode", VAR_STRING), NULL, VV_RO},
88 {VV_NAME("val", VAR_UNKNOWN), NULL, VV_RO},
89 {VV_NAME("key", VAR_UNKNOWN), NULL, VV_RO},
90 {VV_NAME("profiling", VAR_NUMBER), NULL, VV_RO},
91 {VV_NAME("fcs_reason", VAR_STRING), NULL, VV_RO},
92 {VV_NAME("fcs_choice", VAR_STRING), NULL, 0},
93 {VV_NAME("beval_bufnr", VAR_NUMBER), NULL, VV_RO},
94 {VV_NAME("beval_winnr", VAR_NUMBER), NULL, VV_RO},
95 {VV_NAME("beval_winid", VAR_NUMBER), NULL, VV_RO},
96 {VV_NAME("beval_lnum", VAR_NUMBER), NULL, VV_RO},
97 {VV_NAME("beval_col", VAR_NUMBER), NULL, VV_RO},
98 {VV_NAME("beval_text", VAR_STRING), NULL, VV_RO},
99 {VV_NAME("scrollstart", VAR_STRING), NULL, 0},
100 {VV_NAME("swapname", VAR_STRING), NULL, VV_RO},
101 {VV_NAME("swapchoice", VAR_STRING), NULL, 0},
102 {VV_NAME("swapcommand", VAR_STRING), NULL, VV_RO},
103 {VV_NAME("char", VAR_STRING), NULL, 0},
104 {VV_NAME("mouse_win", VAR_NUMBER), NULL, 0},
105 {VV_NAME("mouse_winid", VAR_NUMBER), NULL, 0},
106 {VV_NAME("mouse_lnum", VAR_NUMBER), NULL, 0},
107 {VV_NAME("mouse_col", VAR_NUMBER), NULL, 0},
108 {VV_NAME("operator", VAR_STRING), NULL, VV_RO},
109 {VV_NAME("searchforward", VAR_NUMBER), NULL, 0},
110 {VV_NAME("hlsearch", VAR_NUMBER), NULL, 0},
111 {VV_NAME("oldfiles", VAR_LIST), &t_list_string, 0},
112 {VV_NAME("windowid", VAR_NUMBER), NULL, VV_RO},
113 {VV_NAME("progpath", VAR_STRING), NULL, VV_RO},
114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, VV_RO},
115 {VV_NAME("option_new", VAR_STRING), NULL, VV_RO},
116 {VV_NAME("option_old", VAR_STRING), NULL, VV_RO},
117 {VV_NAME("option_oldlocal", VAR_STRING), NULL, VV_RO},
118 {VV_NAME("option_oldglobal", VAR_STRING), NULL, VV_RO},
119 {VV_NAME("option_command", VAR_STRING), NULL, VV_RO},
120 {VV_NAME("option_type", VAR_STRING), NULL, VV_RO},
121 {VV_NAME("errors", VAR_LIST), &t_list_string, 0},
122 {VV_NAME("false", VAR_BOOL), NULL, VV_RO},
123 {VV_NAME("true", VAR_BOOL), NULL, VV_RO},
124 {VV_NAME("none", VAR_SPECIAL), NULL, VV_RO},
125 {VV_NAME("null", VAR_SPECIAL), NULL, VV_RO},
126 {VV_NAME("numbermax", VAR_NUMBER), NULL, VV_RO},
127 {VV_NAME("numbermin", VAR_NUMBER), NULL, VV_RO},
128 {VV_NAME("numbersize", VAR_NUMBER), NULL, VV_RO},
129 {VV_NAME("vim_did_enter", VAR_NUMBER), NULL, VV_RO},
130 {VV_NAME("testing", VAR_NUMBER), NULL, 0},
131 {VV_NAME("t_number", VAR_NUMBER), NULL, VV_RO},
132 {VV_NAME("t_string", VAR_NUMBER), NULL, VV_RO},
133 {VV_NAME("t_func", VAR_NUMBER), NULL, VV_RO},
134 {VV_NAME("t_list", VAR_NUMBER), NULL, VV_RO},
135 {VV_NAME("t_dict", VAR_NUMBER), NULL, VV_RO},
136 {VV_NAME("t_float", VAR_NUMBER), NULL, VV_RO},
137 {VV_NAME("t_bool", VAR_NUMBER), NULL, VV_RO},
138 {VV_NAME("t_none", VAR_NUMBER), NULL, VV_RO},
139 {VV_NAME("t_job", VAR_NUMBER), NULL, VV_RO},
140 {VV_NAME("t_channel", VAR_NUMBER), NULL, VV_RO},
141 {VV_NAME("t_blob", VAR_NUMBER), NULL, VV_RO},
142 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
143 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
144 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
148 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
149 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
150 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
151 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
152 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
153 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
154 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
155 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
156 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000157 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200158};
159
160// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000161#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200162#define vv_nr vv_di.di_tv.vval.v_number
163#define vv_float vv_di.di_tv.vval.v_float
164#define vv_str vv_di.di_tv.vval.v_string
165#define vv_list vv_di.di_tv.vval.v_list
166#define vv_dict vv_di.di_tv.vval.v_dict
167#define vv_blob vv_di.di_tv.vval.v_blob
168#define vv_tv vv_di.di_tv
169
170static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200171static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200172#define vimvarht vimvardict.dv_hashtab
173
174// for VIM_VERSION_ defines
175#include "version.h"
176
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200177static void list_glob_vars(int *first);
178static void list_buf_vars(int *first);
179static void list_win_vars(int *first);
180static void list_tab_vars(int *first);
181static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100182static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int flags, char_u *endchars, char_u *op, int var_idx);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +0200183static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
184static int do_lock_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200185static void list_one_var(dictitem_T *v, char *prefix, int *first);
186static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
187
188/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200189 * Initialize global and vim special variables
190 */
191 void
192evalvars_init(void)
193{
194 int i;
195 struct vimvar *p;
196
197 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
198 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
199 vimvardict.dv_lock = VAR_FIXED;
200 hash_init(&compat_hashtab);
201
202 for (i = 0; i < VV_LEN; ++i)
203 {
204 p = &vimvars[i];
205 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
206 {
207 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
208 getout(1);
209 }
210 STRCPY(p->vv_di.di_key, p->vv_name);
211 if (p->vv_flags & VV_RO)
212 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
213 else if (p->vv_flags & VV_RO_SBX)
214 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
215 else
216 p->vv_di.di_flags = DI_FLAGS_FIX;
217
218 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000219 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200220 hash_add(&vimvarht, p->vv_di.di_key);
221 if (p->vv_flags & VV_COMPAT)
222 // add to compat scope dict
223 hash_add(&compat_hashtab, p->vv_di.di_key);
224 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200225 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
226 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200227
228 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
229 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100230 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
232 set_vim_var_list(VV_ERRORS, list_alloc());
233 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
234
235 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
236 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
237 set_vim_var_nr(VV_NONE, VVAL_NONE);
238 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100239 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
240 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100241 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000242 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
243 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
244 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000245 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200246
247 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
248 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
249 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
250 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
251 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
252 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
253 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
254 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
255 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
256 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
257 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
258
259 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
260
Drew Vogele30d1022021-10-24 20:35:07 +0100261 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
262
Bram Moolenaar439c0362020-06-06 15:58:03 +0200263 // Default for v:register is not 0 but '"'. This is adjusted once the
264 // clipboard has been setup by calling reset_reg_var().
265 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200266}
267
268#if defined(EXITFREE) || defined(PROTO)
269/*
270 * Free all vim variables information on exit
271 */
272 void
273evalvars_clear(void)
274{
275 int i;
276 struct vimvar *p;
277
278 for (i = 0; i < VV_LEN; ++i)
279 {
280 p = &vimvars[i];
281 if (p->vv_di.di_tv.v_type == VAR_STRING)
282 VIM_CLEAR(p->vv_str);
283 else if (p->vv_di.di_tv.v_type == VAR_LIST)
284 {
285 list_unref(p->vv_list);
286 p->vv_list = NULL;
287 }
288 }
289 hash_clear(&vimvarht);
290 hash_init(&vimvarht); // garbage_collect() will access it
291 hash_clear(&compat_hashtab);
292
293 // global variables
294 vars_clear(&globvarht);
295
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100296 // Script-local variables. Clear all the variables here.
297 // The scriptvar_T is cleared later in free_scriptnames(), because a
298 // variable in one script might hold a reference to the whole scope of
299 // another script.
300 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200301 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200302}
303#endif
304
305 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200306garbage_collect_globvars(int copyID)
307{
308 return set_ref_in_ht(&globvarht, copyID, NULL);
309}
310
311 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200312garbage_collect_vimvars(int copyID)
313{
314 return set_ref_in_ht(&vimvarht, copyID, NULL);
315}
316
317 int
318garbage_collect_scriptvars(int copyID)
319{
Bram Moolenaared234f22020-10-15 20:42:20 +0200320 int i;
321 int idx;
322 int abort = FALSE;
323 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200324
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100325 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200326 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200327 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
328
Bram Moolenaared234f22020-10-15 20:42:20 +0200329 si = SCRIPT_ITEM(i);
330 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
331 {
332 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
333
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100334 if (sv->sv_name != NULL)
335 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 }
337 }
338
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200339 return abort;
340}
341
342/*
343 * Set an internal variable to a string value. Creates the variable if it does
344 * not already exist.
345 */
346 void
347set_internal_string_var(char_u *name, char_u *value)
348{
349 char_u *val;
350 typval_T *tvp;
351
352 val = vim_strsave(value);
353 if (val != NULL)
354 {
355 tvp = alloc_string_tv(val);
356 if (tvp != NULL)
357 {
358 set_var(name, tvp, FALSE);
359 free_tv(tvp);
360 }
361 }
362}
363
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200364 int
365eval_charconvert(
366 char_u *enc_from,
367 char_u *enc_to,
368 char_u *fname_from,
369 char_u *fname_to)
370{
371 int err = FALSE;
372
373 set_vim_var_string(VV_CC_FROM, enc_from, -1);
374 set_vim_var_string(VV_CC_TO, enc_to, -1);
375 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
376 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
377 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
378 err = TRUE;
379 set_vim_var_string(VV_CC_FROM, NULL, -1);
380 set_vim_var_string(VV_CC_TO, NULL, -1);
381 set_vim_var_string(VV_FNAME_IN, NULL, -1);
382 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
383
384 if (err)
385 return FAIL;
386 return OK;
387}
388
389# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
390 int
391eval_printexpr(char_u *fname, char_u *args)
392{
393 int err = FALSE;
394
395 set_vim_var_string(VV_FNAME_IN, fname, -1);
396 set_vim_var_string(VV_CMDARG, args, -1);
397 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
398 err = TRUE;
399 set_vim_var_string(VV_FNAME_IN, NULL, -1);
400 set_vim_var_string(VV_CMDARG, NULL, -1);
401
402 if (err)
403 {
404 mch_remove(fname);
405 return FAIL;
406 }
407 return OK;
408}
409# endif
410
411# if defined(FEAT_DIFF) || defined(PROTO)
412 void
413eval_diff(
414 char_u *origfile,
415 char_u *newfile,
416 char_u *outfile)
417{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000418 sctx_T saved_sctx = current_sctx;
419 sctx_T *ctx;
420 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200421
422 set_vim_var_string(VV_FNAME_IN, origfile, -1);
423 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
424 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000425
426 ctx = get_option_sctx("diffexpr");
427 if (ctx != NULL)
428 current_sctx = *ctx;
429
430 // errors are ignored
431 tv = eval_expr(p_dex, NULL);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000432 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000433
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200434 set_vim_var_string(VV_FNAME_IN, NULL, -1);
435 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
436 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000437 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200438}
439
440 void
441eval_patch(
442 char_u *origfile,
443 char_u *difffile,
444 char_u *outfile)
445{
446 int err;
447
448 set_vim_var_string(VV_FNAME_IN, origfile, -1);
449 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
450 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
451 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
452 set_vim_var_string(VV_FNAME_IN, NULL, -1);
453 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
454 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
455}
456# endif
457
458#if defined(FEAT_SPELL) || defined(PROTO)
459/*
460 * Evaluate an expression to a list with suggestions.
461 * For the "expr:" part of 'spellsuggest'.
462 * Returns NULL when there is an error.
463 */
464 list_T *
465eval_spell_expr(char_u *badword, char_u *expr)
466{
467 typval_T save_val;
468 typval_T rettv;
469 list_T *list = NULL;
470 char_u *p = skipwhite(expr);
471
472 // Set "v:val" to the bad word.
473 prepare_vimvar(VV_VAL, &save_val);
474 set_vim_var_string(VV_VAL, badword, -1);
475 if (p_verbose == 0)
476 ++emsg_off;
477
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200478 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200479 {
480 if (rettv.v_type != VAR_LIST)
481 clear_tv(&rettv);
482 else
483 list = rettv.vval.v_list;
484 }
485
486 if (p_verbose == 0)
487 --emsg_off;
488 clear_tv(get_vim_var_tv(VV_VAL));
489 restore_vimvar(VV_VAL, &save_val);
490
491 return list;
492}
493
494/*
495 * "list" is supposed to contain two items: a word and a number. Return the
496 * word in "pp" and the number as the return value.
497 * Return -1 if anything isn't right.
498 * Used to get the good word and score from the eval_spell_expr() result.
499 */
500 int
501get_spellword(list_T *list, char_u **pp)
502{
503 listitem_T *li;
504
505 li = list->lv_first;
506 if (li == NULL)
507 return -1;
508 *pp = tv_get_string(&li->li_tv);
509
510 li = li->li_next;
511 if (li == NULL)
512 return -1;
513 return (int)tv_get_number(&li->li_tv);
514}
515#endif
516
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200517/*
518 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200519 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200520 * When not used yet add the variable to the v: hashtable.
521 */
522 void
523prepare_vimvar(int idx, typval_T *save_tv)
524{
525 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200526 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000527 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200528 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
529}
530
531/*
532 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200533 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200534 * When no longer defined, remove the variable from the v: hashtable.
535 */
536 void
537restore_vimvar(int idx, typval_T *save_tv)
538{
539 hashitem_T *hi;
540
541 vimvars[idx].vv_tv = *save_tv;
Bram Moolenaard787e402021-12-24 21:36:12 +0000542 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200543 {
544 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
545 if (HASHITEM_EMPTY(hi))
546 internal_error("restore_vimvar()");
547 else
548 hash_remove(&vimvarht, hi);
549 }
550}
551
552/*
553 * List Vim variables.
554 */
555 static void
556list_vim_vars(int *first)
557{
558 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
559}
560
561/*
562 * List script-local variables, if there is a script.
563 */
564 static void
565list_script_vars(int *first)
566{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200567 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200568 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
569 "s:", FALSE, first);
570}
571
572/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200573 * Get a list of lines from a HERE document. The here document is a list of
574 * lines surrounded by a marker.
575 * cmd << {marker}
576 * {line1}
577 * {line2}
578 * ....
579 * {marker}
580 *
581 * The {marker} is a string. If the optional 'trim' word is supplied before the
582 * marker, then the leading indentation before the lines (matching the
583 * indentation in the 'cmd' line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200584 *
585 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
586 * tcl, mzscheme), script_get is set to TRUE. In this case, if the marker is
587 * missing, then '.' is accepted as a marker.
588 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200589 * Returns a List with {lines} or NULL.
590 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100591 list_T *
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200592heredoc_get(exarg_T *eap, char_u *cmd, int script_get)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200593{
594 char_u *theline;
595 char_u *marker;
596 list_T *l;
597 char_u *p;
598 int marker_indent_len = 0;
599 int text_indent_len = 0;
600 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200601 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200602 int comment_char = in_vim9script() ? '#' : '"';
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200603
604 if (eap->getline == NULL)
605 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000606 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200607 return NULL;
608 }
609
610 // Check for the optional 'trim' word before the marker
611 cmd = skipwhite(cmd);
612 if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
613 {
614 cmd = skipwhite(cmd + 4);
615
616 // Trim the indentation from all the lines in the here document.
617 // The amount of indentation trimmed is the same as the indentation of
618 // the first line after the :let command line. To find the end marker
619 // the indent of the :let command line is trimmed.
620 p = *eap->cmdlinep;
621 while (VIM_ISWHITE(*p))
622 {
623 p++;
624 marker_indent_len++;
625 }
626 text_indent_len = -1;
627 }
628
629 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200630 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200631 {
632 marker = skipwhite(cmd);
633 p = skiptowhite(marker);
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200634 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200635 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000636 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200637 return NULL;
638 }
639 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200640 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200641 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000642 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200643 return NULL;
644 }
645 }
646 else
647 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200648 // When getting lines for an embedded script, if the marker is missing,
649 // accept '.' as the marker.
650 if (script_get)
651 marker = dot;
652 else
653 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000654 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200655 return NULL;
656 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200657 }
658
659 l = list_alloc();
660 if (l == NULL)
661 return NULL;
662
663 for (;;)
664 {
665 int mi = 0;
666 int ti = 0;
667
668 theline = eap->getline(NUL, eap->cookie, 0, FALSE);
669 if (theline == NULL)
670 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000671 semsg(_(e_missing_end_marker_str), marker);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200672 break;
673 }
674
675 // with "trim": skip the indent matching the :let line to find the
676 // marker
677 if (marker_indent_len > 0
678 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
679 mi = marker_indent_len;
680 if (STRCMP(marker, theline + mi) == 0)
681 {
682 vim_free(theline);
683 break;
684 }
685
686 if (text_indent_len == -1 && *theline != NUL)
687 {
688 // set the text indent from the first line.
689 p = theline;
690 text_indent_len = 0;
691 while (VIM_ISWHITE(*p))
692 {
693 p++;
694 text_indent_len++;
695 }
696 text_indent = vim_strnsave(theline, text_indent_len);
697 }
698 // with "trim": skip the indent matching the first line
699 if (text_indent != NULL)
700 for (ti = 0; ti < text_indent_len; ++ti)
701 if (theline[ti] != text_indent[ti])
702 break;
703
704 if (list_append_string(l, theline + ti, -1) == FAIL)
705 break;
706 vim_free(theline);
707 }
708 vim_free(text_indent);
709
710 return l;
711}
712
713/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200714 * Vim9 variable declaration:
715 * ":var name"
716 * ":var name: type"
717 * ":var name = expr"
718 * ":var name: type = expr"
719 * etc.
720 */
721 void
722ex_var(exarg_T *eap)
723{
724 if (!in_vim9script())
725 {
726 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
727 return;
728 }
729 ex_let(eap);
730}
731
732/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200733 * ":let" list all variable values
734 * ":let var1 var2" list variable values
735 * ":let var = expr" assignment command.
736 * ":let var += expr" assignment command.
737 * ":let var -= expr" assignment command.
738 * ":let var *= expr" assignment command.
739 * ":let var /= expr" assignment command.
740 * ":let var %= expr" assignment command.
741 * ":let var .= expr" assignment command.
742 * ":let var ..= expr" assignment command.
743 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +0100745 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200746 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200747 * ":final var = expr" assignment command.
748 * ":final [var1, var2] = expr" unpack list.
749 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200750 * ":const" list all variable values
751 * ":const var1 var2" list variable values
752 * ":const var = expr" assignment command.
753 * ":const [var1, var2] = expr" unpack list.
754 */
755 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +0200756ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200757{
758 char_u *arg = eap->arg;
759 char_u *expr = NULL;
760 typval_T rettv;
761 int i;
762 int var_count = 0;
763 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200764 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200765 char_u *argend;
766 int first = TRUE;
767 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200768 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100769 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200770 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200771
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200772 if (eap->cmdidx == CMD_final && !vim9script)
773 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100774 // In legacy Vim script ":final" is short for ":finally".
775 ex_finally(eap);
776 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200777 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +0200778 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +0200779 {
780 emsg(_(e_cannot_use_let_in_vim9_script));
781 return;
782 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200783
Bram Moolenaar89b474d2020-12-22 21:19:39 +0100784 if (eap->cmdidx == CMD_const)
785 flags |= ASSIGN_CONST;
786 else if (eap->cmdidx == CMD_final)
787 flags |= ASSIGN_FINAL;
788
789 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200791 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792
Bram Moolenaar47a519a2020-06-14 23:05:10 +0200793 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 if (argend == NULL)
795 return;
796 if (argend > arg && argend[-1] == '.') // for var.='str'
797 --argend;
798 expr = skipwhite(argend);
799 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +0200800 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200801 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +0200802 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
803 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +0200804 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200805 {
806 // ":let" without "=": list variables
807 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000808 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200809 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000810 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +0200811 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200812 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200813 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200814 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100815 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +0000816 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +0100817 else
818 // Vim9 declaration ":var name: type"
819 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +0200820 }
821 else
822 {
823 // ":let var1 var2" - list values
824 arg = list_arg_vars(eap, arg, &first);
825 }
826 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200827 else if (!eap->skip)
828 {
829 // ":let"
830 list_glob_vars(&first);
831 list_buf_vars(&first);
832 list_win_vars(&first);
833 list_tab_vars(&first);
834 list_script_vars(&first);
835 list_func_vars(&first);
836 list_vim_vars(&first);
837 }
Bram Moolenaar63b91732021-08-05 20:40:03 +0200838 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200839 }
840 else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
841 {
842 list_T *l;
Bram Moolenaar81530e32021-07-28 21:25:49 +0200843 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200844
845 // HERE document
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200846 l = heredoc_get(eap, expr + 3, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200847 if (l != NULL)
848 {
849 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200850 if (!eap->skip)
851 {
Bram Moolenaar81530e32021-07-28 21:25:49 +0200852 // errors are for the assignment, not the end marker
853 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200854 op[0] = '=';
855 op[1] = NUL;
856 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +0200858 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200859 clear_tv(&rettv);
860 }
861 }
862 else
863 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200864 evalarg_T evalarg;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200865 int len = 1;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200866
Bram Moolenaarc1ec0422020-09-09 22:27:58 +0200867 CLEAR_FIELD(rettv);
Bram Moolenaar32e35112020-05-14 22:41:15 +0200868 i = FAIL;
869 if (has_assign || concat)
870 {
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100871 int cur_lnum;
872
Bram Moolenaar32e35112020-05-14 22:41:15 +0200873 op[0] = '=';
874 op[1] = NUL;
875 if (*expr != '=')
876 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200877 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar122616d2020-08-21 21:32:50 +0200878 {
879 // +=, /=, etc. require an existing variable
880 semsg(_(e_cannot_use_operator_on_new_variable), eap->arg);
881 i = FAIL;
882 }
883 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
Bram Moolenaar32e35112020-05-14 22:41:15 +0200884 {
885 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200886 ++len;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200887 if (expr[0] == '.' && expr[1] == '.') // ..=
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200888 {
Bram Moolenaar32e35112020-05-14 22:41:15 +0200889 ++expr;
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200890 ++len;
891 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200892 }
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200893 expr += 2;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200894 }
895 else
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200896 ++expr;
897
Bram Moolenaar7f2c3412021-11-29 16:01:49 +0000898 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200899 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200900 {
901 vim_strncpy(op, expr - len, len);
Bram Moolenaare7a73e02021-01-01 19:17:55 +0100902 semsg(_(e_white_space_required_before_and_after_str_at_str),
903 op, argend);
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200904 i = FAIL;
905 }
Bram Moolenaar32e35112020-05-14 22:41:15 +0200906
907 if (eap->skip)
908 ++emsg_skip;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200909 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaarc7e44a72020-07-29 21:37:43 +0200910 expr = skipwhite_and_linebreak(expr, &evalarg);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100911 cur_lnum = SOURCING_LNUM;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200912 i = eval0(expr, &rettv, eap, &evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200913 if (eap->skip)
914 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200915 clear_evalarg(&evalarg, eap);
Bram Moolenaar9a562c12021-01-23 13:39:14 +0100916
917 // Restore the line number so that any type error is given for the
918 // declaration, not the expression.
919 SOURCING_LNUM = cur_lnum;
Bram Moolenaar32e35112020-05-14 22:41:15 +0200920 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200921 if (eap->skip)
922 {
923 if (i != FAIL)
924 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200925 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200926 else if (i != FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200927 {
928 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar63be3d42020-07-23 13:11:37 +0200929 flags, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200930 clear_tv(&rettv);
931 }
932 }
933}
934
935/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +0100936 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200937 * Handles both "var" with any type and "[var, var; var]" with a list type.
938 * When "op" is not NULL it points to a string with characters that
939 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
940 * or concatenate.
941 * Returns OK or FAIL;
942 */
943 int
944ex_let_vars(
945 char_u *arg_start,
946 typval_T *tv,
947 int copy, // copy values from "tv", don't move
948 int semicolon, // from skip_var_list()
949 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100950 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200951 char_u *op)
952{
953 char_u *arg = arg_start;
954 list_T *l;
955 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100956 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200957 listitem_T *item;
958 typval_T ltv;
959
960 if (*arg != '[')
961 {
962 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100963 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200964 return FAIL;
965 return OK;
966 }
967
968 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
969 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
970 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +0000971 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200972 return FAIL;
973 }
974
975 i = list_len(l);
976 if (semicolon == 0 && var_count < i)
977 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000978 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200979 return FAIL;
980 }
981 if (var_count - semicolon > i)
982 {
Bram Moolenaara6f79292022-01-04 21:30:47 +0000983 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200984 return FAIL;
985 }
986
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200987 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200988 item = l->lv_first;
989 while (*arg != ']')
990 {
991 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100992 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +0200993 arg = ex_let_one(arg, &item->li_tv, TRUE,
994 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200995 item = item->li_next;
996 if (arg == NULL)
997 return FAIL;
998
999 arg = skipwhite(arg);
1000 if (*arg == ';')
1001 {
1002 // Put the rest of the list (may be empty) in the var after ';'.
1003 // Create a new list for this.
1004 l = list_alloc();
1005 if (l == NULL)
1006 return FAIL;
1007 while (item != NULL)
1008 {
1009 list_append_tv(l, &item->li_tv);
1010 item = item->li_next;
1011 }
1012
1013 ltv.v_type = VAR_LIST;
1014 ltv.v_lock = 0;
1015 ltv.vval.v_list = l;
1016 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001017 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001018
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001019 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1020 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001021 clear_tv(&ltv);
1022 if (arg == NULL)
1023 return FAIL;
1024 break;
1025 }
1026 else if (*arg != ',' && *arg != ']')
1027 {
1028 internal_error("ex_let_vars()");
1029 return FAIL;
1030 }
1031 }
1032
1033 return OK;
1034}
1035
1036/*
1037 * Skip over assignable variable "var" or list of variables "[var, var]".
1038 * Used for ":let varvar = expr" and ":for varvar in expr".
1039 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001040 * for "[var, var; var]" set "semicolon" to 1.
1041 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042 * Return NULL for an error.
1043 */
1044 char_u *
1045skip_var_list(
1046 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001048 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001049 int *semicolon,
1050 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001051{
1052 char_u *p, *s;
1053
1054 if (*arg == '[')
1055 {
1056 // "[var, var]": find the matching ']'.
1057 p = arg;
1058 for (;;)
1059 {
1060 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001061 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001062 if (s == p)
1063 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001064 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001065 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001066 return NULL;
1067 }
1068 ++*var_count;
1069
1070 p = skipwhite(s);
1071 if (*p == ']')
1072 break;
1073 else if (*p == ';')
1074 {
1075 if (*semicolon == 1)
1076 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001077 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001078 return NULL;
1079 }
1080 *semicolon = 1;
1081 }
1082 else if (*p != ',')
1083 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001084 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001085 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001086 return NULL;
1087 }
1088 }
1089 return p + 1;
1090 }
1091 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001093}
1094
1095/*
1096 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1097 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001099 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001100 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001102{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001103 char_u *end;
1104 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001106 if (*arg == '@' && arg[1] != NUL)
1107 return arg + 2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001109 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001110
1111 // "a: type" is declaring variable "a" with a type, not "a:".
1112 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001113 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001114 --end;
1115
Bram Moolenaar585587d2021-01-17 20:52:13 +01001116 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001118 if (*end == ':')
Bram Moolenaar4fc224c2020-07-26 17:56:25 +02001119 end = skip_type(skipwhite(end + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 }
1121 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001122}
1123
1124/*
1125 * List variables for hashtab "ht" with prefix "prefix".
1126 * If "empty" is TRUE also list NULL strings as empty strings.
1127 */
1128 void
1129list_hashtable_vars(
1130 hashtab_T *ht,
1131 char *prefix,
1132 int empty,
1133 int *first)
1134{
1135 hashitem_T *hi;
1136 dictitem_T *di;
1137 int todo;
1138 char_u buf[IOSIZE];
1139
1140 todo = (int)ht->ht_used;
1141 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1142 {
1143 if (!HASHITEM_EMPTY(hi))
1144 {
1145 --todo;
1146 di = HI2DI(hi);
1147
1148 // apply :filter /pat/ to variable name
1149 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1150 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1151 if (message_filtered(buf))
1152 continue;
1153
1154 if (empty || di->di_tv.v_type != VAR_STRING
1155 || di->di_tv.vval.v_string != NULL)
1156 list_one_var(di, prefix, first);
1157 }
1158 }
1159}
1160
1161/*
1162 * List global variables.
1163 */
1164 static void
1165list_glob_vars(int *first)
1166{
1167 list_hashtable_vars(&globvarht, "", TRUE, first);
1168}
1169
1170/*
1171 * List buffer variables.
1172 */
1173 static void
1174list_buf_vars(int *first)
1175{
1176 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1177}
1178
1179/*
1180 * List window variables.
1181 */
1182 static void
1183list_win_vars(int *first)
1184{
1185 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1186}
1187
1188/*
1189 * List tab page variables.
1190 */
1191 static void
1192list_tab_vars(int *first)
1193{
1194 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1195}
1196
1197/*
1198 * List variables in "arg".
1199 */
1200 static char_u *
1201list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1202{
1203 int error = FALSE;
1204 int len;
1205 char_u *name;
1206 char_u *name_start;
1207 char_u *arg_subsc;
1208 char_u *tofree;
1209 typval_T tv;
1210
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001211 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001212 {
1213 if (error || eap->skip)
1214 {
1215 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1216 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1217 {
1218 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001219 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001220 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001221 break;
1222 }
1223 }
1224 else
1225 {
1226 // get_name_len() takes care of expanding curly braces
1227 name_start = name = arg;
1228 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1229 if (len <= 0)
1230 {
1231 // This is mainly to keep test 49 working: when expanding
1232 // curly braces fails overrule the exception error message.
1233 if (len < 0 && !aborting())
1234 {
1235 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001236 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001237 break;
1238 }
1239 error = TRUE;
1240 }
1241 else
1242 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001243 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001244 if (tofree != NULL)
1245 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001246 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001247 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001248 error = TRUE;
1249 else
1250 {
1251 // handle d.key, l[idx], f(expr)
1252 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001253 if (handle_subscript(&arg, name_start, &tv,
1254 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001255 error = TRUE;
1256 else
1257 {
1258 if (arg == arg_subsc && len == 2 && name[1] == ':')
1259 {
1260 switch (*name)
1261 {
1262 case 'g': list_glob_vars(first); break;
1263 case 'b': list_buf_vars(first); break;
1264 case 'w': list_win_vars(first); break;
1265 case 't': list_tab_vars(first); break;
1266 case 'v': list_vim_vars(first); break;
1267 case 's': list_script_vars(first); break;
1268 case 'l': list_func_vars(first); break;
1269 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001270 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 }
1272 }
1273 else
1274 {
1275 char_u numbuf[NUMBUFLEN];
1276 char_u *tf;
1277 int c;
1278 char_u *s;
1279
1280 s = echo_string(&tv, &tf, numbuf, 0);
1281 c = *arg;
1282 *arg = NUL;
1283 list_one_var_a("",
1284 arg == arg_subsc ? name : name_start,
1285 tv.v_type,
1286 s == NULL ? (char_u *)"" : s,
1287 first);
1288 *arg = c;
1289 vim_free(tf);
1290 }
1291 clear_tv(&tv);
1292 }
1293 }
1294 }
1295
1296 vim_free(tofree);
1297 }
1298
1299 arg = skipwhite(arg);
1300 }
1301
1302 return arg;
1303}
1304
1305/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001306 * Set an environment variable, part of ex_let_one().
1307 */
1308 static char_u *
1309ex_let_env(
1310 char_u *arg,
1311 typval_T *tv,
1312 int flags,
1313 char_u *endchars,
1314 char_u *op)
1315{
1316 char_u *arg_end = NULL;
1317 char_u *name;
1318 int len;
1319
1320 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1321 && (flags & ASSIGN_FOR_LOOP) == 0)
1322 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001323 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001324 return NULL;
1325 }
1326
1327 // Find the end of the name.
1328 ++arg;
1329 name = arg;
1330 len = get_env_len(&arg);
1331 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001332 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001333 else
1334 {
1335 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001336 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001337 else if (endchars != NULL
1338 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1339 emsg(_(e_unexpected_characters_in_let));
1340 else if (!check_secure())
1341 {
1342 char_u *tofree = NULL;
1343 int c1 = name[len];
1344 char_u *p;
1345
1346 name[len] = NUL;
1347 p = tv_get_string_chk(tv);
1348 if (p != NULL && op != NULL && *op == '.')
1349 {
1350 int mustfree = FALSE;
1351 char_u *s = vim_getenv(name, &mustfree);
1352
1353 if (s != NULL)
1354 {
1355 p = tofree = concat_str(s, p);
1356 if (mustfree)
1357 vim_free(s);
1358 }
1359 }
1360 if (p != NULL)
1361 {
1362 vim_setenv_ext(name, p);
1363 arg_end = arg;
1364 }
1365 name[len] = c1;
1366 vim_free(tofree);
1367 }
1368 }
1369 return arg_end;
1370}
1371
1372/*
1373 * Set an option, part of ex_let_one().
1374 */
1375 static char_u *
1376ex_let_option(
1377 char_u *arg,
1378 typval_T *tv,
1379 int flags,
1380 char_u *endchars,
1381 char_u *op)
1382{
1383 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001384 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001385 char_u *arg_end = NULL;
1386
1387 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1388 && (flags & ASSIGN_FOR_LOOP) == 0)
1389 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001390 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001391 return NULL;
1392 }
1393
1394 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001395 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001396 if (p == NULL || (endchars != NULL
1397 && vim_strchr(endchars, *skipwhite(p)) == NULL))
1398 emsg(_(e_unexpected_characters_in_let));
1399 else
1400 {
1401 int c1;
1402 long n = 0;
1403 getoption_T opt_type;
1404 long numval;
1405 char_u *stringval = NULL;
1406 char_u *s = NULL;
1407 int failed = FALSE;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001408 int opt_p_flags;
1409 char_u *tofree = NULL;
Bram Moolenaar92c33eb2021-12-07 11:03:39 +00001410 char_u numbuf[NUMBUFLEN];
1411
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001412 c1 = *p;
1413 *p = NUL;
1414
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001415 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags,
1416 scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001417 if ((opt_type == gov_bool
1418 || opt_type == gov_number
1419 || opt_type == gov_hidden_bool
1420 || opt_type == gov_hidden_number)
1421 && (tv->v_type != VAR_STRING || !in_vim9script()))
1422 {
1423 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1424 // bool, possibly hidden
1425 n = (long)tv_get_bool(tv);
1426 else
1427 // number, possibly hidden
1428 n = (long)tv_get_number(tv);
1429 }
1430
Bram Moolenaaref082e12021-12-12 21:02:03 +00001431 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001432 || tv->v_type == VAR_FUNC))
1433 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001434 // If the option can be set to a function reference or a lambda
1435 // and the passed value is a function reference, then convert it to
1436 // the name (string) of the function reference.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001437 s = tv2string(tv, &tofree, numbuf, 0);
1438 }
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001439 // Avoid setting a string option to the text "v:false" or similar.
1440 // In Vim9 script also don't convert a number to string.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001441 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001442 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1443 s = tv_get_string_chk(tv);
1444
1445 if (op != NULL && *op != '=')
1446 {
1447 if (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1448 || (opt_type == gov_string && *op != '.'))
1449 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001450 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001451 failed = TRUE; // don't set the value
1452
1453 }
1454 else
1455 {
1456 // number, in legacy script also bool
1457 if (opt_type == gov_number
1458 || (opt_type == gov_bool && !in_vim9script()))
1459 {
1460 switch (*op)
1461 {
1462 case '+': n = numval + n; break;
1463 case '-': n = numval - n; break;
1464 case '*': n = numval * n; break;
1465 case '/': n = (long)num_divide(numval, n,
1466 &failed); break;
1467 case '%': n = (long)num_modulus(numval, n,
1468 &failed); break;
1469 }
1470 s = NULL;
1471 }
1472 else if (opt_type == gov_string
1473 && stringval != NULL && s != NULL)
1474 {
1475 // string
1476 s = concat_str(stringval, s);
1477 vim_free(stringval);
1478 stringval = s;
1479 }
1480 }
1481 }
1482
1483 if (!failed)
1484 {
1485 if (opt_type != gov_string || s != NULL)
1486 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001487 set_option_value(arg, n, s, scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001488 arg_end = p;
1489 }
1490 else
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001491 emsg(_(e_string_required));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001492 }
1493 *p = c1;
1494 vim_free(stringval);
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001495 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001496 }
1497 return arg_end;
1498}
1499
1500/*
1501 * Set a register, part of ex_let_one().
1502 */
1503 static char_u *
1504ex_let_register(
1505 char_u *arg,
1506 typval_T *tv,
1507 int flags,
1508 char_u *endchars,
1509 char_u *op)
1510{
1511 char_u *arg_end = NULL;
1512
1513 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1514 && (flags & ASSIGN_FOR_LOOP) == 0)
1515 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001516 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001517 return NULL;
1518 }
1519 ++arg;
1520 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001521 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001522 else if (endchars != NULL
1523 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1524 emsg(_(e_unexpected_characters_in_let));
1525 else
1526 {
1527 char_u *ptofree = NULL;
1528 char_u *p;
1529
1530 p = tv_get_string_chk(tv);
1531 if (p != NULL && op != NULL && *op == '.')
1532 {
1533 char_u *s = get_reg_contents(*arg == '@'
1534 ? '"' : *arg, GREG_EXPR_SRC);
1535
1536 if (s != NULL)
1537 {
1538 p = ptofree = concat_str(s, p);
1539 vim_free(s);
1540 }
1541 }
1542 if (p != NULL)
1543 {
1544 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1545 arg_end = arg + 1;
1546 }
1547 vim_free(ptofree);
1548 }
1549 return arg_end;
1550}
1551
1552/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1554 * Returns a pointer to the char just after the var name.
1555 * Returns NULL if there is an error.
1556 */
1557 static char_u *
1558ex_let_one(
1559 char_u *arg, // points to variable name
1560 typval_T *tv, // value to assign to variable
1561 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001562 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001563 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001564 char_u *op, // "+", "-", "." or NULL
1565 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001566{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001567 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001568
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001569 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001570 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001571 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1572 {
1573 vim9_declare_error(arg);
1574 return NULL;
1575 }
1576
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001577 if (*arg == '$')
1578 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001579 // ":let $VAR = expr": Set environment variable.
1580 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001581 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001582 else if (*arg == '&')
1583 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001584 // ":let &option = expr": Set option value.
1585 // ":let &l:option = expr": Set local option value.
1586 // ":let &g:option = expr": Set global option value.
1587 // ":for &ts in range(8)": Set option value for for loop
1588 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001589 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001590 else if (*arg == '@')
1591 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001592 // ":let @r = expr": Set register contents.
1593 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001594 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001595 else if (eval_isnamec1(*arg) || *arg == '{')
1596 {
1597 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001598 char_u *p;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001599
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001600 // ":let var = expr": Set internal variable.
1601 // ":let var: type = expr": Set internal variable with type.
1602 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001603 p = get_lval(arg, tv, &lv, FALSE, FALSE,
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001604 (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1605 ? GLV_NO_DECL : 0, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001606 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001607 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 if (endchars != NULL && vim_strchr(endchars,
1609 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001610 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001611 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001612 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001613 else
1614 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001615 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001616 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001617 }
1618 }
1619 clear_lval(&lv);
1620 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001621 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001622 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001623
1624 return arg_end;
1625}
1626
1627/*
1628 * ":unlet[!] var1 ... " command.
1629 */
1630 void
1631ex_unlet(exarg_T *eap)
1632{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001633 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001634}
1635
1636/*
1637 * ":lockvar" and ":unlockvar" commands
1638 */
1639 void
1640ex_lockvar(exarg_T *eap)
1641{
1642 char_u *arg = eap->arg;
1643 int deep = 2;
1644
1645 if (eap->forceit)
1646 deep = -1;
1647 else if (vim_isdigit(*arg))
1648 {
1649 deep = getdigits(&arg);
1650 arg = skipwhite(arg);
1651 }
1652
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001653 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001654}
1655
1656/*
1657 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001658 * Also used for Vim9 script. "callback" is invoked as:
1659 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001660 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001661 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001662ex_unletlock(
1663 exarg_T *eap,
1664 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001665 int deep,
1666 int glv_flags,
1667 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1668 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001669{
1670 char_u *arg = argstart;
1671 char_u *name_end;
1672 int error = FALSE;
1673 lval_T lv;
1674
1675 do
1676 {
1677 if (*arg == '$')
1678 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001679 lv.ll_name = arg;
1680 lv.ll_tv = NULL;
1681 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001682 if (get_env_len(&arg) == 0)
1683 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001684 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001685 return;
1686 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001687 if (!error && !eap->skip
1688 && callback(&lv, arg, eap, deep, cookie) == FAIL)
1689 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001690 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001691 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001692 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001693 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001694 // Parse the name and find the end.
1695 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01001696 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001697 if (lv.ll_name == NULL)
1698 error = TRUE; // error but continue parsing
1699 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
1700 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001701 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001702 if (name_end != NULL)
1703 {
1704 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00001705 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001706 }
1707 if (!(eap->skip || error))
1708 clear_lval(&lv);
1709 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001710 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001711
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001712 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001713 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001714 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001715
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02001716 if (!eap->skip)
1717 clear_lval(&lv);
1718 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001719
1720 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001721 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001722
Bram Moolenaar63b91732021-08-05 20:40:03 +02001723 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001724}
1725
1726 static int
1727do_unlet_var(
1728 lval_T *lp,
1729 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001730 exarg_T *eap,
1731 int deep UNUSED,
1732 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001733{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001734 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001735 int ret = OK;
1736 int cc;
1737
1738 if (lp->ll_tv == NULL)
1739 {
1740 cc = *name_end;
1741 *name_end = NUL;
1742
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001743 // Environment variable, normal name or expanded name.
1744 if (*lp->ll_name == '$')
1745 vim_unsetenv(lp->ll_name + 1);
1746 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001747 ret = FAIL;
1748 *name_end = cc;
1749 }
1750 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001751 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001752 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001753 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001754 return FAIL;
1755 else if (lp->ll_range)
1756 {
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001757 if (list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_name, lp->ll_n1,
1758 !lp->ll_empty2, lp->ll_n2) == FAIL)
1759 return FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001760 }
1761 else
1762 {
1763 if (lp->ll_list != NULL)
1764 // unlet a List item.
1765 listitem_remove(lp->ll_list, lp->ll_li);
1766 else
1767 // unlet a Dictionary item.
1768 dictitem_remove(lp->ll_dict, lp->ll_di);
1769 }
1770
1771 return ret;
1772}
1773
1774/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001775 * Unlet one item or a range of items from a list.
1776 * Return OK or FAIL.
1777 */
1778 int
1779list_unlet_range(
1780 list_T *l,
1781 listitem_T *li_first,
1782 char_u *name,
1783 long n1_arg,
1784 int has_n2,
1785 long n2)
1786{
1787 listitem_T *li = li_first;
1788 int n1 = n1_arg;
1789
1790 while (li != NULL && (!has_n2 || n2 >= n1))
1791 {
1792 if (value_check_lock(li->li_tv.v_lock, name, FALSE))
1793 return FAIL;
1794 li = li->li_next;
1795 ++n1;
1796 }
1797
1798 // Delete a range of List items.
1799 li = li_first;
1800 n1 = n1_arg;
1801 while (li != NULL && (!has_n2 || n2 >= n1))
1802 {
1803 listitem_T *next = li->li_next;
1804
1805 listitem_remove(l, li);
1806 li = next;
1807 ++n1;
1808 }
1809 return OK;
1810}
1811/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001812 * "unlet" a variable. Return OK if it existed, FAIL if not.
1813 * When "forceit" is TRUE don't complain if the variable doesn't exist.
1814 */
1815 int
1816do_unlet(char_u *name, int forceit)
1817{
1818 hashtab_T *ht;
1819 hashitem_T *hi;
1820 char_u *varname;
1821 dict_T *d;
1822 dictitem_T *di;
1823
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001824 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02001825 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001826 return FAIL;
1827
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001828 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01001829
1830 // can't :unlet a script variable in Vim9 script from a function
1831 if (ht == get_script_local_ht()
1832 && SCRIPT_ID_VALID(current_sctx.sc_sid)
1833 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
1834 == SCRIPT_VERSION_VIM9
1835 && check_vim9_unlet(name) == FAIL)
1836 return FAIL;
1837
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001838 if (ht != NULL && *varname != NUL)
1839 {
1840 d = get_current_funccal_dict(ht);
1841 if (d == NULL)
1842 {
1843 if (ht == &globvarht)
1844 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02001845 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001846 d = &vimvardict;
1847 else
1848 {
1849 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
1850 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
1851 }
1852 if (d == NULL)
1853 {
1854 internal_error("do_unlet()");
1855 return FAIL;
1856 }
1857 }
1858 hi = hash_find(ht, varname);
1859 if (HASHITEM_EMPTY(hi))
1860 hi = find_hi_in_scoped_ht(name, &ht);
1861 if (hi != NULL && !HASHITEM_EMPTY(hi))
1862 {
1863 di = HI2DI(hi);
1864 if (var_check_fixed(di->di_flags, name, FALSE)
1865 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaara187c432020-09-16 21:08:28 +02001866 || value_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001867 return FAIL;
1868
1869 delete_var(ht, hi);
1870 return OK;
1871 }
1872 }
1873 if (forceit)
1874 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00001875 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876 return FAIL;
1877}
1878
1879/*
1880 * Lock or unlock variable indicated by "lp".
1881 * "deep" is the levels to go (-1 for unlimited);
1882 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
1883 */
1884 static int
1885do_lock_var(
1886 lval_T *lp,
1887 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001888 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001889 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001890 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001891{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001892 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 int ret = OK;
1894 int cc;
1895 dictitem_T *di;
1896
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001897 if (lp->ll_tv == NULL)
1898 {
1899 cc = *name_end;
1900 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001901 if (*lp->ll_name == '$')
1902 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001903 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001904 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001905 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001906 else
1907 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001908 // Normal name or expanded name.
1909 di = find_var(lp->ll_name, NULL, TRUE);
1910 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001911 {
1912 if (in_vim9script())
1913 semsg(_(e_cannot_find_variable_to_unlock_str),
1914 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001915 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00001916 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001917 else if ((di->di_flags & DI_FLAGS_FIX)
1918 && di->di_tv.v_type != VAR_DICT
1919 && di->di_tv.v_type != VAR_LIST)
1920 {
1921 // For historic reasons this error is not given for a list or
1922 // dict. E.g., the b: dict could be locked/unlocked.
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001923 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001924 ret = FAIL;
1925 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001926 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001927 {
1928 if (lock)
1929 di->di_flags |= DI_FLAGS_LOCK;
1930 else
1931 di->di_flags &= ~DI_FLAGS_LOCK;
Bram Moolenaara187c432020-09-16 21:08:28 +02001932 if (deep != 0)
1933 item_lock(&di->di_tv, deep, lock, FALSE);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001934 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001935 }
1936 *name_end = cc;
1937 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001938 else if (deep == 0)
1939 {
1940 // nothing to do
1941 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001942 else if (lp->ll_range)
1943 {
1944 listitem_T *li = lp->ll_li;
1945
1946 // (un)lock a range of List items.
1947 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
1948 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02001949 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001950 li = li->li_next;
1951 ++lp->ll_n1;
1952 }
1953 }
1954 else if (lp->ll_list != NULL)
1955 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001956 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001957 else
1958 // (un)lock a Dictionary item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001959 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001960
1961 return ret;
1962}
1963
1964/*
1965 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02001966 * When "check_refcount" is TRUE do not lock a list or dict with a reference
1967 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001968 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001969 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02001970item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001971{
1972 static int recurse = 0;
1973 list_T *l;
1974 listitem_T *li;
1975 dict_T *d;
1976 blob_T *b;
1977 hashitem_T *hi;
1978 int todo;
1979
1980 if (recurse >= DICT_MAXNEST)
1981 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00001982 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001983 return;
1984 }
1985 if (deep == 0)
1986 return;
1987 ++recurse;
1988
1989 // lock/unlock the item itself
1990 if (lock)
1991 tv->v_lock |= VAR_LOCKED;
1992 else
1993 tv->v_lock &= ~VAR_LOCKED;
1994
1995 switch (tv->v_type)
1996 {
1997 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001998 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002000 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002002 case VAR_STRING:
2003 case VAR_FUNC:
2004 case VAR_PARTIAL:
2005 case VAR_FLOAT:
2006 case VAR_SPECIAL:
2007 case VAR_JOB:
2008 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002009 case VAR_INSTR:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002010 break;
2011
2012 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002013 if ((b = tv->vval.v_blob) != NULL
2014 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002015 {
2016 if (lock)
2017 b->bv_lock |= VAR_LOCKED;
2018 else
2019 b->bv_lock &= ~VAR_LOCKED;
2020 }
2021 break;
2022 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002023 if ((l = tv->vval.v_list) != NULL
2024 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002025 {
2026 if (lock)
2027 l->lv_lock |= VAR_LOCKED;
2028 else
2029 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar50985eb2020-01-27 22:09:39 +01002030 if ((deep < 0 || deep > 1) && l->lv_first != &range_list_item)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002031 // recursive: lock/unlock the items the List contains
Bram Moolenaaraeea7212020-04-02 18:50:46 +02002032 FOR_ALL_LIST_ITEMS(l, li)
Bram Moolenaar021bda52020-08-17 21:07:22 +02002033 item_lock(&li->li_tv, deep - 1, lock, check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002034 }
2035 break;
2036 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002037 if ((d = tv->vval.v_dict) != NULL
2038 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002039 {
2040 if (lock)
2041 d->dv_lock |= VAR_LOCKED;
2042 else
2043 d->dv_lock &= ~VAR_LOCKED;
2044 if (deep < 0 || deep > 1)
2045 {
2046 // recursive: lock/unlock the items the List contains
2047 todo = (int)d->dv_hashtab.ht_used;
2048 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
2049 {
2050 if (!HASHITEM_EMPTY(hi))
2051 {
2052 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002053 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2054 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002055 }
2056 }
2057 }
2058 }
2059 }
2060 --recurse;
2061}
2062
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002063#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2064/*
2065 * Delete all "menutrans_" variables.
2066 */
2067 void
2068del_menutrans_vars(void)
2069{
2070 hashitem_T *hi;
2071 int todo;
2072
2073 hash_lock(&globvarht);
2074 todo = (int)globvarht.ht_used;
2075 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2076 {
2077 if (!HASHITEM_EMPTY(hi))
2078 {
2079 --todo;
2080 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2081 delete_var(&globvarht, hi);
2082 }
2083 }
2084 hash_unlock(&globvarht);
2085}
2086#endif
2087
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002088/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002089 * Local string buffer for the next two functions to store a variable name
2090 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2091 * get_user_var_name().
2092 */
2093
2094static char_u *varnamebuf = NULL;
2095static int varnamebuflen = 0;
2096
2097/*
2098 * Function to concatenate a prefix and a variable name.
2099 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002100 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002101cat_prefix_varname(int prefix, char_u *name)
2102{
2103 int len;
2104
2105 len = (int)STRLEN(name) + 3;
2106 if (len > varnamebuflen)
2107 {
2108 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002109 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002110 varnamebuf = alloc(len);
2111 if (varnamebuf == NULL)
2112 {
2113 varnamebuflen = 0;
2114 return NULL;
2115 }
2116 varnamebuflen = len;
2117 }
2118 *varnamebuf = prefix;
2119 varnamebuf[1] = ':';
2120 STRCPY(varnamebuf + 2, name);
2121 return varnamebuf;
2122}
2123
2124/*
2125 * Function given to ExpandGeneric() to obtain the list of user defined
2126 * (global/buffer/window/built-in) variable names.
2127 */
2128 char_u *
2129get_user_var_name(expand_T *xp, int idx)
2130{
2131 static long_u gdone;
2132 static long_u bdone;
2133 static long_u wdone;
2134 static long_u tdone;
2135 static int vidx;
2136 static hashitem_T *hi;
2137 hashtab_T *ht;
2138
2139 if (idx == 0)
2140 {
2141 gdone = bdone = wdone = vidx = 0;
2142 tdone = 0;
2143 }
2144
2145 // Global variables
2146 if (gdone < globvarht.ht_used)
2147 {
2148 if (gdone++ == 0)
2149 hi = globvarht.ht_array;
2150 else
2151 ++hi;
2152 while (HASHITEM_EMPTY(hi))
2153 ++hi;
2154 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2155 return cat_prefix_varname('g', hi->hi_key);
2156 return hi->hi_key;
2157 }
2158
2159 // b: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002160 ht =
2161#ifdef FEAT_CMDWIN
2162 // In cmdwin, the alternative buffer should be used.
mityua1198122021-11-20 19:13:39 +00002163 is_in_cmdwin() ? &prevwin->w_buffer->b_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002164#endif
2165 &curbuf->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002166 if (bdone < ht->ht_used)
2167 {
2168 if (bdone++ == 0)
2169 hi = ht->ht_array;
2170 else
2171 ++hi;
2172 while (HASHITEM_EMPTY(hi))
2173 ++hi;
2174 return cat_prefix_varname('b', hi->hi_key);
2175 }
2176
2177 // w: variables
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002178 ht =
2179#ifdef FEAT_CMDWIN
2180 // In cmdwin, the alternative window should be used.
mityua1198122021-11-20 19:13:39 +00002181 is_in_cmdwin() ? &prevwin->w_vars->dv_hashtab :
Bram Moolenaar4ff2f2f2020-10-25 13:22:42 +01002182#endif
2183 &curwin->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002184 if (wdone < ht->ht_used)
2185 {
2186 if (wdone++ == 0)
2187 hi = ht->ht_array;
2188 else
2189 ++hi;
2190 while (HASHITEM_EMPTY(hi))
2191 ++hi;
2192 return cat_prefix_varname('w', hi->hi_key);
2193 }
2194
2195 // t: variables
2196 ht = &curtab->tp_vars->dv_hashtab;
2197 if (tdone < ht->ht_used)
2198 {
2199 if (tdone++ == 0)
2200 hi = ht->ht_array;
2201 else
2202 ++hi;
2203 while (HASHITEM_EMPTY(hi))
2204 ++hi;
2205 return cat_prefix_varname('t', hi->hi_key);
2206 }
2207
2208 // v: variables
2209 if (vidx < VV_LEN)
2210 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2211
2212 VIM_CLEAR(varnamebuf);
2213 varnamebuflen = 0;
2214 return NULL;
2215}
2216
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002217 char *
2218get_var_special_name(int nr)
2219{
2220 switch (nr)
2221 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002222 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2223 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002224 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002225 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002226 }
2227 internal_error("get_var_special_name()");
2228 return "42";
2229}
2230
2231/*
2232 * Returns the global variable dictionary
2233 */
2234 dict_T *
2235get_globvar_dict(void)
2236{
2237 return &globvardict;
2238}
2239
2240/*
2241 * Returns the global variable hash table
2242 */
2243 hashtab_T *
2244get_globvar_ht(void)
2245{
2246 return &globvarht;
2247}
2248
2249/*
2250 * Returns the v: variable dictionary
2251 */
2252 dict_T *
2253get_vimvar_dict(void)
2254{
2255 return &vimvardict;
2256}
2257
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002258/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002260 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 */
2262 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002263find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002265 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2266 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267
2268 if (di == NULL)
2269 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002270 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2272 return (int)(vv - vimvars);
2273}
2274
2275
2276/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002277 * Set type of v: variable to "type".
2278 */
2279 void
2280set_vim_var_type(int idx, vartype_T type)
2281{
Bram Moolenaard787e402021-12-24 21:36:12 +00002282 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002283}
2284
2285/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002286 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002287 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002288 */
2289 void
2290set_vim_var_nr(int idx, varnumber_T val)
2291{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002292 vimvars[idx].vv_nr = val;
2293}
2294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 char *
2296get_vim_var_name(int idx)
2297{
2298 return vimvars[idx].vv_name;
2299}
2300
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002301/*
2302 * Get typval_T v: variable value.
2303 */
2304 typval_T *
2305get_vim_var_tv(int idx)
2306{
2307 return &vimvars[idx].vv_tv;
2308}
2309
Bram Moolenaard787e402021-12-24 21:36:12 +00002310 type_T *
2311get_vim_var_type(int idx, garray_T *type_list)
2312{
2313 if (vimvars[idx].vv_type != NULL)
2314 return vimvars[idx].vv_type;
2315 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2316}
2317
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002318/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002319 * Set v: variable to "tv". Only accepts the same type.
2320 * Takes over the value of "tv".
2321 */
2322 int
2323set_vim_var_tv(int idx, typval_T *tv)
2324{
Bram Moolenaard787e402021-12-24 21:36:12 +00002325 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002326 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002327 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002328 clear_tv(tv);
2329 return FAIL;
2330 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002331 // VV_RO is also checked when compiling, but let's check here as well.
2332 if (vimvars[idx].vv_flags & VV_RO)
2333 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002334 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002335 return FAIL;
2336 }
2337 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2338 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002339 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002340 return FAIL;
2341 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002342 clear_tv(&vimvars[idx].vv_di.di_tv);
2343 vimvars[idx].vv_di.di_tv = *tv;
2344 return OK;
2345}
2346
2347/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002348 * Get number v: variable value.
2349 */
2350 varnumber_T
2351get_vim_var_nr(int idx)
2352{
2353 return vimvars[idx].vv_nr;
2354}
2355
2356/*
2357 * Get string v: variable value. Uses a static buffer, can only be used once.
2358 * If the String variable has never been set, return an empty string.
2359 * Never returns NULL;
2360 */
2361 char_u *
2362get_vim_var_str(int idx)
2363{
2364 return tv_get_string(&vimvars[idx].vv_tv);
2365}
2366
2367/*
2368 * Get List v: variable value. Caller must take care of reference count when
2369 * needed.
2370 */
2371 list_T *
2372get_vim_var_list(int idx)
2373{
2374 return vimvars[idx].vv_list;
2375}
2376
2377/*
2378 * Get Dict v: variable value. Caller must take care of reference count when
2379 * needed.
2380 */
2381 dict_T *
2382get_vim_var_dict(int idx)
2383{
2384 return vimvars[idx].vv_dict;
2385}
2386
2387/*
2388 * Set v:char to character "c".
2389 */
2390 void
2391set_vim_var_char(int c)
2392{
2393 char_u buf[MB_MAXBYTES + 1];
2394
2395 if (has_mbyte)
2396 buf[(*mb_char2bytes)(c, buf)] = NUL;
2397 else
2398 {
2399 buf[0] = c;
2400 buf[1] = NUL;
2401 }
2402 set_vim_var_string(VV_CHAR, buf, -1);
2403}
2404
2405/*
2406 * Set v:count to "count" and v:count1 to "count1".
2407 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2408 */
2409 void
2410set_vcount(
2411 long count,
2412 long count1,
2413 int set_prevcount)
2414{
2415 if (set_prevcount)
2416 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2417 vimvars[VV_COUNT].vv_nr = count;
2418 vimvars[VV_COUNT1].vv_nr = count1;
2419}
2420
2421/*
2422 * Save variables that might be changed as a side effect. Used when executing
2423 * a timer callback.
2424 */
2425 void
2426save_vimvars(vimvars_save_T *vvsave)
2427{
2428 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2429 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2430 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2431}
2432
2433/*
2434 * Restore variables saved by save_vimvars().
2435 */
2436 void
2437restore_vimvars(vimvars_save_T *vvsave)
2438{
2439 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2440 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2441 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2442}
2443
2444/*
2445 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2446 * value.
2447 */
2448 void
2449set_vim_var_string(
2450 int idx,
2451 char_u *val,
2452 int len) // length of "val" to use or -1 (whole string)
2453{
2454 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002455 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002456 if (val == NULL)
2457 vimvars[idx].vv_str = NULL;
2458 else if (len == -1)
2459 vimvars[idx].vv_str = vim_strsave(val);
2460 else
2461 vimvars[idx].vv_str = vim_strnsave(val, len);
2462}
2463
2464/*
2465 * Set List v: variable to "val".
2466 */
2467 void
2468set_vim_var_list(int idx, list_T *val)
2469{
2470 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002471 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002472 vimvars[idx].vv_list = val;
2473 if (val != NULL)
2474 ++val->lv_refcount;
2475}
2476
2477/*
2478 * Set Dictionary v: variable to "val".
2479 */
2480 void
2481set_vim_var_dict(int idx, dict_T *val)
2482{
2483 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002484 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002485 vimvars[idx].vv_dict = val;
2486 if (val != NULL)
2487 {
2488 ++val->dv_refcount;
2489 dict_set_items_ro(val);
2490 }
2491}
2492
2493/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002494 * Set the v:argv list.
2495 */
2496 void
2497set_argv_var(char **argv, int argc)
2498{
2499 list_T *l = list_alloc();
2500 int i;
2501
2502 if (l == NULL)
2503 getout(1);
2504 l->lv_lock = VAR_FIXED;
2505 for (i = 0; i < argc; ++i)
2506 {
2507 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2508 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002509 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002510 }
2511 set_vim_var_list(VV_ARGV, l);
2512}
2513
2514/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002515 * Reset v:register, taking the 'clipboard' setting into account.
2516 */
2517 void
2518reset_reg_var(void)
2519{
2520 int regname = 0;
2521
2522 // Adjust the register according to 'clipboard', so that when
2523 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2524#ifdef FEAT_CLIPBOARD
2525 adjust_clip_reg(&regname);
2526#endif
2527 set_reg_var(regname);
2528}
2529
2530/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002531 * Set v:register if needed.
2532 */
2533 void
2534set_reg_var(int c)
2535{
2536 char_u regname;
2537
2538 if (c == 0 || c == ' ')
2539 regname = '"';
2540 else
2541 regname = c;
2542 // Avoid free/alloc when the value is already right.
2543 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2544 set_vim_var_string(VV_REG, &regname, 1);
2545}
2546
2547/*
2548 * Get or set v:exception. If "oldval" == NULL, return the current value.
2549 * Otherwise, restore the value to "oldval" and return NULL.
2550 * Must always be called in pairs to save and restore v:exception! Does not
2551 * take care of memory allocations.
2552 */
2553 char_u *
2554v_exception(char_u *oldval)
2555{
2556 if (oldval == NULL)
2557 return vimvars[VV_EXCEPTION].vv_str;
2558
2559 vimvars[VV_EXCEPTION].vv_str = oldval;
2560 return NULL;
2561}
2562
2563/*
2564 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2565 * Otherwise, restore the value to "oldval" and return NULL.
2566 * Must always be called in pairs to save and restore v:throwpoint! Does not
2567 * take care of memory allocations.
2568 */
2569 char_u *
2570v_throwpoint(char_u *oldval)
2571{
2572 if (oldval == NULL)
2573 return vimvars[VV_THROWPOINT].vv_str;
2574
2575 vimvars[VV_THROWPOINT].vv_str = oldval;
2576 return NULL;
2577}
2578
2579/*
2580 * Set v:cmdarg.
2581 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2582 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2583 * Must always be called in pairs!
2584 */
2585 char_u *
2586set_cmdarg(exarg_T *eap, char_u *oldarg)
2587{
2588 char_u *oldval;
2589 char_u *newval;
2590 unsigned len;
2591
2592 oldval = vimvars[VV_CMDARG].vv_str;
2593 if (eap == NULL)
2594 {
2595 vim_free(oldval);
2596 vimvars[VV_CMDARG].vv_str = oldarg;
2597 return NULL;
2598 }
2599
2600 if (eap->force_bin == FORCE_BIN)
2601 len = 6;
2602 else if (eap->force_bin == FORCE_NOBIN)
2603 len = 8;
2604 else
2605 len = 0;
2606
2607 if (eap->read_edit)
2608 len += 7;
2609
2610 if (eap->force_ff != 0)
2611 len += 10; // " ++ff=unix"
2612 if (eap->force_enc != 0)
2613 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2614 if (eap->bad_char != 0)
2615 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2616
2617 newval = alloc(len + 1);
2618 if (newval == NULL)
2619 return NULL;
2620
2621 if (eap->force_bin == FORCE_BIN)
2622 sprintf((char *)newval, " ++bin");
2623 else if (eap->force_bin == FORCE_NOBIN)
2624 sprintf((char *)newval, " ++nobin");
2625 else
2626 *newval = NUL;
2627
2628 if (eap->read_edit)
2629 STRCAT(newval, " ++edit");
2630
2631 if (eap->force_ff != 0)
2632 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
2633 eap->force_ff == 'u' ? "unix"
2634 : eap->force_ff == 'd' ? "dos"
2635 : "mac");
2636 if (eap->force_enc != 0)
2637 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
2638 eap->cmd + eap->force_enc);
2639 if (eap->bad_char == BAD_KEEP)
2640 STRCPY(newval + STRLEN(newval), " ++bad=keep");
2641 else if (eap->bad_char == BAD_DROP)
2642 STRCPY(newval + STRLEN(newval), " ++bad=drop");
2643 else if (eap->bad_char != 0)
2644 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
2645 vimvars[VV_CMDARG].vv_str = newval;
2646 return oldval;
2647}
2648
2649/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002650 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002651 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
2652 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002653 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
2654 */
2655 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002656eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002657 char_u *name,
2658 int len, // length of "name"
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002659 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002660 typval_T *rettv, // NULL when only checking existence
2661 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002662 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002663{
2664 int ret = OK;
2665 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002666 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002667 hashtab_T *ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002668 int cc;
Bram Moolenaarc967d572021-07-08 21:38:50 +02002669 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002670
2671 // truncate the name, so that we can use strcmp()
2672 cc = name[len];
2673 name[len] = NUL;
2674
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002675 // Check for local variable when debugging.
2676 if ((tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002677 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002678 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02002679 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
2680
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02002681 if (v != NULL)
2682 {
2683 tv = &v->di_tv;
2684 if (dip != NULL)
2685 *dip = v;
2686 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02002687 else
2688 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002689 }
2690
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002691 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002693 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02002694 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
2695
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002696 if (sid == 0)
Bram Moolenaardc4451d2022-01-09 21:36:37 +00002697 import = find_imported(p, 0, TRUE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698
2699 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002700 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002702 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02002703 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00002704 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02002705 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002706 ht = &SCRIPT_VARS(sid);
2707 if (ht != NULL)
2708 {
2709 dictitem_T *v = find_var_in_ht(ht, 0, name,
2710 flags & EVAL_VAR_NOAUTOLOAD);
2711
2712 if (v != NULL)
2713 {
2714 tv = &v->di_tv;
2715 if (dip != NULL)
2716 *dip = v;
2717 }
2718 else
2719 ht = NULL;
2720 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002721 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002722 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002723 {
2724 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00002725 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002726 ret = FAIL;
2727 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02002728 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002729 else
2730 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002731 if (rettv != NULL)
2732 {
2733 rettv->v_type = VAR_ANY;
2734 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
2735 }
2736 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02002737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00002739 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002740 {
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00002741 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002742
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002743 // In Vim9 script we can get a function reference by using the
2744 // function name.
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002745 if (ufunc != NULL)
2746 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002747 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002748 if (rettv != NULL)
2749 {
2750 rettv->v_type = VAR_FUNC;
Bram Moolenaaref082e12021-12-12 21:02:03 +00002751 if (STRNCMP(name, "g:", 2) == 0)
2752 // Keep the "g:", otherwise script-local may be
2753 // assumed.
2754 rettv->vval.v_string = vim_strsave(name);
2755 else
2756 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02002757 if (rettv->vval.v_string != NULL)
2758 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02002759 }
2760 }
2761 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 }
2763
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002764 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002765 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02002766 if (tv == NULL)
2767 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002768 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002769 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02002770 ret = FAIL;
2771 }
2772 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002773 {
Bram Moolenaar11005b02021-07-11 20:59:00 +02002774 if (ht != NULL && ht == get_script_local_ht()
2775 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002776 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002777 svar_T *sv = find_typval_in_script(tv, 0);
Bram Moolenaarf055d452021-07-08 20:57:24 +02002778
Bram Moolenaarf055d452021-07-08 20:57:24 +02002779 if (sv != NULL)
2780 type = sv->sv_type;
2781 }
2782
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002783 // If a list or dict variable wasn't initialized, do it now.
2784 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL)
2785 {
2786 tv->vval.v_dict = dict_alloc();
2787 if (tv->vval.v_dict != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002788 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002789 ++tv->vval.v_dict->dv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002790 tv->vval.v_dict->dv_type = alloc_type(type);
2791 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002792 }
2793 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL)
2794 {
2795 tv->vval.v_list = list_alloc();
2796 if (tv->vval.v_list != NULL)
Bram Moolenaarf055d452021-07-08 20:57:24 +02002797 {
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002798 ++tv->vval.v_list->lv_refcount;
Bram Moolenaarf055d452021-07-08 20:57:24 +02002799 tv->vval.v_list->lv_type = alloc_type(type);
2800 }
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002801 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02002802 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL)
2803 {
2804 tv->vval.v_blob = blob_alloc();
2805 if (tv->vval.v_blob != NULL)
2806 ++tv->vval.v_blob->bv_refcount;
2807 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02002808 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01002809 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002810 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002811
2812 name[len] = cc;
2813
2814 return ret;
2815}
2816
2817/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002818 * Check if variable "name[len]" is a local variable or an argument.
2819 * If so, "*eval_lavars_used" is set to TRUE.
2820 */
2821 void
2822check_vars(char_u *name, int len)
2823{
2824 int cc;
2825 char_u *varname;
2826 hashtab_T *ht;
2827
2828 if (eval_lavars_used == NULL)
2829 return;
2830
2831 // truncate the name, so that we can use strcmp()
2832 cc = name[len];
2833 name[len] = NUL;
2834
2835 ht = find_var_ht(name, &varname);
2836 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
2837 {
2838 if (find_var(name, NULL, TRUE) != NULL)
2839 *eval_lavars_used = TRUE;
2840 }
2841
2842 name[len] = cc;
2843}
2844
2845/*
2846 * Find variable "name" in the list of variables.
2847 * Return a pointer to it if found, NULL if not found.
2848 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002849 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002850 */
2851 dictitem_T *
2852find_var(char_u *name, hashtab_T **htp, int no_autoload)
2853{
2854 char_u *varname;
2855 hashtab_T *ht;
2856 dictitem_T *ret = NULL;
2857
2858 ht = find_var_ht(name, &varname);
2859 if (htp != NULL)
2860 *htp = ht;
2861 if (ht == NULL)
2862 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002863 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002864 if (ret != NULL)
2865 return ret;
2866
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002867 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002868 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002869 if (ret != NULL)
2870 return ret;
2871
2872 // in Vim9 script items without a scope can be script-local
2873 if (in_vim9script() && name[0] != NUL && name[1] != ':')
2874 {
2875 ht = get_script_local_ht();
2876 if (ht != NULL)
2877 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002878 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002879 if (ret != NULL)
2880 {
2881 if (htp != NULL)
2882 *htp = ht;
2883 return ret;
2884 }
2885 }
2886 }
2887
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002888 // When using "vim9script autoload" script-local items are prefixed but can
2889 // be used with s:name.
2890 if (SCRIPT_ID_VALID(current_sctx.sc_sid)
2891 && name[0] == 's' && name[1] == ':')
2892 {
2893 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
2894
2895 if (si->sn_autoload_prefix != NULL)
2896 {
2897 char_u *auto_name = concat_str(si->sn_autoload_prefix, name + 2);
2898
2899 if (auto_name != NULL)
2900 {
2901 ht = &globvarht;
2902 ret = find_var_in_ht(ht, *name, auto_name, TRUE);
Bram Moolenaar130f65d2022-01-13 20:39:41 +00002903 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002904 if (ret != NULL)
2905 {
2906 if (htp != NULL)
2907 *htp = ht;
2908 return ret;
2909 }
2910 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00002911 }
2912 }
2913
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01002914 return NULL;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002915}
2916
2917/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00002918 * Like find_var() but if the name starts with <SNR>99_ then look in the
2919 * referenced script (used for a funcref).
2920 */
2921 dictitem_T *
2922find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
2923{
2924 if (STRNCMP(name, "<SNR>", 5) == 0 && isdigit(name[5]))
2925 {
2926 char_u *p = name + 5;
2927 int sid = getdigits(&p);
2928
2929 if (SCRIPT_ID_VALID(sid) && *p == '_')
2930 {
2931 hashtab_T *ht = &SCRIPT_VARS(sid);
2932
2933 if (ht != NULL)
2934 {
2935 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
2936
2937 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002938 {
2939 if (htp != NULL)
2940 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00002941 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00002942 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00002943 }
2944 }
2945 }
2946
2947 return find_var(name, htp, no_autoload);
2948}
2949
2950/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002951 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02002952 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002953 * Returns NULL if not found.
2954 */
2955 dictitem_T *
2956find_var_in_ht(
2957 hashtab_T *ht,
2958 int htname,
2959 char_u *varname,
2960 int no_autoload)
2961{
2962 hashitem_T *hi;
2963
2964 if (*varname == NUL)
2965 {
2966 // Must be something like "s:", otherwise "ht" would be NULL.
2967 switch (htname)
2968 {
2969 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
2970 case 'g': return &globvars_var;
2971 case 'v': return &vimvars_var;
2972 case 'b': return &curbuf->b_bufvar;
2973 case 'w': return &curwin->w_winvar;
2974 case 't': return &curtab->tp_winvar;
2975 case 'l': return get_funccal_local_var();
2976 case 'a': return get_funccal_args_var();
2977 }
2978 return NULL;
2979 }
2980
2981 hi = hash_find(ht, varname);
2982 if (HASHITEM_EMPTY(hi))
2983 {
2984 // For global variables we may try auto-loading the script. If it
2985 // worked find the variable again. Don't auto-load a script if it was
2986 // loaded already, otherwise it would be loaded every time when
2987 // checking if a function name is a Funcref variable.
2988 if (ht == &globvarht && !no_autoload)
2989 {
2990 // Note: script_autoload() may make "hi" invalid. It must either
2991 // be obtained again or not used.
2992 if (!script_autoload(varname, FALSE) || aborting())
2993 return NULL;
2994 hi = hash_find(ht, varname);
2995 }
2996 if (HASHITEM_EMPTY(hi))
2997 return NULL;
2998 }
2999 return HI2DI(hi);
3000}
3001
3002/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 * Get the script-local hashtab. NULL if not in a script context.
3004 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003005 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006get_script_local_ht(void)
3007{
3008 scid_T sid = current_sctx.sc_sid;
3009
Bram Moolenaare3d46852020-08-29 13:39:17 +02003010 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 return &SCRIPT_VARS(sid);
3012 return NULL;
3013}
3014
3015/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003016 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003017 * When "cmd" is TRUE it must look like a command, a function must be followed
3018 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003019 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003021 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003022lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003023 char_u *name,
3024 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003025 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003026 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027{
3028 hashtab_T *ht = get_script_local_ht();
3029 char_u buffer[30];
3030 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003031 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003033 int is_global = FALSE;
3034 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003035
3036 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003037 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 if (len < sizeof(buffer) - 1)
3039 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003040 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 vim_strncpy(buffer, name, len);
3042 p = buffer;
3043 }
3044 else
3045 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003046 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003048 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003049 }
3050
3051 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003052 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053
3054 // if not script-local, then perhaps imported
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003055 if (res == FAIL && find_imported(p, 0, FALSE, NULL) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003056 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 if (p != buffer)
3058 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003059
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003060 // Find a function, so that a following "->" works.
3061 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3062 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003063 if (res != OK)
3064 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003065 p = skipwhite(name + len);
3066
3067 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003068 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003069 // Do not check for an internal function, since it might also be a
3070 // valid command, such as ":split" versus "split()".
3071 // Skip "g:" before a function name.
3072 if (name[0] == 'g' && name[1] == ':')
3073 {
3074 is_global = TRUE;
3075 fname = name + 2;
3076 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003077 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003078 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003079 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003080 }
3081
Bram Moolenaar709664c2020-12-12 14:33:41 +01003082 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083}
3084
3085/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003086 * Find the hashtab used for a variable name.
3087 * Return NULL if the name is not valid.
3088 * Set "varname" to the start of name without ':'.
3089 */
3090 hashtab_T *
3091find_var_ht(char_u *name, char_u **varname)
3092{
3093 hashitem_T *hi;
3094 hashtab_T *ht;
3095
3096 if (name[0] == NUL)
3097 return NULL;
3098 if (name[1] != ':')
3099 {
3100 // The name must not start with a colon or #.
3101 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3102 return NULL;
3103 *varname = name;
3104
3105 // "version" is "v:version" in all scopes if scriptversion < 3.
3106 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003107 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003108 {
3109 hi = hash_find(&compat_hashtab, name);
3110 if (!HASHITEM_EMPTY(hi))
3111 return &compat_hashtab;
3112 }
3113
3114 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 if (ht != NULL)
3116 return ht; // local variable
3117
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003118 // In Vim9 script items at the script level are script-local, except
3119 // for autoload names.
3120 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 {
3122 ht = get_script_local_ht();
3123 if (ht != NULL)
3124 return ht;
3125 }
3126
3127 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003128 }
3129 *varname = name + 2;
3130 if (*name == 'g') // global variable
3131 return &globvarht;
3132 // There must be no ':' or '#' in the rest of the name, unless g: is used
3133 if (vim_strchr(name + 2, ':') != NULL
3134 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3135 return NULL;
3136 if (*name == 'b') // buffer variable
3137 return &curbuf->b_vars->dv_hashtab;
3138 if (*name == 'w') // window variable
3139 return &curwin->w_vars->dv_hashtab;
3140 if (*name == 't') // tab page variable
3141 return &curtab->tp_vars->dv_hashtab;
3142 if (*name == 'v') // v: variable
3143 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003144 if (get_current_funccal() != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003145 && get_current_funccal()->func->uf_def_status == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003147 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 if (*name == 'a') // a: function argument
3149 return get_funccal_args_ht();
3150 if (*name == 'l') // l: local function variable
3151 return get_funccal_local_ht();
3152 }
3153 if (*name == 's') // script variable
3154 {
3155 ht = get_script_local_ht();
3156 if (ht != NULL)
3157 return ht;
3158 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003159 return NULL;
3160}
3161
3162/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003163 * Get the string value of a (global/local) variable.
3164 * Note: see tv_get_string() for how long the pointer remains valid.
3165 * Returns NULL when it doesn't exist.
3166 */
3167 char_u *
3168get_var_value(char_u *name)
3169{
3170 dictitem_T *v;
3171
3172 v = find_var(name, NULL, FALSE);
3173 if (v == NULL)
3174 return NULL;
3175 return tv_get_string(&v->di_tv);
3176}
3177
3178/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003179 * Allocate a new hashtab for a sourced script. It will be used while
3180 * sourcing this script and when executing functions defined in the script.
3181 */
3182 void
3183new_script_vars(scid_T id)
3184{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003185 scriptvar_T *sv;
3186
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003187 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3188 if (sv == NULL)
3189 return;
3190 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003191 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003192}
3193
3194/*
3195 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3196 * point to it.
3197 */
3198 void
3199init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3200{
3201 hash_init(&dict->dv_hashtab);
3202 dict->dv_lock = 0;
3203 dict->dv_scope = scope;
3204 dict->dv_refcount = DO_NOT_FREE_CNT;
3205 dict->dv_copyID = 0;
3206 dict_var->di_tv.vval.v_dict = dict;
3207 dict_var->di_tv.v_type = VAR_DICT;
3208 dict_var->di_tv.v_lock = VAR_FIXED;
3209 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3210 dict_var->di_key[0] = NUL;
3211}
3212
3213/*
3214 * Unreference a dictionary initialized by init_var_dict().
3215 */
3216 void
3217unref_var_dict(dict_T *dict)
3218{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003219 // Now the dict needs to be freed if no one else is using it, go back to
3220 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003221 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3222 dict_unref(dict);
3223}
3224
3225/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003226 * Clean up a list of internal variables.
3227 * Frees all allocated variables and the value they contain.
3228 * Clears hashtab "ht", does not free it.
3229 */
3230 void
3231vars_clear(hashtab_T *ht)
3232{
3233 vars_clear_ext(ht, TRUE);
3234}
3235
3236/*
3237 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3238 */
3239 void
3240vars_clear_ext(hashtab_T *ht, int free_val)
3241{
3242 int todo;
3243 hashitem_T *hi;
3244 dictitem_T *v;
3245
3246 hash_lock(ht);
3247 todo = (int)ht->ht_used;
3248 for (hi = ht->ht_array; todo > 0; ++hi)
3249 {
3250 if (!HASHITEM_EMPTY(hi))
3251 {
3252 --todo;
3253
3254 // Free the variable. Don't remove it from the hashtab,
3255 // ht_array might change then. hash_clear() takes care of it
3256 // later.
3257 v = HI2DI(hi);
3258 if (free_val)
3259 clear_tv(&v->di_tv);
3260 if (v->di_flags & DI_FLAGS_ALLOC)
3261 vim_free(v);
3262 }
3263 }
3264 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003265 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003266}
3267
3268/*
3269 * Delete a variable from hashtab "ht" at item "hi".
3270 * Clear the variable value and free the dictitem.
3271 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003272 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003273delete_var(hashtab_T *ht, hashitem_T *hi)
3274{
3275 dictitem_T *di = HI2DI(hi);
3276
3277 hash_remove(ht, hi);
3278 clear_tv(&di->di_tv);
3279 vim_free(di);
3280}
3281
3282/*
3283 * List the value of one internal variable.
3284 */
3285 static void
3286list_one_var(dictitem_T *v, char *prefix, int *first)
3287{
3288 char_u *tofree;
3289 char_u *s;
3290 char_u numbuf[NUMBUFLEN];
3291
3292 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3293 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3294 s == NULL ? (char_u *)"" : s, first);
3295 vim_free(tofree);
3296}
3297
3298 static void
3299list_one_var_a(
3300 char *prefix,
3301 char_u *name,
3302 int type,
3303 char_u *string,
3304 int *first) // when TRUE clear rest of screen and set to FALSE
3305{
3306 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3307 msg_start();
3308 msg_puts(prefix);
3309 if (name != NULL) // "a:" vars don't have a name stored
3310 msg_puts((char *)name);
3311 msg_putchar(' ');
3312 msg_advance(22);
3313 if (type == VAR_NUMBER)
3314 msg_putchar('#');
3315 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3316 msg_putchar('*');
3317 else if (type == VAR_LIST)
3318 {
3319 msg_putchar('[');
3320 if (*string == '[')
3321 ++string;
3322 }
3323 else if (type == VAR_DICT)
3324 {
3325 msg_putchar('{');
3326 if (*string == '{')
3327 ++string;
3328 }
3329 else
3330 msg_putchar(' ');
3331
3332 msg_outtrans(string);
3333
3334 if (type == VAR_FUNC || type == VAR_PARTIAL)
3335 msg_puts("()");
3336 if (*first)
3337 {
3338 msg_clr_eos();
3339 *first = FALSE;
3340 }
3341}
3342
3343/*
3344 * Set variable "name" to value in "tv".
3345 * If the variable already exists, the value is updated.
3346 * Otherwise the variable is created.
3347 */
3348 void
3349set_var(
3350 char_u *name,
3351 typval_T *tv,
3352 int copy) // make copy of value in "tv"
3353{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003354 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003355}
3356
3357/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003358 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003359 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003360 * If the variable already exists and "is_const" is FALSE the value is updated.
3361 * Otherwise the variable is created.
3362 */
3363 void
3364set_var_const(
3365 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003366 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003367 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003368 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003369 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003370 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003371 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003372{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003373 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003374 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003375 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 dictitem_T *di;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003377 typval_T *dest_tv = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003378 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003379 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003380 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003381 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003382 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003383 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003384 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003385 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003386 int free_tv_arg = !copy; // free tv_arg if not used
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003387
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003388 if (sid != 0)
3389 {
3390 if (SCRIPT_ID_VALID(sid))
3391 ht = &SCRIPT_VARS(sid);
3392 varname = name;
3393 }
3394 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003395 {
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003396 scriptitem_T *si;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003397
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003398 if (in_vim9script() && is_export
3399 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3400 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3401 ->sn_autoload_prefix != NULL)
3402 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003403 // In a vim9 autoload script an exported variable is put in the
3404 // global namespace with the autoload prefix.
3405 var_in_autoload = TRUE;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003406 varname = concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003407 if (varname == NULL)
3408 goto failed;
3409 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003410 ht = &globvarht;
3411 }
3412 else
3413 ht = find_var_ht(name, &varname);
3414 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003415 if (ht == NULL || *varname == NUL)
3416 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003417 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003418 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003419 }
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003420 is_script_local = ht == get_script_local_ht() || sid != 0 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003421
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003422 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003423 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003424 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003425 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003426 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003427 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003428 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003429 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003430 }
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003431 if ((flags & ASSIGN_FOR_LOOP) && name[1] == ':'
3432 && vim_strchr((char_u *)"gwbt", name[0]) != NULL)
3433 // Do not make g:var, w:var, b:var or t:var final.
3434 flags &= ~ASSIGN_FINAL;
3435
Bram Moolenaare535db82021-03-31 21:07:24 +02003436 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003437 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
3438 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02003439 // For "[a, _] = list" the underscore is ignored.
3440 if ((flags & ASSIGN_UNPACK) == 0)
3441 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02003442 goto failed;
3443 }
Bram Moolenaar67979662020-06-20 22:50:47 +02003444
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003446
Bram Moolenaar24e93162021-07-18 20:40:33 +02003447 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003448 {
Bram Moolenaardc4451d2022-01-09 21:36:37 +00003449 imported_T *import = find_imported(varname, 0, FALSE, NULL);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003450
Bram Moolenaar24e93162021-07-18 20:40:33 +02003451 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003452 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003453 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02003454 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003456 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003457 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003459 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
3460 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02003461 }
3462 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463
Bram Moolenaar24e93162021-07-18 20:40:33 +02003464 if (dest_tv == NULL)
3465 {
3466 // Search in parent scope which is possible to reference from lambda
3467 if (di == NULL)
3468 di = find_var_in_scoped_ht(name, TRUE);
3469
3470 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003471 && var_wrong_func_name(name, di == NULL))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003472 goto failed;
3473
3474 if (need_convert_to_bool(type, tv))
3475 {
Bram Moolenaarf6488542021-07-18 21:24:50 +02003476 // Destination is a bool and the value is not, but it can be
3477 // converted.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003478 CLEAR_FIELD(bool_tv);
3479 bool_tv.v_type = VAR_BOOL;
3480 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
3481 tv = &bool_tv;
3482 }
3483
3484 if (di != NULL)
3485 {
3486 // Item already exists. Allowed to replace when reloading.
3487 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
3488 {
3489 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
Bram Moolenaarf6488542021-07-18 21:24:50 +02003490 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003491 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003492 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar24e93162021-07-18 20:40:33 +02003493 goto failed;
3494 }
3495
3496 if (is_script_local && vim9script
Bram Moolenaarf6488542021-07-18 21:24:50 +02003497 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003498 {
3499 semsg(_(e_redefining_script_item_str), name);
3500 goto failed;
3501 }
3502
Bram Moolenaara06758d2021-10-15 00:18:37 +01003503 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003504 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003505 where_T where = WHERE_INIT;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003506 svar_T *sv = find_typval_in_script(&di->di_tv, sid);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003507
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003508 if (sv != NULL)
3509 {
3510 // check the type and adjust to bool if needed
3511 where.wt_index = var_idx;
3512 where.wt_variable = TRUE;
3513 if (check_script_var_type(sv, tv, name, where) == FAIL)
3514 goto failed;
3515 if (type == NULL)
3516 type = sv->sv_type;
3517 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003518 }
3519
Bram Moolenaara06758d2021-10-15 00:18:37 +01003520 if ((flags & ASSIGN_FOR_LOOP) == 0
3521 && var_check_permission(di, name) == FAIL)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003522 goto failed;
3523 }
3524 else
3525 {
3526 // can only redefine once
3527 di->di_flags &= ~DI_FLAGS_RELOAD;
3528
Bram Moolenaarf6488542021-07-18 21:24:50 +02003529 // A Vim9 script-local variable is also present in sn_all_vars
3530 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003531 if (var_in_vim9script || var_in_autoload)
3532 update_vim9_script_var(FALSE, di,
3533 var_in_autoload ? name : di->di_key, flags,
3534 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003535 }
3536
3537 // existing variable, need to clear the value
3538
3539 // Handle setting internal di: variables separately where needed to
3540 // prevent changing the type.
3541 if (ht == &vimvarht)
3542 {
3543 if (di->di_tv.v_type == VAR_STRING)
3544 {
3545 VIM_CLEAR(di->di_tv.vval.v_string);
3546 if (copy || tv->v_type != VAR_STRING)
3547 {
3548 char_u *val = tv_get_string(tv);
3549
Bram Moolenaarf6488542021-07-18 21:24:50 +02003550 // Careful: when assigning to v:errmsg and
3551 // tv_get_string() causes an error message the variable
3552 // will already be set.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003553 if (di->di_tv.vval.v_string == NULL)
3554 di->di_tv.vval.v_string = vim_strsave(val);
3555 }
3556 else
3557 {
3558 // Take over the string to avoid an extra alloc/free.
3559 di->di_tv.vval.v_string = tv->vval.v_string;
3560 tv->vval.v_string = NULL;
3561 }
3562 goto failed;
3563 }
3564 else if (di->di_tv.v_type == VAR_NUMBER)
3565 {
3566 di->di_tv.vval.v_number = tv_get_number(tv);
3567 if (STRCMP(varname, "searchforward") == 0)
Bram Moolenaarf6488542021-07-18 21:24:50 +02003568 set_search_direction(di->di_tv.vval.v_number
3569 ? '/' : '?');
Bram Moolenaar24e93162021-07-18 20:40:33 +02003570#ifdef FEAT_SEARCH_EXTRA
3571 else if (STRCMP(varname, "hlsearch") == 0)
3572 {
3573 no_hlsearch = !di->di_tv.vval.v_number;
3574 redraw_all_later(SOME_VALID);
3575 }
3576#endif
3577 goto failed;
3578 }
3579 else if (di->di_tv.v_type != tv->v_type)
3580 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00003581 semsg(_(e_setting_str_to_value_with_wrong_type), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003582 goto failed;
3583 }
3584 }
3585
3586 clear_tv(&di->di_tv);
3587 }
3588 else
3589 {
3590 // Item not found, check if a function already exists.
3591 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaarf6488542021-07-18 21:24:50 +02003592 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
Bram Moolenaar12be7342021-03-31 21:47:33 +02003593 {
3594 semsg(_(e_redefining_script_item_str), name);
3595 goto failed;
3596 }
3597
Bram Moolenaar24e93162021-07-18 20:40:33 +02003598 // add a new variable
3599 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
3600 {
3601 semsg(_(e_unknown_variable_str), name);
3602 goto failed;
3603 }
3604
3605 // Can't add "v:" or "a:" variable.
3606 if (ht == &vimvarht || ht == get_funccal_args_ht())
3607 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003608 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003609 goto failed;
3610 }
3611
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003612 // Make sure the variable name is valid. In Vim9 script an
3613 // autoload variable must be prefixed with "g:" unless in an
3614 // autoload script.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003615 if (!valid_varname(varname, -1, !vim9script
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003616 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
Bram Moolenaar24e93162021-07-18 20:40:33 +02003617 goto failed;
3618
3619 di = alloc(sizeof(dictitem_T) + STRLEN(varname));
3620 if (di == NULL)
3621 goto failed;
3622 STRCPY(di->di_key, varname);
3623 if (hash_add(ht, DI2HIKEY(di)) == FAIL)
3624 {
3625 vim_free(di);
3626 goto failed;
3627 }
3628 di->di_flags = DI_FLAGS_ALLOC;
3629 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
3630 di->di_flags |= DI_FLAGS_LOCK;
3631
3632 // A Vim9 script-local variable is also added to sn_all_vars and
3633 // sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003634 if (var_in_vim9script || var_in_autoload)
3635 update_vim9_script_var(TRUE, di,
3636 var_in_autoload ? name : di->di_key, flags,
3637 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01003638 }
3639
Bram Moolenaar24e93162021-07-18 20:40:33 +02003640 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003641 }
3642
3643 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02003644 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003645 else
3646 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003647 *dest_tv = *tv;
3648 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003649 init_tv(tv);
3650 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003651 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003652
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003653 if (vim9script && type != NULL)
3654 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02003655 if (type->tt_type == VAR_DICT && dest_tv->vval.v_dict != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003656 {
3657 if (dest_tv->vval.v_dict->dv_type != type)
3658 {
3659 free_type(dest_tv->vval.v_dict->dv_type);
3660 dest_tv->vval.v_dict->dv_type = alloc_type(type);
3661 }
3662 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02003663 else if (type->tt_type == VAR_LIST && dest_tv->vval.v_list != NULL)
Bram Moolenaar464393a2021-09-11 23:07:44 +02003664 {
3665 if (dest_tv->vval.v_list->lv_type != type)
3666 {
3667 free_type(dest_tv->vval.v_list->lv_type);
3668 dest_tv->vval.v_list->lv_type = alloc_type(type);
3669 }
3670 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003671 }
3672
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01003673 // ":const var = value" locks the value
3674 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02003675 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02003676 // Like :lockvar! name: lock the value and what it contains, but only
3677 // if the reference count is up to one. That locks only literal
3678 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02003679 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02003680
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003681failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003682 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003683 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003684 clear_tv(tv_arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003685}
3686
3687/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01003688 * Check in this order for backwards compatibility:
3689 * - Whether the variable is read-only
3690 * - Whether the variable value is locked
3691 * - Whether the variable is locked
3692 */
3693 int
3694var_check_permission(dictitem_T *di, char_u *name)
3695{
3696 if (var_check_ro(di->di_flags, name, FALSE)
3697 || value_check_lock(di->di_tv.v_lock, name, FALSE)
3698 || var_check_lock(di->di_flags, name, FALSE))
3699 return FAIL;
3700 return OK;
3701}
3702
3703/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003704 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
3705 * Also give an error message.
3706 */
3707 int
3708var_check_ro(int flags, char_u *name, int use_gettext)
3709{
3710 if (flags & DI_FLAGS_RO)
3711 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003712 if (name == NULL)
3713 emsg(_(e_cannot_change_readonly_variable));
3714 else
3715 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02003716 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003717 return TRUE;
3718 }
3719 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
3720 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003721 if (name == NULL)
3722 emsg(_(e_cannot_set_variable_in_sandbox));
3723 else
3724 semsg(_(e_cannot_set_variable_in_sandbox_str),
3725 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003726 return TRUE;
3727 }
3728 return FALSE;
3729}
3730
3731/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003732 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
3733 * Also give an error message.
3734 */
3735 int
3736var_check_lock(int flags, char_u *name, int use_gettext)
3737{
3738 if (flags & DI_FLAGS_LOCK)
3739 {
3740 semsg(_(e_variable_is_locked_str),
3741 use_gettext ? (char_u *)_(name) : name);
3742 return TRUE;
3743 }
3744 return FALSE;
3745}
3746
3747/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003748 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
3749 * Also give an error message.
3750 */
3751 int
3752var_check_fixed(int flags, char_u *name, int use_gettext)
3753{
3754 if (flags & DI_FLAGS_FIX)
3755 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003756 if (name == NULL)
3757 emsg(_(e_cannot_delete_variable));
3758 else
3759 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003760 use_gettext ? (char_u *)_(name) : name);
3761 return TRUE;
3762 }
3763 return FALSE;
3764}
3765
3766/*
3767 * Check if a funcref is assigned to a valid variable name.
3768 * Return TRUE and give an error if not.
3769 */
3770 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02003771var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003772 char_u *name, // points to start of variable name
3773 int new_var) // TRUE when creating the variable
3774{
Bram Moolenaar32154662021-03-28 21:14:06 +02003775 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
3776 // the name can be used without the s: prefix.
3777 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
3778 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003779 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
3780 ? name[2] : name[0]))
3781 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003782 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003783 return TRUE;
3784 }
3785 // Don't allow hiding a function. When "v" is not NULL we might be
3786 // assigning another function to the same var, the type is checked
3787 // below.
3788 if (new_var && function_exists(name, FALSE))
3789 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00003790 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003791 name);
3792 return TRUE;
3793 }
3794 return FALSE;
3795}
3796
3797/*
Bram Moolenaara187c432020-09-16 21:08:28 +02003798 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
3799 * value. Also give an error message, using "name" or _("name") when
3800 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003801 */
3802 int
Bram Moolenaara187c432020-09-16 21:08:28 +02003803value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003804{
3805 if (lock & VAR_LOCKED)
3806 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003807 if (name == NULL)
3808 emsg(_(e_value_is_locked));
3809 else
3810 semsg(_(e_value_is_locked_str),
3811 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003812 return TRUE;
3813 }
3814 if (lock & VAR_FIXED)
3815 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00003816 if (name == NULL)
3817 emsg(_(e_cannot_change_value));
3818 else
3819 semsg(_(e_cannot_change_value_of_str),
3820 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003821 return TRUE;
3822 }
3823 return FALSE;
3824}
3825
3826/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01003827 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003828 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003829 * Return FALSE and give an error if not.
3830 */
3831 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003832valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003833{
3834 char_u *p;
3835
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00003836 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003837 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01003838 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003839 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003840 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003841 return FALSE;
3842 }
3843 return TRUE;
3844}
3845
3846/*
3847 * getwinvar() and gettabwinvar()
3848 */
3849 static void
3850getwinvar(
3851 typval_T *argvars,
3852 typval_T *rettv,
3853 int off) // 1 for gettabwinvar()
3854{
3855 win_T *win;
3856 char_u *varname;
3857 dictitem_T *v;
3858 tabpage_T *tp = NULL;
3859 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003860 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003861 int need_switch_win;
3862
3863 if (off == 1)
3864 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3865 else
3866 tp = curtab;
3867 win = find_win_by_nr(&argvars[off], tp);
3868 varname = tv_get_string_chk(&argvars[off + 1]);
3869 ++emsg_off;
3870
3871 rettv->v_type = VAR_STRING;
3872 rettv->vval.v_string = NULL;
3873
3874 if (win != NULL && varname != NULL)
3875 {
3876 // Set curwin to be our win, temporarily. Also set the tabpage,
3877 // otherwise the window is not valid. Only do this when needed,
3878 // autocommands get blocked.
3879 need_switch_win = !(tp == curtab && win == curwin);
3880 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003881 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003882 {
3883 if (*varname == '&')
3884 {
3885 if (varname[1] == NUL)
3886 {
3887 // get all window-local options in a dict
3888 dict_T *opts = get_winbuf_options(FALSE);
3889
3890 if (opts != NULL)
3891 {
3892 rettv_dict_set(rettv, opts);
3893 done = TRUE;
3894 }
3895 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003896 else if (eval_option(&varname, rettv, 1) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003897 // window-local-option
3898 done = TRUE;
3899 }
3900 else
3901 {
3902 // Look up the variable.
3903 // Let getwinvar({nr}, "") return the "w:" dictionary.
3904 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
3905 varname, FALSE);
3906 if (v != NULL)
3907 {
3908 copy_tv(&v->di_tv, rettv);
3909 done = TRUE;
3910 }
3911 }
3912 }
3913
3914 if (need_switch_win)
3915 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00003916 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003917 }
3918
3919 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
3920 // use the default return value
3921 copy_tv(&argvars[off + 2], rettv);
3922
3923 --emsg_off;
3924}
3925
3926/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02003927 * Set option "varname" to the value of "varp" for the current buffer/window.
3928 */
3929 static void
3930set_option_from_tv(char_u *varname, typval_T *varp)
3931{
3932 long numval = 0;
3933 char_u *strval;
3934 char_u nbuf[NUMBUFLEN];
3935 int error = FALSE;
3936
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003937 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003938 {
Bram Moolenaar31a201a2021-01-03 14:47:25 +01003939 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01003940 strval = (char_u *)"0"; // avoid using "false"
3941 }
3942 else
3943 {
3944 if (!in_vim9script() || varp->v_type != VAR_STRING)
3945 numval = (long)tv_get_number_chk(varp, &error);
3946 strval = tv_get_string_buf_chk(varp, nbuf);
3947 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02003948 if (!error && strval != NULL)
3949 set_option_value(varname, numval, strval, OPT_LOCAL);
3950}
3951
3952/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003953 * "setwinvar()" and "settabwinvar()" functions
3954 */
3955 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01003956setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003957{
3958 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00003959 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003960 int need_switch_win;
3961 char_u *varname, *winvarname;
3962 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003963 tabpage_T *tp = NULL;
3964
3965 if (check_secure())
3966 return;
3967
3968 if (off == 1)
3969 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
3970 else
3971 tp = curtab;
3972 win = find_win_by_nr(&argvars[off], tp);
3973 varname = tv_get_string_chk(&argvars[off + 1]);
3974 varp = &argvars[off + 2];
3975
3976 if (win != NULL && varname != NULL && varp != NULL)
3977 {
3978 need_switch_win = !(tp == curtab && win == curwin);
3979 if (!need_switch_win
Bram Moolenaar18f47402022-01-06 13:24:51 +00003980 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003981 {
3982 if (*varname == '&')
Bram Moolenaar191929b2020-08-19 21:20:49 +02003983 set_option_from_tv(varname + 1, varp);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003984 else
3985 {
3986 winvarname = alloc(STRLEN(varname) + 3);
3987 if (winvarname != NULL)
3988 {
3989 STRCPY(winvarname, "w:");
3990 STRCPY(winvarname + 2, varname);
3991 set_var(winvarname, varp, TRUE);
3992 vim_free(winvarname);
3993 }
3994 }
3995 }
3996 if (need_switch_win)
Bram Moolenaar18f47402022-01-06 13:24:51 +00003997 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003998 }
3999}
4000
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004001/*
4002 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4003 * v:option_type, and v:option_command.
4004 */
4005 void
4006reset_v_option_vars(void)
4007{
4008 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4009 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4010 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4011 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4012 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4013 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4014}
4015
4016/*
4017 * Add an assert error to v:errors.
4018 */
4019 void
4020assert_error(garray_T *gap)
4021{
4022 struct vimvar *vp = &vimvars[VV_ERRORS];
4023
Bram Moolenaard787e402021-12-24 21:36:12 +00004024 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004025 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004026 set_vim_var_list(VV_ERRORS, list_alloc());
4027 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4028}
4029
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004030 int
4031var_exists(char_u *var)
4032{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004033 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004034 char_u *name;
4035 char_u *tofree;
4036 typval_T tv;
4037 int len = 0;
4038 int n = FALSE;
4039
4040 // get_name_len() takes care of expanding curly braces
4041 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004042 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004043 if (len > 0)
4044 {
4045 if (tofree != NULL)
4046 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004047 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004048 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004049 if (n)
4050 {
4051 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004052 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004053 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4054 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004055 if (n)
4056 clear_tv(&tv);
4057 }
4058 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004059 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004060 n = FALSE;
4061
4062 vim_free(tofree);
4063 return n;
4064}
4065
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004066static lval_T *redir_lval = NULL;
4067#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4068static garray_T redir_ga; // only valid when redir_lval is not NULL
4069static char_u *redir_endp = NULL;
4070static char_u *redir_varname = NULL;
4071
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004072 int
4073alloc_redir_lval(void)
4074{
4075 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4076 if (redir_lval == NULL)
4077 return FAIL;
4078 return OK;
4079}
4080
4081 void
4082clear_redir_lval(void)
4083{
4084 VIM_CLEAR(redir_lval);
4085}
4086
4087 void
4088init_redir_ga(void)
4089{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004090 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004091}
4092
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004093/*
4094 * Start recording command output to a variable
4095 * When "append" is TRUE append to an existing variable.
4096 * Returns OK if successfully completed the setup. FAIL otherwise.
4097 */
4098 int
4099var_redir_start(char_u *name, int append)
4100{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004101 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004102 typval_T tv;
4103
4104 // Catch a bad name early.
4105 if (!eval_isnamec1(*name))
4106 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004107 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004108 return FAIL;
4109 }
4110
4111 // Make a copy of the name, it is used in redir_lval until redir ends.
4112 redir_varname = vim_strsave(name);
4113 if (redir_varname == NULL)
4114 return FAIL;
4115
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004116 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004117 {
4118 var_redir_stop();
4119 return FAIL;
4120 }
4121
4122 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004123 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004124
4125 // Parse the variable name (can be a dict or list entry).
4126 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4127 FNE_CHECK_START);
4128 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4129 {
4130 clear_lval(redir_lval);
4131 if (redir_endp != NULL && *redir_endp != NUL)
4132 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004133 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004134 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004135 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004136 redir_endp = NULL; // don't store a value, only cleanup
4137 var_redir_stop();
4138 return FAIL;
4139 }
4140
4141 // check if we can write to the variable: set it to or append an empty
4142 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004143 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004144 tv.v_type = VAR_STRING;
4145 tv.vval.v_string = (char_u *)"";
4146 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004147 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004148 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004149 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004150 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004151 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004152 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004153 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004154 {
4155 redir_endp = NULL; // don't store a value, only cleanup
4156 var_redir_stop();
4157 return FAIL;
4158 }
4159
4160 return OK;
4161}
4162
4163/*
4164 * Append "value[value_len]" to the variable set by var_redir_start().
4165 * The actual appending is postponed until redirection ends, because the value
4166 * appended may in fact be the string we write to, changing it may cause freed
4167 * memory to be used:
4168 * :redir => foo
4169 * :let foo
4170 * :redir END
4171 */
4172 void
4173var_redir_str(char_u *value, int value_len)
4174{
4175 int len;
4176
4177 if (redir_lval == NULL)
4178 return;
4179
4180 if (value_len == -1)
4181 len = (int)STRLEN(value); // Append the entire string
4182 else
4183 len = value_len; // Append only "value_len" characters
4184
4185 if (ga_grow(&redir_ga, len) == OK)
4186 {
4187 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4188 redir_ga.ga_len += len;
4189 }
4190 else
4191 var_redir_stop();
4192}
4193
4194/*
4195 * Stop redirecting command output to a variable.
4196 * Frees the allocated memory.
4197 */
4198 void
4199var_redir_stop(void)
4200{
4201 typval_T tv;
4202
4203 if (EVALCMD_BUSY)
4204 {
4205 redir_lval = NULL;
4206 return;
4207 }
4208
4209 if (redir_lval != NULL)
4210 {
4211 // If there was no error: assign the text to the variable.
4212 if (redir_endp != NULL)
4213 {
4214 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4215 tv.v_type = VAR_STRING;
4216 tv.vval.v_string = redir_ga.ga_data;
4217 // Call get_lval() again, if it's inside a Dict or List it may
4218 // have changed.
4219 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4220 FALSE, FALSE, 0, FNE_CHECK_START);
4221 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004222 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004223 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004224 clear_lval(redir_lval);
4225 }
4226
4227 // free the collected output
4228 VIM_CLEAR(redir_ga.ga_data);
4229
4230 VIM_CLEAR(redir_lval);
4231 }
4232 VIM_CLEAR(redir_varname);
4233}
4234
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004235/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004236 * Get the collected redirected text and clear redir_ga.
4237 */
4238 char_u *
4239get_clear_redir_ga(void)
4240{
4241 char_u *res;
4242
4243 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4244 res = redir_ga.ga_data;
4245 redir_ga.ga_data = NULL;
4246 return res;
4247}
4248
4249/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004250 * "gettabvar()" function
4251 */
4252 void
4253f_gettabvar(typval_T *argvars, typval_T *rettv)
4254{
Bram Moolenaar18f47402022-01-06 13:24:51 +00004255 switchwin_T switchwin;
4256 tabpage_T *tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004257 dictitem_T *v;
4258 char_u *varname;
4259 int done = FALSE;
4260
4261 rettv->v_type = VAR_STRING;
4262 rettv->vval.v_string = NULL;
4263
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004264 if (in_vim9script()
4265 && (check_for_number_arg(argvars, 0) == FAIL
4266 || check_for_string_arg(argvars, 1) == FAIL))
4267 return;
4268
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004269 varname = tv_get_string_chk(&argvars[1]);
4270 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4271 if (tp != NULL && varname != NULL)
4272 {
4273 // Set tp to be our tabpage, temporarily. Also set the window to the
4274 // first window in the tabpage, otherwise the window is not valid.
Bram Moolenaar18f47402022-01-06 13:24:51 +00004275 if (switch_win(&switchwin,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004276 tp == curtab || tp->tp_firstwin == NULL ? firstwin
4277 : tp->tp_firstwin, tp, TRUE) == OK)
4278 {
4279 // look up the variable
4280 // Let gettabvar({nr}, "") return the "t:" dictionary.
4281 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
4282 if (v != NULL)
4283 {
4284 copy_tv(&v->di_tv, rettv);
4285 done = TRUE;
4286 }
4287 }
4288
4289 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004290 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004291 }
4292
4293 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4294 copy_tv(&argvars[2], rettv);
4295}
4296
4297/*
4298 * "gettabwinvar()" function
4299 */
4300 void
4301f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4302{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004303 if (in_vim9script()
4304 && (check_for_number_arg(argvars, 0) == FAIL
4305 || check_for_number_arg(argvars, 1) == FAIL
4306 || check_for_string_arg(argvars, 2) == FAIL))
4307 return;
4308
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004309 getwinvar(argvars, rettv, 1);
4310}
4311
4312/*
4313 * "getwinvar()" function
4314 */
4315 void
4316f_getwinvar(typval_T *argvars, typval_T *rettv)
4317{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004318 if (in_vim9script()
4319 && (check_for_number_arg(argvars, 0) == FAIL
4320 || check_for_string_arg(argvars, 1) == FAIL))
4321 return;
4322
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004323 getwinvar(argvars, rettv, 0);
4324}
4325
4326/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004327 * "getbufvar()" function
4328 */
4329 void
4330f_getbufvar(typval_T *argvars, typval_T *rettv)
4331{
4332 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004333 char_u *varname;
4334 dictitem_T *v;
4335 int done = FALSE;
4336
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004337 if (in_vim9script()
4338 && (check_for_buffer_arg(argvars, 0) == FAIL
4339 || check_for_string_arg(argvars, 1) == FAIL))
4340 return;
4341
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004342 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004343 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004344
4345 rettv->v_type = VAR_STRING;
4346 rettv->vval.v_string = NULL;
4347
4348 if (buf != NULL && varname != NULL)
4349 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004350 if (*varname == '&')
4351 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004352 buf_T *save_curbuf = curbuf;
4353
4354 // set curbuf to be our buf, temporarily
4355 curbuf = buf;
4356
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004357 if (varname[1] == NUL)
4358 {
4359 // get all buffer-local options in a dict
4360 dict_T *opts = get_winbuf_options(TRUE);
4361
4362 if (opts != NULL)
4363 {
4364 rettv_dict_set(rettv, opts);
4365 done = TRUE;
4366 }
4367 }
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004368 else if (eval_option(&varname, rettv, TRUE) == OK)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004369 // buffer-local-option
4370 done = TRUE;
Bram Moolenaar86015452020-03-29 15:12:15 +02004371
4372 // restore previous notion of curbuf
4373 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004374 }
4375 else
4376 {
4377 // Look up the variable.
Bram Moolenaar52592752020-04-03 18:43:35 +02004378 if (*varname == NUL)
4379 // Let getbufvar({nr}, "") return the "b:" dictionary.
4380 v = &buf->b_bufvar;
4381 else
4382 v = find_var_in_ht(&buf->b_vars->dv_hashtab, 'b',
4383 varname, FALSE);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004384 if (v != NULL)
4385 {
4386 copy_tv(&v->di_tv, rettv);
4387 done = TRUE;
4388 }
4389 }
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004390 }
4391
4392 if (!done && argvars[2].v_type != VAR_UNKNOWN)
4393 // use the default value
4394 copy_tv(&argvars[2], rettv);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004395}
4396
4397/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004398 * "settabvar()" function
4399 */
4400 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004401f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004402{
4403 tabpage_T *save_curtab;
4404 tabpage_T *tp;
4405 char_u *varname, *tabvarname;
4406 typval_T *varp;
4407
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004408 if (check_secure())
4409 return;
4410
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004411 if (in_vim9script()
4412 && (check_for_number_arg(argvars, 0) == FAIL
4413 || check_for_string_arg(argvars, 1) == FAIL))
4414 return;
4415
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004416 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4417 varname = tv_get_string_chk(&argvars[1]);
4418 varp = &argvars[2];
4419
4420 if (varname != NULL && varp != NULL && tp != NULL)
4421 {
4422 save_curtab = curtab;
4423 goto_tabpage_tp(tp, FALSE, FALSE);
4424
4425 tabvarname = alloc(STRLEN(varname) + 3);
4426 if (tabvarname != NULL)
4427 {
4428 STRCPY(tabvarname, "t:");
4429 STRCPY(tabvarname + 2, varname);
4430 set_var(tabvarname, varp, TRUE);
4431 vim_free(tabvarname);
4432 }
4433
4434 // Restore current tabpage
4435 if (valid_tabpage(save_curtab))
4436 goto_tabpage_tp(save_curtab, FALSE, FALSE);
4437 }
4438}
4439
4440/*
4441 * "settabwinvar()" function
4442 */
4443 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004444f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004445{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004446 if (in_vim9script()
4447 && (check_for_number_arg(argvars, 0) == FAIL
4448 || check_for_number_arg(argvars, 1) == FAIL
4449 || check_for_string_arg(argvars, 2) == FAIL))
4450 return;
4451
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004452 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004453}
4454
4455/*
4456 * "setwinvar()" function
4457 */
4458 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004459f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004460{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004461 if (in_vim9script()
4462 && (check_for_number_arg(argvars, 0) == FAIL
4463 || check_for_string_arg(argvars, 1) == FAIL))
4464 return;
4465
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004466 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004467}
4468
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004469/*
4470 * "setbufvar()" function
4471 */
4472 void
4473f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
4474{
4475 buf_T *buf;
4476 char_u *varname, *bufvarname;
4477 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004478
4479 if (check_secure())
4480 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004481
4482 if (in_vim9script()
4483 && (check_for_buffer_arg(argvars, 0) == FAIL
4484 || check_for_string_arg(argvars, 1) == FAIL))
4485 return;
4486
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004487 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004488 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004489 varp = &argvars[2];
4490
4491 if (buf != NULL && varname != NULL && varp != NULL)
4492 {
4493 if (*varname == '&')
4494 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004495 aco_save_T aco;
4496
4497 // set curbuf to be our buf, temporarily
4498 aucmd_prepbuf(&aco, buf);
4499
Bram Moolenaar191929b2020-08-19 21:20:49 +02004500 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004501
4502 // reset notion of buffer
4503 aucmd_restbuf(&aco);
4504 }
4505 else
4506 {
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004507 bufvarname = alloc(STRLEN(varname) + 3);
4508 if (bufvarname != NULL)
4509 {
Bram Moolenaar86015452020-03-29 15:12:15 +02004510 buf_T *save_curbuf = curbuf;
4511
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004512 curbuf = buf;
4513 STRCPY(bufvarname, "b:");
4514 STRCPY(bufvarname + 2, varname);
4515 set_var(bufvarname, varp, TRUE);
4516 vim_free(bufvarname);
4517 curbuf = save_curbuf;
4518 }
4519 }
4520 }
4521}
4522
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004523/*
4524 * Get a callback from "arg". It can be a Funcref or a function name.
4525 * When "arg" is zero return an empty string.
4526 * "cb_name" is not allocated.
4527 * "cb_name" is set to NULL for an invalid argument.
4528 */
4529 callback_T
4530get_callback(typval_T *arg)
4531{
Bram Moolenaar14e579092020-03-07 16:59:25 +01004532 callback_T res;
4533 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004534
4535 res.cb_free_name = FALSE;
4536 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
4537 {
4538 res.cb_partial = arg->vval.v_partial;
4539 ++res.cb_partial->pt_refcount;
4540 res.cb_name = partial_name(res.cb_partial);
4541 }
4542 else
4543 {
4544 res.cb_partial = NULL;
Bram Moolenaar14e579092020-03-07 16:59:25 +01004545 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
4546 && isdigit(*arg->vval.v_string))
4547 r = FAIL;
4548 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004549 {
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00004550 if (arg->v_type == VAR_STRING)
4551 {
4552 char_u *name;
4553
4554 name = get_scriptlocal_funcname(arg->vval.v_string);
4555 if (name != NULL)
4556 {
4557 vim_free(arg->vval.v_string);
4558 arg->vval.v_string = name;
4559 }
4560 }
4561
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004562 res.cb_name = arg->vval.v_string;
4563 func_ref(res.cb_name);
4564 }
4565 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004566 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004567 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01004568 r = FAIL;
4569
4570 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004571 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00004572 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004573 res.cb_name = NULL;
4574 }
4575 }
4576 return res;
4577}
4578
4579/*
4580 * Copy a callback into a typval_T.
4581 */
4582 void
4583put_callback(callback_T *cb, typval_T *tv)
4584{
4585 if (cb->cb_partial != NULL)
4586 {
4587 tv->v_type = VAR_PARTIAL;
4588 tv->vval.v_partial = cb->cb_partial;
4589 ++tv->vval.v_partial->pt_refcount;
4590 }
4591 else
4592 {
4593 tv->v_type = VAR_FUNC;
4594 tv->vval.v_string = vim_strsave(cb->cb_name);
4595 func_ref(cb->cb_name);
4596 }
4597}
4598
4599/*
4600 * Make a copy of "src" into "dest", allocating the function name if needed,
4601 * without incrementing the refcount.
4602 */
4603 void
4604set_callback(callback_T *dest, callback_T *src)
4605{
4606 if (src->cb_partial == NULL)
4607 {
4608 // just a function name, make a copy
4609 dest->cb_name = vim_strsave(src->cb_name);
4610 dest->cb_free_name = TRUE;
4611 }
4612 else
4613 {
4614 // cb_name is a pointer into cb_partial
4615 dest->cb_name = src->cb_name;
4616 dest->cb_free_name = FALSE;
4617 }
4618 dest->cb_partial = src->cb_partial;
4619}
4620
4621/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02004622 * Copy callback from "src" to "dest", incrementing the refcounts.
4623 */
4624 void
4625copy_callback(callback_T *dest, callback_T *src)
4626{
4627 dest->cb_partial = src->cb_partial;
4628 if (dest->cb_partial != NULL)
4629 {
4630 dest->cb_name = src->cb_name;
4631 dest->cb_free_name = FALSE;
4632 ++dest->cb_partial->pt_refcount;
4633 }
4634 else
4635 {
4636 dest->cb_name = vim_strsave(src->cb_name);
4637 dest->cb_free_name = TRUE;
4638 func_ref(src->cb_name);
4639 }
4640}
4641
4642/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00004643 * When a callback refers to an autoload import, change the function name to
4644 * the "path#name" form. Uses the current script context.
4645 * Only works when the name is allocated.
4646 */
4647 void
4648expand_autload_callback(callback_T *cb)
4649{
4650 char_u *p;
4651 imported_T *import;
4652
4653 if (!in_vim9script() || cb->cb_name == NULL || !cb->cb_free_name)
4654 return;
4655 p = vim_strchr(cb->cb_name, '.');
4656 if (p == NULL)
4657 return;
4658 import = find_imported(cb->cb_name, p - cb->cb_name, FALSE, NULL);
4659 if (import != NULL && SCRIPT_ID_VALID(import->imp_sid))
4660 {
4661 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
4662
4663 if (si->sn_autoload_prefix != NULL)
4664 {
4665 char_u *name = concat_str(si->sn_autoload_prefix, p + 1);
4666
4667 if (name != NULL)
4668 {
4669 vim_free(cb->cb_name);
4670 cb->cb_name = name;
4671 }
4672 }
4673 }
4674}
4675
4676/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02004677 * Unref/free "callback" returned by get_callback() or set_callback().
4678 */
4679 void
4680free_callback(callback_T *callback)
4681{
4682 if (callback->cb_partial != NULL)
4683 {
4684 partial_unref(callback->cb_partial);
4685 callback->cb_partial = NULL;
4686 }
4687 else if (callback->cb_name != NULL)
4688 func_unref(callback->cb_name);
4689 if (callback->cb_free_name)
4690 {
4691 vim_free(callback->cb_name);
4692 callback->cb_free_name = FALSE;
4693 }
4694 callback->cb_name = NULL;
4695}
4696
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004697#endif // FEAT_EVAL