blob: 94bb4e3770a8e2c2573aed3655c0568078b2536d [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 Moolenaar9bbf63d2016-01-16 16:49:28 +010023#define NAMESPACE_CHAR (char_u *)"abglstvw"
24
Bram Moolenaar532c7802005-01-27 14:44:31 +000025/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000026 * When recursively copying lists and dicts we need to remember which ones we
27 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000028 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000029 */
30static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020031
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020032static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
33static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
34static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
35static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010036static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +020037static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +010038static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
39static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
40static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000041
Bram Moolenaar48e697e2016-01-23 22:17:30 +010042static int free_unref_items(int copyID);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020043static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020044
Bram Moolenaare21c1582019-03-02 11:57:09 +010045/*
46 * Return "n1" divided by "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010047 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010048 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020049 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010050num_divide(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010051{
52 varnumber_T result;
53
Bram Moolenaar99880f92021-01-20 21:23:14 +010054 if (n2 == 0)
Bram Moolenaare21c1582019-03-02 11:57:09 +010055 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010056 if (in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010057 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010058 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010059 if (failed != NULL)
60 *failed = TRUE;
61 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010062 if (n1 == 0)
63 result = VARNUM_MIN; // similar to NaN
64 else if (n1 < 0)
65 result = -VARNUM_MAX;
66 else
67 result = VARNUM_MAX;
68 }
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +010069 else if (n1 == VARNUM_MIN && n2 == -1)
70 {
71 // specific case: trying to do VARNUM_MIN / -1 results in a positive
72 // number that doesn't fit in varnumber_T and causes an FPE
73 result = VARNUM_MAX;
74 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010075 else
76 result = n1 / n2;
77
78 return result;
79}
80
81/*
82 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010084 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020085 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010086num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010087{
Bram Moolenaar99880f92021-01-20 21:23:14 +010088 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010089 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010090 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010091 if (failed != NULL)
92 *failed = TRUE;
93 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010094 return (n2 == 0) ? 0 : (n1 % n2);
95}
96
Bram Moolenaar33570922005-01-25 22:26:29 +000097/*
98 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000102{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200103 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200104 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +0000105}
106
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000107#if defined(EXITFREE) || defined(PROTO)
108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100109eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000110{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200111 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100112 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200113 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000114
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200115 // autoloaded script names
116 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000117
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200118 // unreferenced lists and dicts
119 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200120
121 // functions not garbage collected
122 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000123}
124#endif
125
Bram Moolenaare6b53242020-07-01 17:28:33 +0200126 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200127fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
128{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100129 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200130 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200131 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200132 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200133 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100134 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200135 {
136 evalarg->eval_getline = eap->getline;
137 evalarg->eval_cookie = eap->cookie;
138 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200139 }
140}
141
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142/*
143 * Top level evaluation function, returning a boolean.
144 * Sets "error" to TRUE if there was an error.
145 * Return TRUE or FALSE.
146 */
147 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100148eval_to_bool(
149 char_u *arg,
150 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200151 exarg_T *eap,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100152 int skip, // only parse, don't execute
153 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154{
Bram Moolenaar33570922005-01-25 22:26:29 +0000155 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200156 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200157 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100158 int r;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200159
Bram Moolenaar37c83712020-06-30 21:18:36 +0200160 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161
162 if (skip)
163 ++emsg_skip;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100164 if (use_simple_function)
165 r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
166 else
167 r = eval0(arg, &tv, eap, &evalarg);
168 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 else
171 {
172 *error = FALSE;
173 if (!skip)
174 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200175 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200176 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200177 else
178 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000179 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 }
181 }
182 if (skip)
183 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200184 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200186 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187}
188
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100189/*
190 * Call eval1() and give an error message if not done at a lower level.
191 */
192 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200193eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100194{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100195 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100196 int ret;
197 int did_emsg_before = did_emsg;
198 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200199 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100200
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200201 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
202
203 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100204 if (ret == FAIL)
205 {
206 // Report the invalid expression unless the expression evaluation has
207 // been cancelled due to an aborting error, an interrupt, or an
208 // exception, or we already gave a more specific error.
209 // Also check called_emsg for when using assert_fails().
210 if (!aborting() && did_emsg == did_emsg_before
211 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200212 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100213 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200214 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100215 return ret;
216}
217
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200219 * Return whether a typval is a valid expression to pass to eval_expr_typval()
220 * or eval_expr_to_bool(). An empty string returns FALSE;
221 */
222 int
223eval_expr_valid_arg(typval_T *tv)
224{
225 return tv->v_type != VAR_UNKNOWN
226 && (tv->v_type != VAR_STRING
227 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
228}
229
230/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100231 * When calling eval_expr_typval() many times we only need one funccall_T.
232 * Returns NULL when no funccall_T is to be used.
233 * When returning non-NULL remove_funccal() must be called later.
234 */
235 funccall_T *
236eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
237{
238 if (expr->v_type != VAR_PARTIAL)
239 return NULL;
240
241 partial_T *partial = expr->vval.v_partial;
242 if (partial == NULL)
243 return NULL;
244 if (partial->pt_func == NULL
245 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
246 return NULL;
247
248 return create_funccal(partial->pt_func, rettv);
249}
250
251/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252 * Evaluate an expression, which can be a function, partial or string.
253 * Pass arguments "argv[argc]".
Bram Moolenaar82418262022-09-28 16:16:15 +0100254 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 * Return the result in "rettv" and OK or FAIL.
256 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200257 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100258eval_expr_typval(
259 typval_T *expr,
260 typval_T *argv,
261 int argc,
262 funccall_T *fc_arg,
263 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100264{
265 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100266 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200267 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100268
269 if (expr->v_type == VAR_FUNC)
270 {
271 s = expr->vval.v_string;
272 if (s == NULL || *s == NUL)
273 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200274 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000275 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200276 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100277 return FAIL;
278 }
279 else if (expr->v_type == VAR_PARTIAL)
280 {
281 partial_T *partial = expr->vval.v_partial;
282
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200283 if (partial == NULL)
284 return FAIL;
285
Bram Moolenaar822ba242020-05-24 23:00:18 +0200286 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200287 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100289 funccall_T *fc = fc_arg != NULL ? fc_arg
290 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100291 int r;
292
293 if (fc == NULL)
294 return FAIL;
295
Bram Moolenaar69082912022-09-22 21:35:19 +0100296 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100297 r = call_def_function(partial->pt_func, argc, argv,
Bram Moolenaarffdaca92022-12-09 21:41:48 +0000298 DEF_USE_PT_ARGV, partial, NULL, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100299 if (fc_arg == NULL)
300 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100301 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100302 return FAIL;
303 }
304 else
305 {
306 s = partial_name(partial);
307 if (s == NULL || *s == NUL)
308 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200309 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000310 funcexe.fe_evaluate = TRUE;
311 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100312 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
313 return FAIL;
314 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100315 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200316 else if (expr->v_type == VAR_INSTR)
317 {
318 return exe_typval_instr(expr, rettv);
319 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 else
321 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000322 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100323 if (s == NULL)
324 return FAIL;
325 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200326 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100327 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200328 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100329 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200330 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200331 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100332 return FAIL;
333 }
334 }
335 return OK;
336}
337
338/*
339 * Like eval_to_bool() but using a typval_T instead of a string.
340 * Works for string, funcref and partial.
341 */
342 int
343eval_expr_to_bool(typval_T *expr, int *error)
344{
345 typval_T rettv;
346 int res;
347
Bram Moolenaar82418262022-09-28 16:16:15 +0100348 if (eval_expr_typval(expr, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100349 {
350 *error = TRUE;
351 return FALSE;
352 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200353 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100354 clear_tv(&rettv);
355 return res;
356}
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358/*
359 * Top level evaluation function, returning a string. If "skip" is TRUE,
360 * only parsing to "nextcmd" is done, without reporting errors. Return
361 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
362 */
363 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100364eval_to_string_skip(
365 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200366 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100367 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368{
Bram Moolenaar33570922005-01-25 22:26:29 +0000369 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200371 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
Bram Moolenaar37c83712020-06-30 21:18:36 +0200373 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374 if (skip)
375 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200376 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000377 retval = NULL;
378 else
379 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100380 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 }
383 if (skip)
384 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200385 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386
387 return retval;
388}
389
390/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100391 * Initialize "evalarg" for use.
392 */
393 void
394init_evalarg(evalarg_T *evalarg)
395{
396 CLEAR_POINTER(evalarg);
397 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
398}
399
400/*
401 * If "evalarg->eval_tofree" is not NULL free it later.
402 * Caller is expected to overwrite "evalarg->eval_tofree" next.
403 */
404 static void
405free_eval_tofree_later(evalarg_T *evalarg)
406{
407 if (evalarg->eval_tofree != NULL)
408 {
409 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
410 ((char_u **)evalarg->eval_tofree_ga.ga_data)
411 [evalarg->eval_tofree_ga.ga_len++]
412 = evalarg->eval_tofree;
413 else
414 vim_free(evalarg->eval_tofree);
415 }
416}
417
418/*
419 * After using "evalarg" filled from "eap": free the memory.
420 */
421 void
422clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
423{
424 if (evalarg != NULL)
425 {
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100426 garray_T *etga = &evalarg->eval_tofree_ga;
427
428 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100429 {
430 if (eap != NULL)
431 {
432 // We may need to keep the original command line, e.g. for
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100433 // ":let" it has the variable names. But we may also need
434 // the new one, "nextcmd" points into it. Keep both.
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100435 vim_free(eap->cmdline_tofree);
436 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100437
438 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
439 {
440 // "nextcmd" points into the last line in eval_tofree_ga,
441 // need to keep it around.
442 --etga->ga_len;
443 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
Bram Moolenaar86fb3f82022-09-23 13:27:57 +0100444 vim_free(evalarg->eval_tofree);
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100445 }
446 else
447 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100448 }
449 else
450 vim_free(evalarg->eval_tofree);
451 evalarg->eval_tofree = NULL;
452 }
453
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100454 ga_clear_strings(etga);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100455 VIM_CLEAR(evalarg->eval_tofree_lambda);
456 }
457}
458
459/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000460 * Skip over an expression at "*pp".
461 * Return FAIL for an error, OK otherwise.
462 */
463 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200464skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000465{
Bram Moolenaar33570922005-01-25 22:26:29 +0000466 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000467
468 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200469 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000470}
471
472/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200473 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200474 * If in Vim9 script and line breaks are encountered, the lines are
475 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200476 * "arg" is advanced to just after the expression.
477 * "start" is set to the start of the expression, "end" to just after the end.
478 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200479 * Return FAIL for an error, OK otherwise.
480 */
481 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200482skip_expr_concatenate(
483 char_u **arg,
484 char_u **start,
485 char_u **end,
486 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200487{
488 typval_T rettv;
489 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200490 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200491 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200492 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200493 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200494 int evaluate = evalarg == NULL
495 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200496
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200497 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200498 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200499 {
500 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200501 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200502 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200503 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200504 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200505 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200506 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507
508 // Don't evaluate the expression.
509 if (evalarg != NULL)
510 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200511 *arg = skipwhite(*arg);
512 res = eval1(arg, &rettv, evalarg);
513 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200514 if (evalarg != NULL)
515 evalarg->eval_flags = save_flags;
516
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200517 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200518 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200519 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200520 if (evalarg->eval_ga.ga_len == 1)
521 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200522 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200523 ga_clear(gap);
524 gap->ga_itemsize = 0;
525 }
526 else
527 {
528 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200529 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200530
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200531 // Line breaks encountered, concatenate all the lines.
532 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200533 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200534
535 // free the lines only when using getsourceline()
536 if (evalarg->eval_cookie != NULL)
537 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200538 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200539 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200540 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100541 // later. Also free "eval_tofree" later if needed.
542 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200543 evalarg->eval_tofree =
544 ((char_u **)gap->ga_data)[gap->ga_len - 1];
545 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200546 ga_clear_strings(gap);
547 }
548 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200549 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200550 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200551
552 // free lines that were explicitly marked for freeing
553 ga_clear_strings(freegap);
554 }
555
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200556 gap->ga_itemsize = 0;
557 if (p == NULL)
558 return FAIL;
559 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200560 vim_free(evalarg->eval_tofree_lambda);
561 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200562 // Compute "end" relative to the end.
563 *end = *start + STRLEN(*start) - endoff;
564 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200565 }
566
567 return res;
568}
569
570/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100571 * Convert "tv" to a string.
572 * When "convert" is TRUE convert a List into a sequence of lines and convert
573 * a Float to a String.
574 * Returns an allocated string (NULL when out of memory).
575 */
576 char_u *
577typval2string(typval_T *tv, int convert)
578{
579 garray_T ga;
580 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100581 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100582
583 if (convert && tv->v_type == VAR_LIST)
584 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000585 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100586 if (tv->vval.v_list != NULL)
587 {
588 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
589 if (tv->vval.v_list->lv_len > 0)
590 ga_append(&ga, NL);
591 }
592 ga_append(&ga, NUL);
593 retval = (char_u *)ga.ga_data;
594 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100595 else if (convert && tv->v_type == VAR_FLOAT)
596 {
597 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
598 retval = vim_strsave(numbuf);
599 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100600 else
601 retval = vim_strsave(tv_get_string(tv));
602 return retval;
603}
604
605/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200606 * Top level evaluation function, returning a string. Does not handle line
607 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000608 * When "convert" is TRUE convert a List into a sequence of lines and convert
609 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610 * Return pointer to allocated memory, or NULL for failure.
611 */
612 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100613eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100614 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100615 int convert,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100616 exarg_T *eap,
617 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618{
Bram Moolenaar33570922005-01-25 22:26:29 +0000619 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100621 evalarg_T evalarg;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100622 int r;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100624 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100625 if (use_simple_function)
626 r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
627 else
628 r = eval0(arg, &tv, NULL, &evalarg);
629 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 retval = NULL;
631 else
632 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100633 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000634 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100636 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637
638 return retval;
639}
640
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100641 char_u *
642eval_to_string(
643 char_u *arg,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100644 int convert,
645 int use_simple_function)
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100646{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100647 return eval_to_string_eap(arg, convert, NULL, use_simple_function);
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100648}
649
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000651 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100652 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200653 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 */
655 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100656eval_to_string_safe(
657 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000658 int use_sandbox,
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100659 int keep_script_version,
660 int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661{
662 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200663 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200664 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200665 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
Bram Moolenaar9530b582022-01-22 13:39:08 +0000667 if (!keep_script_version)
668 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200669 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000670 if (use_sandbox)
671 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100672 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200673 may_garbage_collect = FALSE;
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100674 retval = eval_to_string(arg, FALSE, use_simple_function);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000675 if (use_sandbox)
676 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100677 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200678 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200679 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200680 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 return retval;
682}
683
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684/*
685 * Top level evaluation function, returning a number.
686 * Evaluates "expr" silently.
687 * Returns -1 for an error.
688 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200689 varnumber_T
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100690eval_to_number(char_u *expr, int use_simple_function)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691{
Bram Moolenaar33570922005-01-25 22:26:29 +0000692 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200693 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000694 char_u *p = skipwhite(expr);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100695 int r = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
697 ++emsg_off;
698
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100699 if (use_simple_function)
700 r = may_call_simple_func(expr, &rettv);
701 if (r == NOTDONE)
702 r = eval1(&p, &rettv, &EVALARG_EVALUATE);
703 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 retval = -1;
705 else
706 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100707 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000708 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 }
710 --emsg_off;
711
712 return retval;
713}
714
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000715/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000716 * Top level evaluation function.
717 * Returns an allocated typval_T with the result.
718 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000719 */
720 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200721eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000722{
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100723 return eval_expr_ext(arg, eap, FALSE);
724}
725
726 typval_T *
727eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
728{
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000729 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200730 evalarg_T evalarg;
731
732 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000733
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200734 tv = ALLOC_ONE(typval_T);
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100735 if (tv != NULL)
736 {
737 int r = NOTDONE;
738
739 if (use_simple_function)
740 r = eval0_simple_funccal(arg, tv, eap, &evalarg);
741 if (r == NOTDONE)
742 r = eval0(arg, tv, eap, &evalarg);
743
744 if (r == FAIL)
745 VIM_CLEAR(tv);
746 }
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000747
Bram Moolenaar37c83712020-06-30 21:18:36 +0200748 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000749 return tv;
750}
751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000753 * "*arg" points to what can be a function name in the form of "import.Name" or
754 * "Funcref". Return the name of the function. Set "tofree" to something that
755 * was allocated.
756 * If "verbose" is FALSE no errors are given.
757 * Return NULL for any failure.
758 */
759 static char_u *
760deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000761 char_u **arg,
762 char_u **tofree,
763 evalarg_T *evalarg,
764 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000765{
766 typval_T ref;
767 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100768 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000769
770 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100771 if (evalarg != NULL)
772 {
773 // need to evaluate this to get an import, like in "a.Func"
774 save_flags = evalarg->eval_flags;
775 evalarg->eval_flags |= EVAL_EVALUATE;
776 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100777 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100778 {
779 dictitem_T *v;
780
781 // If <SID>VarName was used it would not be found, try another way.
782 v = find_var_also_in_script(name, NULL, FALSE);
783 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100784 {
785 name = NULL;
786 goto theend;
787 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100788 copy_tv(&v->di_tv, &ref);
789 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000790 if (*skipwhite(*arg) != NUL)
791 {
792 if (verbose)
793 semsg(_(e_trailing_characters_str), *arg);
794 name = NULL;
795 }
796 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
797 {
798 name = ref.vval.v_string;
799 ref.vval.v_string = NULL;
800 *tofree = name;
801 }
802 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
803 {
804 if (ref.vval.v_partial->pt_argc > 0
805 || ref.vval.v_partial->pt_dict != NULL)
806 {
807 if (verbose)
808 emsg(_(e_cannot_use_partial_here));
809 name = NULL;
810 }
811 else
812 {
813 name = vim_strsave(partial_name(ref.vval.v_partial));
814 *tofree = name;
815 }
816 }
817 else
818 {
819 if (verbose)
820 semsg(_(e_not_callable_type_str), name);
821 name = NULL;
822 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100823
824theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000825 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100826 if (evalarg != NULL)
827 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000828 return name;
829}
830
831/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100832 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200833 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
834 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000835 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100838call_vim_function(
839 char_u *func,
840 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200841 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200842 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000844 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200845 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000846 char_u *arg;
847 char_u *name;
848 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000849 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100851 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200852 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000853 funcexe.fe_firstline = curwin->w_cursor.lnum;
854 funcexe.fe_lastline = curwin->w_cursor.lnum;
855 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000856
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000857 // The name might be "import.Func" or "Funcref". We don't know, we need to
858 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000859 // autoload script has errors. Guess that when there is a dot in the name
860 // showing errors is the right choice.
861 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000862 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000863 if (ignore_errors)
864 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000865 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000866 if (ignore_errors)
867 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000868 if (name == NULL)
869 name = func;
870
871 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
872
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873 if (ret == FAIL)
874 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000875 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000876
877 return ret;
878}
879
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100880/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100881 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000882 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
883 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000884 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000885 */
886 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100887call_func_retstr(
888 char_u *func,
889 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200890 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000891{
892 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000893 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000894
Bram Moolenaarded27a12018-08-01 19:06:03 +0200895 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000896 return NULL;
897
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100898 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000899 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 return retval;
901}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000902
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000903/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100904 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000905 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000906 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100907 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000908 */
909 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100910call_func_retlist(
911 char_u *func,
912 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200913 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000914{
915 typval_T rettv;
916
Bram Moolenaarded27a12018-08-01 19:06:03 +0200917 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000918 return NULL;
919
920 if (rettv.v_type != VAR_LIST)
921 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100922 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
923 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000924 clear_tv(&rettv);
925 return NULL;
926 }
927
928 return rettv.vval.v_list;
929}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930
Bram Moolenaare70dd112022-01-21 16:31:11 +0000931#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100933 * Evaluate "arg", which is 'foldexpr'.
934 * Note: caller must set "curwin" to match "arg".
935 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
936 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 */
938 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000939eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000941 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200943 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000945 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000946 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100947 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100949 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000950 current_sctx = wp->w_p_script_ctx[WV_FDE];
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000953 if (use_sandbox)
954 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100955 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100957
Bram Moolenaara4e0b972022-10-01 19:43:52 +0100958 // Evaluate the expression. If the expression is "FuncName()" call the
959 // function directly.
960 if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 retval = 0;
962 else
963 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100964 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 if (tv.v_type == VAR_NUMBER)
966 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000967 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 retval = 0;
969 else
970 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100971 // If the result is a string, check if there is a non-digit before
972 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000973 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 if (!VIM_ISDIGIT(*s) && *s != '-')
975 *cp = *s++;
976 retval = atol((char *)s);
977 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000978 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 }
980 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000981 if (use_sandbox)
982 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100983 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200984 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000985 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200987 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988}
989#endif
990
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 * Get an lval: variable, Dict item or List item that can be assigned a value
993 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
994 * "name.key", "name.key[expr]" etc.
995 * Indexing only works if "name" is an existing List or Dictionary.
996 * "name" points to the start of the name.
997 * If "rettv" is not NULL it points to the value to be assigned.
998 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
999 * wrong; must end in space or cmd separator.
1000 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001001 * flags:
1002 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001003 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001004 * GLV_NO_AUTOLOAD: do not use script autoloading
1005 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001006 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001007 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001008 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001010 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001011get_lval(
1012 char_u *name,
1013 typval_T *rettv,
1014 lval_T *lp,
1015 int unlet,
1016 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001017 int flags, // GLV_ values
1018 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001020 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001021 char_u *expr_start, *expr_end;
1022 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001023 dictitem_T *v;
1024 typval_T var1;
1025 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001026 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001027 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001028 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001029 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001030 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001031 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001032 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001034 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001035 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001036
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001037 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001038 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001039 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001041 lp->ll_name_end = find_name_end(name, NULL, NULL,
1042 FNE_INCL_BR | fne_flags);
1043 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 }
1045
Bram Moolenaara749a422022-02-12 19:52:25 +00001046 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001047 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001048 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1049 {
1050 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1051 return NULL;
1052 }
1053
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001054 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001055 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 if (expr_start != NULL)
1058 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001059 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001060 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001061 && *p != '[' && *p != '.')
1062 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001063 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001064 return NULL;
1065 }
1066
1067 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1068 if (lp->ll_exp_name == NULL)
1069 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001070 // Report an invalid expression in braces, unless the
1071 // expression evaluation has been cancelled due to an
1072 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 if (!aborting() && !quiet)
1074 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001075 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001076 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 return NULL;
1078 }
1079 }
1080 lp->ll_name = lp->ll_exp_name;
1081 }
1082 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 lp->ll_name = name;
1085
Bram Moolenaar4525a572022-02-13 11:57:33 +00001086 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001088 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001089 // However, "g:[key]" is indexing a dictionary.
1090 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001091 {
1092 --p;
1093 lp->ll_name_end = p;
1094 }
1095 if (*p == ':')
1096 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001097 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001098
Bram Moolenaar9510d222022-09-11 15:14:05 +01001099 if (is_scoped_variable(name))
1100 {
1101 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1102 return NULL;
1103 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001104 if (tp == p + 1 && !quiet)
1105 {
1106 semsg(_(e_white_space_required_after_str_str), ":", p);
1107 return NULL;
1108 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001109 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001110 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001111 semsg(_(e_using_type_not_in_script_context_str), p);
1112 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001113 }
1114
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001115 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001116 lp->ll_type = parse_type(&tp,
1117 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1118 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001119 if (lp->ll_type == NULL && !quiet)
1120 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001121 lp->ll_name_end = tp;
1122 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 }
1124 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001125 if (lp->ll_name == NULL)
1126 return p;
1127
Bram Moolenaar62aec932022-01-29 21:45:34 +00001128 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001129 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001130 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001131
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001132 if (import != NULL)
1133 {
1134 ufunc_T *ufunc;
1135 type_T *type;
1136
Bram Moolenaar753885b2022-08-24 16:30:36 +01001137 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001138 lp->ll_sid = import->imp_sid;
1139 lp->ll_name = skipwhite(p + 1);
1140 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1141 lp->ll_name_end = p;
1142
1143 // check the item is exported
1144 cc = *p;
1145 *p = NUL;
1146 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001147 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001148 {
1149 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001150 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001151 }
1152 *p = cc;
1153 }
1154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001156 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001157 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001158 return p;
1159
Bram Moolenaar4525a572022-02-13 11:57:33 +00001160 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001161 {
1162 // using local variable
1163 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001164 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001165 }
1166 else
1167 {
1168 cc = *p;
1169 *p = NUL;
1170 // When we would write to the variable pass &ht and prevent autoload.
1171 writing = !(flags & GLV_READ_ONLY);
1172 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001173 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001174 if (v == NULL && !quiet)
1175 semsg(_(e_undefined_variable_str), lp->ll_name);
1176 *p = cc;
1177 if (v == NULL)
1178 return NULL;
1179 lp->ll_tv = &v->di_tv;
1180 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001181
Bram Moolenaar4525a572022-02-13 11:57:33 +00001182 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001183 {
1184 if (!quiet)
1185 semsg(_(e_variable_already_declared), lp->ll_name);
1186 return NULL;
1187 }
1188
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001189 /*
1190 * Loop until no more [idx] or .key is following.
1191 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001192 var1.v_type = VAR_UNKNOWN;
1193 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001194 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001195 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001196 vartype_T v_type = lp->ll_tv->v_type;
1197
1198 if (*p == '.' && v_type != VAR_DICT
1199 && v_type != VAR_OBJECT
1200 && v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001201 {
1202 if (!quiet)
1203 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1204 return NULL;
1205 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001206 if (v_type != VAR_LIST
1207 && v_type != VAR_DICT
1208 && v_type != VAR_BLOB
1209 && v_type != VAR_OBJECT
1210 && v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001213 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001214 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001215 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001216
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001217 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001218 int r = OK;
Bram Moolenaard505d172022-12-18 21:42:55 +00001219 if (v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001220 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaard505d172022-12-18 21:42:55 +00001221 else if (v_type == VAR_BLOB
Bram Moolenaare65081d2021-06-27 15:04:05 +02001222 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001223 r = rettv_blob_alloc(lp->ll_tv);
1224 if (r == FAIL)
1225 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001226
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001230 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001231 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001232 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001233
Bram Moolenaar4525a572022-02-13 11:57:33 +00001234 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001235 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001236 && lp->ll_tv == &v->di_tv
1237 && ht != NULL && ht == get_script_local_ht())
1238 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001239 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001240
1241 // Vim9 script local variable: get the type
1242 if (sv != NULL)
1243 lp->ll_valtype = sv->sv_type;
1244 }
1245
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 len = -1;
1247 if (*p == '.')
1248 {
1249 key = p + 1;
1250 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1251 ;
1252 if (len == 0)
1253 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001255 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001256 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001257 }
1258 p = key + len;
1259 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001260 else
1261 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001262 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001263 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001264 if (*p == ':')
1265 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001266 else
1267 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001268 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001269 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001270 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001271 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001273 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001274 clear_tv(&var1);
1275 return NULL;
1276 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001277 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001278 }
1279
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001280 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001281 if (*p == ':')
1282 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001283 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001284 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001285 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001286 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001287 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001288 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001289 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001290 if (rettv != NULL
1291 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001292 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001293 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001294 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001295 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001296 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001297 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001298 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001300 }
1301 p = skipwhite(p + 1);
1302 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001303 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001304 else
1305 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001306 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001307 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001308 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001309 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001310 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001311 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001312 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001313 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001314 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001315 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001316 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001317 clear_tv(&var2);
1318 return NULL;
1319 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001320 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001321 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001322 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001323 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001324 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001325
Bram Moolenaar8c711452005-01-14 21:53:12 +00001326 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001327 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001328 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001329 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001330 clear_tv(&var1);
1331 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001332 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001333 }
1334
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001335 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001336 ++p;
1337 }
1338
Bram Moolenaard505d172022-12-18 21:42:55 +00001339 if (v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001340 {
1341 if (len == -1)
1342 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001343 // "[key]": get key from "var1"
1344 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001345 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001347 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001348 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001349 }
1350 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001351 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001352
1353 // a NULL dict is equivalent with an empty dict
1354 if (lp->ll_tv->vval.v_dict == NULL)
1355 {
1356 lp->ll_tv->vval.v_dict = dict_alloc();
1357 if (lp->ll_tv->vval.v_dict == NULL)
1358 {
1359 clear_tv(&var1);
1360 return NULL;
1361 }
1362 ++lp->ll_tv->vval.v_dict->dv_refcount;
1363 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001364 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001365
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001366 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001367
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001368 // When assigning to a scope dictionary check that a function and
1369 // variable name is valid (only variable name unless it is l: or
1370 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001371 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001372 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001373 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001374
1375 if (len != -1)
1376 {
1377 prevval = key[len];
1378 key[len] = NUL;
1379 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001380 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001381 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001382 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001383 && (rettv->v_type == VAR_FUNC
1384 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001385 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001386 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001387 if (len != -1)
1388 key[len] = prevval;
1389 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001390 {
1391 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001392 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001393 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001394 }
1395
Bram Moolenaar10c65862020-10-08 21:16:42 +02001396 if (lp->ll_valtype != NULL)
1397 // use the type of the member
1398 lp->ll_valtype = lp->ll_valtype->tt_member;
1399
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001400 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001401 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001402 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001403 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001404 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001405 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001406 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001407 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001408 return NULL;
1409 }
1410
Bram Moolenaar31b81602019-02-10 22:14:27 +01001411 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001413 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001414 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001415 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001416 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001417 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001418 }
1419 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001421 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001423 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001424 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001425 p = NULL;
1426 break;
1427 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001428 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001429 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001430 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1431 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001432 {
1433 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001434 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001435 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001436
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001437 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001438 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001439 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001440 else if (v_type == VAR_BLOB)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001441 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001442 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1443
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001444 /*
1445 * Get the number and item for the only or first index of the List.
1446 */
1447 if (empty1)
1448 lp->ll_n1 = 0;
1449 else
1450 // is number or string
1451 lp->ll_n1 = (long)tv_get_number(&var1);
1452 clear_tv(&var1);
1453
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001454 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001455 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001456 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001457 return NULL;
1458 }
1459 if (lp->ll_range && !lp->ll_empty2)
1460 {
1461 lp->ll_n2 = (long)tv_get_number(&var2);
1462 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001463 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1464 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001465 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001466 }
1467 lp->ll_blob = lp->ll_tv->vval.v_blob;
1468 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001469 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001470 }
Bram Moolenaard505d172022-12-18 21:42:55 +00001471 else if (v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001472 {
1473 /*
1474 * Get the number and item for the only or first index of the List.
1475 */
1476 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001478 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001479 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001480 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001481 clear_tv(&var1);
1482
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001483 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001484 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001485 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1486 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001487 if (lp->ll_li == NULL)
1488 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001489 clear_tv(&var2);
1490 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001491 }
1492
Bram Moolenaar10c65862020-10-08 21:16:42 +02001493 if (lp->ll_valtype != NULL)
1494 // use the type of the member
1495 lp->ll_valtype = lp->ll_valtype->tt_member;
1496
Bram Moolenaar8c711452005-01-14 21:53:12 +00001497 /*
1498 * May need to find the item or absolute index for the second
1499 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 * When no index given: "lp->ll_empty2" is TRUE.
1501 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001502 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001503 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001504 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001505 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001506 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001507 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001508 if (check_range_index_two(lp->ll_list,
1509 &lp->ll_n1, lp->ll_li,
1510 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001511 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001512 }
1513
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001515 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001516 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001517 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001518 class_T *cl = (v_type == VAR_OBJECT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001519 && lp->ll_tv->vval.v_object != NULL)
1520 ? lp->ll_tv->vval.v_object->obj_class
1521 : lp->ll_tv->vval.v_class;
1522 // TODO: what if class is NULL?
1523 if (cl != NULL)
1524 {
1525 lp->ll_valtype = NULL;
Bram Moolenaard505d172022-12-18 21:42:55 +00001526 int count = v_type == VAR_OBJECT ? cl->class_obj_member_count
1527 : cl->class_class_member_count;
1528 ocmember_T *members = v_type == VAR_OBJECT
1529 ? cl->class_obj_members
1530 : cl->class_class_members;
1531 for (int i = 0; i < count; ++i)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001532 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001533 ocmember_T *om = members + i;
1534 if (STRNCMP(om->ocm_name, key, p - key) == 0
1535 && om->ocm_name[p - key] == NUL)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001536 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001537 switch (om->ocm_access)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001538 {
1539 case ACCESS_PRIVATE:
Bram Moolenaard505d172022-12-18 21:42:55 +00001540 semsg(_(e_cannot_access_private_member_str),
1541 om->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001542 return NULL;
1543 case ACCESS_READ:
1544 if (!(flags & GLV_READ_ONLY))
1545 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001546 semsg(_(e_member_is_not_writable_str),
1547 om->ocm_name);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001548 return NULL;
1549 }
1550 break;
1551 case ACCESS_ALL:
1552 break;
1553 }
1554
Bram Moolenaard505d172022-12-18 21:42:55 +00001555 lp->ll_valtype = om->ocm_type;
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001556
Bram Moolenaard505d172022-12-18 21:42:55 +00001557 if (v_type == VAR_OBJECT)
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001558 lp->ll_tv = ((typval_T *)(
1559 lp->ll_tv->vval.v_object + 1)) + i;
Bram Moolenaard505d172022-12-18 21:42:55 +00001560 else
1561 lp->ll_tv = &cl->class_members_tv[i];
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001562 break;
1563 }
1564 }
1565 if (lp->ll_valtype == NULL)
1566 {
Bram Moolenaard505d172022-12-18 21:42:55 +00001567 if (v_type == VAR_OBJECT)
1568 semsg(_(e_object_member_not_found_str), key);
1569 else
1570 semsg(_(e_class_member_not_found_str), key);
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001571 return NULL;
1572 }
1573 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001575 }
1576
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001577 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001579 return p;
1580}
1581
1582/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001583 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001584 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001586clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001587{
1588 vim_free(lp->ll_exp_name);
1589 vim_free(lp->ll_newkey);
1590}
1591
1592/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001593 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001594 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001595 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1596 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001597 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001598 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001599set_var_lval(
1600 lval_T *lp,
1601 char_u *endp,
1602 typval_T *rettv,
1603 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001604 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1605 char_u *op,
1606 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001607{
1608 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001609 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001610
1611 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001612 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001613 cc = *endp;
1614 *endp = NUL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001615 if (in_vim9script() && check_reserved_name(lp->ll_name, NULL) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001616 return;
1617
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001618 if (lp->ll_blob != NULL)
1619 {
1620 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001621
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001622 if (op != NULL && *op != '=')
1623 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001624 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001625 return;
1626 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001627 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001628 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001629
1630 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1631 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001632 if (lp->ll_empty2)
1633 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1634
Bram Moolenaar68452172021-04-12 21:21:02 +02001635 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1636 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001637 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001638 }
1639 else
1640 {
1641 val = (int)tv_get_number_chk(rettv, &error);
1642 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001643 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001644 }
1645 }
1646 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001648 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001649
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001650 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1651 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001652 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001653 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001654 *endp = cc;
1655 return;
1656 }
1657
Bram Moolenaarff697e62019-02-12 22:28:33 +01001658 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001659 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001660 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001661 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001662 {
1663 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001664 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1665 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001666 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001667 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001668 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001669 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001670 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001672 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001673 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001674 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1675 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001676 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001677 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001678 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001679 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001680 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001681 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001682 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001683 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001684 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001685 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001686 else if (lp->ll_range)
1687 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001688 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1689 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001690 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001691 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001692 return;
1693 }
1694
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001695 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1696 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001697 }
1698 else
1699 {
1700 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001701 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001702 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001703 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1704 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001705 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001706 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001707 return;
1708 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001709
1710 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001711 && check_typval_arg_type(lp->ll_valtype, rettv,
1712 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001713 return;
1714
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001715 if (lp->ll_newkey != NULL)
1716 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001717 if (op != NULL && *op != '=')
1718 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001719 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 return;
1721 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001722 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1723 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001724 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001725
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001726 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001727 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001728 if (di == NULL)
1729 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001730 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1731 {
1732 vim_free(di);
1733 return;
1734 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001735 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001736 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001737 else if (op != NULL && *op != '=')
1738 {
1739 tv_op(lp->ll_tv, rettv, op);
1740 return;
1741 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001742 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001743 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001744
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001745 /*
1746 * Assign the value to the variable or list item.
1747 */
1748 if (copy)
1749 copy_tv(rettv, lp->ll_tv);
1750 else
1751 {
1752 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001753 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001754 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001755 }
1756 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001757}
1758
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001760 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1761 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001762 * Returns OK or FAIL.
1763 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001767 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001768 char_u numbuf[NUMBUFLEN];
1769 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001770 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001772 // Can't do anything with a Funcref or Dict on the right.
1773 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001774 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001775 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1776 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001777 {
1778 switch (tv1->v_type)
1779 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001780 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001781 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001783 case VAR_DICT:
1784 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001785 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001786 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001787 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001788 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001789 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001790 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001791 case VAR_CLASS:
1792 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 break;
1794
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001795 case VAR_BLOB:
1796 if (*op != '+' || tv2->v_type != VAR_BLOB)
1797 break;
1798 // BLOB += BLOB
1799 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1800 {
1801 blob_T *b1 = tv1->vval.v_blob;
1802 blob_T *b2 = tv2->vval.v_blob;
1803 int i, len = blob_len(b2);
1804 for (i = 0; i < len; i++)
1805 ga_append(&b1->bv_ga, blob_get(b2, i));
1806 }
1807 return OK;
1808
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001809 case VAR_LIST:
1810 if (*op != '+' || tv2->v_type != VAR_LIST)
1811 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001812 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001813 if (tv2->vval.v_list != NULL)
1814 {
1815 if (tv1->vval.v_list == NULL)
1816 {
1817 tv1->vval.v_list = tv2->vval.v_list;
1818 ++tv1->vval.v_list->lv_refcount;
1819 }
1820 else
1821 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1822 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001823 return OK;
1824
1825 case VAR_NUMBER:
1826 case VAR_STRING:
1827 if (tv2->v_type == VAR_LIST)
1828 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001829 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001831 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001832 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001833 if (tv2->v_type == VAR_FLOAT)
1834 {
1835 float_T f = n;
1836
Bram Moolenaarff697e62019-02-12 22:28:33 +01001837 if (*op == '%')
1838 break;
1839 switch (*op)
1840 {
1841 case '+': f += tv2->vval.v_float; break;
1842 case '-': f -= tv2->vval.v_float; break;
1843 case '*': f *= tv2->vval.v_float; break;
1844 case '/': f /= tv2->vval.v_float; break;
1845 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001846 clear_tv(tv1);
1847 tv1->v_type = VAR_FLOAT;
1848 tv1->vval.v_float = f;
1849 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001850 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001851 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001852 switch (*op)
1853 {
1854 case '+': n += tv_get_number(tv2); break;
1855 case '-': n -= tv_get_number(tv2); break;
1856 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001857 case '/': n = num_divide(n, tv_get_number(tv2),
1858 &failed); break;
1859 case '%': n = num_modulus(n, tv_get_number(tv2),
1860 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001861 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001862 clear_tv(tv1);
1863 tv1->v_type = VAR_NUMBER;
1864 tv1->vval.v_number = n;
1865 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 }
1867 else
1868 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001869 if (tv2->v_type == VAR_FLOAT)
1870 break;
1871
Bram Moolenaarff697e62019-02-12 22:28:33 +01001872 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001873 s = tv_get_string(tv1);
1874 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001875 clear_tv(tv1);
1876 tv1->v_type = VAR_STRING;
1877 tv1->vval.v_string = s;
1878 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001879 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001880
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001881 case VAR_FLOAT:
1882 {
1883 float_T f;
1884
Bram Moolenaarff697e62019-02-12 22:28:33 +01001885 if (*op == '%' || *op == '.'
1886 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001887 && tv2->v_type != VAR_NUMBER
1888 && tv2->v_type != VAR_STRING))
1889 break;
1890 if (tv2->v_type == VAR_FLOAT)
1891 f = tv2->vval.v_float;
1892 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001893 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001894 switch (*op)
1895 {
1896 case '+': tv1->vval.v_float += f; break;
1897 case '-': tv1->vval.v_float -= f; break;
1898 case '*': tv1->vval.v_float *= f; break;
1899 case '/': tv1->vval.v_float /= f; break;
1900 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001901 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001902 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 }
1904 }
1905
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001906 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 return FAIL;
1908}
1909
1910/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 * Evaluate the expression used in a ":for var in expr" command.
1912 * "arg" points to "var".
1913 * Set "*errp" to TRUE for an error, FALSE otherwise;
1914 * Return a pointer that holds the info. Null when there is an error.
1915 */
1916 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001917eval_for_line(
1918 char_u *arg,
1919 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001920 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001921 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922{
Bram Moolenaar33570922005-01-25 22:26:29 +00001923 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001924 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 typval_T tv;
1927 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001928 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001930 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001932 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 if (fi == NULL)
1934 return NULL;
1935
Bram Moolenaar404557e2021-07-05 21:41:48 +02001936 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1937 &fi->fi_semicolon, FALSE);
1938 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001939 return fi;
1940
Bram Moolenaar404557e2021-07-05 21:41:48 +02001941 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001942 if (expr[0] != 'i' || expr[1] != 'n'
1943 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001945 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1946 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1947 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001948 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001949 return fi;
1950 }
1951
1952 if (skip)
1953 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001954 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1955 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001956 {
1957 *errp = FALSE;
1958 if (!skip)
1959 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001960 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001961 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001962 l = tv.vval.v_list;
1963 if (l == NULL)
1964 {
1965 // a null list is like an empty list: do nothing
1966 clear_tv(&tv);
1967 }
1968 else
1969 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001971 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001973 // No need to increment the refcount, it's already set for
1974 // the list being used in "tv".
1975 fi->fi_list = l;
1976 list_add_watch(l, &fi->fi_lw);
1977 fi->fi_lw.lw_item = l->lv_first;
1978 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001979 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001980 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001981 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001982 fi->fi_bi = 0;
1983 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001984 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001985 typval_T btv;
1986
1987 // Make a copy, so that the iteration still works when the
1988 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001990 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001991 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001992 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001993 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001994 else if (tv.v_type == VAR_STRING)
1995 {
1996 fi->fi_byte_idx = 0;
1997 fi->fi_string = tv.vval.v_string;
1998 tv.vval.v_string = NULL;
1999 if (fi->fi_string == NULL)
2000 fi->fi_string = vim_strsave((char_u *)"");
2001 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 else
2003 {
Sean Dewar80d73952021-08-04 19:25:54 +02002004 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002005 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 }
2007 }
2008 }
2009 if (skip)
2010 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002011 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012
2013 return fi;
2014}
2015
2016/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002017 * Used when looping over a :for line, skip the "in expr" part.
2018 */
2019 void
2020skip_for_lines(void *fi_void, evalarg_T *evalarg)
2021{
2022 forinfo_T *fi = (forinfo_T *)fi_void;
2023 int i;
2024
2025 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002026 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002027}
2028
2029/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002030 * Use the first item in a ":for" list. Advance to the next.
2031 * Assign the values to the variable (list). "arg" points to the first one.
2032 * Return TRUE when a valid item was found, FALSE when at end of list or
2033 * something wrong.
2034 */
2035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002036next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002037{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002038 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002040 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002041 ? (ASSIGN_FINAL
2042 // first round: error if variable exists
2043 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002044 | ASSIGN_NO_MEMBER_TYPE
2045 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002046 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002047 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002048 int skip_assign = in_vim9script() && arg[0] == '_'
2049 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002051 if (fi->fi_blob != NULL)
2052 {
2053 typval_T tv;
2054
2055 if (fi->fi_bi >= blob_len(fi->fi_blob))
2056 return FALSE;
2057 tv.v_type = VAR_NUMBER;
2058 tv.v_lock = VAR_FIXED;
2059 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2060 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002061 if (skip_assign)
2062 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002063 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002064 fi->fi_varcount, flag, NULL) == OK;
2065 }
2066
2067 if (fi->fi_string != NULL)
2068 {
2069 typval_T tv;
2070 int len;
2071
2072 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2073 if (len == 0)
2074 return FALSE;
2075 tv.v_type = VAR_STRING;
2076 tv.v_lock = VAR_FIXED;
2077 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2078 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002079 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002080 if (skip_assign)
2081 result = TRUE;
2082 else
2083 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002084 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002085 vim_free(tv.vval.v_string);
2086 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002087 }
2088
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 item = fi->fi_lw.lw_item;
2090 if (item == NULL)
2091 result = FALSE;
2092 else
2093 {
2094 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002095 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002096 if (skip_assign)
2097 result = TRUE;
2098 else
2099 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002100 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101 }
2102 return result;
2103}
2104
2105/*
2106 * Free the structure used to store info used by ":for".
2107 */
2108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002110{
Bram Moolenaar33570922005-01-25 22:26:29 +00002111 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002112
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002113 if (fi == NULL)
2114 return;
2115 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002116 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002117 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002118 list_unref(fi->fi_list);
2119 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002120 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002121 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002122 else
2123 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 vim_free(fi);
2125}
2126
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002128set_context_for_expression(
2129 expand_T *xp,
2130 char_u *arg,
2131 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002133 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002135 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002137 if (cmdidx == CMD_let || cmdidx == CMD_var
2138 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139 {
2140 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002141 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002142 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002143 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002144 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002145 {
2146 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002147 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002148 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002149 break;
2150 }
2151 return;
2152 }
2153 }
2154 else
2155 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2156 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 while ((xp->xp_pattern = vim_strpbrk(arg,
2158 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2159 {
2160 c = *xp->xp_pattern;
2161 if (c == '&')
2162 {
2163 c = xp->xp_pattern[1];
2164 if (c == '&')
2165 {
2166 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002167 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002170 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002172 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2173 xp->xp_pattern += 2;
2174
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 }
2177 else if (c == '$')
2178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002179 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 xp->xp_context = EXPAND_ENV_VARS;
2181 }
2182 else if (c == '=')
2183 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002184 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185 xp->xp_context = EXPAND_EXPRESSION;
2186 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002187 else if (c == '#'
2188 && xp->xp_context == EXPAND_EXPRESSION)
2189 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002190 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002191 break;
2192 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002193 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 && xp->xp_context == EXPAND_FUNCTIONS
2195 && vim_strchr(xp->xp_pattern, '(') == NULL)
2196 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002197 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 break;
2199 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002200 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002202 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
2204 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2205 if (c == '\\' && xp->xp_pattern[1] != NUL)
2206 ++xp->xp_pattern;
2207 xp->xp_context = EXPAND_NOTHING;
2208 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002209 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002211 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2213 /* skip */ ;
2214 xp->xp_context = EXPAND_NOTHING;
2215 }
2216 else if (c == '|')
2217 {
2218 if (xp->xp_pattern[1] == '|')
2219 {
2220 ++xp->xp_pattern;
2221 xp->xp_context = EXPAND_EXPRESSION;
2222 }
2223 else
2224 xp->xp_context = EXPAND_COMMANDS;
2225 }
2226 else
2227 xp->xp_context = EXPAND_EXPRESSION;
2228 }
2229 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002230 // Doesn't look like something valid, expand as an expression
2231 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002232 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 arg = xp->xp_pattern;
2234 if (*arg != NUL)
2235 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2236 /* skip */ ;
2237 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002238
2239 // ":exe one two" completes "two"
2240 if ((cmdidx == CMD_execute
2241 || cmdidx == CMD_echo
2242 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002243 || cmdidx == CMD_echomsg
2244 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002245 && xp->xp_context == EXPAND_EXPRESSION)
2246 {
2247 for (;;)
2248 {
2249 char_u *n = skiptowhite(arg);
2250
2251 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2252 break;
2253 arg = skipwhite(n);
2254 }
2255 }
2256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 xp->xp_pattern = arg;
2258}
2259
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002261 * Return TRUE if "pat" matches "text".
2262 * Does not use 'cpo' and always uses 'magic'.
2263 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002264 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002265pattern_match(char_u *pat, char_u *text, int ic)
2266{
2267 int matches = FALSE;
2268 char_u *save_cpo;
2269 regmatch_T regmatch;
2270
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002271 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002272 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002273 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002274 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2275 if (regmatch.regprog != NULL)
2276 {
2277 regmatch.rm_ic = ic;
2278 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2279 vim_regfree(regmatch.regprog);
2280 }
2281 p_cpo = save_cpo;
2282 return matches;
2283}
2284
2285/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002286 * Handle a name followed by "(". Both for just "name(arg)" and for
2287 * "expr->name(arg)".
2288 * Returns OK or FAIL.
2289 */
2290 static int
2291eval_func(
2292 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002293 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002294 char_u *name,
2295 int name_len,
2296 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002297 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002298 typval_T *basetv) // "expr" for "expr->name(arg)"
2299{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002300 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002301 char_u *s = name;
2302 int len = name_len;
2303 partial_T *partial;
2304 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002305 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002306 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002307
2308 if (!evaluate)
2309 check_vars(s, len);
2310
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002311 // If "s" is the name of a variable of type VAR_FUNC
2312 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002313 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002314 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002315
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002316 // Need to make a copy, in case evaluating the arguments makes
2317 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002318 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002319 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002320 ret = FAIL;
2321 else
2322 {
2323 funcexe_T funcexe;
2324
2325 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002326 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002327 funcexe.fe_firstline = curwin->w_cursor.lnum;
2328 funcexe.fe_lastline = curwin->w_cursor.lnum;
2329 funcexe.fe_evaluate = evaluate;
2330 funcexe.fe_partial = partial;
2331 funcexe.fe_basetv = basetv;
2332 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002333 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002334 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002335 }
2336 vim_free(s);
2337
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002338 // If evaluate is FALSE rettv->v_type was not set in
2339 // get_func_tv, but it's needed in handle_subscript() to parse
2340 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002341 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2342 {
2343 rettv->vval.v_string = NULL;
2344 rettv->v_type = VAR_FUNC;
2345 }
2346
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002347 // Stop the expression evaluation when immediately
2348 // aborting on error, or when an interrupt occurred or
2349 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002350 if (evaluate && aborting())
2351 {
2352 if (ret == OK)
2353 clear_tv(rettv);
2354 ret = FAIL;
2355 }
2356 return ret;
2357}
2358
2359/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002360 * After a NL, skip over empty lines and comment-only lines.
2361 */
2362 static char_u *
2363newline_skip_comments(char_u *arg)
2364{
2365 char_u *p = arg + 1;
2366
2367 for (;;)
2368 {
2369 p = skipwhite(p);
2370
2371 if (*p == NUL)
2372 break;
2373 if (vim9_comment_start(p))
2374 {
2375 char_u *nl = vim_strchr(p, NL);
2376
2377 if (nl == NULL)
2378 break;
2379 p = nl;
2380 }
2381 if (*p != NL)
2382 break;
2383 ++p; // skip another NL
2384 }
2385 return p;
2386}
2387
2388/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002389 * Get the next line source line without advancing. But do skip over comment
2390 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002391 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002392 */
2393 static char_u *
2394getline_peek_skip_comments(evalarg_T *evalarg)
2395{
2396 for (;;)
2397 {
2398 char_u *next = getline_peek(evalarg->eval_getline,
2399 evalarg->eval_cookie);
2400 char_u *p;
2401
2402 if (next == NULL)
2403 break;
2404 p = skipwhite(next);
2405 if (*p != NUL && !vim9_comment_start(p))
2406 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002407 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002408 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002409 }
2410 return NULL;
2411}
2412
2413/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002414 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2415 * comment) and there is a next line, return the next line (skipping blanks)
2416 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002417 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2418 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002419 * "arg" must point somewhere inside a line, not at the start.
2420 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002421 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002422eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2423{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002424 char_u *p = skipwhite(arg);
2425
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002427 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002428 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002429 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2430 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002431 && (*p == NUL || *p == NL
2432 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002434 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002435
Bram Moolenaare442d592022-05-05 12:20:28 +01002436 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002437 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002438 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002439 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002440 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002441 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002442
Bram Moolenaar9d489562020-07-30 20:08:50 +02002443 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002444 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002445 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002446 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002447 }
2448 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002449 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002450}
2451
2452/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002453 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002454 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002455 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002456 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002457eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002458{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002459 garray_T *gap = &evalarg->eval_ga;
2460 char_u *line;
2461
Bram Moolenaar39be4982022-05-06 21:51:50 +01002462 if (arg != NULL)
2463 {
2464 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002465 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002466 // Truncate before a trailing comment, so that concatenating the lines
2467 // won't turn the rest into a comment.
2468 if (*skipwhite(arg) == '#')
2469 *arg = NUL;
2470 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002471
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002472 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002473 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2474 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002475 else
2476 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002477 if (line == NULL)
2478 return NULL;
2479
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002480 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002481 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2482 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002483 char_u *p = skipwhite(line);
2484
2485 // Going to concatenate the lines after parsing. For an empty or
2486 // comment line use an empty string.
2487 if (*p == NUL || vim9_comment_start(p))
2488 {
2489 vim_free(line);
2490 line = vim_strsave((char_u *)"");
2491 }
2492
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002493 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2494 ++gap->ga_len;
2495 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002496 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002497 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002498 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002499 evalarg->eval_tofree = line;
2500 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002501
2502 // Advanced to the next line, "arg" no longer points into the previous
2503 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002504 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002505 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002506}
2507
Bram Moolenaare6b53242020-07-01 17:28:33 +02002508/*
2509 * Call eval_next_non_blank() and get the next line if needed.
2510 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002511 char_u *
2512skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2513{
2514 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002515 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002516
Bram Moolenaar962d7212020-07-04 14:15:00 +02002517 if (evalarg == NULL)
2518 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002519 eval_next_non_blank(p, evalarg, &getnext);
2520 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002521 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002522 return p;
2523}
2524
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002525/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002526 * The "eval" functions have an "evalarg" argument: When NULL or
2527 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2528 * parsed but not executed. The functions may return OK, but the rettv will be
2529 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 */
2531
2532/*
2533 * Handle zero level expression.
2534 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002536 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002537 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 * Return OK or FAIL.
2539 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002541eval0(
2542 char_u *arg,
2543 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002544 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002545 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002547 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2548}
2549
2550/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002551 * If "arg" is a simple function call without arguments then call it and return
2552 * the result. Otherwise return NOTDONE.
2553 */
2554 int
2555may_call_simple_func(
2556 char_u *arg,
2557 typval_T *rettv)
2558{
2559 char_u *parens = (char_u *)strstr((char *)arg, "()");
2560 int r = NOTDONE;
2561
2562 // If the expression is "FuncName()" then we can skip a lot of overhead.
2563 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2564 {
2565 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2566
2567 if (to_name_end(p, TRUE) == parens)
2568 r = call_simple_func(arg, (int)(parens - arg), rettv);
2569 }
2570 return r;
2571}
2572
2573/*
2574 * Handle zero level expression with optimization for a simple function call.
2575 * Same arguments and return value as eval0().
2576 */
2577 int
2578eval0_simple_funccal(
2579 char_u *arg,
2580 typval_T *rettv,
2581 exarg_T *eap,
2582 evalarg_T *evalarg)
2583{
2584 int r = may_call_simple_func(arg, rettv);
2585
2586 if (r == NOTDONE)
2587 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2588 return r;
2589}
2590
2591/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002592 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2593 * expression and don't check what comes after the expression.
2594 */
2595 int
2596eval0_retarg(
2597 char_u *arg,
2598 typval_T *rettv,
2599 exarg_T *eap,
2600 evalarg_T *evalarg,
2601 char_u **retarg)
2602{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 int ret;
2604 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002605 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002606 int did_emsg_before = did_emsg;
2607 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002608 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002609 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002610 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002613 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002614
Bram Moolenaar79481362022-06-27 20:15:10 +01002615 if (ret != FAIL)
2616 {
2617 expr_end = p;
2618 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002619
Bram Moolenaar79481362022-06-27 20:15:10 +01002620 // In Vim9 script a command block is not split at NL characters for
2621 // commands using an expression argument. Skip over a '#' comment to
2622 // check for a following NL. Require white space before the '#'.
2623 if (in_vim9script() && p > expr_end && retarg == NULL)
2624 while (*p == '#')
2625 {
2626 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002627
Bram Moolenaar79481362022-06-27 20:15:10 +01002628 if (nl == NULL)
2629 break;
2630 p = skipwhite(nl + 1);
2631 if (eap != NULL && *p != NUL)
2632 eap->nextcmd = p;
2633 check_for_end = FALSE;
2634 }
2635
2636 if (check_for_end)
2637 end_error = !ends_excmd2(arg, p);
2638 }
2639
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002640 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
2642 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002643 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 /*
2645 * Report the invalid expression unless the expression evaluation has
2646 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002647 * exception, or we already gave a more specific error.
2648 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002650 if (!aborting()
2651 && did_emsg == did_emsg_before
2652 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002653 && (flags & EVAL_CONSTANT) == 0
2654 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002655 {
2656 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002657 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002658 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002659 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002660 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002661
2662 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002663 // a next command to avoid more errors, unless "|" is following, which
2664 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002665 if (eap != NULL && p != NULL
2666 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002667 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002668 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002670
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002671 if (retarg != NULL)
2672 *retarg = p;
2673 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002674 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002675
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 return ret;
2677}
2678
2679/*
2680 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002681 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002682 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 *
2684 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002685 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002687 * Note: "rettv.v_lock" is not set.
2688 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 * Return OK or FAIL.
2690 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002691 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002692eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002694 char_u *p;
2695 int getnext;
2696
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002697 CLEAR_POINTER(rettv);
2698
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 /*
2700 * Get the first variable.
2701 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002702 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 return FAIL;
2704
Bram Moolenaar793648f2020-06-26 21:28:25 +02002705 p = eval_next_non_blank(*arg, evalarg, &getnext);
2706 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002708 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002709 int result;
2710 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002711 evalarg_T *evalarg_used = evalarg;
2712 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002714 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002715 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002716
2717 if (evalarg == NULL)
2718 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002719 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002720 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002721 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002722 orig_flags = evalarg_used->eval_flags;
2723 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002724
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002725 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002726 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002727 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002728 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002729 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002730 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002731 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002732 clear_tv(rettv);
2733 return FAIL;
2734 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002735 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002736 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002737
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002739 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002741 int error = FALSE;
2742
Bram Moolenaar13106602020-10-04 16:06:05 +02002743 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002744 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002745 else if (vim9script)
2746 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002747 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002749 if (error || !op_falsy || !result)
2750 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002751 if (error)
2752 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754
2755 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002756 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002758 if (op_falsy)
2759 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002760 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002761 {
mityu4ac198c2021-05-28 17:52:40 +02002762 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002763 clear_tv(rettv);
2764 return FAIL;
2765 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002766 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002767 evalarg_used->eval_flags = (op_falsy ? !result : result)
2768 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2769 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002770 {
2771 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002773 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002774 if (!op_falsy || !result)
2775 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002777 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002779 /*
2780 * Check for the ":".
2781 */
2782 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2783 if (*p != ':')
2784 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002785 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002786 if (evaluate && result)
2787 clear_tv(rettv);
2788 evalarg_used->eval_flags = orig_flags;
2789 return FAIL;
2790 }
2791 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002792 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002793 else
2794 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002795 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002796 {
2797 error_white_both(p, 1);
2798 clear_tv(rettv);
2799 evalarg_used->eval_flags = orig_flags;
2800 return FAIL;
2801 }
2802 *arg = p;
2803 }
2804
2805 /*
2806 * Get the third variable. Recursive!
2807 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002808 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002809 {
mityu4ac198c2021-05-28 17:52:40 +02002810 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002811 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002812 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002813 return FAIL;
2814 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002815 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2816 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002817 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002818 if (eval1(arg, &var2, evalarg_used) == FAIL)
2819 {
2820 if (evaluate && result)
2821 clear_tv(rettv);
2822 evalarg_used->eval_flags = orig_flags;
2823 return FAIL;
2824 }
2825 if (evaluate && !result)
2826 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002828
2829 if (evalarg == NULL)
2830 clear_evalarg(&local_evalarg, NULL);
2831 else
2832 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 }
2834
2835 return OK;
2836}
2837
2838/*
2839 * Handle first level expression:
2840 * expr2 || expr2 || expr2 logical OR
2841 *
2842 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002843 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 *
2845 * Return OK or FAIL.
2846 */
2847 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002848eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002850 char_u *p;
2851 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852
2853 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002854 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 return FAIL;
2858
2859 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002860 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002862 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002863 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002865 evalarg_T *evalarg_used = evalarg;
2866 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 int evaluate;
2868 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002869 long result = FALSE;
2870 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002871 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002872 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002873
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002874 if (evalarg == NULL)
2875 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002876 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002877 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002878 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002879 orig_flags = evalarg_used->eval_flags;
2880 evaluate = orig_flags & EVAL_EVALUATE;
2881 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002882 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002883 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002884 result = tv_get_bool_chk(rettv, &error);
2885 else if (tv_get_number_chk(rettv, &error) != 0)
2886 result = TRUE;
2887 clear_tv(rettv);
2888 if (error)
2889 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 }
2891
2892 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002893 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002895 while (p[0] == '|' && p[1] == '|')
2896 {
2897 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002898 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002899 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002900 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002901 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002902 {
2903 error_white_both(p, 2);
2904 clear_tv(rettv);
2905 return FAIL;
2906 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002907 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002908 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002909
2910 /*
2911 * Get the second variable.
2912 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002913 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002914 {
mityu4ac198c2021-05-28 17:52:40 +02002915 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002916 clear_tv(rettv);
2917 return FAIL;
2918 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002919 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2920 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002921 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002922 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002923 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002924
2925 /*
2926 * Compute the result.
2927 */
2928 if (evaluate && !result)
2929 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002930 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002931 result = tv_get_bool_chk(&var2, &error);
2932 else if (tv_get_number_chk(&var2, &error) != 0)
2933 result = TRUE;
2934 clear_tv(&var2);
2935 if (error)
2936 return FAIL;
2937 }
2938 if (evaluate)
2939 {
2940 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002941 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002942 rettv->v_type = VAR_BOOL;
2943 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002944 }
2945 else
2946 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002947 rettv->v_type = VAR_NUMBER;
2948 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002949 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002950 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002951
2952 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002954
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002955 if (evalarg == NULL)
2956 clear_evalarg(&local_evalarg, NULL);
2957 else
2958 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 }
2960
2961 return OK;
2962}
2963
2964/*
2965 * Handle second level expression:
2966 * expr3 && expr3 && expr3 logical AND
2967 *
2968 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002969 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 *
2971 * Return OK or FAIL.
2972 */
2973 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002974eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002976 char_u *p;
2977 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978
2979 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002980 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 return FAIL;
2984
2985 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002986 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002988 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002989 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002991 evalarg_T *evalarg_used = evalarg;
2992 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002993 int orig_flags;
2994 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002995 long result = TRUE;
2996 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002997 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002998 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002999
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003000 if (evalarg == NULL)
3001 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01003002 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003003 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003004 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003005 orig_flags = evalarg_used->eval_flags;
3006 evaluate = orig_flags & EVAL_EVALUATE;
3007 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003008 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003009 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003010 result = tv_get_bool_chk(rettv, &error);
3011 else if (tv_get_number_chk(rettv, &error) == 0)
3012 result = FALSE;
3013 clear_tv(rettv);
3014 if (error)
3015 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 }
3017
3018 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003019 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003021 while (p[0] == '&' && p[1] == '&')
3022 {
3023 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003024 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003025 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003026 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003027 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003028 {
3029 error_white_both(p, 2);
3030 clear_tv(rettv);
3031 return FAIL;
3032 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003033 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003034 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003035
3036 /*
3037 * Get the second variable.
3038 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003039 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003040 {
mityu4ac198c2021-05-28 17:52:40 +02003041 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003042 clear_tv(rettv);
3043 return FAIL;
3044 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003045 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3046 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003047 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003048 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003049 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003050 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003051
3052 /*
3053 * Compute the result.
3054 */
3055 if (evaluate && result)
3056 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003057 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003058 result = tv_get_bool_chk(&var2, &error);
3059 else if (tv_get_number_chk(&var2, &error) == 0)
3060 result = FALSE;
3061 clear_tv(&var2);
3062 if (error)
3063 return FAIL;
3064 }
3065 if (evaluate)
3066 {
3067 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003068 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003069 rettv->v_type = VAR_BOOL;
3070 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003071 }
3072 else
3073 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003074 rettv->v_type = VAR_NUMBER;
3075 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003076 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003077 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003078
3079 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003081
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003082 if (evalarg == NULL)
3083 clear_evalarg(&local_evalarg, NULL);
3084 else
3085 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 }
3087
3088 return OK;
3089}
3090
3091/*
3092 * Handle third level expression:
3093 * var1 == var2
3094 * var1 =~ var2
3095 * var1 != var2
3096 * var1 !~ var2
3097 * var1 > var2
3098 * var1 >= var2
3099 * var1 < var2
3100 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003101 * var1 is var2
3102 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 *
3104 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003105 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 *
3107 * Return OK or FAIL.
3108 */
3109 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003110eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003113 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003114 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003116 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003119 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003121 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 return FAIL;
3123
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003124 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003125
Bram Moolenaar696ba232020-07-29 21:20:41 +02003126 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127
3128 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003129 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003131 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003133 typval_T var2;
3134 int ic;
3135 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003136 int evaluate = evalarg == NULL
3137 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003138 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003139
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003140 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003141 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003142 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003143 p = *arg;
3144 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003145 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3146 {
mityu4ac198c2021-05-28 17:52:40 +02003147 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003148 clear_tv(rettv);
3149 return FAIL;
3150 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003151
Bram Moolenaar696ba232020-07-29 21:20:41 +02003152 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3153 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003154 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003155 clear_tv(rettv);
3156 return FAIL;
3157 }
3158
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003159 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 if (p[len] == '?')
3161 {
3162 ic = TRUE;
3163 ++len;
3164 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003165 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 else if (p[len] == '#')
3167 {
3168 ic = FALSE;
3169 ++len;
3170 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003171 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003173 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
3175 /*
3176 * Get the second variable.
3177 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003178 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3179 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003180 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003181 clear_tv(rettv);
3182 return FAIL;
3183 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003184 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003185 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003187 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 return FAIL;
3189 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003190 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003191 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003192 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003193
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003194 // use the line of the comparison for messages
3195 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003196 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003197 {
3198 ret = FAIL;
3199 clear_tv(rettv);
3200 }
3201 else
3202 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003203 clear_tv(&var2);
3204 return ret;
3205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 }
3207
3208 return OK;
3209}
3210
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003211/*
3212 * Make a copy of blob "tv1" and append blob "tv2".
3213 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 void
3215eval_addblob(typval_T *tv1, typval_T *tv2)
3216{
3217 blob_T *b1 = tv1->vval.v_blob;
3218 blob_T *b2 = tv2->vval.v_blob;
3219 blob_T *b = blob_alloc();
3220 int i;
3221
3222 if (b != NULL)
3223 {
3224 for (i = 0; i < blob_len(b1); i++)
3225 ga_append(&b->bv_ga, blob_get(b1, i));
3226 for (i = 0; i < blob_len(b2); i++)
3227 ga_append(&b->bv_ga, blob_get(b2, i));
3228
3229 clear_tv(tv1);
3230 rettv_blob_set(tv1, b);
3231 }
3232}
3233
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003234/*
3235 * Make a copy of list "tv1" and append list "tv2".
3236 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 int
3238eval_addlist(typval_T *tv1, typval_T *tv2)
3239{
3240 typval_T var3;
3241
3242 // concatenate Lists
3243 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3244 {
3245 clear_tv(tv1);
3246 clear_tv(tv2);
3247 return FAIL;
3248 }
3249 clear_tv(tv1);
3250 *tv1 = var3;
3251 return OK;
3252}
3253
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003255 * Handle the bitwise left/right shift operator expression:
3256 * var1 << var2
3257 * var1 >> var2
3258 *
3259 * "arg" must point to the first non-white of the expression.
3260 * "arg" is advanced to just after the recognized expression.
3261 *
3262 * Return OK or FAIL.
3263 */
3264 static int
3265eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3266{
3267 /*
3268 * Get the first expression.
3269 */
3270 if (eval6(arg, rettv, evalarg) == FAIL)
3271 return FAIL;
3272
3273 /*
3274 * Repeat computing, until no '<<' or '>>' is following.
3275 */
3276 for (;;)
3277 {
3278 char_u *p;
3279 int getnext;
3280 exprtype_T type;
3281 int evaluate;
3282 typval_T var2;
3283 int vim9script;
3284
3285 p = eval_next_non_blank(*arg, evalarg, &getnext);
3286 if (p[0] == '<' && p[1] == '<')
3287 type = EXPR_LSHIFT;
3288 else if (p[0] == '>' && p[1] == '>')
3289 type = EXPR_RSHIFT;
3290 else
3291 return OK;
3292
3293 // Handle a bitwise left or right shift operator
3294 if (rettv->v_type != VAR_NUMBER)
3295 {
3296 // left operand should be a number
3297 emsg(_(e_bitshift_ops_must_be_number));
3298 clear_tv(rettv);
3299 return FAIL;
3300 }
3301
3302 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3303 vim9script = in_vim9script();
3304 if (getnext)
3305 {
3306 *arg = eval_next_line(*arg, evalarg);
3307 p = *arg;
3308 }
3309 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3310 {
3311 error_white_both(*arg, 2);
3312 clear_tv(rettv);
3313 return FAIL;
3314 }
3315
3316 /*
3317 * Get the second variable.
3318 */
3319 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3320 {
3321 error_white_both(p, 2);
3322 clear_tv(rettv);
3323 return FAIL;
3324 }
3325 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3326 if (eval6(arg, &var2, evalarg) == FAIL)
3327 {
3328 clear_tv(rettv);
3329 return FAIL;
3330 }
3331
3332 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3333 {
3334 // right operand should be a positive number
3335 if (var2.v_type != VAR_NUMBER)
3336 emsg(_(e_bitshift_ops_must_be_number));
3337 else
dundargocc57b5bc2022-11-02 13:30:51 +00003338 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003339 clear_tv(rettv);
3340 clear_tv(&var2);
3341 return FAIL;
3342 }
3343
3344 if (evaluate)
3345 {
3346 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3347 // shifting more bits than we have always results in zero
3348 rettv->vval.v_number = 0;
3349 else if (type == EXPR_LSHIFT)
3350 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003351 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003352 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003353 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003354 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003355 }
3356
3357 clear_tv(&var2);
3358 }
3359
3360 return OK;
3361}
3362
3363/*
3364 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003365 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003367 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003368 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 *
3370 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003371 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 *
3373 * Return OK or FAIL.
3374 */
3375 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003376eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003379 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003381 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 return FAIL;
3383
3384 /*
3385 * Repeat computing, until no '+', '-' or '.' is following.
3386 */
3387 for (;;)
3388 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003389 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003390 int getnext;
3391 char_u *p;
3392 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003393 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003394 int concat;
3395 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003396 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003397
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003398 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003399 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003400 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003401 p = eval_next_non_blank(*arg, evalarg, &getnext);
3402 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003403 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003404 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3405 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003407 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3408 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003409
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003410 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003411 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003412 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003413 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003414 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003415 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003416 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003417 {
mityu4ac198c2021-05-28 17:52:40 +02003418 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003419 clear_tv(rettv);
3420 return FAIL;
3421 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003422 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003423 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003424 if ((op != '+' || (rettv->v_type != VAR_LIST
3425 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003426 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003427 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003428 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003429 int error = FALSE;
3430
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003431 // For "list + ...", an illegal use of the first operand as
3432 // a number cannot be determined before evaluating the 2nd
3433 // operand: if this is also a list, all is ok.
3434 // For "something . ...", "something - ..." or "non-list + ...",
3435 // we know that the first operand needs to be a string or number
3436 // without evaluating the 2nd operand. So check before to avoid
3437 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003438 if (op != '.')
3439 tv_get_number_chk(rettv, &error);
3440 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003441 {
3442 clear_tv(rettv);
3443 return FAIL;
3444 }
3445 }
3446
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 /*
3448 * Get the second variable.
3449 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003450 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003451 {
mityu89dcb4d2021-05-28 13:50:17 +02003452 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003453 clear_tv(rettv);
3454 return FAIL;
3455 }
3456 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003457 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003459 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 return FAIL;
3461 }
3462
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003463 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 /*
3466 * Compute the result.
3467 */
3468 if (op == '.')
3469 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003470 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3471 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003472 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003473
Bram Moolenaar2e086612020-08-25 22:37:48 +02003474 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003475 || var2.v_type == VAR_CHANNEL
3476 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003477 semsg(_(e_using_invalid_value_as_string_str),
3478 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003479 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003480 {
3481 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3482 var2.vval.v_float);
3483 s2 = buf2;
3484 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003485 else
3486 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003487 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003488 {
3489 clear_tv(rettv);
3490 clear_tv(&var2);
3491 return FAIL;
3492 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003493 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003494 clear_tv(rettv);
3495 rettv->v_type = VAR_STRING;
3496 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003498 else if (op == '+' && rettv->v_type == VAR_BLOB
3499 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003501 else if (op == '+' && rettv->v_type == VAR_LIST
3502 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003503 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003505 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 else
3508 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003509 int error = FALSE;
3510 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003511 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003512
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003513 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003514 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003515 f1 = rettv->vval.v_float;
3516 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003517 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003518 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003519 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003520 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003521 if (error)
3522 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003523 // This can only happen for "list + non-list" or
3524 // "blob + non-blob". For "non-list + ..." or
3525 // "something - ...", we returned before evaluating the
3526 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003527 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003528 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003529 return FAIL;
3530 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003531 if (var2.v_type == VAR_FLOAT)
3532 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003533 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003534 if (var2.v_type == VAR_FLOAT)
3535 {
3536 f2 = var2.vval.v_float;
3537 n2 = 0;
3538 }
3539 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003540 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003541 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003542 if (error)
3543 {
3544 clear_tv(rettv);
3545 clear_tv(&var2);
3546 return FAIL;
3547 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 if (rettv->v_type == VAR_FLOAT)
3549 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003550 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003551 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003552
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003553 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003554 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3555 {
3556 if (op == '+')
3557 f1 = f1 + f2;
3558 else
3559 f1 = f1 - f2;
3560 rettv->v_type = VAR_FLOAT;
3561 rettv->vval.v_float = f1;
3562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003564 {
3565 if (op == '+')
3566 n1 = n1 + n2;
3567 else
3568 n1 = n1 - n2;
3569 rettv->v_type = VAR_NUMBER;
3570 rettv->vval.v_number = n1;
3571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003573 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 }
3575 }
3576 return OK;
3577}
3578
3579/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003580 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581 * * number multiplication
3582 * / number division
3583 * % number modulo
3584 *
3585 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003586 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 *
3588 * Return OK or FAIL.
3589 */
3590 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003591eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003592 char_u **arg,
3593 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003594 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003595 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003597 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598
3599 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003600 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003602 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 return FAIL;
3604
3605 /*
3606 * Repeat computing, until no '*', '/' or '%' is following.
3607 */
3608 for (;;)
3609 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003610 int evaluate;
3611 int getnext;
3612 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003613 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003614 int op;
3615 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003616 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003617 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003618
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003619 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003620 p = eval_next_non_blank(*arg, evalarg, &getnext);
3621 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003622 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003624
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003625 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003626 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003627 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003628 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003629 {
3630 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3631 {
mityu4ac198c2021-05-28 17:52:40 +02003632 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003633 clear_tv(rettv);
3634 return FAIL;
3635 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003636 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003639 f1 = 0;
3640 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003641 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003642 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003644 if (rettv->v_type == VAR_FLOAT)
3645 {
3646 f1 = rettv->vval.v_float;
3647 use_float = TRUE;
3648 n1 = 0;
3649 }
3650 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003651 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003652 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003653 if (error)
3654 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 else
3657 n1 = 0;
3658
3659 /*
3660 * Get the second variable.
3661 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003662 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3663 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003664 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003665 clear_tv(rettv);
3666 return FAIL;
3667 }
3668 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003669 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 return FAIL;
3671
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003672 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003674 if (var2.v_type == VAR_FLOAT)
3675 {
3676 if (!use_float)
3677 {
3678 f1 = n1;
3679 use_float = TRUE;
3680 }
3681 f2 = var2.vval.v_float;
3682 n2 = 0;
3683 }
3684 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003685 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003686 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003687 clear_tv(&var2);
3688 if (error)
3689 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003690 if (use_float)
3691 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693
3694 /*
3695 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003696 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003698 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003700 if (op == '*')
3701 f1 = f1 * f2;
3702 else if (op == '/')
3703 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003704#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003705 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003706 if (f2 == 0.0)
3707 {
3708 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003709 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003710 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003711 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003712 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003713 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003714 }
3715 else
3716 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003717#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003718 // We rely on the floating point library to handle divide
3719 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003720 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003721#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003724 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003725 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003726 return FAIL;
3727 }
3728 rettv->v_type = VAR_FLOAT;
3729 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
3731 else
3732 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003733 int failed = FALSE;
3734
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003735 if (op == '*')
3736 n1 = n1 * n2;
3737 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003738 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003740 n1 = num_modulus(n1, n2, &failed);
3741 if (failed)
3742 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003743
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003744 rettv->v_type = VAR_NUMBER;
3745 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748 }
3749
3750 return OK;
3751}
3752
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003753/*
3754 * Handle a type cast before a base level expression.
3755 * "arg" must point to the first non-white of the expression.
3756 * "arg" is advanced to just after the recognized expression.
3757 * Return OK or FAIL.
3758 */
3759 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003760eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003761 char_u **arg,
3762 typval_T *rettv,
3763 evalarg_T *evalarg,
3764 int want_string) // after "." operator
3765{
3766 type_T *want_type = NULL;
3767 garray_T type_list; // list of pointers to allocated types
3768 int res;
3769 int evaluate = evalarg == NULL ? 0
3770 : (evalarg->eval_flags & EVAL_EVALUATE);
3771
3772 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003773 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3774 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003775 {
3776 ++*arg;
3777 ga_init2(&type_list, sizeof(type_T *), 10);
3778 want_type = parse_type(arg, &type_list, TRUE);
3779 if (want_type == NULL && (evaluate || **arg != '>'))
3780 {
3781 clear_type_list(&type_list);
3782 return FAIL;
3783 }
3784
3785 if (**arg != '>')
3786 {
3787 if (*skipwhite(*arg) == '>')
3788 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3789 else
3790 emsg(_(e_missing_gt));
3791 clear_type_list(&type_list);
3792 return FAIL;
3793 }
3794 ++*arg;
3795 *arg = skipwhite_and_linebreak(*arg, evalarg);
3796 }
3797
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003798 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003799
3800 if (want_type != NULL && evaluate)
3801 {
3802 if (res == OK)
3803 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003804 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3805 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003806
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003807 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003808 {
3809 if (want_type == &t_bool && actual != &t_bool
3810 && (actual->tt_flags & TTFLAG_BOOL_OK))
3811 {
3812 int n = tv2bool(rettv);
3813
3814 // can use "0" and "1" for boolean in some places
3815 clear_tv(rettv);
3816 rettv->v_type = VAR_BOOL;
3817 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3818 }
3819 else
3820 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003821 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003822
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003823 where.wt_variable = TRUE;
3824 res = check_type(want_type, actual, TRUE, where);
3825 }
3826 }
3827 }
3828 clear_type_list(&type_list);
3829 }
3830
3831 return res;
3832}
3833
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003834 int
3835eval_leader(char_u **arg, int vim9)
3836{
3837 char_u *s = *arg;
3838 char_u *p = *arg;
3839
3840 while (*p == '!' || *p == '-' || *p == '+')
3841 {
3842 char_u *n = skipwhite(p + 1);
3843
3844 // ++, --, -+ and +- are not accepted in Vim9 script
3845 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3846 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003847 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003848 return FAIL;
3849 }
3850 p = n;
3851 }
3852 *arg = p;
3853 return OK;
3854}
3855
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003857 * Check for a predefined value "true", "false" and "null.*".
3858 * Return OK when recognized.
3859 */
3860 int
3861handle_predefined(char_u *s, int len, typval_T *rettv)
3862{
3863 switch (len)
3864 {
3865 case 4: if (STRNCMP(s, "true", 4) == 0)
3866 {
3867 rettv->v_type = VAR_BOOL;
3868 rettv->vval.v_number = VVAL_TRUE;
3869 return OK;
3870 }
3871 if (STRNCMP(s, "null", 4) == 0)
3872 {
3873 rettv->v_type = VAR_SPECIAL;
3874 rettv->vval.v_number = VVAL_NULL;
3875 return OK;
3876 }
3877 break;
3878 case 5: if (STRNCMP(s, "false", 5) == 0)
3879 {
3880 rettv->v_type = VAR_BOOL;
3881 rettv->vval.v_number = VVAL_FALSE;
3882 return OK;
3883 }
3884 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003885 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3886 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003887#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003888 rettv->v_type = VAR_JOB;
3889 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003890#else
3891 rettv->v_type = VAR_SPECIAL;
3892 rettv->vval.v_number = VVAL_NULL;
3893#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003894 return OK;
3895 }
3896 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003897 case 9:
3898 if (STRNCMP(s, "null_", 5) != 0)
3899 break;
3900 if (STRNCMP(s + 5, "list", 4) == 0)
3901 {
3902 rettv->v_type = VAR_LIST;
3903 rettv->vval.v_list = NULL;
3904 return OK;
3905 }
3906 if (STRNCMP(s + 5, "dict", 4) == 0)
3907 {
3908 rettv->v_type = VAR_DICT;
3909 rettv->vval.v_dict = NULL;
3910 return OK;
3911 }
3912 if (STRNCMP(s + 5, "blob", 4) == 0)
3913 {
3914 rettv->v_type = VAR_BLOB;
3915 rettv->vval.v_blob = NULL;
3916 return OK;
3917 }
3918 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003919 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3920 {
3921 rettv->v_type = VAR_CLASS;
3922 rettv->vval.v_class = NULL;
3923 return OK;
3924 }
3925 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003926 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3927 {
3928 rettv->v_type = VAR_STRING;
3929 rettv->vval.v_string = NULL;
3930 return OK;
3931 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003932 if (STRNCMP(s, "null_object", 11) == 0)
3933 {
3934 rettv->v_type = VAR_OBJECT;
3935 rettv->vval.v_object = NULL;
3936 return OK;
3937 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003938 break;
3939 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003940 if (STRNCMP(s, "null_channel", 12) == 0)
3941 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003942#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003943 rettv->v_type = VAR_CHANNEL;
3944 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003945#else
3946 rettv->v_type = VAR_SPECIAL;
3947 rettv->vval.v_number = VVAL_NULL;
3948#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003949 return OK;
3950 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003951 if (STRNCMP(s, "null_partial", 12) == 0)
3952 {
3953 rettv->v_type = VAR_PARTIAL;
3954 rettv->vval.v_partial = NULL;
3955 return OK;
3956 }
3957 break;
3958 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3959 {
3960 rettv->v_type = VAR_FUNC;
3961 rettv->vval.v_string = NULL;
3962 return OK;
3963 }
3964 break;
3965 }
3966 return FAIL;
3967}
3968
3969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 * Handle sixth level expression:
3971 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003972 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003973 * "string" string constant
3974 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * &option-name option value
3976 * @r register contents
3977 * identifier variable value
3978 * function() function call
3979 * $VAR environment variable
3980 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003981 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003982 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003983 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003984 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 *
3986 * Also handle:
3987 * ! in front logical NOT
3988 * - in front unary minus
3989 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003990 * trailing [] subscript in String or List
3991 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003992 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 *
3994 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003995 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 *
3997 * Return OK or FAIL.
3998 */
3999 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004000eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001 char_u **arg,
4002 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004003 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004006 int evaluate = evalarg != NULL
4007 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 int len;
4009 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004010 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 char_u *start_leader, *end_leader;
4012 int ret = OK;
4013 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004014 static int recurse = 0;
4015 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016
4017 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004019 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004021 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022
4023 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004024 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 */
4026 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004027 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004028 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 end_leader = *arg;
4030
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004031 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004032 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004033 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004034 ++*arg;
4035 return FAIL;
4036 }
4037
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004038 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004039 // and crash. With MSVC the stack is smaller.
4040 if (recurse ==
4041#ifdef _MSC_VER
4042 300
4043#else
4044 1000
4045#endif
4046 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004047 {
4048 semsg(_(e_expression_too_recursive_str), *arg);
4049 return FAIL;
4050 }
4051 ++recurse;
4052
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 switch (**arg)
4054 {
4055 /*
4056 * Number constant.
4057 */
4058 case '0':
4059 case '1':
4060 case '2':
4061 case '3':
4062 case '4':
4063 case '5':
4064 case '6':
4065 case '7':
4066 case '8':
4067 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004068 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004069
4070 // Apply prefixed "-" and "+" now. Matters especially when
4071 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004072 if (ret == OK && evaluate && end_leader > start_leader
4073 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004074 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 break;
4076
4077 /*
4078 * String constant: "string".
4079 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004080 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 break;
4082
4083 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004084 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004086 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004087 break;
4088
4089 /*
4090 * List: [expr, expr]
4091 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004092 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 break;
4094
4095 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004096 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004097 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004098 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004099 {
4100 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4101 }
4102 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004103 {
4104 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004105 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004106 }
4107 else
4108 ret = NOTDONE;
4109 break;
4110
4111 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004112 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004113 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004114 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004115 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004116 ret = NOTDONE;
4117 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004118 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004119 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004120 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004121 break;
4122
4123 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004124 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004126 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 break;
4128
4129 /*
4130 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004131 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
LemonBoy2eaef102022-05-06 13:14:50 +01004133 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4134 ret = eval_interp_string(arg, rettv, evaluate);
4135 else
4136 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 break;
4138
4139 /*
4140 * Register contents: @r.
4141 */
4142 case '@': ++*arg;
4143 if (evaluate)
4144 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004145 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004146 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004147 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004148 emsg_invreg(**arg);
4149 else
4150 {
4151 rettv->v_type = VAR_STRING;
4152 rettv->vval.v_string = get_reg_contents(**arg,
4153 GREG_EXPR_SRC);
4154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004155 }
4156 if (**arg != NUL)
4157 ++*arg;
4158 break;
4159
4160 /*
4161 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004162 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004164 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004165 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004166 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004167 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004168 if (ret == OK && evaluate)
4169 {
4170 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4171
Bram Moolenaara9931532021-06-12 15:58:16 +02004172 // Compile it here to get the return type. The return
4173 // type is optional, when it's missing use t_unknown.
4174 // This is recognized in compile_return().
4175 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4176 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004177 if (compile_def_function(ufunc, FALSE,
4178 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004179 {
4180 clear_tv(rettv);
4181 ret = FAIL;
4182 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004183 }
4184 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004185 if (ret == NOTDONE)
4186 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004187 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004188 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004189
Bram Moolenaar9215f012020-06-27 21:18:00 +02004190 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004191 if (**arg == ')')
4192 ++*arg;
4193 else if (ret == OK)
4194 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004195 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004196 clear_tv(rettv);
4197 ret = FAIL;
4198 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200 break;
4201
Bram Moolenaar8c711452005-01-14 21:53:12 +00004202 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 break;
4204 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004205
4206 if (ret == NOTDONE)
4207 {
4208 /*
4209 * Must be a variable or function name.
4210 * Can also be a curly-braces kind of name: {expr}.
4211 */
4212 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004213 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004214 if (alias != NULL)
4215 s = alias;
4216
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004217 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004218 ret = FAIL;
4219 else
4220 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004221 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4222
Bram Moolenaar4525a572022-02-13 11:57:33 +00004223 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004224 {
4225 emsg(_(e_cannot_use_underscore_here));
4226 ret = FAIL;
4227 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004228 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004229 && s[0] == 's' && s[1] == ':')
4230 {
4231 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4232 ret = FAIL;
4233 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004234 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004235 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004236 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004237 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004238 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004239 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004240 else if (flags & EVAL_CONSTANT)
4241 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004242 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004243 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004244 // get the value of "true", "false", etc. or a variable
4245 ret = FAIL;
4246 if (vim9script)
4247 ret = handle_predefined(s, len, rettv);
4248 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004249 {
4250 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004251 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004252 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004253 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004254 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004255 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004256 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004257 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004258 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004259 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004260 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004261 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004262 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004263 }
4264
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004265 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4266 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004267 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004268 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
4270 /*
4271 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4272 */
4273 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004274 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004275
4276 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004277 return ret;
4278}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004279
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004280/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004281 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004282 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004283 * Adjusts "end_leaderp" until it is at "start_leader".
4284 */
4285 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004286eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004287 typval_T *rettv,
4288 int numeric_only,
4289 char_u *start_leader,
4290 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004291{
4292 char_u *end_leader = *end_leaderp;
4293 int ret = OK;
4294 int error = FALSE;
4295 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004296 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004297 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004298 float_T f = 0.0;
4299
4300 if (rettv->v_type == VAR_FLOAT)
4301 f = rettv->vval.v_float;
4302 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004303 {
4304 while (VIM_ISWHITE(end_leader[-1]))
4305 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004306 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004307 val = tv2bool(rettv);
4308 else
4309 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004310 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004311 if (error)
4312 {
4313 clear_tv(rettv);
4314 ret = FAIL;
4315 }
4316 else
4317 {
4318 while (end_leader > start_leader)
4319 {
4320 --end_leader;
4321 if (*end_leader == '!')
4322 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004323 if (numeric_only)
4324 {
4325 ++end_leader;
4326 break;
4327 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004328 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004329 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004330 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004331 {
4332 rettv->v_type = VAR_BOOL;
4333 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4334 }
4335 else
4336 f = !f;
4337 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004338 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004339 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004340 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004341 type = VAR_BOOL;
4342 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004343 }
4344 else if (*end_leader == '-')
4345 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004346 if (rettv->v_type == VAR_FLOAT)
4347 f = -f;
4348 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004349 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004350 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004351 type = VAR_NUMBER;
4352 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004353 }
4354 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004355 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004358 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 else
4361 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004362 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004363 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004364 rettv->v_type = type;
4365 else
4366 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004367 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004370 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 return ret;
4372}
4373
4374/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004375 * Call the function referred to in "rettv".
4376 */
4377 static int
4378call_func_rettv(
4379 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004380 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004381 typval_T *rettv,
4382 int evaluate,
4383 dict_T *selfdict,
4384 typval_T *basetv)
4385{
4386 partial_T *pt = NULL;
4387 funcexe_T funcexe;
4388 typval_T functv;
4389 char_u *s;
4390 int ret;
4391
4392 // need to copy the funcref so that we can clear rettv
4393 if (evaluate)
4394 {
4395 functv = *rettv;
4396 rettv->v_type = VAR_UNKNOWN;
4397
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004398 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004399 if (functv.v_type == VAR_PARTIAL)
4400 {
4401 pt = functv.vval.v_partial;
4402 s = partial_name(pt);
4403 }
4404 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004405 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004406 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004407 if (s == NULL || *s == NUL)
4408 {
4409 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004410 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004411 goto theend;
4412 }
4413 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004414 }
4415 else
4416 s = (char_u *)"";
4417
Bram Moolenaara80faa82020-04-12 19:37:17 +02004418 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004419 funcexe.fe_firstline = curwin->w_cursor.lnum;
4420 funcexe.fe_lastline = curwin->w_cursor.lnum;
4421 funcexe.fe_evaluate = evaluate;
4422 funcexe.fe_partial = pt;
4423 funcexe.fe_selfdict = selfdict;
4424 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004425 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004426
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004427theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004428 // Clear the funcref afterwards, so that deleting it while
4429 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004430 if (evaluate)
4431 clear_tv(&functv);
4432
4433 return ret;
4434}
4435
4436/*
4437 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004438 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004439 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4440 */
4441 static int
4442eval_lambda(
4443 char_u **arg,
4444 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004445 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004446 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004447{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004448 int evaluate = evalarg != NULL
4449 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004450 typval_T base = *rettv;
4451 int ret;
4452
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004453 rettv->v_type = VAR_UNKNOWN;
4454
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004455 if (**arg == '{')
4456 {
4457 // ->{lambda}()
4458 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4459 }
4460 else
4461 {
4462 // ->(lambda)()
4463 ++*arg;
4464 ret = eval1(arg, rettv, evalarg);
4465 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004466 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004467 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004468 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004469 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004470 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004471 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4472 && rettv->v_type != VAR_PARTIAL)
4473 {
4474 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4475 return FAIL;
4476 }
4477 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004478 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004479 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004480 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004481
4482 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004483 {
4484 if (verbose)
4485 {
4486 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004487 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004488 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004489 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004490 }
4491 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004492 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004493 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004494 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004495 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004496
4497 // Clear the funcref afterwards, so that deleting it while
4498 // evaluating the arguments is possible (see test55).
4499 if (evaluate)
4500 clear_tv(&base);
4501
4502 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004503}
4504
4505/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004506 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004507 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004508 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4509 */
4510 static int
4511eval_method(
4512 char_u **arg,
4513 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004514 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004515 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004516{
4517 char_u *name;
4518 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004519 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004520 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004521 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004522 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004523 int evaluate = evalarg != NULL
4524 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004525
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004526 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004527
Bram Moolenaarac92e252019-08-03 21:58:38 +02004528 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004529 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004530 if (alias != NULL)
4531 name = alias;
4532
4533 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004534 {
4535 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004536 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004537 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004538 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004539 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004540 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004541 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004542
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004543 // If there is no "(" immediately following, but there is further on,
4544 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4545 // Does not handle anything where "(" is part of the expression.
4546 *arg = skipwhite(*arg);
4547
4548 if (**arg != '(' && alias == NULL
4549 && (paren = vim_strchr(*arg, '(')) != NULL)
4550 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004551 *arg = name;
Bram Moolenaar34820942022-12-19 20:28:38 +00004552
4553 // Truncate the name a the "(". Avoid trying to get another line
4554 // by making "getline" NULL.
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004555 *paren = NUL;
Bram Moolenaar34820942022-12-19 20:28:38 +00004556 char_u *(*getline)(int, void *, int, getline_opt_T) = NULL;
4557 if (evalarg != NULL)
4558 {
4559 getline = evalarg->eval_getline;
4560 evalarg->eval_getline = NULL;
4561 }
4562
4563 char_u *deref = deref_function_name(arg, &tofree, evalarg, verbose);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004564 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004565 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004566 *arg = name + len;
4567 ret = FAIL;
4568 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004569 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004570 {
4571 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004572 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004573 }
Bram Moolenaar34820942022-12-19 20:28:38 +00004574
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004575 *paren = '(';
Bram Moolenaar34820942022-12-19 20:28:38 +00004576 if (getline != NULL)
4577 evalarg->eval_getline = getline;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004578 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004579
4580 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004581 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004582 *arg = skipwhite(*arg);
4583
4584 if (**arg != '(')
4585 {
4586 if (verbose)
4587 semsg(_(e_missing_parenthesis_str), name);
4588 ret = FAIL;
4589 }
4590 else if (VIM_ISWHITE((*arg)[-1]))
4591 {
4592 if (verbose)
4593 emsg(_(e_no_white_space_allowed_before_parenthesis));
4594 ret = FAIL;
4595 }
4596 else
4597 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004598 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004599 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004600 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004601
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004602 // Clear the funcref afterwards, so that deleting it while
4603 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004604 if (evaluate)
4605 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004606 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004607
Bram Moolenaarac92e252019-08-03 21:58:38 +02004608 return ret;
4609}
4610
4611/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004612 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4613 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4615 */
4616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004617eval_index(
4618 char_u **arg,
4619 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004620 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004621 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004622{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004623 int evaluate = evalarg != NULL
4624 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004625 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004626 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004627 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004628 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004629 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004630 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004631
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004632 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4633 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004634
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004635 init_tv(&var1);
4636 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004637 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004639 /*
4640 * dict.name
4641 */
4642 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004643 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004644 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004645 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004646 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004647 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 }
4649 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004650 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004651 /*
4652 * something[idx]
4653 *
4654 * Get the (first) variable from inside the [].
4655 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004656 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004657 if (**arg == ':')
4658 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004659 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004660 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004661 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004662 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004663 semsg(_(e_white_space_required_before_and_after_str_at_str),
4664 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004665 clear_tv(&var1);
4666 return FAIL;
4667 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004668 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004669 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004670 int error = FALSE;
4671
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004672 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004673 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004674 && var1.v_type == VAR_FLOAT)
4675 {
4676 var1.vval.v_string = typval_tostring(&var1, TRUE);
4677 var1.v_type = VAR_STRING;
4678 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004679
Bram Moolenaar4525a572022-02-13 11:57:33 +00004680 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004681 tv_get_number_chk(&var1, &error);
4682 else
4683 error = tv_get_string_chk(&var1) == NULL;
4684 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004685 {
4686 // not a number or string
4687 clear_tv(&var1);
4688 return FAIL;
4689 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004691
4692 /*
4693 * Get the second variable from inside the [:].
4694 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004695 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004696 if (**arg == ':')
4697 {
4698 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004699 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004700 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004701 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004702 semsg(_(e_white_space_required_before_and_after_str_at_str),
4703 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004704 if (!empty1)
4705 clear_tv(&var1);
4706 return FAIL;
4707 }
4708 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004709 if (**arg == ']')
4710 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004711 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004713 if (!empty1)
4714 clear_tv(&var1);
4715 return FAIL;
4716 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004717 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004718 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004719 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004720 if (!empty1)
4721 clear_tv(&var1);
4722 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004723 return FAIL;
4724 }
4725 }
4726
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004727 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004728 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004729 if (**arg != ']')
4730 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004731 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004732 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004733 clear_tv(&var1);
4734 if (range)
4735 clear_tv(&var2);
4736 return FAIL;
4737 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004738 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 }
4740
4741 if (evaluate)
4742 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004743 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004744 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004745 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004746
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004747 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004749 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004750 clear_tv(&var2);
4751 return res;
4752 }
4753 return OK;
4754}
4755
4756/*
4757 * Check if "rettv" can have an [index] or [sli:ce]
4758 */
4759 int
4760check_can_index(typval_T *rettv, int evaluate, int verbose)
4761{
4762 switch (rettv->v_type)
4763 {
4764 case VAR_FUNC:
4765 case VAR_PARTIAL:
4766 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004767 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004768 return FAIL;
4769 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004770 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004771 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004772 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 case VAR_BOOL:
4774 case VAR_SPECIAL:
4775 case VAR_JOB:
4776 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004777 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004778 case VAR_CLASS:
4779 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004780 if (verbose)
4781 emsg(_(e_cannot_index_special_variable));
4782 return FAIL;
4783 case VAR_UNKNOWN:
4784 case VAR_ANY:
4785 case VAR_VOID:
4786 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004787 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004788 emsg(_(e_cannot_index_special_variable));
4789 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004790 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004791 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004792
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004793 case VAR_STRING:
4794 case VAR_LIST:
4795 case VAR_DICT:
4796 case VAR_BLOB:
4797 break;
4798 case VAR_NUMBER:
4799 if (in_vim9script())
4800 emsg(_(e_cannot_index_number));
4801 break;
4802 }
4803 return OK;
4804}
4805
4806/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004807 * slice() function
4808 */
4809 void
4810f_slice(typval_T *argvars, typval_T *rettv)
4811{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004812 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004813 && ((argvars[0].v_type != VAR_STRING
4814 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004815 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004816 && check_for_list_arg(argvars, 0) == FAIL)
4817 || check_for_number_arg(argvars, 1) == FAIL
4818 || check_for_opt_number_arg(argvars, 2) == FAIL))
4819 return;
4820
Bram Moolenaar6601b622021-01-13 21:47:15 +01004821 if (check_can_index(argvars, TRUE, FALSE) == OK)
4822 {
4823 copy_tv(argvars, rettv);
4824 eval_index_inner(rettv, TRUE, argvars + 1,
4825 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4826 TRUE, NULL, 0, FALSE);
4827 }
4828}
4829
4830/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004831 * Apply index or range to "rettv".
4832 * "var1" is the first index, NULL for [:expr].
4833 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004834 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4835 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004836 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4837 */
4838 int
4839eval_index_inner(
4840 typval_T *rettv,
4841 int is_range,
4842 typval_T *var1,
4843 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004844 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004845 char_u *key,
4846 int keylen,
4847 int verbose)
4848{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004849 varnumber_T n1, n2 = 0;
4850 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004851
4852 n1 = 0;
4853 if (var1 != NULL && rettv->v_type != VAR_DICT)
4854 n1 = tv_get_number(var1);
4855
4856 if (is_range)
4857 {
4858 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004859 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004860 if (verbose)
4861 emsg(_(e_cannot_slice_dictionary));
4862 return FAIL;
4863 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004864 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004865 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004866 else
4867 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004868 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004869
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004870 switch (rettv->v_type)
4871 {
4872 case VAR_UNKNOWN:
4873 case VAR_ANY:
4874 case VAR_VOID:
4875 case VAR_FUNC:
4876 case VAR_PARTIAL:
4877 case VAR_FLOAT:
4878 case VAR_BOOL:
4879 case VAR_SPECIAL:
4880 case VAR_JOB:
4881 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004882 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004883 case VAR_CLASS:
4884 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004885 break; // not evaluating, skipping over subscript
4886
4887 case VAR_NUMBER:
4888 case VAR_STRING:
4889 {
4890 char_u *s = tv_get_string(rettv);
4891
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004892 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004893 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004894 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004895 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004896 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004897 else
4898 s = char_from_string(s, n1);
4899 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004900 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004901 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004902 // The resulting variable is a substring. If the indexes
4903 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004904 if (n1 < 0)
4905 {
4906 n1 = len + n1;
4907 if (n1 < 0)
4908 n1 = 0;
4909 }
4910 if (n2 < 0)
4911 n2 = len + n2;
4912 else if (n2 >= len)
4913 n2 = len;
4914 if (n1 >= len || n2 < 0 || n1 > n2)
4915 s = NULL;
4916 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004917 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004918 }
4919 else
4920 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004921 // The resulting variable is a string of a single
4922 // character. If the index is too big or negative the
4923 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004924 if (n1 >= len || n1 < 0)
4925 s = NULL;
4926 else
4927 s = vim_strnsave(s + n1, 1);
4928 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 clear_tv(rettv);
4930 rettv->v_type = VAR_STRING;
4931 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004932 }
4933 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004934
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004935 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004936 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4937 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004938 break;
4939
4940 case VAR_LIST:
4941 if (var1 == NULL)
4942 n1 = 0;
4943 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004944 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004945 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004946 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004947 return FAIL;
4948 break;
4949
4950 case VAR_DICT:
4951 {
4952 dictitem_T *item;
4953 typval_T tmp;
4954
4955 if (key == NULL)
4956 {
4957 key = tv_get_string_chk(var1);
4958 if (key == NULL)
4959 return FAIL;
4960 }
4961
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004962 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004963
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004964 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004965 {
4966 if (verbose)
4967 {
4968 if (keylen > 0)
4969 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004970 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004971 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004972 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004973 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004974
4975 copy_tv(&item->di_tv, &tmp);
4976 clear_tv(rettv);
4977 *rettv = tmp;
4978 }
4979 break;
4980 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004981 return OK;
4982}
4983
4984/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004985 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004986 */
4987 char_u *
4988partial_name(partial_T *pt)
4989{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004990 if (pt != NULL)
4991 {
4992 if (pt->pt_name != NULL)
4993 return pt->pt_name;
4994 if (pt->pt_func != NULL)
4995 return pt->pt_func->uf_name;
4996 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004997 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004998}
4999
Bram Moolenaarddecc252016-04-06 22:59:37 +02005000 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005001partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005002{
5003 int i;
5004
5005 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005006 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005007 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005008 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005009 if (pt->pt_name != NULL)
5010 {
5011 func_unref(pt->pt_name);
5012 vim_free(pt->pt_name);
5013 }
5014 else
5015 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005016
Bram Moolenaar54656012021-06-09 20:50:46 +02005017 // "out_up" is no longer used, decrement refcount on partial that owns it.
5018 partial_unref(pt->pt_outer.out_up_partial);
5019
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00005020 // Using pt_outer from another partial.
5021 partial_unref(pt->pt_outer_partial);
5022
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005023 // Decrease the reference count for the context of a closure. If down
5024 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005025 if (pt->pt_funcstack != NULL)
5026 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005027 --pt->pt_funcstack->fs_refcount;
5028 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005029 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005030 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005031 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5032 if (pt->pt_loopvars[i] != NULL)
5033 {
5034 --pt->pt_loopvars[i]->lvs_refcount;
5035 loopvars_check_refcount(pt->pt_loopvars[i]);
5036 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005037
Bram Moolenaarddecc252016-04-06 22:59:37 +02005038 vim_free(pt);
5039}
5040
5041/*
5042 * Unreference a closure: decrement the reference count and free it when it
5043 * becomes zero.
5044 */
5045 void
5046partial_unref(partial_T *pt)
5047{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005048 if (pt != NULL)
5049 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005050 int done = FALSE;
5051
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005052 if (--pt->pt_refcount <= 0)
5053 partial_free(pt);
5054
5055 // If the reference count goes down to one, the funcstack may be the
5056 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005057 else if (pt->pt_refcount == 1)
5058 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005059 // careful: if the funcstack is freed it may contain this partial
5060 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005061 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005062 done = funcstack_check_refcount(pt->pt_funcstack);
5063
Bram Moolenaarcc341812022-09-19 15:54:34 +01005064 if (!done)
5065 {
5066 int depth;
5067
5068 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5069 if (pt->pt_loopvars[depth] != NULL
5070 && loopvars_check_refcount(pt->pt_loopvars[depth]))
5071 break;
5072 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005073 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005074 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005075}
5076
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005077/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005078 * Return the next (unique) copy ID.
5079 * Used for serializing nested structures.
5080 */
5081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005082get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005083{
5084 current_copyID += COPYID_INC;
5085 return current_copyID;
5086}
5087
5088/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005089 * Garbage collection for lists and dictionaries.
5090 *
5091 * We use reference counts to be able to free most items right away when they
5092 * are no longer used. But for composite items it's possible that it becomes
5093 * unused while the reference count is > 0: When there is a recursive
5094 * reference. Example:
5095 * :let l = [1, 2, 3]
5096 * :let d = {9: l}
5097 * :let l[1] = d
5098 *
5099 * Since this is quite unusual we handle this with garbage collection: every
5100 * once in a while find out which lists and dicts are not referenced from any
5101 * variable.
5102 *
5103 * Here is a good reference text about garbage collection (refers to Python
5104 * but it applies to all reference-counting mechanisms):
5105 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005106 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005107
5108/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005109 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005110 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005111 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005112 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005113 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005114garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005115{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005116 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005117 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005118 buf_T *buf;
5119 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005120 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005121 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005122
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005123 if (!testing)
5124 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005125 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005126 want_garbage_collect = FALSE;
5127 may_garbage_collect = FALSE;
5128 garbage_collect_at_exit = FALSE;
5129 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005130
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005131 // The execution stack can grow big, limit the size.
5132 if (exestack.ga_maxlen - exestack.ga_len > 500)
5133 {
5134 size_t new_len;
5135 char_u *pp;
5136 int n;
5137
5138 // Keep 150% of the current size, with a minimum of the growth size.
5139 n = exestack.ga_len / 2;
5140 if (n < exestack.ga_growsize)
5141 n = exestack.ga_growsize;
5142
5143 // Don't make it bigger though.
5144 if (exestack.ga_len + n < exestack.ga_maxlen)
5145 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005146 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005147 pp = vim_realloc(exestack.ga_data, new_len);
5148 if (pp == NULL)
5149 return FAIL;
5150 exestack.ga_maxlen = exestack.ga_len + n;
5151 exestack.ga_data = pp;
5152 }
5153 }
5154
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005155 // We advance by two because we add one for items referenced through
5156 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005157 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005158
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005159 /*
5160 * 1. Go through all accessible variables and mark all lists and dicts
5161 * with copyID.
5162 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005163
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005164 // Don't free variables in the previous_funccal list unless they are only
5165 // referenced through previous_funccal. This must be first, because if
5166 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005167 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005168
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005169 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005170 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005172 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005173 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005174 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5175 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005176
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005177 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005178 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005179 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5180 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005181 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005182 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005183 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005184 abort = abort || set_ref_in_item(
5185 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005186#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005187 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005188 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5189 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005190 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005191 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005192 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5193 NULL, NULL);
5194#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005196 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005197 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005198 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5199 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005200 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005201 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005203 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005204 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005206 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005207 abort = abort || set_ref_in_functions(copyID);
5208
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005209 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005210 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005211
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005212 // funcstacks keep variables for closures
5213 abort = abort || set_ref_in_funcstacks(copyID);
5214
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005215 // loopvars keep variables for loop blocks
5216 abort = abort || set_ref_in_loopvars(copyID);
5217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005219 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005220
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005221 // callbacks in buffers
5222 abort = abort || set_ref_in_buffers(copyID);
5223
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005224 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5225 abort = abort || set_ref_in_insexpand_funcs(copyID);
5226
5227 // 'operatorfunc' callback
5228 abort = abort || set_ref_in_opfunc(copyID);
5229
5230 // 'tagfunc' callback
5231 abort = abort || set_ref_in_tagfunc(copyID);
5232
5233 // 'imactivatefunc' and 'imstatusfunc' callbacks
5234 abort = abort || set_ref_in_im_funcs(copyID);
5235
Bram Moolenaar1dced572012-04-05 16:54:08 +02005236#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005237 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005238#endif
5239
Bram Moolenaardb913952012-06-29 12:54:53 +02005240#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005241 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005242#endif
5243
5244#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005245 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005246#endif
5247
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005248#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005249 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005250 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005251#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005252#ifdef FEAT_NETBEANS_INTG
5253 abort = abort || set_ref_in_nb_channel(copyID);
5254#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005255
Bram Moolenaare3188e22016-05-31 21:13:04 +02005256#ifdef FEAT_TIMERS
5257 abort = abort || set_ref_in_timer(copyID);
5258#endif
5259
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005260#ifdef FEAT_QUICKFIX
5261 abort = abort || set_ref_in_quickfix(copyID);
5262#endif
5263
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005264#ifdef FEAT_TERMINAL
5265 abort = abort || set_ref_in_term(copyID);
5266#endif
5267
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005268#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005269 abort = abort || set_ref_in_popups(copyID);
5270#endif
5271
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005272 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005273 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005274 /*
5275 * 2. Free lists and dictionaries that are not referenced.
5276 */
5277 did_free = free_unref_items(copyID);
5278
5279 /*
5280 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005281 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005282 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005283 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005284 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005285 else if (p_verbose > 0)
5286 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005287 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005288 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005289
5290 return did_free;
5291}
5292
5293/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005294 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005295 */
5296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005297free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005298{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005299 int did_free = FALSE;
5300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005301 // Let all "free" functions know that we are here. This means no
5302 // dictionaries, lists, channels or jobs are to be freed, because we will
5303 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005304 in_free_unref_items = TRUE;
5305
5306 /*
5307 * PASS 1: free the contents of the items. We don't free the items
5308 * themselves yet, so that it is possible to decrement refcount counters
5309 */
5310
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005311 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005312 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005313
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005314 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005315 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005316
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005317 // Go through the list of objects and free items without this copyID.
5318 did_free |= object_free_nonref(copyID);
5319
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005320#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005321 // Go through the list of jobs and free items without the copyID. This
5322 // must happen before doing channels, because jobs refer to channels, but
5323 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005324 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5325
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005326 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005327 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5328#endif
5329
5330 /*
5331 * PASS 2: free the items themselves.
5332 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005333 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005334 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005335
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005336#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005337 // Go through the list of jobs and free items without the copyID. This
5338 // must happen before doing channels, because jobs refer to channels, but
5339 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005340 free_unused_jobs(copyID, COPYID_MASK);
5341
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005342 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005343 free_unused_channels(copyID, COPYID_MASK);
5344#endif
5345
5346 in_free_unref_items = FALSE;
5347
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005348 return did_free;
5349}
5350
5351/*
5352 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005353 * "list_stack" is used to add lists to be marked. Can be NULL.
5354 *
5355 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005356 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005358set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005359{
5360 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005361 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005362 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005363 hashtab_T *cur_ht;
5364 ht_stack_T *ht_stack = NULL;
5365 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005366
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005367 cur_ht = ht;
5368 for (;;)
5369 {
5370 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005371 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005372 // Mark each item in the hashtab. If the item contains a hashtab
5373 // it is added to ht_stack, if it contains a list it is added to
5374 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005375 todo = (int)cur_ht->ht_used;
5376 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5377 if (!HASHITEM_EMPTY(hi))
5378 {
5379 --todo;
5380 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5381 &ht_stack, list_stack);
5382 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005383 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005384
5385 if (ht_stack == NULL)
5386 break;
5387
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005388 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 cur_ht = ht_stack->ht;
5390 tempitem = ht_stack;
5391 ht_stack = ht_stack->prev;
5392 free(tempitem);
5393 }
5394
5395 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005396}
5397
Dominique Pelle748b3082022-01-08 12:41:16 +00005398#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5399 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005400/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005401 * Mark a dict and its items with "copyID".
5402 * Returns TRUE if setting references failed somehow.
5403 */
5404 int
5405set_ref_in_dict(dict_T *d, int copyID)
5406{
5407 if (d != NULL && d->dv_copyID != copyID)
5408 {
5409 d->dv_copyID = copyID;
5410 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5411 }
5412 return FALSE;
5413}
Dominique Pelle748b3082022-01-08 12:41:16 +00005414#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005415
5416/*
5417 * Mark a list and its items with "copyID".
5418 * Returns TRUE if setting references failed somehow.
5419 */
5420 int
5421set_ref_in_list(list_T *ll, int copyID)
5422{
5423 if (ll != NULL && ll->lv_copyID != copyID)
5424 {
5425 ll->lv_copyID = copyID;
5426 return set_ref_in_list_items(ll, copyID, NULL);
5427 }
5428 return FALSE;
5429}
5430
5431/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005432 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005433 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5434 *
5435 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005436 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005437 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005438set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005439{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005440 listitem_T *li;
5441 int abort = FALSE;
5442 list_T *cur_l;
5443 list_stack_T *list_stack = NULL;
5444 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005446 cur_l = l;
5447 for (;;)
5448 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005449 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005450 // Mark each item in the list. If the item contains a hashtab
5451 // it is added to ht_stack, if it contains a list it is added to
5452 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005453 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5454 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5455 ht_stack, &list_stack);
5456 if (list_stack == NULL)
5457 break;
5458
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005459 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005460 cur_l = list_stack->list;
5461 tempitem = list_stack;
5462 list_stack = list_stack->prev;
5463 free(tempitem);
5464 }
5465
5466 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005467}
5468
5469/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005470 * Mark the partial in callback 'cb' with "copyID".
5471 */
5472 int
5473set_ref_in_callback(callback_T *cb, int copyID)
5474{
5475 typval_T tv;
5476
5477 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5478 return FALSE;
5479
5480 tv.v_type = VAR_PARTIAL;
5481 tv.vval.v_partial = cb->cb_partial;
5482 return set_ref_in_item(&tv, copyID, NULL, NULL);
5483}
5484
5485/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005486 * Mark all lists, dicts and other container types referenced through typval
5487 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005488 * "list_stack" is used to add lists to be marked. Can be NULL.
5489 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5490 *
5491 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005492 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005494set_ref_in_item(
5495 typval_T *tv,
5496 int copyID,
5497 ht_stack_T **ht_stack,
5498 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005499{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005500 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005501
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005502 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005503 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005504 case VAR_DICT:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005505 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005506 dict_T *dd = tv->vval.v_dict;
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005507
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005508 if (dd != NULL && dd->dv_copyID != copyID)
5509 {
5510 // Didn't see this dict yet.
5511 dd->dv_copyID = copyID;
5512 if (ht_stack == NULL)
5513 {
5514 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5515 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005516 else
5517 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005518 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005519
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005520 if (newitem == NULL)
5521 abort = TRUE;
5522 else
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005523 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005524 newitem->ht = &dd->dv_hashtab;
5525 newitem->prev = *ht_stack;
5526 *ht_stack = newitem;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005527 }
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005528 }
5529 }
5530 break;
5531 }
5532
5533 case VAR_LIST:
5534 {
5535 list_T *ll = tv->vval.v_list;
5536
5537 if (ll != NULL && ll->lv_copyID != copyID)
5538 {
5539 // Didn't see this list yet.
5540 ll->lv_copyID = copyID;
5541 if (list_stack == NULL)
5542 {
5543 abort = set_ref_in_list_items(ll, copyID, ht_stack);
5544 }
5545 else
5546 {
5547 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5548
5549 if (newitem == NULL)
5550 abort = TRUE;
5551 else
5552 {
5553 newitem->list = ll;
5554 newitem->prev = *list_stack;
5555 *list_stack = newitem;
5556 }
5557 }
5558 }
5559 break;
5560 }
5561
5562 case VAR_FUNC:
5563 {
5564 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5565 break;
5566 }
5567
5568 case VAR_PARTIAL:
5569 {
5570 partial_T *pt = tv->vval.v_partial;
5571 int i;
5572
5573 if (pt != NULL && pt->pt_copyID != copyID)
5574 {
5575 // Didn't see this partial yet.
5576 pt->pt_copyID = copyID;
5577
5578 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5579
5580 if (pt->pt_dict != NULL)
5581 {
5582 typval_T dtv;
5583
5584 dtv.v_type = VAR_DICT;
5585 dtv.vval.v_dict = pt->pt_dict;
5586 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5587 }
5588
5589 for (i = 0; i < pt->pt_argc; ++i)
5590 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5591 ht_stack, list_stack);
5592 // pt_funcstack is handled in set_ref_in_funcstacks()
5593 // pt_loopvars is handled in set_ref_in_loopvars()
5594 }
5595 break;
5596 }
5597
5598 case VAR_JOB:
5599 {
5600#ifdef FEAT_JOB_CHANNEL
5601 job_T *job = tv->vval.v_job;
5602 typval_T dtv;
5603
5604 if (job != NULL && job->jv_copyID != copyID)
5605 {
5606 job->jv_copyID = copyID;
5607 if (job->jv_channel != NULL)
5608 {
5609 dtv.v_type = VAR_CHANNEL;
5610 dtv.vval.v_channel = job->jv_channel;
5611 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5612 }
5613 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005614 {
5615 dtv.v_type = VAR_PARTIAL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005616 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005617 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5618 }
5619 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005620#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005621 break;
5622 }
5623
5624 case VAR_CHANNEL:
5625 {
5626#ifdef FEAT_JOB_CHANNEL
5627 channel_T *ch = tv->vval.v_channel;
5628 ch_part_T part;
5629 typval_T dtv;
5630 jsonq_T *jq;
5631 cbq_T *cq;
5632
5633 if (ch != NULL && ch->ch_copyID != copyID)
5634 {
5635 ch->ch_copyID = copyID;
5636 for (part = PART_SOCK; part < PART_COUNT; ++part)
5637 {
5638 for (jq = ch->ch_part[part].ch_json_head.jq_next;
5639 jq != NULL; jq = jq->jq_next)
5640 set_ref_in_item(jq->jq_value, copyID,
5641 ht_stack, list_stack);
5642 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5643 cq = cq->cq_next)
5644 if (cq->cq_callback.cb_partial != NULL)
5645 {
5646 dtv.v_type = VAR_PARTIAL;
5647 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5648 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5649 }
5650 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5651 {
5652 dtv.v_type = VAR_PARTIAL;
5653 dtv.vval.v_partial =
5654 ch->ch_part[part].ch_callback.cb_partial;
5655 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5656 }
5657 }
5658 if (ch->ch_callback.cb_partial != NULL)
5659 {
5660 dtv.v_type = VAR_PARTIAL;
5661 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5662 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5663 }
5664 if (ch->ch_close_cb.cb_partial != NULL)
5665 {
5666 dtv.v_type = VAR_PARTIAL;
5667 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5668 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5669 }
5670 }
5671#endif
5672 break;
5673 }
5674
5675 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005676 // TODO: Mark methods in class_obj_methods ?
5677 // Mark initializer expressions?
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005678 break;
5679
5680 case VAR_OBJECT:
5681 {
5682 object_T *obj = tv->vval.v_object;
5683 if (obj != NULL && obj->obj_copyID != copyID)
5684 {
5685 obj->obj_copyID = copyID;
5686
5687 // The typval_T array is right after the object_T.
5688 typval_T *mtv = (typval_T *)(obj + 1);
5689 for (int i = 0; !abort
5690 && i < obj->obj_class->class_obj_member_count; ++i)
5691 abort = abort || set_ref_in_item(mtv + i, copyID,
5692 ht_stack, list_stack);
5693 }
5694 break;
5695 }
5696
5697 case VAR_UNKNOWN:
5698 case VAR_ANY:
5699 case VAR_VOID:
5700 case VAR_BOOL:
5701 case VAR_SPECIAL:
5702 case VAR_NUMBER:
5703 case VAR_FLOAT:
5704 case VAR_STRING:
5705 case VAR_BLOB:
5706 case VAR_INSTR:
5707 // Types that do not contain any other item
5708 break;
5709 }
5710
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005711 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005712}
5713
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005715 * Return a string with the string representation of a variable.
5716 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005717 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005718 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005719 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005720 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005721 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005722 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5723 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005724 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005725 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005726 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005727echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005728 typval_T *tv,
5729 char_u **tofree,
5730 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005731 int copyID,
5732 int echo_style,
5733 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005734 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005735{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005736 static int recurse = 0;
5737 char_u *r = NULL;
5738
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005740 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005741 if (!did_echo_string_emsg)
5742 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005743 // Only give this message once for a recursive call to avoid
5744 // flooding the user with errors. And stop iterating over lists
5745 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005746 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005747 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005748 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005749 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005750 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005751 }
5752 ++recurse;
5753
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005754 switch (tv->v_type)
5755 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005756 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005757 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005758 {
5759 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005760 r = tv->vval.v_string;
5761 if (r == NULL)
5762 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005763 }
5764 else
5765 {
5766 *tofree = string_quote(tv->vval.v_string, FALSE);
5767 r = *tofree;
5768 }
5769 break;
5770
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005771 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005772 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005773 char_u buf[MAX_FUNC_NAME_LEN];
5774
5775 if (echo_style)
5776 {
LemonBoya5d35902022-04-29 21:15:02 +01005777 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5778 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005779 buf, MAX_FUNC_NAME_LEN);
5780 if (r == buf)
5781 {
5782 r = vim_strsave(buf);
5783 *tofree = r;
5784 }
5785 else
5786 *tofree = NULL;
5787 }
5788 else
5789 {
5790 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5791 : make_ufunc_name_readable(
5792 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5793 TRUE);
5794 r = *tofree;
5795 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005796 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005797 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005798
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005799 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005800 {
5801 partial_T *pt = tv->vval.v_partial;
5802 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005803 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005804 garray_T ga;
5805 int i;
5806 char_u *tf;
5807
5808 ga_init2(&ga, 1, 100);
5809 ga_concat(&ga, (char_u *)"function(");
5810 if (fname != NULL)
5811 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005812 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005813 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005814 && vim_isupper(fname[1]))
5815 {
5816 ga_concat(&ga, (char_u *)"'g:");
5817 ga_concat(&ga, fname + 1);
5818 }
5819 else
5820 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005821 vim_free(fname);
5822 }
5823 if (pt != NULL && pt->pt_argc > 0)
5824 {
5825 ga_concat(&ga, (char_u *)", [");
5826 for (i = 0; i < pt->pt_argc; ++i)
5827 {
5828 if (i > 0)
5829 ga_concat(&ga, (char_u *)", ");
5830 ga_concat(&ga,
5831 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5832 vim_free(tf);
5833 }
5834 ga_concat(&ga, (char_u *)"]");
5835 }
5836 if (pt != NULL && pt->pt_dict != NULL)
5837 {
5838 typval_T dtv;
5839
5840 ga_concat(&ga, (char_u *)", ");
5841 dtv.v_type = VAR_DICT;
5842 dtv.vval.v_dict = pt->pt_dict;
5843 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5844 vim_free(tf);
5845 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005846 // terminate with ')' and a NUL
5847 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005848
5849 *tofree = ga.ga_data;
5850 r = *tofree;
5851 break;
5852 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005853
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005854 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005855 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005856 break;
5857
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005858 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005859 if (tv->vval.v_list == NULL)
5860 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005861 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005862 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005863 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005864 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005865 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5866 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005867 {
5868 *tofree = NULL;
5869 r = (char_u *)"[...]";
5870 }
5871 else
5872 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005873 int old_copyID = tv->vval.v_list->lv_copyID;
5874
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005875 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005876 *tofree = list2string(tv, copyID, restore_copyID);
5877 if (restore_copyID)
5878 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005879 r = *tofree;
5880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005881 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005882
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005884 if (tv->vval.v_dict == NULL)
5885 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005886 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005887 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005888 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005889 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005890 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5891 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005892 {
5893 *tofree = NULL;
5894 r = (char_u *)"{...}";
5895 }
5896 else
5897 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005898 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005899
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005900 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005901 *tofree = dict2string(tv, copyID, restore_copyID);
5902 if (restore_copyID)
5903 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005904 r = *tofree;
5905 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005906 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005907
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005908 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005909 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005910 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005911 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005912 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005913 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005914 break;
5915
Bram Moolenaar835dc632016-02-07 14:27:38 +01005916 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005917 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005918#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005919 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005920 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5921 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005922 if (composite_val)
5923 {
5924 *tofree = string_quote(r, FALSE);
5925 r = *tofree;
5926 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005927#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005929
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005930 case VAR_INSTR:
5931 *tofree = NULL;
5932 r = (char_u *)"instructions";
5933 break;
5934
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005935 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005936 {
5937 class_T *cl = tv->vval.v_class;
5938 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
5939 r = *tofree = alloc(len);
5940 vim_snprintf((char *)r, len, "class %s",
5941 cl == NULL ? "[unknown]" : (char *)cl->class_name);
5942 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005943 break;
5944
5945 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005946 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005947 garray_T ga;
5948 ga_init2(&ga, 1, 50);
5949 ga_concat(&ga, (char_u *)"object of ");
5950 object_T *obj = tv->vval.v_object;
5951 class_T *cl = obj == NULL ? NULL : obj->obj_class;
5952 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
5953 : cl->class_name);
5954 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005955 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005956 ga_concat(&ga, (char_u *)" {");
5957 for (int i = 0; i < cl->class_obj_member_count; ++i)
5958 {
5959 if (i > 0)
5960 ga_concat(&ga, (char_u *)", ");
Bram Moolenaard505d172022-12-18 21:42:55 +00005961 ocmember_T *m = &cl->class_obj_members[i];
5962 ga_concat(&ga, m->ocm_name);
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005963 ga_concat(&ga, (char_u *)": ");
5964 char_u *tf = NULL;
5965 ga_concat(&ga, echo_string_core(
5966 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005967 &tf, numbuf, copyID, echo_style,
5968 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005969 vim_free(tf);
5970 }
5971 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005972 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005973
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005974 *tofree = r = ga.ga_data;
5975 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005976 break;
5977
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005978 case VAR_FLOAT:
5979 *tofree = NULL;
5980 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5981 r = numbuf;
5982 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005983
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005984 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005985 case VAR_SPECIAL:
5986 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005987 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005988 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005989 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005990
Bram Moolenaar8502c702014-06-17 12:51:16 +02005991 if (--recurse == 0)
5992 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005993 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005994}
5995
5996/*
5997 * Return a string with the string representation of a variable.
5998 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5999 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006000 * Does not put quotes around strings, as ":echo" displays values.
6001 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6002 * May return NULL.
6003 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006004 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006005echo_string(
6006 typval_T *tv,
6007 char_u **tofree,
6008 char_u *numbuf,
6009 int copyID)
6010{
6011 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6012}
6013
6014/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006015 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
6016 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01006017 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006018 */
6019 int
6020buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
6021{
6022 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006023 char_u *t;
6024 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006025
6026 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6027 return -1;
6028
6029 if (lnum > buf->b_ml.ml_line_count)
6030 lnum = buf->b_ml.ml_line_count;
6031
6032 str = ml_get_buf(buf, lnum, FALSE);
6033 if (str == NULL)
6034 return -1;
6035
6036 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006037 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006038
Bram Moolenaar91458462021-01-13 20:08:38 +01006039 // count the number of characters
6040 t = str;
6041 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6042 t += mb_ptr2len(t);
6043
6044 // In insert mode, when the cursor is at the end of a non-empty line,
6045 // byteidx points to the NUL character immediately past the end of the
6046 // string. In this case, add one to the character count.
6047 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6048 count++;
6049
6050 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006051}
6052
6053/*
6054 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006055 * byte index. Works only for loaded buffers. Returns -1 on failure.
6056 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006057 */
6058 int
6059buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6060{
6061 char_u *str;
6062 char_u *t;
6063
6064 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6065 return -1;
6066
6067 if (lnum > buf->b_ml.ml_line_count)
6068 lnum = buf->b_ml.ml_line_count;
6069
6070 str = ml_get_buf(buf, lnum, FALSE);
6071 if (str == NULL)
6072 return -1;
6073
6074 // Convert the character offset to a byte offset
6075 t = str;
6076 while (*t != NUL && --charidx > 0)
6077 t += mb_ptr2len(t);
6078
Bram Moolenaar91458462021-01-13 20:08:38 +01006079 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006080}
6081
6082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006084 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006086 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087var2fpos(
6088 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006089 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006090 int *fnum, // set to fnum for '0, 'A, etc.
6091 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006093 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006095 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006097 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006098 if (varp->v_type == VAR_LIST)
6099 {
6100 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006101 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006102 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006103 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006104
6105 l = varp->vval.v_list;
6106 if (l == NULL)
6107 return NULL;
6108
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006109 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006110 pos.lnum = list_find_nr(l, 0L, &error);
6111 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006112 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006113 if (charcol)
6114 len = (long)mb_charlen(ml_get(pos.lnum));
6115 else
6116 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006117
Bram Moolenaarec65d772020-08-20 22:29:12 +02006118 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006120 li = list_find(l, 1L);
6121 if (li != NULL && li->li_tv.v_type == VAR_STRING
6122 && li->li_tv.vval.v_string != NULL
6123 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006124 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006125 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006126 }
6127 else
6128 {
6129 pos.col = list_find_nr(l, 1L, &error);
6130 if (error)
6131 return NULL;
6132 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006134 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006135 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006136 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006137 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006138
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006139 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006140 pos.coladd = list_find_nr(l, 2L, &error);
6141 if (error)
6142 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006143
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006144 return &pos;
6145 }
6146
Bram Moolenaarc5809432021-03-27 21:23:30 +01006147 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6148 return NULL;
6149
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006150 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006151 if (name == NULL)
6152 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006153
6154 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006155 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006156 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006157 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006158 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006159 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006160 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006161 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006162 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006163 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006164 pos = VIsual;
6165 else
6166 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006167 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006168 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006169 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006170 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006171 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006172 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006173 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6174 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006175 pos = *pp;
6176 }
6177 if (pos.lnum != 0)
6178 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006179 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006180 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6181 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006183
Bram Moolenaara5525202006-03-02 22:52:09 +00006184 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006185
Bram Moolenaar477933c2007-07-17 14:32:23 +00006186 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006187 {
6188 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006189 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006190 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006191 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006192 // In silent Ex mode topline is zero, but that's not a valid line
6193 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006194 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006195 return &pos;
6196 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006197 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006198 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006199 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006200 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006201 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006202 return &pos;
6203 }
6204 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006205 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006207 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
6209 pos.lnum = curbuf->b_ml.ml_line_count;
6210 pos.col = 0;
6211 }
6212 else
6213 {
6214 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006215 if (charcol)
6216 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6217 else
6218 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006219 }
6220 return &pos;
6221 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006222 if (in_vim9script())
6223 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 return NULL;
6225}
6226
6227/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006228 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006229 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006230 * Note that the column is passed on as-is, the caller may want to decrement
6231 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006232 * If "charcol" is TRUE use the column as the character index instead of the
6233 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006234 * Return FAIL when conversion is not possible, doesn't check the position for
6235 * validity.
6236 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006238list2fpos(
6239 typval_T *arg,
6240 pos_T *posp,
6241 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006242 colnr_T *curswantp,
6243 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006244{
6245 list_T *l = arg->vval.v_list;
6246 long i = 0;
6247 long n;
6248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006249 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6250 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006251 if (arg->v_type != VAR_LIST
6252 || l == NULL
6253 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006254 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006255 return FAIL;
6256
6257 if (fnump != NULL)
6258 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006259 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006260 if (n < 0)
6261 return FAIL;
6262 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006263 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006264 *fnump = n;
6265 }
6266
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006267 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006268 if (n < 0)
6269 return FAIL;
6270 posp->lnum = n;
6271
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006272 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006273 if (n < 0)
6274 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006275 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006276 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006277 if (charcol)
6278 {
6279 buf_T *buf;
6280
6281 // Get the text for the specified line in a loaded buffer
6282 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6283 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6284 return FAIL;
6285
Bram Moolenaar79f23442022-10-10 12:42:57 +01006286 n = buf_charidx_to_byteidx(buf,
6287 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006288 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006289 posp->col = n;
6290
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006291 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006292 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006293 posp->coladd = 0;
6294 else
6295 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006296
Bram Moolenaar493c1782014-05-28 14:34:46 +02006297 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006298 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006299
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006300 return OK;
6301}
6302
6303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006304 * Get the length of an environment variable name.
6305 * Advance "arg" to the first character after the name.
6306 * Return 0 for error.
6307 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006309get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006310{
6311 char_u *p;
6312 int len;
6313
6314 for (p = *arg; vim_isIDc(*p); ++p)
6315 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006316 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317 return 0;
6318
6319 len = (int)(p - *arg);
6320 *arg = p;
6321 return len;
6322}
6323
6324/*
6325 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006326 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 * Return 0 if something is wrong.
6328 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006329 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006330get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331{
6332 char_u *p;
6333 int len;
6334
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006335 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006337 {
6338 if (*p == ':')
6339 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006340 // "s:" is start of "s:var", but "n:" is not and can be used in
6341 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006342 len = (int)(p - *arg);
6343 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6344 || len > 1)
6345 break;
6346 }
6347 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006348 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 return 0;
6350
6351 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006352 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353
6354 return len;
6355}
6356
6357/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006358 * Get the length of the name of a variable or function.
6359 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006361 * Return -1 if curly braces expansion failed.
6362 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006363 * If the name contains 'magic' {}'s, expand them and return the
6364 * expanded name in an allocated string via 'alias' - caller must free.
6365 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006366 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006367get_name_len(
6368 char_u **arg,
6369 char_u **alias,
6370 int evaluate,
6371 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372{
6373 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 char_u *p;
6375 char_u *expr_start;
6376 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006378 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379
6380 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6381 && (*arg)[2] == (int)KE_SNR)
6382 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006383 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384 *arg += 3;
6385 return get_id_len(arg) + 3;
6386 }
6387 len = eval_fname_script(*arg);
6388 if (len > 0)
6389 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006390 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 *arg += len;
6392 }
6393
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006395 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006397 p = find_name_end(*arg, &expr_start, &expr_end,
6398 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 if (expr_start != NULL)
6400 {
6401 char_u *temp_string;
6402
6403 if (!evaluate)
6404 {
6405 len += (int)(p - *arg);
6406 *arg = skipwhite(p);
6407 return len;
6408 }
6409
6410 /*
6411 * Include any <SID> etc in the expanded string:
6412 * Thus the -len here.
6413 */
6414 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6415 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006416 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 *alias = temp_string;
6418 *arg = skipwhite(p);
6419 return (int)STRLEN(temp_string);
6420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006423 // Only give an error when there is something, otherwise it will be
6424 // reported at a higher level.
6425 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006426 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427
6428 return len;
6429}
6430
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006431/*
6432 * Find the end of a variable or function name, taking care of magic braces.
6433 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6434 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006435 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006436 * Return a pointer to just after the name. Equal to "arg" if there is no
6437 * valid name.
6438 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006439 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440find_name_end(
6441 char_u *arg,
6442 char_u **expr_start,
6443 char_u **expr_end,
6444 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006446 int mb_nest = 0;
6447 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006449 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006450 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006452 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006454 *expr_start = NULL;
6455 *expr_end = NULL;
6456 }
6457
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006458 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006459 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6460 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006461 return arg;
6462
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006463 for (p = arg; *p != NUL
6464 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006465 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006466 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006467 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006468 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006469 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006470 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006471 if (*p == '\'')
6472 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006473 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006474 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006475 ;
6476 if (*p == NUL)
6477 break;
6478 }
6479 else if (*p == '"')
6480 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006481 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006482 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006483 if (*p == '\\' && p[1] != NUL)
6484 ++p;
6485 if (*p == NUL)
6486 break;
6487 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006488 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6489 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006490 // "s:" is start of "s:var", but "n:" is not and can be used in
6491 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006492 len = (int)(p - arg);
6493 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006494 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006495 break;
6496 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006498 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006500 if (*p == '[')
6501 ++br_nest;
6502 else if (*p == ']')
6503 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006505
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006506 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006508 if (*p == '{')
6509 {
6510 mb_nest++;
6511 if (expr_start != NULL && *expr_start == NULL)
6512 *expr_start = p;
6513 }
6514 else if (*p == '}')
6515 {
6516 mb_nest--;
6517 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6518 *expr_end = p;
6519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 }
6522
6523 return p;
6524}
6525
6526/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006527 * Expands out the 'magic' {}'s in a variable/function name.
6528 * Note that this can call itself recursively, to deal with
6529 * constructs like foo{bar}{baz}{bam}
6530 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6531 * "in_start" ^
6532 * "expr_start" ^
6533 * "expr_end" ^
6534 * "in_end" ^
6535 *
6536 * Returns a new allocated string, which the caller must free.
6537 * Returns NULL for failure.
6538 */
6539 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006540make_expanded_name(
6541 char_u *in_start,
6542 char_u *expr_start,
6543 char_u *expr_end,
6544 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006545{
6546 char_u c1;
6547 char_u *retval = NULL;
6548 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006549
6550 if (expr_end == NULL || in_end == NULL)
6551 return NULL;
6552 *expr_start = NUL;
6553 *expr_end = NUL;
6554 c1 = *in_end;
6555 *in_end = NUL;
6556
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006557 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006558 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006559 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006560 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6561 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006562 if (retval != NULL)
6563 {
6564 STRCPY(retval, in_start);
6565 STRCAT(retval, temp_result);
6566 STRCAT(retval, expr_end + 1);
6567 }
6568 }
6569 vim_free(temp_result);
6570
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006571 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006572 *expr_start = '{';
6573 *expr_end = '}';
6574
6575 if (retval != NULL)
6576 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006577 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006578 if (expr_start != NULL)
6579 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006580 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006581 temp_result = make_expanded_name(retval, expr_start,
6582 expr_end, temp_result);
6583 vim_free(retval);
6584 retval = temp_result;
6585 }
6586 }
6587
6588 return retval;
6589}
6590
6591/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006595 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006596eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006598 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006599}
6600
6601/*
6602 * Return TRUE if character "c" can be used as the first character in a
6603 * variable or function name (excluding '{' and '}').
6604 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006605 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006606eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006607{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006608 return ASCII_ISALPHA(c) || c == '_';
6609}
6610
6611/*
6612 * Return TRUE if character "c" can be used as the first character of a
6613 * dictionary key.
6614 */
6615 int
6616eval_isdictc(int c)
6617{
6618 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619}
6620
6621/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006622 * Handle:
6623 * - expr[expr], expr[expr:expr] subscript
6624 * - ".name" lookup
6625 * - function call with Funcref variable: func(expr)
6626 * - method call: var->method()
6627 *
6628 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006629 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006630 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006631 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006632handle_subscript(
6633 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006634 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006635 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006636 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006637 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006638{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006639 int evaluate = evalarg != NULL
6640 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006641 int ret = OK;
6642 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006643 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006644 int getnext;
6645 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006646
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006647 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006648 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006649 // When at the end of the line and ".name" or "->{" or "->X" follows in
6650 // the next line then consume the line break.
6651 p = eval_next_non_blank(*arg, evalarg, &getnext);
6652 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006653 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006654 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6655 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6656 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006657 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006658 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006659 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006660 check_white = FALSE;
6661 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006662
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006663 if (rettv->v_type == VAR_ANY)
6664 {
6665 char_u *exp_name;
6666 int cc;
6667 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006668 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006669 type_T *type;
6670
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006671 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006672 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006673 if (**arg != '.')
6674 {
6675 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006676 semsg(_(e_expected_dot_after_name_str),
6677 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006678 ret = FAIL;
6679 break;
6680 }
6681 ++*arg;
6682 if (IS_WHITE_OR_NUL(**arg))
6683 {
6684 if (verbose)
6685 emsg(_(e_no_white_space_allowed_after_dot));
6686 ret = FAIL;
6687 break;
6688 }
6689
6690 // isolate the name
6691 exp_name = *arg;
6692 while (eval_isnamec(**arg))
6693 ++*arg;
6694 cc = **arg;
6695 **arg = NUL;
6696
6697 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006698 evalarg == NULL ? NULL : evalarg->eval_cctx,
6699 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006700 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006701
6702 if (idx < 0 && ufunc == NULL)
6703 {
6704 ret = FAIL;
6705 break;
6706 }
6707 if (idx >= 0)
6708 {
6709 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6710 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6711
6712 copy_tv(sv->sv_tv, rettv);
6713 }
6714 else
6715 {
6716 rettv->v_type = VAR_FUNC;
6717 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6718 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006719 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006720 }
6721
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006722 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6723 || rettv->v_type == VAR_PARTIAL))
6724 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006725 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006726 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6727 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006728
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006729 // Stop the expression evaluation when immediately aborting on
6730 // error, or when an interrupt occurred or an exception was thrown
6731 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006732 if (aborting())
6733 {
6734 if (ret == OK)
6735 clear_tv(rettv);
6736 ret = FAIL;
6737 }
6738 dict_unref(selfdict);
6739 selfdict = NULL;
6740 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006741 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006742 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006743 if (in_vim9script())
6744 *arg = skipwhite(p + 2);
6745 else
6746 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006747 if (ret == OK)
6748 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006749 if (VIM_ISWHITE(**arg))
6750 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006751 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006752 ret = FAIL;
6753 }
6754 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006755 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006756 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006757 else
6758 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006759 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006760 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006761 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006762 // "." is ".name" lookup when we found a dict or when evaluating and
6763 // scriptversion is at least 2, where string concatenation is "..".
6764 else if (**arg == '['
6765 || (**arg == '.' && (rettv->v_type == VAR_DICT
6766 || (!evaluate
6767 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006768 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006769 {
6770 dict_unref(selfdict);
6771 if (rettv->v_type == VAR_DICT)
6772 {
6773 selfdict = rettv->vval.v_dict;
6774 if (selfdict != NULL)
6775 ++selfdict->dv_refcount;
6776 }
6777 else
6778 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006779 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006780 {
6781 clear_tv(rettv);
6782 ret = FAIL;
6783 }
6784 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006785 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6786 || rettv->v_type == VAR_OBJECT))
6787 {
6788 // class member: SomeClass.varname
6789 // class method: SomeClass.SomeMethod()
6790 // class constructor: SomeClass.new()
6791 // object member: someObject.varname
6792 // object method: someObject.SomeMethod()
6793 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6794 {
6795 clear_tv(rettv);
6796 ret = FAIL;
6797 }
6798 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006799 else
6800 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006801 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006802
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006803 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6804 // Don't do this when "Func" is already a partial that was bound
6805 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006806 if (selfdict != NULL
6807 && (rettv->v_type == VAR_FUNC
6808 || (rettv->v_type == VAR_PARTIAL
6809 && (rettv->vval.v_partial->pt_auto
6810 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006811 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006812
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006813 dict_unref(selfdict);
6814 return ret;
6815}
6816
6817/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006818 * Make a copy of an item.
6819 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006820 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006821 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6822 * reference to an already copied list/dict can be used.
6823 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006824 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006826item_copy(
6827 typval_T *from,
6828 typval_T *to,
6829 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006830 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006831 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006832{
6833 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006834 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835
Bram Moolenaar33570922005-01-25 22:26:29 +00006836 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006837 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006838 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006839 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006840 }
6841 ++recurse;
6842
6843 switch (from->v_type)
6844 {
6845 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006846 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006847 case VAR_STRING:
6848 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006849 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006850 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006851 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006852 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006853 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006854 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006855 case VAR_CLASS:
6856 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857 copy_tv(from, to);
6858 break;
6859 case VAR_LIST:
6860 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006861 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006862 if (from->vval.v_list == NULL)
6863 to->vval.v_list = NULL;
6864 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6865 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006866 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006867 to->vval.v_list = from->vval.v_list->lv_copylist;
6868 ++to->vval.v_list->lv_refcount;
6869 }
6870 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006871 to->vval.v_list = list_copy(from->vval.v_list,
6872 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006873 if (to->vval.v_list == NULL)
6874 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006875 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006876 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006877 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006878 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 case VAR_DICT:
6880 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006881 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006882 if (from->vval.v_dict == NULL)
6883 to->vval.v_dict = NULL;
6884 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6885 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006886 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006887 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6888 ++to->vval.v_dict->dv_refcount;
6889 }
6890 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006891 to->vval.v_dict = dict_copy(from->vval.v_dict,
6892 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006893 if (to->vval.v_dict == NULL)
6894 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006895 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006896 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006897 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006898 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006899 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006900 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006901 }
6902 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006903 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006904}
6905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006906 void
6907echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6908{
6909 char_u *tofree;
6910 char_u numbuf[NUMBUFLEN];
6911 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6912
6913 if (*atstart)
6914 {
6915 *atstart = FALSE;
6916 // Call msg_start() after eval1(), evaluating the expression
6917 // may cause a message to appear.
6918 if (with_space)
6919 {
6920 // Mark the saved text as finishing the line, so that what
6921 // follows is displayed on a new line when scrolling back
6922 // at the more prompt.
6923 msg_sb_eol();
6924 msg_start();
6925 }
6926 }
6927 else if (with_space)
6928 msg_puts_attr(" ", echo_attr);
6929
6930 if (p != NULL)
6931 for ( ; *p != NUL && !got_int; ++p)
6932 {
6933 if (*p == '\n' || *p == '\r' || *p == TAB)
6934 {
6935 if (*p != TAB && *needclr)
6936 {
6937 // remove any text still there from the command
6938 msg_clr_eos();
6939 *needclr = FALSE;
6940 }
6941 msg_putchar_attr(*p, echo_attr);
6942 }
6943 else
6944 {
6945 if (has_mbyte)
6946 {
6947 int i = (*mb_ptr2len)(p);
6948
6949 (void)msg_outtrans_len_attr(p, i, echo_attr);
6950 p += i - 1;
6951 }
6952 else
6953 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6954 }
6955 }
6956 vim_free(tofree);
6957}
6958
Bram Moolenaare9a41262005-01-15 22:18:47 +00006959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 * ":echo expr1 ..." print each argument separated with a space, add a
6961 * newline at the end.
6962 * ":echon expr1 ..." print each argument plain.
6963 */
6964 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006965ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966{
6967 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006968 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006969 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 int needclr = TRUE;
6971 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006972 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006973 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006974 evalarg_T evalarg;
6975
Bram Moolenaare707c882020-07-01 13:07:37 +02006976 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 if (eap->skip)
6979 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006980 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006982 // If eval1() causes an error message the text from the command may
6983 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006984 need_clr_eos = needclr;
6985
Bram Moolenaar68db9962021-05-09 23:19:22 +02006986 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006987 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 {
6989 /*
6990 * Report the invalid expression unless the expression evaluation
6991 * has been cancelled due to an aborting error, an interrupt, or an
6992 * exception.
6993 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006994 if (!aborting() && did_emsg == did_emsg_before
6995 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006996 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006997 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 break;
6999 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007000 need_clr_eos = FALSE;
7001
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02007003 {
7004 if (rettv.v_type == VAR_VOID)
7005 {
7006 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
7007 break;
7008 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01007009 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02007010 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007011
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007012 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 arg = skipwhite(arg);
7014 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02007015 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02007016 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018 if (eap->skip)
7019 --emsg_skip;
7020 else
7021 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007022 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 if (needclr)
7024 msg_clr_eos();
7025 if (eap->cmdidx == CMD_echo)
7026 msg_end();
7027 }
7028}
7029
7030/*
7031 * ":echohl {name}".
7032 */
7033 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007034ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007036 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037}
7038
7039/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007040 * Returns the :echo attribute
7041 */
7042 int
7043get_echo_attr(void)
7044{
7045 return echo_attr;
7046}
7047
7048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 * ":execute expr1 ..." execute the result of an expression.
7050 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007051 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007053 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 * Each gets spaces around each argument and a newline at the end for
7055 * echo commands
7056 */
7057 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007058ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059{
7060 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007061 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 int ret = OK;
7063 char_u *p;
7064 garray_T ga;
7065 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007066 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067
7068 ga_init2(&ga, 1, 80);
7069
7070 if (eap->skip)
7071 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007072 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007074 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007075 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 if (!eap->skip)
7079 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007080 char_u buf[NUMBUFLEN];
7081
7082 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007083 {
7084 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7085 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007086 semsg(_(e_using_invalid_value_as_string_str),
7087 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007088 p = NULL;
7089 }
7090 else
7091 p = tv_get_string_buf(&rettv, buf);
7092 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007093 else
7094 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007095 if (p == NULL)
7096 {
7097 clear_tv(&rettv);
7098 ret = FAIL;
7099 break;
7100 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101 len = (int)STRLEN(p);
7102 if (ga_grow(&ga, len + 2) == FAIL)
7103 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007104 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105 ret = FAIL;
7106 break;
7107 }
7108 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 ga.ga_len += len;
7112 }
7113
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007114 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115 arg = skipwhite(arg);
7116 }
7117
7118 if (ret != FAIL && ga.ga_data != NULL)
7119 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007120 // use the first line of continuation lines for messages
7121 SOURCING_LNUM = start_lnum;
7122
Bram Moolenaar37fef162022-08-29 18:16:32 +01007123 if (eap->cmdidx == CMD_echomsg
7124 || eap->cmdidx == CMD_echowindow
7125 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007127 // Mark the already saved text as finishing the line, so that what
7128 // follows is displayed on a new line when scrolling back at the
7129 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007130 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007131 }
7132
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007133 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007134 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007135 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007136 out_flush();
7137 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007138 else if (eap->cmdidx == CMD_echowindow)
7139 {
7140#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007141 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007142#endif
7143 msg_attr(ga.ga_data, echo_attr);
7144#ifdef HAS_MESSAGE_WINDOW
7145 end_echowindow();
7146#endif
7147 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007148 else if (eap->cmdidx == CMD_echoconsole)
7149 {
7150 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7151 ui_write((char_u *)"\r\n", 2, TRUE);
7152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 else if (eap->cmdidx == CMD_echoerr)
7154 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007155 int save_did_emsg = did_emsg;
7156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007157 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007158 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 if (!force_abort)
7160 did_emsg = save_did_emsg;
7161 }
7162 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007163 {
7164 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7165
7166 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7167 sticky_cmdmod_flags = cmdmod.cmod_flags
7168 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 do_cmdline((char_u *)ga.ga_data,
7170 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007171 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 }
7174
7175 ga_clear(&ga);
7176
7177 if (eap->skip)
7178 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007179 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180}
7181
7182/*
7183 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7184 * "arg" points to the "&" or '+' when called, to "option" when returning.
7185 * Returns NULL when no option name found. Otherwise pointer to the char
7186 * after the option name.
7187 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007188 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007189find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
7191 char_u *p = *arg;
7192
7193 ++p;
7194 if (*p == 'g' && p[1] == ':')
7195 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007196 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 p += 2;
7198 }
7199 else if (*p == 'l' && p[1] == ':')
7200 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007201 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007202 p += 2;
7203 }
7204 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007205 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206
7207 if (!ASCII_ISALPHA(*p))
7208 return NULL;
7209 *arg = p;
7210
7211 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007212 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 else
7214 while (ASCII_ISALPHA(*p))
7215 ++p;
7216 return p;
7217}
7218
7219/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007220 * Display script name where an item was last set.
7221 * Should only be invoked when 'verbose' is non-zero.
7222 */
7223 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007224last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007225{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007226 char_u *p;
7227
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007228 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007229 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007230 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007231 if (p != NULL)
7232 {
7233 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01007234 msg_puts(_("\n\tLast set from "));
7235 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007236 if (script_ctx.sc_lnum > 0)
7237 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01007238 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007239 msg_outnum((long)script_ctx.sc_lnum);
7240 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007241 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007242 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007243 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00007244 }
7245}
7246
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007247#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248
7249/*
7250 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007251 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007252 * "flags" can be "g" to do a global substitute.
7253 * Returns an allocated string, NULL for error.
7254 */
7255 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007256do_string_sub(
7257 char_u *str,
7258 char_u *pat,
7259 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007260 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007261 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262{
7263 int sublen;
7264 regmatch_T regmatch;
7265 int i;
7266 int do_all;
7267 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007268 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269 garray_T ga;
7270 char_u *ret;
7271 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007272 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007273
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007274 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007276 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007277
7278 ga_init2(&ga, 1, 200);
7279
7280 do_all = (flags[0] == 'g');
7281
7282 regmatch.rm_ic = p_ic;
7283 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7284 if (regmatch.regprog != NULL)
7285 {
7286 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007287 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7289 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007290 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007291 if (regmatch.startp[0] == regmatch.endp[0])
7292 {
7293 if (zero_width == regmatch.startp[0])
7294 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007295 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007296 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007297 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7298 (size_t)i);
7299 ga.ga_len += i;
7300 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007301 continue;
7302 }
7303 zero_width = regmatch.startp[0];
7304 }
7305
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 /*
7307 * Get some space for a temporary buffer to do the substitution
7308 * into. It will contain:
7309 * - The text up to where the match is.
7310 * - The substituted text.
7311 * - The text after the match.
7312 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007313 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007314 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7316 {
7317 ga_clear(&ga);
7318 break;
7319 }
7320
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007321 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 i = (int)(regmatch.startp[0] - tail);
7323 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007324 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007325 (void)vim_regsub(&regmatch, sub, expr,
7326 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7327 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007329 tail = regmatch.endp[0];
7330 if (*tail == NUL)
7331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332 if (!do_all)
7333 break;
7334 }
7335
7336 if (ga.ga_data != NULL)
7337 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7338
Bram Moolenaar473de612013-06-08 18:19:48 +02007339 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340 }
7341
7342 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7343 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007344 if (p_cpo == empty_option)
7345 p_cpo = save_cpo;
7346 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007347 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007348 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007349 // If it's still empty it was changed and restored, need to restore in
7350 // the complicated way.
7351 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007352 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007353 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355
7356 return ret;
7357}