blob: 1b11f2847d17a6fc0bbc74196a008a46e0c1d36b [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},
Shougo Matsushita61021aa2022-07-27 14:40:00 +0100114 {VV_NAME("completed_item", VAR_DICT), &t_dict_string, 0},
Bram Moolenaard787e402021-12-24 21:36:12 +0000115 {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},
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000142 {VV_NAME("t_class", VAR_NUMBER), NULL, VV_RO},
143 {VV_NAME("t_object", VAR_NUMBER), NULL, VV_RO},
Bram Moolenaard787e402021-12-24 21:36:12 +0000144 {VV_NAME("termrfgresp", VAR_STRING), NULL, VV_RO},
145 {VV_NAME("termrbgresp", VAR_STRING), NULL, VV_RO},
146 {VV_NAME("termu7resp", VAR_STRING), NULL, VV_RO},
147 {VV_NAME("termstyleresp", VAR_STRING), NULL, VV_RO},
148 {VV_NAME("termblinkresp", VAR_STRING), NULL, VV_RO},
149 {VV_NAME("event", VAR_DICT), NULL, VV_RO},
150 {VV_NAME("versionlong", VAR_NUMBER), NULL, VV_RO},
151 {VV_NAME("echospace", VAR_NUMBER), NULL, VV_RO},
152 {VV_NAME("argv", VAR_LIST), &t_list_string, VV_RO},
153 {VV_NAME("collate", VAR_STRING), NULL, VV_RO},
154 {VV_NAME("exiting", VAR_SPECIAL), NULL, VV_RO},
155 {VV_NAME("colornames", VAR_DICT), &t_dict_string, VV_RO},
156 {VV_NAME("sizeofint", VAR_NUMBER), NULL, VV_RO},
157 {VV_NAME("sizeoflong", VAR_NUMBER), NULL, VV_RO},
158 {VV_NAME("sizeofpointer", VAR_NUMBER), NULL, VV_RO},
naohiro ono56200ee2022-01-01 14:59:44 +0000159 {VV_NAME("maxcol", VAR_NUMBER), NULL, VV_RO},
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200160 {VV_NAME("python3_version", VAR_NUMBER), NULL, VV_RO},
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200161 {VV_NAME("t_typealias", VAR_NUMBER), NULL, VV_RO},
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100162 {VV_NAME("t_enum", VAR_NUMBER), NULL, VV_RO},
ichizok663d18d2025-01-02 18:06:00 +0100163 {VV_NAME("t_enumvalue", VAR_NUMBER), NULL, VV_RO},
zeertzjq6655bef2025-01-06 18:32:13 +0100164 {VV_NAME("stacktrace", VAR_LIST), &t_list_dict_any, VV_RO},
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200165};
166
167// shorthand
Bram Moolenaard787e402021-12-24 21:36:12 +0000168#define vv_tv_type vv_di.di_tv.v_type
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200169#define vv_nr vv_di.di_tv.vval.v_number
170#define vv_float vv_di.di_tv.vval.v_float
171#define vv_str vv_di.di_tv.vval.v_string
172#define vv_list vv_di.di_tv.vval.v_list
173#define vv_dict vv_di.di_tv.vval.v_dict
174#define vv_blob vv_di.di_tv.vval.v_blob
175#define vv_tv vv_di.di_tv
176
177static dictitem_T vimvars_var; // variable used for v:
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200178static dict_T vimvardict; // Dictionary with v: variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200179#define vimvarht vimvardict.dv_hashtab
180
181// for VIM_VERSION_ defines
182#include "version.h"
183
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200184static void list_glob_vars(int *first);
185static void list_buf_vars(int *first);
186static void list_win_vars(int *first);
187static void list_tab_vars(int *first);
188static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100189static 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 +0200190static int do_unlet_var(lval_T *lp, char_u *name_end, exarg_T *eap, int deep, void *cookie);
191static 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 +0200192static void list_one_var(dictitem_T *v, char *prefix, int *first);
193static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
194
195/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200196 * Initialize global and vim special variables
197 */
198 void
199evalvars_init(void)
200{
201 int i;
202 struct vimvar *p;
203
204 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
205 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
206 vimvardict.dv_lock = VAR_FIXED;
207 hash_init(&compat_hashtab);
208
209 for (i = 0; i < VV_LEN; ++i)
210 {
211 p = &vimvars[i];
212 if (STRLEN(p->vv_name) > DICTITEM16_KEY_LEN)
213 {
Bram Moolenaar097c5372023-05-24 21:02:24 +0100214 iemsg("Name too long, increase size of dictitem16_T");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200215 getout(1);
216 }
217 STRCPY(p->vv_di.di_key, p->vv_name);
218 if (p->vv_flags & VV_RO)
219 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
220 else if (p->vv_flags & VV_RO_SBX)
221 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
222 else
223 p->vv_di.di_flags = DI_FLAGS_FIX;
224
225 // add to v: scope dict, unless the value is not always available
Bram Moolenaard787e402021-12-24 21:36:12 +0000226 if (p->vv_tv_type != VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000227 hash_add(&vimvarht, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200228 if (p->vv_flags & VV_COMPAT)
229 // add to compat scope dict
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000230 hash_add(&compat_hashtab, p->vv_di.di_key, "initialization");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200231 }
Bram Moolenaar016faaa2020-10-03 12:57:27 +0200232 set_vim_var_nr(VV_VERSION, VIM_VERSION_100);
233 set_vim_var_nr(VV_VERSIONLONG, VIM_VERSION_100 * 10000 + highest_patch());
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200234
235 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
236 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaarf0068c52020-11-30 17:42:10 +0100237 set_vim_var_nr(VV_EXITING, VVAL_NULL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200238 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
239 set_vim_var_list(VV_ERRORS, list_alloc());
240 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
241
242 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
243 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
244 set_vim_var_nr(VV_NONE, VVAL_NONE);
245 set_vim_var_nr(VV_NULL, VVAL_NULL);
Bram Moolenaar57d5a012021-01-21 21:42:31 +0100246 set_vim_var_nr(VV_NUMBERMAX, VARNUM_MAX);
247 set_vim_var_nr(VV_NUMBERMIN, VARNUM_MIN);
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100248 set_vim_var_nr(VV_NUMBERSIZE, sizeof(varnumber_T) * 8);
Bram Moolenaar69b30722021-11-02 21:39:49 +0000249 set_vim_var_nr(VV_SIZEOFINT, sizeof(int));
250 set_vim_var_nr(VV_SIZEOFLONG, sizeof(long));
251 set_vim_var_nr(VV_SIZEOFPOINTER, sizeof(char *));
naohiro ono56200ee2022-01-01 14:59:44 +0000252 set_vim_var_nr(VV_MAXCOL, MAXCOL);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200253
254 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
255 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
256 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
257 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
258 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
259 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
260 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
261 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
262 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
263 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
264 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarc0c2c262023-01-12 21:08:53 +0000265 set_vim_var_nr(VV_TYPE_CLASS, VAR_TYPE_CLASS);
266 set_vim_var_nr(VV_TYPE_OBJECT, VAR_TYPE_OBJECT);
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200267 set_vim_var_nr(VV_TYPE_TYPEALIAS, VAR_TYPE_TYPEALIAS);
Yegappan Lakshmanan3164cf82024-03-28 10:36:42 +0100268 set_vim_var_nr(VV_TYPE_ENUM, VAR_TYPE_ENUM);
269 set_vim_var_nr(VV_TYPE_ENUMVALUE, VAR_TYPE_ENUMVALUE);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200270
271 set_vim_var_nr(VV_ECHOSPACE, sc_col - 1);
272
Drew Vogele30d1022021-10-24 20:35:07 +0100273 set_vim_var_dict(VV_COLORNAMES, dict_alloc());
274
Yee Cheng Chinc13b3d12023-08-20 21:18:38 +0200275#ifdef FEAT_PYTHON3
276 set_vim_var_nr(VV_PYTHON3_VERSION, python3_version());
277#endif
278
Bram Moolenaar439c0362020-06-06 15:58:03 +0200279 // Default for v:register is not 0 but '"'. This is adjusted once the
280 // clipboard has been setup by calling reset_reg_var().
281 set_reg_var(0);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200282}
283
284#if defined(EXITFREE) || defined(PROTO)
285/*
286 * Free all vim variables information on exit
287 */
288 void
289evalvars_clear(void)
290{
291 int i;
292 struct vimvar *p;
293
294 for (i = 0; i < VV_LEN; ++i)
295 {
296 p = &vimvars[i];
297 if (p->vv_di.di_tv.v_type == VAR_STRING)
298 VIM_CLEAR(p->vv_str);
299 else if (p->vv_di.di_tv.v_type == VAR_LIST)
300 {
301 list_unref(p->vv_list);
302 p->vv_list = NULL;
303 }
304 }
305 hash_clear(&vimvarht);
306 hash_init(&vimvarht); // garbage_collect() will access it
307 hash_clear(&compat_hashtab);
308
309 // global variables
310 vars_clear(&globvarht);
311
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100312 // Script-local variables. Clear all the variables here.
313 // The scriptvar_T is cleared later in free_scriptnames(), because a
314 // variable in one script might hold a reference to the whole scope of
315 // another script.
316 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200317 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200318}
319#endif
320
321 int
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200322garbage_collect_globvars(int copyID)
323{
324 return set_ref_in_ht(&globvarht, copyID, NULL);
325}
326
327 int
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200328garbage_collect_vimvars(int copyID)
329{
330 return set_ref_in_ht(&vimvarht, copyID, NULL);
331}
332
333 int
334garbage_collect_scriptvars(int copyID)
335{
Bram Moolenaared234f22020-10-15 20:42:20 +0200336 int i;
337 int idx;
338 int abort = FALSE;
339 scriptitem_T *si;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200340
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100341 for (i = 1; i <= script_items.ga_len; ++i)
Bram Moolenaared234f22020-10-15 20:42:20 +0200342 {
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200343 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
344
Bram Moolenaared234f22020-10-15 20:42:20 +0200345 si = SCRIPT_ITEM(i);
346 for (idx = 0; idx < si->sn_var_vals.ga_len; ++idx)
347 {
348 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
349
Bram Moolenaard00a7fb2021-03-08 20:47:14 +0100350 if (sv->sv_name != NULL)
351 abort = abort || set_ref_in_item(sv->sv_tv, copyID, NULL, NULL);
Bram Moolenaared234f22020-10-15 20:42:20 +0200352 }
353 }
354
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200355 return abort;
356}
357
358/*
359 * Set an internal variable to a string value. Creates the variable if it does
360 * not already exist.
361 */
362 void
363set_internal_string_var(char_u *name, char_u *value)
364{
365 char_u *val;
366 typval_T *tvp;
367
368 val = vim_strsave(value);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000369 if (val == NULL)
370 return;
371
372 tvp = alloc_string_tv(val);
373 if (tvp == NULL)
374 return;
375
376 set_var(name, tvp, FALSE);
377 free_tv(tvp);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200378}
379
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200380 int
381eval_charconvert(
382 char_u *enc_from,
383 char_u *enc_to,
384 char_u *fname_from,
385 char_u *fname_to)
386{
387 int err = FALSE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000388 sctx_T saved_sctx = current_sctx;
389 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200390
391 set_vim_var_string(VV_CC_FROM, enc_from, -1);
392 set_vim_var_string(VV_CC_TO, enc_to, -1);
393 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
394 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000395 ctx = get_option_sctx("charconvert");
396 if (ctx != NULL)
397 current_sctx = *ctx;
398
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100399 if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200400 err = TRUE;
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000401
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200402 set_vim_var_string(VV_CC_FROM, NULL, -1);
403 set_vim_var_string(VV_CC_TO, NULL, -1);
404 set_vim_var_string(VV_FNAME_IN, NULL, -1);
405 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaarf4e88f22022-01-23 14:17:28 +0000406 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200407
408 if (err)
409 return FAIL;
410 return OK;
411}
412
413# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
414 int
415eval_printexpr(char_u *fname, char_u *args)
416{
417 int err = FALSE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000418 sctx_T saved_sctx = current_sctx;
419 sctx_T *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200420
421 set_vim_var_string(VV_FNAME_IN, fname, -1);
422 set_vim_var_string(VV_CMDARG, args, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000423 ctx = get_option_sctx("printexpr");
424 if (ctx != NULL)
425 current_sctx = *ctx;
426
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100427 if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200428 err = TRUE;
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000429
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200430 set_vim_var_string(VV_FNAME_IN, NULL, -1);
431 set_vim_var_string(VV_CMDARG, NULL, -1);
Bram Moolenaar7ef4a2f2022-01-23 13:44:35 +0000432 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200433
434 if (err)
435 {
436 mch_remove(fname);
437 return FAIL;
438 }
439 return OK;
440}
441# endif
442
443# if defined(FEAT_DIFF) || defined(PROTO)
444 void
445eval_diff(
446 char_u *origfile,
447 char_u *newfile,
448 char_u *outfile)
449{
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000450 sctx_T saved_sctx = current_sctx;
451 sctx_T *ctx;
452 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200453
454 set_vim_var_string(VV_FNAME_IN, origfile, -1);
455 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
456 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000457
458 ctx = get_option_sctx("diffexpr");
459 if (ctx != NULL)
460 current_sctx = *ctx;
461
462 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100463 tv = eval_expr_ext(p_dex, NULL, TRUE);
Bram Moolenaar39b89442022-01-22 18:21:36 +0000464 free_tv(tv);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000465
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200466 set_vim_var_string(VV_FNAME_IN, NULL, -1);
467 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
468 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar7b29f6a2022-01-22 17:58:13 +0000469 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200470}
471
472 void
473eval_patch(
474 char_u *origfile,
475 char_u *difffile,
476 char_u *outfile)
477{
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000478 sctx_T saved_sctx = current_sctx;
479 sctx_T *ctx;
480 typval_T *tv;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200481
482 set_vim_var_string(VV_FNAME_IN, origfile, -1);
483 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
484 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000485
486 ctx = get_option_sctx("patchexpr");
487 if (ctx != NULL)
488 current_sctx = *ctx;
489
490 // errors are ignored
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100491 tv = eval_expr_ext(p_pex, NULL, TRUE);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000492 free_tv(tv);
493
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200494 set_vim_var_string(VV_FNAME_IN, NULL, -1);
495 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
496 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
Bram Moolenaar36c2add2022-01-22 20:55:30 +0000497 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200498}
499# endif
500
501#if defined(FEAT_SPELL) || defined(PROTO)
502/*
503 * Evaluate an expression to a list with suggestions.
504 * For the "expr:" part of 'spellsuggest'.
505 * Returns NULL when there is an error.
506 */
507 list_T *
508eval_spell_expr(char_u *badword, char_u *expr)
509{
510 typval_T save_val;
511 typval_T rettv;
512 list_T *list = NULL;
513 char_u *p = skipwhite(expr);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000514 sctx_T saved_sctx = current_sctx;
515 sctx_T *ctx;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100516 int r;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200517
518 // Set "v:val" to the bad word.
519 prepare_vimvar(VV_VAL, &save_val);
520 set_vim_var_string(VV_VAL, badword, -1);
521 if (p_verbose == 0)
522 ++emsg_off;
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000523 ctx = get_option_sctx("spellsuggest");
524 if (ctx != NULL)
525 current_sctx = *ctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200526
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100527 r = may_call_simple_func(p, &rettv);
528 if (r == NOTDONE)
529 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
530 if (r == OK)
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200531 {
532 if (rettv.v_type != VAR_LIST)
533 clear_tv(&rettv);
534 else
535 list = rettv.vval.v_list;
536 }
537
538 if (p_verbose == 0)
539 --emsg_off;
540 clear_tv(get_vim_var_tv(VV_VAL));
541 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar2a7aa832022-01-23 17:59:06 +0000542 current_sctx = saved_sctx;
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200543
544 return list;
545}
546
547/*
548 * "list" is supposed to contain two items: a word and a number. Return the
549 * word in "pp" and the number as the return value.
550 * Return -1 if anything isn't right.
551 * Used to get the good word and score from the eval_spell_expr() result.
552 */
553 int
554get_spellword(list_T *list, char_u **pp)
555{
556 listitem_T *li;
557
558 li = list->lv_first;
559 if (li == NULL)
560 return -1;
561 *pp = tv_get_string(&li->li_tv);
562
563 li = li->li_next;
564 if (li == NULL)
565 return -1;
566 return (int)tv_get_number(&li->li_tv);
567}
568#endif
569
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200570/*
571 * Prepare v: variable "idx" to be used.
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200572 * Save the current typeval in "save_tv" and clear it.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200573 * When not used yet add the variable to the v: hashtable.
574 */
575 void
576prepare_vimvar(int idx, typval_T *save_tv)
577{
578 *save_tv = vimvars[idx].vv_tv;
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200579 vimvars[idx].vv_str = NULL; // don't free it now
Bram Moolenaard787e402021-12-24 21:36:12 +0000580 if (vimvars[idx].vv_tv_type == VAR_UNKNOWN)
Bram Moolenaaref2c3252022-11-25 16:31:51 +0000581 hash_add(&vimvarht, vimvars[idx].vv_di.di_key, "prepare vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200582}
583
584/*
585 * Restore v: variable "idx" to typeval "save_tv".
Bram Moolenaar27da7de2019-09-03 17:13:37 +0200586 * Note that the v: variable must have been cleared already.
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200587 * When no longer defined, remove the variable from the v: hashtable.
588 */
589 void
590restore_vimvar(int idx, typval_T *save_tv)
591{
592 hashitem_T *hi;
593
594 vimvars[idx].vv_tv = *save_tv;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +0000595 if (vimvars[idx].vv_tv_type != VAR_UNKNOWN)
596 return;
597
598 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
599 if (HASHITEM_EMPTY(hi))
600 internal_error("restore_vimvar()");
601 else
602 hash_remove(&vimvarht, hi, "restore vimvar");
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200603}
604
605/*
606 * List Vim variables.
607 */
608 static void
609list_vim_vars(int *first)
610{
611 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
612}
613
614/*
615 * List script-local variables, if there is a script.
616 */
617 static void
618list_script_vars(int *first)
619{
Bram Moolenaare3d46852020-08-29 13:39:17 +0200620 if (SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200621 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
622 "s:", FALSE, first);
623}
624
625/*
Bram Moolenaar9510d222022-09-11 15:14:05 +0100626 * Return TRUE if "name" starts with "g:", "w:", "t:" or "b:".
627 * But only when an identifier character follows.
628 */
629 int
630is_scoped_variable(char_u *name)
631{
632 return vim_strchr((char_u *)"gwbt", name[0]) != NULL
633 && name[1] == ':'
634 && eval_isnamec(name[2]);
635}
636
637/*
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100638 * Evaluate one Vim expression {expr} in string "p" and append the
639 * resulting string to "gap". "p" points to the opening "{".
Bram Moolenaar70c41242022-05-10 18:11:43 +0100640 * When "evaluate" is FALSE only skip over the expression.
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100641 * Return a pointer to the character after "}", NULL for an error.
642 */
643 char_u *
Bram Moolenaar70c41242022-05-10 18:11:43 +0100644eval_one_expr_in_str(char_u *p, garray_T *gap, int evaluate)
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100645{
646 char_u *block_start = skipwhite(p + 1); // skip the opening {
647 char_u *block_end = block_start;
648 char_u *expr_val;
649
650 if (*block_start == NUL)
651 {
652 semsg(_(e_missing_close_curly_str), p);
653 return NULL;
654 }
655 if (skip_expr(&block_end, NULL) == FAIL)
656 return NULL;
657 block_end = skipwhite(block_end);
658 if (*block_end != '}')
659 {
660 semsg(_(e_missing_close_curly_str), p);
661 return NULL;
662 }
Bram Moolenaar70c41242022-05-10 18:11:43 +0100663 if (evaluate)
664 {
665 *block_end = NUL;
Yegappan Lakshmananbce51d92024-04-15 19:19:52 +0200666 expr_val = eval_to_string(block_start, FALSE, FALSE);
Bram Moolenaar70c41242022-05-10 18:11:43 +0100667 *block_end = '}';
668 if (expr_val == NULL)
669 return NULL;
670 ga_concat(gap, expr_val);
671 vim_free(expr_val);
672 }
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100673
674 return block_end + 1;
675}
676
677/*
678 * Evaluate all the Vim expressions {expr} in "str" and return the resulting
679 * string in allocated memory. "{{" is reduced to "{" and "}}" to "}".
680 * Used for a heredoc assignment.
681 * Returns NULL for an error.
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100682 */
Yegappan Lakshmananee47eac2022-06-29 12:55:36 +0100683 static char_u *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100684eval_all_expr_in_str(char_u *str)
685{
686 garray_T ga;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100687 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100688
689 ga_init2(&ga, 1, 80);
690 p = str;
691
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100692 while (*p != NUL)
693 {
LemonBoy2eaef102022-05-06 13:14:50 +0100694 char_u *lit_start;
LemonBoy2eaef102022-05-06 13:14:50 +0100695 int escaped_brace = FALSE;
696
697 // Look for a block start.
698 lit_start = p;
699 while (*p != '{' && *p != '}' && *p != NUL)
700 ++p;
701
702 if (*p != NUL && *p == p[1])
703 {
704 // Escaped brace, unescape and continue.
705 // Include the brace in the literal string.
706 ++p;
707 escaped_brace = TRUE;
708 }
709 else if (*p == '}')
710 {
711 semsg(_(e_stray_closing_curly_str), str);
712 ga_clear(&ga);
713 return NULL;
714 }
715
716 // Append the literal part.
717 ga_concat_len(&ga, lit_start, (size_t)(p - lit_start));
718
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100719 if (*p == NUL)
LemonBoy2eaef102022-05-06 13:14:50 +0100720 break;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100721
LemonBoy2eaef102022-05-06 13:14:50 +0100722 if (escaped_brace)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100723 {
LemonBoy2eaef102022-05-06 13:14:50 +0100724 // Skip the second brace.
725 ++p;
726 continue;
727 }
728
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100729 // Evaluate the expression and append the result.
Bram Moolenaar70c41242022-05-10 18:11:43 +0100730 p = eval_one_expr_in_str(p, &ga, TRUE);
Bram Moolenaar0abc2872022-05-10 13:24:30 +0100731 if (p == NULL)
LemonBoy2eaef102022-05-06 13:14:50 +0100732 {
733 ga_clear(&ga);
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100734 return NULL;
735 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100736 }
737 ga_append(&ga, NUL);
738
739 return ga.ga_data;
740}
741
742/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200743 * Get a list of lines from a HERE document. The here document is a list of
744 * lines surrounded by a marker.
745 * cmd << {marker}
746 * {line1}
747 * {line2}
748 * ....
749 * {marker}
750 *
751 * The {marker} is a string. If the optional 'trim' word is supplied before the
752 * marker, then the leading indentation before the lines (matching the
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100753 * indentation in the "cmd" line) is stripped.
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200754 *
755 * When getting lines for an embedded script (e.g. python, lua, perl, ruby,
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100756 * tcl, mzscheme), "script_get" is set to TRUE. In this case, if the marker is
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200757 * missing, then '.' is accepted as a marker.
758 *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100759 * When compiling a heredoc assignment to a variable in a Vim9 def function,
760 * "vim9compile" is set to TRUE. In this case, instead of generating a list of
761 * string values from the heredoc, vim9 instructions are generated. On success
762 * the returned list will be empty.
763 *
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100764 * Returns a List with {lines} or NULL on failure.
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200765 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 list_T *
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100767heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200768{
Bram Moolenaar42ccb8d2022-04-18 15:45:23 +0100769 char_u *theline = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200770 char_u *marker;
771 list_T *l;
772 char_u *p;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100773 char_u *str;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200774 int marker_indent_len = 0;
775 int text_indent_len = 0;
776 char_u *text_indent = NULL;
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200777 char_u dot[] = ".";
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200778 int comment_char = in_vim9script() ? '#' : '"';
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100779 int evalstr = FALSE;
780 int eval_failed = FALSE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100781 cctx_T *cctx = vim9compile ? eap->cookie : NULL;
782 int count = 0;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200783 int heredoc_in_string = FALSE;
784 char_u *line_arg = NULL;
zeertzjq1f5175d2024-04-13 17:52:26 +0200785 char_u *nl_ptr = vim_strchr(cmd, '\n');
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200786
zeertzjq1f5175d2024-04-13 17:52:26 +0200787 if (nl_ptr != NULL)
788 {
789 heredoc_in_string = TRUE;
790 line_arg = nl_ptr + 1;
791 *nl_ptr = NUL;
792 }
793 else if (eap->ea_getline == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200794 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +0000795 emsg(_(e_cannot_use_heredoc_here));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200796 return NULL;
797 }
798
799 // Check for the optional 'trim' word before the marker
800 cmd = skipwhite(cmd);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200801
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100802 while (TRUE)
803 {
804 if (STRNCMP(cmd, "trim", 4) == 0
805 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200806 {
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100807 cmd = skipwhite(cmd + 4);
808
809 // Trim the indentation from all the lines in the here document.
810 // The amount of indentation trimmed is the same as the indentation
811 // of the first line after the :let command line. To find the end
812 // marker the indent of the :let command line is trimmed.
813 p = *eap->cmdlinep;
814 while (VIM_ISWHITE(*p))
815 {
816 p++;
817 marker_indent_len++;
818 }
819 text_indent_len = -1;
820
821 continue;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200822 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100823 if (STRNCMP(cmd, "eval", 4) == 0
824 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4])))
825 {
826 cmd = skipwhite(cmd + 4);
827 evalstr = TRUE;
828 continue;
829 }
830 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200831 }
832
833 // The marker is the next word.
Bram Moolenaarc0e29012020-09-27 14:22:48 +0200834 if (*cmd != NUL && *cmd != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200835 {
836 marker = skipwhite(cmd);
zeertzjq1f5175d2024-04-13 17:52:26 +0200837 p = skiptowhite(marker);
838 if (*skipwhite(p) != NUL && *skipwhite(p) != comment_char)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200839 {
Bram Moolenaar74409f62022-01-01 15:58:22 +0000840 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200841 return NULL;
842 }
843 *p = NUL;
Bram Moolenaar6ab09532020-05-01 14:10:13 +0200844 if (!script_get && vim_islower(*marker))
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200845 {
Bram Moolenaar6d057012021-12-31 18:49:43 +0000846 emsg(_(e_marker_cannot_start_with_lower_case_letter));
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200847 return NULL;
848 }
849 }
850 else
851 {
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200852 // When getting lines for an embedded script, if the marker is missing,
853 // accept '.' as the marker.
854 if (script_get)
855 marker = dot;
856 else
857 {
Bram Moolenaar1a992222021-12-31 17:25:48 +0000858 emsg(_(e_missing_marker));
Bram Moolenaar6c2b7b82020-04-14 20:15:49 +0200859 return NULL;
860 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200861 }
862
863 l = list_alloc();
864 if (l == NULL)
865 return NULL;
866
867 for (;;)
868 {
869 int mi = 0;
870 int ti = 0;
871
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200872 if (heredoc_in_string)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200873 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200874 char_u *next_line;
875
876 // heredoc in a string separated by newlines. Get the next line
877 // from the string.
878
879 if (*line_arg == NUL)
880 {
881 semsg(_(e_missing_end_marker_str), marker);
882 break;
883 }
884
885 theline = line_arg;
886 next_line = vim_strchr(theline, '\n');
887 if (next_line == NULL)
888 line_arg += STRLEN(line_arg);
889 else
890 {
891 *next_line = NUL;
892 line_arg = next_line + 1;
893 }
894 }
895 else
896 {
897 vim_free(theline);
898 theline = eap->ea_getline(NUL, eap->cookie, 0, FALSE);
899 if (theline == NULL)
900 {
901 semsg(_(e_missing_end_marker_str), marker);
902 break;
903 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200904 }
905
906 // with "trim": skip the indent matching the :let line to find the
907 // marker
908 if (marker_indent_len > 0
909 && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0)
910 mi = marker_indent_len;
911 if (STRCMP(marker, theline + mi) == 0)
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200912 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200913
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100914 // If expression evaluation failed in the heredoc, then skip till the
915 // end marker.
916 if (eval_failed)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100917 continue;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100918
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200919 if (text_indent_len == -1 && *theline != NUL)
920 {
921 // set the text indent from the first line.
922 p = theline;
923 text_indent_len = 0;
924 while (VIM_ISWHITE(*p))
925 {
926 p++;
927 text_indent_len++;
928 }
929 text_indent = vim_strnsave(theline, text_indent_len);
930 }
931 // with "trim": skip the indent matching the first line
932 if (text_indent != NULL)
933 for (ti = 0; ti < text_indent_len; ++ti)
934 if (theline[ti] != text_indent[ti])
935 break;
936
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100937 str = theline + ti;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100938 if (vim9compile)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100939 {
LemonBoy2eaef102022-05-06 13:14:50 +0100940 if (compile_all_expr_in_str(str, evalstr, cctx) == FAIL)
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100941 {
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100942 vim_free(theline);
943 vim_free(text_indent);
944 return FAIL;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100945 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100946 count++;
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100947 }
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100948 else
949 {
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200950 int free_str = FALSE;
951
Bram Moolenaar05c7f5d2022-04-28 16:51:41 +0100952 if (evalstr && !eap->skip)
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100953 {
954 str = eval_all_expr_in_str(str);
955 if (str == NULL)
956 {
957 // expression evaluation failed
958 eval_failed = TRUE;
959 continue;
960 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200961 free_str = TRUE;
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100962 }
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100963
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100964 if (list_append_string(l, str, -1) == FAIL)
965 break;
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200966 if (free_str)
967 vim_free(str);
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100968 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200969 }
Yegappan Lakshmanane74cad32024-04-12 18:48:35 +0200970 if (heredoc_in_string)
971 // Next command follows the heredoc in the string.
972 eap->nextcmd = line_arg;
973 else
974 vim_free(theline);
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200975 vim_free(text_indent);
976
Yegappan Lakshmanan1fc6ea92022-04-21 23:30:15 +0100977 if (vim9compile && cctx->ctx_skip != SKIP_YES && !eval_failed)
978 generate_NEWLIST(cctx, count, FALSE);
979
Yegappan Lakshmananefbfa862022-04-17 12:47:40 +0100980 if (eval_failed)
981 {
982 // expression evaluation in the heredoc failed
983 list_free(l);
984 return NULL;
985 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +0200986 return l;
987}
988
989/*
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200990 * Vim9 variable declaration:
991 * ":var name"
992 * ":var name: type"
993 * ":var name = expr"
994 * ":var name: type = expr"
995 * etc.
996 */
997 void
998ex_var(exarg_T *eap)
999{
Bram Moolenaar330a3882022-03-05 11:05:57 +00001000 char_u *p = eap->cmd;
Bram Moolenaare1d12112022-03-05 11:37:48 +00001001 int has_var;
Bram Moolenaar330a3882022-03-05 11:05:57 +00001002
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001003 if (!in_vim9script())
1004 {
1005 semsg(_(e_str_cannot_be_used_in_legacy_vim_script), ":var");
1006 return;
1007 }
Bram Moolenaare1d12112022-03-05 11:37:48 +00001008 has_var = checkforcmd_noparen(&p, "var", 3);
1009 if (current_sctx.sc_sid == 0 && has_var)
Bram Moolenaar0e1574c2022-03-03 17:05:35 +00001010 {
1011 emsg(_(e_cannot_declare_variable_on_command_line));
1012 return;
1013 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001014 ex_let(eap);
1015}
1016
1017/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001018 * ":let" list all variable values
1019 * ":let var1 var2" list variable values
1020 * ":let var = expr" assignment command.
1021 * ":let var += expr" assignment command.
1022 * ":let var -= expr" assignment command.
1023 * ":let var *= expr" assignment command.
1024 * ":let var /= expr" assignment command.
1025 * ":let var %= expr" assignment command.
1026 * ":let var .= expr" assignment command.
1027 * ":let var ..= expr" assignment command.
1028 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 * ":let var =<< ..." heredoc
Bram Moolenaard672dde2020-02-26 13:43:51 +01001030 * ":let var: string" Vim9 declaration
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001031 *
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001032 * ":final var = expr" assignment command.
1033 * ":final [var1, var2] = expr" unpack list.
1034 *
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001035 * ":const" list all variable values
1036 * ":const var1 var2" list variable values
1037 * ":const var = expr" assignment command.
1038 * ":const [var1, var2] = expr" unpack list.
1039 */
1040 void
Bram Moolenaar2eec3792020-05-25 20:33:55 +02001041ex_let(exarg_T *eap)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001042{
1043 char_u *arg = eap->arg;
1044 char_u *expr = NULL;
1045 typval_T rettv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001046 int var_count = 0;
1047 int semicolon = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001048 char_u op[4];
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001049 char_u *argend;
1050 int first = TRUE;
1051 int concat;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001052 int has_assign;
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001053 int flags = 0;
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001054 int vim9script = in_vim9script();
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001055
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001056 if (eap->cmdidx == CMD_final && !vim9script)
1057 {
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001058 // In legacy Vim script ":final" is short for ":finally".
1059 ex_finally(eap);
1060 return;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001061 }
Bram Moolenaarc58f5452020-10-21 20:58:52 +02001062 if (eap->cmdidx == CMD_let && vim9script)
Bram Moolenaarcfcd0112020-09-27 15:19:27 +02001063 {
1064 emsg(_(e_cannot_use_let_in_vim9_script));
1065 return;
1066 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001067
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001068 if (eap->cmdidx == CMD_const)
1069 flags |= ASSIGN_CONST;
1070 else if (eap->cmdidx == CMD_final)
1071 flags |= ASSIGN_FINAL;
1072
1073 // Vim9 assignment without ":let", ":const" or ":final"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 if (eap->arg == eap->cmd)
Bram Moolenaar30fd8202020-09-26 15:09:30 +02001075 flags |= ASSIGN_NO_DECL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001077 argend = skip_var_list(arg, TRUE, &var_count, &semicolon, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001078 if (argend == NULL)
1079 return;
1080 if (argend > arg && argend[-1] == '.') // for var.='str'
1081 --argend;
1082 expr = skipwhite(argend);
1083 concat = expr[0] == '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02001084 && ((expr[1] == '=' && in_old_script(2))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001085 || (expr[1] == '.' && expr[2] == '='));
Bram Moolenaar32e35112020-05-14 22:41:15 +02001086 has_assign = *expr == '=' || (vim_strchr((char_u *)"+-*/%", *expr) != NULL
1087 && expr[1] == '=');
Bram Moolenaar822ba242020-05-24 23:00:18 +02001088 if (!has_assign && !concat)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001089 {
1090 // ":let" without "=": list variables
1091 if (*arg == '[')
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001092 emsg(_(e_invalid_argument));
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001093 else if (expr[0] == '.' && expr[1] == '=')
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001094 emsg(_(e_dot_equal_not_supported_with_script_version_two));
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001095 else if (!ends_excmd2(eap->cmd, arg))
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001096 {
Bram Moolenaar63be3d42020-07-23 13:11:37 +02001097 if (vim9script)
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001098 {
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001099 if (!ends_excmd2(eap->cmd, skipwhite(argend)))
Bram Moolenaar74409f62022-01-01 15:58:22 +00001100 semsg(_(e_trailing_characters_str), argend);
Bram Moolenaarccc25aa2021-03-26 21:27:52 +01001101 else
1102 // Vim9 declaration ":var name: type"
1103 arg = vim9_declare_scriptvar(eap, arg);
Bram Moolenaarc82a5b52020-06-13 18:09:19 +02001104 }
1105 else
1106 {
1107 // ":let var1 var2" - list values
1108 arg = list_arg_vars(eap, arg, &first);
1109 }
1110 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001111 else if (!eap->skip)
1112 {
1113 // ":let"
1114 list_glob_vars(&first);
1115 list_buf_vars(&first);
1116 list_win_vars(&first);
1117 list_tab_vars(&first);
1118 list_script_vars(&first);
1119 list_func_vars(&first);
1120 list_vim_vars(&first);
1121 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02001122 set_nextcmd(eap, arg);
zeertzjq474891b2023-04-12 21:36:03 +01001123 return;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001124 }
zeertzjq474891b2023-04-12 21:36:03 +01001125
1126 if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001127 {
Bram Moolenaard9876422022-10-12 12:58:54 +01001128 list_T *l = NULL;
Bram Moolenaar81530e32021-07-28 21:25:49 +02001129 long cur_lnum = SOURCING_LNUM;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001130
Bram Moolenaard9876422022-10-12 12:58:54 +01001131 // :let text =<< [trim] [eval] END
1132 // :var text =<< [trim] [eval] END
1133 if (vim9script && !eap->skip && (!VIM_ISWHITE(expr[-1])
1134 || !IS_WHITE_OR_NUL(expr[3])))
1135 semsg(_(e_white_space_required_before_and_after_str_at_str),
1136 "=<<", expr);
1137 else
1138 l = heredoc_get(eap, expr + 3, FALSE, FALSE);
1139
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001140 if (l != NULL)
1141 {
1142 rettv_list_set(&rettv, l);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001143 if (!eap->skip)
1144 {
Bram Moolenaar81530e32021-07-28 21:25:49 +02001145 // errors are for the assignment, not the end marker
1146 SOURCING_LNUM = cur_lnum;
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001147 op[0] = '=';
1148 op[1] = NUL;
1149 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 flags, op);
Bram Moolenaarb1ba9ab2019-10-16 23:34:42 +02001151 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001152 clear_tv(&rettv);
1153 }
zeertzjq474891b2023-04-12 21:36:03 +01001154 return;
1155 }
1156
1157 evalarg_T evalarg;
1158 int len = 1;
1159
1160 CLEAR_FIELD(rettv);
1161
1162 int cur_lnum;
1163
1164 op[0] = '=';
1165 op[1] = NUL;
1166 if (*expr != '=')
1167 {
1168 if (vim9script && (flags & ASSIGN_NO_DECL) == 0)
1169 {
1170 // +=, /=, etc. require an existing variable
1171 semsg(_(e_cannot_use_operator_on_new_variable_str), eap->arg);
1172 }
1173 else if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1174 {
1175 op[0] = *expr; // +=, -=, *=, /=, %= or .=
1176 ++len;
1177 if (expr[0] == '.' && expr[1] == '.') // ..=
1178 {
1179 ++expr;
1180 ++len;
1181 }
1182 }
1183 expr += 2;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001184 }
1185 else
zeertzjq474891b2023-04-12 21:36:03 +01001186 ++expr;
1187
1188 if (vim9script && !eap->skip && (!VIM_ISWHITE(*argend)
1189 || !IS_WHITE_OR_NUL(*expr)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001190 {
zeertzjq474891b2023-04-12 21:36:03 +01001191 vim_strncpy(op, expr - len, len);
1192 semsg(_(e_white_space_required_before_and_after_str_at_str),
1193 op, argend);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001194 }
zeertzjq474891b2023-04-12 21:36:03 +01001195
1196 if (eap->skip)
1197 ++emsg_skip;
1198 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
1199 expr = skipwhite_and_linebreak(expr, &evalarg);
1200 cur_lnum = SOURCING_LNUM;
1201 int eval_res = eval0(expr, &rettv, eap, &evalarg);
1202 if (eap->skip)
1203 --emsg_skip;
1204 clear_evalarg(&evalarg, eap);
1205
1206 // Restore the line number so that any type error is given for the
1207 // declaration, not the expression.
1208 SOURCING_LNUM = cur_lnum;
1209
1210 if (!eap->skip && eval_res != FAIL)
1211 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
1212 flags, op);
1213 if (eval_res != FAIL)
1214 clear_tv(&rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001215}
1216
1217/*
Bram Moolenaar6c3843c2021-03-04 12:38:21 +01001218 * Assign the typeval "tv" to the variable or variables at "arg_start".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001219 * Handles both "var" with any type and "[var, var; var]" with a list type.
1220 * When "op" is not NULL it points to a string with characters that
1221 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1222 * or concatenate.
1223 * Returns OK or FAIL;
1224 */
1225 int
1226ex_let_vars(
1227 char_u *arg_start,
1228 typval_T *tv,
1229 int copy, // copy values from "tv", don't move
1230 int semicolon, // from skip_var_list()
1231 int var_count, // from skip_var_list()
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001232 int flags, // ASSIGN_FINAL, ASSIGN_CONST, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001233 char_u *op)
1234{
1235 char_u *arg = arg_start;
1236 list_T *l;
1237 int i;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001238 int var_idx = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001239 listitem_T *item;
1240 typval_T ltv;
1241
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00001242 if (tv->v_type == VAR_VOID)
1243 {
1244 emsg(_(e_cannot_use_void_value));
1245 return FAIL;
1246 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001247 if (*arg != '[')
1248 {
1249 // ":let var = expr" or ":for var in list"
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001250 if (ex_let_one(arg, tv, copy, flags, op, op, var_idx) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001251 return FAIL;
1252 return OK;
1253 }
1254
1255 // ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1256 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
1257 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001258 emsg(_(e_list_required));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001259 return FAIL;
1260 }
1261
1262 i = list_len(l);
1263 if (semicolon == 0 && var_count < i)
1264 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001265 emsg(_(e_less_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001266 return FAIL;
1267 }
1268 if (var_count - semicolon > i)
1269 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00001270 emsg(_(e_more_targets_than_list_items));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001271 return FAIL;
1272 }
1273
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001274 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001275 item = l->lv_first;
1276 while (*arg != ']')
1277 {
1278 arg = skipwhite(arg + 1);
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001279 ++var_idx;
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001280 arg = ex_let_one(arg, &item->li_tv, TRUE,
1281 flags | ASSIGN_UNPACK, (char_u *)",;]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001282 item = item->li_next;
1283 if (arg == NULL)
1284 return FAIL;
1285
1286 arg = skipwhite(arg);
1287 if (*arg == ';')
1288 {
1289 // Put the rest of the list (may be empty) in the var after ';'.
1290 // Create a new list for this.
1291 l = list_alloc();
1292 if (l == NULL)
1293 return FAIL;
1294 while (item != NULL)
1295 {
1296 list_append_tv(l, &item->li_tv);
1297 item = item->li_next;
1298 }
1299
1300 ltv.v_type = VAR_LIST;
1301 ltv.v_lock = 0;
1302 ltv.vval.v_list = l;
1303 l->lv_refcount = 1;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001304 ++var_idx;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001305
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02001306 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1307 flags | ASSIGN_UNPACK, (char_u *)"]", op, var_idx);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001308 clear_tv(&ltv);
1309 if (arg == NULL)
1310 return FAIL;
1311 break;
1312 }
1313 else if (*arg != ',' && *arg != ']')
1314 {
1315 internal_error("ex_let_vars()");
1316 return FAIL;
1317 }
1318 }
1319
1320 return OK;
1321}
1322
1323/*
1324 * Skip over assignable variable "var" or list of variables "[var, var]".
1325 * Used for ":let varvar = expr" and ":for varvar in expr".
1326 * For "[var, var]" increment "*var_count" for each variable.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001327 * for "[var, var; var]" set "semicolon" to 1.
1328 * If "silent" is TRUE do not give an "invalid argument" error message.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001329 * Return NULL for an error.
1330 */
1331 char_u *
1332skip_var_list(
1333 char_u *arg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 int include_type,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001335 int *var_count,
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001336 int *semicolon,
1337 int silent)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001338{
1339 char_u *p, *s;
1340
1341 if (*arg == '[')
1342 {
1343 // "[var, var]": find the matching ']'.
1344 p = arg;
1345 for (;;)
1346 {
1347 p = skipwhite(p + 1); // skip whites after '[', ';' or ','
Bram Moolenaar036d0712021-01-17 20:23:38 +01001348 s = skip_var_one(p, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001349 if (s == p)
1350 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001351 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001352 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001353 return NULL;
1354 }
1355 ++*var_count;
1356
1357 p = skipwhite(s);
1358 if (*p == ']')
1359 break;
1360 else if (*p == ';')
1361 {
1362 if (*semicolon == 1)
1363 {
Bram Moolenaar8b716f52022-02-15 21:17:56 +00001364 if (!silent)
1365 emsg(_(e_double_semicolon_in_list_of_variables));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001366 return NULL;
1367 }
1368 *semicolon = 1;
1369 }
1370 else if (*p != ',')
1371 {
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001372 if (!silent)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001373 semsg(_(e_invalid_argument_str), p);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001374 return NULL;
1375 }
1376 }
1377 return p + 1;
1378 }
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01001379
Bram Moolenaare8e369a2022-09-21 18:59:14 +01001380 return skip_var_one(arg, include_type);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001381}
1382
1383/*
1384 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
1385 * l[idx].
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 * In Vim9 script also skip over ": type" if "include_type" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001387 */
Bram Moolenaar47a519a2020-06-14 23:05:10 +02001388 char_u *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389skip_var_one(char_u *arg, int include_type)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001390{
Bram Moolenaar585587d2021-01-17 20:52:13 +01001391 char_u *end;
1392 int vim9 = in_vim9script();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001394 if (*arg == '@' && arg[1] != NUL)
1395 return arg + 2;
Bram Moolenaar1573e732022-11-16 20:33:21 +00001396
1397 // termcap option name may have non-alpha characters
1398 if (STRNCMP(arg, "&t_", 3) == 0 && arg[3] != NUL && arg[4] != NUL)
1399 return arg + 5;
1400
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 end = find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001402 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar036d0712021-01-17 20:23:38 +01001403
1404 // "a: type" is declaring variable "a" with a type, not "a:".
1405 // Same for "s: type".
Bram Moolenaar585587d2021-01-17 20:52:13 +01001406 if (vim9 && end == arg + 2 && end[-1] == ':')
Bram Moolenaar036d0712021-01-17 20:23:38 +01001407 --end;
1408
Bram Moolenaar585587d2021-01-17 20:52:13 +01001409 if (include_type && vim9)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 {
Bram Moolenaarce93d162023-01-30 21:12:34 +00001411 if (*skipwhite(end) == ':')
1412 end = skip_type(skipwhite(skipwhite(end) + 1), FALSE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 }
1414 return end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001415}
1416
1417/*
1418 * List variables for hashtab "ht" with prefix "prefix".
1419 * If "empty" is TRUE also list NULL strings as empty strings.
1420 */
1421 void
1422list_hashtable_vars(
1423 hashtab_T *ht,
1424 char *prefix,
1425 int empty,
1426 int *first)
1427{
1428 hashitem_T *hi;
1429 dictitem_T *di;
1430 int todo;
1431 char_u buf[IOSIZE];
1432
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001433 int save_ht_flags = ht->ht_flags;
1434 ht->ht_flags |= HTFLAGS_FROZEN;
1435
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001436 todo = (int)ht->ht_used;
1437 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1438 {
1439 if (!HASHITEM_EMPTY(hi))
1440 {
1441 --todo;
1442 di = HI2DI(hi);
1443
1444 // apply :filter /pat/ to variable name
1445 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1446 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
1447 if (message_filtered(buf))
1448 continue;
1449
1450 if (empty || di->di_tv.v_type != VAR_STRING
1451 || di->di_tv.vval.v_string != NULL)
1452 list_one_var(di, prefix, first);
1453 }
1454 }
Bram Moolenaaref2c3252022-11-25 16:31:51 +00001455
1456 ht->ht_flags = save_ht_flags;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001457}
1458
1459/*
1460 * List global variables.
1461 */
1462 static void
1463list_glob_vars(int *first)
1464{
1465 list_hashtable_vars(&globvarht, "", TRUE, first);
1466}
1467
1468/*
1469 * List buffer variables.
1470 */
1471 static void
1472list_buf_vars(int *first)
1473{
1474 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
1475}
1476
1477/*
1478 * List window variables.
1479 */
1480 static void
1481list_win_vars(int *first)
1482{
1483 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
1484}
1485
1486/*
1487 * List tab page variables.
1488 */
1489 static void
1490list_tab_vars(int *first)
1491{
1492 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
1493}
1494
1495/*
1496 * List variables in "arg".
1497 */
1498 static char_u *
1499list_arg_vars(exarg_T *eap, char_u *arg, int *first)
1500{
1501 int error = FALSE;
1502 int len;
1503 char_u *name;
1504 char_u *name_start;
1505 char_u *arg_subsc;
1506 char_u *tofree;
1507 typval_T tv;
1508
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001509 while (!ends_excmd2(eap->cmd, arg) && !got_int)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001510 {
1511 if (error || eap->skip)
1512 {
1513 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
1514 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
1515 {
1516 emsg_severe = TRUE;
Bram Moolenaar4830c212021-08-14 14:59:27 +02001517 if (!did_emsg)
Bram Moolenaar74409f62022-01-01 15:58:22 +00001518 semsg(_(e_trailing_characters_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001519 break;
1520 }
1521 }
1522 else
1523 {
1524 // get_name_len() takes care of expanding curly braces
1525 name_start = name = arg;
1526 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1527 if (len <= 0)
1528 {
1529 // This is mainly to keep test 49 working: when expanding
1530 // curly braces fails overrule the exception error message.
1531 if (len < 0 && !aborting())
1532 {
1533 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001534 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001535 break;
1536 }
1537 error = TRUE;
1538 }
1539 else
1540 {
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02001541 arg = skipwhite(arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001542 if (tofree != NULL)
1543 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001544 if (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001545 EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001546 error = TRUE;
1547 else
1548 {
1549 // handle d.key, l[idx], f(expr)
1550 arg_subsc = arg;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00001551 if (handle_subscript(&arg, name_start, &tv,
1552 &EVALARG_EVALUATE, TRUE) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001553 error = TRUE;
1554 else
1555 {
1556 if (arg == arg_subsc && len == 2 && name[1] == ':')
1557 {
1558 switch (*name)
1559 {
1560 case 'g': list_glob_vars(first); break;
1561 case 'b': list_buf_vars(first); break;
1562 case 'w': list_win_vars(first); break;
1563 case 't': list_tab_vars(first); break;
1564 case 'v': list_vim_vars(first); break;
1565 case 's': list_script_vars(first); break;
1566 case 'l': list_func_vars(first); break;
1567 default:
Bram Moolenaara6f79292022-01-04 21:30:47 +00001568 semsg(_(e_cant_list_variables_for_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001569 }
1570 }
1571 else
1572 {
1573 char_u numbuf[NUMBUFLEN];
1574 char_u *tf;
1575 int c;
1576 char_u *s;
1577
1578 s = echo_string(&tv, &tf, numbuf, 0);
1579 c = *arg;
1580 *arg = NUL;
1581 list_one_var_a("",
1582 arg == arg_subsc ? name : name_start,
1583 tv.v_type,
1584 s == NULL ? (char_u *)"" : s,
1585 first);
1586 *arg = c;
1587 vim_free(tf);
1588 }
1589 clear_tv(&tv);
1590 }
1591 }
1592 }
1593
1594 vim_free(tofree);
1595 }
1596
1597 arg = skipwhite(arg);
1598 }
1599
1600 return arg;
1601}
1602
1603/*
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001604 * Set an environment variable, part of ex_let_one().
1605 */
1606 static char_u *
1607ex_let_env(
1608 char_u *arg,
1609 typval_T *tv,
1610 int flags,
1611 char_u *endchars,
1612 char_u *op)
1613{
1614 char_u *arg_end = NULL;
1615 char_u *name;
1616 int len;
1617
1618 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1619 && (flags & ASSIGN_FOR_LOOP) == 0)
1620 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001621 emsg(_(e_cannot_lock_environment_variable));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001622 return NULL;
1623 }
1624
1625 // Find the end of the name.
1626 ++arg;
1627 name = arg;
1628 len = get_env_len(&arg);
1629 if (len == 0)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001630 semsg(_(e_invalid_argument_str), name - 1);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001631 else
1632 {
1633 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001634 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001635 else if (endchars != NULL
1636 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
1637 emsg(_(e_unexpected_characters_in_let));
1638 else if (!check_secure())
1639 {
1640 char_u *tofree = NULL;
1641 int c1 = name[len];
1642 char_u *p;
1643
1644 name[len] = NUL;
1645 p = tv_get_string_chk(tv);
1646 if (p != NULL && op != NULL && *op == '.')
1647 {
1648 int mustfree = FALSE;
1649 char_u *s = vim_getenv(name, &mustfree);
1650
1651 if (s != NULL)
1652 {
1653 p = tofree = concat_str(s, p);
1654 if (mustfree)
1655 vim_free(s);
1656 }
1657 }
1658 if (p != NULL)
1659 {
1660 vim_setenv_ext(name, p);
1661 arg_end = arg;
1662 }
1663 name[len] = c1;
1664 vim_free(tofree);
1665 }
1666 }
1667 return arg_end;
1668}
1669
1670/*
1671 * Set an option, part of ex_let_one().
1672 */
1673 static char_u *
1674ex_let_option(
1675 char_u *arg,
1676 typval_T *tv,
1677 int flags,
1678 char_u *endchars,
1679 char_u *op)
1680{
1681 char_u *p;
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001682 int scope;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001683 char_u *arg_end = NULL;
1684
1685 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1686 && (flags & ASSIGN_FOR_LOOP) == 0)
1687 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001688 emsg(_(e_cannot_lock_option));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001689 return NULL;
1690 }
1691
1692 // Find the end of the name.
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00001693 p = find_option_end(&arg, &scope);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001694 if (p == NULL || (endchars != NULL
1695 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001696 {
zeertzjq4c7cb372023-06-14 16:39:54 +01001697 emsg(_(e_unexpected_characters_in_let));
1698 return NULL;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001699 }
zeertzjq4c7cb372023-06-14 16:39:54 +01001700
1701 int c1;
1702 long n = 0;
1703 getoption_T opt_type;
1704 long numval;
1705 char_u *stringval = NULL;
1706 char_u *s = NULL;
1707 int failed = FALSE;
1708 int opt_p_flags;
1709 char_u *tofree = NULL;
1710 char_u numbuf[NUMBUFLEN];
1711
1712 c1 = *p;
1713 *p = NUL;
1714
1715 opt_type = get_option_value(arg, &numval, &stringval, &opt_p_flags, scope);
1716 if (opt_type == gov_unknown && arg[0] != 't' && arg[1] != '_')
1717 {
1718 semsg(_(e_unknown_option_str_2), arg);
1719 goto theend;
1720 }
1721 if (op != NULL && *op != '='
1722 && (((opt_type == gov_bool || opt_type == gov_number) && *op == '.')
1723 || (opt_type == gov_string && *op != '.')))
1724 {
1725 semsg(_(e_wrong_variable_type_for_str_equal), op);
1726 goto theend;
1727 }
1728
1729 if ((opt_type == gov_bool
1730 || opt_type == gov_number
1731 || opt_type == gov_hidden_bool
1732 || opt_type == gov_hidden_number)
1733 && (tv->v_type != VAR_STRING || !in_vim9script()))
1734 {
1735 if (opt_type == gov_bool || opt_type == gov_hidden_bool)
1736 // bool, possibly hidden
1737 n = (long)tv_get_bool_chk(tv, &failed);
1738 else
1739 // number, possibly hidden
1740 n = (long)tv_get_number_chk(tv, &failed);
1741 if (failed)
1742 goto theend;
1743 }
1744
1745 if ((opt_p_flags & P_FUNC) && (tv->v_type == VAR_PARTIAL
1746 || tv->v_type == VAR_FUNC))
1747 {
1748 // If the option can be set to a function reference or a lambda
1749 // and the passed value is a function reference, then convert it to
1750 // the name (string) of the function reference.
1751 s = tv2string(tv, &tofree, numbuf, 0);
1752 if (s == NULL)
1753 goto theend;
1754 }
1755 // Avoid setting a string option to the text "v:false" or similar.
1756 // In Vim9 script also don't convert a number to string.
1757 else if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL
1758 && (!in_vim9script() || tv->v_type != VAR_NUMBER))
1759 {
1760 s = tv_get_string_chk(tv);
1761 if (s == NULL)
1762 goto theend;
1763 }
1764 else if (opt_type == gov_string || opt_type == gov_hidden_string)
1765 {
1766 emsg(_(e_string_required));
1767 goto theend;
1768 }
1769
1770 if (op != NULL && *op != '=')
1771 {
1772 // number, in legacy script also bool
1773 if (opt_type == gov_number
1774 || (opt_type == gov_bool && !in_vim9script()))
1775 {
1776 switch (*op)
1777 {
1778 case '+': n = numval + n; break;
1779 case '-': n = numval - n; break;
1780 case '*': n = numval * n; break;
1781 case '/': n = (long)num_divide(numval, n, &failed); break;
1782 case '%': n = (long)num_modulus(numval, n, &failed); break;
1783 }
1784 s = NULL;
1785 if (failed)
1786 goto theend;
1787 }
1788 else if (opt_type == gov_string && stringval != NULL && s != NULL)
1789 {
1790 // string
1791 s = concat_str(stringval, s);
1792 vim_free(stringval);
1793 stringval = s;
1794 }
1795 }
1796
1797 char *err = set_option_value(arg, n, s, scope);
1798 arg_end = p;
1799 if (err != NULL)
1800 emsg(_(err));
1801
1802theend:
1803 *p = c1;
1804 vim_free(stringval);
1805 vim_free(tofree);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001806 return arg_end;
1807}
1808
1809/*
1810 * Set a register, part of ex_let_one().
1811 */
1812 static char_u *
1813ex_let_register(
1814 char_u *arg,
1815 typval_T *tv,
1816 int flags,
1817 char_u *endchars,
1818 char_u *op)
1819{
1820 char_u *arg_end = NULL;
1821
1822 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1823 && (flags & ASSIGN_FOR_LOOP) == 0)
1824 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001825 emsg(_(e_cannot_lock_register));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001826 return NULL;
1827 }
1828 ++arg;
1829 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001830 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001831 else if (endchars != NULL
1832 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
1833 emsg(_(e_unexpected_characters_in_let));
1834 else
1835 {
1836 char_u *ptofree = NULL;
1837 char_u *p;
1838
1839 p = tv_get_string_chk(tv);
1840 if (p != NULL && op != NULL && *op == '.')
1841 {
1842 char_u *s = get_reg_contents(*arg == '@'
1843 ? '"' : *arg, GREG_EXPR_SRC);
1844
1845 if (s != NULL)
1846 {
1847 p = ptofree = concat_str(s, p);
1848 vim_free(s);
1849 }
1850 }
1851 if (p != NULL)
1852 {
1853 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
1854 arg_end = arg + 1;
1855 }
1856 vim_free(ptofree);
1857 }
1858 return arg_end;
1859}
1860
1861/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001862 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1863 * Returns a pointer to the char just after the var name.
1864 * Returns NULL if there is an error.
1865 */
1866 static char_u *
1867ex_let_one(
1868 char_u *arg, // points to variable name
1869 typval_T *tv, // value to assign to variable
1870 int copy, // copy value from "tv"
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001871 int flags, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001872 char_u *endchars, // valid chars after variable name or NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001873 char_u *op, // "+", "-", "." or NULL
1874 int var_idx) // variable index for "let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001875{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001876 char_u *arg_end = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001877
Bram Moolenaar3862ea32021-01-01 21:05:55 +01001878 if (in_vim9script() && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01001879 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaarc2ee44c2020-08-02 16:59:00 +02001880 && vim_strchr((char_u *)"$@&", *arg) != NULL)
1881 {
1882 vim9_declare_error(arg);
1883 return NULL;
1884 }
1885
Ernie Rael9ed53752023-12-11 17:40:46 +01001886 if (check_typval_is_value(tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001887 return NULL;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02001888
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001889 if (*arg == '$')
1890 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001891 // ":let $VAR = expr": Set environment variable.
1892 return ex_let_env(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001893 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001894 else if (*arg == '&')
1895 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001896 // ":let &option = expr": Set option value.
1897 // ":let &l:option = expr": Set local option value.
1898 // ":let &g:option = expr": Set global option value.
1899 // ":for &ts in range(8)": Set option value for for loop
1900 return ex_let_option(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001901 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001902 else if (*arg == '@')
1903 {
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001904 // ":let @r = expr": Set register contents.
1905 return ex_let_register(arg, tv, flags, endchars, op);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001906 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001907 else if (eval_isnamec1(*arg) || *arg == '{')
1908 {
1909 lval_T lv;
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001910 char_u *p;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001911 int lval_flags = (flags & (ASSIGN_NO_DECL | ASSIGN_DECL))
1912 ? GLV_NO_DECL : 0;
Yegappan Lakshmananb5a07192023-10-05 20:14:43 +02001913 lval_flags |= (flags & ASSIGN_FOR_LOOP) ? GLV_FOR_LOOP : 0;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001914 if (op != NULL && *op != '=')
1915 lval_flags |= GLV_ASSIGN_WITH_OP;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001916
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001917 // ":let var = expr": Set internal variable.
1918 // ":let var: type = expr": Set internal variable with type.
1919 // ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001920 p = get_lval(arg, tv, &lv, FALSE, FALSE, lval_flags, FNE_CHECK_START);
Bram Moolenaar822ba242020-05-24 23:00:18 +02001921 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001922 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 if (endchars != NULL && vim_strchr(endchars,
1924 *skipwhite(lv.ll_name_end)) == NULL)
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001925 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02001926 emsg(_(e_unexpected_characters_in_let));
Bram Moolenaar3ccb5792021-11-28 19:53:42 +00001927 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001928 else
1929 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001930 set_var_lval(&lv, p, tv, copy, flags, op, var_idx);
Bram Moolenaara3589a02021-04-14 13:30:46 +02001931 arg_end = lv.ll_name_end;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001932 }
1933 }
1934 clear_lval(&lv);
1935 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001936 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001937 semsg(_(e_invalid_argument_str), arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001938
1939 return arg_end;
1940}
1941
1942/*
1943 * ":unlet[!] var1 ... " command.
1944 */
1945 void
1946ex_unlet(exarg_T *eap)
1947{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001948 ex_unletlock(eap, eap->arg, 0, 0, do_unlet_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001949}
1950
1951/*
1952 * ":lockvar" and ":unlockvar" commands
1953 */
1954 void
1955ex_lockvar(exarg_T *eap)
1956{
1957 char_u *arg = eap->arg;
1958 int deep = 2;
1959
1960 if (eap->forceit)
1961 deep = -1;
1962 else if (vim_isdigit(*arg))
1963 {
1964 deep = getdigits(&arg);
1965 arg = skipwhite(arg);
1966 }
1967
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001968 ex_unletlock(eap, arg, deep, 0, do_lock_var, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001969}
1970
1971/*
1972 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001973 * Also used for Vim9 script. "callback" is invoked as:
1974 * callback(&lv, name_end, eap, deep, cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001975 */
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001976 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001977ex_unletlock(
1978 exarg_T *eap,
1979 char_u *argstart,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001980 int deep,
1981 int glv_flags,
1982 int (*callback)(lval_T *, char_u *, exarg_T *, int, void *),
1983 void *cookie)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001984{
1985 char_u *arg = argstart;
1986 char_u *name_end;
1987 int error = FALSE;
1988 lval_T lv;
1989
1990 do
1991 {
1992 if (*arg == '$')
1993 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02001994 lv.ll_name = arg;
1995 lv.ll_tv = NULL;
1996 ++arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001997 if (get_env_len(&arg) == 0)
1998 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001999 semsg(_(e_invalid_argument_str), arg - 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002000 return;
2001 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002002 if (!error && !eap->skip
2003 && callback(&lv, arg, eap, deep, cookie) == FAIL)
2004 error = TRUE;
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002005 name_end = arg;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002006 }
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002007 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002008 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002009 // Parse the name and find the end.
2010 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error,
Bram Moolenaarc3689572021-01-01 19:40:02 +01002011 glv_flags | GLV_NO_DECL, FNE_CHECK_START);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002012 if (lv.ll_name == NULL)
2013 error = TRUE; // error but continue parsing
2014 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
2015 && !ends_excmd(*name_end)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002016 {
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002017 if (name_end != NULL)
2018 {
2019 emsg_severe = TRUE;
Bram Moolenaar74409f62022-01-01 15:58:22 +00002020 semsg(_(e_trailing_characters_str), name_end);
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002021 }
2022 if (!(eap->skip || error))
2023 clear_lval(&lv);
2024 break;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002025 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002026
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002027 if (!error && !eap->skip
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002028 && callback(&lv, name_end, eap, deep, cookie) == FAIL)
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002029 error = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002030
Bram Moolenaar2bb76ac2020-04-19 22:57:44 +02002031 if (!eap->skip)
2032 clear_lval(&lv);
2033 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002034
2035 arg = skipwhite(name_end);
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002036 } while (!ends_excmd2(name_end, arg));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002037
Bram Moolenaar63b91732021-08-05 20:40:03 +02002038 set_nextcmd(eap, arg);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002039}
2040
2041 static int
2042do_unlet_var(
2043 lval_T *lp,
2044 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002045 exarg_T *eap,
2046 int deep UNUSED,
2047 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002048{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002049 int forceit = eap->forceit;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002050 int ret = OK;
2051 int cc;
2052
2053 if (lp->ll_tv == NULL)
2054 {
2055 cc = *name_end;
2056 *name_end = NUL;
2057
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002058 // Environment variable, normal name or expanded name.
2059 if (*lp->ll_name == '$')
LemonBoy77142312022-04-15 20:50:46 +01002060 vim_unsetenv_ext(lp->ll_name + 1);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002061 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002062 ret = FAIL;
2063 *name_end = cc;
2064 }
2065 else if ((lp->ll_list != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002066 && value_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002067 || (lp->ll_dict != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02002068 && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002069 return FAIL;
2070 else if (lp->ll_range)
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002071 list_unlet_range(lp->ll_list, lp->ll_li, lp->ll_n1,
2072 !lp->ll_empty2, lp->ll_n2);
2073 else if (lp->ll_list != NULL)
2074 // unlet a List item.
2075 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002076 else
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002077 // unlet a Dictionary item.
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002078 dictitem_remove(lp->ll_dict, lp->ll_di, "unlet");
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002079
2080 return ret;
2081}
2082
2083/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002084 * Unlet one item or a range of items from a list.
2085 * Return OK or FAIL.
2086 */
Bram Moolenaar6b8c7ba2022-03-20 17:46:06 +00002087 void
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002088list_unlet_range(
2089 list_T *l,
2090 listitem_T *li_first,
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002091 long n1_arg,
2092 int has_n2,
2093 long n2)
2094{
2095 listitem_T *li = li_first;
2096 int n1 = n1_arg;
2097
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002098 // Delete a range of List items.
2099 li = li_first;
2100 n1 = n1_arg;
2101 while (li != NULL && (!has_n2 || n2 >= n1))
2102 {
2103 listitem_T *next = li->li_next;
2104
2105 listitem_remove(l, li);
2106 li = next;
2107 ++n1;
2108 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002109}
2110/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002111 * "unlet" a variable. Return OK if it existed, FAIL if not.
2112 * When "forceit" is TRUE don't complain if the variable doesn't exist.
2113 */
2114 int
2115do_unlet(char_u *name, int forceit)
2116{
2117 hashtab_T *ht;
2118 hashitem_T *hi;
mityu8be36ee2022-05-25 17:29:46 +01002119 char_u *varname = NULL; // init to shut up gcc
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002120 dict_T *d;
2121 dictitem_T *di;
2122
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002123 // can't :unlet a script variable in Vim9 script
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002124 if (in_vim9script() && check_vim9_unlet(name) == FAIL)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002125 return FAIL;
2126
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002127 ht = find_var_ht(name, &varname);
Bram Moolenaar9aed7292020-12-18 15:38:00 +01002128
2129 // can't :unlet a script variable in Vim9 script from a function
2130 if (ht == get_script_local_ht()
2131 && SCRIPT_ID_VALID(current_sctx.sc_sid)
2132 && SCRIPT_ITEM(current_sctx.sc_sid)->sn_version
2133 == SCRIPT_VERSION_VIM9
2134 && check_vim9_unlet(name) == FAIL)
2135 return FAIL;
2136
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002137 if (ht != NULL && *varname != NUL)
2138 {
2139 d = get_current_funccal_dict(ht);
2140 if (d == NULL)
2141 {
2142 if (ht == &globvarht)
2143 d = &globvardict;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002144 else if (ht == &compat_hashtab)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002145 d = &vimvardict;
2146 else
2147 {
2148 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
2149 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
2150 }
2151 if (d == NULL)
2152 {
2153 internal_error("do_unlet()");
2154 return FAIL;
2155 }
2156 }
2157 hi = hash_find(ht, varname);
2158 if (HASHITEM_EMPTY(hi))
2159 hi = find_hi_in_scoped_ht(name, &ht);
2160 if (hi != NULL && !HASHITEM_EMPTY(hi))
2161 {
2162 di = HI2DI(hi);
2163 if (var_check_fixed(di->di_flags, name, FALSE)
2164 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002165 || value_check_lock(d->dv_lock, name, FALSE)
2166 || check_hashtab_frozen(ht, "unlet"))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002167 return FAIL;
2168
2169 delete_var(ht, hi);
2170 return OK;
2171 }
2172 }
2173 if (forceit)
2174 return OK;
Bram Moolenaare1242042021-12-16 20:56:57 +00002175 semsg(_(e_no_such_variable_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002176 return FAIL;
2177}
2178
Ernie Raelee865f32023-09-29 19:53:55 +02002179 static void
2180report_lockvar_member(char *msg, lval_T *lp)
2181{
2182 int did_alloc = FALSE;
2183 char_u *vname = (char_u *)"";
2184 char_u *class_name = lp->ll_class != NULL
2185 ? lp->ll_class->class_name : (char_u *)"";
2186 if (lp->ll_name != NULL)
2187 {
2188 if (lp->ll_name_end == NULL)
2189 vname = lp->ll_name;
2190 else
2191 {
2192 vname = vim_strnsave(lp->ll_name, lp->ll_name_end - lp->ll_name);
2193 if (vname == NULL)
2194 return;
2195 did_alloc = TRUE;
2196 }
2197 }
2198 semsg(_(msg), vname, class_name);
2199 if (did_alloc)
2200 vim_free(vname);
2201}
2202
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002203/*
2204 * Lock or unlock variable indicated by "lp".
2205 * "deep" is the levels to go (-1 for unlimited);
2206 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
2207 */
2208 static int
2209do_lock_var(
2210 lval_T *lp,
2211 char_u *name_end,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002212 exarg_T *eap,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002213 int deep,
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002214 void *cookie UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002215{
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002216 int lock = eap->cmdidx == CMD_lockvar;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002217 int ret = OK;
2218 int cc;
2219 dictitem_T *di;
2220
Ernie Raelee865f32023-09-29 19:53:55 +02002221#ifdef LOG_LOCKVAR
2222 ch_log(NULL, "LKVAR: do_lock_var(): name %s, is_root %d", lp->ll_name, lp->ll_is_root);
2223#endif
2224
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002225 if (lp->ll_tv == NULL)
2226 {
2227 cc = *name_end;
2228 *name_end = NUL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002229 if (*lp->ll_name == '$')
2230 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00002231 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002232 ret = FAIL;
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002233 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002234 else
2235 {
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002236 // Normal name or expanded name.
2237 di = find_var(lp->ll_name, NULL, TRUE);
2238 if (di == NULL)
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002239 {
2240 if (in_vim9script())
2241 semsg(_(e_cannot_find_variable_to_unlock_str),
2242 lp->ll_name);
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002243 ret = FAIL;
Bram Moolenaar04b568b2021-11-22 21:58:41 +00002244 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002245 else
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002246 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002247 if ((di->di_flags & DI_FLAGS_FIX)
2248 && di->di_tv.v_type != VAR_DICT
2249 && di->di_tv.v_type != VAR_LIST)
2250 {
2251 // For historic reasons this error is not given for a list
2252 // or dict. E.g., the b: dict could be locked/unlocked.
2253 semsg(_(e_cannot_lock_or_unlock_variable_str), lp->ll_name);
2254 ret = FAIL;
2255 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002256 else
Bram Moolenaar7a411a32022-04-04 14:58:06 +01002257 {
2258 if (in_vim9script())
2259 {
2260 svar_T *sv = find_typval_in_script(&di->di_tv,
2261 0, FALSE);
2262
2263 if (sv != NULL && sv->sv_const != 0)
2264 {
2265 semsg(_(e_cannot_change_readonly_variable_str),
2266 lp->ll_name);
2267 ret = FAIL;
2268 }
2269 }
2270
2271 if (ret == OK)
2272 {
2273 if (lock)
2274 di->di_flags |= DI_FLAGS_LOCK;
2275 else
2276 di->di_flags &= ~DI_FLAGS_LOCK;
2277 if (deep != 0)
2278 item_lock(&di->di_tv, deep, lock, FALSE);
2279 }
2280 }
Bram Moolenaar7e0868e2020-04-19 17:24:53 +02002281 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002282 }
2283 *name_end = cc;
2284 }
Ernie Raelee865f32023-09-29 19:53:55 +02002285 else if (deep == 0 && lp->ll_object == NULL && lp->ll_class == NULL)
Bram Moolenaara187c432020-09-16 21:08:28 +02002286 {
2287 // nothing to do
2288 }
Ernie Raelee865f32023-09-29 19:53:55 +02002289 else if (lp->ll_is_root)
2290 // (un)lock the item.
2291 item_lock(lp->ll_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002292 else if (lp->ll_range)
2293 {
2294 listitem_T *li = lp->ll_li;
2295
2296 // (un)lock a range of List items.
2297 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
2298 {
Bram Moolenaar021bda52020-08-17 21:07:22 +02002299 item_lock(&li->li_tv, deep, lock, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002300 li = li->li_next;
2301 ++lp->ll_n1;
2302 }
2303 }
2304 else if (lp->ll_list != NULL)
2305 // (un)lock a List item.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002306 item_lock(&lp->ll_li->li_tv, deep, lock, FALSE);
Ernie Raelee865f32023-09-29 19:53:55 +02002307 else if (lp->ll_object != NULL) // This check must be before ll_class.
2308 {
2309 // (un)lock an object variable.
2310 report_lockvar_member(e_cannot_lock_object_variable_str, lp);
2311 ret = FAIL;
2312 }
2313 else if (lp->ll_class != NULL)
2314 {
2315 // (un)lock a class variable.
2316 report_lockvar_member(e_cannot_lock_class_variable_str, lp);
2317 ret = FAIL;
2318 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002319 else
Ernie Raelee865f32023-09-29 19:53:55 +02002320 {
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002321 // (un)lock a Dictionary item.
Ernie Raelee865f32023-09-29 19:53:55 +02002322 if (lp->ll_di == NULL)
2323 {
2324 emsg(_(e_dictionary_required));
2325 ret = FAIL;
2326 }
2327 else
2328 item_lock(&lp->ll_di->di_tv, deep, lock, FALSE);
2329 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002330
2331 return ret;
2332}
2333
2334/*
2335 * Lock or unlock an item. "deep" is nr of levels to go.
Bram Moolenaar021bda52020-08-17 21:07:22 +02002336 * When "check_refcount" is TRUE do not lock a list or dict with a reference
2337 * count larger than 1.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002338 */
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002339 void
Bram Moolenaar021bda52020-08-17 21:07:22 +02002340item_lock(typval_T *tv, int deep, int lock, int check_refcount)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002341{
2342 static int recurse = 0;
2343 list_T *l;
2344 listitem_T *li;
2345 dict_T *d;
2346 blob_T *b;
2347 hashitem_T *hi;
2348 int todo;
2349
Ernie Raelee865f32023-09-29 19:53:55 +02002350#ifdef LOG_LOCKVAR
Ernie Rael64885642023-10-04 20:16:22 +02002351 ch_log(NULL, "LKVAR: item_lock(): type %s", vartype_name(tv->v_type));
Ernie Raelee865f32023-09-29 19:53:55 +02002352#endif
2353
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002354 if (recurse >= DICT_MAXNEST)
2355 {
Bram Moolenaar677658a2022-01-05 16:09:06 +00002356 emsg(_(e_variable_nested_too_deep_for_unlock));
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002357 return;
2358 }
2359 if (deep == 0)
2360 return;
2361 ++recurse;
2362
2363 // lock/unlock the item itself
2364 if (lock)
2365 tv->v_lock |= VAR_LOCKED;
2366 else
2367 tv->v_lock &= ~VAR_LOCKED;
2368
2369 switch (tv->v_type)
2370 {
2371 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002372 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 case VAR_VOID:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002374 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 case VAR_BOOL:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002376 case VAR_STRING:
2377 case VAR_FUNC:
2378 case VAR_PARTIAL:
2379 case VAR_FLOAT:
2380 case VAR_SPECIAL:
2381 case VAR_JOB:
2382 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02002383 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00002384 case VAR_CLASS:
2385 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02002386 case VAR_TYPEALIAS:
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002387 break;
2388
2389 case VAR_BLOB:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002390 if ((b = tv->vval.v_blob) != NULL
2391 && !(check_refcount && b->bv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002392 {
2393 if (lock)
2394 b->bv_lock |= VAR_LOCKED;
2395 else
2396 b->bv_lock &= ~VAR_LOCKED;
2397 }
2398 break;
2399 case VAR_LIST:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002400 if ((l = tv->vval.v_list) != NULL
2401 && !(check_refcount && l->lv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002402 {
2403 if (lock)
2404 l->lv_lock |= VAR_LOCKED;
2405 else
2406 l->lv_lock &= ~VAR_LOCKED;
Bram Moolenaar70c43d82022-01-26 21:01:15 +00002407 if (deep < 0 || deep > 1)
2408 {
2409 if (l->lv_first == &range_list_item)
2410 l->lv_lock |= VAR_ITEMS_LOCKED;
2411 else
2412 {
2413 // recursive: lock/unlock the items the List contains
2414 CHECK_LIST_MATERIALIZE(l);
2415 FOR_ALL_LIST_ITEMS(l, li) item_lock(&li->li_tv,
2416 deep - 1, lock, check_refcount);
2417 }
2418 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002419 }
2420 break;
2421 case VAR_DICT:
Bram Moolenaar021bda52020-08-17 21:07:22 +02002422 if ((d = tv->vval.v_dict) != NULL
2423 && !(check_refcount && d->dv_refcount > 1))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002424 {
2425 if (lock)
2426 d->dv_lock |= VAR_LOCKED;
2427 else
2428 d->dv_lock &= ~VAR_LOCKED;
2429 if (deep < 0 || deep > 1)
2430 {
2431 // recursive: lock/unlock the items the List contains
2432 todo = (int)d->dv_hashtab.ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00002433 FOR_ALL_HASHTAB_ITEMS(&d->dv_hashtab, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002434 {
2435 if (!HASHITEM_EMPTY(hi))
2436 {
2437 --todo;
Bram Moolenaar021bda52020-08-17 21:07:22 +02002438 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock,
2439 check_refcount);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002440 }
2441 }
2442 }
2443 }
2444 }
2445 --recurse;
2446}
2447
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002448#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
2449/*
2450 * Delete all "menutrans_" variables.
2451 */
2452 void
2453del_menutrans_vars(void)
2454{
2455 hashitem_T *hi;
2456 int todo;
2457
2458 hash_lock(&globvarht);
2459 todo = (int)globvarht.ht_used;
2460 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
2461 {
2462 if (!HASHITEM_EMPTY(hi))
2463 {
2464 --todo;
2465 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
2466 delete_var(&globvarht, hi);
2467 }
2468 }
2469 hash_unlock(&globvarht);
2470}
2471#endif
2472
Bram Moolenaar0522ba02019-08-27 22:48:30 +02002473/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002474 * Local string buffer for the next two functions to store a variable name
2475 * with its prefix. Allocated in cat_prefix_varname(), freed later in
2476 * get_user_var_name().
2477 */
2478
2479static char_u *varnamebuf = NULL;
2480static int varnamebuflen = 0;
2481
2482/*
2483 * Function to concatenate a prefix and a variable name.
2484 */
Bram Moolenaar1bb4de52021-01-13 19:48:46 +01002485 char_u *
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002486cat_prefix_varname(int prefix, char_u *name)
2487{
2488 int len;
2489
2490 len = (int)STRLEN(name) + 3;
2491 if (len > varnamebuflen)
2492 {
2493 vim_free(varnamebuf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002494 len += 10; // some additional space
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002495 varnamebuf = alloc(len);
2496 if (varnamebuf == NULL)
2497 {
2498 varnamebuflen = 0;
2499 return NULL;
2500 }
2501 varnamebuflen = len;
2502 }
2503 *varnamebuf = prefix;
2504 varnamebuf[1] = ':';
2505 STRCPY(varnamebuf + 2, name);
2506 return varnamebuf;
2507}
2508
2509/*
2510 * Function given to ExpandGeneric() to obtain the list of user defined
2511 * (global/buffer/window/built-in) variable names.
2512 */
2513 char_u *
2514get_user_var_name(expand_T *xp, int idx)
2515{
2516 static long_u gdone;
2517 static long_u bdone;
2518 static long_u wdone;
2519 static long_u tdone;
2520 static int vidx;
2521 static hashitem_T *hi;
2522 hashtab_T *ht;
2523
2524 if (idx == 0)
2525 {
2526 gdone = bdone = wdone = vidx = 0;
2527 tdone = 0;
2528 }
2529
2530 // Global variables
2531 if (gdone < globvarht.ht_used)
2532 {
2533 if (gdone++ == 0)
2534 hi = globvarht.ht_array;
2535 else
2536 ++hi;
2537 while (HASHITEM_EMPTY(hi))
2538 ++hi;
2539 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
2540 return cat_prefix_varname('g', hi->hi_key);
2541 return hi->hi_key;
2542 }
2543
2544 // b: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002545 ht = &prevwin_curwin()->w_buffer->b_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002546 if (bdone < ht->ht_used)
2547 {
2548 if (bdone++ == 0)
2549 hi = ht->ht_array;
2550 else
2551 ++hi;
2552 while (HASHITEM_EMPTY(hi))
2553 ++hi;
2554 return cat_prefix_varname('b', hi->hi_key);
2555 }
2556
2557 // w: variables
Bram Moolenaar0f6e28f2022-02-20 20:49:35 +00002558 ht = &prevwin_curwin()->w_vars->dv_hashtab;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002559 if (wdone < ht->ht_used)
2560 {
2561 if (wdone++ == 0)
2562 hi = ht->ht_array;
2563 else
2564 ++hi;
2565 while (HASHITEM_EMPTY(hi))
2566 ++hi;
2567 return cat_prefix_varname('w', hi->hi_key);
2568 }
2569
2570 // t: variables
2571 ht = &curtab->tp_vars->dv_hashtab;
2572 if (tdone < ht->ht_used)
2573 {
2574 if (tdone++ == 0)
2575 hi = ht->ht_array;
2576 else
2577 ++hi;
2578 while (HASHITEM_EMPTY(hi))
2579 ++hi;
2580 return cat_prefix_varname('t', hi->hi_key);
2581 }
2582
2583 // v: variables
2584 if (vidx < VV_LEN)
2585 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
2586
2587 VIM_CLEAR(varnamebuf);
2588 varnamebuflen = 0;
2589 return NULL;
2590}
2591
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002592 char *
2593get_var_special_name(int nr)
2594{
2595 switch (nr)
2596 {
Bram Moolenaara8b8af12021-01-01 15:11:04 +01002597 case VVAL_FALSE: return in_vim9script() ? "false" : "v:false";
2598 case VVAL_TRUE: return in_vim9script() ? "true" : "v:true";
Bram Moolenaar67977822021-01-03 21:53:53 +01002599 case VVAL_NULL: return in_vim9script() ? "null" : "v:null";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002600 case VVAL_NONE: return "v:none";
Bram Moolenaarda6c0332019-09-01 16:01:30 +02002601 }
2602 internal_error("get_var_special_name()");
2603 return "42";
2604}
2605
2606/*
2607 * Returns the global variable dictionary
2608 */
2609 dict_T *
2610get_globvar_dict(void)
2611{
2612 return &globvardict;
2613}
2614
2615/*
2616 * Returns the global variable hash table
2617 */
2618 hashtab_T *
2619get_globvar_ht(void)
2620{
2621 return &globvarht;
2622}
2623
2624/*
2625 * Returns the v: variable dictionary
2626 */
2627 dict_T *
2628get_vimvar_dict(void)
2629{
2630 return &vimvardict;
2631}
2632
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002633/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 * Returns the index of a v:variable. Negative if not found.
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002635 * Returns DI_ flags in "di_flags".
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 */
2637 int
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002638find_vim_var(char_u *name, int *di_flags)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639{
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002640 dictitem_T *di = find_var_in_ht(&vimvarht, 0, name, TRUE);
2641 struct vimvar *vv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642
2643 if (di == NULL)
2644 return -1;
Bram Moolenaar5da356e2020-04-09 19:34:43 +02002645 *di_flags = di->di_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 vv = (struct vimvar *)((char *)di - offsetof(vimvar_T, vv_di));
2647 return (int)(vv - vimvars);
2648}
2649
2650
2651/*
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002652 * Set type of v: variable to "type".
2653 */
2654 void
2655set_vim_var_type(int idx, vartype_T type)
2656{
Bram Moolenaard787e402021-12-24 21:36:12 +00002657 vimvars[idx].vv_tv_type = type;
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02002658}
2659
2660/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002661 * Set number v: variable to "val".
Bram Moolenaar8d71b542019-08-30 15:46:30 +02002662 * Note that this does not set the type, use set_vim_var_type() for that.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002663 */
2664 void
2665set_vim_var_nr(int idx, varnumber_T val)
2666{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002667 vimvars[idx].vv_nr = val;
2668}
2669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 char *
2671get_vim_var_name(int idx)
2672{
2673 return vimvars[idx].vv_name;
2674}
2675
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002676/*
2677 * Get typval_T v: variable value.
2678 */
2679 typval_T *
2680get_vim_var_tv(int idx)
2681{
2682 return &vimvars[idx].vv_tv;
2683}
2684
Bram Moolenaard787e402021-12-24 21:36:12 +00002685 type_T *
2686get_vim_var_type(int idx, garray_T *type_list)
2687{
2688 if (vimvars[idx].vv_type != NULL)
2689 return vimvars[idx].vv_type;
2690 return typval2type_vimvar(&vimvars[idx].vv_tv, type_list);
2691}
2692
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002693/*
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002694 * Set v: variable to "tv". Only accepts the same type.
2695 * Takes over the value of "tv".
2696 */
2697 int
2698set_vim_var_tv(int idx, typval_T *tv)
2699{
Bram Moolenaard787e402021-12-24 21:36:12 +00002700 if (vimvars[idx].vv_tv_type != tv->v_type)
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002701 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002702 emsg(_(e_type_mismatch_for_v_variable));
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002703 clear_tv(tv);
2704 return FAIL;
2705 }
Bram Moolenaarcab27672020-04-09 20:10:55 +02002706 // VV_RO is also checked when compiling, but let's check here as well.
2707 if (vimvars[idx].vv_flags & VV_RO)
2708 {
Bram Moolenaard8e44472021-07-21 22:20:33 +02002709 semsg(_(e_cannot_change_readonly_variable_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002710 return FAIL;
2711 }
2712 if (sandbox && (vimvars[idx].vv_flags & VV_RO_SBX))
2713 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002714 semsg(_(e_cannot_set_variable_in_sandbox_str), vimvars[idx].vv_name);
Bram Moolenaarcab27672020-04-09 20:10:55 +02002715 return FAIL;
2716 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002717 clear_tv(&vimvars[idx].vv_di.di_tv);
2718 vimvars[idx].vv_di.di_tv = *tv;
2719 return OK;
2720}
2721
2722/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002723 * Get number v: variable value.
2724 */
2725 varnumber_T
2726get_vim_var_nr(int idx)
2727{
2728 return vimvars[idx].vv_nr;
2729}
2730
2731/*
2732 * Get string v: variable value. Uses a static buffer, can only be used once.
2733 * If the String variable has never been set, return an empty string.
2734 * Never returns NULL;
2735 */
2736 char_u *
2737get_vim_var_str(int idx)
2738{
2739 return tv_get_string(&vimvars[idx].vv_tv);
2740}
2741
2742/*
2743 * Get List v: variable value. Caller must take care of reference count when
2744 * needed.
2745 */
2746 list_T *
2747get_vim_var_list(int idx)
2748{
2749 return vimvars[idx].vv_list;
2750}
2751
2752/*
2753 * Get Dict v: variable value. Caller must take care of reference count when
2754 * needed.
2755 */
2756 dict_T *
2757get_vim_var_dict(int idx)
2758{
2759 return vimvars[idx].vv_dict;
2760}
2761
2762/*
2763 * Set v:char to character "c".
2764 */
2765 void
2766set_vim_var_char(int c)
2767{
2768 char_u buf[MB_MAXBYTES + 1];
2769
2770 if (has_mbyte)
2771 buf[(*mb_char2bytes)(c, buf)] = NUL;
2772 else
2773 {
2774 buf[0] = c;
2775 buf[1] = NUL;
2776 }
2777 set_vim_var_string(VV_CHAR, buf, -1);
2778}
2779
2780/*
2781 * Set v:count to "count" and v:count1 to "count1".
2782 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
2783 */
2784 void
2785set_vcount(
2786 long count,
2787 long count1,
2788 int set_prevcount)
2789{
2790 if (set_prevcount)
2791 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
2792 vimvars[VV_COUNT].vv_nr = count;
2793 vimvars[VV_COUNT1].vv_nr = count1;
2794}
2795
2796/*
2797 * Save variables that might be changed as a side effect. Used when executing
2798 * a timer callback.
2799 */
2800 void
2801save_vimvars(vimvars_save_T *vvsave)
2802{
2803 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
2804 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
2805 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
2806}
2807
2808/*
2809 * Restore variables saved by save_vimvars().
2810 */
2811 void
2812restore_vimvars(vimvars_save_T *vvsave)
2813{
2814 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
2815 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
2816 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
2817}
2818
2819/*
2820 * Set string v: variable to a copy of "val". If 'copy' is FALSE, then set the
2821 * value.
2822 */
2823 void
2824set_vim_var_string(
2825 int idx,
2826 char_u *val,
2827 int len) // length of "val" to use or -1 (whole string)
2828{
2829 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002830 vimvars[idx].vv_tv_type = VAR_STRING;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002831 if (val == NULL)
2832 vimvars[idx].vv_str = NULL;
2833 else if (len == -1)
2834 vimvars[idx].vv_str = vim_strsave(val);
2835 else
2836 vimvars[idx].vv_str = vim_strnsave(val, len);
2837}
2838
2839/*
2840 * Set List v: variable to "val".
2841 */
2842 void
2843set_vim_var_list(int idx, list_T *val)
2844{
2845 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002846 vimvars[idx].vv_tv_type = VAR_LIST;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002847 vimvars[idx].vv_list = val;
2848 if (val != NULL)
2849 ++val->lv_refcount;
2850}
2851
2852/*
2853 * Set Dictionary v: variable to "val".
2854 */
2855 void
2856set_vim_var_dict(int idx, dict_T *val)
2857{
2858 clear_tv(&vimvars[idx].vv_di.di_tv);
Bram Moolenaard787e402021-12-24 21:36:12 +00002859 vimvars[idx].vv_tv_type = VAR_DICT;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002860 vimvars[idx].vv_dict = val;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00002861 if (val == NULL)
2862 return;
2863
2864 ++val->dv_refcount;
2865 dict_set_items_ro(val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002866}
2867
2868/*
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002869 * Set the v:argv list.
2870 */
2871 void
2872set_argv_var(char **argv, int argc)
2873{
2874 list_T *l = list_alloc();
2875 int i;
2876
2877 if (l == NULL)
2878 getout(1);
2879 l->lv_lock = VAR_FIXED;
2880 for (i = 0; i < argc; ++i)
2881 {
2882 if (list_append_string(l, (char_u *)argv[i], -1) == FAIL)
2883 getout(1);
Bram Moolenaar0ff6aad2020-01-29 21:27:21 +01002884 l->lv_u.mat.lv_last->li_tv.v_lock = VAR_FIXED;
Bram Moolenaar69bf6342019-10-29 04:16:57 +01002885 }
2886 set_vim_var_list(VV_ARGV, l);
2887}
2888
2889/*
Bram Moolenaar439c0362020-06-06 15:58:03 +02002890 * Reset v:register, taking the 'clipboard' setting into account.
2891 */
2892 void
2893reset_reg_var(void)
2894{
2895 int regname = 0;
2896
2897 // Adjust the register according to 'clipboard', so that when
2898 // "unnamed" is present it becomes '*' or '+' instead of '"'.
2899#ifdef FEAT_CLIPBOARD
2900 adjust_clip_reg(&regname);
2901#endif
2902 set_reg_var(regname);
2903}
2904
2905/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02002906 * Set v:register if needed.
2907 */
2908 void
2909set_reg_var(int c)
2910{
2911 char_u regname;
2912
2913 if (c == 0 || c == ' ')
2914 regname = '"';
2915 else
2916 regname = c;
2917 // Avoid free/alloc when the value is already right.
2918 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
2919 set_vim_var_string(VV_REG, &regname, 1);
2920}
2921
2922/*
2923 * Get or set v:exception. If "oldval" == NULL, return the current value.
2924 * Otherwise, restore the value to "oldval" and return NULL.
2925 * Must always be called in pairs to save and restore v:exception! Does not
2926 * take care of memory allocations.
2927 */
2928 char_u *
2929v_exception(char_u *oldval)
2930{
2931 if (oldval == NULL)
2932 return vimvars[VV_EXCEPTION].vv_str;
2933
2934 vimvars[VV_EXCEPTION].vv_str = oldval;
2935 return NULL;
2936}
2937
2938/*
2939 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
2940 * Otherwise, restore the value to "oldval" and return NULL.
2941 * Must always be called in pairs to save and restore v:throwpoint! Does not
2942 * take care of memory allocations.
2943 */
2944 char_u *
2945v_throwpoint(char_u *oldval)
2946{
2947 if (oldval == NULL)
2948 return vimvars[VV_THROWPOINT].vv_str;
2949
2950 vimvars[VV_THROWPOINT].vv_str = oldval;
2951 return NULL;
2952}
2953
2954/*
2955 * Set v:cmdarg.
2956 * If "eap" != NULL, use "eap" to generate the value and return the old value.
2957 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
2958 * Must always be called in pairs!
2959 */
2960 char_u *
2961set_cmdarg(exarg_T *eap, char_u *oldarg)
2962{
2963 char_u *oldval;
2964 char_u *newval;
2965 unsigned len;
2966
2967 oldval = vimvars[VV_CMDARG].vv_str;
2968 if (eap == NULL)
2969 {
2970 vim_free(oldval);
2971 vimvars[VV_CMDARG].vv_str = oldarg;
2972 return NULL;
2973 }
2974
2975 if (eap->force_bin == FORCE_BIN)
2976 len = 6;
2977 else if (eap->force_bin == FORCE_NOBIN)
2978 len = 8;
2979 else
2980 len = 0;
2981
2982 if (eap->read_edit)
2983 len += 7;
2984
2985 if (eap->force_ff != 0)
2986 len += 10; // " ++ff=unix"
2987 if (eap->force_enc != 0)
2988 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
2989 if (eap->bad_char != 0)
2990 len += 7 + 4; // " ++bad=" + "keep" or "drop"
2991
2992 newval = alloc(len + 1);
2993 if (newval == NULL)
2994 return NULL;
2995
2996 if (eap->force_bin == FORCE_BIN)
2997 sprintf((char *)newval, " ++bin");
2998 else if (eap->force_bin == FORCE_NOBIN)
2999 sprintf((char *)newval, " ++nobin");
3000 else
3001 *newval = NUL;
3002
3003 if (eap->read_edit)
3004 STRCAT(newval, " ++edit");
3005
3006 if (eap->force_ff != 0)
3007 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
3008 eap->force_ff == 'u' ? "unix"
3009 : eap->force_ff == 'd' ? "dos"
3010 : "mac");
3011 if (eap->force_enc != 0)
3012 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
3013 eap->cmd + eap->force_enc);
3014 if (eap->bad_char == BAD_KEEP)
3015 STRCPY(newval + STRLEN(newval), " ++bad=keep");
3016 else if (eap->bad_char == BAD_DROP)
3017 STRCPY(newval + STRLEN(newval), " ++bad=drop");
3018 else if (eap->bad_char != 0)
3019 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
3020 vimvars[VV_CMDARG].vv_str = newval;
3021 return oldval;
3022}
3023
3024/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003025 * Get the value of internal variable "name".
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003026 * If "flags" has EVAL_VAR_IMPORT may return a VAR_ANY with v_number set to the
3027 * imported script ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003028 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3029 */
3030 int
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003031eval_variable(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003032 char_u *name,
Bram Moolenaar94674f22023-01-06 18:42:20 +00003033 int len, // length of "name" or zero
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003034 scid_T sid, // script ID for imported item or zero
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003035 typval_T *rettv, // NULL when only checking existence
3036 dictitem_T **dip, // non-NULL when typval's dict item is needed
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003037 int flags) // EVAL_VAR_ flags
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003038{
3039 int ret = OK;
3040 typval_T *tv = NULL;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003041 int found = FALSE;
Bram Moolenaarf055d452021-07-08 20:57:24 +02003042 hashtab_T *ht = NULL;
Bram Moolenaar94674f22023-01-06 18:42:20 +00003043 int cc = 0;
Bram Moolenaarc967d572021-07-08 21:38:50 +02003044 type_T *type = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003045
Bram Moolenaar94674f22023-01-06 18:42:20 +00003046 if (len > 0)
3047 {
3048 // truncate the name, so that we can use strcmp()
3049 cc = name[len];
3050 name[len] = NUL;
3051 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003052
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003053 // Check for local variable when debugging.
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003054 if ((sid == 0) && (tv = lookup_debug_var(name)) == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003055 {
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003056 // Check for user-defined variables.
Bram Moolenaarc967d572021-07-08 21:38:50 +02003057 dictitem_T *v = find_var(name, &ht, flags & EVAL_VAR_NOAUTOLOAD);
3058
Bram Moolenaar1b0a9dd2021-06-14 21:32:21 +02003059 if (v != NULL)
3060 {
3061 tv = &v->di_tv;
3062 if (dip != NULL)
3063 *dip = v;
3064 }
Bram Moolenaarc967d572021-07-08 21:38:50 +02003065 else
3066 ht = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003067 }
3068
Bram Moolenaareb6880b2020-07-12 17:07:05 +02003069 if (tv == NULL && (in_vim9script() || STRNCMP(name, "s:", 2) == 0))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003071 imported_T *import = NULL;
Bram Moolenaar9721fb42020-06-11 23:10:46 +02003072 char_u *p = STRNCMP(name, "s:", 2) == 0 ? name + 2 : name;
3073
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003074 if (sid == 0)
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003075 import = find_imported(p, 0, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076
3077 // imported variable from another script
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003078 if (import != NULL || sid != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003080 if ((flags & EVAL_VAR_IMPORT) == 0)
Bram Moolenaarc620c052020-07-08 15:16:19 +02003081 {
Bram Moolenaar71f21932022-01-07 18:20:55 +00003082 if (SCRIPT_ID_VALID(sid))
Bram Moolenaarc620c052020-07-08 15:16:19 +02003083 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003084 ht = &SCRIPT_VARS(sid);
3085 if (ht != NULL)
3086 {
3087 dictitem_T *v = find_var_in_ht(ht, 0, name,
3088 flags & EVAL_VAR_NOAUTOLOAD);
3089
3090 if (v != NULL)
3091 {
3092 tv = &v->di_tv;
3093 if (dip != NULL)
3094 *dip = v;
3095 }
3096 else
3097 ht = NULL;
3098 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003099 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003100 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003101 {
3102 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003103 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003104 ret = FAIL;
3105 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003106 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003107 else
3108 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003109 if (rettv != NULL)
3110 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003111 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003112 rettv->v_type = VAR_ANY;
3113 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3114 }
3115 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003116 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003118 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003119 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003120 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003121 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003122
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003123 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003124 // function name. For a global non-autoload function "g:" is
3125 // required.
3126 if (ufunc != NULL && (has_g_prefix
3127 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003128 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003129 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003130 if (rettv != NULL)
3131 {
3132 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003133 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003134 // Keep the "g:", otherwise script-local may be
3135 // assumed.
3136 rettv->vval.v_string = vim_strsave(name);
3137 else
John Marriottb32800f2025-02-01 15:25:34 +01003138 rettv->vval.v_string = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003139 if (rettv->vval.v_string != NULL)
3140 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003141 }
3142 }
3143 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 }
3145
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003146 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003147 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003148 if (tv == NULL)
3149 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003150 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003151 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003152 ret = FAIL;
3153 }
3154 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003155 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003156 svar_T *sv = NULL;
3157 int was_assigned = FALSE;
3158
Bram Moolenaar11005b02021-07-11 20:59:00 +02003159 if (ht != NULL && ht == get_script_local_ht()
3160 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003161 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003162 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003163 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003164 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003165 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003166 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3167 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003168 }
3169
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003170 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3171 && sid != 0)
3172 {
3173 // type alias or class imported from another script. Check
3174 // whether it is exported from the other script.
3175 sv = find_typval_in_script(tv, sid, TRUE);
3176 if (sv == NULL)
3177 {
3178 ret = FAIL;
3179 goto done;
3180 }
3181 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3182 {
3183 semsg(_(e_item_not_exported_in_script_str), name);
3184 ret = FAIL;
3185 goto done;
3186 }
3187 }
3188
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003189 // If a list or dict variable wasn't initialized and has meaningful
3190 // type, do it now. Not for global variables, they are not
3191 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003192 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003193 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003194 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003195 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003196 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003197 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003198 tv->vval.v_dict = dict_alloc();
3199 if (tv->vval.v_dict != NULL)
3200 {
3201 ++tv->vval.v_dict->dv_refcount;
3202 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003203 if (sv != NULL)
3204 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003205 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003206 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003207 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003208 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003209 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003210 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003211 tv->vval.v_list = list_alloc();
3212 if (tv->vval.v_list != NULL)
3213 {
3214 ++tv->vval.v_list->lv_refcount;
3215 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003216 if (sv != NULL)
3217 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003218 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003219 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003220 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003221 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003222 || !in_vim9script()))
3223 {
3224 tv->vval.v_blob = blob_alloc();
3225 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003226 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003227 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003228 if (sv != NULL)
3229 sv->sv_flags |= SVFLAG_ASSIGNED;
3230 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003231 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003232 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003233 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003234 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003235 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003236
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003237done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003238 if (len > 0)
3239 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003240
3241 return ret;
3242}
3243
3244/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003245 * Get the value of internal variable "name", also handling "import.name".
3246 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3247 */
3248 int
3249eval_variable_import(
3250 char_u *name,
3251 typval_T *rettv)
3252{
3253 char_u *s = name;
3254 while (ASCII_ISALNUM(*s) || *s == '_')
3255 ++s;
3256 int len = (int)(s - name);
3257
3258 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3259 return FAIL;
3260 if (rettv->v_type == VAR_ANY && *s == '.')
3261 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003262 char_u *ns = s + 1;
3263 s = ns;
3264 while (ASCII_ISALNUM(*s) || *s == '_')
3265 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003266 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003267 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003268 }
3269 return OK;
3270}
3271
3272
3273/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003274 * Check if variable "name[len]" is a local variable or an argument.
3275 * If so, "*eval_lavars_used" is set to TRUE.
3276 */
3277 void
3278check_vars(char_u *name, int len)
3279{
3280 int cc;
3281 char_u *varname;
3282 hashtab_T *ht;
3283
3284 if (eval_lavars_used == NULL)
3285 return;
3286
3287 // truncate the name, so that we can use strcmp()
3288 cc = name[len];
3289 name[len] = NUL;
3290
3291 ht = find_var_ht(name, &varname);
3292 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3293 {
3294 if (find_var(name, NULL, TRUE) != NULL)
3295 *eval_lavars_used = TRUE;
3296 }
3297
3298 name[len] = cc;
3299}
3300
3301/*
3302 * Find variable "name" in the list of variables.
3303 * Return a pointer to it if found, NULL if not found.
3304 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003305 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003306 */
3307 dictitem_T *
3308find_var(char_u *name, hashtab_T **htp, int no_autoload)
3309{
3310 char_u *varname;
3311 hashtab_T *ht;
3312 dictitem_T *ret = NULL;
3313
3314 ht = find_var_ht(name, &varname);
3315 if (htp != NULL)
3316 *htp = ht;
3317 if (ht == NULL)
3318 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003319 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003320 if (ret != NULL)
3321 return ret;
3322
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003323 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003324 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003325 if (ret != NULL)
3326 return ret;
3327
3328 // in Vim9 script items without a scope can be script-local
3329 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3330 {
3331 ht = get_script_local_ht();
3332 if (ht != NULL)
3333 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003334 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003335 if (ret != NULL)
3336 {
3337 if (htp != NULL)
3338 *htp = ht;
3339 return ret;
3340 }
3341 }
3342 }
3343
Ernie Rael84f6dc72024-04-21 14:45:48 +02003344 // and finally try
3345 return find_var_autoload_prefix(name, 0, htp, NULL);
3346}
3347
3348/*
3349 * Find variable "name" with sn_autoload_prefix.
3350 * Return a pointer to it if found, NULL if not found.
3351 * When "sid" > 0, use it otherwise use "current_sctx.sc_sid".
3352 * When "htp" is not NULL set "htp" to the hashtab_T used.
3353 * When "namep" is not NULL set "namep" to the generated name, and
3354 * then the caller gets ownership and is responsible for freeing the name.
3355 */
3356 dictitem_T *
3357find_var_autoload_prefix(char_u *name, int sid, hashtab_T **htp,
3358 char_u **namep)
3359{
3360 hashtab_T *ht;
3361 dictitem_T *ret = NULL;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003362 // When using "vim9script autoload" script-local items are prefixed but can
3363 // be used with s:name.
Ernie Rael84f6dc72024-04-21 14:45:48 +02003364 int check_sid = sid > 0 ? sid : current_sctx.sc_sid;
3365 if (SCRIPT_ID_VALID(check_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003366 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003367 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003368 scriptitem_T *si = SCRIPT_ITEM(check_sid);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003369
3370 if (si->sn_autoload_prefix != NULL)
3371 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003372 char_u *base_name = (name[0] == 's' && name[1] == ':')
3373 ? name + 2 : name;
3374 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003375
3376 if (auto_name != NULL)
3377 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003378 int free_auto_name = TRUE;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003379 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003380 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003381 if (ret != NULL)
3382 {
3383 if (htp != NULL)
3384 *htp = ht;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003385 if (namep != NULL)
3386 {
3387 free_auto_name = FALSE;
3388 *namep = auto_name;
3389 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003390 }
Ernie Rael84f6dc72024-04-21 14:45:48 +02003391 if (free_auto_name)
3392 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003393 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003394 }
3395 }
3396
Ernie Rael84f6dc72024-04-21 14:45:48 +02003397 return ret;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003398}
3399
3400/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003401 * Like find_var() but if the name starts with <SNR>99_ then look in the
3402 * referenced script (used for a funcref).
3403 */
3404 dictitem_T *
3405find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3406{
Keith Thompson184f71c2024-01-04 21:19:04 +01003407 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003408 {
3409 char_u *p = name + 5;
3410 int sid = getdigits(&p);
3411
3412 if (SCRIPT_ID_VALID(sid) && *p == '_')
3413 {
3414 hashtab_T *ht = &SCRIPT_VARS(sid);
3415
3416 if (ht != NULL)
3417 {
3418 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3419
3420 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003421 {
3422 if (htp != NULL)
3423 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003424 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003425 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003426 }
3427 }
3428 }
3429
3430 return find_var(name, htp, no_autoload);
3431}
3432
3433/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003434 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003435 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003436 * Returns NULL if not found.
3437 */
3438 dictitem_T *
3439find_var_in_ht(
3440 hashtab_T *ht,
3441 int htname,
3442 char_u *varname,
3443 int no_autoload)
3444{
3445 hashitem_T *hi;
3446
3447 if (*varname == NUL)
3448 {
3449 // Must be something like "s:", otherwise "ht" would be NULL.
3450 switch (htname)
3451 {
3452 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3453 case 'g': return &globvars_var;
3454 case 'v': return &vimvars_var;
3455 case 'b': return &curbuf->b_bufvar;
3456 case 'w': return &curwin->w_winvar;
3457 case 't': return &curtab->tp_winvar;
3458 case 'l': return get_funccal_local_var();
3459 case 'a': return get_funccal_args_var();
3460 }
3461 return NULL;
3462 }
3463
3464 hi = hash_find(ht, varname);
3465 if (HASHITEM_EMPTY(hi))
3466 {
3467 // For global variables we may try auto-loading the script. If it
3468 // worked find the variable again. Don't auto-load a script if it was
3469 // loaded already, otherwise it would be loaded every time when
3470 // checking if a function name is a Funcref variable.
3471 if (ht == &globvarht && !no_autoload)
3472 {
3473 // Note: script_autoload() may make "hi" invalid. It must either
3474 // be obtained again or not used.
3475 if (!script_autoload(varname, FALSE) || aborting())
3476 return NULL;
3477 hi = hash_find(ht, varname);
3478 }
3479 if (HASHITEM_EMPTY(hi))
3480 return NULL;
3481 }
3482 return HI2DI(hi);
3483}
3484
3485/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 * Get the script-local hashtab. NULL if not in a script context.
3487 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003488 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489get_script_local_ht(void)
3490{
3491 scid_T sid = current_sctx.sc_sid;
3492
Bram Moolenaare3d46852020-08-29 13:39:17 +02003493 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003494 return &SCRIPT_VARS(sid);
3495 return NULL;
3496}
3497
3498/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003499 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003500 * When "cmd" is TRUE it must look like a command, a function must be followed
3501 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003502 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003504 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003505lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003506 char_u *name,
3507 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003508 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003509 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003510{
3511 hashtab_T *ht = get_script_local_ht();
3512 char_u buffer[30];
3513 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003514 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003516 int is_global = FALSE;
3517 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518
3519 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003520 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521 if (len < sizeof(buffer) - 1)
3522 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003523 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 vim_strncpy(buffer, name, len);
3525 p = buffer;
3526 }
3527 else
3528 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003529 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003530 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003531 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 }
3533
3534 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003535 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536
Ernie Rael84f6dc72024-04-21 14:45:48 +02003537 // if not script-local, then perhaps autoload-exported
3538 if (res == FAIL && find_var_autoload_prefix(p, 0, NULL, NULL) != NULL)
3539 res = OK;
3540
3541 // if not script-local or autoload, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003542 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003543 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 if (p != buffer)
3545 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003546
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003547 // Find a function, so that a following "->" works.
3548 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3549 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003550 if (res != OK)
3551 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003552 p = skipwhite(name + len);
3553
3554 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003555 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003556 // Do not check for an internal function, since it might also be a
3557 // valid command, such as ":split" versus "split()".
3558 // Skip "g:" before a function name.
3559 if (name[0] == 'g' && name[1] == ':')
3560 {
3561 is_global = TRUE;
3562 fname = name + 2;
3563 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003564 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003565 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003566 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003567 }
3568
Bram Moolenaar709664c2020-12-12 14:33:41 +01003569 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570}
3571
3572/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003573 * Find the hashtab used for a variable name.
3574 * Return NULL if the name is not valid.
3575 * Set "varname" to the start of name without ':'.
3576 */
3577 hashtab_T *
3578find_var_ht(char_u *name, char_u **varname)
3579{
3580 hashitem_T *hi;
3581 hashtab_T *ht;
3582
3583 if (name[0] == NUL)
3584 return NULL;
3585 if (name[1] != ':')
3586 {
3587 // The name must not start with a colon or #.
3588 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3589 return NULL;
3590 *varname = name;
3591
3592 // "version" is "v:version" in all scopes if scriptversion < 3.
3593 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003594 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003595 {
3596 hi = hash_find(&compat_hashtab, name);
3597 if (!HASHITEM_EMPTY(hi))
3598 return &compat_hashtab;
3599 }
3600
3601 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602 if (ht != NULL)
3603 return ht; // local variable
3604
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003605 // In Vim9 script items at the script level are script-local, except
3606 // for autoload names.
3607 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 {
3609 ht = get_script_local_ht();
3610 if (ht != NULL)
3611 return ht;
3612 }
3613
3614 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003615 }
3616 *varname = name + 2;
3617 if (*name == 'g') // global variable
3618 return &globvarht;
3619 // There must be no ':' or '#' in the rest of the name, unless g: is used
3620 if (vim_strchr(name + 2, ':') != NULL
3621 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3622 return NULL;
3623 if (*name == 'b') // buffer variable
3624 return &curbuf->b_vars->dv_hashtab;
3625 if (*name == 'w') // window variable
3626 return &curwin->w_vars->dv_hashtab;
3627 if (*name == 't') // tab page variable
3628 return &curtab->tp_vars->dv_hashtab;
3629 if (*name == 'v') // v: variable
3630 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003631 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003632 && get_current_funccal()->fc_func->uf_def_status
3633 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003635 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 if (*name == 'a') // a: function argument
3637 return get_funccal_args_ht();
3638 if (*name == 'l') // l: local function variable
3639 return get_funccal_local_ht();
3640 }
3641 if (*name == 's') // script variable
3642 {
3643 ht = get_script_local_ht();
3644 if (ht != NULL)
3645 return ht;
3646 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003647 return NULL;
3648}
3649
3650/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003651 * Get the string value of a (global/local) variable.
3652 * Note: see tv_get_string() for how long the pointer remains valid.
3653 * Returns NULL when it doesn't exist.
3654 */
3655 char_u *
3656get_var_value(char_u *name)
3657{
3658 dictitem_T *v;
3659
3660 v = find_var(name, NULL, FALSE);
3661 if (v == NULL)
3662 return NULL;
3663 return tv_get_string(&v->di_tv);
3664}
3665
3666/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003667 * Allocate a new hashtab for a sourced script. It will be used while
3668 * sourcing this script and when executing functions defined in the script.
3669 */
3670 void
3671new_script_vars(scid_T id)
3672{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003673 scriptvar_T *sv;
3674
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003675 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3676 if (sv == NULL)
3677 return;
3678 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003679 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003680}
3681
3682/*
3683 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3684 * point to it.
3685 */
3686 void
3687init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3688{
3689 hash_init(&dict->dv_hashtab);
3690 dict->dv_lock = 0;
3691 dict->dv_scope = scope;
3692 dict->dv_refcount = DO_NOT_FREE_CNT;
3693 dict->dv_copyID = 0;
3694 dict_var->di_tv.vval.v_dict = dict;
3695 dict_var->di_tv.v_type = VAR_DICT;
3696 dict_var->di_tv.v_lock = VAR_FIXED;
3697 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3698 dict_var->di_key[0] = NUL;
3699}
3700
3701/*
3702 * Unreference a dictionary initialized by init_var_dict().
3703 */
3704 void
3705unref_var_dict(dict_T *dict)
3706{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003707 // Now the dict needs to be freed if no one else is using it, go back to
3708 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003709 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3710 dict_unref(dict);
3711}
3712
3713/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003714 * Clean up a list of internal variables.
3715 * Frees all allocated variables and the value they contain.
3716 * Clears hashtab "ht", does not free it.
3717 */
3718 void
3719vars_clear(hashtab_T *ht)
3720{
3721 vars_clear_ext(ht, TRUE);
3722}
3723
3724/*
3725 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3726 */
3727 void
3728vars_clear_ext(hashtab_T *ht, int free_val)
3729{
3730 int todo;
3731 hashitem_T *hi;
3732 dictitem_T *v;
3733
3734 hash_lock(ht);
3735 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003736 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003737 {
3738 if (!HASHITEM_EMPTY(hi))
3739 {
3740 --todo;
3741
3742 // Free the variable. Don't remove it from the hashtab,
3743 // ht_array might change then. hash_clear() takes care of it
3744 // later.
3745 v = HI2DI(hi);
3746 if (free_val)
3747 clear_tv(&v->di_tv);
3748 if (v->di_flags & DI_FLAGS_ALLOC)
3749 vim_free(v);
3750 }
3751 }
3752 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003753 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003754}
3755
3756/*
3757 * Delete a variable from hashtab "ht" at item "hi".
3758 * Clear the variable value and free the dictitem.
3759 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003760 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003761delete_var(hashtab_T *ht, hashitem_T *hi)
3762{
3763 dictitem_T *di = HI2DI(hi);
3764
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003765 if (hash_remove(ht, hi, "delete variable") != OK)
3766 return;
3767
3768 clear_tv(&di->di_tv);
3769 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003770}
3771
3772/*
3773 * List the value of one internal variable.
3774 */
3775 static void
3776list_one_var(dictitem_T *v, char *prefix, int *first)
3777{
3778 char_u *tofree;
3779 char_u *s;
3780 char_u numbuf[NUMBUFLEN];
3781
3782 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3783 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3784 s == NULL ? (char_u *)"" : s, first);
3785 vim_free(tofree);
3786}
3787
3788 static void
3789list_one_var_a(
3790 char *prefix,
3791 char_u *name,
3792 int type,
3793 char_u *string,
3794 int *first) // when TRUE clear rest of screen and set to FALSE
3795{
3796 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3797 msg_start();
3798 msg_puts(prefix);
3799 if (name != NULL) // "a:" vars don't have a name stored
3800 msg_puts((char *)name);
3801 msg_putchar(' ');
3802 msg_advance(22);
3803 if (type == VAR_NUMBER)
3804 msg_putchar('#');
3805 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3806 msg_putchar('*');
3807 else if (type == VAR_LIST)
3808 {
3809 msg_putchar('[');
3810 if (*string == '[')
3811 ++string;
3812 }
3813 else if (type == VAR_DICT)
3814 {
3815 msg_putchar('{');
3816 if (*string == '{')
3817 ++string;
3818 }
3819 else
3820 msg_putchar(' ');
3821
3822 msg_outtrans(string);
3823
3824 if (type == VAR_FUNC || type == VAR_PARTIAL)
3825 msg_puts("()");
3826 if (*first)
3827 {
3828 msg_clr_eos();
3829 *first = FALSE;
3830 }
3831}
3832
3833/*
zeertzjqedcba962023-09-24 23:13:51 +02003834 * Addition handling for setting a v: variable.
3835 * Return TRUE if the variable should be set normally,
3836 * FALSE if nothing else needs to be done.
3837 */
3838 int
3839before_set_vvar(
3840 char_u *varname,
3841 dictitem_T *di,
3842 typval_T *tv,
3843 int copy,
3844 int *type_error)
3845{
3846 if (di->di_tv.v_type == VAR_STRING)
3847 {
3848 VIM_CLEAR(di->di_tv.vval.v_string);
3849 if (copy || tv->v_type != VAR_STRING)
3850 {
3851 char_u *val = tv_get_string(tv);
3852
3853 // Careful: when assigning to v:errmsg and
3854 // tv_get_string() causes an error message the variable
3855 // will already be set.
3856 if (di->di_tv.vval.v_string == NULL)
3857 di->di_tv.vval.v_string = vim_strsave(val);
3858 }
3859 else
3860 {
3861 // Take over the string to avoid an extra alloc/free.
3862 di->di_tv.vval.v_string = tv->vval.v_string;
3863 tv->vval.v_string = NULL;
3864 }
3865 return FALSE;
3866 }
3867 else if (di->di_tv.v_type == VAR_NUMBER)
3868 {
3869 di->di_tv.vval.v_number = tv_get_number(tv);
3870 if (STRCMP(varname, "searchforward") == 0)
3871 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3872#ifdef FEAT_SEARCH_EXTRA
3873 else if (STRCMP(varname, "hlsearch") == 0)
3874 {
3875 no_hlsearch = !di->di_tv.vval.v_number;
3876 redraw_all_later(UPD_SOME_VALID);
3877 }
3878#endif
3879 return FALSE;
3880 }
3881 else if (di->di_tv.v_type != tv->v_type)
3882 {
3883 *type_error = TRUE;
3884 return FALSE;
3885 }
3886 return TRUE;
3887}
3888
3889/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003890 * Set variable "name" to value in "tv".
3891 * If the variable already exists, the value is updated.
3892 * Otherwise the variable is created.
3893 */
3894 void
3895set_var(
3896 char_u *name,
3897 typval_T *tv,
3898 int copy) // make copy of value in "tv"
3899{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003900 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003901}
3902
3903/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003904 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003905 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003906 * If the variable already exists and "is_const" is FALSE the value is updated.
3907 * Otherwise the variable is created.
3908 */
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003909 int
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003910set_var_const(
3911 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003912 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003913 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003914 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003915 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003916 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003917 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003918{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003919 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003920 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003921 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003923 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003924 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003925 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003926 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003927 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003928 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003929 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003930 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003931 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003932 int free_tv_arg = !copy; // free tv_arg if not used
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003933 int rc = FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003934
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003935 if (sid != 0)
3936 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003937 varname = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003938 if (SCRIPT_ID_VALID(sid))
Ernie Rael84f6dc72024-04-21 14:45:48 +02003939 {
3940 char_u *auto_name = NULL;
3941 if (find_var_autoload_prefix(name, sid, &ht, &auto_name) != NULL)
3942 {
3943 var_in_autoload = TRUE;
3944 varname = auto_name;
3945 name_tofree = varname;
3946 }
3947 else
3948 ht = &SCRIPT_VARS(sid);
3949 }
3950 if (varname == NULL)
3951 varname = name;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003952 }
3953 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003954 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003955 scriptitem_T *si;
3956 char_u *auto_name = NULL;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003957
Ernie Rael84f6dc72024-04-21 14:45:48 +02003958 if (in_vim9script()
3959 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3960 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3961 ->sn_autoload_prefix != NULL
3962 && (is_export
3963 || find_var_autoload_prefix(name, 0, NULL, &auto_name)
3964 != NULL))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003965 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003966 // In a vim9 autoload script an exported variable is put in the
3967 // global namespace with the autoload prefix.
3968 var_in_autoload = TRUE;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003969 varname = auto_name != NULL ? auto_name
3970 : concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003971 if (varname == NULL)
3972 goto failed;
3973 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003974 ht = &globvarht;
3975 }
3976 else
3977 ht = find_var_ht(name, &varname);
3978 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003979 if (ht == NULL || *varname == NUL)
3980 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003981 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003982 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003983 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003984 is_script_local = ht == get_script_local_ht() || sid != 0
3985 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003986
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003987 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003988 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003989 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003990 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003991 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003992 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003993 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003994 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003995 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003996 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003997 // Do not make g:var, w:var, b:var or t:var final.
3998 flags &= ~ASSIGN_FINAL;
3999
Bram Moolenaare535db82021-03-31 21:07:24 +02004000 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004001 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
4002 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02004003 // For "[a, _] = list" the underscore is ignored.
4004 if ((flags & ASSIGN_UNPACK) == 0)
4005 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004006 goto failed;
4007 }
Bram Moolenaar67979662020-06-20 22:50:47 +02004008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004010
Bram Moolenaar24e93162021-07-18 20:40:33 +02004011 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004012 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004013 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004014
Bram Moolenaar24e93162021-07-18 20:40:33 +02004015 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004016 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004017 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02004018 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004020 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004021 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004023 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
4024 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004025 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00004026 if (!in_vim9script())
4027 {
4028 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
4029 name);
4030 goto failed;
4031 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004032 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033
Bram Moolenaar993faa32022-02-21 15:59:11 +00004034 // Search in parent scope which is possible to reference from lambda
4035 if (di == NULL)
4036 di = find_var_in_scoped_ht(name, TRUE);
4037
4038 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
4039 && var_wrong_func_name(name, di == NULL))
4040 goto failed;
4041
4042 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004043 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004044 // Destination is a bool and the value is not, but it can be
4045 // converted.
4046 CLEAR_FIELD(bool_tv);
4047 bool_tv.v_type = VAR_BOOL;
4048 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
4049 tv = &bool_tv;
4050 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004051
Bram Moolenaar993faa32022-02-21 15:59:11 +00004052 if (di != NULL)
4053 {
4054 // Item already exists. Allowed to replace when reloading.
4055 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004056 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004057 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
4058 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004059 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004060 emsg(_(e_cannot_modify_existing_variable));
4061 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004062 }
4063
Bram Moolenaar993faa32022-02-21 15:59:11 +00004064 if (is_script_local && vim9script
4065 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02004066 {
4067 semsg(_(e_redefining_script_item_str), name);
4068 goto failed;
4069 }
4070
Ernie Raele75fde62023-12-21 17:18:54 +01004071 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004072 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004073
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004074 // List and Blob types can be modified in-place using the "+="
4075 // compound operator. For other types, this is not allowed.
4076 int type_inplace_modifiable =
4077 (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
4078
4079 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
4080 && ((flags & ASSIGN_COMPOUND_OP) == 0
4081 || !type_inplace_modifiable))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004082 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004083 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01004084 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004085
4086 if (sv != NULL)
4087 {
4088 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02004089 if (var_idx > 0)
4090 {
4091 where.wt_index = var_idx;
4092 where.wt_kind = WT_VARIABLE;
4093 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004094 if (check_script_var_type(sv, tv, name, where) == FAIL)
4095 goto failed;
4096 if (type == NULL)
4097 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004098 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004099 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004100 }
4101
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004102 // Modifying a final variable with a List value using the "+="
4103 // operator is allowed. For other types, it is not allowed.
zeertzjq6b97d7a2024-08-08 21:05:57 +02004104 if ((((flags & ASSIGN_FOR_LOOP) == 0 || (flags & ASSIGN_DECL) == 0)
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004105 && ((flags & ASSIGN_COMPOUND_OP) == 0
4106 || !type_inplace_modifiable))
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004107 ? var_check_permission(di, name) == FAIL
4108 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004109 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004110 }
4111 else
4112 {
4113 // can only redefine once
4114 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004115
Bram Moolenaar993faa32022-02-21 15:59:11 +00004116 // A Vim9 script-local variable is also present in sn_all_vars
4117 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004118 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004119 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004120 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004121 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004122 }
4123
Bram Moolenaar993faa32022-02-21 15:59:11 +00004124 // existing variable, need to clear the value
4125
zeertzjqedcba962023-09-24 23:13:51 +02004126 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004127 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004128 int type_error = FALSE;
4129 if (ht == &vimvarht
4130 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004131 {
zeertzjqedcba962023-09-24 23:13:51 +02004132 if (type_error)
4133 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4134 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004135 }
4136
4137 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004138
4139 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4140 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4141 {
4142 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4143
4144 update_script_var_block_id(name, si->sn_current_block_id);
4145 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004146 }
4147 else
4148 {
4149 // Item not found, check if a function already exists.
4150 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4151 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4152 {
4153 semsg(_(e_redefining_script_item_str), name);
4154 goto failed;
4155 }
4156
4157 // add a new variable
4158 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4159 {
4160 semsg(_(e_unknown_variable_str), name);
4161 goto failed;
4162 }
4163
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004164 if (check_hashtab_frozen(ht, "add variable"))
4165 goto failed;
4166
Bram Moolenaar993faa32022-02-21 15:59:11 +00004167 // Can't add "v:" or "a:" variable.
4168 if (ht == &vimvarht || ht == get_funccal_args_ht())
4169 {
4170 semsg(_(e_illegal_variable_name_str), name);
4171 goto failed;
4172 }
4173
4174 // Make sure the variable name is valid. In Vim9 script an
4175 // autoload variable must be prefixed with "g:" unless in an
4176 // autoload script.
4177 if (!valid_varname(varname, -1, !vim9script
4178 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4179 goto failed;
4180
zeertzjq1b438a82023-02-01 13:11:15 +00004181 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004182 if (di == NULL)
4183 goto failed;
4184 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004185 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004186 {
4187 vim_free(di);
4188 goto failed;
4189 }
4190 di->di_flags = DI_FLAGS_ALLOC;
4191 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4192 di->di_flags |= DI_FLAGS_LOCK;
4193
4194 // A Vim9 script-local variable is also added to sn_all_vars and
4195 // sn_var_vals. It may set "type" from "tv".
4196 if (var_in_vim9script || var_in_autoload)
4197 update_vim9_script_var(TRUE, di,
4198 var_in_autoload ? name : di->di_key, flags,
4199 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004200 }
4201
Bram Moolenaar993faa32022-02-21 15:59:11 +00004202 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004203 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004204 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004205 else
4206 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004207 *dest_tv = *tv;
4208 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004209 init_tv(tv);
4210 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004211 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004212
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004213 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004214 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004215
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004216 // ":const var = value" locks the value
4217 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004218 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004219 // Like :lockvar! name: lock the value and what it contains, but only
4220 // if the reference count is up to one. That locks only literal
4221 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004222 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004223
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004224 rc = OK;
4225
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004226failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004227 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004228 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004229 clear_tv(tv_arg);
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004230
4231 return rc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004232}
4233
4234/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004235 * Check in this order for backwards compatibility:
4236 * - Whether the variable is read-only
4237 * - Whether the variable value is locked
4238 * - Whether the variable is locked
Ernie Rael3f821d62024-04-24 20:07:50 +02004239 * NOTE: "name" is only used for error messages.
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004240 */
4241 int
4242var_check_permission(dictitem_T *di, char_u *name)
4243{
4244 if (var_check_ro(di->di_flags, name, FALSE)
4245 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4246 || var_check_lock(di->di_flags, name, FALSE))
4247 return FAIL;
4248 return OK;
4249}
4250
4251/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004252 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4253 * Also give an error message.
4254 */
4255 int
4256var_check_ro(int flags, char_u *name, int use_gettext)
4257{
4258 if (flags & DI_FLAGS_RO)
4259 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004260 if (name == NULL)
4261 emsg(_(e_cannot_change_readonly_variable));
4262 else
4263 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004264 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004265 return TRUE;
4266 }
4267 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4268 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004269 if (name == NULL)
4270 emsg(_(e_cannot_set_variable_in_sandbox));
4271 else
4272 semsg(_(e_cannot_set_variable_in_sandbox_str),
4273 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004274 return TRUE;
4275 }
4276 return FALSE;
4277}
4278
4279/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004280 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4281 * Also give an error message.
4282 */
4283 int
4284var_check_lock(int flags, char_u *name, int use_gettext)
4285{
4286 if (flags & DI_FLAGS_LOCK)
4287 {
4288 semsg(_(e_variable_is_locked_str),
4289 use_gettext ? (char_u *)_(name) : name);
4290 return TRUE;
4291 }
4292 return FALSE;
4293}
4294
4295/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004296 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4297 * Also give an error message.
4298 */
4299 int
4300var_check_fixed(int flags, char_u *name, int use_gettext)
4301{
4302 if (flags & DI_FLAGS_FIX)
4303 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004304 if (name == NULL)
4305 emsg(_(e_cannot_delete_variable));
4306 else
4307 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004308 use_gettext ? (char_u *)_(name) : name);
4309 return TRUE;
4310 }
4311 return FALSE;
4312}
4313
4314/*
4315 * Check if a funcref is assigned to a valid variable name.
4316 * Return TRUE and give an error if not.
4317 */
4318 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004319var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004320 char_u *name, // points to start of variable name
4321 int new_var) // TRUE when creating the variable
4322{
Bram Moolenaar32154662021-03-28 21:14:06 +02004323 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4324 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004325 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004326 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4327 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004328 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004329 ? name[2] : name[0])
4330 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004331 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004332 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004333 return TRUE;
4334 }
4335 // Don't allow hiding a function. When "v" is not NULL we might be
4336 // assigning another function to the same var, the type is checked
4337 // below.
4338 if (new_var && function_exists(name, FALSE))
4339 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004340 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004341 name);
4342 return TRUE;
4343 }
4344 return FALSE;
4345}
4346
4347/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004348 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4349 * value. Also give an error message, using "name" or _("name") when
4350 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004351 */
4352 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004353value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004354{
4355 if (lock & VAR_LOCKED)
4356 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004357 if (name == NULL)
4358 emsg(_(e_value_is_locked));
4359 else
4360 semsg(_(e_value_is_locked_str),
4361 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004362 return TRUE;
4363 }
4364 if (lock & VAR_FIXED)
4365 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004366 if (name == NULL)
4367 emsg(_(e_cannot_change_value));
4368 else
4369 semsg(_(e_cannot_change_value_of_str),
4370 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004371 return TRUE;
4372 }
4373 return FALSE;
4374}
4375
4376/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004377 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004378 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004379 * Return FALSE and give an error if not.
4380 */
4381 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004382valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004383{
4384 char_u *p;
4385
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004386 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004387 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004388 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004389 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004390 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004391 return FALSE;
4392 }
4393 return TRUE;
4394}
4395
4396/*
LemonBoy47d4e312022-05-04 18:12:55 +01004397 * Implements the logic to retrieve local variable and option values.
4398 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004399 */
4400 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004401get_var_from(
4402 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004403 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004404 typval_T *deftv, // Default value if not found.
4405 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4406 tabpage_T *tp, // can be NULL
4407 win_T *win,
4408 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004409{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004410 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004411 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004412 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004413 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004414 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004415
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004416 ++emsg_off;
4417
4418 rettv->v_type = VAR_STRING;
4419 rettv->vval.v_string = NULL;
4420
LemonBoy47d4e312022-05-04 18:12:55 +01004421 if (varname != NULL && tp != NULL && win != NULL
4422 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004423 {
4424 // Set curwin to be our win, temporarily. Also set the tabpage,
4425 // otherwise the window is not valid. Only do this when needed,
4426 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004427 // If we have a buffer reference avoid the switching, we're saving and
4428 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004429 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004430 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004431 {
LemonBoy47d4e312022-05-04 18:12:55 +01004432 // Handle options. There are no tab-local options.
4433 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004434 {
LemonBoy47d4e312022-05-04 18:12:55 +01004435 buf_T *save_curbuf = curbuf;
4436
4437 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004438 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004439 curbuf = buf;
4440
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004441 if (varname[1] == NUL)
4442 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004443 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004444 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004445
4446 if (opts != NULL)
4447 {
4448 rettv_dict_set(rettv, opts);
4449 done = TRUE;
4450 }
4451 }
LemonBoy47d4e312022-05-04 18:12:55 +01004452 else if (eval_option(&varname, rettv, TRUE) == OK)
4453 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004454 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004455
4456 curbuf = save_curbuf;
4457 }
4458 else if (*varname == NUL)
4459 {
4460 // Empty string: return a dict with all the local variables.
4461 if (htname == 'b')
4462 v = &buf->b_bufvar;
4463 else if (htname == 'w')
4464 v = &win->w_winvar;
4465 else
4466 v = &tp->tp_winvar;
4467 copy_tv(&v->di_tv, rettv);
4468 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004469 }
4470 else
4471 {
LemonBoy47d4e312022-05-04 18:12:55 +01004472 hashtab_T *ht;
4473
4474 if (htname == 'b')
4475 ht = &buf->b_vars->dv_hashtab;
4476 else if (htname == 'w')
4477 ht = &win->w_vars->dv_hashtab;
4478 else
4479 ht = &tp->tp_vars->dv_hashtab;
4480
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004481 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004482 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004483 if (v != NULL)
4484 {
4485 copy_tv(&v->di_tv, rettv);
4486 done = TRUE;
4487 }
4488 }
4489 }
4490
4491 if (need_switch_win)
4492 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004493 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004494 }
4495
LemonBoy47d4e312022-05-04 18:12:55 +01004496 if (!done && deftv->v_type != VAR_UNKNOWN)
4497 // use the default value
4498 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004499
4500 --emsg_off;
4501}
4502
4503/*
LemonBoy47d4e312022-05-04 18:12:55 +01004504 * getwinvar() and gettabwinvar()
4505 */
4506 static void
4507getwinvar(
4508 typval_T *argvars,
4509 typval_T *rettv,
4510 int off) // 1 for gettabwinvar()
4511{
4512 char_u *varname;
4513 tabpage_T *tp;
4514 win_T *win;
4515
4516 if (off == 1)
4517 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4518 else
4519 tp = curtab;
4520 win = find_win_by_nr(&argvars[off], tp);
4521 varname = tv_get_string_chk(&argvars[off + 1]);
4522
4523 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4524}
4525
4526/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004527 * Set option "varname" to the value of "varp" for the current buffer/window.
4528 */
4529 static void
4530set_option_from_tv(char_u *varname, typval_T *varp)
4531{
4532 long numval = 0;
4533 char_u *strval;
4534 char_u nbuf[NUMBUFLEN];
4535 int error = FALSE;
4536
zeertzjq4c7cb372023-06-14 16:39:54 +01004537 int opt_idx = findoption(varname);
4538 if (opt_idx < 0)
4539 {
4540 semsg(_(e_unknown_option_str_2), varname);
4541 return;
4542 }
4543 int opt_p_flags = get_option_flags(opt_idx);
4544
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004545 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004546 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004547 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004548 {
4549 emsg(_(e_string_required));
4550 return;
4551 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004552 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004553 strval = (char_u *)"0"; // avoid using "false"
4554 }
4555 else
4556 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004557 if ((opt_p_flags & (P_NUM|P_BOOL))
4558 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004559 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004560 if (!error)
4561 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004562 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004563 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004564 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004565}
4566
4567/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004568 * "setwinvar()" and "settabwinvar()" functions
4569 */
4570 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004571setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004572{
4573 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004574 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004575 int need_switch_win;
4576 char_u *varname, *winvarname;
4577 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004578 tabpage_T *tp = NULL;
4579
4580 if (check_secure())
4581 return;
4582
4583 if (off == 1)
4584 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4585 else
4586 tp = curtab;
4587 win = find_win_by_nr(&argvars[off], tp);
4588 varname = tv_get_string_chk(&argvars[off + 1]);
4589 varp = &argvars[off + 2];
4590
zeertzjqea720ae2023-01-03 10:54:09 +00004591 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004592 return;
4593
4594 need_switch_win = !(tp == curtab && win == curwin);
4595 if (!need_switch_win
4596 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004597 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004598 if (*varname == '&')
4599 set_option_from_tv(varname + 1, varp);
4600 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004601 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004602 winvarname = alloc(STRLEN(varname) + 3);
4603 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004604 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004605 STRCPY(winvarname, "w:");
4606 STRCPY(winvarname + 2, varname);
4607 set_var(winvarname, varp, TRUE);
4608 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004609 }
4610 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004611 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004612 if (need_switch_win)
4613 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004614}
4615
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004616/*
4617 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4618 * v:option_type, and v:option_command.
4619 */
4620 void
4621reset_v_option_vars(void)
4622{
4623 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4624 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4625 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4626 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4627 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4628 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4629}
4630
4631/*
4632 * Add an assert error to v:errors.
4633 */
4634 void
4635assert_error(garray_T *gap)
4636{
4637 struct vimvar *vp = &vimvars[VV_ERRORS];
4638
Bram Moolenaard787e402021-12-24 21:36:12 +00004639 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004640 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004641 set_vim_var_list(VV_ERRORS, list_alloc());
4642 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4643}
4644
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004645 int
4646var_exists(char_u *var)
4647{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004648 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004649 char_u *name;
4650 char_u *tofree;
4651 typval_T tv;
4652 int len = 0;
4653 int n = FALSE;
4654
4655 // get_name_len() takes care of expanding curly braces
4656 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004657 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004658 if (len > 0)
4659 {
4660 if (tofree != NULL)
4661 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004662 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004663 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004664 if (n)
4665 {
4666 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004667 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004668 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4669 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004670 if (n)
4671 clear_tv(&tv);
4672 }
4673 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004674 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004675 n = FALSE;
4676
4677 vim_free(tofree);
4678 return n;
4679}
4680
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004681static lval_T *redir_lval = NULL;
4682#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4683static garray_T redir_ga; // only valid when redir_lval is not NULL
4684static char_u *redir_endp = NULL;
4685static char_u *redir_varname = NULL;
4686
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004687 int
4688alloc_redir_lval(void)
4689{
4690 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4691 if (redir_lval == NULL)
4692 return FAIL;
4693 return OK;
4694}
4695
4696 void
4697clear_redir_lval(void)
4698{
4699 VIM_CLEAR(redir_lval);
4700}
4701
4702 void
4703init_redir_ga(void)
4704{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004705 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004706}
4707
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004708/*
4709 * Start recording command output to a variable
4710 * When "append" is TRUE append to an existing variable.
4711 * Returns OK if successfully completed the setup. FAIL otherwise.
4712 */
4713 int
4714var_redir_start(char_u *name, int append)
4715{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004716 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004717 typval_T tv;
4718
4719 // Catch a bad name early.
4720 if (!eval_isnamec1(*name))
4721 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004722 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004723 return FAIL;
4724 }
4725
4726 // Make a copy of the name, it is used in redir_lval until redir ends.
4727 redir_varname = vim_strsave(name);
4728 if (redir_varname == NULL)
4729 return FAIL;
4730
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004731 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004732 {
4733 var_redir_stop();
4734 return FAIL;
4735 }
4736
4737 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004738 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004739
4740 // Parse the variable name (can be a dict or list entry).
4741 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4742 FNE_CHECK_START);
4743 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4744 {
4745 clear_lval(redir_lval);
4746 if (redir_endp != NULL && *redir_endp != NUL)
4747 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004748 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004749 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004750 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004751 redir_endp = NULL; // don't store a value, only cleanup
4752 var_redir_stop();
4753 return FAIL;
4754 }
4755
4756 // check if we can write to the variable: set it to or append an empty
4757 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004758 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004759 tv.v_type = VAR_STRING;
4760 tv.vval.v_string = (char_u *)"";
4761 if (append)
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004762 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004763 ASSIGN_NO_DECL, (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004764 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004765 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004766 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004767 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004768 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004769 {
4770 redir_endp = NULL; // don't store a value, only cleanup
4771 var_redir_stop();
4772 return FAIL;
4773 }
4774
4775 return OK;
4776}
4777
4778/*
4779 * Append "value[value_len]" to the variable set by var_redir_start().
4780 * The actual appending is postponed until redirection ends, because the value
4781 * appended may in fact be the string we write to, changing it may cause freed
4782 * memory to be used:
4783 * :redir => foo
4784 * :let foo
4785 * :redir END
4786 */
4787 void
4788var_redir_str(char_u *value, int value_len)
4789{
4790 int len;
4791
4792 if (redir_lval == NULL)
4793 return;
4794
4795 if (value_len == -1)
4796 len = (int)STRLEN(value); // Append the entire string
4797 else
4798 len = value_len; // Append only "value_len" characters
4799
4800 if (ga_grow(&redir_ga, len) == OK)
4801 {
4802 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4803 redir_ga.ga_len += len;
4804 }
4805 else
4806 var_redir_stop();
4807}
4808
4809/*
4810 * Stop redirecting command output to a variable.
4811 * Frees the allocated memory.
4812 */
4813 void
4814var_redir_stop(void)
4815{
4816 typval_T tv;
4817
4818 if (EVALCMD_BUSY)
4819 {
4820 redir_lval = NULL;
4821 return;
4822 }
4823
4824 if (redir_lval != NULL)
4825 {
4826 // If there was no error: assign the text to the variable.
4827 if (redir_endp != NULL)
4828 {
4829 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4830 tv.v_type = VAR_STRING;
4831 tv.vval.v_string = redir_ga.ga_data;
4832 // Call get_lval() again, if it's inside a Dict or List it may
4833 // have changed.
4834 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4835 FALSE, FALSE, 0, FNE_CHECK_START);
4836 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004837 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004838 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004839 clear_lval(redir_lval);
4840 }
4841
4842 // free the collected output
4843 VIM_CLEAR(redir_ga.ga_data);
4844
4845 VIM_CLEAR(redir_lval);
4846 }
4847 VIM_CLEAR(redir_varname);
4848}
4849
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004850/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004851 * Get the collected redirected text and clear redir_ga.
4852 */
4853 char_u *
4854get_clear_redir_ga(void)
4855{
4856 char_u *res;
4857
4858 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4859 res = redir_ga.ga_data;
4860 redir_ga.ga_data = NULL;
4861 return res;
4862}
4863
4864/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004865 * "gettabvar()" function
4866 */
4867 void
4868f_gettabvar(typval_T *argvars, typval_T *rettv)
4869{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004870 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004871 tabpage_T *tp;
4872 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004873
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004874 if (in_vim9script()
4875 && (check_for_number_arg(argvars, 0) == FAIL
4876 || check_for_string_arg(argvars, 1) == FAIL))
4877 return;
4878
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004879 varname = tv_get_string_chk(&argvars[1]);
4880 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004881 if (tp != NULL)
4882 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4883 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004884
LemonBoy47d4e312022-05-04 18:12:55 +01004885 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004886}
4887
4888/*
4889 * "gettabwinvar()" function
4890 */
4891 void
4892f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4893{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004894 if (in_vim9script()
4895 && (check_for_number_arg(argvars, 0) == FAIL
4896 || check_for_number_arg(argvars, 1) == FAIL
4897 || check_for_string_arg(argvars, 2) == FAIL))
4898 return;
4899
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004900 getwinvar(argvars, rettv, 1);
4901}
4902
4903/*
4904 * "getwinvar()" function
4905 */
4906 void
4907f_getwinvar(typval_T *argvars, typval_T *rettv)
4908{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004909 if (in_vim9script()
4910 && (check_for_number_arg(argvars, 0) == FAIL
4911 || check_for_string_arg(argvars, 1) == FAIL))
4912 return;
4913
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004914 getwinvar(argvars, rettv, 0);
4915}
4916
4917/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004918 * "getbufvar()" function
4919 */
4920 void
4921f_getbufvar(typval_T *argvars, typval_T *rettv)
4922{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004923 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004924 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004925
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004926 if (in_vim9script()
4927 && (check_for_buffer_arg(argvars, 0) == FAIL
4928 || check_for_string_arg(argvars, 1) == FAIL))
4929 return;
4930
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004931 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004932 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004933
LemonBoy47d4e312022-05-04 18:12:55 +01004934 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004935}
4936
4937/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004938 * "settabvar()" function
4939 */
4940 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004941f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004942{
4943 tabpage_T *save_curtab;
4944 tabpage_T *tp;
zeertzjqb47fbb42024-02-12 22:50:26 +01004945 tabpage_T *save_lu_tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004946 char_u *varname, *tabvarname;
4947 typval_T *varp;
4948
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004949 if (check_secure())
4950 return;
4951
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004952 if (in_vim9script()
4953 && (check_for_number_arg(argvars, 0) == FAIL
4954 || check_for_string_arg(argvars, 1) == FAIL))
4955 return;
4956
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004957 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4958 varname = tv_get_string_chk(&argvars[1]);
4959 varp = &argvars[2];
4960
zeertzjqea720ae2023-01-03 10:54:09 +00004961 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004962 return;
4963
4964 save_curtab = curtab;
zeertzjqb47fbb42024-02-12 22:50:26 +01004965 save_lu_tp = lastused_tabpage;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004966 goto_tabpage_tp(tp, FALSE, FALSE);
4967
4968 tabvarname = alloc(STRLEN(varname) + 3);
4969 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004970 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004971 STRCPY(tabvarname, "t:");
4972 STRCPY(tabvarname + 2, varname);
4973 set_var(tabvarname, varp, TRUE);
4974 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004975 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004976
zeertzjqb47fbb42024-02-12 22:50:26 +01004977 // Restore current tabpage and last accessed tabpage.
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004978 if (valid_tabpage(save_curtab))
zeertzjqb47fbb42024-02-12 22:50:26 +01004979 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004980 goto_tabpage_tp(save_curtab, FALSE, FALSE);
zeertzjqb47fbb42024-02-12 22:50:26 +01004981 if (valid_tabpage(save_lu_tp))
4982 lastused_tabpage = save_lu_tp;
4983 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004984}
4985
4986/*
4987 * "settabwinvar()" function
4988 */
4989 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004990f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004991{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004992 if (in_vim9script()
4993 && (check_for_number_arg(argvars, 0) == FAIL
4994 || check_for_number_arg(argvars, 1) == FAIL
4995 || check_for_string_arg(argvars, 2) == FAIL))
4996 return;
4997
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004998 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004999}
5000
5001/*
5002 * "setwinvar()" function
5003 */
5004 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005005f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005006{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005007 if (in_vim9script()
5008 && (check_for_number_arg(argvars, 0) == FAIL
5009 || check_for_string_arg(argvars, 1) == FAIL))
5010 return;
5011
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005012 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005013}
5014
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005015/*
5016 * "setbufvar()" function
5017 */
5018 void
5019f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
5020{
5021 buf_T *buf;
5022 char_u *varname, *bufvarname;
5023 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005024
5025 if (check_secure())
5026 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02005027
5028 if (in_vim9script()
5029 && (check_for_buffer_arg(argvars, 0) == FAIL
5030 || check_for_string_arg(argvars, 1) == FAIL))
5031 return;
5032
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005033 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02005034 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005035 varp = &argvars[2];
5036
zeertzjqea720ae2023-01-03 10:54:09 +00005037 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005038 return;
5039
5040 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005041 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005042 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005043 // safe the current window position, it could
5044 // change because of 'scrollbind' window-local
5045 // options
5046 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005047
5048 // Set curbuf to be our buf, temporarily.
5049 aucmd_prepbuf(&aco, buf);
5050 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005051 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005052 // Only when it worked to set "curbuf".
5053 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005054
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005055 // reset notion of buffer
5056 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005057 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005058 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005059 }
5060 else
5061 {
5062 bufvarname = alloc(STRLEN(varname) + 3);
5063 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005064 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005065 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02005066
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005067 curbuf = buf;
5068 STRCPY(bufvarname, "b:");
5069 STRCPY(bufvarname + 2, varname);
5070 set_var(bufvarname, varp, TRUE);
5071 vim_free(bufvarname);
5072 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005073 }
5074 }
5075}
5076
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005077/*
5078 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005079 * When "arg" is zero "res.cb_name" is set to an empty string.
5080 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
5081 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005082 */
5083 callback_T
5084get_callback(typval_T *arg)
5085{
Bram Moolenaar14e579092020-03-07 16:59:25 +01005086 callback_T res;
5087 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005088
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005089 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005090 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
5091 {
5092 res.cb_partial = arg->vval.v_partial;
5093 ++res.cb_partial->pt_refcount;
5094 res.cb_name = partial_name(res.cb_partial);
5095 }
5096 else
5097 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01005098 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01005099 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01005100 r = FAIL;
5101 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005102 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005103 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005104 if (arg->v_type == VAR_STRING)
5105 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005106 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005107 if (name != NULL)
5108 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005109 res.cb_name = name;
5110 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005111 }
5112 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005113 func_ref(res.cb_name);
5114 }
5115 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005116 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005117 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005118 r = FAIL;
5119
5120 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005121 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005122 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005123 res.cb_name = NULL;
5124 }
5125 }
5126 return res;
5127}
5128
5129/*
5130 * Copy a callback into a typval_T.
5131 */
5132 void
5133put_callback(callback_T *cb, typval_T *tv)
5134{
5135 if (cb->cb_partial != NULL)
5136 {
5137 tv->v_type = VAR_PARTIAL;
5138 tv->vval.v_partial = cb->cb_partial;
5139 ++tv->vval.v_partial->pt_refcount;
5140 }
5141 else
5142 {
5143 tv->v_type = VAR_FUNC;
5144 tv->vval.v_string = vim_strsave(cb->cb_name);
5145 func_ref(cb->cb_name);
5146 }
5147}
5148
5149/*
5150 * Make a copy of "src" into "dest", allocating the function name if needed,
5151 * without incrementing the refcount.
5152 */
5153 void
5154set_callback(callback_T *dest, callback_T *src)
5155{
5156 if (src->cb_partial == NULL)
5157 {
5158 // just a function name, make a copy
5159 dest->cb_name = vim_strsave(src->cb_name);
5160 dest->cb_free_name = TRUE;
5161 }
5162 else
5163 {
5164 // cb_name is a pointer into cb_partial
5165 dest->cb_name = src->cb_name;
5166 dest->cb_free_name = FALSE;
5167 }
5168 dest->cb_partial = src->cb_partial;
5169}
5170
5171/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005172 * Copy callback from "src" to "dest", incrementing the refcounts.
5173 */
5174 void
5175copy_callback(callback_T *dest, callback_T *src)
5176{
5177 dest->cb_partial = src->cb_partial;
5178 if (dest->cb_partial != NULL)
5179 {
5180 dest->cb_name = src->cb_name;
5181 dest->cb_free_name = FALSE;
5182 ++dest->cb_partial->pt_refcount;
5183 }
5184 else
5185 {
5186 dest->cb_name = vim_strsave(src->cb_name);
5187 dest->cb_free_name = TRUE;
5188 func_ref(src->cb_name);
5189 }
5190}
5191
5192/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005193 * When a callback refers to an autoload import, change the function name to
5194 * the "path#name" form. Uses the current script context.
5195 * Only works when the name is allocated.
5196 */
5197 void
5198expand_autload_callback(callback_T *cb)
5199{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005200 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005201 char_u *p;
5202 imported_T *import;
5203
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005204 if (!in_vim9script() || cb->cb_name == NULL
5205 || (!cb->cb_free_name
5206 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005207 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005208 if (cb->cb_partial != NULL)
5209 name = cb->cb_partial->pt_name;
5210 else
5211 name = cb->cb_name;
5212 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005213 if (p == NULL)
5214 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005215
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005216 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005217 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5218 return;
5219
5220 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5221 if (si->sn_autoload_prefix == NULL)
5222 return;
5223
5224 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5225 if (newname == NULL)
5226 return;
5227
5228 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005229 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005230 if (cb->cb_name == cb->cb_partial->pt_name)
5231 cb->cb_name = newname;
5232 vim_free(cb->cb_partial->pt_name);
5233 cb->cb_partial->pt_name = newname;
5234 }
5235 else
5236 {
5237 vim_free(cb->cb_name);
5238 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005239 }
5240}
5241
5242/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005243 * Unref/free "callback" returned by get_callback() or set_callback().
5244 */
5245 void
5246free_callback(callback_T *callback)
5247{
5248 if (callback->cb_partial != NULL)
5249 {
5250 partial_unref(callback->cb_partial);
5251 callback->cb_partial = NULL;
5252 }
5253 else if (callback->cb_name != NULL)
5254 func_unref(callback->cb_name);
5255 if (callback->cb_free_name)
5256 {
5257 vim_free(callback->cb_name);
5258 callback->cb_free_name = FALSE;
5259 }
5260 callback->cb_name = NULL;
5261}
5262
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005263#endif // FEAT_EVAL