blob: 260ce96f12cdd1517f756824b3f4a5643c7191a2 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar314f11d2010-08-09 22:07:08 +020019#ifdef VMS
20# include <float.h>
21#endif
22
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020024#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020025static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020026#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000027
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010028#define NAMESPACE_CHAR (char_u *)"abglstvw"
29
Bram Moolenaar532c7802005-01-27 14:44:31 +000030/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000031 * When recursively copying lists and dicts we need to remember which ones we
32 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000033 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000034 */
35static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020036
Bram Moolenaar071d4272004-06-13 20:20:40 +000037/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000038 * Info used by a ":for" loop.
39 */
Bram Moolenaar33570922005-01-25 22:26:29 +000040typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000041{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010042 int fi_semicolon; // TRUE if ending in '; var]'
43 int fi_varcount; // nr of variables in the list
44 listwatch_T fi_lw; // keep an eye on the item used.
45 list_T *fi_list; // list being used
46 int fi_bi; // index of blob
47 blob_T *fi_blob; // blob being used
Bram Moolenaar33570922005-01-25 22:26:29 +000048} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000049
Bram Moolenaar48e697e2016-01-23 22:17:30 +010050static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010051static int eval2(char_u **arg, typval_T *rettv, int evaluate);
52static int eval3(char_u **arg, typval_T *rettv, int evaluate);
53static int eval4(char_u **arg, typval_T *rettv, int evaluate);
54static int eval5(char_u **arg, typval_T *rettv, int evaluate);
55static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
56static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020057static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000058
Bram Moolenaar48e697e2016-01-23 22:17:30 +010059static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020060static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010061static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020062
Bram Moolenaare21c1582019-03-02 11:57:09 +010063/*
64 * Return "n1" divided by "n2", taking care of dividing by zero.
65 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020066 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010067num_divide(varnumber_T n1, varnumber_T n2)
68{
69 varnumber_T result;
70
71 if (n2 == 0) // give an error message?
72 {
73 if (n1 == 0)
74 result = VARNUM_MIN; // similar to NaN
75 else if (n1 < 0)
76 result = -VARNUM_MAX;
77 else
78 result = VARNUM_MAX;
79 }
80 else
81 result = n1 / n2;
82
83 return result;
84}
85
86/*
87 * Return "n1" modulus "n2", taking care of dividing by zero.
88 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020089 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010090num_modulus(varnumber_T n1, varnumber_T n2)
91{
92 // Give an error when n2 is 0?
93 return (n2 == 0) ? 0 : (n1 % n2);
94}
95
Bram Moolenaara1fa8922017-01-12 20:06:33 +010096#if defined(EBCDIC) || defined(PROTO)
97/*
98 * Compare struct fst by function name.
99 */
100 static int
101compare_func_name(const void *s1, const void *s2)
102{
103 struct fst *p1 = (struct fst *)s1;
104 struct fst *p2 = (struct fst *)s2;
105
106 return STRCMP(p1->f_name, p2->f_name);
107}
108
109/*
110 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100111 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100112 * On machines using EBCDIC we have to sort it.
113 */
114 static void
115sortFunctions(void)
116{
117 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
118
119 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
120}
121#endif
122
Bram Moolenaar33570922005-01-25 22:26:29 +0000123/*
124 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000125 */
126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100127eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000128{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200129 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200130 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000131
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200132#ifdef EBCDIC
133 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100134 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200135 */
136 sortFunctions();
137#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000138}
139
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000140#if defined(EXITFREE) || defined(PROTO)
141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100142eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000143{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200144 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100145 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200146 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000147
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200148 // autoloaded script names
149 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000150
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200151 // unreferenced lists and dicts
152 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200153
154 // functions not garbage collected
155 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000156}
157#endif
158
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159/*
160 * Top level evaluation function, returning a boolean.
161 * Sets "error" to TRUE if there was an error.
162 * Return TRUE or FALSE.
163 */
164 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100165eval_to_bool(
166 char_u *arg,
167 int *error,
168 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100169 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170{
Bram Moolenaar33570922005-01-25 22:26:29 +0000171 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200172 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
174 if (skip)
175 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000176 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178 else
179 {
180 *error = FALSE;
181 if (!skip)
182 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100183 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000184 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 }
186 }
187 if (skip)
188 --emsg_skip;
189
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200190 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191}
192
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100193/*
194 * Call eval1() and give an error message if not done at a lower level.
195 */
196 static int
197eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
198{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100199 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200 int ret;
201 int did_emsg_before = did_emsg;
202 int called_emsg_before = called_emsg;
203
204 ret = eval1(arg, rettv, evaluate);
205 if (ret == FAIL)
206 {
207 // Report the invalid expression unless the expression evaluation has
208 // been cancelled due to an aborting error, an interrupt, or an
209 // exception, or we already gave a more specific error.
210 // Also check called_emsg for when using assert_fails().
211 if (!aborting() && did_emsg == did_emsg_before
212 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100213 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100214 }
215 return ret;
216}
217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218/*
219 * Evaluate an expression, which can be a function, partial or string.
220 * Pass arguments "argv[argc]".
221 * Return the result in "rettv" and OK or FAIL.
222 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200223 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100224eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
225{
226 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100227 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200228 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100229
230 if (expr->v_type == VAR_FUNC)
231 {
232 s = expr->vval.v_string;
233 if (s == NULL || *s == NUL)
234 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200235 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200236 funcexe.evaluate = TRUE;
237 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100238 return FAIL;
239 }
240 else if (expr->v_type == VAR_PARTIAL)
241 {
242 partial_T *partial = expr->vval.v_partial;
243
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 if (partial->pt_func != NULL && partial->pt_func->uf_dfunc_idx >= 0)
245 {
246 if (call_def_function(partial->pt_func, argc, argv, rettv) == FAIL)
247 return FAIL;
248 }
249 else
250 {
251 s = partial_name(partial);
252 if (s == NULL || *s == NUL)
253 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200254 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 funcexe.evaluate = TRUE;
256 funcexe.partial = partial;
257 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
258 return FAIL;
259 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100260 }
261 else
262 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100263 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100264 if (s == NULL)
265 return FAIL;
266 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100267 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100268 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100269 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100270 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200271 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100272 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 return FAIL;
274 }
275 }
276 return OK;
277}
278
279/*
280 * Like eval_to_bool() but using a typval_T instead of a string.
281 * Works for string, funcref and partial.
282 */
283 int
284eval_expr_to_bool(typval_T *expr, int *error)
285{
286 typval_T rettv;
287 int res;
288
289 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
290 {
291 *error = TRUE;
292 return FALSE;
293 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100294 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100295 clear_tv(&rettv);
296 return res;
297}
298
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299/*
300 * Top level evaluation function, returning a string. If "skip" is TRUE,
301 * only parsing to "nextcmd" is done, without reporting errors. Return
302 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
303 */
304 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100305eval_to_string_skip(
306 char_u *arg,
307 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100308 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000309{
Bram Moolenaar33570922005-01-25 22:26:29 +0000310 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 char_u *retval;
312
313 if (skip)
314 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000315 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 retval = NULL;
317 else
318 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100319 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000320 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 }
322 if (skip)
323 --emsg_skip;
324
325 return retval;
326}
327
328/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000329 * Skip over an expression at "*pp".
330 * Return FAIL for an error, OK otherwise.
331 */
332 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100333skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000334{
Bram Moolenaar33570922005-01-25 22:26:29 +0000335 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000336
337 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000338 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000339}
340
341/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000343 * When "convert" is TRUE convert a List into a sequence of lines and convert
344 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * Return pointer to allocated memory, or NULL for failure.
346 */
347 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100348eval_to_string(
349 char_u *arg,
350 char_u **nextcmd,
351 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352{
Bram Moolenaar33570922005-01-25 22:26:29 +0000353 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000355 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000356#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000357 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000360 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000361 retval = NULL;
362 else
363 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000364 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000365 {
366 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000367 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200368 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200369 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200370 if (tv.vval.v_list->lv_len > 0)
371 ga_append(&ga, NL);
372 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000373 ga_append(&ga, NUL);
374 retval = (char_u *)ga.ga_data;
375 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000376#ifdef FEAT_FLOAT
377 else if (convert && tv.v_type == VAR_FLOAT)
378 {
379 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
380 retval = vim_strsave(numbuf);
381 }
382#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000383 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100384 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000385 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386 }
387
388 return retval;
389}
390
391/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000392 * Call eval_to_string() without using current local variables and using
393 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394 */
395 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100396eval_to_string_safe(
397 char_u *arg,
398 char_u **nextcmd,
399 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200402 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200404 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000405 if (use_sandbox)
406 ++sandbox;
407 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000408 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000409 if (use_sandbox)
410 --sandbox;
411 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200412 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000413 return retval;
414}
415
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416/*
417 * Top level evaluation function, returning a number.
418 * Evaluates "expr" silently.
419 * Returns -1 for an error.
420 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200421 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100422eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423{
Bram Moolenaar33570922005-01-25 22:26:29 +0000424 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200425 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000426 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
428 ++emsg_off;
429
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000430 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431 retval = -1;
432 else
433 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100434 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000435 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 }
437 --emsg_off;
438
439 return retval;
440}
441
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000442/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000443 * Top level evaluation function.
444 * Returns an allocated typval_T with the result.
445 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000446 */
447 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100448eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000449{
450 typval_T *tv;
451
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200452 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000453 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100454 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000455
456 return tv;
457}
458
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100460 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200461 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
462 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000463 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000464 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100466call_vim_function(
467 char_u *func,
468 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200469 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200470 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000471{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000472 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200473 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100475 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200476 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200477 funcexe.firstline = curwin->w_cursor.lnum;
478 funcexe.lastline = curwin->w_cursor.lnum;
479 funcexe.evaluate = TRUE;
480 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000481 if (ret == FAIL)
482 clear_tv(rettv);
483
484 return ret;
485}
486
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100487/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100488 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100489 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200490 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
491 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100492 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200493 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100494call_func_retnr(
495 char_u *func,
496 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200497 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100498{
499 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200500 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501
Bram Moolenaarded27a12018-08-01 19:06:03 +0200502 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100503 return -1;
504
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100505 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100506 clear_tv(&rettv);
507 return retval;
508}
509
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000510/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100511 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000512 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200513 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
514 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000515 */
516 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100517call_func_retstr(
518 char_u *func,
519 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200520 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000521{
522 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000523 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000524
Bram Moolenaarded27a12018-08-01 19:06:03 +0200525 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000526 return NULL;
527
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100528 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 return retval;
531}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000532
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000533/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100534 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200535 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
536 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000537 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000538 */
539 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100540call_func_retlist(
541 char_u *func,
542 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200543 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000544{
545 typval_T rettv;
546
Bram Moolenaarded27a12018-08-01 19:06:03 +0200547 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000548 return NULL;
549
550 if (rettv.v_type != VAR_LIST)
551 {
552 clear_tv(&rettv);
553 return NULL;
554 }
555
556 return rettv.vval.v_list;
557}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#ifdef FEAT_FOLDING
560/*
561 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
562 * it in "*cp". Doesn't give error messages.
563 */
564 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566{
Bram Moolenaar33570922005-01-25 22:26:29 +0000567 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200568 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000570 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
571 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572
573 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000574 if (use_sandbox)
575 ++sandbox;
576 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000578 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 retval = 0;
580 else
581 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100582 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000583 if (tv.v_type == VAR_NUMBER)
584 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000585 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 retval = 0;
587 else
588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100589 // If the result is a string, check if there is a non-digit before
590 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000591 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000592 if (!VIM_ISDIGIT(*s) && *s != '-')
593 *cp = *s++;
594 retval = atol((char *)s);
595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000596 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 }
598 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000599 if (use_sandbox)
600 --sandbox;
601 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200603 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604}
605#endif
606
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000608 * Get an lval: variable, Dict item or List item that can be assigned a value
609 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
610 * "name.key", "name.key[expr]" etc.
611 * Indexing only works if "name" is an existing List or Dictionary.
612 * "name" points to the start of the name.
613 * If "rettv" is not NULL it points to the value to be assigned.
614 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
615 * wrong; must end in space or cmd separator.
616 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100617 * flags:
618 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100619 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100620 * GLV_NO_AUTOLOAD: do not use script autoloading
621 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000622 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000623 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000624 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000625 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200626 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100627get_lval(
628 char_u *name,
629 typval_T *rettv,
630 lval_T *lp,
631 int unlet,
632 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100633 int flags, // GLV_ values
634 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000636 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000637 char_u *expr_start, *expr_end;
638 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000639 dictitem_T *v;
640 typval_T var1;
641 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000642 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000643 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000644 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000645 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000646 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100647 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000648
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100649 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200650 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000651
652 if (skip)
653 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100654 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000655 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000656 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000657 }
658
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100659 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000660 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000662 if (expr_start != NULL)
663 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100664 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100665 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000666 && *p != '[' && *p != '.')
667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100668 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000669 return NULL;
670 }
671
672 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
673 if (lp->ll_exp_name == NULL)
674 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100675 // Report an invalid expression in braces, unless the
676 // expression evaluation has been cancelled due to an
677 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000678 if (!aborting() && !quiet)
679 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000680 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100681 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000682 return NULL;
683 }
684 }
685 lp->ll_name = lp->ll_exp_name;
686 }
687 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000689 lp->ll_name = name;
690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
692 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100693 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 char_u *tp = skipwhite(p + 1);
695
696 // parse the type after the name
697 lp->ll_type = parse_type(&tp, &si->sn_type_list);
698 lp->ll_name_end = tp;
699 }
700 }
701
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100702 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000703 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
704 return p;
705
706 cc = *p;
707 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100708 // Only pass &ht when we would write to the variable, it prevents autoload
709 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100710 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
711 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000712 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100713 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000714 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000715 if (v == NULL)
716 return NULL;
717
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000718 /*
719 * Loop until no more [idx] or .key is following.
720 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000721 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100722 var1.v_type = VAR_UNKNOWN;
723 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000724 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000726 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
727 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100728 && lp->ll_tv->vval.v_dict != NULL)
729 && !(lp->ll_tv->v_type == VAR_BLOB
730 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000732 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100733 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000734 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000735 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000736 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000737 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000738 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100739 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000740 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000741 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000742
Bram Moolenaar8c711452005-01-14 21:53:12 +0000743 len = -1;
744 if (*p == '.')
745 {
746 key = p + 1;
747 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
748 ;
749 if (len == 0)
750 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000751 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100752 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000753 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000754 }
755 p = key + len;
756 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000757 else
758 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100759 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000760 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000761 if (*p == ':')
762 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000763 else
764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000765 empty1 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100766 if (eval1(&p, &var1, TRUE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000767 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100768 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000769 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100770 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000771 clear_tv(&var1);
772 return NULL;
773 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000774 }
775
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100776 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000777 if (*p == ':')
778 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000779 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000780 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000781 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100782 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100783 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000785 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100786 if (rettv != NULL
787 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100788 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100789 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100790 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000791 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000792 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100793 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100794 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000796 }
797 p = skipwhite(p + 1);
798 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000799 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000800 else
801 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 lp->ll_empty2 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100803 if (eval1(&p, &var2, TRUE) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +0000804 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100805 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000806 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000807 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100808 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000809 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100810 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100811 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000812 clear_tv(&var2);
813 return NULL;
814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000815 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000816 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000818 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000820
Bram Moolenaar8c711452005-01-14 21:53:12 +0000821 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000823 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100825 clear_tv(&var1);
826 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000827 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000828 }
829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100830 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 ++p;
832 }
833
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000834 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000835 {
836 if (len == -1)
837 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100838 // "[key]": get key from "var1"
839 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200840 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000842 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000843 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000844 }
845 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000846 lp->ll_list = NULL;
847 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000848 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200849
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100850 // When assigning to a scope dictionary check that a function and
851 // variable name is valid (only variable name unless it is l: or
852 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200853 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200854 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200855 int prevval;
856 int wrong;
857
858 if (len != -1)
859 {
860 prevval = key[len];
861 key[len] = NUL;
862 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200863 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100864 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200865 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
866 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200867 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200868 || !valid_varname(key);
869 if (len != -1)
870 key[len] = prevval;
871 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200872 {
873 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200874 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200875 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200876 }
877
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000878 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000879 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100880 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200881 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100882 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200883 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100884 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100885 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200886 return NULL;
887 }
888
Bram Moolenaar31b81602019-02-10 22:14:27 +0100889 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000890 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000892 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100893 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100894 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000896 }
897 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000899 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000900 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100901 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000902 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000903 p = NULL;
904 break;
905 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100906 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100907 else if ((flags & GLV_READ_ONLY) == 0
908 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100909 {
910 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200911 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100912 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200913
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100914 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000916 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100917 else if (lp->ll_tv->v_type == VAR_BLOB)
918 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100919 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
920
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100921 /*
922 * Get the number and item for the only or first index of the List.
923 */
924 if (empty1)
925 lp->ll_n1 = 0;
926 else
927 // is number or string
928 lp->ll_n1 = (long)tv_get_number(&var1);
929 clear_tv(&var1);
930
931 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100932 || lp->ll_n1 > bloblen
933 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100934 {
935 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100936 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100937 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100938 return NULL;
939 }
940 if (lp->ll_range && !lp->ll_empty2)
941 {
942 lp->ll_n2 = (long)tv_get_number(&var2);
943 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100944 if (lp->ll_n2 < 0
945 || lp->ll_n2 >= bloblen
946 || lp->ll_n2 < lp->ll_n1)
947 {
948 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100949 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100950 return NULL;
951 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100952 }
953 lp->ll_blob = lp->ll_tv->vval.v_blob;
954 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100955 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100956 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000957 else
958 {
959 /*
960 * Get the number and item for the only or first index of the List.
961 */
962 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000964 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100965 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100966 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100967 clear_tv(&var1);
968
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000969 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000970 lp->ll_list = lp->ll_tv->vval.v_list;
971 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
972 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000973 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000974 if (lp->ll_n1 < 0)
975 {
976 lp->ll_n1 = 0;
977 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
978 }
979 }
980 if (lp->ll_li == NULL)
981 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100982 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200983 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100984 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000985 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000986 }
987
988 /*
989 * May need to find the item or absolute index for the second
990 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000991 * When no index given: "lp->ll_empty2" is TRUE.
992 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000993 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000995 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100996 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100997 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000999 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001000 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001001 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001002 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001003 {
1004 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001005 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001009 }
1010
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001011 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 if (lp->ll_n1 < 0)
1013 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1014 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001015 {
1016 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001017 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001019 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001020 }
1021
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001022 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001023 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001024 }
1025
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001026 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 return p;
1029}
1030
1031/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001032 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001035clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036{
1037 vim_free(lp->ll_exp_name);
1038 vim_free(lp->ll_newkey);
1039}
1040
1041/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001042 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001043 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001044 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1045 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001048set_var_lval(
1049 lval_T *lp,
1050 char_u *endp,
1051 typval_T *rettv,
1052 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001054 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055{
1056 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001057 listitem_T *ri;
1058 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059
1060 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001061 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001062 cc = *endp;
1063 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001064 if (lp->ll_blob != NULL)
1065 {
1066 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001067
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001068 if (op != NULL && *op != '=')
1069 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001070 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 return;
1072 }
1073
1074 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1075 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001076 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001077
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001078 if (lp->ll_empty2)
1079 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1080
1081 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001083 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001084 return;
1085 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001086 if (lp->ll_empty2)
1087 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001088
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001089 ir = 0;
1090 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1091 blob_set(lp->ll_blob, il,
1092 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001093 }
1094 else
1095 {
1096 val = (int)tv_get_number_chk(rettv, &error);
1097 if (!error)
1098 {
1099 garray_T *gap = &lp->ll_blob->bv_ga;
1100
1101 // Allow for appending a byte. Setting a byte beyond
1102 // the end is an error otherwise.
1103 if (lp->ll_n1 < gap->ga_len
1104 || (lp->ll_n1 == gap->ga_len
1105 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1106 {
1107 blob_set(lp->ll_blob, lp->ll_n1, val);
1108 if (lp->ll_n1 == gap->ga_len)
1109 ++gap->ga_len;
1110 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001111 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001112 }
1113 }
1114 }
1115 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001116 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001117 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001118
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001120 {
1121 emsg(_(e_cannot_mod));
1122 *endp = cc;
1123 return;
1124 }
1125
Bram Moolenaarff697e62019-02-12 22:28:33 +01001126 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001127 di = NULL;
1128 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1129 &tv, &di, TRUE, FALSE) == OK)
1130 {
1131 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001132 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1133 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001134 && tv_op(&tv, rettv, op) == OK)
1135 set_var(lp->ll_name, &tv, FALSE);
1136 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001137 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001138 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001139 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001141 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001142 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001143 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001144 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001145 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001146 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001147 else if (lp->ll_range)
1148 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001149 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001150 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001151
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001153 {
1154 emsg(_("E996: Cannot lock a range"));
1155 return;
1156 }
1157
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001158 /*
1159 * Check whether any of the list items is locked
1160 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001161 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001162 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001163 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001164 return;
1165 ri = ri->li_next;
1166 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1167 break;
1168 ll_li = ll_li->li_next;
1169 ++ll_n1;
1170 }
1171
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001172 /*
1173 * Assign the List values to the list items.
1174 */
1175 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001176 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001177 if (op != NULL && *op != '=')
1178 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1179 else
1180 {
1181 clear_tv(&lp->ll_li->li_tv);
1182 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1183 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001184 ri = ri->li_next;
1185 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1186 break;
1187 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001188 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001190 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001191 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001192 ri = NULL;
1193 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001194 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001195 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001196 lp->ll_li = lp->ll_li->li_next;
1197 ++lp->ll_n1;
1198 }
1199 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001200 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001201 else if (lp->ll_empty2
1202 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001203 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001204 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 }
1206 else
1207 {
1208 /*
1209 * Assign to a List or Dictionary item.
1210 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001212 {
1213 emsg(_("E996: Cannot lock a list or dict"));
1214 return;
1215 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001216 if (lp->ll_newkey != NULL)
1217 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001218 if (op != NULL && *op != '=')
1219 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001220 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001221 return;
1222 }
1223
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001224 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001225 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001226 if (di == NULL)
1227 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001228 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1229 {
1230 vim_free(di);
1231 return;
1232 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001233 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001234 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001235 else if (op != NULL && *op != '=')
1236 {
1237 tv_op(lp->ll_tv, rettv, op);
1238 return;
1239 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001240 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001241 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001242
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001243 /*
1244 * Assign the value to the variable or list item.
1245 */
1246 if (copy)
1247 copy_tv(rettv, lp->ll_tv);
1248 else
1249 {
1250 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001251 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001253 }
1254 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001255}
1256
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001257/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001258 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1259 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001260 * Returns OK or FAIL.
1261 */
1262 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001263tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001264{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001265 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001266 char_u numbuf[NUMBUFLEN];
1267 char_u *s;
1268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001269 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001270 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001271 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001272 {
1273 switch (tv1->v_type)
1274 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001275 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001276 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001278 case VAR_DICT:
1279 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001280 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001281 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001282 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001283 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001284 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001285 break;
1286
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001287 case VAR_BLOB:
1288 if (*op != '+' || tv2->v_type != VAR_BLOB)
1289 break;
1290 // BLOB += BLOB
1291 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1292 {
1293 blob_T *b1 = tv1->vval.v_blob;
1294 blob_T *b2 = tv2->vval.v_blob;
1295 int i, len = blob_len(b2);
1296 for (i = 0; i < len; i++)
1297 ga_append(&b1->bv_ga, blob_get(b2, i));
1298 }
1299 return OK;
1300
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001301 case VAR_LIST:
1302 if (*op != '+' || tv2->v_type != VAR_LIST)
1303 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001304 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001305 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1306 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1307 return OK;
1308
1309 case VAR_NUMBER:
1310 case VAR_STRING:
1311 if (tv2->v_type == VAR_LIST)
1312 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001313 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001314 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001315 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001316 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001317#ifdef FEAT_FLOAT
1318 if (tv2->v_type == VAR_FLOAT)
1319 {
1320 float_T f = n;
1321
Bram Moolenaarff697e62019-02-12 22:28:33 +01001322 if (*op == '%')
1323 break;
1324 switch (*op)
1325 {
1326 case '+': f += tv2->vval.v_float; break;
1327 case '-': f -= tv2->vval.v_float; break;
1328 case '*': f *= tv2->vval.v_float; break;
1329 case '/': f /= tv2->vval.v_float; break;
1330 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001331 clear_tv(tv1);
1332 tv1->v_type = VAR_FLOAT;
1333 tv1->vval.v_float = f;
1334 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001335 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001336#endif
1337 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001338 switch (*op)
1339 {
1340 case '+': n += tv_get_number(tv2); break;
1341 case '-': n -= tv_get_number(tv2); break;
1342 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001343 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1344 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001345 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001346 clear_tv(tv1);
1347 tv1->v_type = VAR_NUMBER;
1348 tv1->vval.v_number = n;
1349 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001350 }
1351 else
1352 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001353 if (tv2->v_type == VAR_FLOAT)
1354 break;
1355
Bram Moolenaarff697e62019-02-12 22:28:33 +01001356 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001357 s = tv_get_string(tv1);
1358 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001359 clear_tv(tv1);
1360 tv1->v_type = VAR_STRING;
1361 tv1->vval.v_string = s;
1362 }
1363 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001364
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001365 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001366#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001367 {
1368 float_T f;
1369
Bram Moolenaarff697e62019-02-12 22:28:33 +01001370 if (*op == '%' || *op == '.'
1371 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001372 && tv2->v_type != VAR_NUMBER
1373 && tv2->v_type != VAR_STRING))
1374 break;
1375 if (tv2->v_type == VAR_FLOAT)
1376 f = tv2->vval.v_float;
1377 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001378 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001379 switch (*op)
1380 {
1381 case '+': tv1->vval.v_float += f; break;
1382 case '-': tv1->vval.v_float -= f; break;
1383 case '*': tv1->vval.v_float *= f; break;
1384 case '/': tv1->vval.v_float /= f; break;
1385 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001386 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001387#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001388 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001389 }
1390 }
1391
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001392 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001393 return FAIL;
1394}
1395
1396/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001397 * Evaluate the expression used in a ":for var in expr" command.
1398 * "arg" points to "var".
1399 * Set "*errp" to TRUE for an error, FALSE otherwise;
1400 * Return a pointer that holds the info. Null when there is an error.
1401 */
1402 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001403eval_for_line(
1404 char_u *arg,
1405 int *errp,
1406 char_u **nextcmdp,
1407 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001410 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001411 typval_T tv;
1412 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001414 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001415
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001416 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001417 if (fi == NULL)
1418 return NULL;
1419
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001421 if (expr == NULL)
1422 return fi;
1423
1424 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001425 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001426 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001428 return fi;
1429 }
1430
1431 if (skip)
1432 ++emsg_skip;
1433 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1434 {
1435 *errp = FALSE;
1436 if (!skip)
1437 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001438 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001439 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001440 l = tv.vval.v_list;
1441 if (l == NULL)
1442 {
1443 // a null list is like an empty list: do nothing
1444 clear_tv(&tv);
1445 }
1446 else
1447 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 // Need a real list here.
1449 range_list_materialize(l);
1450
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001451 // No need to increment the refcount, it's already set for
1452 // the list being used in "tv".
1453 fi->fi_list = l;
1454 list_add_watch(l, &fi->fi_lw);
1455 fi->fi_lw.lw_item = l->lv_first;
1456 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001457 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001458 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001459 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001460 fi->fi_bi = 0;
1461 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001462 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001463 typval_T btv;
1464
1465 // Make a copy, so that the iteration still works when the
1466 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001468 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001469 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001470 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001471 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001472 else
1473 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001474 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001475 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001476 }
1477 }
1478 }
1479 if (skip)
1480 --emsg_skip;
1481
1482 return fi;
1483}
1484
1485/*
1486 * Use the first item in a ":for" list. Advance to the next.
1487 * Assign the values to the variable (list). "arg" points to the first one.
1488 * Return TRUE when a valid item was found, FALSE when at end of list or
1489 * something wrong.
1490 */
1491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001493{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001494 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001495 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001496 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1497 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001498 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001499
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001500 if (fi->fi_blob != NULL)
1501 {
1502 typval_T tv;
1503
1504 if (fi->fi_bi >= blob_len(fi->fi_blob))
1505 return FALSE;
1506 tv.v_type = VAR_NUMBER;
1507 tv.v_lock = VAR_FIXED;
1508 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1509 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001510 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001511 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001512 }
1513
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001514 item = fi->fi_lw.lw_item;
1515 if (item == NULL)
1516 result = FALSE;
1517 else
1518 {
1519 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001520 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001521 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001522 }
1523 return result;
1524}
1525
1526/*
1527 * Free the structure used to store info used by ":for".
1528 */
1529 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001530free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531{
Bram Moolenaar33570922005-01-25 22:26:29 +00001532 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001533
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001534 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001535 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001537 list_unref(fi->fi_list);
1538 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001539 if (fi != NULL && fi->fi_blob != NULL)
1540 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001541 vim_free(fi);
1542}
1543
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001545set_context_for_expression(
1546 expand_T *xp,
1547 char_u *arg,
1548 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549{
1550 int got_eq = FALSE;
1551 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001552 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001554 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 {
1556 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001557 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001559 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001560 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 {
1562 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001563 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001564 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001565 break;
1566 }
1567 return;
1568 }
1569 }
1570 else
1571 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1572 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 while ((xp->xp_pattern = vim_strpbrk(arg,
1574 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1575 {
1576 c = *xp->xp_pattern;
1577 if (c == '&')
1578 {
1579 c = xp->xp_pattern[1];
1580 if (c == '&')
1581 {
1582 ++xp->xp_pattern;
1583 xp->xp_context = cmdidx != CMD_let || got_eq
1584 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1585 }
1586 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001587 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001589 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1590 xp->xp_pattern += 2;
1591
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593 }
1594 else if (c == '$')
1595 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001596 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 xp->xp_context = EXPAND_ENV_VARS;
1598 }
1599 else if (c == '=')
1600 {
1601 got_eq = TRUE;
1602 xp->xp_context = EXPAND_EXPRESSION;
1603 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001604 else if (c == '#'
1605 && xp->xp_context == EXPAND_EXPRESSION)
1606 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001607 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001608 break;
1609 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001610 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 && xp->xp_context == EXPAND_FUNCTIONS
1612 && vim_strchr(xp->xp_pattern, '(') == NULL)
1613 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001614 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 break;
1616 }
1617 else if (cmdidx != CMD_let || got_eq)
1618 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001619 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 {
1621 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1622 if (c == '\\' && xp->xp_pattern[1] != NUL)
1623 ++xp->xp_pattern;
1624 xp->xp_context = EXPAND_NOTHING;
1625 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001626 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001628 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1630 /* skip */ ;
1631 xp->xp_context = EXPAND_NOTHING;
1632 }
1633 else if (c == '|')
1634 {
1635 if (xp->xp_pattern[1] == '|')
1636 {
1637 ++xp->xp_pattern;
1638 xp->xp_context = EXPAND_EXPRESSION;
1639 }
1640 else
1641 xp->xp_context = EXPAND_COMMANDS;
1642 }
1643 else
1644 xp->xp_context = EXPAND_EXPRESSION;
1645 }
1646 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001647 // Doesn't look like something valid, expand as an expression
1648 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001649 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 arg = xp->xp_pattern;
1651 if (*arg != NUL)
1652 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1653 /* skip */ ;
1654 }
1655 xp->xp_pattern = arg;
1656}
1657
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001659 * Return TRUE if "pat" matches "text".
1660 * Does not use 'cpo' and always uses 'magic'.
1661 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001662 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001663pattern_match(char_u *pat, char_u *text, int ic)
1664{
1665 int matches = FALSE;
1666 char_u *save_cpo;
1667 regmatch_T regmatch;
1668
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001669 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001670 save_cpo = p_cpo;
1671 p_cpo = (char_u *)"";
1672 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1673 if (regmatch.regprog != NULL)
1674 {
1675 regmatch.rm_ic = ic;
1676 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1677 vim_regfree(regmatch.regprog);
1678 }
1679 p_cpo = save_cpo;
1680 return matches;
1681}
1682
1683/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001684 * Handle a name followed by "(". Both for just "name(arg)" and for
1685 * "expr->name(arg)".
1686 * Returns OK or FAIL.
1687 */
1688 static int
1689eval_func(
1690 char_u **arg, // points to "(", will be advanced
1691 char_u *name,
1692 int name_len,
1693 typval_T *rettv,
1694 int evaluate,
1695 typval_T *basetv) // "expr" for "expr->name(arg)"
1696{
1697 char_u *s = name;
1698 int len = name_len;
1699 partial_T *partial;
1700 int ret = OK;
1701
1702 if (!evaluate)
1703 check_vars(s, len);
1704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001705 // If "s" is the name of a variable of type VAR_FUNC
1706 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001707 s = deref_func_name(s, &len, &partial, !evaluate);
1708
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001709 // Need to make a copy, in case evaluating the arguments makes
1710 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001711 s = vim_strsave(s);
1712 if (s == NULL)
1713 ret = FAIL;
1714 else
1715 {
1716 funcexe_T funcexe;
1717
1718 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001719 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001720 funcexe.firstline = curwin->w_cursor.lnum;
1721 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001722 funcexe.evaluate = evaluate;
1723 funcexe.partial = partial;
1724 funcexe.basetv = basetv;
1725 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1726 }
1727 vim_free(s);
1728
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001729 // If evaluate is FALSE rettv->v_type was not set in
1730 // get_func_tv, but it's needed in handle_subscript() to parse
1731 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001732 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1733 {
1734 rettv->vval.v_string = NULL;
1735 rettv->v_type = VAR_FUNC;
1736 }
1737
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001738 // Stop the expression evaluation when immediately
1739 // aborting on error, or when an interrupt occurred or
1740 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001741 if (evaluate && aborting())
1742 {
1743 if (ret == OK)
1744 clear_tv(rettv);
1745 ret = FAIL;
1746 }
1747 return ret;
1748}
1749
1750/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001752 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1754 */
1755
1756/*
1757 * Handle zero level expression.
1758 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001759 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001760 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 * Return OK or FAIL.
1762 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001764eval0(
1765 char_u *arg,
1766 typval_T *rettv,
1767 char_u **nextcmd,
1768 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769{
1770 int ret;
1771 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001772 int did_emsg_before = did_emsg;
1773 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001776 ret = eval1(&p, rettv, evaluate);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001777 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
1779 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001780 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 /*
1782 * Report the invalid expression unless the expression evaluation has
1783 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001784 * exception, or we already gave a more specific error.
1785 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001787 if (!aborting() && did_emsg == did_emsg_before
1788 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001789 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 ret = FAIL;
1791 }
1792 if (nextcmd != NULL)
1793 *nextcmd = check_nextcmd(p);
1794
1795 return ret;
1796}
1797
1798/*
1799 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001800 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 *
1802 * "arg" must point to the first non-white of the expression.
1803 * "arg" is advanced to the next non-white after the recognized expression.
1804 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001805 * Note: "rettv.v_lock" is not set.
1806 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 * Return OK or FAIL.
1808 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001809 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811{
1812 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001813 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814
1815 /*
1816 * Get the first variable.
1817 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001818 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819 return FAIL;
1820
1821 if ((*arg)[0] == '?')
1822 {
1823 result = FALSE;
1824 if (evaluate)
1825 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001826 int error = FALSE;
1827
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001828 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001831 if (error)
1832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833 }
1834
1835 /*
1836 * Get the second variable.
1837 */
1838 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001839 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 return FAIL;
1841
1842 /*
1843 * Check for the ":".
1844 */
1845 if ((*arg)[0] != ':')
1846 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001849 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 return FAIL;
1851 }
1852
1853 /*
1854 * Get the third variable.
1855 */
1856 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001857 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
1859 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 return FAIL;
1862 }
1863 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 }
1866
1867 return OK;
1868}
1869
1870/*
1871 * Handle first level expression:
1872 * expr2 || expr2 || expr2 logical OR
1873 *
1874 * "arg" must point to the first non-white of the expression.
1875 * "arg" is advanced to the next non-white after the recognized expression.
1876 *
1877 * Return OK or FAIL.
1878 */
1879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001880eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881{
Bram Moolenaar33570922005-01-25 22:26:29 +00001882 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 long result;
1884 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886
1887 /*
1888 * Get the first variable.
1889 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 return FAIL;
1892
1893 /*
1894 * Repeat until there is no following "||".
1895 */
1896 first = TRUE;
1897 result = FALSE;
1898 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1899 {
1900 if (evaluate && first)
1901 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001902 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001905 if (error)
1906 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 first = FALSE;
1908 }
1909
1910 /*
1911 * Get the second variable.
1912 */
1913 *arg = skipwhite(*arg + 2);
1914 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1915 return FAIL;
1916
1917 /*
1918 * Compute the result.
1919 */
1920 if (evaluate && !result)
1921 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001922 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001925 if (error)
1926 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 }
1928 if (evaluate)
1929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 rettv->v_type = VAR_NUMBER;
1931 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 }
1933 }
1934
1935 return OK;
1936}
1937
1938/*
1939 * Handle second level expression:
1940 * expr3 && expr3 && expr3 logical AND
1941 *
1942 * "arg" must point to the first non-white of the expression.
1943 * "arg" is advanced to the next non-white after the recognized expression.
1944 *
1945 * Return OK or FAIL.
1946 */
1947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001948eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949{
Bram Moolenaar33570922005-01-25 22:26:29 +00001950 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 long result;
1952 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001953 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954
1955 /*
1956 * Get the first variable.
1957 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 return FAIL;
1960
1961 /*
1962 * Repeat until there is no following "&&".
1963 */
1964 first = TRUE;
1965 result = TRUE;
1966 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1967 {
1968 if (evaluate && first)
1969 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001970 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001972 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001973 if (error)
1974 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 first = FALSE;
1976 }
1977
1978 /*
1979 * Get the second variable.
1980 */
1981 *arg = skipwhite(*arg + 2);
1982 if (eval4(arg, &var2, evaluate && result) == FAIL)
1983 return FAIL;
1984
1985 /*
1986 * Compute the result.
1987 */
1988 if (evaluate && result)
1989 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001990 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001992 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001993 if (error)
1994 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 }
1996 if (evaluate)
1997 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001998 rettv->v_type = VAR_NUMBER;
1999 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 }
2001 }
2002
2003 return OK;
2004}
2005
2006/*
2007 * Handle third level expression:
2008 * var1 == var2
2009 * var1 =~ var2
2010 * var1 != var2
2011 * var1 !~ var2
2012 * var1 > var2
2013 * var1 >= var2
2014 * var1 < var2
2015 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002016 * var1 is var2
2017 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 *
2019 * "arg" must point to the first non-white of the expression.
2020 * "arg" is advanced to the next non-white after the recognized expression.
2021 *
2022 * Return OK or FAIL.
2023 */
2024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002025eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
Bram Moolenaar33570922005-01-25 22:26:29 +00002027 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 char_u *p;
2029 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002030 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033
2034 /*
2035 * Get the first variable.
2036 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002037 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 return FAIL;
2039
2040 p = *arg;
2041 switch (p[0])
2042 {
2043 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002044 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002046 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 break;
2048 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002049 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002051 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 break;
2053 case '>': if (p[1] != '=')
2054 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002055 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 len = 1;
2057 }
2058 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002059 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 break;
2061 case '<': if (p[1] != '=')
2062 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002063 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 len = 1;
2065 }
2066 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002067 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002069 case 'i': if (p[1] == 's')
2070 {
2071 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2072 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002073 i = p[len];
2074 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002075 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002076 }
2077 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 }
2079
2080 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002081 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002083 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002085 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 if (p[len] == '?')
2087 {
2088 ic = TRUE;
2089 ++len;
2090 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002091 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 else if (p[len] == '#')
2093 {
2094 ic = FALSE;
2095 ++len;
2096 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002097 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 else
2099 ic = p_ic;
2100
2101 /*
2102 * Get the second variable.
2103 */
2104 *arg = skipwhite(p + len);
2105 if (eval5(arg, &var2, evaluate) == FAIL)
2106 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002107 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 return FAIL;
2109 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002110 if (evaluate)
2111 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002112 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002113
2114 clear_tv(&var2);
2115 return ret;
2116 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 }
2118
2119 return OK;
2120}
2121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 void
2123eval_addblob(typval_T *tv1, typval_T *tv2)
2124{
2125 blob_T *b1 = tv1->vval.v_blob;
2126 blob_T *b2 = tv2->vval.v_blob;
2127 blob_T *b = blob_alloc();
2128 int i;
2129
2130 if (b != NULL)
2131 {
2132 for (i = 0; i < blob_len(b1); i++)
2133 ga_append(&b->bv_ga, blob_get(b1, i));
2134 for (i = 0; i < blob_len(b2); i++)
2135 ga_append(&b->bv_ga, blob_get(b2, i));
2136
2137 clear_tv(tv1);
2138 rettv_blob_set(tv1, b);
2139 }
2140}
2141
2142 int
2143eval_addlist(typval_T *tv1, typval_T *tv2)
2144{
2145 typval_T var3;
2146
2147 // concatenate Lists
2148 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2149 {
2150 clear_tv(tv1);
2151 clear_tv(tv2);
2152 return FAIL;
2153 }
2154 clear_tv(tv1);
2155 *tv1 = var3;
2156 return OK;
2157}
2158
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159/*
2160 * Handle fourth level expression:
2161 * + number addition
2162 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002163 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002164 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 *
2166 * "arg" must point to the first non-white of the expression.
2167 * "arg" is advanced to the next non-white after the recognized expression.
2168 *
2169 * Return OK or FAIL.
2170 */
2171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002172eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173{
Bram Moolenaar33570922005-01-25 22:26:29 +00002174 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002176 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002177#ifdef FEAT_FLOAT
2178 float_T f1 = 0, f2 = 0;
2179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 char_u *s1, *s2;
2181 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2182 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002183 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 /*
2186 * Get the first variable.
2187 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002188 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 return FAIL;
2190
2191 /*
2192 * Repeat computing, until no '+', '-' or '.' is following.
2193 */
2194 for (;;)
2195 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002196 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002198 concat = op == '.'
2199 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2200 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 break;
2202
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002203 if ((op != '+' || (rettv->v_type != VAR_LIST
2204 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002205#ifdef FEAT_FLOAT
2206 && (op == '.' || rettv->v_type != VAR_FLOAT)
2207#endif
2208 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002210 // For "list + ...", an illegal use of the first operand as
2211 // a number cannot be determined before evaluating the 2nd
2212 // operand: if this is also a list, all is ok.
2213 // For "something . ...", "something - ..." or "non-list + ...",
2214 // we know that the first operand needs to be a string or number
2215 // without evaluating the 2nd operand. So check before to avoid
2216 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002217 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002218 {
2219 clear_tv(rettv);
2220 return FAIL;
2221 }
2222 }
2223
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 /*
2225 * Get the second variable.
2226 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002227 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2228 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002230 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002232 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 return FAIL;
2234 }
2235
2236 if (evaluate)
2237 {
2238 /*
2239 * Compute the result.
2240 */
2241 if (op == '.')
2242 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002243 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002244 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002245 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002246 {
2247 clear_tv(rettv);
2248 clear_tv(&var2);
2249 return FAIL;
2250 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002251 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252 clear_tv(rettv);
2253 rettv->v_type = VAR_STRING;
2254 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002256 else if (op == '+' && rettv->v_type == VAR_BLOB
2257 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002259 else if (op == '+' && rettv->v_type == VAR_LIST
2260 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002261 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002263 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 else
2266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002267 int error = FALSE;
2268
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002269#ifdef FEAT_FLOAT
2270 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002271 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002272 f1 = rettv->vval.v_float;
2273 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002275 else
2276#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002278 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002279 if (error)
2280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002281 // This can only happen for "list + non-list". For
2282 // "non-list + ..." or "something - ...", we returned
2283 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002284 clear_tv(rettv);
2285 return FAIL;
2286 }
2287#ifdef FEAT_FLOAT
2288 if (var2.v_type == VAR_FLOAT)
2289 f1 = n1;
2290#endif
2291 }
2292#ifdef FEAT_FLOAT
2293 if (var2.v_type == VAR_FLOAT)
2294 {
2295 f2 = var2.vval.v_float;
2296 n2 = 0;
2297 }
2298 else
2299#endif
2300 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002301 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002302 if (error)
2303 {
2304 clear_tv(rettv);
2305 clear_tv(&var2);
2306 return FAIL;
2307 }
2308#ifdef FEAT_FLOAT
2309 if (rettv->v_type == VAR_FLOAT)
2310 f2 = n2;
2311#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002312 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002313 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002314
2315#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002316 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002317 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2318 {
2319 if (op == '+')
2320 f1 = f1 + f2;
2321 else
2322 f1 = f1 - f2;
2323 rettv->v_type = VAR_FLOAT;
2324 rettv->vval.v_float = f1;
2325 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002327#endif
2328 {
2329 if (op == '+')
2330 n1 = n1 + n2;
2331 else
2332 n1 = n1 - n2;
2333 rettv->v_type = VAR_NUMBER;
2334 rettv->vval.v_number = n1;
2335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 }
2339 }
2340 return OK;
2341}
2342
2343/*
2344 * Handle fifth level expression:
2345 * * number multiplication
2346 * / number division
2347 * % number modulo
2348 *
2349 * "arg" must point to the first non-white of the expression.
2350 * "arg" is advanced to the next non-white after the recognized expression.
2351 *
2352 * Return OK or FAIL.
2353 */
2354 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002355eval6(
2356 char_u **arg,
2357 typval_T *rettv,
2358 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002359 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360{
Bram Moolenaar33570922005-01-25 22:26:29 +00002361 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002363 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002364#ifdef FEAT_FLOAT
2365 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002366 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002367#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002368 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 /*
2371 * Get the first variable.
2372 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002373 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return FAIL;
2375
2376 /*
2377 * Repeat computing, until no '*', '/' or '%' is following.
2378 */
2379 for (;;)
2380 {
2381 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002382 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 break;
2384
2385 if (evaluate)
2386 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002387#ifdef FEAT_FLOAT
2388 if (rettv->v_type == VAR_FLOAT)
2389 {
2390 f1 = rettv->vval.v_float;
2391 use_float = TRUE;
2392 n1 = 0;
2393 }
2394 else
2395#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002396 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002398 if (error)
2399 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 }
2401 else
2402 n1 = 0;
2403
2404 /*
2405 * Get the second variable.
2406 */
2407 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002408 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 return FAIL;
2410
2411 if (evaluate)
2412 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002413#ifdef FEAT_FLOAT
2414 if (var2.v_type == VAR_FLOAT)
2415 {
2416 if (!use_float)
2417 {
2418 f1 = n1;
2419 use_float = TRUE;
2420 }
2421 f2 = var2.vval.v_float;
2422 n2 = 0;
2423 }
2424 else
2425#endif
2426 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002427 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002428 clear_tv(&var2);
2429 if (error)
2430 return FAIL;
2431#ifdef FEAT_FLOAT
2432 if (use_float)
2433 f2 = n2;
2434#endif
2435 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436
2437 /*
2438 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002439 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002441#ifdef FEAT_FLOAT
2442 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002444 if (op == '*')
2445 f1 = f1 * f2;
2446 else if (op == '/')
2447 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002448# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002449 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002450 if (f2 == 0.0)
2451 {
2452 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002453 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002454 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002455 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002456 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002457 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002458 }
2459 else
2460 f1 = f1 / f2;
2461# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002462 // We rely on the floating point library to handle divide
2463 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002464 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002465# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002468 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002470 return FAIL;
2471 }
2472 rettv->v_type = VAR_FLOAT;
2473 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
2475 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002478 if (op == '*')
2479 n1 = n1 * n2;
2480 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002481 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002483 n1 = num_modulus(n1, n2);
2484
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002485 rettv->v_type = VAR_NUMBER;
2486 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 }
2489 }
2490
2491 return OK;
2492}
2493
2494/*
2495 * Handle sixth level expression:
2496 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002497 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002498 * "string" string constant
2499 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 * &option-name option value
2501 * @r register contents
2502 * identifier variable value
2503 * function() function call
2504 * $VAR environment variable
2505 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002506 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002508 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002509 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 *
2511 * Also handle:
2512 * ! in front logical NOT
2513 * - in front unary minus
2514 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002515 * trailing [] subscript in String or List
2516 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002517 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 *
2519 * "arg" must point to the first non-white of the expression.
2520 * "arg" is advanced to the next non-white after the recognized expression.
2521 *
2522 * Return OK or FAIL.
2523 */
2524 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002525eval7(
2526 char_u **arg,
2527 typval_T *rettv,
2528 int evaluate,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 int len;
2532 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 char_u *start_leader, *end_leader;
2534 int ret = OK;
2535 char_u *alias;
2536
2537 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002539 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542
2543 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002544 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
2546 start_leader = *arg;
2547 while (**arg == '!' || **arg == '-' || **arg == '+')
2548 *arg = skipwhite(*arg + 1);
2549 end_leader = *arg;
2550
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002551 if (**arg == '.' && (!isdigit(*(*arg + 1))
2552#ifdef FEAT_FLOAT
2553 || current_sctx.sc_version < 2
2554#endif
2555 ))
2556 {
2557 semsg(_(e_invexpr2), *arg);
2558 ++*arg;
2559 return FAIL;
2560 }
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 switch (**arg)
2563 {
2564 /*
2565 * Number constant.
2566 */
2567 case '0':
2568 case '1':
2569 case '2':
2570 case '3':
2571 case '4':
2572 case '5':
2573 case '6':
2574 case '7':
2575 case '8':
2576 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 break;
2579
2580 /*
2581 * String constant: "string".
2582 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 break;
2585
2586 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002587 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002590 break;
2591
2592 /*
2593 * List: [expr, expr]
2594 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 case '[': ret = get_list_tv(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 break;
2597
2598 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002599 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002600 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002601 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002602 {
2603 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002604 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002605 }
2606 else
2607 ret = NOTDONE;
2608 break;
2609
2610 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002611 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002612 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002613 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002614 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2615 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002616 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 break;
2618
2619 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002620 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002622 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 break;
2624
2625 /*
2626 * Environment variable: $VAR.
2627 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002628 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 break;
2630
2631 /*
2632 * Register contents: @r.
2633 */
2634 case '@': ++*arg;
2635 if (evaluate)
2636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002637 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002638 rettv->vval.v_string = get_reg_contents(**arg,
2639 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 }
2641 if (**arg != NUL)
2642 ++*arg;
2643 break;
2644
2645 /*
2646 * nested expression: (expression).
2647 */
2648 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002649 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 if (**arg == ')')
2651 ++*arg;
2652 else if (ret == OK)
2653 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 ret = FAIL;
2657 }
2658 break;
2659
Bram Moolenaar8c711452005-01-14 21:53:12 +00002660 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 break;
2662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663
2664 if (ret == NOTDONE)
2665 {
2666 /*
2667 * Must be a variable or function name.
2668 * Can also be a curly-braces kind of name: {expr}.
2669 */
2670 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002671 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 if (alias != NULL)
2673 s = alias;
2674
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002675 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 ret = FAIL;
2677 else
2678 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002679 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002680 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002682 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002683 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002684 {
2685 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002689 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 *arg = skipwhite(*arg);
2693
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002694 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2695 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002696 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002697 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2698 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699
2700 /*
2701 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2702 */
2703 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002704 ret = eval7_leader(rettv, start_leader, &end_leader);
2705 return ret;
2706}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002707
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002708/*
2709 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2710 * Adjusts "end_leaderp" until it is at "start_leader".
2711 */
2712 static int
2713eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2714{
2715 char_u *end_leader = *end_leaderp;
2716 int ret = OK;
2717 int error = FALSE;
2718 varnumber_T val = 0;
2719#ifdef FEAT_FLOAT
2720 float_T f = 0.0;
2721
2722 if (rettv->v_type == VAR_FLOAT)
2723 f = rettv->vval.v_float;
2724 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002725#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002726 val = tv_get_number_chk(rettv, &error);
2727 if (error)
2728 {
2729 clear_tv(rettv);
2730 ret = FAIL;
2731 }
2732 else
2733 {
2734 while (end_leader > start_leader)
2735 {
2736 --end_leader;
2737 if (*end_leader == '!')
2738 {
2739#ifdef FEAT_FLOAT
2740 if (rettv->v_type == VAR_FLOAT)
2741 f = !f;
2742 else
2743#endif
2744 val = !val;
2745 }
2746 else if (*end_leader == '-')
2747 {
2748#ifdef FEAT_FLOAT
2749 if (rettv->v_type == VAR_FLOAT)
2750 f = -f;
2751 else
2752#endif
2753 val = -val;
2754 }
2755 }
2756#ifdef FEAT_FLOAT
2757 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002759 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002760 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002763#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002764 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002765 clear_tv(rettv);
2766 rettv->v_type = VAR_NUMBER;
2767 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002770 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 return ret;
2772}
2773
2774/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002775 * Call the function referred to in "rettv".
2776 */
2777 static int
2778call_func_rettv(
2779 char_u **arg,
2780 typval_T *rettv,
2781 int evaluate,
2782 dict_T *selfdict,
2783 typval_T *basetv)
2784{
2785 partial_T *pt = NULL;
2786 funcexe_T funcexe;
2787 typval_T functv;
2788 char_u *s;
2789 int ret;
2790
2791 // need to copy the funcref so that we can clear rettv
2792 if (evaluate)
2793 {
2794 functv = *rettv;
2795 rettv->v_type = VAR_UNKNOWN;
2796
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002797 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002798 if (functv.v_type == VAR_PARTIAL)
2799 {
2800 pt = functv.vval.v_partial;
2801 s = partial_name(pt);
2802 }
2803 else
2804 s = functv.vval.v_string;
2805 }
2806 else
2807 s = (char_u *)"";
2808
Bram Moolenaara80faa82020-04-12 19:37:17 +02002809 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002810 funcexe.firstline = curwin->w_cursor.lnum;
2811 funcexe.lastline = curwin->w_cursor.lnum;
2812 funcexe.evaluate = evaluate;
2813 funcexe.partial = pt;
2814 funcexe.selfdict = selfdict;
2815 funcexe.basetv = basetv;
2816 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2817
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002818 // Clear the funcref afterwards, so that deleting it while
2819 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002820 if (evaluate)
2821 clear_tv(&functv);
2822
2823 return ret;
2824}
2825
2826/*
2827 * Evaluate "->method()".
2828 * "*arg" points to the '-'.
2829 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2830 */
2831 static int
2832eval_lambda(
2833 char_u **arg,
2834 typval_T *rettv,
2835 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002836 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002837{
2838 typval_T base = *rettv;
2839 int ret;
2840
2841 // Skip over the ->.
2842 *arg += 2;
2843 rettv->v_type = VAR_UNKNOWN;
2844
2845 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002846 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002847 return FAIL;
2848 else if (**arg != '(')
2849 {
2850 if (verbose)
2851 {
2852 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002853 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002854 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002856 }
2857 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002858 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002859 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002860 else
2861 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2862
2863 // Clear the funcref afterwards, so that deleting it while
2864 // evaluating the arguments is possible (see test55).
2865 if (evaluate)
2866 clear_tv(&base);
2867
2868 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002869}
2870
2871/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002872 * Evaluate "->method()".
2873 * "*arg" points to the '-'.
2874 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2875 */
2876 static int
2877eval_method(
2878 char_u **arg,
2879 typval_T *rettv,
2880 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002881 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002882{
2883 char_u *name;
2884 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002885 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002886 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002887 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002888
2889 // Skip over the ->.
2890 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002891 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002892
Bram Moolenaarac92e252019-08-03 21:58:38 +02002893 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002894 len = get_name_len(arg, &alias, evaluate, TRUE);
2895 if (alias != NULL)
2896 name = alias;
2897
2898 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002899 {
2900 if (verbose)
2901 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002902 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002903 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002904 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002905 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002906 if (**arg != '(')
2907 {
2908 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002910 ret = FAIL;
2911 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002912 else if (VIM_ISWHITE((*arg)[-1]))
2913 {
2914 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002915 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002916 ret = FAIL;
2917 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002918 else
2919 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002920 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002921
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002922 // Clear the funcref afterwards, so that deleting it while
2923 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002924 if (evaluate)
2925 clear_tv(&base);
2926
Bram Moolenaarac92e252019-08-03 21:58:38 +02002927 return ret;
2928}
2929
2930/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002931 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2932 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002933 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2934 */
2935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002936eval_index(
2937 char_u **arg,
2938 typval_T *rettv,
2939 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002940 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002941{
2942 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002944 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002945 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002946 long len = -1;
2947 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002948 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002949 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002950
Bram Moolenaara03f2332016-02-06 18:09:59 +01002951 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002952 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002953 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002954 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002955 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002956 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002957 return FAIL;
2958 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002959#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002960 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002961 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002962 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002963#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002964 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002965 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002966 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002967 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002968 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002969 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002970 return FAIL;
2971 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002972 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002974 if (evaluate)
2975 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002976 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002977
2978 case VAR_STRING:
2979 case VAR_NUMBER:
2980 case VAR_LIST:
2981 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002982 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002983 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002984 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002985
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02002986 init_tv(&var1);
2987 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002988 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002989 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002990 /*
2991 * dict.name
2992 */
2993 key = *arg + 1;
2994 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2995 ;
2996 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002997 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002998 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002999 }
3000 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003001 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003002 /*
3003 * something[idx]
3004 *
3005 * Get the (first) variable from inside the [].
3006 */
3007 *arg = skipwhite(*arg + 1);
3008 if (**arg == ':')
3009 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003010 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003011 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003012 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003013 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003014 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003015 clear_tv(&var1);
3016 return FAIL;
3017 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003018
3019 /*
3020 * Get the second variable from inside the [:].
3021 */
3022 if (**arg == ':')
3023 {
3024 range = TRUE;
3025 *arg = skipwhite(*arg + 1);
3026 if (**arg == ']')
3027 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003028 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003029 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003030 if (!empty1)
3031 clear_tv(&var1);
3032 return FAIL;
3033 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003034 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003035 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003036 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003037 if (!empty1)
3038 clear_tv(&var1);
3039 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003040 return FAIL;
3041 }
3042 }
3043
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003044 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003045 if (**arg != ']')
3046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003047 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003048 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003049 clear_tv(&var1);
3050 if (range)
3051 clear_tv(&var2);
3052 return FAIL;
3053 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003054 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003055 }
3056
3057 if (evaluate)
3058 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059 n1 = 0;
3060 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003061 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003062 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003063 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003064 }
3065 if (range)
3066 {
3067 if (empty2)
3068 n2 = -1;
3069 else
3070 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003071 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003072 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003073 }
3074 }
3075
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003077 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003078 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003079 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003081 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003082 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003083 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003084 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003085 case VAR_SPECIAL:
3086 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003087 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003088 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003089
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003090 case VAR_NUMBER:
3091 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003092 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003093 len = (long)STRLEN(s);
3094 if (range)
3095 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003096 // The resulting variable is a substring. If the indexes
3097 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003098 if (n1 < 0)
3099 {
3100 n1 = len + n1;
3101 if (n1 < 0)
3102 n1 = 0;
3103 }
3104 if (n2 < 0)
3105 n2 = len + n2;
3106 else if (n2 >= len)
3107 n2 = len;
3108 if (n1 >= len || n2 < 0 || n1 > n2)
3109 s = NULL;
3110 else
3111 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3112 }
3113 else
3114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003115 // The resulting variable is a string of a single
3116 // character. If the index is too big or negative the
3117 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003118 if (n1 >= len || n1 < 0)
3119 s = NULL;
3120 else
3121 s = vim_strnsave(s + n1, 1);
3122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003123 clear_tv(rettv);
3124 rettv->v_type = VAR_STRING;
3125 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003126 break;
3127
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003128 case VAR_BLOB:
3129 len = blob_len(rettv->vval.v_blob);
3130 if (range)
3131 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003132 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003133 // are out of range the result is empty.
3134 if (n1 < 0)
3135 {
3136 n1 = len + n1;
3137 if (n1 < 0)
3138 n1 = 0;
3139 }
3140 if (n2 < 0)
3141 n2 = len + n2;
3142 else if (n2 >= len)
3143 n2 = len - 1;
3144 if (n1 >= len || n2 < 0 || n1 > n2)
3145 {
3146 clear_tv(rettv);
3147 rettv->v_type = VAR_BLOB;
3148 rettv->vval.v_blob = NULL;
3149 }
3150 else
3151 {
3152 blob_T *blob = blob_alloc();
3153
3154 if (blob != NULL)
3155 {
3156 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3157 {
3158 blob_free(blob);
3159 return FAIL;
3160 }
3161 blob->bv_ga.ga_len = n2 - n1 + 1;
3162 for (i = n1; i <= n2; i++)
3163 blob_set(blob, i - n1,
3164 blob_get(rettv->vval.v_blob, i));
3165
3166 clear_tv(rettv);
3167 rettv_blob_set(rettv, blob);
3168 }
3169 }
3170 }
3171 else
3172 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003173 // The resulting variable is a byte value.
3174 // If the index is too big or negative that is an error.
3175 if (n1 < 0)
3176 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003177 if (n1 < len && n1 >= 0)
3178 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003179 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003180
3181 clear_tv(rettv);
3182 rettv->v_type = VAR_NUMBER;
3183 rettv->vval.v_number = v;
3184 }
3185 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003186 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003187 }
3188 break;
3189
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003190 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003191 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003192 if (n1 < 0)
3193 n1 = len + n1;
3194 if (!empty1 && (n1 < 0 || n1 >= len))
3195 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003196 // For a range we allow invalid values and return an empty
3197 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003198 if (!range)
3199 {
3200 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003201 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003202 return FAIL;
3203 }
3204 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003205 }
3206 if (range)
3207 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003208 list_T *l;
3209 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003210
3211 if (n2 < 0)
3212 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003213 else if (n2 >= len)
3214 n2 = len - 1;
3215 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003216 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003217 l = list_alloc();
3218 if (l == NULL)
3219 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003220 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003221 n1 <= n2; ++n1)
3222 {
3223 if (list_append_tv(l, &item->li_tv) == FAIL)
3224 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003225 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003226 return FAIL;
3227 }
3228 item = item->li_next;
3229 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003230 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003231 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003232 }
3233 else
3234 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003235 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003236 clear_tv(rettv);
3237 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003238 }
3239 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003240
3241 case VAR_DICT:
3242 if (range)
3243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003244 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003245 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003246 if (len == -1)
3247 clear_tv(&var1);
3248 return FAIL;
3249 }
3250 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003252
3253 if (len == -1)
3254 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003255 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003256 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003257 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003258 clear_tv(&var1);
3259 return FAIL;
3260 }
3261 }
3262
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003263 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003264
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003265 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003266 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267 if (len == -1)
3268 clear_tv(&var1);
3269 if (item == NULL)
3270 return FAIL;
3271
3272 copy_tv(&item->di_tv, &var1);
3273 clear_tv(rettv);
3274 *rettv = var1;
3275 }
3276 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003277 }
3278 }
3279
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003280 return OK;
3281}
3282
3283/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 * Get an option value.
3285 * "arg" points to the '&' or '+' before the option name.
3286 * "arg" is advanced to character after the option name.
3287 * Return OK or FAIL.
3288 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290get_option_tv(
3291 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003292 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 char_u *option_end;
3296 long numval;
3297 char_u *stringval;
3298 int opt_type;
3299 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003300 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003301 int ret = OK;
3302 int opt_flags;
3303
3304 /*
3305 * Isolate the option name and find its value.
3306 */
3307 option_end = find_option_end(arg, &opt_flags);
3308 if (option_end == NULL)
3309 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003310 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003311 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 return FAIL;
3313 }
3314
3315 if (!evaluate)
3316 {
3317 *arg = option_end;
3318 return OK;
3319 }
3320
3321 c = *option_end;
3322 *option_end = NUL;
3323 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003324 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003326 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003328 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 ret = FAIL;
3331 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003332 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003334 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003336 rettv->v_type = VAR_STRING;
3337 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003339 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003341 rettv->v_type = VAR_NUMBER;
3342 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003344 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003346 rettv->v_type = VAR_NUMBER;
3347 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003349 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003351 rettv->v_type = VAR_STRING;
3352 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354 }
3355 else if (working && (opt_type == -2 || opt_type == -1))
3356 ret = FAIL;
3357
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003358 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 *arg = option_end;
3360
3361 return ret;
3362}
3363
3364/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3366 * Return OK or FAIL.
3367 */
3368 int
3369get_number_tv(
3370 char_u **arg,
3371 typval_T *rettv,
3372 int evaluate,
3373 int want_string UNUSED)
3374{
3375 int len;
3376#ifdef FEAT_FLOAT
3377 char_u *p;
3378 int get_float = FALSE;
3379
3380 // We accept a float when the format matches
3381 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3382 // strict to avoid backwards compatibility problems.
3383 // With script version 2 and later the leading digit can be
3384 // omitted.
3385 // Don't look for a float after the "." operator, so that
3386 // ":let vers = 1.2.3" doesn't fail.
3387 if (**arg == '.')
3388 p = *arg;
3389 else
3390 p = skipdigits(*arg + 1);
3391 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3392 {
3393 get_float = TRUE;
3394 p = skipdigits(p + 2);
3395 if (*p == 'e' || *p == 'E')
3396 {
3397 ++p;
3398 if (*p == '-' || *p == '+')
3399 ++p;
3400 if (!vim_isdigit(*p))
3401 get_float = FALSE;
3402 else
3403 p = skipdigits(p + 1);
3404 }
3405 if (ASCII_ISALPHA(*p) || *p == '.')
3406 get_float = FALSE;
3407 }
3408 if (get_float)
3409 {
3410 float_T f;
3411
3412 *arg += string2float(*arg, &f);
3413 if (evaluate)
3414 {
3415 rettv->v_type = VAR_FLOAT;
3416 rettv->vval.v_float = f;
3417 }
3418 }
3419 else
3420#endif
3421 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3422 {
3423 char_u *bp;
3424 blob_T *blob = NULL; // init for gcc
3425
3426 // Blob constant: 0z0123456789abcdef
3427 if (evaluate)
3428 blob = blob_alloc();
3429 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3430 {
3431 if (!vim_isxdigit(bp[1]))
3432 {
3433 if (blob != NULL)
3434 {
3435 emsg(_("E973: Blob literal should have an even number of hex characters"));
3436 ga_clear(&blob->bv_ga);
3437 VIM_CLEAR(blob);
3438 }
3439 return FAIL;
3440 }
3441 if (blob != NULL)
3442 ga_append(&blob->bv_ga,
3443 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3444 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3445 ++bp;
3446 }
3447 if (blob != NULL)
3448 rettv_blob_set(rettv, blob);
3449 *arg = bp;
3450 }
3451 else
3452 {
3453 varnumber_T n;
3454
3455 // decimal, hex or octal number
3456 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3457 ? STR2NR_NO_OCT + STR2NR_QUOTE
3458 : STR2NR_ALL, &n, NULL, 0, TRUE);
3459 if (len == 0)
3460 {
3461 semsg(_(e_invexpr2), *arg);
3462 return FAIL;
3463 }
3464 *arg += len;
3465 if (evaluate)
3466 {
3467 rettv->v_type = VAR_NUMBER;
3468 rettv->vval.v_number = n;
3469 }
3470 }
3471 return OK;
3472}
3473
3474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 * Allocate a variable for a string constant.
3476 * Return OK or FAIL.
3477 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003479get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 char_u *p;
3482 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 int extra = 0;
3484
3485 /*
3486 * Find the end of the string, skipping backslashed characters.
3487 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003488 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 {
3490 if (*p == '\\' && p[1] != NUL)
3491 {
3492 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003493 // A "\<x>" form occupies at least 4 characters, and produces up
3494 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (*p == '<')
3496 extra += 2;
3497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 }
3499
3500 if (*p != '"')
3501 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003502 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 return FAIL;
3504 }
3505
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003506 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 if (!evaluate)
3508 {
3509 *arg = p + 1;
3510 return OK;
3511 }
3512
3513 /*
3514 * Copy the string into allocated memory, handling backslashed
3515 * characters.
3516 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003517 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (name == NULL)
3519 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003520 rettv->v_type = VAR_STRING;
3521 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
3525 if (*p == '\\')
3526 {
3527 switch (*++p)
3528 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003529 case 'b': *name++ = BS; ++p; break;
3530 case 'e': *name++ = ESC; ++p; break;
3531 case 'f': *name++ = FF; ++p; break;
3532 case 'n': *name++ = NL; ++p; break;
3533 case 'r': *name++ = CAR; ++p; break;
3534 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003536 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003538 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 case 'U':
3540 if (vim_isxdigit(p[1]))
3541 {
3542 int n, nr;
3543 int c = toupper(*p);
3544
3545 if (c == 'X')
3546 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003547 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003549 else
3550 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 nr = 0;
3552 while (--n >= 0 && vim_isxdigit(p[1]))
3553 {
3554 ++p;
3555 nr = (nr << 4) + hex2nr(*p);
3556 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003557 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003558 // For "\u" store the number according to
3559 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003561 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003563 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 break;
3566
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003567 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 case '0':
3569 case '1':
3570 case '2':
3571 case '3':
3572 case '4':
3573 case '5':
3574 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003575 case '7': *name = *p++ - '0';
3576 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 *name = (*name << 3) + *p++ - '0';
3579 if (*p >= '0' && *p <= '7')
3580 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003582 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 break;
3584
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003585 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003586 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3587 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 if (extra != 0)
3589 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003590 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 break;
3592 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003593 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 break;
3597 }
3598 }
3599 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003604 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003605 ++p;
3606 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 return OK;
3609}
3610
3611/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003612 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 * Return OK or FAIL.
3614 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003616get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617{
3618 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003619 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003620 int reduce = 0;
3621
3622 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003623 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003624 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003625 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003626 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003627 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003629 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003630 break;
3631 ++reduce;
3632 ++p;
3633 }
3634 }
3635
Bram Moolenaar8c711452005-01-14 21:53:12 +00003636 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003637 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003638 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003639 return FAIL;
3640 }
3641
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003642 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003643 if (!evaluate)
3644 {
3645 *arg = p + 1;
3646 return OK;
3647 }
3648
3649 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003650 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003651 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003652 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003653 if (str == NULL)
3654 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003655 rettv->v_type = VAR_STRING;
3656 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003657
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003659 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003661 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003662 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003663 break;
3664 ++p;
3665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003667 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003669 *arg = p + 1;
3670
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003671 return OK;
3672}
3673
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003674/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003675 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003676 */
3677 char_u *
3678partial_name(partial_T *pt)
3679{
3680 if (pt->pt_name != NULL)
3681 return pt->pt_name;
3682 return pt->pt_func->uf_name;
3683}
3684
Bram Moolenaarddecc252016-04-06 22:59:37 +02003685 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003686partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003687{
3688 int i;
3689
3690 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003691 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003692 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003693 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003694 if (pt->pt_name != NULL)
3695 {
3696 func_unref(pt->pt_name);
3697 vim_free(pt->pt_name);
3698 }
3699 else
3700 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003701 vim_free(pt);
3702}
3703
3704/*
3705 * Unreference a closure: decrement the reference count and free it when it
3706 * becomes zero.
3707 */
3708 void
3709partial_unref(partial_T *pt)
3710{
3711 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003712 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003713}
3714
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003715static int tv_equal_recurse_limit;
3716
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003717 static int
3718func_equal(
3719 typval_T *tv1,
3720 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003721 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003722{
3723 char_u *s1, *s2;
3724 dict_T *d1, *d2;
3725 int a1, a2;
3726 int i;
3727
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003728 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003729 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003730 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003731 if (s1 != NULL && *s1 == NUL)
3732 s1 = NULL;
3733 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003734 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003735 if (s2 != NULL && *s2 == NUL)
3736 s2 = NULL;
3737 if (s1 == NULL || s2 == NULL)
3738 {
3739 if (s1 != s2)
3740 return FALSE;
3741 }
3742 else if (STRCMP(s1, s2) != 0)
3743 return FALSE;
3744
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003745 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003746 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3747 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3748 if (d1 == NULL || d2 == NULL)
3749 {
3750 if (d1 != d2)
3751 return FALSE;
3752 }
3753 else if (!dict_equal(d1, d2, ic, TRUE))
3754 return FALSE;
3755
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003756 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003757 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3758 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3759 if (a1 != a2)
3760 return FALSE;
3761 for (i = 0; i < a1; ++i)
3762 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3763 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3764 return FALSE;
3765
3766 return TRUE;
3767}
3768
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003769/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003770 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003771 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003772 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003773 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003774 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003775tv_equal(
3776 typval_T *tv1,
3777 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003778 int ic, // ignore case
3779 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003780{
3781 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003782 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003783 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003784 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003785
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 // Catch lists and dicts that have an endless loop by limiting
3787 // recursiveness to a limit. We guess they are equal then.
3788 // A fixed limit has the problem of still taking an awful long time.
3789 // Reduce the limit every time running into it. That should work fine for
3790 // deeply linked structures that are not recursively linked and catch
3791 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003792 if (!recursive)
3793 tv_equal_recurse_limit = 1000;
3794 if (recursive_cnt >= tv_equal_recurse_limit)
3795 {
3796 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003797 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003798 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003800 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3801 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003802 if ((tv1->v_type == VAR_FUNC
3803 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3804 && (tv2->v_type == VAR_FUNC
3805 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3806 {
3807 ++recursive_cnt;
3808 r = func_equal(tv1, tv2, ic);
3809 --recursive_cnt;
3810 return r;
3811 }
3812
3813 if (tv1->v_type != tv2->v_type)
3814 return FALSE;
3815
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003816 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003817 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003818 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003819 ++recursive_cnt;
3820 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3821 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003822 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003823
3824 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003825 ++recursive_cnt;
3826 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3827 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003828 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003829
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003830 case VAR_BLOB:
3831 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3832
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003833 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 case VAR_BOOL:
3835 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003836 return tv1->vval.v_number == tv2->vval.v_number;
3837
3838 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003839 s1 = tv_get_string_buf(tv1, buf1);
3840 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003841 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003842
Bram Moolenaar835dc632016-02-07 14:27:38 +01003843 case VAR_FLOAT:
3844#ifdef FEAT_FLOAT
3845 return tv1->vval.v_float == tv2->vval.v_float;
3846#endif
3847 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003848#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003849 return tv1->vval.v_job == tv2->vval.v_job;
3850#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003851 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003852#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003853 return tv1->vval.v_channel == tv2->vval.v_channel;
3854#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003856 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003857 return tv1->vval.v_partial == tv2->vval.v_partial;
3858
3859 case VAR_FUNC:
3860 return tv1->vval.v_string == tv2->vval.v_string;
3861
Bram Moolenaar835dc632016-02-07 14:27:38 +01003862 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003863 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003865 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003866 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003867
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003868 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3869 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003870 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003871}
3872
3873/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003874 * Return the next (unique) copy ID.
3875 * Used for serializing nested structures.
3876 */
3877 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003878get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003879{
3880 current_copyID += COPYID_INC;
3881 return current_copyID;
3882}
3883
3884/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003885 * Garbage collection for lists and dictionaries.
3886 *
3887 * We use reference counts to be able to free most items right away when they
3888 * are no longer used. But for composite items it's possible that it becomes
3889 * unused while the reference count is > 0: When there is a recursive
3890 * reference. Example:
3891 * :let l = [1, 2, 3]
3892 * :let d = {9: l}
3893 * :let l[1] = d
3894 *
3895 * Since this is quite unusual we handle this with garbage collection: every
3896 * once in a while find out which lists and dicts are not referenced from any
3897 * variable.
3898 *
3899 * Here is a good reference text about garbage collection (refers to Python
3900 * but it applies to all reference-counting mechanisms):
3901 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003902 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003903
3904/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003905 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003906 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003907 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003908 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003909 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003910garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003911{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003912 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003913 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003914 buf_T *buf;
3915 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003916 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003917 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003918
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003919 if (!testing)
3920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003921 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003922 want_garbage_collect = FALSE;
3923 may_garbage_collect = FALSE;
3924 garbage_collect_at_exit = FALSE;
3925 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003926
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003927 // The execution stack can grow big, limit the size.
3928 if (exestack.ga_maxlen - exestack.ga_len > 500)
3929 {
3930 size_t new_len;
3931 char_u *pp;
3932 int n;
3933
3934 // Keep 150% of the current size, with a minimum of the growth size.
3935 n = exestack.ga_len / 2;
3936 if (n < exestack.ga_growsize)
3937 n = exestack.ga_growsize;
3938
3939 // Don't make it bigger though.
3940 if (exestack.ga_len + n < exestack.ga_maxlen)
3941 {
3942 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3943 pp = vim_realloc(exestack.ga_data, new_len);
3944 if (pp == NULL)
3945 return FAIL;
3946 exestack.ga_maxlen = exestack.ga_len + n;
3947 exestack.ga_data = pp;
3948 }
3949 }
3950
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003951 // We advance by two because we add one for items referenced through
3952 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003953 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003954
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003955 /*
3956 * 1. Go through all accessible variables and mark all lists and dicts
3957 * with copyID.
3958 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003959
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003960 // Don't free variables in the previous_funccal list unless they are only
3961 // referenced through previous_funccal. This must be first, because if
3962 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003963 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003965 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003966 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003968 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003969 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003970 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3971 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003972
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003973 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003975 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3976 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003977 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003978 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3979 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003980#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003981 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003982 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3983 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003984 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003985 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003986 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3987 NULL, NULL);
3988#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003990 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003991 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003992 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3993 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003994 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003995 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003998 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004000 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004001 abort = abort || set_ref_in_functions(copyID);
4002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004003 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004004 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004006 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004007 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004008
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004009 // callbacks in buffers
4010 abort = abort || set_ref_in_buffers(copyID);
4011
Bram Moolenaar1dced572012-04-05 16:54:08 +02004012#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004013 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004014#endif
4015
Bram Moolenaardb913952012-06-29 12:54:53 +02004016#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004017 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004018#endif
4019
4020#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004021 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004022#endif
4023
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004024#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004025 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004026 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004027#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004028#ifdef FEAT_NETBEANS_INTG
4029 abort = abort || set_ref_in_nb_channel(copyID);
4030#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004031
Bram Moolenaare3188e22016-05-31 21:13:04 +02004032#ifdef FEAT_TIMERS
4033 abort = abort || set_ref_in_timer(copyID);
4034#endif
4035
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004036#ifdef FEAT_QUICKFIX
4037 abort = abort || set_ref_in_quickfix(copyID);
4038#endif
4039
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004040#ifdef FEAT_TERMINAL
4041 abort = abort || set_ref_in_term(copyID);
4042#endif
4043
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004044#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004045 abort = abort || set_ref_in_popups(copyID);
4046#endif
4047
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004048 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004049 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004050 /*
4051 * 2. Free lists and dictionaries that are not referenced.
4052 */
4053 did_free = free_unref_items(copyID);
4054
4055 /*
4056 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004057 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004058 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004059 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004060 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004061 else if (p_verbose > 0)
4062 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004063 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004065
4066 return did_free;
4067}
4068
4069/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004070 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004071 */
4072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004073free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004074{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004075 int did_free = FALSE;
4076
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004077 // Let all "free" functions know that we are here. This means no
4078 // dictionaries, lists, channels or jobs are to be freed, because we will
4079 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004080 in_free_unref_items = TRUE;
4081
4082 /*
4083 * PASS 1: free the contents of the items. We don't free the items
4084 * themselves yet, so that it is possible to decrement refcount counters
4085 */
4086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004087 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004088 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004090 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004091 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004092
4093#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004094 // Go through the list of jobs and free items without the copyID. This
4095 // must happen before doing channels, because jobs refer to channels, but
4096 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004097 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4098
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004099 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004100 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4101#endif
4102
4103 /*
4104 * PASS 2: free the items themselves.
4105 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004106 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004107 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004108
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004109#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004110 // Go through the list of jobs and free items without the copyID. This
4111 // must happen before doing channels, because jobs refer to channels, but
4112 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004113 free_unused_jobs(copyID, COPYID_MASK);
4114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004115 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004116 free_unused_channels(copyID, COPYID_MASK);
4117#endif
4118
4119 in_free_unref_items = FALSE;
4120
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004121 return did_free;
4122}
4123
4124/*
4125 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004126 * "list_stack" is used to add lists to be marked. Can be NULL.
4127 *
4128 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004129 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004130 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004131set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004132{
4133 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004134 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004135 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004136 hashtab_T *cur_ht;
4137 ht_stack_T *ht_stack = NULL;
4138 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004139
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004140 cur_ht = ht;
4141 for (;;)
4142 {
4143 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004144 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004145 // Mark each item in the hashtab. If the item contains a hashtab
4146 // it is added to ht_stack, if it contains a list it is added to
4147 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004148 todo = (int)cur_ht->ht_used;
4149 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4150 if (!HASHITEM_EMPTY(hi))
4151 {
4152 --todo;
4153 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4154 &ht_stack, list_stack);
4155 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004156 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004157
4158 if (ht_stack == NULL)
4159 break;
4160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004161 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004162 cur_ht = ht_stack->ht;
4163 tempitem = ht_stack;
4164 ht_stack = ht_stack->prev;
4165 free(tempitem);
4166 }
4167
4168 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004169}
4170
4171/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004172 * Mark a dict and its items with "copyID".
4173 * Returns TRUE if setting references failed somehow.
4174 */
4175 int
4176set_ref_in_dict(dict_T *d, int copyID)
4177{
4178 if (d != NULL && d->dv_copyID != copyID)
4179 {
4180 d->dv_copyID = copyID;
4181 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4182 }
4183 return FALSE;
4184}
4185
4186/*
4187 * Mark a list and its items with "copyID".
4188 * Returns TRUE if setting references failed somehow.
4189 */
4190 int
4191set_ref_in_list(list_T *ll, int copyID)
4192{
4193 if (ll != NULL && ll->lv_copyID != copyID)
4194 {
4195 ll->lv_copyID = copyID;
4196 return set_ref_in_list_items(ll, copyID, NULL);
4197 }
4198 return FALSE;
4199}
4200
4201/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004202 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004203 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4204 *
4205 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004206 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004207 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004208set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004209{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004210 listitem_T *li;
4211 int abort = FALSE;
4212 list_T *cur_l;
4213 list_stack_T *list_stack = NULL;
4214 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004215
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004216 cur_l = l;
4217 for (;;)
4218 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004219 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004220 // Mark each item in the list. If the item contains a hashtab
4221 // it is added to ht_stack, if it contains a list it is added to
4222 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004223 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4224 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4225 ht_stack, &list_stack);
4226 if (list_stack == NULL)
4227 break;
4228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004229 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004230 cur_l = list_stack->list;
4231 tempitem = list_stack;
4232 list_stack = list_stack->prev;
4233 free(tempitem);
4234 }
4235
4236 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004237}
4238
4239/*
4240 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004241 * "list_stack" is used to add lists to be marked. Can be NULL.
4242 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4243 *
4244 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004245 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004247set_ref_in_item(
4248 typval_T *tv,
4249 int copyID,
4250 ht_stack_T **ht_stack,
4251 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004252{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004253 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004254
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004255 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004256 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004257 dict_T *dd = tv->vval.v_dict;
4258
Bram Moolenaara03f2332016-02-06 18:09:59 +01004259 if (dd != NULL && dd->dv_copyID != copyID)
4260 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004261 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004262 dd->dv_copyID = copyID;
4263 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004264 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004265 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4266 }
4267 else
4268 {
4269 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4270 if (newitem == NULL)
4271 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004272 else
4273 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004274 newitem->ht = &dd->dv_hashtab;
4275 newitem->prev = *ht_stack;
4276 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004277 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004278 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004279 }
4280 }
4281 else if (tv->v_type == VAR_LIST)
4282 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004283 list_T *ll = tv->vval.v_list;
4284
Bram Moolenaara03f2332016-02-06 18:09:59 +01004285 if (ll != NULL && ll->lv_copyID != copyID)
4286 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004287 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004288 ll->lv_copyID = copyID;
4289 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004290 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004291 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004292 }
4293 else
4294 {
4295 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004296 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004297 if (newitem == NULL)
4298 abort = TRUE;
4299 else
4300 {
4301 newitem->list = ll;
4302 newitem->prev = *list_stack;
4303 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004304 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004305 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004306 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004307 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004308 else if (tv->v_type == VAR_FUNC)
4309 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004310 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004311 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004312 else if (tv->v_type == VAR_PARTIAL)
4313 {
4314 partial_T *pt = tv->vval.v_partial;
4315 int i;
4316
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004317 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004318 if (pt != NULL)
4319 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004320 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004321
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004322 if (pt->pt_dict != NULL)
4323 {
4324 typval_T dtv;
4325
4326 dtv.v_type = VAR_DICT;
4327 dtv.vval.v_dict = pt->pt_dict;
4328 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4329 }
4330
4331 for (i = 0; i < pt->pt_argc; ++i)
4332 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4333 ht_stack, list_stack);
4334 }
4335 }
4336#ifdef FEAT_JOB_CHANNEL
4337 else if (tv->v_type == VAR_JOB)
4338 {
4339 job_T *job = tv->vval.v_job;
4340 typval_T dtv;
4341
4342 if (job != NULL && job->jv_copyID != copyID)
4343 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004344 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004345 if (job->jv_channel != NULL)
4346 {
4347 dtv.v_type = VAR_CHANNEL;
4348 dtv.vval.v_channel = job->jv_channel;
4349 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4350 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004351 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004352 {
4353 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004354 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004355 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4356 }
4357 }
4358 }
4359 else if (tv->v_type == VAR_CHANNEL)
4360 {
4361 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004362 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004363 typval_T dtv;
4364 jsonq_T *jq;
4365 cbq_T *cq;
4366
4367 if (ch != NULL && ch->ch_copyID != copyID)
4368 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004369 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004370 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004371 {
4372 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4373 jq = jq->jq_next)
4374 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4375 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4376 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004377 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004378 {
4379 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004380 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004381 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4382 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004383 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004384 {
4385 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004386 dtv.vval.v_partial =
4387 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004388 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4389 }
4390 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004391 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004392 {
4393 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004394 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004395 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4396 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004397 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004398 {
4399 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004400 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004401 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4402 }
4403 }
4404 }
4405#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004406 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004407}
4408
Bram Moolenaar8c711452005-01-14 21:53:12 +00004409/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004410 * Return a string with the string representation of a variable.
4411 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004413 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004414 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4415 * stings as "string()", otherwise does not put quotes around strings, as
4416 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004417 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4418 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004419 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004420 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004421 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004422echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004423 typval_T *tv,
4424 char_u **tofree,
4425 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004426 int copyID,
4427 int echo_style,
4428 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004429 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004430{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004431 static int recurse = 0;
4432 char_u *r = NULL;
4433
Bram Moolenaar33570922005-01-25 22:26:29 +00004434 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004435 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004436 if (!did_echo_string_emsg)
4437 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004438 // Only give this message once for a recursive call to avoid
4439 // flooding the user with errors. And stop iterating over lists
4440 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004441 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004442 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004443 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004444 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004445 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004446 }
4447 ++recurse;
4448
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 switch (tv->v_type)
4450 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004451 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004452 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004453 {
4454 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004455 r = tv->vval.v_string;
4456 if (r == NULL)
4457 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004458 }
4459 else
4460 {
4461 *tofree = string_quote(tv->vval.v_string, FALSE);
4462 r = *tofree;
4463 }
4464 break;
4465
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004466 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004467 if (echo_style)
4468 {
4469 *tofree = NULL;
4470 r = tv->vval.v_string;
4471 }
4472 else
4473 {
4474 *tofree = string_quote(tv->vval.v_string, TRUE);
4475 r = *tofree;
4476 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004477 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004478
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004479 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004480 {
4481 partial_T *pt = tv->vval.v_partial;
4482 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004483 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004484 garray_T ga;
4485 int i;
4486 char_u *tf;
4487
4488 ga_init2(&ga, 1, 100);
4489 ga_concat(&ga, (char_u *)"function(");
4490 if (fname != NULL)
4491 {
4492 ga_concat(&ga, fname);
4493 vim_free(fname);
4494 }
4495 if (pt != NULL && pt->pt_argc > 0)
4496 {
4497 ga_concat(&ga, (char_u *)", [");
4498 for (i = 0; i < pt->pt_argc; ++i)
4499 {
4500 if (i > 0)
4501 ga_concat(&ga, (char_u *)", ");
4502 ga_concat(&ga,
4503 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4504 vim_free(tf);
4505 }
4506 ga_concat(&ga, (char_u *)"]");
4507 }
4508 if (pt != NULL && pt->pt_dict != NULL)
4509 {
4510 typval_T dtv;
4511
4512 ga_concat(&ga, (char_u *)", ");
4513 dtv.v_type = VAR_DICT;
4514 dtv.vval.v_dict = pt->pt_dict;
4515 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4516 vim_free(tf);
4517 }
4518 ga_concat(&ga, (char_u *)")");
4519
4520 *tofree = ga.ga_data;
4521 r = *tofree;
4522 break;
4523 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004524
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004525 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004526 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004527 break;
4528
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004529 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004530 if (tv->vval.v_list == NULL)
4531 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004532 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004533 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004534 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004535 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004536 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4537 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004538 {
4539 *tofree = NULL;
4540 r = (char_u *)"[...]";
4541 }
4542 else
4543 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004544 int old_copyID = tv->vval.v_list->lv_copyID;
4545
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004546 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004547 *tofree = list2string(tv, copyID, restore_copyID);
4548 if (restore_copyID)
4549 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004550 r = *tofree;
4551 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004552 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004553
Bram Moolenaar8c711452005-01-14 21:53:12 +00004554 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004555 if (tv->vval.v_dict == NULL)
4556 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004557 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004558 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004559 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004560 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004561 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4562 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004563 {
4564 *tofree = NULL;
4565 r = (char_u *)"{...}";
4566 }
4567 else
4568 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004569 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004570
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004571 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004572 *tofree = dict2string(tv, copyID, restore_copyID);
4573 if (restore_copyID)
4574 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004575 r = *tofree;
4576 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004577 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004578
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004579 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004580 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004581 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004582 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004583 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004584 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004585 break;
4586
Bram Moolenaar835dc632016-02-07 14:27:38 +01004587 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004588 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004589 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004590 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004591 if (composite_val)
4592 {
4593 *tofree = string_quote(r, FALSE);
4594 r = *tofree;
4595 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004596 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004598 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004599#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004600 *tofree = NULL;
4601 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4602 r = numbuf;
4603 break;
4604#endif
4605
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004606 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004607 case VAR_SPECIAL:
4608 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004609 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004610 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004611 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004612
Bram Moolenaar8502c702014-06-17 12:51:16 +02004613 if (--recurse == 0)
4614 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004615 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004616}
4617
4618/*
4619 * Return a string with the string representation of a variable.
4620 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4621 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004622 * Does not put quotes around strings, as ":echo" displays values.
4623 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4624 * May return NULL.
4625 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004626 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004627echo_string(
4628 typval_T *tv,
4629 char_u **tofree,
4630 char_u *numbuf,
4631 int copyID)
4632{
4633 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4634}
4635
4636/*
4637 * Return a string with the string representation of a variable.
4638 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4639 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004640 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004641 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004642 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004643 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004644tv2string(
4645 typval_T *tv,
4646 char_u **tofree,
4647 char_u *numbuf,
4648 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004649{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004650 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651}
4652
4653/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004654 * Return string "str" in ' quotes, doubling ' characters.
4655 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004656 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004657 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004658 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004659string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004660{
Bram Moolenaar33570922005-01-25 22:26:29 +00004661 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004662 char_u *p, *r, *s;
4663
Bram Moolenaar33570922005-01-25 22:26:29 +00004664 len = (function ? 13 : 3);
4665 if (str != NULL)
4666 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004667 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004668 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004669 if (*p == '\'')
4670 ++len;
4671 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004672 s = r = alloc(len);
4673 if (r != NULL)
4674 {
4675 if (function)
4676 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004677 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004678 r += 10;
4679 }
4680 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004681 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004682 if (str != NULL)
4683 for (p = str; *p != NUL; )
4684 {
4685 if (*p == '\'')
4686 *r++ = '\'';
4687 MB_COPY_CHAR(p, r);
4688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004689 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004690 if (function)
4691 *r++ = ')';
4692 *r++ = NUL;
4693 }
4694 return s;
4695}
4696
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004697#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004698/*
4699 * Convert the string "text" to a floating point number.
4700 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4701 * this always uses a decimal point.
4702 * Returns the length of the text that was consumed.
4703 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004705string2float(
4706 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004707 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708{
4709 char *s = (char *)text;
4710 float_T f;
4711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004712 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004713 if (STRNICMP(text, "inf", 3) == 0)
4714 {
4715 *value = INFINITY;
4716 return 3;
4717 }
4718 if (STRNICMP(text, "-inf", 3) == 0)
4719 {
4720 *value = -INFINITY;
4721 return 4;
4722 }
4723 if (STRNICMP(text, "nan", 3) == 0)
4724 {
4725 *value = NAN;
4726 return 3;
4727 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004728 f = strtod(s, &s);
4729 *value = f;
4730 return (int)((char_u *)s - text);
4731}
4732#endif
4733
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004734/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 * Get the value of an environment variable.
4736 * "arg" is pointing to the '$'. It is advanced to after the name.
4737 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004738 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004740 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004741get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742{
4743 char_u *string = NULL;
4744 int len;
4745 int cc;
4746 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004747 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748
4749 ++*arg;
4750 name = *arg;
4751 len = get_env_len(arg);
4752 if (evaluate)
4753 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004754 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004755 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004756
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004757 cc = name[len];
4758 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004759 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004760 string = vim_getenv(name, &mustfree);
4761 if (string != NULL && *string != NUL)
4762 {
4763 if (!mustfree)
4764 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004766 else
4767 {
4768 if (mustfree)
4769 vim_free(string);
4770
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004771 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004772 string = expand_env_save(name - 1);
4773 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004774 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004775 }
4776 name[len] = cc;
4777
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004778 rettv->v_type = VAR_STRING;
4779 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781
4782 return OK;
4783}
4784
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004787 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004789 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004790var2fpos(
4791 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004792 int dollar_lnum, // TRUE when $ is last line
4793 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004795 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004797 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004799 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004800 if (varp->v_type == VAR_LIST)
4801 {
4802 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004803 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004804 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004805 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004806
4807 l = varp->vval.v_list;
4808 if (l == NULL)
4809 return NULL;
4810
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004811 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004812 pos.lnum = list_find_nr(l, 0L, &error);
4813 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004815
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004816 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004817 pos.col = list_find_nr(l, 1L, &error);
4818 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004819 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004820 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004821
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004822 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004823 li = list_find(l, 1L);
4824 if (li != NULL && li->li_tv.v_type == VAR_STRING
4825 && li->li_tv.vval.v_string != NULL
4826 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4827 pos.col = len + 1;
4828
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004829 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004830 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004831 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004832 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004833
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004835 pos.coladd = list_find_nr(l, 2L, &error);
4836 if (error)
4837 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004838
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004839 return &pos;
4840 }
4841
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004842 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004843 if (name == NULL)
4844 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004845 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004847 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004848 {
4849 if (VIsual_active)
4850 return &VIsual;
4851 return &curwin->w_cursor;
4852 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004853 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004855 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4857 return NULL;
4858 return pp;
4859 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004860
Bram Moolenaara5525202006-03-02 22:52:09 +00004861 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004862
Bram Moolenaar477933c2007-07-17 14:32:23 +00004863 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004864 {
4865 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004866 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004867 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004868 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004869 // In silent Ex mode topline is zero, but that's not a valid line
4870 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004871 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004872 return &pos;
4873 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004874 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004875 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004876 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004878 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004879 return &pos;
4880 }
4881 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004882 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004884 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 {
4886 pos.lnum = curbuf->b_ml.ml_line_count;
4887 pos.col = 0;
4888 }
4889 else
4890 {
4891 pos.lnum = curwin->w_cursor.lnum;
4892 pos.col = (colnr_T)STRLEN(ml_get_curline());
4893 }
4894 return &pos;
4895 }
4896 return NULL;
4897}
4898
4899/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004900 * Convert list in "arg" into a position and optional file number.
4901 * When "fnump" is NULL there is no file number, only 3 items.
4902 * Note that the column is passed on as-is, the caller may want to decrement
4903 * it to use 1 for the first column.
4904 * Return FAIL when conversion is not possible, doesn't check the position for
4905 * validity.
4906 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004907 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004908list2fpos(
4909 typval_T *arg,
4910 pos_T *posp,
4911 int *fnump,
4912 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004913{
4914 list_T *l = arg->vval.v_list;
4915 long i = 0;
4916 long n;
4917
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004918 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4919 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004920 if (arg->v_type != VAR_LIST
4921 || l == NULL
4922 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004923 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004924 return FAIL;
4925
4926 if (fnump != NULL)
4927 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004928 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004929 if (n < 0)
4930 return FAIL;
4931 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004932 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004933 *fnump = n;
4934 }
4935
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004936 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004937 if (n < 0)
4938 return FAIL;
4939 posp->lnum = n;
4940
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004941 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004942 if (n < 0)
4943 return FAIL;
4944 posp->col = n;
4945
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004946 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004947 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004948 posp->coladd = 0;
4949 else
4950 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004951
Bram Moolenaar493c1782014-05-28 14:34:46 +02004952 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004953 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004954
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004955 return OK;
4956}
4957
4958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 * Get the length of an environment variable name.
4960 * Advance "arg" to the first character after the name.
4961 * Return 0 for error.
4962 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004963 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004964get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965{
4966 char_u *p;
4967 int len;
4968
4969 for (p = *arg; vim_isIDc(*p); ++p)
4970 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004971 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 return 0;
4973
4974 len = (int)(p - *arg);
4975 *arg = p;
4976 return len;
4977}
4978
4979/*
4980 * Get the length of the name of a function or internal variable.
4981 * "arg" is advanced to the first non-white character after the name.
4982 * Return 0 if something is wrong.
4983 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004984 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004985get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986{
4987 char_u *p;
4988 int len;
4989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004990 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004992 {
4993 if (*p == ':')
4994 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004995 // "s:" is start of "s:var", but "n:" is not and can be used in
4996 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004997 len = (int)(p - *arg);
4998 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
4999 || len > 1)
5000 break;
5001 }
5002 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005003 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 return 0;
5005
5006 len = (int)(p - *arg);
5007 *arg = skipwhite(p);
5008
5009 return len;
5010}
5011
5012/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005013 * Get the length of the name of a variable or function.
5014 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005016 * Return -1 if curly braces expansion failed.
5017 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 * If the name contains 'magic' {}'s, expand them and return the
5019 * expanded name in an allocated string via 'alias' - caller must free.
5020 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005022get_name_len(
5023 char_u **arg,
5024 char_u **alias,
5025 int evaluate,
5026 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 char_u *p;
5030 char_u *expr_start;
5031 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005033 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034
5035 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5036 && (*arg)[2] == (int)KE_SNR)
5037 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005038 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 *arg += 3;
5040 return get_id_len(arg) + 3;
5041 }
5042 len = eval_fname_script(*arg);
5043 if (len > 0)
5044 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005045 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *arg += len;
5047 }
5048
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005050 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005052 p = find_name_end(*arg, &expr_start, &expr_end,
5053 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 if (expr_start != NULL)
5055 {
5056 char_u *temp_string;
5057
5058 if (!evaluate)
5059 {
5060 len += (int)(p - *arg);
5061 *arg = skipwhite(p);
5062 return len;
5063 }
5064
5065 /*
5066 * Include any <SID> etc in the expanded string:
5067 * Thus the -len here.
5068 */
5069 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5070 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005071 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 *alias = temp_string;
5073 *arg = skipwhite(p);
5074 return (int)STRLEN(temp_string);
5075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076
5077 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005078 // Only give an error when there is something, otherwise it will be
5079 // reported at a higher level.
5080 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005081 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082
5083 return len;
5084}
5085
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005086/*
5087 * Find the end of a variable or function name, taking care of magic braces.
5088 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5089 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005090 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005091 * Return a pointer to just after the name. Equal to "arg" if there is no
5092 * valid name.
5093 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005094 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005095find_name_end(
5096 char_u *arg,
5097 char_u **expr_start,
5098 char_u **expr_end,
5099 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005101 int mb_nest = 0;
5102 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005104 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005105 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005107 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005109 *expr_start = NULL;
5110 *expr_end = NULL;
5111 }
5112
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005113 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005114 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5115 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005116 return arg;
5117
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005118 for (p = arg; *p != NUL
5119 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005120 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005121 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005123 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005124 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005125 if (*p == '\'')
5126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005127 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005128 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005129 ;
5130 if (*p == NUL)
5131 break;
5132 }
5133 else if (*p == '"')
5134 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005135 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005136 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005137 if (*p == '\\' && p[1] != NUL)
5138 ++p;
5139 if (*p == NUL)
5140 break;
5141 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005142 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5143 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005144 // "s:" is start of "s:var", but "n:" is not and can be used in
5145 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005146 len = (int)(p - arg);
5147 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005148 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005149 break;
5150 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005151
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005152 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005154 if (*p == '[')
5155 ++br_nest;
5156 else if (*p == ']')
5157 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005159
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005160 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005162 if (*p == '{')
5163 {
5164 mb_nest++;
5165 if (expr_start != NULL && *expr_start == NULL)
5166 *expr_start = p;
5167 }
5168 else if (*p == '}')
5169 {
5170 mb_nest--;
5171 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5172 *expr_end = p;
5173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 }
5176
5177 return p;
5178}
5179
5180/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005181 * Expands out the 'magic' {}'s in a variable/function name.
5182 * Note that this can call itself recursively, to deal with
5183 * constructs like foo{bar}{baz}{bam}
5184 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5185 * "in_start" ^
5186 * "expr_start" ^
5187 * "expr_end" ^
5188 * "in_end" ^
5189 *
5190 * Returns a new allocated string, which the caller must free.
5191 * Returns NULL for failure.
5192 */
5193 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005194make_expanded_name(
5195 char_u *in_start,
5196 char_u *expr_start,
5197 char_u *expr_end,
5198 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005199{
5200 char_u c1;
5201 char_u *retval = NULL;
5202 char_u *temp_result;
5203 char_u *nextcmd = NULL;
5204
5205 if (expr_end == NULL || in_end == NULL)
5206 return NULL;
5207 *expr_start = NUL;
5208 *expr_end = NUL;
5209 c1 = *in_end;
5210 *in_end = NUL;
5211
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005212 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005213 if (temp_result != NULL && nextcmd == NULL)
5214 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005215 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5216 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005217 if (retval != NULL)
5218 {
5219 STRCPY(retval, in_start);
5220 STRCAT(retval, temp_result);
5221 STRCAT(retval, expr_end + 1);
5222 }
5223 }
5224 vim_free(temp_result);
5225
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005226 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005227 *expr_start = '{';
5228 *expr_end = '}';
5229
5230 if (retval != NULL)
5231 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005232 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005233 if (expr_start != NULL)
5234 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005235 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 temp_result = make_expanded_name(retval, expr_start,
5237 expr_end, temp_result);
5238 vim_free(retval);
5239 retval = temp_result;
5240 }
5241 }
5242
5243 return retval;
5244}
5245
5246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005248 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005251eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005253 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5254}
5255
5256/*
5257 * Return TRUE if character "c" can be used as the first character in a
5258 * variable or function name (excluding '{' and '}').
5259 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005260 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005261eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005262{
5263 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264}
5265
5266/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005267 * Handle:
5268 * - expr[expr], expr[expr:expr] subscript
5269 * - ".name" lookup
5270 * - function call with Funcref variable: func(expr)
5271 * - method call: var->method()
5272 *
5273 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005276handle_subscript(
5277 char_u **arg,
5278 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005279 int evaluate, // do more than finding the end
5280 int verbose, // give error messages
5281 char_u *start_leader, // start of '!' and '-' prefixes
5282 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005283{
5284 int ret = OK;
5285 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286
Bram Moolenaar61343f02019-07-20 21:11:13 +02005287 // "." is ".name" lookup when we found a dict or when evaluating and
5288 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005289 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005290 && (((**arg == '['
5291 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005292 || (!evaluate
5293 && (*arg)[1] != '.'
5294 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005295 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005296 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005297 && !VIM_ISWHITE(*(*arg - 1)))
5298 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299 {
5300 if (**arg == '(')
5301 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005302 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005303
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005304 // Stop the expression evaluation when immediately aborting on
5305 // error, or when an interrupt occurred or an exception was thrown
5306 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005307 if (aborting())
5308 {
5309 if (ret == OK)
5310 clear_tv(rettv);
5311 ret = FAIL;
5312 }
5313 dict_unref(selfdict);
5314 selfdict = NULL;
5315 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005316 else if (**arg == '-')
5317 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005318 // Expression "-1.0->method()" applies the leader "-" before
5319 // applying ->.
5320 if (evaluate && *end_leaderp > start_leader)
5321 ret = eval7_leader(rettv, start_leader, end_leaderp);
5322 if (ret == OK)
5323 {
5324 if ((*arg)[2] == '{')
5325 // expr->{lambda}()
5326 ret = eval_lambda(arg, rettv, evaluate, verbose);
5327 else
5328 // expr->name()
5329 ret = eval_method(arg, rettv, evaluate, verbose);
5330 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005331 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005332 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005333 {
5334 dict_unref(selfdict);
5335 if (rettv->v_type == VAR_DICT)
5336 {
5337 selfdict = rettv->vval.v_dict;
5338 if (selfdict != NULL)
5339 ++selfdict->dv_refcount;
5340 }
5341 else
5342 selfdict = NULL;
5343 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5344 {
5345 clear_tv(rettv);
5346 ret = FAIL;
5347 }
5348 }
5349 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005350
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005351 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5352 // Don't do this when "Func" is already a partial that was bound
5353 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005354 if (selfdict != NULL
5355 && (rettv->v_type == VAR_FUNC
5356 || (rettv->v_type == VAR_PARTIAL
5357 && (rettv->vval.v_partial->pt_auto
5358 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005359 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005360
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005361 dict_unref(selfdict);
5362 return ret;
5363}
5364
5365/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005366 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 * value).
5368 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005369 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005370alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005371{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005372 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005373}
5374
5375/*
5376 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 * The string "s" must have been allocated, it is consumed.
5378 * Return NULL for out of memory, the variable otherwise.
5379 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005380 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005381alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382{
Bram Moolenaar33570922005-01-25 22:26:29 +00005383 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005385 rettv = alloc_tv();
5386 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 rettv->v_type = VAR_STRING;
5389 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 }
5391 else
5392 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005393 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394}
5395
5396/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005399 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005400free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401{
5402 if (varp != NULL)
5403 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 switch (varp->v_type)
5405 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005407 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005408 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005409 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005410 vim_free(varp->vval.v_string);
5411 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005412 case VAR_PARTIAL:
5413 partial_unref(varp->vval.v_partial);
5414 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005415 case VAR_BLOB:
5416 blob_unref(varp->vval.v_blob);
5417 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 case VAR_LIST:
5419 list_unref(varp->vval.v_list);
5420 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005421 case VAR_DICT:
5422 dict_unref(varp->vval.v_dict);
5423 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005424 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005425#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005426 job_unref(varp->vval.v_job);
5427 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005428#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005429 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005430#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005431 channel_unref(varp->vval.v_channel);
5432 break;
5433#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005434 case VAR_NUMBER:
5435 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005436 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005437 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005438 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005439 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005440 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005441 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 vim_free(varp);
5444 }
5445}
5446
5447/*
5448 * Free the memory for a variable value and set the value to NULL or 0.
5449 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005451clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452{
5453 if (varp != NULL)
5454 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005455 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005457 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005458 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005460 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005461 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005463 case VAR_PARTIAL:
5464 partial_unref(varp->vval.v_partial);
5465 varp->vval.v_partial = NULL;
5466 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005467 case VAR_BLOB:
5468 blob_unref(varp->vval.v_blob);
5469 varp->vval.v_blob = NULL;
5470 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 case VAR_LIST:
5472 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005473 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 case VAR_DICT:
5476 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005477 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005479 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005480 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005481 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005482 varp->vval.v_number = 0;
5483 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005484 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005486 varp->vval.v_float = 0.0;
5487 break;
5488#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005489 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005490#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005491 job_unref(varp->vval.v_job);
5492 varp->vval.v_job = NULL;
5493#endif
5494 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005495 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005496#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005497 channel_unref(varp->vval.v_channel);
5498 varp->vval.v_channel = NULL;
5499#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005501 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005502 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005505 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
5507}
5508
5509/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005510 * Set the value of a variable to NULL without freeing items.
5511 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005512 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005513init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005514{
5515 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005516 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517}
5518
5519/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 * Get the number value of a variable.
5521 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005522 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005523 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005524 * caller of incompatible types: it sets *denote to TRUE if "denote"
5525 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005527 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005528tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005530 int error = FALSE;
5531
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005532 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005533}
5534
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005535 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005536tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005537{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005538 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005540 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005542 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005543 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005544 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005545#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005546 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005547 break;
5548#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005550 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005551 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 break;
5553 case VAR_STRING:
5554 if (varp->vval.v_string != NULL)
5555 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005556 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005557 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005558 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005559 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005560 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005561 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005562 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005563 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005564 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005565 case VAR_SPECIAL:
5566 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005567 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005568#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005569 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005570 break;
5571#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005572 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005573#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005574 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005575 break;
5576#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005577 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005578 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005579 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005580 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005581 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005582 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005583 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005586 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005587 n = -1;
5588 else
5589 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005593#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005594 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005595tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005596{
5597 switch (varp->v_type)
5598 {
5599 case VAR_NUMBER:
5600 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005601 case VAR_FLOAT:
5602 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005603 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005604 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005605 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005606 break;
5607 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005608 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005609 break;
5610 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005611 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005612 break;
5613 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005615 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005616 case VAR_BOOL:
5617 emsg(_("E362: Using a boolean value as a Float"));
5618 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005619 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005620 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005621 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005622 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005623# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005624 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005625 break;
5626# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005627 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005628# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005629 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005630 break;
5631# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005632 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005633 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005634 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005635 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005636 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005637 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005638 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005639 break;
5640 }
5641 return 0;
5642}
5643#endif
5644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646 * Get the string value of a variable.
5647 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005648 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5649 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 * If the String variable has never been set, return an empty string.
5651 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005652 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005653 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005655 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005656tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657{
5658 static char_u mybuf[NUMBUFLEN];
5659
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005660 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661}
5662
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005663 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005664tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005666 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005667
5668 return res != NULL ? res : (char_u *)"";
5669}
5670
Bram Moolenaar7d647822014-04-05 21:28:56 +02005671/*
5672 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5673 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005674 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005675tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005676{
5677 static char_u mybuf[NUMBUFLEN];
5678
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005679 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005680}
5681
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005682 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005683tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005684{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005685 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005687 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005688 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005689 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005690 return buf;
5691 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005692 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005693 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005694 break;
5695 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005696 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005697 break;
5698 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005699 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005700 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005701 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005702#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005703 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005704 break;
5705#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005706 case VAR_STRING:
5707 if (varp->vval.v_string != NULL)
5708 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005709 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005710 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005711 case VAR_SPECIAL:
5712 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5713 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005714 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005715 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005716 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005717 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005718#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005719 {
5720 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005721 char *status;
5722
5723 if (job == NULL)
5724 return (char_u *)"no process";
5725 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005726 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005727 : "run";
5728# ifdef UNIX
5729 vim_snprintf((char *)buf, NUMBUFLEN,
5730 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005731# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005732 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005733 "process %ld %s",
5734 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005735 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005736# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005737 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005738 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5739# endif
5740 return buf;
5741 }
5742#endif
5743 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005744 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005745#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005746 {
5747 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005748 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005749
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005750 if (channel == NULL)
5751 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5752 else
5753 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005754 "channel %d %s", channel->ch_id, status);
5755 return buf;
5756 }
5757#endif
5758 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005759 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005760 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005761 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005762 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005763 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005765 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766}
5767
5768/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005769 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5770 * string() on Dict, List, etc.
5771 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005772 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005773tv_stringify(typval_T *varp, char_u *buf)
5774{
5775 if (varp->v_type == VAR_LIST
5776 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005777 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005778 || varp->v_type == VAR_FUNC
5779 || varp->v_type == VAR_PARTIAL
5780 || varp->v_type == VAR_FLOAT)
5781 {
5782 typval_T tmp;
5783
5784 f_string(varp, &tmp);
5785 tv_get_string_buf(&tmp, buf);
5786 clear_tv(varp);
5787 *varp = tmp;
5788 return tmp.vval.v_string;
5789 }
5790 return tv_get_string_buf(varp, buf);
5791}
5792
5793/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005794 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5795 * Also give an error message, using "name" or _("name") when use_gettext is
5796 * TRUE.
5797 */
5798 static int
5799tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5800{
5801 int lock = 0;
5802
5803 switch (tv->v_type)
5804 {
5805 case VAR_BLOB:
5806 if (tv->vval.v_blob != NULL)
5807 lock = tv->vval.v_blob->bv_lock;
5808 break;
5809 case VAR_LIST:
5810 if (tv->vval.v_list != NULL)
5811 lock = tv->vval.v_list->lv_lock;
5812 break;
5813 case VAR_DICT:
5814 if (tv->vval.v_dict != NULL)
5815 lock = tv->vval.v_dict->dv_lock;
5816 break;
5817 default:
5818 break;
5819 }
5820 return var_check_lock(tv->v_lock, name, use_gettext)
5821 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5822}
5823
5824/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005825 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005826 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005827 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005828 * It is OK for "from" and "to" to point to the same item. This is used to
5829 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005830 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005831 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005832copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005835 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 switch (from->v_type)
5837 {
5838 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005839 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005840 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005841 to->vval.v_number = from->vval.v_number;
5842 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005843 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005844#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005845 to->vval.v_float = from->vval.v_float;
5846 break;
5847#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005848 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005849#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005850 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005851 if (to->vval.v_job != NULL)
5852 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 break;
5854#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005855 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005856#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005857 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005858 if (to->vval.v_channel != NULL)
5859 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005860 break;
5861#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 case VAR_STRING:
5863 case VAR_FUNC:
5864 if (from->vval.v_string == NULL)
5865 to->vval.v_string = NULL;
5866 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005867 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005868 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005869 if (from->v_type == VAR_FUNC)
5870 func_ref(to->vval.v_string);
5871 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005873 case VAR_PARTIAL:
5874 if (from->vval.v_partial == NULL)
5875 to->vval.v_partial = NULL;
5876 else
5877 {
5878 to->vval.v_partial = from->vval.v_partial;
5879 ++to->vval.v_partial->pt_refcount;
5880 }
5881 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005882 case VAR_BLOB:
5883 if (from->vval.v_blob == NULL)
5884 to->vval.v_blob = NULL;
5885 else
5886 {
5887 to->vval.v_blob = from->vval.v_blob;
5888 ++to->vval.v_blob->bv_refcount;
5889 }
5890 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005891 case VAR_LIST:
5892 if (from->vval.v_list == NULL)
5893 to->vval.v_list = NULL;
5894 else
5895 {
5896 to->vval.v_list = from->vval.v_list;
5897 ++to->vval.v_list->lv_refcount;
5898 }
5899 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005900 case VAR_DICT:
5901 if (from->vval.v_dict == NULL)
5902 to->vval.v_dict = NULL;
5903 else
5904 {
5905 to->vval.v_dict = from->vval.v_dict;
5906 ++to->vval.v_dict->dv_refcount;
5907 }
5908 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005909 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005910 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005912 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913 break;
5914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915}
5916
5917/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005918 * Make a copy of an item.
5919 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005920 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5921 * reference to an already copied list/dict can be used.
5922 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005923 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005925item_copy(
5926 typval_T *from,
5927 typval_T *to,
5928 int deep,
5929 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005930{
5931 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005932 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933
Bram Moolenaar33570922005-01-25 22:26:29 +00005934 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005936 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005937 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 }
5939 ++recurse;
5940
5941 switch (from->v_type)
5942 {
5943 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005944 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005945 case VAR_STRING:
5946 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005947 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005948 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005949 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005950 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005951 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005952 copy_tv(from, to);
5953 break;
5954 case VAR_LIST:
5955 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005956 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005957 if (from->vval.v_list == NULL)
5958 to->vval.v_list = NULL;
5959 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5960 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005961 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005962 to->vval.v_list = from->vval.v_list->lv_copylist;
5963 ++to->vval.v_list->lv_refcount;
5964 }
5965 else
5966 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5967 if (to->vval.v_list == NULL)
5968 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005969 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005970 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005971 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005972 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973 case VAR_DICT:
5974 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005975 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005976 if (from->vval.v_dict == NULL)
5977 to->vval.v_dict = NULL;
5978 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5979 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005980 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005981 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5982 ++to->vval.v_dict->dv_refcount;
5983 }
5984 else
5985 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5986 if (to->vval.v_dict == NULL)
5987 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005988 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005989 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005990 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005991 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005992 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005993 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005994 }
5995 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005996 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997}
5998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005999 void
6000echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6001{
6002 char_u *tofree;
6003 char_u numbuf[NUMBUFLEN];
6004 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6005
6006 if (*atstart)
6007 {
6008 *atstart = FALSE;
6009 // Call msg_start() after eval1(), evaluating the expression
6010 // may cause a message to appear.
6011 if (with_space)
6012 {
6013 // Mark the saved text as finishing the line, so that what
6014 // follows is displayed on a new line when scrolling back
6015 // at the more prompt.
6016 msg_sb_eol();
6017 msg_start();
6018 }
6019 }
6020 else if (with_space)
6021 msg_puts_attr(" ", echo_attr);
6022
6023 if (p != NULL)
6024 for ( ; *p != NUL && !got_int; ++p)
6025 {
6026 if (*p == '\n' || *p == '\r' || *p == TAB)
6027 {
6028 if (*p != TAB && *needclr)
6029 {
6030 // remove any text still there from the command
6031 msg_clr_eos();
6032 *needclr = FALSE;
6033 }
6034 msg_putchar_attr(*p, echo_attr);
6035 }
6036 else
6037 {
6038 if (has_mbyte)
6039 {
6040 int i = (*mb_ptr2len)(p);
6041
6042 (void)msg_outtrans_len_attr(p, i, echo_attr);
6043 p += i - 1;
6044 }
6045 else
6046 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6047 }
6048 }
6049 vim_free(tofree);
6050}
6051
Bram Moolenaare9a41262005-01-15 22:18:47 +00006052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 * ":echo expr1 ..." print each argument separated with a space, add a
6054 * newline at the end.
6055 * ":echon expr1 ..." print each argument plain.
6056 */
6057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006058ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062 char_u *p;
6063 int needclr = TRUE;
6064 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006065 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006066 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068 if (eap->skip)
6069 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006070 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006072 // If eval1() causes an error message the text from the command may
6073 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006074 need_clr_eos = needclr;
6075
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006077 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 {
6079 /*
6080 * Report the invalid expression unless the expression evaluation
6081 * has been cancelled due to an aborting error, an interrupt, or an
6082 * exception.
6083 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006084 if (!aborting() && did_emsg == did_emsg_before
6085 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006086 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006087 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 break;
6089 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006090 need_clr_eos = FALSE;
6091
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006093 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006094
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006095 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 arg = skipwhite(arg);
6097 }
6098 eap->nextcmd = check_nextcmd(arg);
6099
6100 if (eap->skip)
6101 --emsg_skip;
6102 else
6103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006104 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 if (needclr)
6106 msg_clr_eos();
6107 if (eap->cmdidx == CMD_echo)
6108 msg_end();
6109 }
6110}
6111
6112/*
6113 * ":echohl {name}".
6114 */
6115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006116ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006118 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119}
6120
6121/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006122 * Returns the :echo attribute
6123 */
6124 int
6125get_echo_attr(void)
6126{
6127 return echo_attr;
6128}
6129
6130/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 * ":execute expr1 ..." execute the result of an expression.
6132 * ":echomsg expr1 ..." Print a message
6133 * ":echoerr expr1 ..." Print an error
6134 * Each gets spaces around each argument and a newline at the end for
6135 * echo commands
6136 */
6137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139{
6140 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006141 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 int ret = OK;
6143 char_u *p;
6144 garray_T ga;
6145 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146
6147 ga_init2(&ga, 1, 80);
6148
6149 if (eap->skip)
6150 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006151 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006152 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006153 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6154 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 if (!eap->skip)
6158 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006159 char_u buf[NUMBUFLEN];
6160
6161 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006162 {
6163 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6164 {
6165 emsg(_(e_inval_string));
6166 p = NULL;
6167 }
6168 else
6169 p = tv_get_string_buf(&rettv, buf);
6170 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006171 else
6172 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006173 if (p == NULL)
6174 {
6175 clear_tv(&rettv);
6176 ret = FAIL;
6177 break;
6178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006179 len = (int)STRLEN(p);
6180 if (ga_grow(&ga, len + 2) == FAIL)
6181 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 ret = FAIL;
6184 break;
6185 }
6186 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006189 ga.ga_len += len;
6190 }
6191
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 arg = skipwhite(arg);
6194 }
6195
6196 if (ret != FAIL && ga.ga_data != NULL)
6197 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006198 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006200 // Mark the already saved text as finishing the line, so that what
6201 // follows is displayed on a new line when scrolling back at the
6202 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006203 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006204 }
6205
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006207 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006208 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006209 out_flush();
6210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006211 else if (eap->cmdidx == CMD_echoerr)
6212 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006213 int save_did_emsg = did_emsg;
6214
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006215 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006216 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (!force_abort)
6218 did_emsg = save_did_emsg;
6219 }
6220 else if (eap->cmdidx == CMD_execute)
6221 do_cmdline((char_u *)ga.ga_data,
6222 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6223 }
6224
6225 ga_clear(&ga);
6226
6227 if (eap->skip)
6228 --emsg_skip;
6229
6230 eap->nextcmd = check_nextcmd(arg);
6231}
6232
6233/*
6234 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6235 * "arg" points to the "&" or '+' when called, to "option" when returning.
6236 * Returns NULL when no option name found. Otherwise pointer to the char
6237 * after the option name.
6238 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006239 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006240find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006241{
6242 char_u *p = *arg;
6243
6244 ++p;
6245 if (*p == 'g' && p[1] == ':')
6246 {
6247 *opt_flags = OPT_GLOBAL;
6248 p += 2;
6249 }
6250 else if (*p == 'l' && p[1] == ':')
6251 {
6252 *opt_flags = OPT_LOCAL;
6253 p += 2;
6254 }
6255 else
6256 *opt_flags = 0;
6257
6258 if (!ASCII_ISALPHA(*p))
6259 return NULL;
6260 *arg = p;
6261
6262 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006263 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 else
6265 while (ASCII_ISALPHA(*p))
6266 ++p;
6267 return p;
6268}
6269
6270/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006271 * Display script name where an item was last set.
6272 * Should only be invoked when 'verbose' is non-zero.
6273 */
6274 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006275last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006276{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006277 char_u *p;
6278
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006279 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006280 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006281 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006282 if (p != NULL)
6283 {
6284 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006285 msg_puts(_("\n\tLast set from "));
6286 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006287 if (script_ctx.sc_lnum > 0)
6288 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006289 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006290 msg_outnum((long)script_ctx.sc_lnum);
6291 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006292 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006293 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006294 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006295 }
6296}
6297
Bram Moolenaar61be3762019-03-19 23:04:17 +01006298/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006299 * Compare "typ1" and "typ2". Put the result in "typ1".
6300 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006301 int
6302typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006303 typval_T *typ1, // first operand
6304 typval_T *typ2, // second operand
6305 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006306 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006307{
6308 int i;
6309 varnumber_T n1, n2;
6310 char_u *s1, *s2;
6311 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006312 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006313
Bram Moolenaar31988702018-02-13 12:57:42 +01006314 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006315 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006316 // For "is" a different type always means FALSE, for "notis"
6317 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006318 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006319 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006320 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6321 {
6322 if (type_is)
6323 {
6324 n1 = (typ1->v_type == typ2->v_type
6325 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006326 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006327 n1 = !n1;
6328 }
6329 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006330 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006331 {
6332 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006333 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006334 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006335 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006336 clear_tv(typ1);
6337 return FAIL;
6338 }
6339 else
6340 {
6341 // Compare two Blobs for being equal or unequal.
6342 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006343 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006344 n1 = !n1;
6345 }
6346 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006347 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6348 {
6349 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006350 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006351 n1 = (typ1->v_type == typ2->v_type
6352 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006353 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006354 n1 = !n1;
6355 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006356 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006357 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006358 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006359 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006360 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006361 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006362 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006363 clear_tv(typ1);
6364 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006365 }
6366 else
6367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006368 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006369 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6370 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006371 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006372 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006373 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006374 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006375
6376 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6377 {
6378 if (type_is)
6379 {
6380 n1 = (typ1->v_type == typ2->v_type
6381 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006382 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006383 n1 = !n1;
6384 }
6385 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006386 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006387 {
6388 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006389 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006390 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006391 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006392 clear_tv(typ1);
6393 return FAIL;
6394 }
6395 else
6396 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006397 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006398 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6399 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006400 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006401 n1 = !n1;
6402 }
6403 }
6404
6405 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6406 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6407 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006408 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6409 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006411 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006412 clear_tv(typ1);
6413 return FAIL;
6414 }
6415 if ((typ1->v_type == VAR_PARTIAL
6416 && typ1->vval.v_partial == NULL)
6417 || (typ2->v_type == VAR_PARTIAL
6418 && typ2->vval.v_partial == NULL))
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006419 // when a partial is NULL assume not equal
Bram Moolenaar31988702018-02-13 12:57:42 +01006420 n1 = FALSE;
6421 else if (type_is)
6422 {
6423 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006424 // strings are considered the same if their value is
6425 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006426 n1 = tv_equal(typ1, typ2, ic, FALSE);
6427 else if (typ1->v_type == VAR_PARTIAL
6428 && typ2->v_type == VAR_PARTIAL)
6429 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6430 else
6431 n1 = FALSE;
6432 }
6433 else
6434 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006435 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006436 n1 = !n1;
6437 }
6438
6439#ifdef FEAT_FLOAT
6440 /*
6441 * If one of the two variables is a float, compare as a float.
6442 * When using "=~" or "!~", always compare as string.
6443 */
6444 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006445 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006446 {
6447 float_T f1, f2;
6448
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006449 f1 = tv_get_float(typ1);
6450 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006451 n1 = FALSE;
6452 switch (type)
6453 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006454 case EXPR_IS:
6455 case EXPR_EQUAL: n1 = (f1 == f2); break;
6456 case EXPR_ISNOT:
6457 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6458 case EXPR_GREATER: n1 = (f1 > f2); break;
6459 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6460 case EXPR_SMALLER: n1 = (f1 < f2); break;
6461 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6462 case EXPR_UNKNOWN:
6463 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006464 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006465 }
6466 }
6467#endif
6468
6469 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006470 * If one of the two variables is a number, compare as a number.
6471 * When using "=~" or "!~", always compare as string.
6472 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006473 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006474 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006475 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006476 n1 = tv_get_number(typ1);
6477 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006478 switch (type)
6479 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006480 case EXPR_IS:
6481 case EXPR_EQUAL: n1 = (n1 == n2); break;
6482 case EXPR_ISNOT:
6483 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6484 case EXPR_GREATER: n1 = (n1 > n2); break;
6485 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6486 case EXPR_SMALLER: n1 = (n1 < n2); break;
6487 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6488 case EXPR_UNKNOWN:
6489 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006490 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006491 }
6492 }
6493 else
6494 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006495 s1 = tv_get_string_buf(typ1, buf1);
6496 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006497 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006498 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6499 else
6500 i = 0;
6501 n1 = FALSE;
6502 switch (type)
6503 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006504 case EXPR_IS:
6505 case EXPR_EQUAL: n1 = (i == 0); break;
6506 case EXPR_ISNOT:
6507 case EXPR_NEQUAL: n1 = (i != 0); break;
6508 case EXPR_GREATER: n1 = (i > 0); break;
6509 case EXPR_GEQUAL: n1 = (i >= 0); break;
6510 case EXPR_SMALLER: n1 = (i < 0); break;
6511 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006512
Bram Moolenaar87396072019-12-31 22:36:18 +01006513 case EXPR_MATCH:
6514 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006515 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006516 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006517 n1 = !n1;
6518 break;
6519
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006520 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006521 }
6522 }
6523 clear_tv(typ1);
6524 typ1->v_type = VAR_NUMBER;
6525 typ1->vval.v_number = n1;
6526
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006527 return OK;
6528}
6529
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006530 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006531typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006532{
6533 char_u *tofree;
6534 char_u numbuf[NUMBUFLEN];
6535 char_u *ret = NULL;
6536
6537 if (arg == NULL)
6538 return vim_strsave((char_u *)"(does not exist)");
6539 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006540 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006541 if (ret != NULL && tofree == NULL)
6542 ret = vim_strsave(ret);
6543 return ret;
6544}
6545
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006546#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547
6548/*
6549 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006550 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 * "flags" can be "g" to do a global substitute.
6552 * Returns an allocated string, NULL for error.
6553 */
6554 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555do_string_sub(
6556 char_u *str,
6557 char_u *pat,
6558 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006559 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006560 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561{
6562 int sublen;
6563 regmatch_T regmatch;
6564 int i;
6565 int do_all;
6566 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006567 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 garray_T ga;
6569 char_u *ret;
6570 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006571 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006573 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006575 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
6577 ga_init2(&ga, 1, 200);
6578
6579 do_all = (flags[0] == 'g');
6580
6581 regmatch.rm_ic = p_ic;
6582 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6583 if (regmatch.regprog != NULL)
6584 {
6585 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006586 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6588 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006589 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006590 if (regmatch.startp[0] == regmatch.endp[0])
6591 {
6592 if (zero_width == regmatch.startp[0])
6593 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006594 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006595 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006596 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6597 (size_t)i);
6598 ga.ga_len += i;
6599 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006600 continue;
6601 }
6602 zero_width = regmatch.startp[0];
6603 }
6604
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605 /*
6606 * Get some space for a temporary buffer to do the substitution
6607 * into. It will contain:
6608 * - The text up to where the match is.
6609 * - The substituted text.
6610 * - The text after the match.
6611 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006612 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006613 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6615 {
6616 ga_clear(&ga);
6617 break;
6618 }
6619
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006620 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 i = (int)(regmatch.startp[0] - tail);
6622 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006623 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006624 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 + ga.ga_len + i, TRUE, TRUE, FALSE);
6626 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006627 tail = regmatch.endp[0];
6628 if (*tail == NUL)
6629 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006630 if (!do_all)
6631 break;
6632 }
6633
6634 if (ga.ga_data != NULL)
6635 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6636
Bram Moolenaar473de612013-06-08 18:19:48 +02006637 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638 }
6639
6640 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6641 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006642 if (p_cpo == empty_option)
6643 p_cpo = save_cpo;
6644 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006645 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006646 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006647
6648 return ret;
6649}