blob: 659588f1d7316126726e651204ef4c1c45340069 [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 Moolenaar9d8d0b52020-04-24 22:47:31 +0200244 if (partial == NULL)
245 return FAIL;
246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 if (partial->pt_func != NULL && partial->pt_func->uf_dfunc_idx >= 0)
248 {
249 if (call_def_function(partial->pt_func, argc, argv, rettv) == FAIL)
250 return FAIL;
251 }
252 else
253 {
254 s = partial_name(partial);
255 if (s == NULL || *s == NUL)
256 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200257 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 funcexe.evaluate = TRUE;
259 funcexe.partial = partial;
260 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
261 return FAIL;
262 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100263 }
264 else
265 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100266 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100267 if (s == NULL)
268 return FAIL;
269 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100270 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100271 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100272 if (*s != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100273 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200274 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100275 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100276 return FAIL;
277 }
278 }
279 return OK;
280}
281
282/*
283 * Like eval_to_bool() but using a typval_T instead of a string.
284 * Works for string, funcref and partial.
285 */
286 int
287eval_expr_to_bool(typval_T *expr, int *error)
288{
289 typval_T rettv;
290 int res;
291
292 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
293 {
294 *error = TRUE;
295 return FALSE;
296 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100297 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100298 clear_tv(&rettv);
299 return res;
300}
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302/*
303 * Top level evaluation function, returning a string. If "skip" is TRUE,
304 * only parsing to "nextcmd" is done, without reporting errors. Return
305 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
306 */
307 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100308eval_to_string_skip(
309 char_u *arg,
310 char_u **nextcmd,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100311 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312{
Bram Moolenaar33570922005-01-25 22:26:29 +0000313 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 char_u *retval;
315
316 if (skip)
317 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000318 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000319 retval = NULL;
320 else
321 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100322 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000323 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 }
325 if (skip)
326 --emsg_skip;
327
328 return retval;
329}
330
331/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000332 * Skip over an expression at "*pp".
333 * Return FAIL for an error, OK otherwise.
334 */
335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100336skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000337{
Bram Moolenaar33570922005-01-25 22:26:29 +0000338 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000339
340 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000341 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000342}
343
344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000346 * When "convert" is TRUE convert a List into a sequence of lines and convert
347 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348 * Return pointer to allocated memory, or NULL for failure.
349 */
350 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100351eval_to_string(
352 char_u *arg,
353 char_u **nextcmd,
354 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000355{
Bram Moolenaar33570922005-01-25 22:26:29 +0000356 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000358 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000359#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000360 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000363 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000364 retval = NULL;
365 else
366 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000367 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000368 {
369 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000370 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200371 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200372 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200373 if (tv.vval.v_list->lv_len > 0)
374 ga_append(&ga, NL);
375 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000376 ga_append(&ga, NUL);
377 retval = (char_u *)ga.ga_data;
378 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000379#ifdef FEAT_FLOAT
380 else if (convert && tv.v_type == VAR_FLOAT)
381 {
382 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
383 retval = vim_strsave(numbuf);
384 }
385#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000386 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100387 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000388 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 }
390
391 return retval;
392}
393
394/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000395 * Call eval_to_string() without using current local variables and using
396 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100399eval_to_string_safe(
400 char_u *arg,
401 char_u **nextcmd,
402 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403{
404 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200405 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200407 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000408 if (use_sandbox)
409 ++sandbox;
410 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000411 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000412 if (use_sandbox)
413 --sandbox;
414 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200415 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000416 return retval;
417}
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419/*
420 * Top level evaluation function, returning a number.
421 * Evaluates "expr" silently.
422 * Returns -1 for an error.
423 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200424 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100425eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426{
Bram Moolenaar33570922005-01-25 22:26:29 +0000427 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200428 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000429 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431 ++emsg_off;
432
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000433 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 retval = -1;
435 else
436 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100437 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000438 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000439 }
440 --emsg_off;
441
442 return retval;
443}
444
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000445/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000446 * Top level evaluation function.
447 * Returns an allocated typval_T with the result.
448 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000449 */
450 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100451eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000452{
453 typval_T *tv;
454
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200455 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000456 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100457 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000458
459 return tv;
460}
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100463 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200464 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
465 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000466 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000467 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100469call_vim_function(
470 char_u *func,
471 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200472 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200473 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000475 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200476 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100478 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200479 CLEAR_FIELD(funcexe);
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200480 funcexe.firstline = curwin->w_cursor.lnum;
481 funcexe.lastline = curwin->w_cursor.lnum;
482 funcexe.evaluate = TRUE;
483 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000484 if (ret == FAIL)
485 clear_tv(rettv);
486
487 return ret;
488}
489
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100490/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100491 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100492 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200493 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
494 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100495 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200496 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100497call_func_retnr(
498 char_u *func,
499 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200500 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100501{
502 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200503 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100504
Bram Moolenaarded27a12018-08-01 19:06:03 +0200505 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100506 return -1;
507
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100508 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100509 clear_tv(&rettv);
510 return retval;
511}
512
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000513/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100514 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000515 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200516 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
517 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000518 */
519 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100520call_func_retstr(
521 char_u *func,
522 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200523 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000524{
525 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000526 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000527
Bram Moolenaarded27a12018-08-01 19:06:03 +0200528 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000529 return NULL;
530
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100531 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000532 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 return retval;
534}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000535
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000536/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100537 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200538 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
539 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000540 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000541 */
542 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100543call_func_retlist(
544 char_u *func,
545 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200546 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000547{
548 typval_T rettv;
549
Bram Moolenaarded27a12018-08-01 19:06:03 +0200550 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000551 return NULL;
552
553 if (rettv.v_type != VAR_LIST)
554 {
555 clear_tv(&rettv);
556 return NULL;
557 }
558
559 return rettv.vval.v_list;
560}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562#ifdef FEAT_FOLDING
563/*
564 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
565 * it in "*cp". Doesn't give error messages.
566 */
567 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100568eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
Bram Moolenaar33570922005-01-25 22:26:29 +0000570 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200571 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000573 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
574 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000577 if (use_sandbox)
578 ++sandbox;
579 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000581 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 retval = 0;
583 else
584 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100585 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000586 if (tv.v_type == VAR_NUMBER)
587 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000588 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 retval = 0;
590 else
591 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100592 // If the result is a string, check if there is a non-digit before
593 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000594 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (!VIM_ISDIGIT(*s) && *s != '-')
596 *cp = *s++;
597 retval = atol((char *)s);
598 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000599 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 }
601 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000602 if (use_sandbox)
603 --sandbox;
604 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200606 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607}
608#endif
609
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000611 * Get an lval: variable, Dict item or List item that can be assigned a value
612 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
613 * "name.key", "name.key[expr]" etc.
614 * Indexing only works if "name" is an existing List or Dictionary.
615 * "name" points to the start of the name.
616 * If "rettv" is not NULL it points to the value to be assigned.
617 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
618 * wrong; must end in space or cmd separator.
619 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100620 * flags:
621 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100622 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100623 * GLV_NO_AUTOLOAD: do not use script autoloading
624 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000625 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000626 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000627 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000628 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200629 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100630get_lval(
631 char_u *name,
632 typval_T *rettv,
633 lval_T *lp,
634 int unlet,
635 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100636 int flags, // GLV_ values
637 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000638{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000639 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000640 char_u *expr_start, *expr_end;
641 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000642 dictitem_T *v;
643 typval_T var1;
644 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000645 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000646 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000647 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000648 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000649 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100650 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000651
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100652 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200653 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000654
655 if (skip)
656 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100657 // When skipping just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000658 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000659 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000660 }
661
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100662 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000663 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000665 if (expr_start != NULL)
666 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100667 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +0100668 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000669 && *p != '[' && *p != '.')
670 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100671 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000672 return NULL;
673 }
674
675 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
676 if (lp->ll_exp_name == NULL)
677 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100678 // Report an invalid expression in braces, unless the
679 // expression evaluation has been cancelled due to an
680 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000681 if (!aborting() && !quiet)
682 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000683 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100684 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000685 return NULL;
686 }
687 }
688 lp->ll_name = lp->ll_exp_name;
689 }
690 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000692 lp->ll_name = name;
693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100694 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9 && *p == ':')
695 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100696 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 char_u *tp = skipwhite(p + 1);
698
699 // parse the type after the name
700 lp->ll_type = parse_type(&tp, &si->sn_type_list);
701 lp->ll_name_end = tp;
702 }
703 }
704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100705 // Without [idx] or .key we are done.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000706 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
707 return p;
708
709 cc = *p;
710 *p = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100711 // Only pass &ht when we would write to the variable, it prevents autoload
712 // as well.
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100713 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
714 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000715 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100716 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000717 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000718 if (v == NULL)
719 return NULL;
720
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000721 /*
722 * Loop until no more [idx] or .key is following.
723 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000724 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100725 var1.v_type = VAR_UNKNOWN;
726 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000727 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000729 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
730 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100731 && lp->ll_tv->vval.v_dict != NULL)
732 && !(lp->ll_tv->v_type == VAR_BLOB
733 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000735 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100736 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000737 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000738 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000739 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000740 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000741 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000743 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000744 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000745
Bram Moolenaar8c711452005-01-14 21:53:12 +0000746 len = -1;
747 if (*p == '.')
748 {
749 key = p + 1;
750 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
751 ;
752 if (len == 0)
753 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000754 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100755 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000756 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000757 }
758 p = key + len;
759 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000760 else
761 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100762 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000763 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000764 if (*p == ':')
765 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000766 else
767 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000768 empty1 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100769 if (eval1(&p, &var1, TRUE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000770 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100771 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000772 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100773 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000774 clear_tv(&var1);
775 return NULL;
776 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000777 }
778
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100779 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +0000780 if (*p == ':')
781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000782 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000783 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000784 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100785 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100786 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000787 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000788 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100789 if (rettv != NULL
790 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100791 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100792 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100793 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +0000794 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000795 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100796 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100797 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000798 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000799 }
800 p = skipwhite(p + 1);
801 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000802 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000803 else
804 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000805 lp->ll_empty2 = FALSE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100806 if (eval1(&p, &var2, TRUE) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +0000807 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100808 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000809 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000810 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100811 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000812 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100813 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100814 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000815 clear_tv(&var2);
816 return NULL;
817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000818 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000819 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000820 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000821 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000822 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000823
Bram Moolenaar8c711452005-01-14 21:53:12 +0000824 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +0000825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000826 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100827 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100828 clear_tv(&var1);
829 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000831 }
832
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100833 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000834 ++p;
835 }
836
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000837 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000838 {
839 if (len == -1)
840 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100841 // "[key]": get key from "var1"
842 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +0200843 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000844 {
Bram Moolenaar8c711452005-01-14 21:53:12 +0000845 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000846 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000847 }
848 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000849 lp->ll_list = NULL;
850 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000851 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200852
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100853 // When assigning to a scope dictionary check that a function and
854 // variable name is valid (only variable name unless it is l: or
855 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200856 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200857 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200858 int prevval;
859 int wrong;
860
861 if (len != -1)
862 {
863 prevval = key[len];
864 key[len] = NUL;
865 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +0200866 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100867 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200868 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
869 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200870 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200871 || !valid_varname(key);
872 if (len != -1)
873 key[len] = prevval;
874 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200875 {
876 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200877 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +0200878 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200879 }
880
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000881 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000882 {
Bram Moolenaar31b81602019-02-10 22:14:27 +0100883 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200884 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +0100885 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200886 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100887 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +0100888 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200889 return NULL;
890 }
891
Bram Moolenaar31b81602019-02-10 22:14:27 +0100892 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000893 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000895 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100896 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100897 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000898 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000899 }
900 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000901 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +0000902 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000903 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100904 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000905 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000906 p = NULL;
907 break;
908 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100909 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +0100910 else if ((flags & GLV_READ_ONLY) == 0
911 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +0100912 {
913 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200914 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +0100915 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200916
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100917 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000918 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000919 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100920 else if (lp->ll_tv->v_type == VAR_BLOB)
921 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100922 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
923
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100924 /*
925 * Get the number and item for the only or first index of the List.
926 */
927 if (empty1)
928 lp->ll_n1 = 0;
929 else
930 // is number or string
931 lp->ll_n1 = (long)tv_get_number(&var1);
932 clear_tv(&var1);
933
934 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100935 || lp->ll_n1 > bloblen
936 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100937 {
938 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100939 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100940 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100941 return NULL;
942 }
943 if (lp->ll_range && !lp->ll_empty2)
944 {
945 lp->ll_n2 = (long)tv_get_number(&var2);
946 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100947 if (lp->ll_n2 < 0
948 || lp->ll_n2 >= bloblen
949 || lp->ll_n2 < lp->ll_n1)
950 {
951 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100952 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +0100953 return NULL;
954 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100955 }
956 lp->ll_blob = lp->ll_tv->vval.v_blob;
957 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +0100958 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100959 }
Bram Moolenaar8c711452005-01-14 21:53:12 +0000960 else
961 {
962 /*
963 * Get the number and item for the only or first index of the List.
964 */
965 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000966 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000967 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100968 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100969 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100970 clear_tv(&var1);
971
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000972 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000973 lp->ll_list = lp->ll_tv->vval.v_list;
974 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
975 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000976 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000977 if (lp->ll_n1 < 0)
978 {
979 lp->ll_n1 = 0;
980 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
981 }
982 }
983 if (lp->ll_li == NULL)
984 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +0100985 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +0200986 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100987 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000988 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000989 }
990
991 /*
992 * May need to find the item or absolute index for the second
993 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 * When no index given: "lp->ll_empty2" is TRUE.
995 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +0000996 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100999 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001000 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001001 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001005 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001006 {
1007 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001008 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001010 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001012 }
1013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 // Check that lp->ll_n2 isn't before lp->ll_n1.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 if (lp->ll_n1 < 0)
1016 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1017 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001018 {
1019 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001020 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001022 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001023 }
1024
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001025 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001026 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001027 }
1028
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001029 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 return p;
1032}
1033
1034/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001037 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001038clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001039{
1040 vim_free(lp->ll_exp_name);
1041 vim_free(lp->ll_newkey);
1042}
1043
1044/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001045 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001046 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001047 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1048 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001049 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001051set_var_lval(
1052 lval_T *lp,
1053 char_u *endp,
1054 typval_T *rettv,
1055 int copy,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 int flags, // LET_IS_CONST and/or LET_NO_COMMAND
Bram Moolenaar7454a062016-01-30 15:14:10 +01001057 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001058{
1059 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001060 listitem_T *ri;
1061 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062
1063 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001064 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001065 cc = *endp;
1066 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 if (lp->ll_blob != NULL)
1068 {
1069 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001070
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001071 if (op != NULL && *op != '=')
1072 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001073 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001074 return;
1075 }
1076
1077 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1078 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001079 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001080
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001081 if (lp->ll_empty2)
1082 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1083
1084 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001086 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001087 return;
1088 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001089 if (lp->ll_empty2)
1090 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001091
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001092 ir = 0;
1093 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1094 blob_set(lp->ll_blob, il,
1095 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001096 }
1097 else
1098 {
1099 val = (int)tv_get_number_chk(rettv, &error);
1100 if (!error)
1101 {
1102 garray_T *gap = &lp->ll_blob->bv_ga;
1103
1104 // Allow for appending a byte. Setting a byte beyond
1105 // the end is an error otherwise.
1106 if (lp->ll_n1 < gap->ga_len
1107 || (lp->ll_n1 == gap->ga_len
1108 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1109 {
1110 blob_set(lp->ll_blob, lp->ll_n1, val);
1111 if (lp->ll_n1 == gap->ga_len)
1112 ++gap->ga_len;
1113 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001114 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001115 }
1116 }
1117 }
1118 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001119 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001120 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001123 {
1124 emsg(_(e_cannot_mod));
1125 *endp = cc;
1126 return;
1127 }
1128
Bram Moolenaarff697e62019-02-12 22:28:33 +01001129 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001130 di = NULL;
1131 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1132 &tv, &di, TRUE, FALSE) == OK)
1133 {
1134 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001135 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1136 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001137 && tv_op(&tv, rettv, op) == OK)
1138 set_var(lp->ll_name, &tv, FALSE);
1139 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001141 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001142 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 set_var_const(lp->ll_name, lp->ll_type, rettv, copy, flags);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001144 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001146 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001147 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001148 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001149 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001150 else if (lp->ll_range)
1151 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001152 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001153 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001156 {
1157 emsg(_("E996: Cannot lock a range"));
1158 return;
1159 }
1160
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001161 /*
1162 * Check whether any of the list items is locked
1163 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001164 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001165 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001166 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001167 return;
1168 ri = ri->li_next;
1169 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1170 break;
1171 ll_li = ll_li->li_next;
1172 ++ll_n1;
1173 }
1174
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 /*
1176 * Assign the List values to the list items.
1177 */
1178 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001179 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001180 if (op != NULL && *op != '=')
1181 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1182 else
1183 {
1184 clear_tv(&lp->ll_li->li_tv);
1185 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1186 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001187 ri = ri->li_next;
1188 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1189 break;
1190 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001192 // Need to add an empty item.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001193 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 ri = NULL;
1196 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001197 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001198 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001199 lp->ll_li = lp->ll_li->li_next;
1200 ++lp->ll_n1;
1201 }
1202 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001203 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001204 else if (lp->ll_empty2
1205 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001206 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001207 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001208 }
1209 else
1210 {
1211 /*
1212 * Assign to a List or Dictionary item.
1213 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 if (flags & LET_IS_CONST)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001215 {
1216 emsg(_("E996: Cannot lock a list or dict"));
1217 return;
1218 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001219 if (lp->ll_newkey != NULL)
1220 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001221 if (op != NULL && *op != '=')
1222 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001223 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001224 return;
1225 }
1226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001227 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001228 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (di == NULL)
1230 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001231 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1232 {
1233 vim_free(di);
1234 return;
1235 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001237 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001238 else if (op != NULL && *op != '=')
1239 {
1240 tv_op(lp->ll_tv, rettv, op);
1241 return;
1242 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001244 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001245
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001246 /*
1247 * Assign the value to the variable or list item.
1248 */
1249 if (copy)
1250 copy_tv(rettv, lp->ll_tv);
1251 else
1252 {
1253 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001254 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001255 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001256 }
1257 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001258}
1259
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001260/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001261 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1262 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001263 * Returns OK or FAIL.
1264 */
1265 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001266tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001267{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001268 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001269 char_u numbuf[NUMBUFLEN];
1270 char_u *s;
1271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001272 // Can't do anything with a Funcref, Dict, v:true on the right.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001273 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001274 && tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001275 {
1276 switch (tv1->v_type)
1277 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001278 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001279 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001281 case VAR_DICT:
1282 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001283 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001284 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001285 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001286 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001287 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001288 break;
1289
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290 case VAR_BLOB:
1291 if (*op != '+' || tv2->v_type != VAR_BLOB)
1292 break;
1293 // BLOB += BLOB
1294 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1295 {
1296 blob_T *b1 = tv1->vval.v_blob;
1297 blob_T *b2 = tv2->vval.v_blob;
1298 int i, len = blob_len(b2);
1299 for (i = 0; i < len; i++)
1300 ga_append(&b1->bv_ga, blob_get(b2, i));
1301 }
1302 return OK;
1303
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001304 case VAR_LIST:
1305 if (*op != '+' || tv2->v_type != VAR_LIST)
1306 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001307 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001308 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1309 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1310 return OK;
1311
1312 case VAR_NUMBER:
1313 case VAR_STRING:
1314 if (tv2->v_type == VAR_LIST)
1315 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001316 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001318 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001319 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001320#ifdef FEAT_FLOAT
1321 if (tv2->v_type == VAR_FLOAT)
1322 {
1323 float_T f = n;
1324
Bram Moolenaarff697e62019-02-12 22:28:33 +01001325 if (*op == '%')
1326 break;
1327 switch (*op)
1328 {
1329 case '+': f += tv2->vval.v_float; break;
1330 case '-': f -= tv2->vval.v_float; break;
1331 case '*': f *= tv2->vval.v_float; break;
1332 case '/': f /= tv2->vval.v_float; break;
1333 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001334 clear_tv(tv1);
1335 tv1->v_type = VAR_FLOAT;
1336 tv1->vval.v_float = f;
1337 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001338 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001339#endif
1340 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001341 switch (*op)
1342 {
1343 case '+': n += tv_get_number(tv2); break;
1344 case '-': n -= tv_get_number(tv2); break;
1345 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001346 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1347 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001348 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001349 clear_tv(tv1);
1350 tv1->v_type = VAR_NUMBER;
1351 tv1->vval.v_number = n;
1352 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001353 }
1354 else
1355 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001356 if (tv2->v_type == VAR_FLOAT)
1357 break;
1358
Bram Moolenaarff697e62019-02-12 22:28:33 +01001359 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001360 s = tv_get_string(tv1);
1361 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 clear_tv(tv1);
1363 tv1->v_type = VAR_STRING;
1364 tv1->vval.v_string = s;
1365 }
1366 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001367
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001368 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001369#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001370 {
1371 float_T f;
1372
Bram Moolenaarff697e62019-02-12 22:28:33 +01001373 if (*op == '%' || *op == '.'
1374 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001375 && tv2->v_type != VAR_NUMBER
1376 && tv2->v_type != VAR_STRING))
1377 break;
1378 if (tv2->v_type == VAR_FLOAT)
1379 f = tv2->vval.v_float;
1380 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001381 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001382 switch (*op)
1383 {
1384 case '+': tv1->vval.v_float += f; break;
1385 case '-': tv1->vval.v_float -= f; break;
1386 case '*': tv1->vval.v_float *= f; break;
1387 case '/': tv1->vval.v_float /= f; break;
1388 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001389 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001390#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001391 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001392 }
1393 }
1394
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001395 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001396 return FAIL;
1397}
1398
1399/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001400 * Evaluate the expression used in a ":for var in expr" command.
1401 * "arg" points to "var".
1402 * Set "*errp" to TRUE for an error, FALSE otherwise;
1403 * Return a pointer that holds the info. Null when there is an error.
1404 */
1405 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001406eval_for_line(
1407 char_u *arg,
1408 int *errp,
1409 char_u **nextcmdp,
1410 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001411{
Bram Moolenaar33570922005-01-25 22:26:29 +00001412 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001413 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T tv;
1415 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001416
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001417 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001418
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001419 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001420 if (fi == NULL)
1421 return NULL;
1422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 expr = skip_var_list(arg, TRUE, &fi->fi_varcount, &fi->fi_semicolon);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424 if (expr == NULL)
1425 return fi;
1426
1427 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001428 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001429 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 emsg(_(e_missing_in));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001431 return fi;
1432 }
1433
1434 if (skip)
1435 ++emsg_skip;
1436 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1437 {
1438 *errp = FALSE;
1439 if (!skip)
1440 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001441 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001442 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001443 l = tv.vval.v_list;
1444 if (l == NULL)
1445 {
1446 // a null list is like an empty list: do nothing
1447 clear_tv(&tv);
1448 }
1449 else
1450 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001451 // Need a real list here.
1452 range_list_materialize(l);
1453
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001454 // No need to increment the refcount, it's already set for
1455 // the list being used in "tv".
1456 fi->fi_list = l;
1457 list_add_watch(l, &fi->fi_lw);
1458 fi->fi_lw.lw_item = l->lv_first;
1459 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001460 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001461 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001462 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001463 fi->fi_bi = 0;
1464 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001465 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001466 typval_T btv;
1467
1468 // Make a copy, so that the iteration still works when the
1469 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001471 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001472 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001473 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001474 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001475 else
1476 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001477 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001478 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001479 }
1480 }
1481 }
1482 if (skip)
1483 --emsg_skip;
1484
1485 return fi;
1486}
1487
1488/*
1489 * Use the first item in a ":for" list. Advance to the next.
1490 * Assign the values to the variable (list). "arg" points to the first one.
1491 * Return TRUE when a valid item was found, FALSE when at end of list or
1492 * something wrong.
1493 */
1494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001495next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001496{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001497 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001498 int result;
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001499 int flag = current_sctx.sc_version == SCRIPT_VERSION_VIM9 ?
1500 LET_NO_COMMAND : 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001502
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001503 if (fi->fi_blob != NULL)
1504 {
1505 typval_T tv;
1506
1507 if (fi->fi_bi >= blob_len(fi->fi_blob))
1508 return FALSE;
1509 tv.v_type = VAR_NUMBER;
1510 tv.v_lock = VAR_FIXED;
1511 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1512 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001513 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001514 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001515 }
1516
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001517 item = fi->fi_lw.lw_item;
1518 if (item == NULL)
1519 result = FALSE;
1520 else
1521 {
1522 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001523 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar41fe0612020-03-01 16:22:40 +01001524 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001525 }
1526 return result;
1527}
1528
1529/*
1530 * Free the structure used to store info used by ":for".
1531 */
1532 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001533free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001534{
Bram Moolenaar33570922005-01-25 22:26:29 +00001535 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001536
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001537 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001538 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001539 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001540 list_unref(fi->fi_list);
1541 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001542 if (fi != NULL && fi->fi_blob != NULL)
1543 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001544 vim_free(fi);
1545}
1546
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001548set_context_for_expression(
1549 expand_T *xp,
1550 char_u *arg,
1551 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
1553 int got_eq = FALSE;
1554 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001555 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556
Bram Moolenaar8f76e6b2019-11-26 16:50:30 +01001557 if (cmdidx == CMD_let || cmdidx == CMD_const)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001558 {
1559 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001560 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001561 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001562 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001563 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001564 {
1565 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001566 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001567 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001568 break;
1569 }
1570 return;
1571 }
1572 }
1573 else
1574 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1575 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 while ((xp->xp_pattern = vim_strpbrk(arg,
1577 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1578 {
1579 c = *xp->xp_pattern;
1580 if (c == '&')
1581 {
1582 c = xp->xp_pattern[1];
1583 if (c == '&')
1584 {
1585 ++xp->xp_pattern;
1586 xp->xp_context = cmdidx != CMD_let || got_eq
1587 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1588 }
1589 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001590 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001592 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1593 xp->xp_pattern += 2;
1594
1595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597 else if (c == '$')
1598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001599 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 xp->xp_context = EXPAND_ENV_VARS;
1601 }
1602 else if (c == '=')
1603 {
1604 got_eq = TRUE;
1605 xp->xp_context = EXPAND_EXPRESSION;
1606 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001607 else if (c == '#'
1608 && xp->xp_context == EXPAND_EXPRESSION)
1609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001610 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02001611 break;
1612 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001613 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 && xp->xp_context == EXPAND_FUNCTIONS
1615 && vim_strchr(xp->xp_pattern, '(') == NULL)
1616 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001617 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 break;
1619 }
1620 else if (cmdidx != CMD_let || got_eq)
1621 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001622 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 {
1624 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1625 if (c == '\\' && xp->xp_pattern[1] != NUL)
1626 ++xp->xp_pattern;
1627 xp->xp_context = EXPAND_NOTHING;
1628 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001629 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001631 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1633 /* skip */ ;
1634 xp->xp_context = EXPAND_NOTHING;
1635 }
1636 else if (c == '|')
1637 {
1638 if (xp->xp_pattern[1] == '|')
1639 {
1640 ++xp->xp_pattern;
1641 xp->xp_context = EXPAND_EXPRESSION;
1642 }
1643 else
1644 xp->xp_context = EXPAND_COMMANDS;
1645 }
1646 else
1647 xp->xp_context = EXPAND_EXPRESSION;
1648 }
1649 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001650 // Doesn't look like something valid, expand as an expression
1651 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001652 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 arg = xp->xp_pattern;
1654 if (*arg != NUL)
1655 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1656 /* skip */ ;
1657 }
1658 xp->xp_pattern = arg;
1659}
1660
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001662 * Return TRUE if "pat" matches "text".
1663 * Does not use 'cpo' and always uses 'magic'.
1664 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001665 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001666pattern_match(char_u *pat, char_u *text, int ic)
1667{
1668 int matches = FALSE;
1669 char_u *save_cpo;
1670 regmatch_T regmatch;
1671
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001672 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001673 save_cpo = p_cpo;
1674 p_cpo = (char_u *)"";
1675 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1676 if (regmatch.regprog != NULL)
1677 {
1678 regmatch.rm_ic = ic;
1679 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1680 vim_regfree(regmatch.regprog);
1681 }
1682 p_cpo = save_cpo;
1683 return matches;
1684}
1685
1686/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001687 * Handle a name followed by "(". Both for just "name(arg)" and for
1688 * "expr->name(arg)".
1689 * Returns OK or FAIL.
1690 */
1691 static int
1692eval_func(
1693 char_u **arg, // points to "(", will be advanced
1694 char_u *name,
1695 int name_len,
1696 typval_T *rettv,
1697 int evaluate,
1698 typval_T *basetv) // "expr" for "expr->name(arg)"
1699{
1700 char_u *s = name;
1701 int len = name_len;
1702 partial_T *partial;
1703 int ret = OK;
1704
1705 if (!evaluate)
1706 check_vars(s, len);
1707
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001708 // If "s" is the name of a variable of type VAR_FUNC
1709 // use its contents.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001710 s = deref_func_name(s, &len, &partial, !evaluate);
1711
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001712 // Need to make a copy, in case evaluating the arguments makes
1713 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001714 s = vim_strsave(s);
1715 if (s == NULL)
1716 ret = FAIL;
1717 else
1718 {
1719 funcexe_T funcexe;
1720
1721 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02001722 CLEAR_FIELD(funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001723 funcexe.firstline = curwin->w_cursor.lnum;
1724 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001725 funcexe.evaluate = evaluate;
1726 funcexe.partial = partial;
1727 funcexe.basetv = basetv;
1728 ret = get_func_tv(s, len, rettv, arg, &funcexe);
1729 }
1730 vim_free(s);
1731
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001732 // If evaluate is FALSE rettv->v_type was not set in
1733 // get_func_tv, but it's needed in handle_subscript() to parse
1734 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001735 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
1736 {
1737 rettv->vval.v_string = NULL;
1738 rettv->v_type = VAR_FUNC;
1739 }
1740
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001741 // Stop the expression evaluation when immediately
1742 // aborting on error, or when an interrupt occurred or
1743 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001744 if (evaluate && aborting())
1745 {
1746 if (ret == OK)
1747 clear_tv(rettv);
1748 ret = FAIL;
1749 }
1750 return ret;
1751}
1752
1753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
1757 */
1758
1759/*
1760 * Handle zero level expression.
1761 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001762 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00001763 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 * Return OK or FAIL.
1765 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001767eval0(
1768 char_u *arg,
1769 typval_T *rettv,
1770 char_u **nextcmd,
1771 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772{
1773 int ret;
1774 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001775 int did_emsg_before = did_emsg;
1776 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001779 ret = eval1(&p, rettv, evaluate);
Bram Moolenaarfaac4102020-04-20 17:46:14 +02001780 if (ret == FAIL || !ends_excmd2(arg, p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 {
1782 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001783 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 /*
1785 * Report the invalid expression unless the expression evaluation has
1786 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001787 * exception, or we already gave a more specific error.
1788 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001790 if (!aborting() && did_emsg == did_emsg_before
1791 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001792 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 ret = FAIL;
1794 }
1795 if (nextcmd != NULL)
1796 *nextcmd = check_nextcmd(p);
1797
1798 return ret;
1799}
1800
1801/*
1802 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00001803 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 *
1805 * "arg" must point to the first non-white of the expression.
1806 * "arg" is advanced to the next non-white after the recognized expression.
1807 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00001808 * Note: "rettv.v_lock" is not set.
1809 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 * Return OK or FAIL.
1811 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02001812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001816 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817
1818 /*
1819 * Get the first variable.
1820 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001821 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 return FAIL;
1823
1824 if ((*arg)[0] == '?')
1825 {
1826 result = FALSE;
1827 if (evaluate)
1828 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001829 int error = FALSE;
1830
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001831 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001834 if (error)
1835 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836 }
1837
1838 /*
1839 * Get the second variable.
1840 */
1841 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001842 if (eval1(arg, rettv, evaluate && result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 return FAIL;
1844
1845 /*
1846 * Check for the ":".
1847 */
1848 if ((*arg)[0] != ':')
1849 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001850 emsg(_(e_missing_colon));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001852 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 return FAIL;
1854 }
1855
1856 /*
1857 * Get the third variable.
1858 */
1859 *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001860 if (eval1(arg, &var2, evaluate && !result) == FAIL) // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 {
1862 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 return FAIL;
1865 }
1866 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 }
1869
1870 return OK;
1871}
1872
1873/*
1874 * Handle first level expression:
1875 * expr2 || expr2 || expr2 logical OR
1876 *
1877 * "arg" must point to the first non-white of the expression.
1878 * "arg" is advanced to the next non-white after the recognized expression.
1879 *
1880 * Return OK or FAIL.
1881 */
1882 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001883eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884{
Bram Moolenaar33570922005-01-25 22:26:29 +00001885 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 long result;
1887 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001888 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 /*
1891 * Get the first variable.
1892 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001893 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 return FAIL;
1895
1896 /*
1897 * Repeat until there is no following "||".
1898 */
1899 first = TRUE;
1900 result = FALSE;
1901 while ((*arg)[0] == '|' && (*arg)[1] == '|')
1902 {
1903 if (evaluate && first)
1904 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001905 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001908 if (error)
1909 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 first = FALSE;
1911 }
1912
1913 /*
1914 * Get the second variable.
1915 */
1916 *arg = skipwhite(*arg + 2);
1917 if (eval3(arg, &var2, evaluate && !result) == FAIL)
1918 return FAIL;
1919
1920 /*
1921 * Compute the result.
1922 */
1923 if (evaluate && !result)
1924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001925 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001928 if (error)
1929 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 }
1931 if (evaluate)
1932 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001933 rettv->v_type = VAR_NUMBER;
1934 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
1936 }
1937
1938 return OK;
1939}
1940
1941/*
1942 * Handle second level expression:
1943 * expr3 && expr3 && expr3 logical AND
1944 *
1945 * "arg" must point to the first non-white of the expression.
1946 * "arg" is advanced to the next non-white after the recognized expression.
1947 *
1948 * Return OK or FAIL.
1949 */
1950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001951eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952{
Bram Moolenaar33570922005-01-25 22:26:29 +00001953 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954 long result;
1955 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001956 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 /*
1959 * Get the first variable.
1960 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 return FAIL;
1963
1964 /*
1965 * Repeat until there is no following "&&".
1966 */
1967 first = TRUE;
1968 result = TRUE;
1969 while ((*arg)[0] == '&' && (*arg)[1] == '&')
1970 {
1971 if (evaluate && first)
1972 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001973 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001976 if (error)
1977 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 first = FALSE;
1979 }
1980
1981 /*
1982 * Get the second variable.
1983 */
1984 *arg = skipwhite(*arg + 2);
1985 if (eval4(arg, &var2, evaluate && result) == FAIL)
1986 return FAIL;
1987
1988 /*
1989 * Compute the result.
1990 */
1991 if (evaluate && result)
1992 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001993 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001995 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001996 if (error)
1997 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 }
1999 if (evaluate)
2000 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002001 rettv->v_type = VAR_NUMBER;
2002 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 }
2005
2006 return OK;
2007}
2008
2009/*
2010 * Handle third level expression:
2011 * var1 == var2
2012 * var1 =~ var2
2013 * var1 != var2
2014 * var1 !~ var2
2015 * var1 > var2
2016 * var1 >= var2
2017 * var1 < var2
2018 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002019 * var1 is var2
2020 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 *
2022 * "arg" must point to the first non-white of the expression.
2023 * "arg" is advanced to the next non-white after the recognized expression.
2024 *
2025 * Return OK or FAIL.
2026 */
2027 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002028eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002029{
Bram Moolenaar33570922005-01-25 22:26:29 +00002030 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 char_u *p;
2032 int i;
Bram Moolenaar87396072019-12-31 22:36:18 +01002033 exptype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036
2037 /*
2038 * Get the first variable.
2039 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002040 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 return FAIL;
2042
2043 p = *arg;
2044 switch (p[0])
2045 {
2046 case '=': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002047 type = EXPR_EQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002049 type = EXPR_MATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 break;
2051 case '!': if (p[1] == '=')
Bram Moolenaar87396072019-12-31 22:36:18 +01002052 type = EXPR_NEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 else if (p[1] == '~')
Bram Moolenaar87396072019-12-31 22:36:18 +01002054 type = EXPR_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 break;
2056 case '>': if (p[1] != '=')
2057 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002058 type = EXPR_GREATER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 len = 1;
2060 }
2061 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002062 type = EXPR_GEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 break;
2064 case '<': if (p[1] != '=')
2065 {
Bram Moolenaar87396072019-12-31 22:36:18 +01002066 type = EXPR_SMALLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 len = 1;
2068 }
2069 else
Bram Moolenaar87396072019-12-31 22:36:18 +01002070 type = EXPR_SEQUAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002072 case 'i': if (p[1] == 's')
2073 {
2074 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2075 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002076 i = p[len];
2077 if (!isalnum(i) && i != '_')
Bram Moolenaar87396072019-12-31 22:36:18 +01002078 type = len == 2 ? EXPR_IS : EXPR_ISNOT;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002079 }
2080 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082
2083 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002084 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002086 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002088 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002089 if (p[len] == '?')
2090 {
2091 ic = TRUE;
2092 ++len;
2093 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002094 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 else if (p[len] == '#')
2096 {
2097 ic = FALSE;
2098 ++len;
2099 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002100 // nothing appended: use 'ignorecase'
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 else
2102 ic = p_ic;
2103
2104 /*
2105 * Get the second variable.
2106 */
2107 *arg = skipwhite(p + len);
2108 if (eval5(arg, &var2, evaluate) == FAIL)
2109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002110 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 return FAIL;
2112 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002113 if (evaluate)
2114 {
Bram Moolenaar07a3db82019-12-25 18:14:14 +01002115 int ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01002116
2117 clear_tv(&var2);
2118 return ret;
2119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121
2122 return OK;
2123}
2124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125 void
2126eval_addblob(typval_T *tv1, typval_T *tv2)
2127{
2128 blob_T *b1 = tv1->vval.v_blob;
2129 blob_T *b2 = tv2->vval.v_blob;
2130 blob_T *b = blob_alloc();
2131 int i;
2132
2133 if (b != NULL)
2134 {
2135 for (i = 0; i < blob_len(b1); i++)
2136 ga_append(&b->bv_ga, blob_get(b1, i));
2137 for (i = 0; i < blob_len(b2); i++)
2138 ga_append(&b->bv_ga, blob_get(b2, i));
2139
2140 clear_tv(tv1);
2141 rettv_blob_set(tv1, b);
2142 }
2143}
2144
2145 int
2146eval_addlist(typval_T *tv1, typval_T *tv2)
2147{
2148 typval_T var3;
2149
2150 // concatenate Lists
2151 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
2152 {
2153 clear_tv(tv1);
2154 clear_tv(tv2);
2155 return FAIL;
2156 }
2157 clear_tv(tv1);
2158 *tv1 = var3;
2159 return OK;
2160}
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162/*
2163 * Handle fourth level expression:
2164 * + number addition
2165 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002166 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002167 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 *
2169 * "arg" must point to the first non-white of the expression.
2170 * "arg" is advanced to the next non-white after the recognized expression.
2171 *
2172 * Return OK or FAIL.
2173 */
2174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176{
Bram Moolenaar33570922005-01-25 22:26:29 +00002177 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002179 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002180#ifdef FEAT_FLOAT
2181 float_T f1 = 0, f2 = 0;
2182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 char_u *s1, *s2;
2184 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2185 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002186 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187
2188 /*
2189 * Get the first variable.
2190 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002191 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 return FAIL;
2193
2194 /*
2195 * Repeat computing, until no '+', '-' or '.' is following.
2196 */
2197 for (;;)
2198 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002199 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002201 concat = op == '.'
2202 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2203 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204 break;
2205
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002206 if ((op != '+' || (rettv->v_type != VAR_LIST
2207 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002208#ifdef FEAT_FLOAT
2209 && (op == '.' || rettv->v_type != VAR_FLOAT)
2210#endif
2211 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002213 // For "list + ...", an illegal use of the first operand as
2214 // a number cannot be determined before evaluating the 2nd
2215 // operand: if this is also a list, all is ok.
2216 // For "something . ...", "something - ..." or "non-list + ...",
2217 // we know that the first operand needs to be a string or number
2218 // without evaluating the 2nd operand. So check before to avoid
2219 // side effects after an error.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002220 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002221 {
2222 clear_tv(rettv);
2223 return FAIL;
2224 }
2225 }
2226
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 /*
2228 * Get the second variable.
2229 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002230 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2231 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002233 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002235 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 return FAIL;
2237 }
2238
2239 if (evaluate)
2240 {
2241 /*
2242 * Compute the result.
2243 */
2244 if (op == '.')
2245 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002246 s1 = tv_get_string_buf(rettv, buf1); // already checked
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002247 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002248 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002249 {
2250 clear_tv(rettv);
2251 clear_tv(&var2);
2252 return FAIL;
2253 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002254 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 clear_tv(rettv);
2256 rettv->v_type = VAR_STRING;
2257 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002259 else if (op == '+' && rettv->v_type == VAR_BLOB
2260 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00002262 else if (op == '+' && rettv->v_type == VAR_LIST
2263 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002264 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002266 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 else
2269 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002270 int error = FALSE;
2271
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002272#ifdef FEAT_FLOAT
2273 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002274 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002275 f1 = rettv->vval.v_float;
2276 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002277 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002278 else
2279#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002281 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002282 if (error)
2283 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002284 // This can only happen for "list + non-list". For
2285 // "non-list + ..." or "something - ...", we returned
2286 // before evaluating the 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002287 clear_tv(rettv);
2288 return FAIL;
2289 }
2290#ifdef FEAT_FLOAT
2291 if (var2.v_type == VAR_FLOAT)
2292 f1 = n1;
2293#endif
2294 }
2295#ifdef FEAT_FLOAT
2296 if (var2.v_type == VAR_FLOAT)
2297 {
2298 f2 = var2.vval.v_float;
2299 n2 = 0;
2300 }
2301 else
2302#endif
2303 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002304 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002305 if (error)
2306 {
2307 clear_tv(rettv);
2308 clear_tv(&var2);
2309 return FAIL;
2310 }
2311#ifdef FEAT_FLOAT
2312 if (rettv->v_type == VAR_FLOAT)
2313 f2 = n2;
2314#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002315 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002317
2318#ifdef FEAT_FLOAT
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002319 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002320 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2321 {
2322 if (op == '+')
2323 f1 = f1 + f2;
2324 else
2325 f1 = f1 - f2;
2326 rettv->v_type = VAR_FLOAT;
2327 rettv->vval.v_float = f1;
2328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002330#endif
2331 {
2332 if (op == '+')
2333 n1 = n1 + n2;
2334 else
2335 n1 = n1 - n2;
2336 rettv->v_type = VAR_NUMBER;
2337 rettv->vval.v_number = n1;
2338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 }
2342 }
2343 return OK;
2344}
2345
2346/*
2347 * Handle fifth level expression:
2348 * * number multiplication
2349 * / number division
2350 * % number modulo
2351 *
2352 * "arg" must point to the first non-white of the expression.
2353 * "arg" is advanced to the next non-white after the recognized expression.
2354 *
2355 * Return OK or FAIL.
2356 */
2357 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002358eval6(
2359 char_u **arg,
2360 typval_T *rettv,
2361 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002362 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363{
Bram Moolenaar33570922005-01-25 22:26:29 +00002364 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002366 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002367#ifdef FEAT_FLOAT
2368 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002369 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002370#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002371 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
2373 /*
2374 * Get the first variable.
2375 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002376 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 return FAIL;
2378
2379 /*
2380 * Repeat computing, until no '*', '/' or '%' is following.
2381 */
2382 for (;;)
2383 {
2384 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002385 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 break;
2387
2388 if (evaluate)
2389 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002390#ifdef FEAT_FLOAT
2391 if (rettv->v_type == VAR_FLOAT)
2392 {
2393 f1 = rettv->vval.v_float;
2394 use_float = TRUE;
2395 n1 = 0;
2396 }
2397 else
2398#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002399 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002400 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002401 if (error)
2402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 }
2404 else
2405 n1 = 0;
2406
2407 /*
2408 * Get the second variable.
2409 */
2410 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002411 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 return FAIL;
2413
2414 if (evaluate)
2415 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002416#ifdef FEAT_FLOAT
2417 if (var2.v_type == VAR_FLOAT)
2418 {
2419 if (!use_float)
2420 {
2421 f1 = n1;
2422 use_float = TRUE;
2423 }
2424 f2 = var2.vval.v_float;
2425 n2 = 0;
2426 }
2427 else
2428#endif
2429 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002430 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002431 clear_tv(&var2);
2432 if (error)
2433 return FAIL;
2434#ifdef FEAT_FLOAT
2435 if (use_float)
2436 f2 = n2;
2437#endif
2438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439
2440 /*
2441 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002442 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002444#ifdef FEAT_FLOAT
2445 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002447 if (op == '*')
2448 f1 = f1 * f2;
2449 else if (op == '/')
2450 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002451# ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002452 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002453 if (f2 == 0.0)
2454 {
2455 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002456 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002457 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002458 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002459 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002460 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002461 }
2462 else
2463 f1 = f1 / f2;
2464# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002465 // We rely on the floating point library to handle divide
2466 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002467 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002468# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002471 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 emsg(_(e_modulus));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002473 return FAIL;
2474 }
2475 rettv->v_type = VAR_FLOAT;
2476 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 }
2478 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002481 if (op == '*')
2482 n1 = n1 * n2;
2483 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002484 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002486 n1 = num_modulus(n1, n2);
2487
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002488 rettv->v_type = VAR_NUMBER;
2489 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 }
2492 }
2493
2494 return OK;
2495}
2496
2497/*
2498 * Handle sixth level expression:
2499 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002500 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002501 * "string" string constant
2502 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * &option-name option value
2504 * @r register contents
2505 * identifier variable value
2506 * function() function call
2507 * $VAR environment variable
2508 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002509 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002511 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002512 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 *
2514 * Also handle:
2515 * ! in front logical NOT
2516 * - in front unary minus
2517 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002518 * trailing [] subscript in String or List
2519 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002520 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 *
2522 * "arg" must point to the first non-white of the expression.
2523 * "arg" is advanced to the next non-white after the recognized expression.
2524 *
2525 * Return OK or FAIL.
2526 */
2527 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002528eval7(
2529 char_u **arg,
2530 typval_T *rettv,
2531 int evaluate,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 int len;
2535 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 char_u *start_leader, *end_leader;
2537 int ret = OK;
2538 char_u *alias;
2539
2540 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002542 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002544 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002547 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 */
2549 start_leader = *arg;
2550 while (**arg == '!' || **arg == '-' || **arg == '+')
2551 *arg = skipwhite(*arg + 1);
2552 end_leader = *arg;
2553
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002554 if (**arg == '.' && (!isdigit(*(*arg + 1))
2555#ifdef FEAT_FLOAT
2556 || current_sctx.sc_version < 2
2557#endif
2558 ))
2559 {
2560 semsg(_(e_invexpr2), *arg);
2561 ++*arg;
2562 return FAIL;
2563 }
2564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 switch (**arg)
2566 {
2567 /*
2568 * Number constant.
2569 */
2570 case '0':
2571 case '1':
2572 case '2':
2573 case '3':
2574 case '4':
2575 case '5':
2576 case '6':
2577 case '7':
2578 case '8':
2579 case '9':
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 case '.': ret = get_number_tv(arg, rettv, evaluate, want_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 break;
2582
2583 /*
2584 * String constant: "string".
2585 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 break;
2588
2589 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002590 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002593 break;
2594
2595 /*
2596 * List: [expr, expr]
2597 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 case '[': ret = get_list_tv(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 break;
2600
2601 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002602 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002603 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002604 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002605 {
2606 ++*arg;
Bram Moolenaar08928322020-01-04 14:32:48 +01002607 ret = eval_dict(arg, rettv, evaluate, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002608 }
2609 else
2610 ret = NOTDONE;
2611 break;
2612
2613 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002614 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002615 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002616 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002617 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2618 if (ret == NOTDONE)
Bram Moolenaar08928322020-01-04 14:32:48 +01002619 ret = eval_dict(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 break;
2621
2622 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002623 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00002625 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 break;
2627
2628 /*
2629 * Environment variable: $VAR.
2630 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002631 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 break;
2633
2634 /*
2635 * Register contents: @r.
2636 */
2637 case '@': ++*arg;
2638 if (evaluate)
2639 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002640 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002641 rettv->vval.v_string = get_reg_contents(**arg,
2642 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644 if (**arg != NUL)
2645 ++*arg;
2646 break;
2647
2648 /*
2649 * nested expression: (expression).
2650 */
2651 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002652 ret = eval1(arg, rettv, evaluate); // recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (**arg == ')')
2654 ++*arg;
2655 else if (ret == OK)
2656 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 emsg(_(e_missing_close));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 ret = FAIL;
2660 }
2661 break;
2662
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 break;
2665 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666
2667 if (ret == NOTDONE)
2668 {
2669 /*
2670 * Must be a variable or function name.
2671 * Can also be a curly-braces kind of name: {expr}.
2672 */
2673 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002674 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002675 if (alias != NULL)
2676 s = alias;
2677
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002678 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 ret = FAIL;
2680 else
2681 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002682 if (**arg == '(') // recursive!
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002683 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002685 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002686 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002687 {
2688 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002689 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01002692 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
2694
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 *arg = skipwhite(*arg);
2696
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002697 // Handle following '[', '(' and '.' for expr[expr], expr.name,
2698 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002699 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002700 ret = handle_subscript(arg, rettv, evaluate, TRUE,
2701 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702
2703 /*
2704 * Apply logical NOT and unary '-', from right to left, ignore '+'.
2705 */
2706 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002707 ret = eval7_leader(rettv, start_leader, &end_leader);
2708 return ret;
2709}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002710
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002711/*
2712 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
2713 * Adjusts "end_leaderp" until it is at "start_leader".
2714 */
2715 static int
2716eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
2717{
2718 char_u *end_leader = *end_leaderp;
2719 int ret = OK;
2720 int error = FALSE;
2721 varnumber_T val = 0;
2722#ifdef FEAT_FLOAT
2723 float_T f = 0.0;
2724
2725 if (rettv->v_type == VAR_FLOAT)
2726 f = rettv->vval.v_float;
2727 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002728#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002729 val = tv_get_number_chk(rettv, &error);
2730 if (error)
2731 {
2732 clear_tv(rettv);
2733 ret = FAIL;
2734 }
2735 else
2736 {
2737 while (end_leader > start_leader)
2738 {
2739 --end_leader;
2740 if (*end_leader == '!')
2741 {
2742#ifdef FEAT_FLOAT
2743 if (rettv->v_type == VAR_FLOAT)
2744 f = !f;
2745 else
2746#endif
2747 val = !val;
2748 }
2749 else if (*end_leader == '-')
2750 {
2751#ifdef FEAT_FLOAT
2752 if (rettv->v_type == VAR_FLOAT)
2753 f = -f;
2754 else
2755#endif
2756 val = -val;
2757 }
2758 }
2759#ifdef FEAT_FLOAT
2760 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002762 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002763 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002765 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002766#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002767 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002768 clear_tv(rettv);
2769 rettv->v_type = VAR_NUMBER;
2770 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02002773 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 return ret;
2775}
2776
2777/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002778 * Call the function referred to in "rettv".
2779 */
2780 static int
2781call_func_rettv(
2782 char_u **arg,
2783 typval_T *rettv,
2784 int evaluate,
2785 dict_T *selfdict,
2786 typval_T *basetv)
2787{
2788 partial_T *pt = NULL;
2789 funcexe_T funcexe;
2790 typval_T functv;
2791 char_u *s;
2792 int ret;
2793
2794 // need to copy the funcref so that we can clear rettv
2795 if (evaluate)
2796 {
2797 functv = *rettv;
2798 rettv->v_type = VAR_UNKNOWN;
2799
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002800 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002801 if (functv.v_type == VAR_PARTIAL)
2802 {
2803 pt = functv.vval.v_partial;
2804 s = partial_name(pt);
2805 }
2806 else
2807 s = functv.vval.v_string;
2808 }
2809 else
2810 s = (char_u *)"";
2811
Bram Moolenaara80faa82020-04-12 19:37:17 +02002812 CLEAR_FIELD(funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002813 funcexe.firstline = curwin->w_cursor.lnum;
2814 funcexe.lastline = curwin->w_cursor.lnum;
2815 funcexe.evaluate = evaluate;
2816 funcexe.partial = pt;
2817 funcexe.selfdict = selfdict;
2818 funcexe.basetv = basetv;
2819 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
2820
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002821 // Clear the funcref afterwards, so that deleting it while
2822 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002823 if (evaluate)
2824 clear_tv(&functv);
2825
2826 return ret;
2827}
2828
2829/*
2830 * Evaluate "->method()".
2831 * "*arg" points to the '-'.
2832 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2833 */
2834 static int
2835eval_lambda(
2836 char_u **arg,
2837 typval_T *rettv,
2838 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002839 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002840{
2841 typval_T base = *rettv;
2842 int ret;
2843
2844 // Skip over the ->.
2845 *arg += 2;
2846 rettv->v_type = VAR_UNKNOWN;
2847
2848 ret = get_lambda_tv(arg, rettv, evaluate);
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01002849 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002850 return FAIL;
2851 else if (**arg != '(')
2852 {
2853 if (verbose)
2854 {
2855 if (*skipwhite(*arg) == '(')
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002856 emsg(_(e_nowhitespace));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002857 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 semsg(_(e_missing_paren), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002859 }
2860 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02002861 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002862 }
Bram Moolenaar86173482019-10-01 17:02:16 +02002863 else
2864 ret = call_func_rettv(arg, rettv, evaluate, NULL, &base);
2865
2866 // Clear the funcref afterwards, so that deleting it while
2867 // evaluating the arguments is possible (see test55).
2868 if (evaluate)
2869 clear_tv(&base);
2870
2871 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002872}
2873
2874/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02002875 * Evaluate "->method()".
2876 * "*arg" points to the '-'.
2877 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
2878 */
2879 static int
2880eval_method(
2881 char_u **arg,
2882 typval_T *rettv,
2883 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002884 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02002885{
2886 char_u *name;
2887 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002888 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002889 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002890 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002891
2892 // Skip over the ->.
2893 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002894 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002895
Bram Moolenaarac92e252019-08-03 21:58:38 +02002896 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002897 len = get_name_len(arg, &alias, evaluate, TRUE);
2898 if (alias != NULL)
2899 name = alias;
2900
2901 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02002902 {
2903 if (verbose)
2904 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002905 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02002906 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002907 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02002908 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002909 if (**arg != '(')
2910 {
2911 if (verbose)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 semsg(_(e_missing_paren), name);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002913 ret = FAIL;
2914 }
Bram Moolenaar51841322019-08-08 21:10:01 +02002915 else if (VIM_ISWHITE((*arg)[-1]))
2916 {
2917 if (verbose)
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002918 emsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02002919 ret = FAIL;
2920 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002921 else
2922 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02002923 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02002924
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02002925 // Clear the funcref afterwards, so that deleting it while
2926 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02002927 if (evaluate)
2928 clear_tv(&base);
2929
Bram Moolenaarac92e252019-08-03 21:58:38 +02002930 return ret;
2931}
2932
2933/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002934 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
2935 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002936 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
2937 */
2938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939eval_index(
2940 char_u **arg,
2941 typval_T *rettv,
2942 int evaluate,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002943 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002944{
2945 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002947 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002948 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002949 long len = -1;
2950 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002951 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002952 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002953
Bram Moolenaara03f2332016-02-06 18:09:59 +01002954 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002955 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01002956 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002957 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002958 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002959 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002960 return FAIL;
2961 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002962#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01002963 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002964 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002965 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02002966#endif
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01002967 case VAR_BOOL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002968 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002969 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002970 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002971 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002972 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01002973 return FAIL;
2974 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002975 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002977 if (evaluate)
2978 return FAIL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002979 // FALLTHROUGH
Bram Moolenaara03f2332016-02-06 18:09:59 +01002980
2981 case VAR_STRING:
2982 case VAR_NUMBER:
2983 case VAR_LIST:
2984 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002985 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01002986 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002987 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002988
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02002989 init_tv(&var1);
2990 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002991 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002992 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002993 /*
2994 * dict.name
2995 */
2996 key = *arg + 1;
2997 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2998 ;
2999 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003000 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003002 }
3003 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003004 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003005 /*
3006 * something[idx]
3007 *
3008 * Get the (first) variable from inside the [].
3009 */
3010 *arg = skipwhite(*arg + 1);
3011 if (**arg == ':')
3012 empty1 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003013 else if (eval1(arg, &var1, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003014 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003015 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003016 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003017 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003018 clear_tv(&var1);
3019 return FAIL;
3020 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003021
3022 /*
3023 * Get the second variable from inside the [:].
3024 */
3025 if (**arg == ':')
3026 {
3027 range = TRUE;
3028 *arg = skipwhite(*arg + 1);
3029 if (**arg == ']')
3030 empty2 = TRUE;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003031 else if (eval1(arg, &var2, evaluate) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00003032 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003033 if (!empty1)
3034 clear_tv(&var1);
3035 return FAIL;
3036 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003037 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003038 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003039 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003040 if (!empty1)
3041 clear_tv(&var1);
3042 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003043 return FAIL;
3044 }
3045 }
3046
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003047 // Check for the ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00003048 if (**arg != ']')
3049 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003050 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003051 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003052 clear_tv(&var1);
3053 if (range)
3054 clear_tv(&var2);
3055 return FAIL;
3056 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003057 *arg = skipwhite(*arg + 1); // skip the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003058 }
3059
3060 if (evaluate)
3061 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003062 n1 = 0;
3063 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003064 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003065 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003067 }
3068 if (range)
3069 {
3070 if (empty2)
3071 n2 = -1;
3072 else
3073 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003074 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003075 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003076 }
3077 }
3078
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003080 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003081 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003082 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 case VAR_VOID:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003084 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003085 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003086 case VAR_FLOAT:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01003087 case VAR_BOOL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003088 case VAR_SPECIAL:
3089 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003090 case VAR_CHANNEL:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003091 break; // not evaluating, skipping over subscript
Bram Moolenaara03f2332016-02-06 18:09:59 +01003092
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003093 case VAR_NUMBER:
3094 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003095 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003096 len = (long)STRLEN(s);
3097 if (range)
3098 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003099 // The resulting variable is a substring. If the indexes
3100 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003101 if (n1 < 0)
3102 {
3103 n1 = len + n1;
3104 if (n1 < 0)
3105 n1 = 0;
3106 }
3107 if (n2 < 0)
3108 n2 = len + n2;
3109 else if (n2 >= len)
3110 n2 = len;
3111 if (n1 >= len || n2 < 0 || n1 > n2)
3112 s = NULL;
3113 else
3114 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3115 }
3116 else
3117 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003118 // The resulting variable is a string of a single
3119 // character. If the index is too big or negative the
3120 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003121 if (n1 >= len || n1 < 0)
3122 s = NULL;
3123 else
3124 s = vim_strnsave(s + n1, 1);
3125 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003126 clear_tv(rettv);
3127 rettv->v_type = VAR_STRING;
3128 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003129 break;
3130
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003131 case VAR_BLOB:
3132 len = blob_len(rettv->vval.v_blob);
3133 if (range)
3134 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003135 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003136 // are out of range the result is empty.
3137 if (n1 < 0)
3138 {
3139 n1 = len + n1;
3140 if (n1 < 0)
3141 n1 = 0;
3142 }
3143 if (n2 < 0)
3144 n2 = len + n2;
3145 else if (n2 >= len)
3146 n2 = len - 1;
3147 if (n1 >= len || n2 < 0 || n1 > n2)
3148 {
3149 clear_tv(rettv);
3150 rettv->v_type = VAR_BLOB;
3151 rettv->vval.v_blob = NULL;
3152 }
3153 else
3154 {
3155 blob_T *blob = blob_alloc();
3156
3157 if (blob != NULL)
3158 {
3159 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3160 {
3161 blob_free(blob);
3162 return FAIL;
3163 }
3164 blob->bv_ga.ga_len = n2 - n1 + 1;
3165 for (i = n1; i <= n2; i++)
3166 blob_set(blob, i - n1,
3167 blob_get(rettv->vval.v_blob, i));
3168
3169 clear_tv(rettv);
3170 rettv_blob_set(rettv, blob);
3171 }
3172 }
3173 }
3174 else
3175 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003176 // The resulting variable is a byte value.
3177 // If the index is too big or negative that is an error.
3178 if (n1 < 0)
3179 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003180 if (n1 < len && n1 >= 0)
3181 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003182 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003183
3184 clear_tv(rettv);
3185 rettv->v_type = VAR_NUMBER;
3186 rettv->vval.v_number = v;
3187 }
3188 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003189 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003190 }
3191 break;
3192
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003193 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003194 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003195 if (n1 < 0)
3196 n1 = len + n1;
3197 if (!empty1 && (n1 < 0 || n1 >= len))
3198 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003199 // For a range we allow invalid values and return an empty
3200 // list. A list index out of range is an error.
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003201 if (!range)
3202 {
3203 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003204 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003205 return FAIL;
3206 }
3207 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003208 }
3209 if (range)
3210 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003211 list_T *l;
3212 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003213
3214 if (n2 < 0)
3215 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003216 else if (n2 >= len)
3217 n2 = len - 1;
3218 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003219 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003220 l = list_alloc();
3221 if (l == NULL)
3222 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003223 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003224 n1 <= n2; ++n1)
3225 {
3226 if (list_append_tv(l, &item->li_tv) == FAIL)
3227 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003228 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003229 return FAIL;
3230 }
3231 item = item->li_next;
3232 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003233 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003234 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003235 }
3236 else
3237 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003238 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003239 clear_tv(rettv);
3240 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003241 }
3242 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003243
3244 case VAR_DICT:
3245 if (range)
3246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003247 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003248 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003249 if (len == -1)
3250 clear_tv(&var1);
3251 return FAIL;
3252 }
3253 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003255
3256 if (len == -1)
3257 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003258 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003259 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003260 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003261 clear_tv(&var1);
3262 return FAIL;
3263 }
3264 }
3265
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003266 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003267
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003268 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003269 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003270 if (len == -1)
3271 clear_tv(&var1);
3272 if (item == NULL)
3273 return FAIL;
3274
3275 copy_tv(&item->di_tv, &var1);
3276 clear_tv(rettv);
3277 *rettv = var1;
3278 }
3279 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003280 }
3281 }
3282
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003283 return OK;
3284}
3285
3286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 * Get an option value.
3288 * "arg" points to the '&' or '+' before the option name.
3289 * "arg" is advanced to character after the option name.
3290 * Return OK or FAIL.
3291 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293get_option_tv(
3294 char_u **arg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003295 typval_T *rettv, // when NULL, only check if option exists
Bram Moolenaar7454a062016-01-30 15:14:10 +01003296 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297{
3298 char_u *option_end;
3299 long numval;
3300 char_u *stringval;
3301 int opt_type;
3302 int c;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003303 int working = (**arg == '+'); // has("+option")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int ret = OK;
3305 int opt_flags;
3306
3307 /*
3308 * Isolate the option name and find its value.
3309 */
3310 option_end = find_option_end(arg, &opt_flags);
3311 if (option_end == NULL)
3312 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003313 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003314 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 return FAIL;
3316 }
3317
3318 if (!evaluate)
3319 {
3320 *arg = option_end;
3321 return OK;
3322 }
3323
3324 c = *option_end;
3325 *option_end = NUL;
3326 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003327 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003329 if (opt_type == -3) // invalid name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003331 if (rettv != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 semsg(_(e_unknown_option), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 ret = FAIL;
3334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003335 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003337 if (opt_type == -2) // hidden string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003339 rettv->v_type = VAR_STRING;
3340 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003342 else if (opt_type == -1) // hidden number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003344 rettv->v_type = VAR_NUMBER;
3345 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003347 else if (opt_type == 1) // number option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003349 rettv->v_type = VAR_NUMBER;
3350 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003352 else // string option
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003354 rettv->v_type = VAR_STRING;
3355 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 }
3357 }
3358 else if (working && (opt_type == -2 || opt_type == -1))
3359 ret = FAIL;
3360
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003361 *option_end = c; // put back for error messages
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 *arg = option_end;
3363
3364 return ret;
3365}
3366
3367/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 * Allocate a variable for a number constant. Also deals with "0z" for blob.
3369 * Return OK or FAIL.
3370 */
3371 int
3372get_number_tv(
3373 char_u **arg,
3374 typval_T *rettv,
3375 int evaluate,
3376 int want_string UNUSED)
3377{
3378 int len;
3379#ifdef FEAT_FLOAT
3380 char_u *p;
3381 int get_float = FALSE;
3382
3383 // We accept a float when the format matches
3384 // "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
3385 // strict to avoid backwards compatibility problems.
3386 // With script version 2 and later the leading digit can be
3387 // omitted.
3388 // Don't look for a float after the "." operator, so that
3389 // ":let vers = 1.2.3" doesn't fail.
3390 if (**arg == '.')
3391 p = *arg;
3392 else
3393 p = skipdigits(*arg + 1);
3394 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
3395 {
3396 get_float = TRUE;
3397 p = skipdigits(p + 2);
3398 if (*p == 'e' || *p == 'E')
3399 {
3400 ++p;
3401 if (*p == '-' || *p == '+')
3402 ++p;
3403 if (!vim_isdigit(*p))
3404 get_float = FALSE;
3405 else
3406 p = skipdigits(p + 1);
3407 }
3408 if (ASCII_ISALPHA(*p) || *p == '.')
3409 get_float = FALSE;
3410 }
3411 if (get_float)
3412 {
3413 float_T f;
3414
3415 *arg += string2float(*arg, &f);
3416 if (evaluate)
3417 {
3418 rettv->v_type = VAR_FLOAT;
3419 rettv->vval.v_float = f;
3420 }
3421 }
3422 else
3423#endif
3424 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
3425 {
3426 char_u *bp;
3427 blob_T *blob = NULL; // init for gcc
3428
3429 // Blob constant: 0z0123456789abcdef
3430 if (evaluate)
3431 blob = blob_alloc();
3432 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
3433 {
3434 if (!vim_isxdigit(bp[1]))
3435 {
3436 if (blob != NULL)
3437 {
3438 emsg(_("E973: Blob literal should have an even number of hex characters"));
3439 ga_clear(&blob->bv_ga);
3440 VIM_CLEAR(blob);
3441 }
3442 return FAIL;
3443 }
3444 if (blob != NULL)
3445 ga_append(&blob->bv_ga,
3446 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
3447 if (bp[2] == '.' && vim_isxdigit(bp[3]))
3448 ++bp;
3449 }
3450 if (blob != NULL)
3451 rettv_blob_set(rettv, blob);
3452 *arg = bp;
3453 }
3454 else
3455 {
3456 varnumber_T n;
3457
3458 // decimal, hex or octal number
3459 vim_str2nr(*arg, NULL, &len, current_sctx.sc_version >= 4
3460 ? STR2NR_NO_OCT + STR2NR_QUOTE
3461 : STR2NR_ALL, &n, NULL, 0, TRUE);
3462 if (len == 0)
3463 {
3464 semsg(_(e_invexpr2), *arg);
3465 return FAIL;
3466 }
3467 *arg += len;
3468 if (evaluate)
3469 {
3470 rettv->v_type = VAR_NUMBER;
3471 rettv->vval.v_number = n;
3472 }
3473 }
3474 return OK;
3475}
3476
3477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 * Allocate a variable for a string constant.
3479 * Return OK or FAIL.
3480 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003482get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483{
3484 char_u *p;
3485 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 int extra = 0;
3487
3488 /*
3489 * Find the end of the string, skipping backslashed characters.
3490 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003491 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 {
3493 if (*p == '\\' && p[1] != NUL)
3494 {
3495 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003496 // A "\<x>" form occupies at least 4 characters, and produces up
3497 // to 6 characters: reserve space for 2 extra
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 if (*p == '<')
3499 extra += 2;
3500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 }
3502
3503 if (*p != '"')
3504 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003505 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 return FAIL;
3507 }
3508
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003509 // If only parsing, set *arg and return here
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 if (!evaluate)
3511 {
3512 *arg = p + 1;
3513 return OK;
3514 }
3515
3516 /*
3517 * Copy the string into allocated memory, handling backslashed
3518 * characters.
3519 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003520 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (name == NULL)
3522 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003523 rettv->v_type = VAR_STRING;
3524 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525
Bram Moolenaar8c711452005-01-14 21:53:12 +00003526 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003527 {
3528 if (*p == '\\')
3529 {
3530 switch (*++p)
3531 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003532 case 'b': *name++ = BS; ++p; break;
3533 case 'e': *name++ = ESC; ++p; break;
3534 case 'f': *name++ = FF; ++p; break;
3535 case 'n': *name++ = NL; ++p; break;
3536 case 'r': *name++ = CAR; ++p; break;
3537 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003539 case 'X': // hex: "\x1", "\x12"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003540 case 'x':
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003541 case 'u': // Unicode: "\u0023"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 case 'U':
3543 if (vim_isxdigit(p[1]))
3544 {
3545 int n, nr;
3546 int c = toupper(*p);
3547
3548 if (c == 'X')
3549 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003550 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003552 else
3553 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 nr = 0;
3555 while (--n >= 0 && vim_isxdigit(p[1]))
3556 {
3557 ++p;
3558 nr = (nr << 4) + hex2nr(*p);
3559 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003560 ++p;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003561 // For "\u" store the number according to
3562 // 'encoding'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003564 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003566 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003570 // octal: "\1", "\12", "\123"
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 case '0':
3572 case '1':
3573 case '2':
3574 case '3':
3575 case '4':
3576 case '5':
3577 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003578 case '7': *name = *p++ - '0';
3579 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003581 *name = (*name << 3) + *p++ - '0';
3582 if (*p >= '0' && *p <= '7')
3583 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003585 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 break;
3587
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003588 // Special key, e.g.: "\<C-W>"
Bram Moolenaar459fd782019-10-13 16:43:39 +02003589 case '<': extra = trans_special(&p, name, TRUE, TRUE,
3590 TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 if (extra != 0)
3592 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003593 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 break;
3595 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003596 // FALLTHROUGH
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 break;
3600 }
3601 }
3602 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003603 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 *name = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003607 if (*p != NUL) // just in case
Bram Moolenaardb249f22016-08-26 16:29:47 +02003608 ++p;
3609 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 return OK;
3612}
3613
3614/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 * Return OK or FAIL.
3617 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003619get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620{
3621 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003622 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003623 int reduce = 0;
3624
3625 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003626 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003627 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003628 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003629 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003630 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003631 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003632 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003633 break;
3634 ++reduce;
3635 ++p;
3636 }
3637 }
3638
Bram Moolenaar8c711452005-01-14 21:53:12 +00003639 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003640 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003641 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003642 return FAIL;
3643 }
3644
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003645 // If only parsing return after setting "*arg"
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003646 if (!evaluate)
3647 {
3648 *arg = p + 1;
3649 return OK;
3650 }
3651
3652 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003653 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003654 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003655 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003656 if (str == NULL)
3657 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003658 rettv->v_type = VAR_STRING;
3659 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003660
Bram Moolenaar8c711452005-01-14 21:53:12 +00003661 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003662 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003663 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003664 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003666 break;
3667 ++p;
3668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003669 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003670 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003672 *arg = p + 1;
3673
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003674 return OK;
3675}
3676
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003677/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02003678 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003679 */
3680 char_u *
3681partial_name(partial_T *pt)
3682{
3683 if (pt->pt_name != NULL)
3684 return pt->pt_name;
3685 return pt->pt_func->uf_name;
3686}
3687
Bram Moolenaarddecc252016-04-06 22:59:37 +02003688 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003689partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003690{
3691 int i;
3692
3693 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003694 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003695 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003696 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003697 if (pt->pt_name != NULL)
3698 {
3699 func_unref(pt->pt_name);
3700 vim_free(pt->pt_name);
3701 }
3702 else
3703 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003704 vim_free(pt);
3705}
3706
3707/*
3708 * Unreference a closure: decrement the reference count and free it when it
3709 * becomes zero.
3710 */
3711 void
3712partial_unref(partial_T *pt)
3713{
3714 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003715 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003716}
3717
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003718static int tv_equal_recurse_limit;
3719
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003720 static int
3721func_equal(
3722 typval_T *tv1,
3723 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003724 int ic) // ignore case
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003725{
3726 char_u *s1, *s2;
3727 dict_T *d1, *d2;
3728 int a1, a2;
3729 int i;
3730
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003731 // empty and NULL function name considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003732 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003733 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003734 if (s1 != NULL && *s1 == NUL)
3735 s1 = NULL;
3736 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003737 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003738 if (s2 != NULL && *s2 == NUL)
3739 s2 = NULL;
3740 if (s1 == NULL || s2 == NULL)
3741 {
3742 if (s1 != s2)
3743 return FALSE;
3744 }
3745 else if (STRCMP(s1, s2) != 0)
3746 return FALSE;
3747
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003748 // empty dict and NULL dict is different
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003749 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
3750 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
3751 if (d1 == NULL || d2 == NULL)
3752 {
3753 if (d1 != d2)
3754 return FALSE;
3755 }
3756 else if (!dict_equal(d1, d2, ic, TRUE))
3757 return FALSE;
3758
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003759 // empty list and no list considered the same
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003760 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
3761 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
3762 if (a1 != a2)
3763 return FALSE;
3764 for (i = 0; i < a1; ++i)
3765 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
3766 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
3767 return FALSE;
3768
3769 return TRUE;
3770}
3771
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003772/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003773 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003774 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003775 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003776 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003777 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003778tv_equal(
3779 typval_T *tv1,
3780 typval_T *tv2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003781 int ic, // ignore case
3782 int recursive) // TRUE when used recursively
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003783{
3784 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003785 char_u *s1, *s2;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003786 static int recursive_cnt = 0; // catch recursive loops
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003787 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003788
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003789 // Catch lists and dicts that have an endless loop by limiting
3790 // recursiveness to a limit. We guess they are equal then.
3791 // A fixed limit has the problem of still taking an awful long time.
3792 // Reduce the limit every time running into it. That should work fine for
3793 // deeply linked structures that are not recursively linked and catch
3794 // recursiveness quickly.
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003795 if (!recursive)
3796 tv_equal_recurse_limit = 1000;
3797 if (recursive_cnt >= tv_equal_recurse_limit)
3798 {
3799 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00003800 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003801 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003803 // For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
3804 // arguments.
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003805 if ((tv1->v_type == VAR_FUNC
3806 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
3807 && (tv2->v_type == VAR_FUNC
3808 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
3809 {
3810 ++recursive_cnt;
3811 r = func_equal(tv1, tv2, ic);
3812 --recursive_cnt;
3813 return r;
3814 }
3815
3816 if (tv1->v_type != tv2->v_type)
3817 return FALSE;
3818
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003819 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003820 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003821 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003822 ++recursive_cnt;
3823 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
3824 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003825 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003826
3827 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003828 ++recursive_cnt;
3829 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
3830 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00003831 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003832
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003833 case VAR_BLOB:
3834 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
3835
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003836 case VAR_NUMBER:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 case VAR_BOOL:
3838 case VAR_SPECIAL:
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003839 return tv1->vval.v_number == tv2->vval.v_number;
3840
3841 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003842 s1 = tv_get_string_buf(tv1, buf1);
3843 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003844 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003845
Bram Moolenaar835dc632016-02-07 14:27:38 +01003846 case VAR_FLOAT:
3847#ifdef FEAT_FLOAT
3848 return tv1->vval.v_float == tv2->vval.v_float;
3849#endif
3850 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003851#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01003852 return tv1->vval.v_job == tv2->vval.v_job;
3853#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01003854 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01003855#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01003856 return tv1->vval.v_channel == tv2->vval.v_channel;
3857#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01003859 case VAR_PARTIAL:
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003860 return tv1->vval.v_partial == tv2->vval.v_partial;
3861
3862 case VAR_FUNC:
3863 return tv1->vval.v_string == tv2->vval.v_string;
3864
Bram Moolenaar835dc632016-02-07 14:27:38 +01003865 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003866 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 case VAR_VOID:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003868 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003869 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00003870
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003871 // VAR_UNKNOWN can be the result of a invalid expression, let's say it
3872 // does not equal anything, not even itself.
Bram Moolenaara03f2332016-02-06 18:09:59 +01003873 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003874}
3875
3876/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003877 * Return the next (unique) copy ID.
3878 * Used for serializing nested structures.
3879 */
3880 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003881get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003882{
3883 current_copyID += COPYID_INC;
3884 return current_copyID;
3885}
3886
3887/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003888 * Garbage collection for lists and dictionaries.
3889 *
3890 * We use reference counts to be able to free most items right away when they
3891 * are no longer used. But for composite items it's possible that it becomes
3892 * unused while the reference count is > 0: When there is a recursive
3893 * reference. Example:
3894 * :let l = [1, 2, 3]
3895 * :let d = {9: l}
3896 * :let l[1] = d
3897 *
3898 * Since this is quite unusual we handle this with garbage collection: every
3899 * once in a while find out which lists and dicts are not referenced from any
3900 * variable.
3901 *
3902 * Here is a good reference text about garbage collection (refers to Python
3903 * but it applies to all reference-counting mechanisms):
3904 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00003905 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00003906
3907/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003908 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02003909 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003910 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00003911 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003912 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003913garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00003914{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003915 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003916 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003917 buf_T *buf;
3918 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01003919 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003920 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003921
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003922 if (!testing)
3923 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003924 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02003925 want_garbage_collect = FALSE;
3926 may_garbage_collect = FALSE;
3927 garbage_collect_at_exit = FALSE;
3928 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00003929
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01003930 // The execution stack can grow big, limit the size.
3931 if (exestack.ga_maxlen - exestack.ga_len > 500)
3932 {
3933 size_t new_len;
3934 char_u *pp;
3935 int n;
3936
3937 // Keep 150% of the current size, with a minimum of the growth size.
3938 n = exestack.ga_len / 2;
3939 if (n < exestack.ga_growsize)
3940 n = exestack.ga_growsize;
3941
3942 // Don't make it bigger though.
3943 if (exestack.ga_len + n < exestack.ga_maxlen)
3944 {
3945 new_len = exestack.ga_itemsize * (exestack.ga_len + n);
3946 pp = vim_realloc(exestack.ga_data, new_len);
3947 if (pp == NULL)
3948 return FAIL;
3949 exestack.ga_maxlen = exestack.ga_len + n;
3950 exestack.ga_data = pp;
3951 }
3952 }
3953
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003954 // We advance by two because we add one for items referenced through
3955 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003956 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003957
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003958 /*
3959 * 1. Go through all accessible variables and mark all lists and dicts
3960 * with copyID.
3961 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003962
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003963 // Don't free variables in the previous_funccal list unless they are only
3964 // referenced through previous_funccal. This must be first, because if
3965 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003966 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00003967
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003968 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02003969 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003970
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003971 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003972 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003973 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
3974 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003975
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003976 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003977 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003978 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3979 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02003980 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003981 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
3982 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01003983#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003984 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003985 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3986 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003987 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02003988 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003989 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
3990 NULL, NULL);
3991#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003993 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02003994 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01003995 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
3996 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003997 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02003998 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00003999
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004000 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004001 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004003 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004004 abort = abort || set_ref_in_functions(copyID);
4005
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004006 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004007 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004008
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004009 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004010 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004011
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004012 // callbacks in buffers
4013 abort = abort || set_ref_in_buffers(copyID);
4014
Bram Moolenaar1dced572012-04-05 16:54:08 +02004015#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004016 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004017#endif
4018
Bram Moolenaardb913952012-06-29 12:54:53 +02004019#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004020 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004021#endif
4022
4023#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004024 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004025#endif
4026
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004027#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004028 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004029 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004030#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004031#ifdef FEAT_NETBEANS_INTG
4032 abort = abort || set_ref_in_nb_channel(copyID);
4033#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004034
Bram Moolenaare3188e22016-05-31 21:13:04 +02004035#ifdef FEAT_TIMERS
4036 abort = abort || set_ref_in_timer(copyID);
4037#endif
4038
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004039#ifdef FEAT_QUICKFIX
4040 abort = abort || set_ref_in_quickfix(copyID);
4041#endif
4042
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004043#ifdef FEAT_TERMINAL
4044 abort = abort || set_ref_in_term(copyID);
4045#endif
4046
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01004047#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004048 abort = abort || set_ref_in_popups(copyID);
4049#endif
4050
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004051 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004052 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004053 /*
4054 * 2. Free lists and dictionaries that are not referenced.
4055 */
4056 did_free = free_unref_items(copyID);
4057
4058 /*
4059 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004060 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004061 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004062 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004063 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004064 else if (p_verbose > 0)
4065 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004066 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004067 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004068
4069 return did_free;
4070}
4071
4072/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004073 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004074 */
4075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004076free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004077{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004078 int did_free = FALSE;
4079
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004080 // Let all "free" functions know that we are here. This means no
4081 // dictionaries, lists, channels or jobs are to be freed, because we will
4082 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004083 in_free_unref_items = TRUE;
4084
4085 /*
4086 * PASS 1: free the contents of the items. We don't free the items
4087 * themselves yet, so that it is possible to decrement refcount counters
4088 */
4089
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004090 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02004091 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004092
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004093 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02004094 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004095
4096#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004097 // Go through the list of jobs and free items without the copyID. This
4098 // must happen before doing channels, because jobs refer to channels, but
4099 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004100 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4101
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004102 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004103 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4104#endif
4105
4106 /*
4107 * PASS 2: free the items themselves.
4108 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004109 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004110 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004111
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004112#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004113 // Go through the list of jobs and free items without the copyID. This
4114 // must happen before doing channels, because jobs refer to channels, but
4115 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004116 free_unused_jobs(copyID, COPYID_MASK);
4117
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004118 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004119 free_unused_channels(copyID, COPYID_MASK);
4120#endif
4121
4122 in_free_unref_items = FALSE;
4123
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004124 return did_free;
4125}
4126
4127/*
4128 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004129 * "list_stack" is used to add lists to be marked. Can be NULL.
4130 *
4131 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004132 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004133 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004134set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004135{
4136 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004137 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004138 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004139 hashtab_T *cur_ht;
4140 ht_stack_T *ht_stack = NULL;
4141 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004142
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004143 cur_ht = ht;
4144 for (;;)
4145 {
4146 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004147 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004148 // Mark each item in the hashtab. If the item contains a hashtab
4149 // it is added to ht_stack, if it contains a list it is added to
4150 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004151 todo = (int)cur_ht->ht_used;
4152 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4153 if (!HASHITEM_EMPTY(hi))
4154 {
4155 --todo;
4156 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4157 &ht_stack, list_stack);
4158 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004159 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004160
4161 if (ht_stack == NULL)
4162 break;
4163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004164 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004165 cur_ht = ht_stack->ht;
4166 tempitem = ht_stack;
4167 ht_stack = ht_stack->prev;
4168 free(tempitem);
4169 }
4170
4171 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004172}
4173
4174/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004175 * Mark a dict and its items with "copyID".
4176 * Returns TRUE if setting references failed somehow.
4177 */
4178 int
4179set_ref_in_dict(dict_T *d, int copyID)
4180{
4181 if (d != NULL && d->dv_copyID != copyID)
4182 {
4183 d->dv_copyID = copyID;
4184 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4185 }
4186 return FALSE;
4187}
4188
4189/*
4190 * Mark a list and its items with "copyID".
4191 * Returns TRUE if setting references failed somehow.
4192 */
4193 int
4194set_ref_in_list(list_T *ll, int copyID)
4195{
4196 if (ll != NULL && ll->lv_copyID != copyID)
4197 {
4198 ll->lv_copyID = copyID;
4199 return set_ref_in_list_items(ll, copyID, NULL);
4200 }
4201 return FALSE;
4202}
4203
4204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004205 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004206 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4207 *
4208 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004209 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004210 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004211set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004212{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004213 listitem_T *li;
4214 int abort = FALSE;
4215 list_T *cur_l;
4216 list_stack_T *list_stack = NULL;
4217 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004218
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004219 cur_l = l;
4220 for (;;)
4221 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01004222 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004223 // Mark each item in the list. If the item contains a hashtab
4224 // it is added to ht_stack, if it contains a list it is added to
4225 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004226 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4227 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4228 ht_stack, &list_stack);
4229 if (list_stack == NULL)
4230 break;
4231
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004232 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004233 cur_l = list_stack->list;
4234 tempitem = list_stack;
4235 list_stack = list_stack->prev;
4236 free(tempitem);
4237 }
4238
4239 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004240}
4241
4242/*
4243 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004244 * "list_stack" is used to add lists to be marked. Can be NULL.
4245 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4246 *
4247 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004248 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004249 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250set_ref_in_item(
4251 typval_T *tv,
4252 int copyID,
4253 ht_stack_T **ht_stack,
4254 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004255{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004256 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004257
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004258 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004259 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004260 dict_T *dd = tv->vval.v_dict;
4261
Bram Moolenaara03f2332016-02-06 18:09:59 +01004262 if (dd != NULL && dd->dv_copyID != copyID)
4263 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004264 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004265 dd->dv_copyID = copyID;
4266 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004267 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004268 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4269 }
4270 else
4271 {
4272 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4273 if (newitem == NULL)
4274 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004275 else
4276 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004277 newitem->ht = &dd->dv_hashtab;
4278 newitem->prev = *ht_stack;
4279 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004280 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004281 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004282 }
4283 }
4284 else if (tv->v_type == VAR_LIST)
4285 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004286 list_T *ll = tv->vval.v_list;
4287
Bram Moolenaara03f2332016-02-06 18:09:59 +01004288 if (ll != NULL && ll->lv_copyID != copyID)
4289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004290 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01004291 ll->lv_copyID = copyID;
4292 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004293 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004294 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004295 }
4296 else
4297 {
4298 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004299 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004300 if (newitem == NULL)
4301 abort = TRUE;
4302 else
4303 {
4304 newitem->list = ll;
4305 newitem->prev = *list_stack;
4306 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004307 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004308 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004309 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004310 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004311 else if (tv->v_type == VAR_FUNC)
4312 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004313 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004314 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004315 else if (tv->v_type == VAR_PARTIAL)
4316 {
4317 partial_T *pt = tv->vval.v_partial;
4318 int i;
4319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004320 // A partial does not have a copyID, because it cannot contain itself.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004321 if (pt != NULL)
4322 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004323 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004324
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004325 if (pt->pt_dict != NULL)
4326 {
4327 typval_T dtv;
4328
4329 dtv.v_type = VAR_DICT;
4330 dtv.vval.v_dict = pt->pt_dict;
4331 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4332 }
4333
4334 for (i = 0; i < pt->pt_argc; ++i)
4335 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4336 ht_stack, list_stack);
4337 }
4338 }
4339#ifdef FEAT_JOB_CHANNEL
4340 else if (tv->v_type == VAR_JOB)
4341 {
4342 job_T *job = tv->vval.v_job;
4343 typval_T dtv;
4344
4345 if (job != NULL && job->jv_copyID != copyID)
4346 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004347 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004348 if (job->jv_channel != NULL)
4349 {
4350 dtv.v_type = VAR_CHANNEL;
4351 dtv.vval.v_channel = job->jv_channel;
4352 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4353 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004354 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004355 {
4356 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004357 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004358 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4359 }
4360 }
4361 }
4362 else if (tv->v_type == VAR_CHANNEL)
4363 {
4364 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004365 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004366 typval_T dtv;
4367 jsonq_T *jq;
4368 cbq_T *cq;
4369
4370 if (ch != NULL && ch->ch_copyID != copyID)
4371 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004372 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004373 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004374 {
4375 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4376 jq = jq->jq_next)
4377 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4378 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4379 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004380 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004381 {
4382 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004383 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004384 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4385 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004386 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004387 {
4388 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004389 dtv.vval.v_partial =
4390 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004391 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4392 }
4393 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004394 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004395 {
4396 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004397 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004398 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4399 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004400 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004401 {
4402 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004403 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004404 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4405 }
4406 }
4407 }
4408#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004409 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004410}
4411
Bram Moolenaar8c711452005-01-14 21:53:12 +00004412/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004413 * Return a string with the string representation of a variable.
4414 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004415 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004416 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004417 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4418 * stings as "string()", otherwise does not put quotes around strings, as
4419 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004420 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4421 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004422 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004423 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004424 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004425echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004426 typval_T *tv,
4427 char_u **tofree,
4428 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004429 int copyID,
4430 int echo_style,
4431 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004432 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004433{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004434 static int recurse = 0;
4435 char_u *r = NULL;
4436
Bram Moolenaar33570922005-01-25 22:26:29 +00004437 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004438 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004439 if (!did_echo_string_emsg)
4440 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004441 // Only give this message once for a recursive call to avoid
4442 // flooding the user with errors. And stop iterating over lists
4443 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02004444 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004445 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004447 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004448 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004449 }
4450 ++recurse;
4451
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004452 switch (tv->v_type)
4453 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004454 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004455 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004456 {
4457 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004458 r = tv->vval.v_string;
4459 if (r == NULL)
4460 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004461 }
4462 else
4463 {
4464 *tofree = string_quote(tv->vval.v_string, FALSE);
4465 r = *tofree;
4466 }
4467 break;
4468
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004469 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004470 if (echo_style)
4471 {
4472 *tofree = NULL;
4473 r = tv->vval.v_string;
4474 }
4475 else
4476 {
4477 *tofree = string_quote(tv->vval.v_string, TRUE);
4478 r = *tofree;
4479 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004480 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004481
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004482 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004483 {
4484 partial_T *pt = tv->vval.v_partial;
4485 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004486 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004487 garray_T ga;
4488 int i;
4489 char_u *tf;
4490
4491 ga_init2(&ga, 1, 100);
4492 ga_concat(&ga, (char_u *)"function(");
4493 if (fname != NULL)
4494 {
4495 ga_concat(&ga, fname);
4496 vim_free(fname);
4497 }
4498 if (pt != NULL && pt->pt_argc > 0)
4499 {
4500 ga_concat(&ga, (char_u *)", [");
4501 for (i = 0; i < pt->pt_argc; ++i)
4502 {
4503 if (i > 0)
4504 ga_concat(&ga, (char_u *)", ");
4505 ga_concat(&ga,
4506 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4507 vim_free(tf);
4508 }
4509 ga_concat(&ga, (char_u *)"]");
4510 }
4511 if (pt != NULL && pt->pt_dict != NULL)
4512 {
4513 typval_T dtv;
4514
4515 ga_concat(&ga, (char_u *)", ");
4516 dtv.v_type = VAR_DICT;
4517 dtv.vval.v_dict = pt->pt_dict;
4518 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4519 vim_free(tf);
4520 }
4521 ga_concat(&ga, (char_u *)")");
4522
4523 *tofree = ga.ga_data;
4524 r = *tofree;
4525 break;
4526 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004527
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004528 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004529 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004530 break;
4531
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004533 if (tv->vval.v_list == NULL)
4534 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02004535 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004536 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02004537 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004538 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004539 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4540 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004541 {
4542 *tofree = NULL;
4543 r = (char_u *)"[...]";
4544 }
4545 else
4546 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004547 int old_copyID = tv->vval.v_list->lv_copyID;
4548
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004549 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004550 *tofree = list2string(tv, copyID, restore_copyID);
4551 if (restore_copyID)
4552 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004553 r = *tofree;
4554 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004555 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004556
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004558 if (tv->vval.v_dict == NULL)
4559 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004560 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004561 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004562 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004563 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004564 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4565 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004566 {
4567 *tofree = NULL;
4568 r = (char_u *)"{...}";
4569 }
4570 else
4571 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004572 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02004573
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004574 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004575 *tofree = dict2string(tv, copyID, restore_copyID);
4576 if (restore_copyID)
4577 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004578 r = *tofree;
4579 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004580 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004581
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004582 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004583 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004584 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004586 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004587 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004588 break;
4589
Bram Moolenaar835dc632016-02-07 14:27:38 +01004590 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004591 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004592 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004593 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004594 if (composite_val)
4595 {
4596 *tofree = string_quote(r, FALSE);
4597 r = *tofree;
4598 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004599 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004600
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004601 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004602#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004603 *tofree = NULL;
4604 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4605 r = numbuf;
4606 break;
4607#endif
4608
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01004609 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004610 case VAR_SPECIAL:
4611 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004612 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004613 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004614 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004615
Bram Moolenaar8502c702014-06-17 12:51:16 +02004616 if (--recurse == 0)
4617 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004618 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004619}
4620
4621/*
4622 * Return a string with the string representation of a variable.
4623 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4624 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004625 * Does not put quotes around strings, as ":echo" displays values.
4626 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4627 * May return NULL.
4628 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004629 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004630echo_string(
4631 typval_T *tv,
4632 char_u **tofree,
4633 char_u *numbuf,
4634 int copyID)
4635{
4636 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4637}
4638
4639/*
4640 * Return a string with the string representation of a variable.
4641 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4642 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004643 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004644 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004645 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004646 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004647tv2string(
4648 typval_T *tv,
4649 char_u **tofree,
4650 char_u *numbuf,
4651 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004652{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004653 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004654}
4655
4656/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004657 * Return string "str" in ' quotes, doubling ' characters.
4658 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004659 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004660 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004661 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004662string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004663{
Bram Moolenaar33570922005-01-25 22:26:29 +00004664 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004665 char_u *p, *r, *s;
4666
Bram Moolenaar33570922005-01-25 22:26:29 +00004667 len = (function ? 13 : 3);
4668 if (str != NULL)
4669 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004670 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004671 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004672 if (*p == '\'')
4673 ++len;
4674 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004675 s = r = alloc(len);
4676 if (r != NULL)
4677 {
4678 if (function)
4679 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004680 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004681 r += 10;
4682 }
4683 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004684 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004685 if (str != NULL)
4686 for (p = str; *p != NUL; )
4687 {
4688 if (*p == '\'')
4689 *r++ = '\'';
4690 MB_COPY_CHAR(p, r);
4691 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004692 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004693 if (function)
4694 *r++ = ')';
4695 *r++ = NUL;
4696 }
4697 return s;
4698}
4699
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004700#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701/*
4702 * Convert the string "text" to a floating point number.
4703 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4704 * this always uses a decimal point.
4705 * Returns the length of the text that was consumed.
4706 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004708string2float(
4709 char_u *text,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004710 float_T *value) // result stored here
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711{
4712 char *s = (char *)text;
4713 float_T f;
4714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004715 // MS-Windows does not deal with "inf" and "nan" properly.
Bram Moolenaar62473612017-01-08 19:25:40 +01004716 if (STRNICMP(text, "inf", 3) == 0)
4717 {
4718 *value = INFINITY;
4719 return 3;
4720 }
4721 if (STRNICMP(text, "-inf", 3) == 0)
4722 {
4723 *value = -INFINITY;
4724 return 4;
4725 }
4726 if (STRNICMP(text, "nan", 3) == 0)
4727 {
4728 *value = NAN;
4729 return 3;
4730 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004731 f = strtod(s, &s);
4732 *value = f;
4733 return (int)((char_u *)s - text);
4734}
4735#endif
4736
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 * Get the value of an environment variable.
4739 * "arg" is pointing to the '$'. It is advanced to after the name.
4740 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004741 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004743 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004744get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 char_u *string = NULL;
4747 int len;
4748 int cc;
4749 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004750 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751
4752 ++*arg;
4753 name = *arg;
4754 len = get_env_len(arg);
4755 if (evaluate)
4756 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004757 if (len == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004758 return FAIL; // invalid empty name
Bram Moolenaar05159a02005-02-26 23:04:13 +00004759
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004760 cc = name[len];
4761 name[len] = NUL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004762 // first try vim_getenv(), fast for normal environment vars
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004763 string = vim_getenv(name, &mustfree);
4764 if (string != NULL && *string != NUL)
4765 {
4766 if (!mustfree)
4767 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004769 else
4770 {
4771 if (mustfree)
4772 vim_free(string);
4773
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004774 // next try expanding things like $VIM and ${HOME}
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004775 string = expand_env_save(name - 1);
4776 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01004777 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004778 }
4779 name[len] = cc;
4780
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004781 rettv->v_type = VAR_STRING;
4782 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 }
4784
4785 return OK;
4786}
4787
Bram Moolenaard6e256c2011-12-14 15:32:50 +01004788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004790 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004792 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004793var2fpos(
4794 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004795 int dollar_lnum, // TRUE when $ is last line
4796 int *fnum) // set to fnum for '0, 'A, etc.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004798 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00004800 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004802 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004803 if (varp->v_type == VAR_LIST)
4804 {
4805 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004806 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00004807 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00004808 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004809
4810 l = varp->vval.v_list;
4811 if (l == NULL)
4812 return NULL;
4813
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004814 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00004815 pos.lnum = list_find_nr(l, 0L, &error);
4816 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004817 return NULL; // invalid line number
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004818
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004819 // Get the column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004820 pos.col = list_find_nr(l, 1L, &error);
4821 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004822 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004823 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00004824
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004825 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00004826 li = list_find(l, 1L);
4827 if (li != NULL && li->li_tv.v_type == VAR_STRING
4828 && li->li_tv.vval.v_string != NULL
4829 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
4830 pos.col = len + 1;
4831
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004832 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00004833 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004834 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00004835 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004836
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004837 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00004838 pos.coladd = list_find_nr(l, 2L, &error);
4839 if (error)
4840 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004841
Bram Moolenaar32466aa2006-02-24 23:53:04 +00004842 return &pos;
4843 }
4844
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004845 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004846 if (name == NULL)
4847 return NULL;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004848 if (name[0] == '.') // cursor
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 return &curwin->w_cursor;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004850 if (name[0] == 'v' && name[1] == NUL) // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00004851 {
4852 if (VIsual_active)
4853 return &VIsual;
4854 return &curwin->w_cursor;
4855 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004856 if (name[0] == '\'') // mark
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004858 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
4860 return NULL;
4861 return pp;
4862 }
Bram Moolenaara5525202006-03-02 22:52:09 +00004863
Bram Moolenaara5525202006-03-02 22:52:09 +00004864 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00004865
Bram Moolenaar477933c2007-07-17 14:32:23 +00004866 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004867 {
4868 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004869 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004870 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004871 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004872 // In silent Ex mode topline is zero, but that's not a valid line
4873 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004874 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004875 return &pos;
4876 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004877 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004878 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00004879 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004880 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02004881 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00004882 return &pos;
4883 }
4884 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004885 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00004887 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
4889 pos.lnum = curbuf->b_ml.ml_line_count;
4890 pos.col = 0;
4891 }
4892 else
4893 {
4894 pos.lnum = curwin->w_cursor.lnum;
4895 pos.col = (colnr_T)STRLEN(ml_get_curline());
4896 }
4897 return &pos;
4898 }
4899 return NULL;
4900}
4901
4902/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004903 * Convert list in "arg" into a position and optional file number.
4904 * When "fnump" is NULL there is no file number, only 3 items.
4905 * Note that the column is passed on as-is, the caller may want to decrement
4906 * it to use 1 for the first column.
4907 * Return FAIL when conversion is not possible, doesn't check the position for
4908 * validity.
4909 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004911list2fpos(
4912 typval_T *arg,
4913 pos_T *posp,
4914 int *fnump,
4915 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004916{
4917 list_T *l = arg->vval.v_list;
4918 long i = 0;
4919 long n;
4920
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
4922 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00004923 if (arg->v_type != VAR_LIST
4924 || l == NULL
4925 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02004926 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004927 return FAIL;
4928
4929 if (fnump != NULL)
4930 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004931 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004932 if (n < 0)
4933 return FAIL;
4934 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004935 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004936 *fnump = n;
4937 }
4938
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004939 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004940 if (n < 0)
4941 return FAIL;
4942 posp->lnum = n;
4943
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004944 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004945 if (n < 0)
4946 return FAIL;
4947 posp->col = n;
4948
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004949 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004950 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00004951 posp->coladd = 0;
4952 else
4953 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004954
Bram Moolenaar493c1782014-05-28 14:34:46 +02004955 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004956 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02004957
Bram Moolenaar0e34f622006-03-03 23:00:03 +00004958 return OK;
4959}
4960
4961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 * Get the length of an environment variable name.
4963 * Advance "arg" to the first character after the name.
4964 * Return 0 for error.
4965 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02004966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004967get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968{
4969 char_u *p;
4970 int len;
4971
4972 for (p = *arg; vim_isIDc(*p); ++p)
4973 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004974 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 return 0;
4976
4977 len = (int)(p - *arg);
4978 *arg = p;
4979 return len;
4980}
4981
4982/*
4983 * Get the length of the name of a function or internal variable.
4984 * "arg" is advanced to the first non-white character after the name.
4985 * Return 0 if something is wrong.
4986 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004988get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989{
4990 char_u *p;
4991 int len;
4992
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004993 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01004995 {
4996 if (*p == ':')
4997 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004998 // "s:" is start of "s:var", but "n:" is not and can be used in
4999 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005000 len = (int)(p - *arg);
5001 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5002 || len > 1)
5003 break;
5004 }
5005 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005006 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 return 0;
5008
5009 len = (int)(p - *arg);
5010 *arg = skipwhite(p);
5011
5012 return len;
5013}
5014
5015/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005016 * Get the length of the name of a variable or function.
5017 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005019 * Return -1 if curly braces expansion failed.
5020 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 * If the name contains 'magic' {}'s, expand them and return the
5022 * expanded name in an allocated string via 'alias' - caller must free.
5023 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005025get_name_len(
5026 char_u **arg,
5027 char_u **alias,
5028 int evaluate,
5029 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
5031 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 char_u *p;
5033 char_u *expr_start;
5034 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037
5038 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5039 && (*arg)[2] == (int)KE_SNR)
5040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005041 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 *arg += 3;
5043 return get_id_len(arg) + 3;
5044 }
5045 len = eval_fname_script(*arg);
5046 if (len > 0)
5047 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005048 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 *arg += len;
5050 }
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005053 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005055 p = find_name_end(*arg, &expr_start, &expr_end,
5056 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 if (expr_start != NULL)
5058 {
5059 char_u *temp_string;
5060
5061 if (!evaluate)
5062 {
5063 len += (int)(p - *arg);
5064 *arg = skipwhite(p);
5065 return len;
5066 }
5067
5068 /*
5069 * Include any <SID> etc in the expanded string:
5070 * Thus the -len here.
5071 */
5072 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5073 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005074 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 *alias = temp_string;
5076 *arg = skipwhite(p);
5077 return (int)STRLEN(temp_string);
5078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
5080 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005081 // Only give an error when there is something, otherwise it will be
5082 // reported at a higher level.
5083 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005084 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085
5086 return len;
5087}
5088
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005089/*
5090 * Find the end of a variable or function name, taking care of magic braces.
5091 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5092 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005093 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005094 * Return a pointer to just after the name. Equal to "arg" if there is no
5095 * valid name.
5096 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005097 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005098find_name_end(
5099 char_u *arg,
5100 char_u **expr_start,
5101 char_u **expr_end,
5102 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005104 int mb_nest = 0;
5105 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005107 int len;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005108 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005110 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005112 *expr_start = NULL;
5113 *expr_end = NULL;
5114 }
5115
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005116 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005117 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
5118 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005119 return arg;
5120
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005121 for (p = arg; *p != NUL
5122 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005123 || (*p == '{' && !vim9script)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005124 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005126 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005127 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005128 if (*p == '\'')
5129 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005130 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005131 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005132 ;
5133 if (*p == NUL)
5134 break;
5135 }
5136 else if (*p == '"')
5137 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005138 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005139 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005140 if (*p == '\\' && p[1] != NUL)
5141 ++p;
5142 if (*p == NUL)
5143 break;
5144 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005145 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5146 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005147 // "s:" is start of "s:var", but "n:" is not and can be used in
5148 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005149 len = (int)(p - arg);
5150 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005151 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005152 break;
5153 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005154
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005155 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 if (*p == '[')
5158 ++br_nest;
5159 else if (*p == ']')
5160 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005162
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02005163 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 if (*p == '{')
5166 {
5167 mb_nest++;
5168 if (expr_start != NULL && *expr_start == NULL)
5169 *expr_start = p;
5170 }
5171 else if (*p == '}')
5172 {
5173 mb_nest--;
5174 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5175 *expr_end = p;
5176 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179
5180 return p;
5181}
5182
5183/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005184 * Expands out the 'magic' {}'s in a variable/function name.
5185 * Note that this can call itself recursively, to deal with
5186 * constructs like foo{bar}{baz}{bam}
5187 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5188 * "in_start" ^
5189 * "expr_start" ^
5190 * "expr_end" ^
5191 * "in_end" ^
5192 *
5193 * Returns a new allocated string, which the caller must free.
5194 * Returns NULL for failure.
5195 */
5196 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005197make_expanded_name(
5198 char_u *in_start,
5199 char_u *expr_start,
5200 char_u *expr_end,
5201 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005202{
5203 char_u c1;
5204 char_u *retval = NULL;
5205 char_u *temp_result;
5206 char_u *nextcmd = NULL;
5207
5208 if (expr_end == NULL || in_end == NULL)
5209 return NULL;
5210 *expr_start = NUL;
5211 *expr_end = NUL;
5212 c1 = *in_end;
5213 *in_end = NUL;
5214
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005215 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005216 if (temp_result != NULL && nextcmd == NULL)
5217 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005218 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5219 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005220 if (retval != NULL)
5221 {
5222 STRCPY(retval, in_start);
5223 STRCAT(retval, temp_result);
5224 STRCAT(retval, expr_end + 1);
5225 }
5226 }
5227 vim_free(temp_result);
5228
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005229 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005230 *expr_start = '{';
5231 *expr_end = '}';
5232
5233 if (retval != NULL)
5234 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005235 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 if (expr_start != NULL)
5237 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005238 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005239 temp_result = make_expanded_name(retval, expr_start,
5240 expr_end, temp_result);
5241 vim_free(retval);
5242 retval = temp_result;
5243 }
5244 }
5245
5246 return retval;
5247}
5248
5249/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005251 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005256 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5257}
5258
5259/*
5260 * Return TRUE if character "c" can be used as the first character in a
5261 * variable or function name (excluding '{' and '}').
5262 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005264eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005265{
5266 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267}
5268
5269/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005270 * Handle:
5271 * - expr[expr], expr[expr:expr] subscript
5272 * - ".name" lookup
5273 * - function call with Funcref variable: func(expr)
5274 * - method call: var->method()
5275 *
5276 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005279handle_subscript(
5280 char_u **arg,
5281 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005282 int evaluate, // do more than finding the end
5283 int verbose, // give error messages
5284 char_u *start_leader, // start of '!' and '-' prefixes
5285 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005286{
5287 int ret = OK;
5288 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005289
Bram Moolenaar61343f02019-07-20 21:11:13 +02005290 // "." is ".name" lookup when we found a dict or when evaluating and
5291 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005292 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005293 && (((**arg == '['
5294 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005295 || (!evaluate
5296 && (*arg)[1] != '.'
5297 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005298 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005299 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005300 && !VIM_ISWHITE(*(*arg - 1)))
5301 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005302 {
5303 if (**arg == '(')
5304 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005305 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005306
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005307 // Stop the expression evaluation when immediately aborting on
5308 // error, or when an interrupt occurred or an exception was thrown
5309 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005310 if (aborting())
5311 {
5312 if (ret == OK)
5313 clear_tv(rettv);
5314 ret = FAIL;
5315 }
5316 dict_unref(selfdict);
5317 selfdict = NULL;
5318 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005319 else if (**arg == '-')
5320 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005321 // Expression "-1.0->method()" applies the leader "-" before
5322 // applying ->.
5323 if (evaluate && *end_leaderp > start_leader)
5324 ret = eval7_leader(rettv, start_leader, end_leaderp);
5325 if (ret == OK)
5326 {
5327 if ((*arg)[2] == '{')
5328 // expr->{lambda}()
5329 ret = eval_lambda(arg, rettv, evaluate, verbose);
5330 else
5331 // expr->name()
5332 ret = eval_method(arg, rettv, evaluate, verbose);
5333 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005334 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005335 else // **arg == '[' || **arg == '.'
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005336 {
5337 dict_unref(selfdict);
5338 if (rettv->v_type == VAR_DICT)
5339 {
5340 selfdict = rettv->vval.v_dict;
5341 if (selfdict != NULL)
5342 ++selfdict->dv_refcount;
5343 }
5344 else
5345 selfdict = NULL;
5346 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5347 {
5348 clear_tv(rettv);
5349 ret = FAIL;
5350 }
5351 }
5352 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005353
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005354 // Turn "dict.Func" into a partial for "Func" bound to "dict".
5355 // Don't do this when "Func" is already a partial that was bound
5356 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02005357 if (selfdict != NULL
5358 && (rettv->v_type == VAR_FUNC
5359 || (rettv->v_type == VAR_PARTIAL
5360 && (rettv->vval.v_partial->pt_auto
5361 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005362 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005363
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005364 dict_unref(selfdict);
5365 return ret;
5366}
5367
5368/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005369 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 * value).
5371 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005372 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005373alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005375 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005376}
5377
5378/*
5379 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 * The string "s" must have been allocated, it is consumed.
5381 * Return NULL for out of memory, the variable otherwise.
5382 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005383 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005384alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385{
Bram Moolenaar33570922005-01-25 22:26:29 +00005386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005388 rettv = alloc_tv();
5389 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005391 rettv->v_type = VAR_STRING;
5392 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 }
5394 else
5395 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005396 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397}
5398
5399/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005400 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005403free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404{
5405 if (varp != NULL)
5406 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 switch (varp->v_type)
5408 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005410 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005411 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005412 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 vim_free(varp->vval.v_string);
5414 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005415 case VAR_PARTIAL:
5416 partial_unref(varp->vval.v_partial);
5417 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005418 case VAR_BLOB:
5419 blob_unref(varp->vval.v_blob);
5420 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 case VAR_LIST:
5422 list_unref(varp->vval.v_list);
5423 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005424 case VAR_DICT:
5425 dict_unref(varp->vval.v_dict);
5426 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005427 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005428#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005429 job_unref(varp->vval.v_job);
5430 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005431#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005432 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005433#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005434 channel_unref(varp->vval.v_channel);
5435 break;
5436#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005437 case VAR_NUMBER:
5438 case VAR_FLOAT:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005439 case VAR_ANY:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005440 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005441 case VAR_VOID:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005442 case VAR_BOOL:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005443 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005444 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 vim_free(varp);
5447 }
5448}
5449
5450/*
5451 * Free the memory for a variable value and set the value to NULL or 0.
5452 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005454clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456 if (varp != NULL)
5457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005458 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005460 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005461 func_unref(varp->vval.v_string);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005462 // FALLTHROUGH
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005463 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005464 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005466 case VAR_PARTIAL:
5467 partial_unref(varp->vval.v_partial);
5468 varp->vval.v_partial = NULL;
5469 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005470 case VAR_BLOB:
5471 blob_unref(varp->vval.v_blob);
5472 varp->vval.v_blob = NULL;
5473 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 case VAR_LIST:
5475 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005476 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 case VAR_DICT:
5479 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005480 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005482 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005483 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005484 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 varp->vval.v_number = 0;
5486 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005487 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005489 varp->vval.v_float = 0.0;
5490 break;
5491#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005492 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005493#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005494 job_unref(varp->vval.v_job);
5495 varp->vval.v_job = NULL;
5496#endif
5497 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005498 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005500 channel_unref(varp->vval.v_channel);
5501 varp->vval.v_channel = NULL;
5502#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005504 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005505 case VAR_VOID:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005506 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005508 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 }
5510}
5511
5512/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005513 * Set the value of a variable to NULL without freeing items.
5514 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005516init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005517{
5518 if (varp != NULL)
Bram Moolenaara80faa82020-04-12 19:37:17 +02005519 CLEAR_POINTER(varp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005520}
5521
5522/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 * Get the number value of a variable.
5524 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005525 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005526 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005527 * caller of incompatible types: it sets *denote to TRUE if "denote"
5528 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005530 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005531tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005533 int error = FALSE;
5534
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005535 return tv_get_number_chk(varp, &error); // return 0L on error
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005536}
5537
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005538 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005539tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005540{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005541 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005545 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005546 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005547 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005548#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005549 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005550 break;
5551#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005553 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005554 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 break;
5556 case VAR_STRING:
5557 if (varp->vval.v_string != NULL)
5558 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005559 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005560 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005561 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005562 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005563 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005564 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005565 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005566 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005567 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005568 case VAR_SPECIAL:
5569 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005570 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005571#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005572 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005573 break;
5574#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005575 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005576#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005577 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005578 break;
5579#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005580 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005581 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005582 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005583 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005584 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005585 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005586 internal_error_no_abort("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005589 if (denote == NULL) // useful for values that must be unsigned
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005590 n = -1;
5591 else
5592 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594}
5595
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005596#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005597 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005598tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005599{
5600 switch (varp->v_type)
5601 {
5602 case VAR_NUMBER:
5603 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005604 case VAR_FLOAT:
5605 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005606 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005607 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005608 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005609 break;
5610 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005611 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005612 break;
5613 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005614 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005615 break;
5616 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005617 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005618 break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005619 case VAR_BOOL:
5620 emsg(_("E362: Using a boolean value as a Float"));
5621 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005622 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005623 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005624 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005625 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005626# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005627 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005628 break;
5629# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005630 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005631# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005632 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005633 break;
5634# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005635 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005636 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005637 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005638 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005639 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005640 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005641 internal_error_no_abort("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005642 break;
5643 }
5644 return 0;
5645}
5646#endif
5647
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 * Get the string value of a variable.
5650 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005651 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5652 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 * If the String variable has never been set, return an empty string.
5654 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005655 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005656 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005658 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005659tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660{
5661 static char_u mybuf[NUMBUFLEN];
5662
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005663 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664}
5665
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005666 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005667tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005669 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005670
5671 return res != NULL ? res : (char_u *)"";
5672}
5673
Bram Moolenaar7d647822014-04-05 21:28:56 +02005674/*
5675 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5676 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005677 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005678tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005679{
5680 static char_u mybuf[NUMBUFLEN];
5681
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005682 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005683}
5684
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005685 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005686tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005687{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005688 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005690 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005691 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +01005692 (varnumber_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 return buf;
5694 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005695 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005696 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005697 break;
5698 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005699 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 break;
5701 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005702 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005704 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005705#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005706 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005707 break;
5708#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005709 case VAR_STRING:
5710 if (varp->vval.v_string != NULL)
5711 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005712 return (char_u *)"";
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005713 case VAR_BOOL:
Bram Moolenaar17a13432016-01-24 14:22:10 +01005714 case VAR_SPECIAL:
5715 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5716 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005717 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005718 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005719 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005720 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005721#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005722 {
5723 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005724 char *status;
5725
5726 if (job == NULL)
5727 return (char_u *)"no process";
5728 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005729 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005730 : "run";
5731# ifdef UNIX
5732 vim_snprintf((char *)buf, NUMBUFLEN,
5733 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005734# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005735 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005736 "process %ld %s",
5737 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005738 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005739# else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005740 // fall-back
Bram Moolenaar835dc632016-02-07 14:27:38 +01005741 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5742# endif
5743 return buf;
5744 }
5745#endif
5746 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005747 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005748#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005749 {
5750 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005751 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005752
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005753 if (channel == NULL)
5754 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5755 else
5756 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005757 "channel %d %s", channel->ch_id, status);
5758 return buf;
5759 }
5760#endif
5761 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005762 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005763 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005764 case VAR_VOID:
Bram Moolenaare2a8f072020-01-08 19:32:18 +01005765 emsg(_(e_inval_string));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005768 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769}
5770
5771/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005772 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5773 * string() on Dict, List, etc.
5774 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005775 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005776tv_stringify(typval_T *varp, char_u *buf)
5777{
5778 if (varp->v_type == VAR_LIST
5779 || varp->v_type == VAR_DICT
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01005780 || varp->v_type == VAR_BLOB
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005781 || varp->v_type == VAR_FUNC
5782 || varp->v_type == VAR_PARTIAL
5783 || varp->v_type == VAR_FLOAT)
5784 {
5785 typval_T tmp;
5786
5787 f_string(varp, &tmp);
5788 tv_get_string_buf(&tmp, buf);
5789 clear_tv(varp);
5790 *varp = tmp;
5791 return tmp.vval.v_string;
5792 }
5793 return tv_get_string_buf(varp, buf);
5794}
5795
5796/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01005797 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
5798 * Also give an error message, using "name" or _("name") when use_gettext is
5799 * TRUE.
5800 */
5801 static int
5802tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
5803{
5804 int lock = 0;
5805
5806 switch (tv->v_type)
5807 {
5808 case VAR_BLOB:
5809 if (tv->vval.v_blob != NULL)
5810 lock = tv->vval.v_blob->bv_lock;
5811 break;
5812 case VAR_LIST:
5813 if (tv->vval.v_list != NULL)
5814 lock = tv->vval.v_list->lv_lock;
5815 break;
5816 case VAR_DICT:
5817 if (tv->vval.v_dict != NULL)
5818 lock = tv->vval.v_dict->dv_lock;
5819 break;
5820 default:
5821 break;
5822 }
5823 return var_check_lock(tv->v_lock, name, use_gettext)
5824 || (lock != 0 && var_check_lock(lock, name, use_gettext));
5825}
5826
5827/*
Bram Moolenaar33570922005-01-25 22:26:29 +00005828 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005829 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01005830 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00005831 * It is OK for "from" and "to" to point to the same item. This is used to
5832 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005833 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01005834 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005835copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005837 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005838 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005839 switch (from->v_type)
5840 {
5841 case VAR_NUMBER:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005842 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005843 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005844 to->vval.v_number = from->vval.v_number;
5845 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005846 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005847#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005848 to->vval.v_float = from->vval.v_float;
5849 break;
5850#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005851 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005852#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005853 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01005854 if (to->vval.v_job != NULL)
5855 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005856 break;
5857#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005858 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005859#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005860 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005861 if (to->vval.v_channel != NULL)
5862 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01005863 break;
5864#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005865 case VAR_STRING:
5866 case VAR_FUNC:
5867 if (from->vval.v_string == NULL)
5868 to->vval.v_string = NULL;
5869 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005872 if (from->v_type == VAR_FUNC)
5873 func_ref(to->vval.v_string);
5874 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005876 case VAR_PARTIAL:
5877 if (from->vval.v_partial == NULL)
5878 to->vval.v_partial = NULL;
5879 else
5880 {
5881 to->vval.v_partial = from->vval.v_partial;
5882 ++to->vval.v_partial->pt_refcount;
5883 }
5884 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005885 case VAR_BLOB:
5886 if (from->vval.v_blob == NULL)
5887 to->vval.v_blob = NULL;
5888 else
5889 {
5890 to->vval.v_blob = from->vval.v_blob;
5891 ++to->vval.v_blob->bv_refcount;
5892 }
5893 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005894 case VAR_LIST:
5895 if (from->vval.v_list == NULL)
5896 to->vval.v_list = NULL;
5897 else
5898 {
5899 to->vval.v_list = from->vval.v_list;
5900 ++to->vval.v_list->lv_refcount;
5901 }
5902 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 case VAR_DICT:
5904 if (from->vval.v_dict == NULL)
5905 to->vval.v_dict = NULL;
5906 else
5907 {
5908 to->vval.v_dict = from->vval.v_dict;
5909 ++to->vval.v_dict->dv_refcount;
5910 }
5911 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005912 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005913 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005914 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005915 internal_error_no_abort("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916 break;
5917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005918}
5919
5920/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005921 * Make a copy of an item.
5922 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005923 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
5924 * reference to an already copied list/dict can be used.
5925 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005926 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005927 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005928item_copy(
5929 typval_T *from,
5930 typval_T *to,
5931 int deep,
5932 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005933{
5934 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005935 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005936
Bram Moolenaar33570922005-01-25 22:26:29 +00005937 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005939 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005940 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005941 }
5942 ++recurse;
5943
5944 switch (from->v_type)
5945 {
5946 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005947 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005948 case VAR_STRING:
5949 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005950 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005951 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01005952 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005953 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005954 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00005955 copy_tv(from, to);
5956 break;
5957 case VAR_LIST:
5958 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005959 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005960 if (from->vval.v_list == NULL)
5961 to->vval.v_list = NULL;
5962 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
5963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005964 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005965 to->vval.v_list = from->vval.v_list->lv_copylist;
5966 ++to->vval.v_list->lv_refcount;
5967 }
5968 else
5969 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
5970 if (to->vval.v_list == NULL)
5971 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005972 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005973 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005974 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01005975 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005976 case VAR_DICT:
5977 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005978 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005979 if (from->vval.v_dict == NULL)
5980 to->vval.v_dict = NULL;
5981 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
5982 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005984 to->vval.v_dict = from->vval.v_dict->dv_copydict;
5985 ++to->vval.v_dict->dv_refcount;
5986 }
5987 else
5988 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
5989 if (to->vval.v_dict == NULL)
5990 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005991 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005992 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005993 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005994 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01005995 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005996 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005997 }
5998 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00005999 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006000}
6001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006002 void
6003echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6004{
6005 char_u *tofree;
6006 char_u numbuf[NUMBUFLEN];
6007 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6008
6009 if (*atstart)
6010 {
6011 *atstart = FALSE;
6012 // Call msg_start() after eval1(), evaluating the expression
6013 // may cause a message to appear.
6014 if (with_space)
6015 {
6016 // Mark the saved text as finishing the line, so that what
6017 // follows is displayed on a new line when scrolling back
6018 // at the more prompt.
6019 msg_sb_eol();
6020 msg_start();
6021 }
6022 }
6023 else if (with_space)
6024 msg_puts_attr(" ", echo_attr);
6025
6026 if (p != NULL)
6027 for ( ; *p != NUL && !got_int; ++p)
6028 {
6029 if (*p == '\n' || *p == '\r' || *p == TAB)
6030 {
6031 if (*p != TAB && *needclr)
6032 {
6033 // remove any text still there from the command
6034 msg_clr_eos();
6035 *needclr = FALSE;
6036 }
6037 msg_putchar_attr(*p, echo_attr);
6038 }
6039 else
6040 {
6041 if (has_mbyte)
6042 {
6043 int i = (*mb_ptr2len)(p);
6044
6045 (void)msg_outtrans_len_attr(p, i, echo_attr);
6046 p += i - 1;
6047 }
6048 else
6049 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6050 }
6051 }
6052 vim_free(tofree);
6053}
6054
Bram Moolenaare9a41262005-01-15 22:18:47 +00006055/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 * ":echo expr1 ..." print each argument separated with a space, add a
6057 * newline at the end.
6058 * ":echon expr1 ..." print each argument plain.
6059 */
6060 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006061ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006062{
6063 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 char_u *p;
6066 int needclr = TRUE;
6067 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006068 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006069 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070
6071 if (eap->skip)
6072 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006073 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006075 // If eval1() causes an error message the text from the command may
6076 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077 need_clr_eos = needclr;
6078
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006080 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
6082 /*
6083 * Report the invalid expression unless the expression evaluation
6084 * has been cancelled due to an aborting error, an interrupt, or an
6085 * exception.
6086 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006087 if (!aborting() && did_emsg == did_emsg_before
6088 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006089 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006090 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 break;
6092 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006093 need_clr_eos = FALSE;
6094
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 if (!eap->skip)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006096 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006097
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006098 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 arg = skipwhite(arg);
6100 }
6101 eap->nextcmd = check_nextcmd(arg);
6102
6103 if (eap->skip)
6104 --emsg_skip;
6105 else
6106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006107 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 if (needclr)
6109 msg_clr_eos();
6110 if (eap->cmdidx == CMD_echo)
6111 msg_end();
6112 }
6113}
6114
6115/*
6116 * ":echohl {name}".
6117 */
6118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006119ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006121 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006122}
6123
6124/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006125 * Returns the :echo attribute
6126 */
6127 int
6128get_echo_attr(void)
6129{
6130 return echo_attr;
6131}
6132
6133/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 * ":execute expr1 ..." execute the result of an expression.
6135 * ":echomsg expr1 ..." Print a message
6136 * ":echoerr expr1 ..." Print an error
6137 * Each gets spaces around each argument and a newline at the end for
6138 * echo commands
6139 */
6140 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006141ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142{
6143 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 int ret = OK;
6146 char_u *p;
6147 garray_T ga;
6148 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149
6150 ga_init2(&ga, 1, 80);
6151
6152 if (eap->skip)
6153 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006154 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006156 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6157 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159
6160 if (!eap->skip)
6161 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006162 char_u buf[NUMBUFLEN];
6163
6164 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006165 {
6166 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6167 {
6168 emsg(_(e_inval_string));
6169 p = NULL;
6170 }
6171 else
6172 p = tv_get_string_buf(&rettv, buf);
6173 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006174 else
6175 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006176 if (p == NULL)
6177 {
6178 clear_tv(&rettv);
6179 ret = FAIL;
6180 break;
6181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 len = (int)STRLEN(p);
6183 if (ga_grow(&ga, len + 2) == FAIL)
6184 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006185 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 ret = FAIL;
6187 break;
6188 }
6189 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006192 ga.ga_len += len;
6193 }
6194
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006195 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 arg = skipwhite(arg);
6197 }
6198
6199 if (ret != FAIL && ga.ga_data != NULL)
6200 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006201 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6202 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006203 // Mark the already saved text as finishing the line, so that what
6204 // follows is displayed on a new line when scrolling back at the
6205 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006206 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006207 }
6208
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006210 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006211 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006212 out_flush();
6213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 else if (eap->cmdidx == CMD_echoerr)
6215 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006216 int save_did_emsg = did_emsg;
6217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006218 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006219 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006220 if (!force_abort)
6221 did_emsg = save_did_emsg;
6222 }
6223 else if (eap->cmdidx == CMD_execute)
6224 do_cmdline((char_u *)ga.ga_data,
6225 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6226 }
6227
6228 ga_clear(&ga);
6229
6230 if (eap->skip)
6231 --emsg_skip;
6232
6233 eap->nextcmd = check_nextcmd(arg);
6234}
6235
6236/*
6237 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6238 * "arg" points to the "&" or '+' when called, to "option" when returning.
6239 * Returns NULL when no option name found. Otherwise pointer to the char
6240 * after the option name.
6241 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006242 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006243find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006244{
6245 char_u *p = *arg;
6246
6247 ++p;
6248 if (*p == 'g' && p[1] == ':')
6249 {
6250 *opt_flags = OPT_GLOBAL;
6251 p += 2;
6252 }
6253 else if (*p == 'l' && p[1] == ':')
6254 {
6255 *opt_flags = OPT_LOCAL;
6256 p += 2;
6257 }
6258 else
6259 *opt_flags = 0;
6260
6261 if (!ASCII_ISALPHA(*p))
6262 return NULL;
6263 *arg = p;
6264
6265 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006266 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 else
6268 while (ASCII_ISALPHA(*p))
6269 ++p;
6270 return p;
6271}
6272
6273/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006274 * Display script name where an item was last set.
6275 * Should only be invoked when 'verbose' is non-zero.
6276 */
6277 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006278last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006279{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006280 char_u *p;
6281
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006282 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006283 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006284 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006285 if (p != NULL)
6286 {
6287 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006288 msg_puts(_("\n\tLast set from "));
6289 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006290 if (script_ctx.sc_lnum > 0)
6291 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006292 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006293 msg_outnum((long)script_ctx.sc_lnum);
6294 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006295 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006296 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006297 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006298 }
6299}
6300
Bram Moolenaar61be3762019-03-19 23:04:17 +01006301/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006302 * Compare "typ1" and "typ2". Put the result in "typ1".
6303 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006304 int
6305typval_compare(
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006306 typval_T *typ1, // first operand
6307 typval_T *typ2, // second operand
6308 exptype_T type, // operator
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006309 int ic) // ignore case
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006310{
6311 int i;
6312 varnumber_T n1, n2;
6313 char_u *s1, *s2;
6314 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar87396072019-12-31 22:36:18 +01006315 int type_is = type == EXPR_IS || type == EXPR_ISNOT;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006316
Bram Moolenaar31988702018-02-13 12:57:42 +01006317 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006318 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006319 // For "is" a different type always means FALSE, for "notis"
6320 // it means TRUE.
Bram Moolenaar87396072019-12-31 22:36:18 +01006321 n1 = (type == EXPR_ISNOT);
Bram Moolenaar31988702018-02-13 12:57:42 +01006322 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006323 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6324 {
6325 if (type_is)
6326 {
6327 n1 = (typ1->v_type == typ2->v_type
6328 && typ1->vval.v_blob == typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006329 if (type == EXPR_ISNOT)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006330 n1 = !n1;
6331 }
6332 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006333 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006334 {
6335 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006336 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006337 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006338 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006339 clear_tv(typ1);
6340 return FAIL;
6341 }
6342 else
6343 {
6344 // Compare two Blobs for being equal or unequal.
6345 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
Bram Moolenaar87396072019-12-31 22:36:18 +01006346 if (type == EXPR_NEQUAL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006347 n1 = !n1;
6348 }
6349 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006350 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6351 {
6352 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006353 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006354 n1 = (typ1->v_type == typ2->v_type
6355 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaar87396072019-12-31 22:36:18 +01006356 if (type == EXPR_ISNOT)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006357 n1 = !n1;
6358 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006359 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006360 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006361 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006362 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006363 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006364 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006365 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006366 clear_tv(typ1);
6367 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006368 }
6369 else
6370 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006371 // Compare two Lists for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006372 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6373 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006374 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006375 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006376 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006377 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006378
6379 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6380 {
6381 if (type_is)
6382 {
6383 n1 = (typ1->v_type == typ2->v_type
6384 && typ1->vval.v_dict == typ2->vval.v_dict);
Bram Moolenaar87396072019-12-31 22:36:18 +01006385 if (type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006386 n1 = !n1;
6387 }
6388 else if (typ1->v_type != typ2->v_type
Bram Moolenaar87396072019-12-31 22:36:18 +01006389 || (type != EXPR_EQUAL && type != EXPR_NEQUAL))
Bram Moolenaar31988702018-02-13 12:57:42 +01006390 {
6391 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006392 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006393 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006394 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006395 clear_tv(typ1);
6396 return FAIL;
6397 }
6398 else
6399 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006400 // Compare two Dictionaries for being equal or unequal.
Bram Moolenaar31988702018-02-13 12:57:42 +01006401 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6402 ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006403 if (type == EXPR_NEQUAL)
Bram Moolenaar31988702018-02-13 12:57:42 +01006404 n1 = !n1;
6405 }
6406 }
6407
6408 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6409 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6410 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006411 if (type != EXPR_EQUAL && type != EXPR_NEQUAL
6412 && type != EXPR_IS && type != EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006413 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006414 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006415 clear_tv(typ1);
6416 return FAIL;
6417 }
6418 if ((typ1->v_type == VAR_PARTIAL
6419 && typ1->vval.v_partial == NULL)
6420 || (typ2->v_type == VAR_PARTIAL
6421 && typ2->vval.v_partial == NULL))
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +02006422 // When both partials are NULL, then they are equal.
6423 // Otherwise they are not equal.
6424 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
Bram Moolenaar31988702018-02-13 12:57:42 +01006425 else if (type_is)
6426 {
6427 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006428 // strings are considered the same if their value is
6429 // the same
Bram Moolenaar31988702018-02-13 12:57:42 +01006430 n1 = tv_equal(typ1, typ2, ic, FALSE);
6431 else if (typ1->v_type == VAR_PARTIAL
6432 && typ2->v_type == VAR_PARTIAL)
6433 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6434 else
6435 n1 = FALSE;
6436 }
6437 else
6438 n1 = tv_equal(typ1, typ2, ic, FALSE);
Bram Moolenaar87396072019-12-31 22:36:18 +01006439 if (type == EXPR_NEQUAL || type == EXPR_ISNOT)
Bram Moolenaar31988702018-02-13 12:57:42 +01006440 n1 = !n1;
6441 }
6442
6443#ifdef FEAT_FLOAT
6444 /*
6445 * If one of the two variables is a float, compare as a float.
6446 * When using "=~" or "!~", always compare as string.
6447 */
6448 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
Bram Moolenaar87396072019-12-31 22:36:18 +01006449 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006450 {
6451 float_T f1, f2;
6452
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006453 f1 = tv_get_float(typ1);
6454 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006455 n1 = FALSE;
6456 switch (type)
6457 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006458 case EXPR_IS:
6459 case EXPR_EQUAL: n1 = (f1 == f2); break;
6460 case EXPR_ISNOT:
6461 case EXPR_NEQUAL: n1 = (f1 != f2); break;
6462 case EXPR_GREATER: n1 = (f1 > f2); break;
6463 case EXPR_GEQUAL: n1 = (f1 >= f2); break;
6464 case EXPR_SMALLER: n1 = (f1 < f2); break;
6465 case EXPR_SEQUAL: n1 = (f1 <= f2); break;
6466 case EXPR_UNKNOWN:
6467 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006468 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006469 }
6470 }
6471#endif
6472
6473 /*
Bram Moolenaarec57ec62019-12-25 19:33:22 +01006474 * If one of the two variables is a number, compare as a number.
6475 * When using "=~" or "!~", always compare as string.
6476 */
Bram Moolenaar31988702018-02-13 12:57:42 +01006477 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
Bram Moolenaar87396072019-12-31 22:36:18 +01006478 && type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006479 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006480 n1 = tv_get_number(typ1);
6481 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006482 switch (type)
6483 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006484 case EXPR_IS:
6485 case EXPR_EQUAL: n1 = (n1 == n2); break;
6486 case EXPR_ISNOT:
6487 case EXPR_NEQUAL: n1 = (n1 != n2); break;
6488 case EXPR_GREATER: n1 = (n1 > n2); break;
6489 case EXPR_GEQUAL: n1 = (n1 >= n2); break;
6490 case EXPR_SMALLER: n1 = (n1 < n2); break;
6491 case EXPR_SEQUAL: n1 = (n1 <= n2); break;
6492 case EXPR_UNKNOWN:
6493 case EXPR_MATCH:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006494 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006495 }
6496 }
6497 else
6498 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006499 s1 = tv_get_string_buf(typ1, buf1);
6500 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar87396072019-12-31 22:36:18 +01006501 if (type != EXPR_MATCH && type != EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006502 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6503 else
6504 i = 0;
6505 n1 = FALSE;
6506 switch (type)
6507 {
Bram Moolenaar87396072019-12-31 22:36:18 +01006508 case EXPR_IS:
6509 case EXPR_EQUAL: n1 = (i == 0); break;
6510 case EXPR_ISNOT:
6511 case EXPR_NEQUAL: n1 = (i != 0); break;
6512 case EXPR_GREATER: n1 = (i > 0); break;
6513 case EXPR_GEQUAL: n1 = (i >= 0); break;
6514 case EXPR_SMALLER: n1 = (i < 0); break;
6515 case EXPR_SEQUAL: n1 = (i <= 0); break;
Bram Moolenaar31988702018-02-13 12:57:42 +01006516
Bram Moolenaar87396072019-12-31 22:36:18 +01006517 case EXPR_MATCH:
6518 case EXPR_NOMATCH:
Bram Moolenaar31988702018-02-13 12:57:42 +01006519 n1 = pattern_match(s2, s1, ic);
Bram Moolenaar87396072019-12-31 22:36:18 +01006520 if (type == EXPR_NOMATCH)
Bram Moolenaar31988702018-02-13 12:57:42 +01006521 n1 = !n1;
6522 break;
6523
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006524 default: break; // avoid gcc warning
Bram Moolenaar31988702018-02-13 12:57:42 +01006525 }
6526 }
6527 clear_tv(typ1);
6528 typ1->v_type = VAR_NUMBER;
6529 typ1->vval.v_number = n1;
6530
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006531 return OK;
6532}
6533
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006534 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006535typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006536{
6537 char_u *tofree;
6538 char_u numbuf[NUMBUFLEN];
6539 char_u *ret = NULL;
6540
6541 if (arg == NULL)
6542 return vim_strsave((char_u *)"(does not exist)");
6543 ret = tv2string(arg, &tofree, numbuf, 0);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006544 // Make a copy if we have a value but it's not in allocated memory.
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006545 if (ret != NULL && tofree == NULL)
6546 ret = vim_strsave(ret);
6547 return ret;
6548}
6549
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006550#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551
6552/*
6553 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006554 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 * "flags" can be "g" to do a global substitute.
6556 * Returns an allocated string, NULL for error.
6557 */
6558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559do_string_sub(
6560 char_u *str,
6561 char_u *pat,
6562 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006563 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006564 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565{
6566 int sublen;
6567 regmatch_T regmatch;
6568 int i;
6569 int do_all;
6570 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006571 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 garray_T ga;
6573 char_u *ret;
6574 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006575 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006577 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006579 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580
6581 ga_init2(&ga, 1, 200);
6582
6583 do_all = (flags[0] == 'g');
6584
6585 regmatch.rm_ic = p_ic;
6586 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6587 if (regmatch.regprog != NULL)
6588 {
6589 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006590 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6592 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006593 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006594 if (regmatch.startp[0] == regmatch.endp[0])
6595 {
6596 if (zero_width == regmatch.startp[0])
6597 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006598 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006599 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006600 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6601 (size_t)i);
6602 ga.ga_len += i;
6603 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006604 continue;
6605 }
6606 zero_width = regmatch.startp[0];
6607 }
6608
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 /*
6610 * Get some space for a temporary buffer to do the substitution
6611 * into. It will contain:
6612 * - The text up to where the match is.
6613 * - The substituted text.
6614 * - The text after the match.
6615 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006616 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01006617 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
6619 {
6620 ga_clear(&ga);
6621 break;
6622 }
6623
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006624 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625 i = (int)(regmatch.startp[0] - tail);
6626 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006627 // add the substituted text
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006628 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 + ga.ga_len + i, TRUE, TRUE, FALSE);
6630 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02006631 tail = regmatch.endp[0];
6632 if (*tail == NUL)
6633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006634 if (!do_all)
6635 break;
6636 }
6637
6638 if (ga.ga_data != NULL)
6639 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
6640
Bram Moolenaar473de612013-06-08 18:19:48 +02006641 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 }
6643
6644 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
6645 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006646 if (p_cpo == empty_option)
6647 p_cpo = save_cpo;
6648 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006649 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006650 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006651
6652 return ret;
6653}