blob: 2745ac271a6a733ae8ce7356e53d68066eb191de [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
Yegappan Lakshmanane9ae35f2025-02-27 19:12:00 +01003090 if (v == NULL)
3091 v = find_var_autoload_prefix(name, sid, NULL, NULL);
3092
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003093 if (v != NULL)
3094 {
3095 tv = &v->di_tv;
3096 if (dip != NULL)
3097 *dip = v;
3098 }
3099 else
3100 ht = NULL;
3101 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003102 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003103 else
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003104 {
3105 if (flags & EVAL_VAR_VERBOSE)
Bram Moolenaardd5893b2022-01-20 21:32:54 +00003106 semsg(_(e_expected_dot_after_name_str), name);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003107 ret = FAIL;
3108 }
Bram Moolenaarf6a44f72020-09-27 13:51:14 +02003109 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003110 else
3111 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003112 if (rettv != NULL)
3113 {
Bram Moolenaar753885b2022-08-24 16:30:36 +01003114 // special value that is used in handle_subscript()
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003115 rettv->v_type = VAR_ANY;
3116 rettv->vval.v_number = sid != 0 ? sid : import->imp_sid;
3117 }
3118 found = TRUE;
Bram Moolenaarc620c052020-07-08 15:16:19 +02003119 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003120 }
Bram Moolenaar052ff292021-12-11 13:54:46 +00003121 else if (in_vim9script() && (flags & EVAL_VAR_NO_FUNC) == 0)
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003122 {
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003123 int has_g_prefix = STRNCMP(name, "g:", 2) == 0;
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003124 ufunc_T *ufunc = find_func(name, FALSE);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003125
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003126 // In Vim9 script we can get a function reference by using the
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003127 // function name. For a global non-autoload function "g:" is
3128 // required.
3129 if (ufunc != NULL && (has_g_prefix
3130 || !func_requires_g_prefix(ufunc)))
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003131 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003132 found = TRUE;
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003133 if (rettv != NULL)
3134 {
3135 rettv->v_type = VAR_FUNC;
Bram Moolenaar848fadd2022-01-30 15:28:30 +00003136 if (has_g_prefix)
Bram Moolenaaref082e12021-12-12 21:02:03 +00003137 // Keep the "g:", otherwise script-local may be
3138 // assumed.
3139 rettv->vval.v_string = vim_strsave(name);
3140 else
John Marriottb32800f2025-02-01 15:25:34 +01003141 rettv->vval.v_string = vim_strnsave(ufunc->uf_name, ufunc->uf_namelen);
Bram Moolenaarb033ee22021-08-15 16:08:36 +02003142 if (rettv->vval.v_string != NULL)
3143 func_ref(ufunc->uf_name);
Bram Moolenaar601e76a2020-08-27 21:33:10 +02003144 }
3145 }
3146 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003147 }
3148
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003149 if (!found)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003150 {
Bram Moolenaarc620c052020-07-08 15:16:19 +02003151 if (tv == NULL)
3152 {
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01003153 if (rettv != NULL && (flags & EVAL_VAR_VERBOSE))
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003154 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarc620c052020-07-08 15:16:19 +02003155 ret = FAIL;
3156 }
3157 else if (rettv != NULL)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003158 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003159 svar_T *sv = NULL;
3160 int was_assigned = FALSE;
3161
Bram Moolenaar11005b02021-07-11 20:59:00 +02003162 if (ht != NULL && ht == get_script_local_ht()
3163 && tv != &SCRIPT_SV(current_sctx.sc_sid)->sv_var.di_tv)
Bram Moolenaarf055d452021-07-08 20:57:24 +02003164 {
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003165 sv = find_typval_in_script(tv, 0, TRUE);
Bram Moolenaarf055d452021-07-08 20:57:24 +02003166 if (sv != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003167 {
Bram Moolenaarf055d452021-07-08 20:57:24 +02003168 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003169 was_assigned = sv->sv_flags & SVFLAG_ASSIGNED;
3170 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003171 }
3172
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003173 if ((tv->v_type == VAR_TYPEALIAS || tv->v_type == VAR_CLASS)
3174 && sid != 0)
3175 {
3176 // type alias or class imported from another script. Check
3177 // whether it is exported from the other script.
3178 sv = find_typval_in_script(tv, sid, TRUE);
3179 if (sv == NULL)
3180 {
3181 ret = FAIL;
3182 goto done;
3183 }
3184 if ((sv->sv_flags & SVFLAG_EXPORTED) == 0)
3185 {
3186 semsg(_(e_item_not_exported_in_script_str), name);
3187 ret = FAIL;
3188 goto done;
3189 }
3190 }
3191
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003192 // If a list or dict variable wasn't initialized and has meaningful
3193 // type, do it now. Not for global variables, they are not
3194 // declared.
Bram Moolenaar7a222242022-03-01 19:23:24 +00003195 if (ht != &globvarht)
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003196 {
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003197 if (tv->v_type == VAR_DICT && tv->vval.v_dict == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003198 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003199 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003200 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003201 tv->vval.v_dict = dict_alloc();
3202 if (tv->vval.v_dict != NULL)
3203 {
3204 ++tv->vval.v_dict->dv_refcount;
3205 tv->vval.v_dict->dv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003206 if (sv != NULL)
3207 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003208 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003209 }
Bram Moolenaarec15b1c2022-03-27 16:29:53 +01003210 else if (tv->v_type == VAR_LIST && tv->vval.v_list == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003211 && ((type != NULL && !was_assigned)
Bram Moolenaar501f9782022-03-27 16:51:04 +01003212 || !in_vim9script()))
Bram Moolenaarf055d452021-07-08 20:57:24 +02003213 {
Bram Moolenaar7a222242022-03-01 19:23:24 +00003214 tv->vval.v_list = list_alloc();
3215 if (tv->vval.v_list != NULL)
3216 {
3217 ++tv->vval.v_list->lv_refcount;
3218 tv->vval.v_list->lv_type = alloc_type(type);
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003219 if (sv != NULL)
3220 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar7a222242022-03-01 19:23:24 +00003221 }
Bram Moolenaarf055d452021-07-08 20:57:24 +02003222 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003223 else if (tv->v_type == VAR_BLOB && tv->vval.v_blob == NULL
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003224 && ((type != NULL && !was_assigned)
Bram Moolenaar859cc212022-03-28 15:22:35 +01003225 || !in_vim9script()))
3226 {
3227 tv->vval.v_blob = blob_alloc();
3228 if (tv->vval.v_blob != NULL)
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003229 {
Bram Moolenaar859cc212022-03-28 15:22:35 +01003230 ++tv->vval.v_blob->bv_refcount;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01003231 if (sv != NULL)
3232 sv->sv_flags |= SVFLAG_ASSIGNED;
3233 }
Bram Moolenaar859cc212022-03-28 15:22:35 +01003234 }
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02003235 }
Bram Moolenaarc620c052020-07-08 15:16:19 +02003236 copy_tv(tv, rettv);
Bram Moolenaar348be7e2020-11-04 11:36:35 +01003237 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003238 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003239
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02003240done:
Bram Moolenaar94674f22023-01-06 18:42:20 +00003241 if (len > 0)
3242 name[len] = cc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003243
3244 return ret;
3245}
3246
3247/*
Bram Moolenaara86655a2023-01-12 17:06:27 +00003248 * Get the value of internal variable "name", also handling "import.name".
3249 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
3250 */
3251 int
3252eval_variable_import(
3253 char_u *name,
3254 typval_T *rettv)
3255{
3256 char_u *s = name;
3257 while (ASCII_ISALNUM(*s) || *s == '_')
3258 ++s;
3259 int len = (int)(s - name);
3260
3261 if (eval_variable(name, len, 0, rettv, NULL, EVAL_VAR_IMPORT) == FAIL)
3262 return FAIL;
3263 if (rettv->v_type == VAR_ANY && *s == '.')
3264 {
Bram Moolenaar40594002023-01-12 20:04:51 +00003265 char_u *ns = s + 1;
3266 s = ns;
3267 while (ASCII_ISALNUM(*s) || *s == '_')
3268 ++s;
Bram Moolenaara86655a2023-01-12 17:06:27 +00003269 int sid = rettv->vval.v_number;
Bram Moolenaar40594002023-01-12 20:04:51 +00003270 return eval_variable(ns, (int)(s - ns), sid, rettv, NULL, 0);
Bram Moolenaara86655a2023-01-12 17:06:27 +00003271 }
3272 return OK;
3273}
3274
3275
3276/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003277 * Check if variable "name[len]" is a local variable or an argument.
3278 * If so, "*eval_lavars_used" is set to TRUE.
3279 */
3280 void
3281check_vars(char_u *name, int len)
3282{
3283 int cc;
3284 char_u *varname;
3285 hashtab_T *ht;
3286
3287 if (eval_lavars_used == NULL)
3288 return;
3289
3290 // truncate the name, so that we can use strcmp()
3291 cc = name[len];
3292 name[len] = NUL;
3293
3294 ht = find_var_ht(name, &varname);
3295 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
3296 {
3297 if (find_var(name, NULL, TRUE) != NULL)
3298 *eval_lavars_used = TRUE;
3299 }
3300
3301 name[len] = cc;
3302}
3303
3304/*
3305 * Find variable "name" in the list of variables.
3306 * Return a pointer to it if found, NULL if not found.
3307 * Careful: "a:0" variables don't have a name.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003308 * When "htp" is not NULL set "htp" to the hashtab_T used.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003309 */
3310 dictitem_T *
3311find_var(char_u *name, hashtab_T **htp, int no_autoload)
3312{
3313 char_u *varname;
3314 hashtab_T *ht;
3315 dictitem_T *ret = NULL;
3316
3317 ht = find_var_ht(name, &varname);
3318 if (htp != NULL)
3319 *htp = ht;
3320 if (ht == NULL)
3321 return NULL;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003322 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003323 if (ret != NULL)
3324 return ret;
3325
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003326 // Search in parent scope for lambda
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003327 ret = find_var_in_scoped_ht(name, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003328 if (ret != NULL)
3329 return ret;
3330
3331 // in Vim9 script items without a scope can be script-local
3332 if (in_vim9script() && name[0] != NUL && name[1] != ':')
3333 {
3334 ht = get_script_local_ht();
3335 if (ht != NULL)
3336 {
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003337 ret = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar2ea95b62020-11-19 21:47:56 +01003338 if (ret != NULL)
3339 {
3340 if (htp != NULL)
3341 *htp = ht;
3342 return ret;
3343 }
3344 }
3345 }
3346
Ernie Rael84f6dc72024-04-21 14:45:48 +02003347 // and finally try
3348 return find_var_autoload_prefix(name, 0, htp, NULL);
3349}
3350
3351/*
3352 * Find variable "name" with sn_autoload_prefix.
3353 * Return a pointer to it if found, NULL if not found.
3354 * When "sid" > 0, use it otherwise use "current_sctx.sc_sid".
3355 * When "htp" is not NULL set "htp" to the hashtab_T used.
3356 * When "namep" is not NULL set "namep" to the generated name, and
3357 * then the caller gets ownership and is responsible for freeing the name.
3358 */
3359 dictitem_T *
3360find_var_autoload_prefix(char_u *name, int sid, hashtab_T **htp,
3361 char_u **namep)
3362{
3363 hashtab_T *ht;
3364 dictitem_T *ret = NULL;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003365 // When using "vim9script autoload" script-local items are prefixed but can
3366 // be used with s:name.
Ernie Rael84f6dc72024-04-21 14:45:48 +02003367 int check_sid = sid > 0 ? sid : current_sctx.sc_sid;
3368 if (SCRIPT_ID_VALID(check_sid)
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003369 && (in_vim9script() || (name[0] == 's' && name[1] == ':')))
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003370 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003371 scriptitem_T *si = SCRIPT_ITEM(check_sid);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003372
3373 if (si->sn_autoload_prefix != NULL)
3374 {
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003375 char_u *base_name = (name[0] == 's' && name[1] == ':')
3376 ? name + 2 : name;
3377 char_u *auto_name = concat_str(si->sn_autoload_prefix, base_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003378
3379 if (auto_name != NULL)
3380 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003381 int free_auto_name = TRUE;
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003382 ht = &globvarht;
Bram Moolenaar6c4d4a62022-10-13 17:47:42 +01003383 ret = find_var_in_ht(ht, 'g', auto_name, TRUE);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003384 if (ret != NULL)
3385 {
3386 if (htp != NULL)
3387 *htp = ht;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003388 if (namep != NULL)
3389 {
3390 free_auto_name = FALSE;
3391 *namep = auto_name;
3392 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003393 }
Ernie Rael84f6dc72024-04-21 14:45:48 +02003394 if (free_auto_name)
3395 vim_free(auto_name);
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003396 }
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003397 }
3398 }
3399
Ernie Rael84f6dc72024-04-21 14:45:48 +02003400 return ret;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003401}
3402
3403/*
Bram Moolenaar71f21932022-01-07 18:20:55 +00003404 * Like find_var() but if the name starts with <SNR>99_ then look in the
3405 * referenced script (used for a funcref).
3406 */
3407 dictitem_T *
3408find_var_also_in_script(char_u *name, hashtab_T **htp, int no_autoload)
3409{
Keith Thompson184f71c2024-01-04 21:19:04 +01003410 if (STRNCMP(name, "<SNR>", 5) == 0 && SAFE_isdigit(name[5]))
Bram Moolenaar71f21932022-01-07 18:20:55 +00003411 {
3412 char_u *p = name + 5;
3413 int sid = getdigits(&p);
3414
3415 if (SCRIPT_ID_VALID(sid) && *p == '_')
3416 {
3417 hashtab_T *ht = &SCRIPT_VARS(sid);
3418
3419 if (ht != NULL)
3420 {
3421 dictitem_T *di = find_var_in_ht(ht, 0, p + 1, no_autoload);
3422
3423 if (di != NULL)
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003424 {
3425 if (htp != NULL)
3426 *htp = ht;
Bram Moolenaar71f21932022-01-07 18:20:55 +00003427 return di;
Bram Moolenaaraa9b3ca2022-01-08 15:44:22 +00003428 }
Bram Moolenaar71f21932022-01-07 18:20:55 +00003429 }
3430 }
3431 }
3432
3433 return find_var(name, htp, no_autoload);
3434}
3435
3436/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003437 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaar52592752020-04-03 18:43:35 +02003438 * When "varname" is empty returns curwin/curtab/etc vars dictionary.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003439 * Returns NULL if not found.
3440 */
3441 dictitem_T *
3442find_var_in_ht(
3443 hashtab_T *ht,
3444 int htname,
3445 char_u *varname,
3446 int no_autoload)
3447{
3448 hashitem_T *hi;
3449
3450 if (*varname == NUL)
3451 {
3452 // Must be something like "s:", otherwise "ht" would be NULL.
3453 switch (htname)
3454 {
3455 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
3456 case 'g': return &globvars_var;
3457 case 'v': return &vimvars_var;
3458 case 'b': return &curbuf->b_bufvar;
3459 case 'w': return &curwin->w_winvar;
3460 case 't': return &curtab->tp_winvar;
3461 case 'l': return get_funccal_local_var();
3462 case 'a': return get_funccal_args_var();
3463 }
3464 return NULL;
3465 }
3466
3467 hi = hash_find(ht, varname);
3468 if (HASHITEM_EMPTY(hi))
3469 {
3470 // For global variables we may try auto-loading the script. If it
3471 // worked find the variable again. Don't auto-load a script if it was
3472 // loaded already, otherwise it would be loaded every time when
3473 // checking if a function name is a Funcref variable.
3474 if (ht == &globvarht && !no_autoload)
3475 {
3476 // Note: script_autoload() may make "hi" invalid. It must either
3477 // be obtained again or not used.
3478 if (!script_autoload(varname, FALSE) || aborting())
3479 return NULL;
3480 hi = hash_find(ht, varname);
3481 }
3482 if (HASHITEM_EMPTY(hi))
3483 return NULL;
3484 }
3485 return HI2DI(hi);
3486}
3487
3488/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 * Get the script-local hashtab. NULL if not in a script context.
3490 */
Bram Moolenaar922acbd2020-10-08 21:30:40 +02003491 hashtab_T *
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492get_script_local_ht(void)
3493{
3494 scid_T sid = current_sctx.sc_sid;
3495
Bram Moolenaare3d46852020-08-29 13:39:17 +02003496 if (SCRIPT_ID_VALID(sid))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 return &SCRIPT_VARS(sid);
3498 return NULL;
3499}
3500
3501/*
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003502 * Look for "name[len]" in script-local variables and functions.
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003503 * When "cmd" is TRUE it must look like a command, a function must be followed
3504 * by "(" or "->".
Bram Moolenaar709664c2020-12-12 14:33:41 +01003505 * Return OK when found, FAIL when not found.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003506 */
Bram Moolenaar709664c2020-12-12 14:33:41 +01003507 int
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003508lookup_scriptitem(
Bram Moolenaar709664c2020-12-12 14:33:41 +01003509 char_u *name,
3510 size_t len,
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003511 int cmd,
Bram Moolenaar709664c2020-12-12 14:33:41 +01003512 cctx_T *dummy UNUSED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513{
3514 hashtab_T *ht = get_script_local_ht();
3515 char_u buffer[30];
3516 char_u *p;
Bram Moolenaar709664c2020-12-12 14:33:41 +01003517 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003518 hashitem_T *hi;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003519 int is_global = FALSE;
3520 char_u *fname = name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003521
3522 if (ht == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003523 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 if (len < sizeof(buffer) - 1)
3525 {
Bram Moolenaar7d3664d2020-05-09 13:06:24 +02003526 // avoid an alloc/free for short names
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003527 vim_strncpy(buffer, name, len);
3528 p = buffer;
3529 }
3530 else
3531 {
Bram Moolenaar71ccd032020-06-12 22:59:11 +02003532 p = vim_strnsave(name, len);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 if (p == NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003534 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 }
3536
3537 hi = hash_find(ht, p);
Bram Moolenaar709664c2020-12-12 14:33:41 +01003538 res = HASHITEM_EMPTY(hi) ? FAIL : OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539
Ernie Rael84f6dc72024-04-21 14:45:48 +02003540 // if not script-local, then perhaps autoload-exported
3541 if (res == FAIL && find_var_autoload_prefix(p, 0, NULL, NULL) != NULL)
3542 res = OK;
3543
3544 // if not script-local or autoload, then perhaps imported
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00003545 if (res == FAIL && find_imported(p, 0, FALSE) != NULL)
Bram Moolenaar709664c2020-12-12 14:33:41 +01003546 res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003547 if (p != buffer)
3548 vim_free(p);
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003549
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003550 // Find a function, so that a following "->" works.
3551 // When used as a command require "(" or "->" to follow, "Cmd" is a user
3552 // command while "Cmd()" is a function call.
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003553 if (res != OK)
3554 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003555 p = skipwhite(name + len);
3556
3557 if (!cmd || name[len] == '(' || (p[0] == '-' && p[1] == '>'))
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003558 {
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003559 // Do not check for an internal function, since it might also be a
3560 // valid command, such as ":split" versus "split()".
3561 // Skip "g:" before a function name.
3562 if (name[0] == 'g' && name[1] == ':')
3563 {
3564 is_global = TRUE;
3565 fname = name + 2;
3566 }
Bram Moolenaard9d2fd02022-01-13 21:15:21 +00003567 if (find_func(fname, is_global) != NULL)
Bram Moolenaar77b10ff2021-03-14 13:21:35 +01003568 res = OK;
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003569 }
Bram Moolenaar2e2d7582021-03-03 21:22:41 +01003570 }
3571
Bram Moolenaar709664c2020-12-12 14:33:41 +01003572 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573}
3574
3575/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003576 * Find the hashtab used for a variable name.
3577 * Return NULL if the name is not valid.
3578 * Set "varname" to the start of name without ':'.
3579 */
3580 hashtab_T *
3581find_var_ht(char_u *name, char_u **varname)
3582{
3583 hashitem_T *hi;
3584 hashtab_T *ht;
3585
3586 if (name[0] == NUL)
3587 return NULL;
3588 if (name[1] != ':')
3589 {
3590 // The name must not start with a colon or #.
3591 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
3592 return NULL;
3593 *varname = name;
3594
3595 // "version" is "v:version" in all scopes if scriptversion < 3.
3596 // Same for a few other variables marked with VV_COMPAT.
Bram Moolenaardd9de502021-08-15 13:49:42 +02003597 if (in_old_script(3))
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003598 {
3599 hi = hash_find(&compat_hashtab, name);
3600 if (!HASHITEM_EMPTY(hi))
3601 return &compat_hashtab;
3602 }
3603
3604 ht = get_funccal_local_ht();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 if (ht != NULL)
3606 return ht; // local variable
3607
Bram Moolenaarf0a40692021-06-11 22:05:47 +02003608 // In Vim9 script items at the script level are script-local, except
3609 // for autoload names.
3610 if (in_vim9script() && vim_strchr(name, AUTOLOAD_CHAR) == NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003611 {
3612 ht = get_script_local_ht();
3613 if (ht != NULL)
3614 return ht;
3615 }
3616
3617 return &globvarht; // global variable
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003618 }
3619 *varname = name + 2;
3620 if (*name == 'g') // global variable
3621 return &globvarht;
3622 // There must be no ':' or '#' in the rest of the name, unless g: is used
3623 if (vim_strchr(name + 2, ':') != NULL
3624 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
3625 return NULL;
3626 if (*name == 'b') // buffer variable
3627 return &curbuf->b_vars->dv_hashtab;
3628 if (*name == 'w') // window variable
3629 return &curwin->w_vars->dv_hashtab;
3630 if (*name == 't') // tab page variable
3631 return &curtab->tp_vars->dv_hashtab;
3632 if (*name == 'v') // v: variable
3633 return &vimvarht;
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003634 if (get_current_funccal() != NULL
Bram Moolenaarca16c602022-09-06 18:57:08 +01003635 && get_current_funccal()->fc_func->uf_def_status
3636 == UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 {
Bram Moolenaarb35efa52020-02-26 20:15:18 +01003638 // a: and l: are only used in functions defined with ":function"
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 if (*name == 'a') // a: function argument
3640 return get_funccal_args_ht();
3641 if (*name == 'l') // l: local function variable
3642 return get_funccal_local_ht();
3643 }
3644 if (*name == 's') // script variable
3645 {
3646 ht = get_script_local_ht();
3647 if (ht != NULL)
3648 return ht;
3649 }
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003650 return NULL;
3651}
3652
3653/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003654 * Get the string value of a (global/local) variable.
3655 * Note: see tv_get_string() for how long the pointer remains valid.
3656 * Returns NULL when it doesn't exist.
3657 */
3658 char_u *
3659get_var_value(char_u *name)
3660{
3661 dictitem_T *v;
3662
3663 v = find_var(name, NULL, FALSE);
3664 if (v == NULL)
3665 return NULL;
3666 return tv_get_string(&v->di_tv);
3667}
3668
3669/*
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003670 * Allocate a new hashtab for a sourced script. It will be used while
3671 * sourcing this script and when executing functions defined in the script.
3672 */
3673 void
3674new_script_vars(scid_T id)
3675{
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003676 scriptvar_T *sv;
3677
Bram Moolenaar7ebcba62020-01-12 17:42:55 +01003678 sv = ALLOC_CLEAR_ONE(scriptvar_T);
3679 if (sv == NULL)
3680 return;
3681 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003682 SCRIPT_ITEM(id)->sn_vars = sv;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003683}
3684
3685/*
3686 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
3687 * point to it.
3688 */
3689 void
3690init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
3691{
3692 hash_init(&dict->dv_hashtab);
3693 dict->dv_lock = 0;
3694 dict->dv_scope = scope;
3695 dict->dv_refcount = DO_NOT_FREE_CNT;
3696 dict->dv_copyID = 0;
3697 dict_var->di_tv.vval.v_dict = dict;
3698 dict_var->di_tv.v_type = VAR_DICT;
3699 dict_var->di_tv.v_lock = VAR_FIXED;
3700 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
3701 dict_var->di_key[0] = NUL;
3702}
3703
3704/*
3705 * Unreference a dictionary initialized by init_var_dict().
3706 */
3707 void
3708unref_var_dict(dict_T *dict)
3709{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02003710 // Now the dict needs to be freed if no one else is using it, go back to
3711 // normal reference counting.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003712 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
3713 dict_unref(dict);
3714}
3715
3716/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003717 * Clean up a list of internal variables.
3718 * Frees all allocated variables and the value they contain.
3719 * Clears hashtab "ht", does not free it.
3720 */
3721 void
3722vars_clear(hashtab_T *ht)
3723{
3724 vars_clear_ext(ht, TRUE);
3725}
3726
3727/*
3728 * Like vars_clear(), but only free the value if "free_val" is TRUE.
3729 */
3730 void
3731vars_clear_ext(hashtab_T *ht, int free_val)
3732{
3733 int todo;
3734 hashitem_T *hi;
3735 dictitem_T *v;
3736
3737 hash_lock(ht);
3738 todo = (int)ht->ht_used;
Yegappan Lakshmanan14113fd2023-03-07 17:13:51 +00003739 FOR_ALL_HASHTAB_ITEMS(ht, hi, todo)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003740 {
3741 if (!HASHITEM_EMPTY(hi))
3742 {
3743 --todo;
3744
3745 // Free the variable. Don't remove it from the hashtab,
3746 // ht_array might change then. hash_clear() takes care of it
3747 // later.
3748 v = HI2DI(hi);
3749 if (free_val)
3750 clear_tv(&v->di_tv);
3751 if (v->di_flags & DI_FLAGS_ALLOC)
3752 vim_free(v);
3753 }
3754 }
3755 hash_clear(ht);
Bram Moolenaar8d739de2020-10-14 19:39:19 +02003756 hash_init(ht);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003757}
3758
3759/*
3760 * Delete a variable from hashtab "ht" at item "hi".
3761 * Clear the variable value and free the dictitem.
3762 */
Bram Moolenaarfcdc5d82020-10-10 19:07:09 +02003763 void
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003764delete_var(hashtab_T *ht, hashitem_T *hi)
3765{
3766 dictitem_T *di = HI2DI(hi);
3767
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00003768 if (hash_remove(ht, hi, "delete variable") != OK)
3769 return;
3770
3771 clear_tv(&di->di_tv);
3772 vim_free(di);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003773}
3774
3775/*
3776 * List the value of one internal variable.
3777 */
3778 static void
3779list_one_var(dictitem_T *v, char *prefix, int *first)
3780{
3781 char_u *tofree;
3782 char_u *s;
3783 char_u numbuf[NUMBUFLEN];
3784
3785 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
3786 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
3787 s == NULL ? (char_u *)"" : s, first);
3788 vim_free(tofree);
3789}
3790
3791 static void
3792list_one_var_a(
3793 char *prefix,
3794 char_u *name,
3795 int type,
3796 char_u *string,
3797 int *first) // when TRUE clear rest of screen and set to FALSE
3798{
3799 // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
3800 msg_start();
3801 msg_puts(prefix);
3802 if (name != NULL) // "a:" vars don't have a name stored
3803 msg_puts((char *)name);
3804 msg_putchar(' ');
3805 msg_advance(22);
3806 if (type == VAR_NUMBER)
3807 msg_putchar('#');
3808 else if (type == VAR_FUNC || type == VAR_PARTIAL)
3809 msg_putchar('*');
3810 else if (type == VAR_LIST)
3811 {
3812 msg_putchar('[');
3813 if (*string == '[')
3814 ++string;
3815 }
3816 else if (type == VAR_DICT)
3817 {
3818 msg_putchar('{');
3819 if (*string == '{')
3820 ++string;
3821 }
3822 else
3823 msg_putchar(' ');
3824
3825 msg_outtrans(string);
3826
3827 if (type == VAR_FUNC || type == VAR_PARTIAL)
3828 msg_puts("()");
3829 if (*first)
3830 {
3831 msg_clr_eos();
3832 *first = FALSE;
3833 }
3834}
3835
3836/*
zeertzjqedcba962023-09-24 23:13:51 +02003837 * Addition handling for setting a v: variable.
3838 * Return TRUE if the variable should be set normally,
3839 * FALSE if nothing else needs to be done.
3840 */
3841 int
3842before_set_vvar(
3843 char_u *varname,
3844 dictitem_T *di,
3845 typval_T *tv,
3846 int copy,
3847 int *type_error)
3848{
3849 if (di->di_tv.v_type == VAR_STRING)
3850 {
3851 VIM_CLEAR(di->di_tv.vval.v_string);
3852 if (copy || tv->v_type != VAR_STRING)
3853 {
3854 char_u *val = tv_get_string(tv);
3855
3856 // Careful: when assigning to v:errmsg and
3857 // tv_get_string() causes an error message the variable
3858 // will already be set.
3859 if (di->di_tv.vval.v_string == NULL)
3860 di->di_tv.vval.v_string = vim_strsave(val);
3861 }
3862 else
3863 {
3864 // Take over the string to avoid an extra alloc/free.
3865 di->di_tv.vval.v_string = tv->vval.v_string;
3866 tv->vval.v_string = NULL;
3867 }
3868 return FALSE;
3869 }
3870 else if (di->di_tv.v_type == VAR_NUMBER)
3871 {
3872 di->di_tv.vval.v_number = tv_get_number(tv);
3873 if (STRCMP(varname, "searchforward") == 0)
3874 set_search_direction(di->di_tv.vval.v_number ? '/' : '?');
3875#ifdef FEAT_SEARCH_EXTRA
3876 else if (STRCMP(varname, "hlsearch") == 0)
3877 {
3878 no_hlsearch = !di->di_tv.vval.v_number;
3879 redraw_all_later(UPD_SOME_VALID);
3880 }
3881#endif
3882 return FALSE;
3883 }
3884 else if (di->di_tv.v_type != tv->v_type)
3885 {
3886 *type_error = TRUE;
3887 return FALSE;
3888 }
3889 return TRUE;
3890}
3891
3892/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003893 * Set variable "name" to value in "tv".
3894 * If the variable already exists, the value is updated.
3895 * Otherwise the variable is created.
3896 */
3897 void
3898set_var(
3899 char_u *name,
3900 typval_T *tv,
3901 int copy) // make copy of value in "tv"
3902{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003903 set_var_const(name, 0, NULL, tv, copy, ASSIGN_DECL, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003904}
3905
3906/*
Bram Moolenaar0e3e7ba2022-01-13 20:18:56 +00003907 * Set variable "name" to value in "tv_arg".
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003908 * When "sid" is non-zero "name" is in the script with this ID.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003909 * If the variable already exists and "is_const" is FALSE the value is updated.
3910 * Otherwise the variable is created.
3911 */
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003912 int
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003913set_var_const(
3914 char_u *name,
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003915 scid_T sid,
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003916 type_T *type_arg,
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003917 typval_T *tv_arg,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003918 int copy, // make copy of value in "tv"
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003919 int flags_arg, // ASSIGN_CONST, ASSIGN_FINAL, etc.
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003920 int var_idx) // index for ":let [a, b] = list"
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003921{
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003922 typval_T *tv = tv_arg;
Bram Moolenaar7824fc82021-11-26 17:36:51 +00003923 type_T *type = type_arg;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003924 typval_T bool_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 dictitem_T *di;
Bram Moolenaar993faa32022-02-21 15:59:11 +00003926 typval_T *dest_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003927 char_u *varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003928 char_u *name_tofree = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003929 hashtab_T *ht = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 int is_script_local;
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003931 int vim9script = in_vim9script();
Bram Moolenaare535db82021-03-31 21:07:24 +02003932 int var_in_vim9script;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003933 int var_in_autoload = FALSE;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02003934 int flags = flags_arg;
Bram Moolenaardd297bc2021-12-10 10:37:38 +00003935 int free_tv_arg = !copy; // free tv_arg if not used
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01003936 int rc = FAIL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003937
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003938 if (sid != 0)
3939 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003940 varname = NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003941 if (SCRIPT_ID_VALID(sid))
Ernie Rael84f6dc72024-04-21 14:45:48 +02003942 {
3943 char_u *auto_name = NULL;
3944 if (find_var_autoload_prefix(name, sid, &ht, &auto_name) != NULL)
3945 {
3946 var_in_autoload = TRUE;
3947 varname = auto_name;
3948 name_tofree = varname;
3949 }
3950 else
3951 ht = &SCRIPT_VARS(sid);
3952 }
3953 if (varname == NULL)
3954 varname = name;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00003955 }
3956 else
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003957 {
Ernie Rael84f6dc72024-04-21 14:45:48 +02003958 scriptitem_T *si;
3959 char_u *auto_name = NULL;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003960
Ernie Rael84f6dc72024-04-21 14:45:48 +02003961 if (in_vim9script()
3962 && SCRIPT_ID_VALID(current_sctx.sc_sid)
3963 && (si = SCRIPT_ITEM(current_sctx.sc_sid))
3964 ->sn_autoload_prefix != NULL
3965 && (is_export
3966 || find_var_autoload_prefix(name, 0, NULL, &auto_name)
3967 != NULL))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00003968 {
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003969 // In a vim9 autoload script an exported variable is put in the
3970 // global namespace with the autoload prefix.
3971 var_in_autoload = TRUE;
Ernie Rael84f6dc72024-04-21 14:45:48 +02003972 varname = auto_name != NULL ? auto_name
3973 : concat_str(si->sn_autoload_prefix, name);
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003974 if (varname == NULL)
3975 goto failed;
3976 name_tofree = varname;
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00003977 ht = &globvarht;
3978 }
3979 else
3980 ht = find_var_ht(name, &varname);
3981 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003982 if (ht == NULL || *varname == NUL)
3983 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003984 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003985 goto failed;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02003986 }
Bram Moolenaar54969f42022-02-07 13:56:44 +00003987 is_script_local = ht == get_script_local_ht() || sid != 0
3988 || var_in_autoload;
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003989
Bram Moolenaardbeecb22020-09-14 18:15:09 +02003990 if (vim9script
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003991 && !is_script_local
Bram Moolenaar3862ea32021-01-01 21:05:55 +01003992 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
Bram Moolenaar89b474d2020-12-22 21:19:39 +01003993 && (flags & (ASSIGN_CONST | ASSIGN_FINAL)) == 0
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003994 && name[1] == ':')
Bram Moolenaar67979662020-06-20 22:50:47 +02003995 {
Bram Moolenaare55b1c02020-06-21 15:52:59 +02003996 vim9_declare_error(name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02003997 goto failed;
Bram Moolenaar67979662020-06-20 22:50:47 +02003998 }
Bram Moolenaar9510d222022-09-11 15:14:05 +01003999 if ((flags & ASSIGN_FOR_LOOP) && is_scoped_variable(name))
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02004000 // Do not make g:var, w:var, b:var or t:var final.
4001 flags &= ~ASSIGN_FINAL;
4002
Bram Moolenaare535db82021-03-31 21:07:24 +02004003 var_in_vim9script = is_script_local && current_script_is_vim9();
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004004 if (var_in_vim9script && name[0] == '_' && name[1] == NUL)
4005 {
Bram Moolenaarf93bbd02021-04-10 22:35:43 +02004006 // For "[a, _] = list" the underscore is ignored.
4007 if ((flags & ASSIGN_UNPACK) == 0)
4008 emsg(_(e_cannot_use_underscore_here));
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004009 goto failed;
4010 }
Bram Moolenaar67979662020-06-20 22:50:47 +02004011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 di = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004013
Bram Moolenaar24e93162021-07-18 20:40:33 +02004014 if (di == NULL && var_in_vim9script)
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004015 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00004016 imported_T *import = find_imported(varname, 0, FALSE);
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02004017
Bram Moolenaar24e93162021-07-18 20:40:33 +02004018 if (import != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004019 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004020 // imported name space cannot be used
Bram Moolenaar24e93162021-07-18 20:40:33 +02004021 if ((flags & ASSIGN_NO_DECL) == 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004023 semsg(_(e_redefining_imported_item_str), name);
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004024 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004026 semsg(_(e_cannot_use_str_itself_it_is_imported), name);
4027 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004028 }
Bram Moolenaar75e27d72022-02-13 13:56:29 +00004029 if (!in_vim9script())
4030 {
4031 semsg(_(e_cannot_create_vim9_script_variable_in_function_str),
4032 name);
4033 goto failed;
4034 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004035 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036
Bram Moolenaar993faa32022-02-21 15:59:11 +00004037 // Search in parent scope which is possible to reference from lambda
4038 if (di == NULL)
4039 di = find_var_in_scoped_ht(name, TRUE);
4040
4041 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
4042 && var_wrong_func_name(name, di == NULL))
4043 goto failed;
4044
4045 if (need_convert_to_bool(type, tv))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004046 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004047 // Destination is a bool and the value is not, but it can be
4048 // converted.
4049 CLEAR_FIELD(bool_tv);
4050 bool_tv.v_type = VAR_BOOL;
4051 bool_tv.vval.v_number = tv2bool(tv) ? VVAL_TRUE : VVAL_FALSE;
4052 tv = &bool_tv;
4053 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004054
Bram Moolenaar993faa32022-02-21 15:59:11 +00004055 if (di != NULL)
4056 {
4057 // Item already exists. Allowed to replace when reloading.
4058 if ((di->di_flags & DI_FLAGS_RELOAD) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004059 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004060 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
4061 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004062 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004063 emsg(_(e_cannot_modify_existing_variable));
4064 goto failed;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004065 }
4066
Bram Moolenaar993faa32022-02-21 15:59:11 +00004067 if (is_script_local && vim9script
4068 && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0)
Bram Moolenaar12be7342021-03-31 21:47:33 +02004069 {
4070 semsg(_(e_redefining_script_item_str), name);
4071 goto failed;
4072 }
4073
Ernie Raele75fde62023-12-21 17:18:54 +01004074 if (check_typval_is_value(&di->di_tv) == FAIL)
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004075 goto failed;
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +02004076
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004077 // List and Blob types can be modified in-place using the "+="
4078 // compound operator. For other types, this is not allowed.
4079 int type_inplace_modifiable =
4080 (di->di_tv.v_type == VAR_LIST || di->di_tv.v_type == VAR_BLOB);
4081
4082 if (var_in_vim9script && (flags & ASSIGN_FOR_LOOP) == 0
4083 && ((flags & ASSIGN_COMPOUND_OP) == 0
4084 || !type_inplace_modifiable))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004085 {
Bram Moolenaar993faa32022-02-21 15:59:11 +00004086 where_T where = WHERE_INIT;
Bram Moolenaar7a411a32022-04-04 14:58:06 +01004087 svar_T *sv = find_typval_in_script(&di->di_tv, sid, TRUE);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004088
4089 if (sv != NULL)
4090 {
4091 // check the type and adjust to bool if needed
LemonBoyc5d27442023-08-19 13:02:35 +02004092 if (var_idx > 0)
4093 {
4094 where.wt_index = var_idx;
4095 where.wt_kind = WT_VARIABLE;
4096 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004097 if (check_script_var_type(sv, tv, name, where) == FAIL)
4098 goto failed;
4099 if (type == NULL)
4100 type = sv->sv_type;
Bram Moolenaaraa7d0c22022-04-05 21:40:38 +01004101 sv->sv_flags |= SVFLAG_ASSIGNED;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004102 }
Bram Moolenaar24e93162021-07-18 20:40:33 +02004103 }
4104
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004105 // Modifying a final variable with a List value using the "+="
4106 // operator is allowed. For other types, it is not allowed.
zeertzjq6b97d7a2024-08-08 21:05:57 +02004107 if ((((flags & ASSIGN_FOR_LOOP) == 0 || (flags & ASSIGN_DECL) == 0)
Yegappan Lakshmanan1af35632024-02-06 11:03:36 +01004108 && ((flags & ASSIGN_COMPOUND_OP) == 0
4109 || !type_inplace_modifiable))
Bram Moolenaar16d2c022023-06-05 19:46:18 +01004110 ? var_check_permission(di, name) == FAIL
4111 : var_check_ro(di->di_flags, name, FALSE))
Bram Moolenaar24e93162021-07-18 20:40:33 +02004112 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004113 }
4114 else
4115 {
4116 // can only redefine once
4117 di->di_flags &= ~DI_FLAGS_RELOAD;
Bram Moolenaar24e93162021-07-18 20:40:33 +02004118
Bram Moolenaar993faa32022-02-21 15:59:11 +00004119 // A Vim9 script-local variable is also present in sn_all_vars
4120 // and sn_var_vals. It may set "type" from "tv".
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004121 if (var_in_vim9script || var_in_autoload)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004122 update_vim9_script_var(FALSE, di,
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004123 var_in_autoload ? name : di->di_key, flags,
Bram Moolenaar993faa32022-02-21 15:59:11 +00004124 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01004125 }
4126
Bram Moolenaar993faa32022-02-21 15:59:11 +00004127 // existing variable, need to clear the value
4128
zeertzjqedcba962023-09-24 23:13:51 +02004129 // Handle setting internal v: variables separately where needed to
Bram Moolenaar993faa32022-02-21 15:59:11 +00004130 // prevent changing the type.
zeertzjqedcba962023-09-24 23:13:51 +02004131 int type_error = FALSE;
4132 if (ht == &vimvarht
4133 && !before_set_vvar(varname, di, tv, copy, &type_error))
Bram Moolenaar993faa32022-02-21 15:59:11 +00004134 {
zeertzjqedcba962023-09-24 23:13:51 +02004135 if (type_error)
4136 semsg(_(e_setting_v_str_to_value_with_wrong_type), varname);
4137 goto failed;
Bram Moolenaar993faa32022-02-21 15:59:11 +00004138 }
4139
4140 clear_tv(&di->di_tv);
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01004141
4142 if ((flags & ASSIGN_UPDATE_BLOCK_ID)
4143 && SCRIPT_ID_VALID(current_sctx.sc_sid))
4144 {
4145 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
4146
4147 update_script_var_block_id(name, si->sn_current_block_id);
4148 }
Bram Moolenaar993faa32022-02-21 15:59:11 +00004149 }
4150 else
4151 {
4152 // Item not found, check if a function already exists.
4153 if (is_script_local && (flags & (ASSIGN_NO_DECL | ASSIGN_DECL)) == 0
4154 && lookup_scriptitem(name, STRLEN(name), FALSE, NULL) == OK)
4155 {
4156 semsg(_(e_redefining_script_item_str), name);
4157 goto failed;
4158 }
4159
4160 // add a new variable
4161 if (var_in_vim9script && (flags & ASSIGN_NO_DECL))
4162 {
4163 semsg(_(e_unknown_variable_str), name);
4164 goto failed;
4165 }
4166
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004167 if (check_hashtab_frozen(ht, "add variable"))
4168 goto failed;
4169
Bram Moolenaar993faa32022-02-21 15:59:11 +00004170 // Can't add "v:" or "a:" variable.
4171 if (ht == &vimvarht || ht == get_funccal_args_ht())
4172 {
4173 semsg(_(e_illegal_variable_name_str), name);
4174 goto failed;
4175 }
4176
4177 // Make sure the variable name is valid. In Vim9 script an
4178 // autoload variable must be prefixed with "g:" unless in an
4179 // autoload script.
4180 if (!valid_varname(varname, -1, !vim9script
4181 || STRNCMP(name, "g:", 2) == 0 || var_in_autoload))
4182 goto failed;
4183
zeertzjq1b438a82023-02-01 13:11:15 +00004184 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(varname) + 1);
Bram Moolenaar993faa32022-02-21 15:59:11 +00004185 if (di == NULL)
4186 goto failed;
4187 STRCPY(di->di_key, varname);
Bram Moolenaaref2c3252022-11-25 16:31:51 +00004188 if (hash_add(ht, DI2HIKEY(di), "add variable") == FAIL)
Bram Moolenaar993faa32022-02-21 15:59:11 +00004189 {
4190 vim_free(di);
4191 goto failed;
4192 }
4193 di->di_flags = DI_FLAGS_ALLOC;
4194 if (flags & (ASSIGN_CONST | ASSIGN_FINAL))
4195 di->di_flags |= DI_FLAGS_LOCK;
4196
4197 // A Vim9 script-local variable is also added to sn_all_vars and
4198 // sn_var_vals. It may set "type" from "tv".
4199 if (var_in_vim9script || var_in_autoload)
4200 update_vim9_script_var(TRUE, di,
4201 var_in_autoload ? name : di->di_key, flags,
4202 tv, &type, (flags & ASSIGN_NO_MEMBER_TYPE) == 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004203 }
4204
Bram Moolenaar993faa32022-02-21 15:59:11 +00004205 dest_tv = &di->di_tv;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004206 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar24e93162021-07-18 20:40:33 +02004207 copy_tv(tv, dest_tv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004208 else
4209 {
Bram Moolenaar24e93162021-07-18 20:40:33 +02004210 *dest_tv = *tv;
4211 dest_tv->v_lock = 0;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004212 init_tv(tv);
4213 }
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004214 free_tv_arg = FALSE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004215
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004216 if (vim9script && type != NULL)
Bram Moolenaar381692b2022-02-02 20:01:27 +00004217 set_tv_type(dest_tv, type);
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004218
Bram Moolenaar1dcf55d2020-12-22 22:07:30 +01004219 // ":const var = value" locks the value
4220 // ":final var = value" locks "var"
Bram Moolenaar30fd8202020-09-26 15:09:30 +02004221 if (flags & ASSIGN_CONST)
Bram Moolenaar021bda52020-08-17 21:07:22 +02004222 // Like :lockvar! name: lock the value and what it contains, but only
4223 // if the reference count is up to one. That locks only literal
4224 // values.
Bram Moolenaar24e93162021-07-18 20:40:33 +02004225 item_lock(dest_tv, DICT_MAXNEST, TRUE, TRUE);
Bram Moolenaar24e93162021-07-18 20:40:33 +02004226
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004227 rc = OK;
4228
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004229failed:
Bram Moolenaarfe2ef0b2022-01-10 18:08:00 +00004230 vim_free(name_tofree);
Bram Moolenaardd297bc2021-12-10 10:37:38 +00004231 if (free_tv_arg)
Bram Moolenaarb0fa5e12020-09-12 19:51:42 +02004232 clear_tv(tv_arg);
Yegappan Lakshmanan35b867b2024-03-09 15:44:19 +01004233
4234 return rc;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004235}
4236
4237/*
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004238 * Check in this order for backwards compatibility:
4239 * - Whether the variable is read-only
4240 * - Whether the variable value is locked
4241 * - Whether the variable is locked
Ernie Rael3f821d62024-04-24 20:07:50 +02004242 * NOTE: "name" is only used for error messages.
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01004243 */
4244 int
4245var_check_permission(dictitem_T *di, char_u *name)
4246{
4247 if (var_check_ro(di->di_flags, name, FALSE)
4248 || value_check_lock(di->di_tv.v_lock, name, FALSE)
4249 || var_check_lock(di->di_flags, name, FALSE))
4250 return FAIL;
4251 return OK;
4252}
4253
4254/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004255 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
4256 * Also give an error message.
4257 */
4258 int
4259var_check_ro(int flags, char_u *name, int use_gettext)
4260{
4261 if (flags & DI_FLAGS_RO)
4262 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004263 if (name == NULL)
4264 emsg(_(e_cannot_change_readonly_variable));
4265 else
4266 semsg(_(e_cannot_change_readonly_variable_str),
Bram Moolenaard8e44472021-07-21 22:20:33 +02004267 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004268 return TRUE;
4269 }
4270 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
4271 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004272 if (name == NULL)
4273 emsg(_(e_cannot_set_variable_in_sandbox));
4274 else
4275 semsg(_(e_cannot_set_variable_in_sandbox_str),
4276 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004277 return TRUE;
4278 }
4279 return FALSE;
4280}
4281
4282/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004283 * Return TRUE if di_flags "flags" indicates variable "name" is locked.
4284 * Also give an error message.
4285 */
4286 int
4287var_check_lock(int flags, char_u *name, int use_gettext)
4288{
4289 if (flags & DI_FLAGS_LOCK)
4290 {
4291 semsg(_(e_variable_is_locked_str),
4292 use_gettext ? (char_u *)_(name) : name);
4293 return TRUE;
4294 }
4295 return FALSE;
4296}
4297
4298/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004299 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
4300 * Also give an error message.
4301 */
4302 int
4303var_check_fixed(int flags, char_u *name, int use_gettext)
4304{
4305 if (flags & DI_FLAGS_FIX)
4306 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004307 if (name == NULL)
4308 emsg(_(e_cannot_delete_variable));
4309 else
4310 semsg(_(e_cannot_delete_variable_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004311 use_gettext ? (char_u *)_(name) : name);
4312 return TRUE;
4313 }
4314 return FALSE;
4315}
4316
4317/*
4318 * Check if a funcref is assigned to a valid variable name.
4319 * Return TRUE and give an error if not.
4320 */
4321 int
Bram Moolenaar98b4f142020-08-08 15:46:01 +02004322var_wrong_func_name(
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004323 char_u *name, // points to start of variable name
4324 int new_var) // TRUE when creating the variable
4325{
Bram Moolenaar32154662021-03-28 21:14:06 +02004326 // Allow for w: b: s: and t:. In Vim9 script s: is not allowed, because
4327 // the name can be used without the s: prefix.
thinca6c667bd2022-09-02 11:25:37 +01004328 // Allow autoload variable.
Bram Moolenaar32154662021-03-28 21:14:06 +02004329 if (!((vim_strchr((char_u *)"wbt", name[0]) != NULL
4330 || (!in_vim9script() && name[0] == 's')) && name[1] == ':')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004331 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
thinca6c667bd2022-09-02 11:25:37 +01004332 ? name[2] : name[0])
4333 && vim_strchr(name, '#') == NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004334 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004335 semsg(_(e_funcref_variable_name_must_start_with_capital_str), name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004336 return TRUE;
4337 }
4338 // Don't allow hiding a function. When "v" is not NULL we might be
4339 // assigning another function to the same var, the type is checked
4340 // below.
4341 if (new_var && function_exists(name, FALSE))
4342 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00004343 semsg(_(e_variable_name_conflicts_with_existing_function_str),
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004344 name);
4345 return TRUE;
4346 }
4347 return FALSE;
4348}
4349
4350/*
Bram Moolenaara187c432020-09-16 21:08:28 +02004351 * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
4352 * value. Also give an error message, using "name" or _("name") when
4353 * "use_gettext" is TRUE.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004354 */
4355 int
Bram Moolenaara187c432020-09-16 21:08:28 +02004356value_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004357{
4358 if (lock & VAR_LOCKED)
4359 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004360 if (name == NULL)
4361 emsg(_(e_value_is_locked));
4362 else
4363 semsg(_(e_value_is_locked_str),
4364 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004365 return TRUE;
4366 }
4367 if (lock & VAR_FIXED)
4368 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00004369 if (name == NULL)
4370 emsg(_(e_cannot_change_value));
4371 else
4372 semsg(_(e_cannot_change_value_of_str),
4373 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004374 return TRUE;
4375 }
4376 return FALSE;
4377}
4378
4379/*
Bram Moolenaar03290b82020-12-19 16:30:44 +01004380 * Check if a variable name is valid. When "autoload" is true "#" is allowed.
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004381 * If "len" is -1 use all of "varname", otherwise up to "varname[len]".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004382 * Return FALSE and give an error if not.
4383 */
4384 int
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004385valid_varname(char_u *varname, int len, int autoload)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004386{
4387 char_u *p;
4388
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00004389 for (p = varname; len < 0 ? *p != NUL : p < varname + len; ++p)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004390 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
Bram Moolenaar03290b82020-12-19 16:30:44 +01004391 && !(autoload && *p == AUTOLOAD_CHAR))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004392 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004393 semsg(_(e_illegal_variable_name_str), varname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004394 return FALSE;
4395 }
4396 return TRUE;
4397}
4398
4399/*
LemonBoy47d4e312022-05-04 18:12:55 +01004400 * Implements the logic to retrieve local variable and option values.
4401 * Used by "getwinvar()" "gettabvar()" "gettabwinvar()" "getbufvar()".
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004402 */
4403 static void
LemonBoy47d4e312022-05-04 18:12:55 +01004404get_var_from(
4405 char_u *varname,
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004406 typval_T *rettv,
LemonBoy47d4e312022-05-04 18:12:55 +01004407 typval_T *deftv, // Default value if not found.
4408 int htname, // 't'ab, 'w'indow or 'b'uffer local.
4409 tabpage_T *tp, // can be NULL
4410 win_T *win,
4411 buf_T *buf) // Ignored if htname is not 'b'.
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004412{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004413 dictitem_T *v;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004414 int done = FALSE;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004415 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004416 int need_switch_win;
zeertzjqcd6ad642022-07-25 12:28:09 +01004417 int do_change_curbuf = buf != NULL && htname == 'b';
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004418
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004419 ++emsg_off;
4420
4421 rettv->v_type = VAR_STRING;
4422 rettv->vval.v_string = NULL;
4423
LemonBoy47d4e312022-05-04 18:12:55 +01004424 if (varname != NULL && tp != NULL && win != NULL
4425 && (htname != 'b' || buf != NULL))
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004426 {
4427 // Set curwin to be our win, temporarily. Also set the tabpage,
4428 // otherwise the window is not valid. Only do this when needed,
4429 // autocommands get blocked.
LemonBoy47d4e312022-05-04 18:12:55 +01004430 // If we have a buffer reference avoid the switching, we're saving and
4431 // restoring curbuf directly.
zeertzjqcd6ad642022-07-25 12:28:09 +01004432 need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
LemonBoy47d4e312022-05-04 18:12:55 +01004433 if (!need_switch_win || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004434 {
LemonBoy47d4e312022-05-04 18:12:55 +01004435 // Handle options. There are no tab-local options.
4436 if (*varname == '&' && htname != 't')
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004437 {
LemonBoy47d4e312022-05-04 18:12:55 +01004438 buf_T *save_curbuf = curbuf;
4439
4440 // Change curbuf so the option is read from the correct buffer.
zeertzjqcd6ad642022-07-25 12:28:09 +01004441 if (do_change_curbuf)
LemonBoy47d4e312022-05-04 18:12:55 +01004442 curbuf = buf;
4443
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004444 if (varname[1] == NUL)
4445 {
zeertzjqcd6ad642022-07-25 12:28:09 +01004446 // get all window-local or buffer-local options in a dict
LemonBoy47d4e312022-05-04 18:12:55 +01004447 dict_T *opts = get_winbuf_options(htname == 'b');
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004448
4449 if (opts != NULL)
4450 {
4451 rettv_dict_set(rettv, opts);
4452 done = TRUE;
4453 }
4454 }
LemonBoy47d4e312022-05-04 18:12:55 +01004455 else if (eval_option(&varname, rettv, TRUE) == OK)
4456 // Local option
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004457 done = TRUE;
LemonBoy47d4e312022-05-04 18:12:55 +01004458
4459 curbuf = save_curbuf;
4460 }
4461 else if (*varname == NUL)
4462 {
4463 // Empty string: return a dict with all the local variables.
4464 if (htname == 'b')
4465 v = &buf->b_bufvar;
4466 else if (htname == 'w')
4467 v = &win->w_winvar;
4468 else
4469 v = &tp->tp_winvar;
4470 copy_tv(&v->di_tv, rettv);
4471 done = TRUE;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004472 }
4473 else
4474 {
LemonBoy47d4e312022-05-04 18:12:55 +01004475 hashtab_T *ht;
4476
4477 if (htname == 'b')
4478 ht = &buf->b_vars->dv_hashtab;
4479 else if (htname == 'w')
4480 ht = &win->w_vars->dv_hashtab;
4481 else
4482 ht = &tp->tp_vars->dv_hashtab;
4483
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004484 // Look up the variable.
LemonBoy47d4e312022-05-04 18:12:55 +01004485 v = find_var_in_ht(ht, htname, varname, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004486 if (v != NULL)
4487 {
4488 copy_tv(&v->di_tv, rettv);
4489 done = TRUE;
4490 }
4491 }
4492 }
4493
4494 if (need_switch_win)
4495 // restore previous notion of curwin
Bram Moolenaar18f47402022-01-06 13:24:51 +00004496 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004497 }
4498
LemonBoy47d4e312022-05-04 18:12:55 +01004499 if (!done && deftv->v_type != VAR_UNKNOWN)
4500 // use the default value
4501 copy_tv(deftv, rettv);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004502
4503 --emsg_off;
4504}
4505
4506/*
LemonBoy47d4e312022-05-04 18:12:55 +01004507 * getwinvar() and gettabwinvar()
4508 */
4509 static void
4510getwinvar(
4511 typval_T *argvars,
4512 typval_T *rettv,
4513 int off) // 1 for gettabwinvar()
4514{
4515 char_u *varname;
4516 tabpage_T *tp;
4517 win_T *win;
4518
4519 if (off == 1)
4520 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4521 else
4522 tp = curtab;
4523 win = find_win_by_nr(&argvars[off], tp);
4524 varname = tv_get_string_chk(&argvars[off + 1]);
4525
4526 get_var_from(varname, rettv, &argvars[off + 2], 'w', tp, win, NULL);
4527}
4528
4529/*
Bram Moolenaar191929b2020-08-19 21:20:49 +02004530 * Set option "varname" to the value of "varp" for the current buffer/window.
4531 */
4532 static void
4533set_option_from_tv(char_u *varname, typval_T *varp)
4534{
4535 long numval = 0;
4536 char_u *strval;
4537 char_u nbuf[NUMBUFLEN];
4538 int error = FALSE;
4539
zeertzjq4c7cb372023-06-14 16:39:54 +01004540 int opt_idx = findoption(varname);
4541 if (opt_idx < 0)
4542 {
4543 semsg(_(e_unknown_option_str_2), varname);
4544 return;
4545 }
4546 int opt_p_flags = get_option_flags(opt_idx);
4547
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004548 if (varp->v_type == VAR_BOOL)
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004549 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004550 if (opt_p_flags & P_STRING)
Bram Moolenaar28f84e12022-07-27 12:30:13 +01004551 {
4552 emsg(_(e_string_required));
4553 return;
4554 }
Bram Moolenaar31a201a2021-01-03 14:47:25 +01004555 numval = (long)varp->vval.v_number;
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004556 strval = (char_u *)"0"; // avoid using "false"
4557 }
4558 else
4559 {
zeertzjq4c7cb372023-06-14 16:39:54 +01004560 if ((opt_p_flags & (P_NUM|P_BOOL))
4561 && (!in_vim9script() || varp->v_type != VAR_STRING))
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004562 numval = (long)tv_get_number_chk(varp, &error);
zeertzjq4c7cb372023-06-14 16:39:54 +01004563 if (!error)
4564 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaarb0d81822021-01-03 15:55:10 +01004565 }
Bram Moolenaar191929b2020-08-19 21:20:49 +02004566 if (!error && strval != NULL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01004567 set_option_value_give_err(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar191929b2020-08-19 21:20:49 +02004568}
4569
4570/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004571 * "setwinvar()" and "settabwinvar()" functions
4572 */
4573 static void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004574setwinvar(typval_T *argvars, int off)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004575{
4576 win_T *win;
Bram Moolenaar18f47402022-01-06 13:24:51 +00004577 switchwin_T switchwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004578 int need_switch_win;
4579 char_u *varname, *winvarname;
4580 typval_T *varp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004581 tabpage_T *tp = NULL;
4582
4583 if (check_secure())
4584 return;
4585
4586 if (off == 1)
4587 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4588 else
4589 tp = curtab;
4590 win = find_win_by_nr(&argvars[off], tp);
4591 varname = tv_get_string_chk(&argvars[off + 1]);
4592 varp = &argvars[off + 2];
4593
zeertzjqea720ae2023-01-03 10:54:09 +00004594 if (win == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004595 return;
4596
4597 need_switch_win = !(tp == curtab && win == curwin);
4598 if (!need_switch_win
4599 || switch_win(&switchwin, win, tp, TRUE) == OK)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004600 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004601 if (*varname == '&')
4602 set_option_from_tv(varname + 1, varp);
4603 else
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004604 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004605 winvarname = alloc(STRLEN(varname) + 3);
4606 if (winvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004607 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004608 STRCPY(winvarname, "w:");
4609 STRCPY(winvarname + 2, varname);
4610 set_var(winvarname, varp, TRUE);
4611 vim_free(winvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004612 }
4613 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004614 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004615 if (need_switch_win)
4616 restore_win(&switchwin, TRUE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004617}
4618
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004619/*
4620 * reset v:option_new, v:option_old, v:option_oldlocal, v:option_oldglobal,
4621 * v:option_type, and v:option_command.
4622 */
4623 void
4624reset_v_option_vars(void)
4625{
4626 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
4627 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
4628 set_vim_var_string(VV_OPTION_OLDLOCAL, NULL, -1);
4629 set_vim_var_string(VV_OPTION_OLDGLOBAL, NULL, -1);
4630 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
4631 set_vim_var_string(VV_OPTION_COMMAND, NULL, -1);
4632}
4633
4634/*
4635 * Add an assert error to v:errors.
4636 */
4637 void
4638assert_error(garray_T *gap)
4639{
4640 struct vimvar *vp = &vimvars[VV_ERRORS];
4641
Bram Moolenaard787e402021-12-24 21:36:12 +00004642 if (vp->vv_tv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004643 // Make sure v:errors is a list.
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004644 set_vim_var_list(VV_ERRORS, list_alloc());
4645 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
4646}
4647
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004648 int
4649var_exists(char_u *var)
4650{
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004651 char_u *arg = var;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004652 char_u *name;
4653 char_u *tofree;
4654 typval_T tv;
4655 int len = 0;
4656 int n = FALSE;
4657
4658 // get_name_len() takes care of expanding curly braces
4659 name = var;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004660 len = get_name_len(&arg, &tofree, TRUE, FALSE);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004661 if (len > 0)
4662 {
4663 if (tofree != NULL)
4664 name = tofree;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004665 n = (eval_variable(name, len, 0, &tv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004666 EVAL_VAR_NOAUTOLOAD + EVAL_VAR_IMPORT) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004667 if (n)
4668 {
4669 // handle d.key, l[idx], f(expr)
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004670 arg = skipwhite(arg);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004671 n = (handle_subscript(&arg, name, &tv, &EVALARG_EVALUATE,
4672 FALSE) == OK);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004673 if (n)
4674 clear_tv(&tv);
4675 }
4676 }
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02004677 if (*arg != NUL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004678 n = FALSE;
4679
4680 vim_free(tofree);
4681 return n;
4682}
4683
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004684static lval_T *redir_lval = NULL;
4685#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
4686static garray_T redir_ga; // only valid when redir_lval is not NULL
4687static char_u *redir_endp = NULL;
4688static char_u *redir_varname = NULL;
4689
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004690 int
4691alloc_redir_lval(void)
4692{
4693 redir_lval = ALLOC_CLEAR_ONE(lval_T);
4694 if (redir_lval == NULL)
4695 return FAIL;
4696 return OK;
4697}
4698
4699 void
4700clear_redir_lval(void)
4701{
4702 VIM_CLEAR(redir_lval);
4703}
4704
4705 void
4706init_redir_ga(void)
4707{
Bram Moolenaar04935fb2022-01-08 16:19:22 +00004708 ga_init2(&redir_ga, sizeof(char), 500);
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004709}
4710
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004711/*
4712 * Start recording command output to a variable
4713 * When "append" is TRUE append to an existing variable.
4714 * Returns OK if successfully completed the setup. FAIL otherwise.
4715 */
4716 int
4717var_redir_start(char_u *name, int append)
4718{
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004719 int called_emsg_before;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004720 typval_T tv;
4721
4722 // Catch a bad name early.
4723 if (!eval_isnamec1(*name))
4724 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004725 emsg(_(e_invalid_argument));
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004726 return FAIL;
4727 }
4728
4729 // Make a copy of the name, it is used in redir_lval until redir ends.
4730 redir_varname = vim_strsave(name);
4731 if (redir_varname == NULL)
4732 return FAIL;
4733
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004734 if (alloc_redir_lval() == FAIL)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004735 {
4736 var_redir_stop();
4737 return FAIL;
4738 }
4739
4740 // The output is stored in growarray "redir_ga" until redirection ends.
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004741 init_redir_ga();
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004742
4743 // Parse the variable name (can be a dict or list entry).
4744 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
4745 FNE_CHECK_START);
4746 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
4747 {
4748 clear_lval(redir_lval);
4749 if (redir_endp != NULL && *redir_endp != NUL)
4750 // Trailing characters are present after the variable name
Bram Moolenaar74409f62022-01-01 15:58:22 +00004751 semsg(_(e_trailing_characters_str), redir_endp);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004752 else
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00004753 semsg(_(e_invalid_argument_str), name);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004754 redir_endp = NULL; // don't store a value, only cleanup
4755 var_redir_stop();
4756 return FAIL;
4757 }
4758
4759 // check if we can write to the variable: set it to or append an empty
4760 // string
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004761 called_emsg_before = called_emsg;
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004762 tv.v_type = VAR_STRING;
4763 tv.vval.v_string = (char_u *)"";
4764 if (append)
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 else
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004768 set_var_lval(redir_lval, redir_endp, &tv, TRUE,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004769 ASSIGN_NO_DECL, (char_u *)"=", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004770 clear_lval(redir_lval);
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02004771 if (called_emsg > called_emsg_before)
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004772 {
4773 redir_endp = NULL; // don't store a value, only cleanup
4774 var_redir_stop();
4775 return FAIL;
4776 }
4777
4778 return OK;
4779}
4780
4781/*
4782 * Append "value[value_len]" to the variable set by var_redir_start().
4783 * The actual appending is postponed until redirection ends, because the value
4784 * appended may in fact be the string we write to, changing it may cause freed
4785 * memory to be used:
4786 * :redir => foo
4787 * :let foo
4788 * :redir END
4789 */
4790 void
4791var_redir_str(char_u *value, int value_len)
4792{
4793 int len;
4794
4795 if (redir_lval == NULL)
4796 return;
4797
4798 if (value_len == -1)
4799 len = (int)STRLEN(value); // Append the entire string
4800 else
4801 len = value_len; // Append only "value_len" characters
4802
4803 if (ga_grow(&redir_ga, len) == OK)
4804 {
4805 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
4806 redir_ga.ga_len += len;
4807 }
4808 else
4809 var_redir_stop();
4810}
4811
4812/*
4813 * Stop redirecting command output to a variable.
4814 * Frees the allocated memory.
4815 */
4816 void
4817var_redir_stop(void)
4818{
4819 typval_T tv;
4820
4821 if (EVALCMD_BUSY)
4822 {
4823 redir_lval = NULL;
4824 return;
4825 }
4826
4827 if (redir_lval != NULL)
4828 {
4829 // If there was no error: assign the text to the variable.
4830 if (redir_endp != NULL)
4831 {
4832 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4833 tv.v_type = VAR_STRING;
4834 tv.vval.v_string = redir_ga.ga_data;
4835 // Call get_lval() again, if it's inside a Dict or List it may
4836 // have changed.
4837 redir_endp = get_lval(redir_varname, NULL, redir_lval,
4838 FALSE, FALSE, 0, FNE_CHECK_START);
4839 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004840 set_var_lval(redir_lval, redir_endp, &tv, FALSE, 0,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004841 (char_u *)".", 0);
Bram Moolenaarda6c0332019-09-01 16:01:30 +02004842 clear_lval(redir_lval);
4843 }
4844
4845 // free the collected output
4846 VIM_CLEAR(redir_ga.ga_data);
4847
4848 VIM_CLEAR(redir_lval);
4849 }
4850 VIM_CLEAR(redir_varname);
4851}
4852
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004853/*
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02004854 * Get the collected redirected text and clear redir_ga.
4855 */
4856 char_u *
4857get_clear_redir_ga(void)
4858{
4859 char_u *res;
4860
4861 ga_append(&redir_ga, NUL); // Append the trailing NUL.
4862 res = redir_ga.ga_data;
4863 redir_ga.ga_data = NULL;
4864 return res;
4865}
4866
4867/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004868 * "gettabvar()" function
4869 */
4870 void
4871f_gettabvar(typval_T *argvars, typval_T *rettv)
4872{
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004873 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004874 tabpage_T *tp;
4875 win_T *win = NULL;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004876
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004877 if (in_vim9script()
4878 && (check_for_number_arg(argvars, 0) == FAIL
4879 || check_for_string_arg(argvars, 1) == FAIL))
4880 return;
4881
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004882 varname = tv_get_string_chk(&argvars[1]);
4883 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
LemonBoy47d4e312022-05-04 18:12:55 +01004884 if (tp != NULL)
4885 win = tp == curtab || tp->tp_firstwin == NULL ? firstwin
4886 : tp->tp_firstwin;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004887
LemonBoy47d4e312022-05-04 18:12:55 +01004888 get_var_from(varname, rettv, &argvars[2], 't', tp, win, NULL);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004889}
4890
4891/*
4892 * "gettabwinvar()" function
4893 */
4894 void
4895f_gettabwinvar(typval_T *argvars, typval_T *rettv)
4896{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004897 if (in_vim9script()
4898 && (check_for_number_arg(argvars, 0) == FAIL
4899 || check_for_number_arg(argvars, 1) == FAIL
4900 || check_for_string_arg(argvars, 2) == FAIL))
4901 return;
4902
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004903 getwinvar(argvars, rettv, 1);
4904}
4905
4906/*
4907 * "getwinvar()" function
4908 */
4909 void
4910f_getwinvar(typval_T *argvars, typval_T *rettv)
4911{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004912 if (in_vim9script()
4913 && (check_for_number_arg(argvars, 0) == FAIL
4914 || check_for_string_arg(argvars, 1) == FAIL))
4915 return;
4916
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004917 getwinvar(argvars, rettv, 0);
4918}
4919
4920/*
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004921 * "getbufvar()" function
4922 */
4923 void
4924f_getbufvar(typval_T *argvars, typval_T *rettv)
4925{
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004926 char_u *varname;
LemonBoy47d4e312022-05-04 18:12:55 +01004927 buf_T *buf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004928
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02004929 if (in_vim9script()
4930 && (check_for_buffer_arg(argvars, 0) == FAIL
4931 || check_for_string_arg(argvars, 1) == FAIL))
4932 return;
4933
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004934 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02004935 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004936
LemonBoy47d4e312022-05-04 18:12:55 +01004937 get_var_from(varname, rettv, &argvars[2], 'b', curtab, curwin, buf);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02004938}
4939
4940/*
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004941 * "settabvar()" function
4942 */
4943 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004944f_settabvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004945{
4946 tabpage_T *save_curtab;
4947 tabpage_T *tp;
zeertzjqb47fbb42024-02-12 22:50:26 +01004948 tabpage_T *save_lu_tp;
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004949 char_u *varname, *tabvarname;
4950 typval_T *varp;
4951
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004952 if (check_secure())
4953 return;
4954
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004955 if (in_vim9script()
4956 && (check_for_number_arg(argvars, 0) == FAIL
4957 || check_for_string_arg(argvars, 1) == FAIL))
4958 return;
4959
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004960 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
4961 varname = tv_get_string_chk(&argvars[1]);
4962 varp = &argvars[2];
4963
zeertzjqea720ae2023-01-03 10:54:09 +00004964 if (varname == NULL || tp == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004965 return;
4966
4967 save_curtab = curtab;
zeertzjqb47fbb42024-02-12 22:50:26 +01004968 save_lu_tp = lastused_tabpage;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004969 goto_tabpage_tp(tp, FALSE, FALSE);
4970
4971 tabvarname = alloc(STRLEN(varname) + 3);
4972 if (tabvarname != NULL)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004973 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004974 STRCPY(tabvarname, "t:");
4975 STRCPY(tabvarname + 2, varname);
4976 set_var(tabvarname, varp, TRUE);
4977 vim_free(tabvarname);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004978 }
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004979
zeertzjqb47fbb42024-02-12 22:50:26 +01004980 // Restore current tabpage and last accessed tabpage.
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004981 if (valid_tabpage(save_curtab))
zeertzjqb47fbb42024-02-12 22:50:26 +01004982 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00004983 goto_tabpage_tp(save_curtab, FALSE, FALSE);
zeertzjqb47fbb42024-02-12 22:50:26 +01004984 if (valid_tabpage(save_lu_tp))
4985 lastused_tabpage = save_lu_tp;
4986 }
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004987}
4988
4989/*
4990 * "settabwinvar()" function
4991 */
4992 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01004993f_settabwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004994{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004995 if (in_vim9script()
4996 && (check_for_number_arg(argvars, 0) == FAIL
4997 || check_for_number_arg(argvars, 1) == FAIL
4998 || check_for_string_arg(argvars, 2) == FAIL))
4999 return;
5000
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005001 setwinvar(argvars, 1);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005002}
5003
5004/*
5005 * "setwinvar()" function
5006 */
5007 void
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005008f_setwinvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005009{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02005010 if (in_vim9script()
5011 && (check_for_number_arg(argvars, 0) == FAIL
5012 || check_for_string_arg(argvars, 1) == FAIL))
5013 return;
5014
Bram Moolenaar3d8a5132020-01-04 16:13:49 +01005015 setwinvar(argvars, 0);
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005016}
5017
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005018/*
5019 * "setbufvar()" function
5020 */
5021 void
5022f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
5023{
5024 buf_T *buf;
5025 char_u *varname, *bufvarname;
5026 typval_T *varp;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005027
5028 if (check_secure())
5029 return;
Yegappan Lakshmanan7973de32021-07-24 16:16:15 +02005030
5031 if (in_vim9script()
5032 && (check_for_buffer_arg(argvars, 0) == FAIL
5033 || check_for_string_arg(argvars, 1) == FAIL))
5034 return;
5035
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005036 varname = tv_get_string_chk(&argvars[1]);
Bram Moolenaar6f84b6d2020-09-01 23:16:32 +02005037 buf = tv_get_buf_from_arg(&argvars[0]);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005038 varp = &argvars[2];
5039
zeertzjqea720ae2023-01-03 10:54:09 +00005040 if (buf == NULL || varname == NULL)
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005041 return;
5042
5043 if (*varname == '&')
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005044 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005045 aco_save_T aco;
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005046 // safe the current window position, it could
5047 // change because of 'scrollbind' window-local
5048 // options
5049 linenr_T old_topline = curwin->w_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005050
5051 // Set curbuf to be our buf, temporarily.
5052 aucmd_prepbuf(&aco, buf);
5053 if (curbuf == buf)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005054 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005055 // Only when it worked to set "curbuf".
5056 set_option_from_tv(varname + 1, varp);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005057
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005058 // reset notion of buffer
5059 aucmd_restbuf(&aco);
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005060 }
Christian Brabandtac4cffc2024-01-16 17:22:38 +01005061 curwin->w_topline = old_topline;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005062 }
5063 else
5064 {
5065 bufvarname = alloc(STRLEN(varname) + 3);
5066 if (bufvarname != NULL)
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005067 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005068 buf_T *save_curbuf = curbuf;
Bram Moolenaar86015452020-03-29 15:12:15 +02005069
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005070 curbuf = buf;
5071 STRCPY(bufvarname, "b:");
5072 STRCPY(bufvarname + 2, varname);
5073 set_var(bufvarname, varp, TRUE);
5074 vim_free(bufvarname);
5075 curbuf = save_curbuf;
Bram Moolenaar8d71b542019-08-30 15:46:30 +02005076 }
5077 }
5078}
5079
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005080/*
5081 * Get a callback from "arg". It can be a Funcref or a function name.
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005082 * When "arg" is zero "res.cb_name" is set to an empty string.
5083 * If "res.cb_name" is allocated then "res.cb_free_name" is set to TRUE.
5084 * "res.cb_name" is set to NULL for an invalid argument.
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005085 */
5086 callback_T
5087get_callback(typval_T *arg)
5088{
Bram Moolenaar14e579092020-03-07 16:59:25 +01005089 callback_T res;
5090 int r = OK;
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005091
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005092 CLEAR_FIELD(res);
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005093 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
5094 {
5095 res.cb_partial = arg->vval.v_partial;
5096 ++res.cb_partial->pt_refcount;
5097 res.cb_name = partial_name(res.cb_partial);
5098 }
5099 else
5100 {
Bram Moolenaar14e579092020-03-07 16:59:25 +01005101 if (arg->v_type == VAR_STRING && arg->vval.v_string != NULL
Keith Thompson184f71c2024-01-04 21:19:04 +01005102 && SAFE_isdigit(*arg->vval.v_string))
Bram Moolenaar14e579092020-03-07 16:59:25 +01005103 r = FAIL;
5104 else if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005105 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005106 res.cb_name = arg->vval.v_string;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005107 if (arg->v_type == VAR_STRING)
5108 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005109 char_u *name = get_scriptlocal_funcname(arg->vval.v_string);
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005110 if (name != NULL)
5111 {
Bram Moolenaarc96b7f52022-12-02 15:58:38 +00005112 res.cb_name = name;
5113 res.cb_free_name = TRUE;
Yegappan Lakshmanane7f4abd2021-12-24 20:47:38 +00005114 }
5115 }
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005116 func_ref(res.cb_name);
5117 }
5118 else if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005119 res.cb_name = (char_u *)"";
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005120 else
Bram Moolenaar14e579092020-03-07 16:59:25 +01005121 r = FAIL;
5122
5123 if (r == FAIL)
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005124 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00005125 emsg(_(e_invalid_callback_argument));
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005126 res.cb_name = NULL;
5127 }
5128 }
5129 return res;
5130}
5131
5132/*
5133 * Copy a callback into a typval_T.
5134 */
5135 void
5136put_callback(callback_T *cb, typval_T *tv)
5137{
5138 if (cb->cb_partial != NULL)
5139 {
5140 tv->v_type = VAR_PARTIAL;
5141 tv->vval.v_partial = cb->cb_partial;
5142 ++tv->vval.v_partial->pt_refcount;
5143 }
5144 else
5145 {
5146 tv->v_type = VAR_FUNC;
5147 tv->vval.v_string = vim_strsave(cb->cb_name);
5148 func_ref(cb->cb_name);
5149 }
5150}
5151
5152/*
5153 * Make a copy of "src" into "dest", allocating the function name if needed,
5154 * without incrementing the refcount.
5155 */
5156 void
5157set_callback(callback_T *dest, callback_T *src)
5158{
5159 if (src->cb_partial == NULL)
5160 {
5161 // just a function name, make a copy
5162 dest->cb_name = vim_strsave(src->cb_name);
5163 dest->cb_free_name = TRUE;
5164 }
5165 else
5166 {
5167 // cb_name is a pointer into cb_partial
5168 dest->cb_name = src->cb_name;
5169 dest->cb_free_name = FALSE;
5170 }
5171 dest->cb_partial = src->cb_partial;
5172}
5173
5174/*
Bram Moolenaard43906d2020-07-20 21:31:32 +02005175 * Copy callback from "src" to "dest", incrementing the refcounts.
5176 */
5177 void
5178copy_callback(callback_T *dest, callback_T *src)
5179{
5180 dest->cb_partial = src->cb_partial;
5181 if (dest->cb_partial != NULL)
5182 {
5183 dest->cb_name = src->cb_name;
5184 dest->cb_free_name = FALSE;
5185 ++dest->cb_partial->pt_refcount;
5186 }
5187 else
5188 {
5189 dest->cb_name = vim_strsave(src->cb_name);
5190 dest->cb_free_name = TRUE;
5191 func_ref(src->cb_name);
5192 }
5193}
5194
5195/*
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005196 * When a callback refers to an autoload import, change the function name to
5197 * the "path#name" form. Uses the current script context.
5198 * Only works when the name is allocated.
5199 */
5200 void
5201expand_autload_callback(callback_T *cb)
5202{
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005203 char_u *name;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005204 char_u *p;
5205 imported_T *import;
5206
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005207 if (!in_vim9script() || cb->cb_name == NULL
5208 || (!cb->cb_free_name
5209 && (cb->cb_partial == NULL || cb->cb_partial->pt_name == NULL)))
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005210 return;
Bram Moolenaar3e93a2b2022-01-24 21:28:01 +00005211 if (cb->cb_partial != NULL)
5212 name = cb->cb_partial->pt_name;
5213 else
5214 name = cb->cb_name;
5215 p = vim_strchr(name, '.');
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005216 if (p == NULL)
5217 return;
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005218
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00005219 import = find_imported(name, p - name, FALSE);
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005220 if (import == NULL || !SCRIPT_ID_VALID(import->imp_sid))
5221 return;
5222
5223 scriptitem_T *si = SCRIPT_ITEM(import->imp_sid);
5224 if (si->sn_autoload_prefix == NULL)
5225 return;
5226
5227 char_u *newname = concat_str(si->sn_autoload_prefix, p + 1);
5228 if (newname == NULL)
5229 return;
5230
5231 if (cb->cb_partial != NULL)
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005232 {
Yegappan Lakshmanandc4daa32023-01-02 16:54:53 +00005233 if (cb->cb_name == cb->cb_partial->pt_name)
5234 cb->cb_name = newname;
5235 vim_free(cb->cb_partial->pt_name);
5236 cb->cb_partial->pt_name = newname;
5237 }
5238 else
5239 {
5240 vim_free(cb->cb_name);
5241 cb->cb_name = newname;
Bram Moolenaarf0e7e632022-01-21 13:29:56 +00005242 }
5243}
5244
5245/*
Bram Moolenaaraf7645d2019-09-05 22:33:28 +02005246 * Unref/free "callback" returned by get_callback() or set_callback().
5247 */
5248 void
5249free_callback(callback_T *callback)
5250{
5251 if (callback->cb_partial != NULL)
5252 {
5253 partial_unref(callback->cb_partial);
5254 callback->cb_partial = NULL;
5255 }
5256 else if (callback->cb_name != NULL)
5257 func_unref(callback->cb_name);
5258 if (callback->cb_free_name)
5259 {
5260 vim_free(callback->cb_name);
5261 callback->cb_free_name = FALSE;
5262 }
5263 callback->cb_name = NULL;
5264}
5265
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005266#endif // FEAT_EVAL