blob: 5baaa99062ccea557ff4eb2291055bb5a778f7c9 [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 Moolenaar7ce7daf2022-12-10 18:42:12 +00001196 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001197 && lp->ll_tv->v_type != VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001198 && lp->ll_tv->v_type != VAR_CLASS)
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001199 {
1200 if (!quiet)
1201 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1202 return NULL;
1203 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001204 if (lp->ll_tv->v_type != VAR_LIST
1205 && lp->ll_tv->v_type != VAR_DICT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001206 && lp->ll_tv->v_type != VAR_BLOB
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001207 && lp->ll_tv->v_type != VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001208 && lp->ll_tv->v_type != VAR_CLASS)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001209 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001210 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001211 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001212 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001213 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001214
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001215 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001216 int r = OK;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001217 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001218 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001219 else if (lp->ll_tv->v_type == VAR_BLOB
1220 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001221 r = rettv_blob_alloc(lp->ll_tv);
1222 if (r == FAIL)
1223 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001224
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001225 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001226 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001227 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001228 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001229 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001230 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001231
Bram Moolenaar4525a572022-02-13 11:57:33 +00001232 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001233 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001234 && lp->ll_tv == &v->di_tv
1235 && ht != NULL && ht == get_script_local_ht())
1236 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001237 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001238
1239 // Vim9 script local variable: get the type
1240 if (sv != NULL)
1241 lp->ll_valtype = sv->sv_type;
1242 }
1243
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 len = -1;
1245 if (*p == '.')
1246 {
1247 key = p + 1;
1248 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1249 ;
1250 if (len == 0)
1251 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001252 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001253 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 }
1256 p = key + len;
1257 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001258 else
1259 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001260 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001261 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001262 if (*p == ':')
1263 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001264 else
1265 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001267 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001268 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001269 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001270 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001271 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001272 clear_tv(&var1);
1273 return NULL;
1274 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001275 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001276 }
1277
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001278 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001279 if (*p == ':')
1280 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001281 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001282 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001284 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001285 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001287 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001288 if (rettv != NULL
1289 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001290 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001291 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001292 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001293 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001295 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001296 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001298 }
1299 p = skipwhite(p + 1);
1300 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001301 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001302 else
1303 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001304 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001305 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001306 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001307 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001308 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001309 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001310 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001311 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001312 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001313 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001314 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001315 clear_tv(&var2);
1316 return NULL;
1317 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001318 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001319 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001320 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001321 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001322 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001323
Bram Moolenaar8c711452005-01-14 21:53:12 +00001324 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001325 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001326 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001327 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001328 clear_tv(&var1);
1329 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001330 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001331 }
1332
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001333 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001334 ++p;
1335 }
1336
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001337 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001338 {
1339 if (len == -1)
1340 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001341 // "[key]": get key from "var1"
1342 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001343 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001345 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001346 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001347 }
1348 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001349 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001350
1351 // a NULL dict is equivalent with an empty dict
1352 if (lp->ll_tv->vval.v_dict == NULL)
1353 {
1354 lp->ll_tv->vval.v_dict = dict_alloc();
1355 if (lp->ll_tv->vval.v_dict == NULL)
1356 {
1357 clear_tv(&var1);
1358 return NULL;
1359 }
1360 ++lp->ll_tv->vval.v_dict->dv_refcount;
1361 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001362 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001363
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001364 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001366 // When assigning to a scope dictionary check that a function and
1367 // variable name is valid (only variable name unless it is l: or
1368 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001369 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001370 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001371 int prevval;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001372
1373 if (len != -1)
1374 {
1375 prevval = key[len];
1376 key[len] = NUL;
1377 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001378 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001379 prevval = 0; // avoid compiler warning
Bram Moolenaar88456cd2022-11-18 22:14:09 +00001380 int wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
zeertzjq91c75d12022-11-05 20:21:58 +00001381 && (rettv->v_type == VAR_FUNC
1382 || rettv->v_type == VAR_PARTIAL)
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001383 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001384 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001385 if (len != -1)
1386 key[len] = prevval;
1387 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001388 {
1389 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001390 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001391 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001392 }
1393
Bram Moolenaar10c65862020-10-08 21:16:42 +02001394 if (lp->ll_valtype != NULL)
1395 // use the type of the member
1396 lp->ll_valtype = lp->ll_valtype->tt_member;
1397
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001398 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001399 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001400 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001401 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001402 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001403 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001404 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001405 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001406 return NULL;
1407 }
1408
Bram Moolenaar31b81602019-02-10 22:14:27 +01001409 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001410 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001411 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001412 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001413 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001414 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001415 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001416 }
1417 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001418 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001419 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001420 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001421 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001422 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001423 p = NULL;
1424 break;
1425 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001426 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001427 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001428 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1429 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001430 {
1431 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001432 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001433 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001434
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001435 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001436 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001438 else if (lp->ll_tv->v_type == VAR_BLOB)
1439 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001440 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1441
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001442 /*
1443 * Get the number and item for the only or first index of the List.
1444 */
1445 if (empty1)
1446 lp->ll_n1 = 0;
1447 else
1448 // is number or string
1449 lp->ll_n1 = (long)tv_get_number(&var1);
1450 clear_tv(&var1);
1451
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001452 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001453 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001454 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001455 return NULL;
1456 }
1457 if (lp->ll_range && !lp->ll_empty2)
1458 {
1459 lp->ll_n2 = (long)tv_get_number(&var2);
1460 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001461 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1462 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001463 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001464 }
1465 lp->ll_blob = lp->ll_tv->vval.v_blob;
1466 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001467 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001468 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001469 else if (lp->ll_tv->v_type == VAR_LIST)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 {
1471 /*
1472 * Get the number and item for the only or first index of the List.
1473 */
1474 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001475 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001476 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001477 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001478 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001479 clear_tv(&var1);
1480
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001481 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001482 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001483 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1484 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001485 if (lp->ll_li == NULL)
1486 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001487 clear_tv(&var2);
1488 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001489 }
1490
Bram Moolenaar10c65862020-10-08 21:16:42 +02001491 if (lp->ll_valtype != NULL)
1492 // use the type of the member
1493 lp->ll_valtype = lp->ll_valtype->tt_member;
1494
Bram Moolenaar8c711452005-01-14 21:53:12 +00001495 /*
1496 * May need to find the item or absolute index for the second
1497 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 * When no index given: "lp->ll_empty2" is TRUE.
1499 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001500 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001502 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001503 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001504 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001505 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001506 if (check_range_index_two(lp->ll_list,
1507 &lp->ll_n1, lp->ll_li,
1508 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001509 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001510 }
1511
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001512 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001513 }
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001514 else // v_type == VAR_CLASS || v_type == VAR_OBJECT
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001515 {
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001516 class_T *cl = (lp->ll_tv->v_type == VAR_OBJECT
1517 && lp->ll_tv->vval.v_object != NULL)
1518 ? lp->ll_tv->vval.v_object->obj_class
1519 : lp->ll_tv->vval.v_class;
1520 // TODO: what if class is NULL?
1521 if (cl != NULL)
1522 {
1523 lp->ll_valtype = NULL;
1524 for (int i = 0; i < cl->class_obj_member_count; ++i)
1525 {
1526 objmember_T *om = cl->class_obj_members + i;
1527 if (STRNCMP(om->om_name, key, p - key) == 0
1528 && om->om_name[p - key] == NUL)
1529 {
1530 switch (om->om_access)
1531 {
1532 case ACCESS_PRIVATE:
1533 semsg(_(e_cannot_access_private_object_member_str),
1534 om->om_name);
1535 return NULL;
1536 case ACCESS_READ:
1537 if (!(flags & GLV_READ_ONLY))
1538 {
1539 semsg(_(e_object_member_is_not_writable_str),
1540 om->om_name);
1541 return NULL;
1542 }
1543 break;
1544 case ACCESS_ALL:
1545 break;
1546 }
1547
1548 lp->ll_valtype = om->om_type;
1549
1550 if (lp->ll_tv->v_type == VAR_OBJECT)
1551 lp->ll_tv = ((typval_T *)(
1552 lp->ll_tv->vval.v_object + 1)) + i;
1553 // TODO: what about a class?
1554 break;
1555 }
1556 }
1557 if (lp->ll_valtype == NULL)
1558 {
1559 semsg(_(e_object_member_not_found_str), key);
1560 return NULL;
1561 }
1562 }
Bram Moolenaar7ce7daf2022-12-10 18:42:12 +00001563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001564 }
1565
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001566 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001568 return p;
1569}
1570
1571/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001572 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001573 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001575clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001576{
1577 vim_free(lp->ll_exp_name);
1578 vim_free(lp->ll_newkey);
1579}
1580
1581/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001582 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001583 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001584 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1585 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001586 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001588set_var_lval(
1589 lval_T *lp,
1590 char_u *endp,
1591 typval_T *rettv,
1592 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001593 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1594 char_u *op,
1595 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001596{
1597 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001598 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001599
1600 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001601 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001602 cc = *endp;
1603 *endp = NUL;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001604 if (in_vim9script() && check_reserved_name(lp->ll_name, NULL) == FAIL)
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001605 return;
1606
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001607 if (lp->ll_blob != NULL)
1608 {
1609 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001610
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001611 if (op != NULL && *op != '=')
1612 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001613 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001614 return;
1615 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001616 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001617 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001618
1619 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1620 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001621 if (lp->ll_empty2)
1622 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1623
Bram Moolenaar68452172021-04-12 21:21:02 +02001624 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1625 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001626 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001627 }
1628 else
1629 {
1630 val = (int)tv_get_number_chk(rettv, &error);
1631 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001632 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001633 }
1634 }
1635 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001636 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001637 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001638
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001639 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1640 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001641 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001642 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001643 *endp = cc;
1644 return;
1645 }
1646
Bram Moolenaarff697e62019-02-12 22:28:33 +01001647 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001648 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001649 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001650 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001651 {
1652 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001653 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1654 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001655 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001656 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001657 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001658 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001659 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001660 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001661 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001662 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001663 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1664 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001665 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001666 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001667 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001668 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001669 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001670 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001671 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001672 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001673 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001674 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001675 else if (lp->ll_range)
1676 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001677 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1678 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001679 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001680 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001681 return;
1682 }
1683
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001684 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1685 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001686 }
1687 else
1688 {
1689 /*
Bram Moolenaar3d473ee2022-12-14 20:59:32 +00001690 * Assign to a List, Dictionary or Object item.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001691 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001692 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1693 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001694 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001695 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001696 return;
1697 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001698
1699 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001700 && check_typval_arg_type(lp->ll_valtype, rettv,
1701 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001702 return;
1703
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001704 if (lp->ll_newkey != NULL)
1705 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001706 if (op != NULL && *op != '=')
1707 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001708 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001709 return;
1710 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001711 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1712 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001713 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001714
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001715 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001716 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001717 if (di == NULL)
1718 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001719 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1720 {
1721 vim_free(di);
1722 return;
1723 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001724 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001726 else if (op != NULL && *op != '=')
1727 {
1728 tv_op(lp->ll_tv, rettv, op);
1729 return;
1730 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001731 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001732 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001733
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001734 /*
1735 * Assign the value to the variable or list item.
1736 */
1737 if (copy)
1738 copy_tv(rettv, lp->ll_tv);
1739 else
1740 {
1741 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001742 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001743 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 }
1745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746}
1747
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001748/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001749 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1750 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001751 * Returns OK or FAIL.
1752 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001753 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001754tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001756 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001757 char_u numbuf[NUMBUFLEN];
1758 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001759 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001760
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001761 // Can't do anything with a Funcref or Dict on the right.
1762 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001763 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001764 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1765 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001766 {
1767 switch (tv1->v_type)
1768 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001769 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001770 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772 case VAR_DICT:
1773 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001774 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001775 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001776 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001777 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001778 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001779 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00001780 case VAR_CLASS:
1781 case VAR_OBJECT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001782 break;
1783
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001784 case VAR_BLOB:
1785 if (*op != '+' || tv2->v_type != VAR_BLOB)
1786 break;
1787 // BLOB += BLOB
1788 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1789 {
1790 blob_T *b1 = tv1->vval.v_blob;
1791 blob_T *b2 = tv2->vval.v_blob;
1792 int i, len = blob_len(b2);
1793 for (i = 0; i < len; i++)
1794 ga_append(&b1->bv_ga, blob_get(b2, i));
1795 }
1796 return OK;
1797
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001798 case VAR_LIST:
1799 if (*op != '+' || tv2->v_type != VAR_LIST)
1800 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001801 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001802 if (tv2->vval.v_list != NULL)
1803 {
1804 if (tv1->vval.v_list == NULL)
1805 {
1806 tv1->vval.v_list = tv2->vval.v_list;
1807 ++tv1->vval.v_list->lv_refcount;
1808 }
1809 else
1810 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1811 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 return OK;
1813
1814 case VAR_NUMBER:
1815 case VAR_STRING:
1816 if (tv2->v_type == VAR_LIST)
1817 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001818 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001819 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001820 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001821 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001822 if (tv2->v_type == VAR_FLOAT)
1823 {
1824 float_T f = n;
1825
Bram Moolenaarff697e62019-02-12 22:28:33 +01001826 if (*op == '%')
1827 break;
1828 switch (*op)
1829 {
1830 case '+': f += tv2->vval.v_float; break;
1831 case '-': f -= tv2->vval.v_float; break;
1832 case '*': f *= tv2->vval.v_float; break;
1833 case '/': f /= tv2->vval.v_float; break;
1834 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001835 clear_tv(tv1);
1836 tv1->v_type = VAR_FLOAT;
1837 tv1->vval.v_float = f;
1838 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001840 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001841 switch (*op)
1842 {
1843 case '+': n += tv_get_number(tv2); break;
1844 case '-': n -= tv_get_number(tv2); break;
1845 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001846 case '/': n = num_divide(n, tv_get_number(tv2),
1847 &failed); break;
1848 case '%': n = num_modulus(n, tv_get_number(tv2),
1849 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001850 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001851 clear_tv(tv1);
1852 tv1->v_type = VAR_NUMBER;
1853 tv1->vval.v_number = n;
1854 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001855 }
1856 else
1857 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001858 if (tv2->v_type == VAR_FLOAT)
1859 break;
1860
Bram Moolenaarff697e62019-02-12 22:28:33 +01001861 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001862 s = tv_get_string(tv1);
1863 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001864 clear_tv(tv1);
1865 tv1->v_type = VAR_STRING;
1866 tv1->vval.v_string = s;
1867 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001868 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001869
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001870 case VAR_FLOAT:
1871 {
1872 float_T f;
1873
Bram Moolenaarff697e62019-02-12 22:28:33 +01001874 if (*op == '%' || *op == '.'
1875 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001876 && tv2->v_type != VAR_NUMBER
1877 && tv2->v_type != VAR_STRING))
1878 break;
1879 if (tv2->v_type == VAR_FLOAT)
1880 f = tv2->vval.v_float;
1881 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001882 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001883 switch (*op)
1884 {
1885 case '+': tv1->vval.v_float += f; break;
1886 case '-': tv1->vval.v_float -= f; break;
1887 case '*': tv1->vval.v_float *= f; break;
1888 case '/': tv1->vval.v_float /= f; break;
1889 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001890 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001891 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 }
1893 }
1894
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001895 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001896 return FAIL;
1897}
1898
1899/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 * Evaluate the expression used in a ":for var in expr" command.
1901 * "arg" points to "var".
1902 * Set "*errp" to TRUE for an error, FALSE otherwise;
1903 * Return a pointer that holds the info. Null when there is an error.
1904 */
1905 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001906eval_for_line(
1907 char_u *arg,
1908 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001909 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001910 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911{
Bram Moolenaar33570922005-01-25 22:26:29 +00001912 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001913 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 typval_T tv;
1916 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001917 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001919 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001921 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 if (fi == NULL)
1923 return NULL;
1924
Bram Moolenaar404557e2021-07-05 21:41:48 +02001925 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1926 &fi->fi_semicolon, FALSE);
1927 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 return fi;
1929
Bram Moolenaar404557e2021-07-05 21:41:48 +02001930 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001931 if (expr[0] != 'i' || expr[1] != 'n'
1932 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001933 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001934 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1935 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1936 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001937 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 return fi;
1939 }
1940
1941 if (skip)
1942 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001943 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1944 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001945 {
1946 *errp = FALSE;
1947 if (!skip)
1948 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001949 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001950 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001951 l = tv.vval.v_list;
1952 if (l == NULL)
1953 {
1954 // a null list is like an empty list: do nothing
1955 clear_tv(&tv);
1956 }
1957 else
1958 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001960 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001962 // No need to increment the refcount, it's already set for
1963 // the list being used in "tv".
1964 fi->fi_list = l;
1965 list_add_watch(l, &fi->fi_lw);
1966 fi->fi_lw.lw_item = l->lv_first;
1967 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001968 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001969 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001970 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001971 fi->fi_bi = 0;
1972 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001973 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001974 typval_T btv;
1975
1976 // Make a copy, so that the iteration still works when the
1977 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001979 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001980 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001981 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001982 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001983 else if (tv.v_type == VAR_STRING)
1984 {
1985 fi->fi_byte_idx = 0;
1986 fi->fi_string = tv.vval.v_string;
1987 tv.vval.v_string = NULL;
1988 if (fi->fi_string == NULL)
1989 fi->fi_string = vim_strsave((char_u *)"");
1990 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 else
1992 {
Sean Dewar80d73952021-08-04 19:25:54 +02001993 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001994 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995 }
1996 }
1997 }
1998 if (skip)
1999 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002000 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002001
2002 return fi;
2003}
2004
2005/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002006 * Used when looping over a :for line, skip the "in expr" part.
2007 */
2008 void
2009skip_for_lines(void *fi_void, evalarg_T *evalarg)
2010{
2011 forinfo_T *fi = (forinfo_T *)fi_void;
2012 int i;
2013
2014 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01002015 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002016}
2017
2018/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 * Use the first item in a ":for" list. Advance to the next.
2020 * Assign the values to the variable (list). "arg" points to the first one.
2021 * Return TRUE when a valid item was found, FALSE when at end of list or
2022 * something wrong.
2023 */
2024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002025next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002027 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002029 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002030 ? (ASSIGN_FINAL
2031 // first round: error if variable exists
2032 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01002033 | ASSIGN_NO_MEMBER_TYPE
2034 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02002035 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00002036 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002037 int skip_assign = in_vim9script() && arg[0] == '_'
2038 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002040 if (fi->fi_blob != NULL)
2041 {
2042 typval_T tv;
2043
2044 if (fi->fi_bi >= blob_len(fi->fi_blob))
2045 return FALSE;
2046 tv.v_type = VAR_NUMBER;
2047 tv.v_lock = VAR_FIXED;
2048 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2049 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002050 if (skip_assign)
2051 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02002052 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002053 fi->fi_varcount, flag, NULL) == OK;
2054 }
2055
2056 if (fi->fi_string != NULL)
2057 {
2058 typval_T tv;
2059 int len;
2060
2061 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
2062 if (len == 0)
2063 return FALSE;
2064 tv.v_type = VAR_STRING;
2065 tv.v_lock = VAR_FIXED;
2066 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
2067 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002068 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002069 if (skip_assign)
2070 result = TRUE;
2071 else
2072 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002073 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01002074 vim_free(tv.vval.v_string);
2075 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002076 }
2077
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078 item = fi->fi_lw.lw_item;
2079 if (item == NULL)
2080 result = FALSE;
2081 else
2082 {
2083 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002084 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002085 if (skip_assign)
2086 result = TRUE;
2087 else
2088 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002089 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002090 }
2091 return result;
2092}
2093
2094/*
2095 * Free the structure used to store info used by ":for".
2096 */
2097 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099{
Bram Moolenaar33570922005-01-25 22:26:29 +00002100 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002101
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002102 if (fi == NULL)
2103 return;
2104 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002105 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002106 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002107 list_unref(fi->fi_list);
2108 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002109 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002110 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002111 else
2112 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002113 vim_free(fi);
2114}
2115
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002117set_context_for_expression(
2118 expand_T *xp,
2119 char_u *arg,
2120 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002122 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002126 if (cmdidx == CMD_let || cmdidx == CMD_var
2127 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002128 {
2129 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002132 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002133 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134 {
2135 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002136 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002137 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002138 break;
2139 }
2140 return;
2141 }
2142 }
2143 else
2144 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2145 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 while ((xp->xp_pattern = vim_strpbrk(arg,
2147 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2148 {
2149 c = *xp->xp_pattern;
2150 if (c == '&')
2151 {
2152 c = xp->xp_pattern[1];
2153 if (c == '&')
2154 {
2155 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002156 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 }
2158 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002159 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002161 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2162 xp->xp_pattern += 2;
2163
2164 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165 }
2166 else if (c == '$')
2167 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002168 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 xp->xp_context = EXPAND_ENV_VARS;
2170 }
2171 else if (c == '=')
2172 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002173 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 xp->xp_context = EXPAND_EXPRESSION;
2175 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002176 else if (c == '#'
2177 && xp->xp_context == EXPAND_EXPRESSION)
2178 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002179 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002180 break;
2181 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002182 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 && xp->xp_context == EXPAND_FUNCTIONS
2184 && vim_strchr(xp->xp_pattern, '(') == NULL)
2185 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002186 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 break;
2188 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002189 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002191 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 {
2193 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2194 if (c == '\\' && xp->xp_pattern[1] != NUL)
2195 ++xp->xp_pattern;
2196 xp->xp_context = EXPAND_NOTHING;
2197 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002198 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002200 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2202 /* skip */ ;
2203 xp->xp_context = EXPAND_NOTHING;
2204 }
2205 else if (c == '|')
2206 {
2207 if (xp->xp_pattern[1] == '|')
2208 {
2209 ++xp->xp_pattern;
2210 xp->xp_context = EXPAND_EXPRESSION;
2211 }
2212 else
2213 xp->xp_context = EXPAND_COMMANDS;
2214 }
2215 else
2216 xp->xp_context = EXPAND_EXPRESSION;
2217 }
2218 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002219 // Doesn't look like something valid, expand as an expression
2220 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002221 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 arg = xp->xp_pattern;
2223 if (*arg != NUL)
2224 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2225 /* skip */ ;
2226 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002227
2228 // ":exe one two" completes "two"
2229 if ((cmdidx == CMD_execute
2230 || cmdidx == CMD_echo
2231 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002232 || cmdidx == CMD_echomsg
2233 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002234 && xp->xp_context == EXPAND_EXPRESSION)
2235 {
2236 for (;;)
2237 {
2238 char_u *n = skiptowhite(arg);
2239
2240 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2241 break;
2242 arg = skipwhite(n);
2243 }
2244 }
2245
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 xp->xp_pattern = arg;
2247}
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002250 * Return TRUE if "pat" matches "text".
2251 * Does not use 'cpo' and always uses 'magic'.
2252 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002253 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002254pattern_match(char_u *pat, char_u *text, int ic)
2255{
2256 int matches = FALSE;
2257 char_u *save_cpo;
2258 regmatch_T regmatch;
2259
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002260 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002261 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002262 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002263 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2264 if (regmatch.regprog != NULL)
2265 {
2266 regmatch.rm_ic = ic;
2267 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2268 vim_regfree(regmatch.regprog);
2269 }
2270 p_cpo = save_cpo;
2271 return matches;
2272}
2273
2274/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002275 * Handle a name followed by "(". Both for just "name(arg)" and for
2276 * "expr->name(arg)".
2277 * Returns OK or FAIL.
2278 */
2279 static int
2280eval_func(
2281 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002282 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002283 char_u *name,
2284 int name_len,
2285 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002286 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002287 typval_T *basetv) // "expr" for "expr->name(arg)"
2288{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002289 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002290 char_u *s = name;
2291 int len = name_len;
2292 partial_T *partial;
2293 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002294 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002295 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002296
2297 if (!evaluate)
2298 check_vars(s, len);
2299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002300 // If "s" is the name of a variable of type VAR_FUNC
2301 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002302 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002303 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002304
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002305 // Need to make a copy, in case evaluating the arguments makes
2306 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002307 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002308 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002309 ret = FAIL;
2310 else
2311 {
2312 funcexe_T funcexe;
2313
2314 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002315 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002316 funcexe.fe_firstline = curwin->w_cursor.lnum;
2317 funcexe.fe_lastline = curwin->w_cursor.lnum;
2318 funcexe.fe_evaluate = evaluate;
2319 funcexe.fe_partial = partial;
2320 funcexe.fe_basetv = basetv;
2321 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002322 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002323 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002324 }
2325 vim_free(s);
2326
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002327 // If evaluate is FALSE rettv->v_type was not set in
2328 // get_func_tv, but it's needed in handle_subscript() to parse
2329 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002330 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2331 {
2332 rettv->vval.v_string = NULL;
2333 rettv->v_type = VAR_FUNC;
2334 }
2335
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002336 // Stop the expression evaluation when immediately
2337 // aborting on error, or when an interrupt occurred or
2338 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002339 if (evaluate && aborting())
2340 {
2341 if (ret == OK)
2342 clear_tv(rettv);
2343 ret = FAIL;
2344 }
2345 return ret;
2346}
2347
2348/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002349 * After a NL, skip over empty lines and comment-only lines.
2350 */
2351 static char_u *
2352newline_skip_comments(char_u *arg)
2353{
2354 char_u *p = arg + 1;
2355
2356 for (;;)
2357 {
2358 p = skipwhite(p);
2359
2360 if (*p == NUL)
2361 break;
2362 if (vim9_comment_start(p))
2363 {
2364 char_u *nl = vim_strchr(p, NL);
2365
2366 if (nl == NULL)
2367 break;
2368 p = nl;
2369 }
2370 if (*p != NL)
2371 break;
2372 ++p; // skip another NL
2373 }
2374 return p;
2375}
2376
2377/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002378 * Get the next line source line without advancing. But do skip over comment
2379 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002380 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002381 */
2382 static char_u *
2383getline_peek_skip_comments(evalarg_T *evalarg)
2384{
2385 for (;;)
2386 {
2387 char_u *next = getline_peek(evalarg->eval_getline,
2388 evalarg->eval_cookie);
2389 char_u *p;
2390
2391 if (next == NULL)
2392 break;
2393 p = skipwhite(next);
2394 if (*p != NUL && !vim9_comment_start(p))
2395 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002396 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002397 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002398 }
2399 return NULL;
2400}
2401
2402/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002403 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2404 * comment) and there is a next line, return the next line (skipping blanks)
2405 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002406 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2407 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002408 * "arg" must point somewhere inside a line, not at the start.
2409 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002410 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002411eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2412{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002413 char_u *p = skipwhite(arg);
2414
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002415 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002416 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002417 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002418 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2419 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002420 && (*p == NUL || *p == NL
2421 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002422 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002423 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002424
Bram Moolenaare442d592022-05-05 12:20:28 +01002425 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002426 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002427 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002428 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002429 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002430 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002431
Bram Moolenaar9d489562020-07-30 20:08:50 +02002432 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002433 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002434 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002435 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002436 }
2437 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002438 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002439}
2440
2441/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002442 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002443 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002444 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002445 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002446eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002447{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002448 garray_T *gap = &evalarg->eval_ga;
2449 char_u *line;
2450
Bram Moolenaar39be4982022-05-06 21:51:50 +01002451 if (arg != NULL)
2452 {
2453 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002454 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002455 // Truncate before a trailing comment, so that concatenating the lines
2456 // won't turn the rest into a comment.
2457 if (*skipwhite(arg) == '#')
2458 *arg = NUL;
2459 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002460
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002461 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002462 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2463 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002464 else
2465 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002466 if (line == NULL)
2467 return NULL;
2468
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002469 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002470 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2471 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002472 char_u *p = skipwhite(line);
2473
2474 // Going to concatenate the lines after parsing. For an empty or
2475 // comment line use an empty string.
2476 if (*p == NUL || vim9_comment_start(p))
2477 {
2478 vim_free(line);
2479 line = vim_strsave((char_u *)"");
2480 }
2481
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002482 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2483 ++gap->ga_len;
2484 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002485 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002486 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002487 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002488 evalarg->eval_tofree = line;
2489 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002490
2491 // Advanced to the next line, "arg" no longer points into the previous
2492 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002493 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002494 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002495}
2496
Bram Moolenaare6b53242020-07-01 17:28:33 +02002497/*
2498 * Call eval_next_non_blank() and get the next line if needed.
2499 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002500 char_u *
2501skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2502{
2503 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002504 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002505
Bram Moolenaar962d7212020-07-04 14:15:00 +02002506 if (evalarg == NULL)
2507 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002508 eval_next_non_blank(p, evalarg, &getnext);
2509 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002510 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002511 return p;
2512}
2513
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002514/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002515 * The "eval" functions have an "evalarg" argument: When NULL or
2516 * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
2517 * parsed but not executed. The functions may return OK, but the rettv will be
2518 * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 */
2520
2521/*
2522 * Handle zero level expression.
2523 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002525 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002526 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 * Return OK or FAIL.
2528 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002530eval0(
2531 char_u *arg,
2532 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002533 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002534 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002536 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2537}
2538
2539/*
Bram Moolenaara4e0b972022-10-01 19:43:52 +01002540 * If "arg" is a simple function call without arguments then call it and return
2541 * the result. Otherwise return NOTDONE.
2542 */
2543 int
2544may_call_simple_func(
2545 char_u *arg,
2546 typval_T *rettv)
2547{
2548 char_u *parens = (char_u *)strstr((char *)arg, "()");
2549 int r = NOTDONE;
2550
2551 // If the expression is "FuncName()" then we can skip a lot of overhead.
2552 if (parens != NULL && *skipwhite(parens + 2) == NUL)
2553 {
2554 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
2555
2556 if (to_name_end(p, TRUE) == parens)
2557 r = call_simple_func(arg, (int)(parens - arg), rettv);
2558 }
2559 return r;
2560}
2561
2562/*
2563 * Handle zero level expression with optimization for a simple function call.
2564 * Same arguments and return value as eval0().
2565 */
2566 int
2567eval0_simple_funccal(
2568 char_u *arg,
2569 typval_T *rettv,
2570 exarg_T *eap,
2571 evalarg_T *evalarg)
2572{
2573 int r = may_call_simple_func(arg, rettv);
2574
2575 if (r == NOTDONE)
2576 r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
2577 return r;
2578}
2579
2580/*
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002581 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2582 * expression and don't check what comes after the expression.
2583 */
2584 int
2585eval0_retarg(
2586 char_u *arg,
2587 typval_T *rettv,
2588 exarg_T *eap,
2589 evalarg_T *evalarg,
2590 char_u **retarg)
2591{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 int ret;
2593 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002594 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002595 int did_emsg_before = did_emsg;
2596 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002597 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002598 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002599 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
2601 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002602 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002603
Bram Moolenaar79481362022-06-27 20:15:10 +01002604 if (ret != FAIL)
2605 {
2606 expr_end = p;
2607 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002608
Bram Moolenaar79481362022-06-27 20:15:10 +01002609 // In Vim9 script a command block is not split at NL characters for
2610 // commands using an expression argument. Skip over a '#' comment to
2611 // check for a following NL. Require white space before the '#'.
2612 if (in_vim9script() && p > expr_end && retarg == NULL)
2613 while (*p == '#')
2614 {
2615 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002616
Bram Moolenaar79481362022-06-27 20:15:10 +01002617 if (nl == NULL)
2618 break;
2619 p = skipwhite(nl + 1);
2620 if (eap != NULL && *p != NUL)
2621 eap->nextcmd = p;
2622 check_for_end = FALSE;
2623 }
2624
2625 if (check_for_end)
2626 end_error = !ends_excmd2(arg, p);
2627 }
2628
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002629 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002632 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 /*
2634 * Report the invalid expression unless the expression evaluation has
2635 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002636 * exception, or we already gave a more specific error.
2637 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002639 if (!aborting()
2640 && did_emsg == did_emsg_before
2641 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002642 && (flags & EVAL_CONSTANT) == 0
2643 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002644 {
2645 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002646 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002647 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002648 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002649 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002650
2651 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002652 // a next command to avoid more errors, unless "|" is following, which
2653 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002654 if (eap != NULL && p != NULL
2655 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002656 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002657 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002659
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002660 if (retarg != NULL)
2661 *retarg = p;
2662 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002663 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 return ret;
2666}
2667
2668/*
2669 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002670 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002671 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 *
2673 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002674 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002676 * Note: "rettv.v_lock" is not set.
2677 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 * Return OK or FAIL.
2679 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002680 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002681eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002683 char_u *p;
2684 int getnext;
2685
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002686 CLEAR_POINTER(rettv);
2687
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 /*
2689 * Get the first variable.
2690 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002691 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return FAIL;
2693
Bram Moolenaar793648f2020-06-26 21:28:25 +02002694 p = eval_next_non_blank(*arg, evalarg, &getnext);
2695 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002697 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002698 int result;
2699 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002700 evalarg_T *evalarg_used = evalarg;
2701 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002702 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002703 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002704 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002705
2706 if (evalarg == NULL)
2707 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002708 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002709 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002710 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002711 orig_flags = evalarg_used->eval_flags;
2712 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002713
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002714 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002715 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002716 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002717 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002718 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002719 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002720 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002721 clear_tv(rettv);
2722 return FAIL;
2723 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002724 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002725 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002728 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002730 int error = FALSE;
2731
Bram Moolenaar13106602020-10-04 16:06:05 +02002732 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002733 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002734 else if (vim9script)
2735 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002736 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002738 if (error || !op_falsy || !result)
2739 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002740 if (error)
2741 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743
2744 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002745 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002747 if (op_falsy)
2748 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002749 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002750 {
mityu4ac198c2021-05-28 17:52:40 +02002751 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002752 clear_tv(rettv);
2753 return FAIL;
2754 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002755 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002756 evalarg_used->eval_flags = (op_falsy ? !result : result)
2757 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2758 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002759 {
2760 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002762 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002763 if (!op_falsy || !result)
2764 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002766 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002768 /*
2769 * Check for the ":".
2770 */
2771 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2772 if (*p != ':')
2773 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002774 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002775 if (evaluate && result)
2776 clear_tv(rettv);
2777 evalarg_used->eval_flags = orig_flags;
2778 return FAIL;
2779 }
2780 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002781 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002782 else
2783 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002784 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002785 {
2786 error_white_both(p, 1);
2787 clear_tv(rettv);
2788 evalarg_used->eval_flags = orig_flags;
2789 return FAIL;
2790 }
2791 *arg = p;
2792 }
2793
2794 /*
2795 * Get the third variable. Recursive!
2796 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002797 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002798 {
mityu4ac198c2021-05-28 17:52:40 +02002799 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002800 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002801 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002802 return FAIL;
2803 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002804 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2805 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002806 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002807 if (eval1(arg, &var2, evalarg_used) == FAIL)
2808 {
2809 if (evaluate && result)
2810 clear_tv(rettv);
2811 evalarg_used->eval_flags = orig_flags;
2812 return FAIL;
2813 }
2814 if (evaluate && !result)
2815 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002817
2818 if (evalarg == NULL)
2819 clear_evalarg(&local_evalarg, NULL);
2820 else
2821 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823
2824 return OK;
2825}
2826
2827/*
2828 * Handle first level expression:
2829 * expr2 || expr2 || expr2 logical OR
2830 *
2831 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002832 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 *
2834 * Return OK or FAIL.
2835 */
2836 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002837eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002839 char_u *p;
2840 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841
2842 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002843 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002845 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 return FAIL;
2847
2848 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002849 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002850 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002851 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002852 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002854 evalarg_T *evalarg_used = evalarg;
2855 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856 int evaluate;
2857 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002858 long result = FALSE;
2859 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002860 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002861 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002862
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002863 if (evalarg == NULL)
2864 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002865 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002866 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002867 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002868 orig_flags = evalarg_used->eval_flags;
2869 evaluate = orig_flags & EVAL_EVALUATE;
2870 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002871 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002872 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002873 result = tv_get_bool_chk(rettv, &error);
2874 else if (tv_get_number_chk(rettv, &error) != 0)
2875 result = TRUE;
2876 clear_tv(rettv);
2877 if (error)
2878 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
2880
2881 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002882 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002884 while (p[0] == '|' && p[1] == '|')
2885 {
2886 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002887 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002888 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002889 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002890 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002891 {
2892 error_white_both(p, 2);
2893 clear_tv(rettv);
2894 return FAIL;
2895 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002896 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002897 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002898
2899 /*
2900 * Get the second variable.
2901 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002902 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002903 {
mityu4ac198c2021-05-28 17:52:40 +02002904 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002905 clear_tv(rettv);
2906 return FAIL;
2907 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002908 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2909 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002910 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002911 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002912 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002913
2914 /*
2915 * Compute the result.
2916 */
2917 if (evaluate && !result)
2918 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002919 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002920 result = tv_get_bool_chk(&var2, &error);
2921 else if (tv_get_number_chk(&var2, &error) != 0)
2922 result = TRUE;
2923 clear_tv(&var2);
2924 if (error)
2925 return FAIL;
2926 }
2927 if (evaluate)
2928 {
2929 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002930 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002931 rettv->v_type = VAR_BOOL;
2932 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002933 }
2934 else
2935 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002936 rettv->v_type = VAR_NUMBER;
2937 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002938 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002939 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002940
2941 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002943
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002944 if (evalarg == NULL)
2945 clear_evalarg(&local_evalarg, NULL);
2946 else
2947 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 }
2949
2950 return OK;
2951}
2952
2953/*
2954 * Handle second level expression:
2955 * expr3 && expr3 && expr3 logical AND
2956 *
2957 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002958 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 *
2960 * Return OK or FAIL.
2961 */
2962 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002963eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002965 char_u *p;
2966 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967
2968 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002969 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002971 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 return FAIL;
2973
2974 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002975 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002977 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002978 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002980 evalarg_T *evalarg_used = evalarg;
2981 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002982 int orig_flags;
2983 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002984 long result = TRUE;
2985 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002986 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002987 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002988
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002989 if (evalarg == NULL)
2990 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002991 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002992 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002993 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002994 orig_flags = evalarg_used->eval_flags;
2995 evaluate = orig_flags & EVAL_EVALUATE;
2996 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002997 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002998 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002999 result = tv_get_bool_chk(rettv, &error);
3000 else if (tv_get_number_chk(rettv, &error) == 0)
3001 result = FALSE;
3002 clear_tv(rettv);
3003 if (error)
3004 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 }
3006
3007 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003008 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003010 while (p[0] == '&' && p[1] == '&')
3011 {
3012 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003013 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003014 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003015 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003016 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003017 {
3018 error_white_both(p, 2);
3019 clear_tv(rettv);
3020 return FAIL;
3021 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003022 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003023 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003024
3025 /*
3026 * Get the second variable.
3027 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003028 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003029 {
mityu4ac198c2021-05-28 17:52:40 +02003030 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02003031 clear_tv(rettv);
3032 return FAIL;
3033 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003034 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
3035 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003036 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02003037 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003038 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003039 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003040
3041 /*
3042 * Compute the result.
3043 */
3044 if (evaluate && result)
3045 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003046 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003047 result = tv_get_bool_chk(&var2, &error);
3048 else if (tv_get_number_chk(&var2, &error) == 0)
3049 result = FALSE;
3050 clear_tv(&var2);
3051 if (error)
3052 return FAIL;
3053 }
3054 if (evaluate)
3055 {
3056 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003057 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003058 rettv->v_type = VAR_BOOL;
3059 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003060 }
3061 else
3062 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003063 rettv->v_type = VAR_NUMBER;
3064 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02003065 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003066 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003067
3068 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02003070
Bram Moolenaar8af81d62020-07-12 16:32:19 +02003071 if (evalarg == NULL)
3072 clear_evalarg(&local_evalarg, NULL);
3073 else
3074 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 }
3076
3077 return OK;
3078}
3079
3080/*
3081 * Handle third level expression:
3082 * var1 == var2
3083 * var1 =~ var2
3084 * var1 != var2
3085 * var1 !~ var2
3086 * var1 > var2
3087 * var1 >= var2
3088 * var1 < var2
3089 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003090 * var1 is var2
3091 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 *
3093 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003094 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 *
3096 * Return OK or FAIL.
3097 */
3098 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003099eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003102 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003103 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02003105 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106
3107 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003108 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003110 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 return FAIL;
3112
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003113 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003114
Bram Moolenaar696ba232020-07-29 21:20:41 +02003115 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003118 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119 */
Bram Moolenaar87396072019-12-31 22:36:18 +01003120 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003122 typval_T var2;
3123 int ic;
3124 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003125 int evaluate = evalarg == NULL
3126 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003127 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003128
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003129 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003130 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003131 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003132 p = *arg;
3133 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003134 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3135 {
mityu4ac198c2021-05-28 17:52:40 +02003136 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003137 clear_tv(rettv);
3138 return FAIL;
3139 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003140
Bram Moolenaar696ba232020-07-29 21:20:41 +02003141 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3142 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003143 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003144 clear_tv(rettv);
3145 return FAIL;
3146 }
3147
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003148 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 if (p[len] == '?')
3150 {
3151 ic = TRUE;
3152 ++len;
3153 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003154 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 else if (p[len] == '#')
3156 {
3157 ic = FALSE;
3158 ++len;
3159 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003160 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003162 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163
3164 /*
3165 * Get the second variable.
3166 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003167 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3168 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003169 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003170 clear_tv(rettv);
3171 return FAIL;
3172 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003173 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003174 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003176 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 return FAIL;
3178 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003179 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003180 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003181 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003182
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003183 // use the line of the comparison for messages
3184 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003185 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003186 {
3187 ret = FAIL;
3188 clear_tv(rettv);
3189 }
3190 else
3191 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003192 clear_tv(&var2);
3193 return ret;
3194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196
3197 return OK;
3198}
3199
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003200/*
3201 * Make a copy of blob "tv1" and append blob "tv2".
3202 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 void
3204eval_addblob(typval_T *tv1, typval_T *tv2)
3205{
3206 blob_T *b1 = tv1->vval.v_blob;
3207 blob_T *b2 = tv2->vval.v_blob;
3208 blob_T *b = blob_alloc();
3209 int i;
3210
3211 if (b != NULL)
3212 {
3213 for (i = 0; i < blob_len(b1); i++)
3214 ga_append(&b->bv_ga, blob_get(b1, i));
3215 for (i = 0; i < blob_len(b2); i++)
3216 ga_append(&b->bv_ga, blob_get(b2, i));
3217
3218 clear_tv(tv1);
3219 rettv_blob_set(tv1, b);
3220 }
3221}
3222
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003223/*
3224 * Make a copy of list "tv1" and append list "tv2".
3225 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 int
3227eval_addlist(typval_T *tv1, typval_T *tv2)
3228{
3229 typval_T var3;
3230
3231 // concatenate Lists
3232 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3233 {
3234 clear_tv(tv1);
3235 clear_tv(tv2);
3236 return FAIL;
3237 }
3238 clear_tv(tv1);
3239 *tv1 = var3;
3240 return OK;
3241}
3242
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003244 * Handle the bitwise left/right shift operator expression:
3245 * var1 << var2
3246 * var1 >> var2
3247 *
3248 * "arg" must point to the first non-white of the expression.
3249 * "arg" is advanced to just after the recognized expression.
3250 *
3251 * Return OK or FAIL.
3252 */
3253 static int
3254eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3255{
3256 /*
3257 * Get the first expression.
3258 */
3259 if (eval6(arg, rettv, evalarg) == FAIL)
3260 return FAIL;
3261
3262 /*
3263 * Repeat computing, until no '<<' or '>>' is following.
3264 */
3265 for (;;)
3266 {
3267 char_u *p;
3268 int getnext;
3269 exprtype_T type;
3270 int evaluate;
3271 typval_T var2;
3272 int vim9script;
3273
3274 p = eval_next_non_blank(*arg, evalarg, &getnext);
3275 if (p[0] == '<' && p[1] == '<')
3276 type = EXPR_LSHIFT;
3277 else if (p[0] == '>' && p[1] == '>')
3278 type = EXPR_RSHIFT;
3279 else
3280 return OK;
3281
3282 // Handle a bitwise left or right shift operator
3283 if (rettv->v_type != VAR_NUMBER)
3284 {
3285 // left operand should be a number
3286 emsg(_(e_bitshift_ops_must_be_number));
3287 clear_tv(rettv);
3288 return FAIL;
3289 }
3290
3291 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3292 vim9script = in_vim9script();
3293 if (getnext)
3294 {
3295 *arg = eval_next_line(*arg, evalarg);
3296 p = *arg;
3297 }
3298 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3299 {
3300 error_white_both(*arg, 2);
3301 clear_tv(rettv);
3302 return FAIL;
3303 }
3304
3305 /*
3306 * Get the second variable.
3307 */
3308 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3309 {
3310 error_white_both(p, 2);
3311 clear_tv(rettv);
3312 return FAIL;
3313 }
3314 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3315 if (eval6(arg, &var2, evalarg) == FAIL)
3316 {
3317 clear_tv(rettv);
3318 return FAIL;
3319 }
3320
3321 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3322 {
3323 // right operand should be a positive number
3324 if (var2.v_type != VAR_NUMBER)
3325 emsg(_(e_bitshift_ops_must_be_number));
3326 else
dundargocc57b5bc2022-11-02 13:30:51 +00003327 emsg(_(e_bitshift_ops_must_be_positive));
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003328 clear_tv(rettv);
3329 clear_tv(&var2);
3330 return FAIL;
3331 }
3332
3333 if (evaluate)
3334 {
3335 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3336 // shifting more bits than we have always results in zero
3337 rettv->vval.v_number = 0;
3338 else if (type == EXPR_LSHIFT)
3339 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003340 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003341 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003342 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003343 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003344 }
3345
3346 clear_tv(&var2);
3347 }
3348
3349 return OK;
3350}
3351
3352/*
3353 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003354 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003356 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003357 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 *
3359 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003360 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 *
3362 * Return OK or FAIL.
3363 */
3364 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003365eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003368 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003370 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 return FAIL;
3372
3373 /*
3374 * Repeat computing, until no '+', '-' or '.' is following.
3375 */
3376 for (;;)
3377 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003378 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003379 int getnext;
3380 char_u *p;
3381 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003382 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003383 int concat;
3384 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003385 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003386
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003387 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003388 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003389 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003390 p = eval_next_non_blank(*arg, evalarg, &getnext);
3391 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003392 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003393 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3394 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003396 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3397 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003398
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003399 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003400 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003401 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003402 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003403 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003404 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003405 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003406 {
mityu4ac198c2021-05-28 17:52:40 +02003407 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003408 clear_tv(rettv);
3409 return FAIL;
3410 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003411 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003412 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003413 if ((op != '+' || (rettv->v_type != VAR_LIST
3414 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003415 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003416 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003417 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003418 int error = FALSE;
3419
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003420 // For "list + ...", an illegal use of the first operand as
3421 // a number cannot be determined before evaluating the 2nd
3422 // operand: if this is also a list, all is ok.
3423 // For "something . ...", "something - ..." or "non-list + ...",
3424 // we know that the first operand needs to be a string or number
3425 // without evaluating the 2nd operand. So check before to avoid
3426 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003427 if (op != '.')
3428 tv_get_number_chk(rettv, &error);
3429 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003430 {
3431 clear_tv(rettv);
3432 return FAIL;
3433 }
3434 }
3435
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 /*
3437 * Get the second variable.
3438 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003439 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003440 {
mityu89dcb4d2021-05-28 13:50:17 +02003441 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003442 clear_tv(rettv);
3443 return FAIL;
3444 }
3445 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003446 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003448 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 return FAIL;
3450 }
3451
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003452 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 {
3454 /*
3455 * Compute the result.
3456 */
3457 if (op == '.')
3458 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003459 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3460 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003461 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003462
Bram Moolenaar2e086612020-08-25 22:37:48 +02003463 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003464 || var2.v_type == VAR_CHANNEL
3465 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003466 semsg(_(e_using_invalid_value_as_string_str),
3467 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003468 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003469 {
3470 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3471 var2.vval.v_float);
3472 s2 = buf2;
3473 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003474 else
3475 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003476 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003477 {
3478 clear_tv(rettv);
3479 clear_tv(&var2);
3480 return FAIL;
3481 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003482 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003483 clear_tv(rettv);
3484 rettv->v_type = VAR_STRING;
3485 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003487 else if (op == '+' && rettv->v_type == VAR_BLOB
3488 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003490 else if (op == '+' && rettv->v_type == VAR_LIST
3491 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003492 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003494 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 else
3497 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003498 int error = FALSE;
3499 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003500 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003501
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003502 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003503 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003504 f1 = rettv->vval.v_float;
3505 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003506 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003507 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003508 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003509 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003510 if (error)
3511 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003512 // This can only happen for "list + non-list" or
3513 // "blob + non-blob". For "non-list + ..." or
3514 // "something - ...", we returned before evaluating the
3515 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003516 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003517 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003518 return FAIL;
3519 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003520 if (var2.v_type == VAR_FLOAT)
3521 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003522 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003523 if (var2.v_type == VAR_FLOAT)
3524 {
3525 f2 = var2.vval.v_float;
3526 n2 = 0;
3527 }
3528 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003529 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003530 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003531 if (error)
3532 {
3533 clear_tv(rettv);
3534 clear_tv(&var2);
3535 return FAIL;
3536 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 if (rettv->v_type == VAR_FLOAT)
3538 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003540 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003541
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003542 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003543 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3544 {
3545 if (op == '+')
3546 f1 = f1 + f2;
3547 else
3548 f1 = f1 - f2;
3549 rettv->v_type = VAR_FLOAT;
3550 rettv->vval.v_float = f1;
3551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553 {
3554 if (op == '+')
3555 n1 = n1 + n2;
3556 else
3557 n1 = n1 - n2;
3558 rettv->v_type = VAR_NUMBER;
3559 rettv->vval.v_number = n1;
3560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564 }
3565 return OK;
3566}
3567
3568/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003569 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 * * number multiplication
3571 * / number division
3572 * % number modulo
3573 *
3574 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003575 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 *
3577 * Return OK or FAIL.
3578 */
3579 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003580eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003581 char_u **arg,
3582 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003583 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003584 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003586 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587
3588 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003589 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003591 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 return FAIL;
3593
3594 /*
3595 * Repeat computing, until no '*', '/' or '%' is following.
3596 */
3597 for (;;)
3598 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003599 int evaluate;
3600 int getnext;
3601 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003602 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003603 int op;
3604 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003605 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003606 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003607
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003608 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003609 p = eval_next_non_blank(*arg, evalarg, &getnext);
3610 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003611 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003613
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003614 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003615 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003616 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003617 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003618 {
3619 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3620 {
mityu4ac198c2021-05-28 17:52:40 +02003621 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003622 clear_tv(rettv);
3623 return FAIL;
3624 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003625 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003628 f1 = 0;
3629 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003630 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003631 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003633 if (rettv->v_type == VAR_FLOAT)
3634 {
3635 f1 = rettv->vval.v_float;
3636 use_float = TRUE;
3637 n1 = 0;
3638 }
3639 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003640 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003641 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003642 if (error)
3643 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 }
3645 else
3646 n1 = 0;
3647
3648 /*
3649 * Get the second variable.
3650 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003651 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3652 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003653 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003654 clear_tv(rettv);
3655 return FAIL;
3656 }
3657 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003658 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 return FAIL;
3660
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003661 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003663 if (var2.v_type == VAR_FLOAT)
3664 {
3665 if (!use_float)
3666 {
3667 f1 = n1;
3668 use_float = TRUE;
3669 }
3670 f2 = var2.vval.v_float;
3671 n2 = 0;
3672 }
3673 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003675 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003676 clear_tv(&var2);
3677 if (error)
3678 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003679 if (use_float)
3680 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682
3683 /*
3684 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003685 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003687 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003689 if (op == '*')
3690 f1 = f1 * f2;
3691 else if (op == '/')
3692 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003693#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003694 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003695 if (f2 == 0.0)
3696 {
3697 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003698 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003699 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003700 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003701 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003702 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003703 }
3704 else
3705 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003706#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003707 // We rely on the floating point library to handle divide
3708 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003709 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003710#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003713 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003714 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003715 return FAIL;
3716 }
3717 rettv->v_type = VAR_FLOAT;
3718 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 }
3720 else
3721 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003722 int failed = FALSE;
3723
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003724 if (op == '*')
3725 n1 = n1 * n2;
3726 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003727 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003729 n1 = num_modulus(n1, n2, &failed);
3730 if (failed)
3731 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003732
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003733 rettv->v_type = VAR_NUMBER;
3734 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737 }
3738
3739 return OK;
3740}
3741
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003742/*
3743 * Handle a type cast before a base level expression.
3744 * "arg" must point to the first non-white of the expression.
3745 * "arg" is advanced to just after the recognized expression.
3746 * Return OK or FAIL.
3747 */
3748 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003749eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003750 char_u **arg,
3751 typval_T *rettv,
3752 evalarg_T *evalarg,
3753 int want_string) // after "." operator
3754{
3755 type_T *want_type = NULL;
3756 garray_T type_list; // list of pointers to allocated types
3757 int res;
3758 int evaluate = evalarg == NULL ? 0
3759 : (evalarg->eval_flags & EVAL_EVALUATE);
3760
3761 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003762 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3763 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003764 {
3765 ++*arg;
3766 ga_init2(&type_list, sizeof(type_T *), 10);
3767 want_type = parse_type(arg, &type_list, TRUE);
3768 if (want_type == NULL && (evaluate || **arg != '>'))
3769 {
3770 clear_type_list(&type_list);
3771 return FAIL;
3772 }
3773
3774 if (**arg != '>')
3775 {
3776 if (*skipwhite(*arg) == '>')
3777 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3778 else
3779 emsg(_(e_missing_gt));
3780 clear_type_list(&type_list);
3781 return FAIL;
3782 }
3783 ++*arg;
3784 *arg = skipwhite_and_linebreak(*arg, evalarg);
3785 }
3786
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003787 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003788
3789 if (want_type != NULL && evaluate)
3790 {
3791 if (res == OK)
3792 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003793 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3794 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003795
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003796 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003797 {
3798 if (want_type == &t_bool && actual != &t_bool
3799 && (actual->tt_flags & TTFLAG_BOOL_OK))
3800 {
3801 int n = tv2bool(rettv);
3802
3803 // can use "0" and "1" for boolean in some places
3804 clear_tv(rettv);
3805 rettv->v_type = VAR_BOOL;
3806 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3807 }
3808 else
3809 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003810 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003811
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003812 where.wt_variable = TRUE;
3813 res = check_type(want_type, actual, TRUE, where);
3814 }
3815 }
3816 }
3817 clear_type_list(&type_list);
3818 }
3819
3820 return res;
3821}
3822
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003823 int
3824eval_leader(char_u **arg, int vim9)
3825{
3826 char_u *s = *arg;
3827 char_u *p = *arg;
3828
3829 while (*p == '!' || *p == '-' || *p == '+')
3830 {
3831 char_u *n = skipwhite(p + 1);
3832
3833 // ++, --, -+ and +- are not accepted in Vim9 script
3834 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3835 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003836 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003837 return FAIL;
3838 }
3839 p = n;
3840 }
3841 *arg = p;
3842 return OK;
3843}
3844
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003846 * Check for a predefined value "true", "false" and "null.*".
3847 * Return OK when recognized.
3848 */
3849 int
3850handle_predefined(char_u *s, int len, typval_T *rettv)
3851{
3852 switch (len)
3853 {
3854 case 4: if (STRNCMP(s, "true", 4) == 0)
3855 {
3856 rettv->v_type = VAR_BOOL;
3857 rettv->vval.v_number = VVAL_TRUE;
3858 return OK;
3859 }
3860 if (STRNCMP(s, "null", 4) == 0)
3861 {
3862 rettv->v_type = VAR_SPECIAL;
3863 rettv->vval.v_number = VVAL_NULL;
3864 return OK;
3865 }
3866 break;
3867 case 5: if (STRNCMP(s, "false", 5) == 0)
3868 {
3869 rettv->v_type = VAR_BOOL;
3870 rettv->vval.v_number = VVAL_FALSE;
3871 return OK;
3872 }
3873 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003874 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3875 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003876#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003877 rettv->v_type = VAR_JOB;
3878 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003879#else
3880 rettv->v_type = VAR_SPECIAL;
3881 rettv->vval.v_number = VVAL_NULL;
3882#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003883 return OK;
3884 }
3885 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003886 case 9:
3887 if (STRNCMP(s, "null_", 5) != 0)
3888 break;
3889 if (STRNCMP(s + 5, "list", 4) == 0)
3890 {
3891 rettv->v_type = VAR_LIST;
3892 rettv->vval.v_list = NULL;
3893 return OK;
3894 }
3895 if (STRNCMP(s + 5, "dict", 4) == 0)
3896 {
3897 rettv->v_type = VAR_DICT;
3898 rettv->vval.v_dict = NULL;
3899 return OK;
3900 }
3901 if (STRNCMP(s + 5, "blob", 4) == 0)
3902 {
3903 rettv->v_type = VAR_BLOB;
3904 rettv->vval.v_blob = NULL;
3905 return OK;
3906 }
3907 break;
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003908 case 10: if (STRNCMP(s, "null_class", 10) == 0)
3909 {
3910 rettv->v_type = VAR_CLASS;
3911 rettv->vval.v_class = NULL;
3912 return OK;
3913 }
3914 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003915 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3916 {
3917 rettv->v_type = VAR_STRING;
3918 rettv->vval.v_string = NULL;
3919 return OK;
3920 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00003921 if (STRNCMP(s, "null_object", 11) == 0)
3922 {
3923 rettv->v_type = VAR_OBJECT;
3924 rettv->vval.v_object = NULL;
3925 return OK;
3926 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003927 break;
3928 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003929 if (STRNCMP(s, "null_channel", 12) == 0)
3930 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003931#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003932 rettv->v_type = VAR_CHANNEL;
3933 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003934#else
3935 rettv->v_type = VAR_SPECIAL;
3936 rettv->vval.v_number = VVAL_NULL;
3937#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003938 return OK;
3939 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003940 if (STRNCMP(s, "null_partial", 12) == 0)
3941 {
3942 rettv->v_type = VAR_PARTIAL;
3943 rettv->vval.v_partial = NULL;
3944 return OK;
3945 }
3946 break;
3947 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3948 {
3949 rettv->v_type = VAR_FUNC;
3950 rettv->vval.v_string = NULL;
3951 return OK;
3952 }
3953 break;
3954 }
3955 return FAIL;
3956}
3957
3958/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 * Handle sixth level expression:
3960 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003961 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003962 * "string" string constant
3963 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 * &option-name option value
3965 * @r register contents
3966 * identifier variable value
3967 * function() function call
3968 * $VAR environment variable
3969 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003970 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003972 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003973 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 *
3975 * Also handle:
3976 * ! in front logical NOT
3977 * - in front unary minus
3978 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003979 * trailing [] subscript in String or List
3980 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003981 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 *
3983 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003984 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 *
3986 * Return OK or FAIL.
3987 */
3988 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003989eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003990 char_u **arg,
3991 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003992 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003995 int evaluate = evalarg != NULL
3996 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 int len;
3998 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003999 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 char_u *start_leader, *end_leader;
4001 int ret = OK;
4002 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004003 static int recurse = 0;
4004 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005
4006 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004008 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004010 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011
4012 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004013 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 */
4015 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004016 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01004017 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 end_leader = *arg;
4019
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004020 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004021 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02004022 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02004023 ++*arg;
4024 return FAIL;
4025 }
4026
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004027 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00004028 // and crash. With MSVC the stack is smaller.
4029 if (recurse ==
4030#ifdef _MSC_VER
4031 300
4032#else
4033 1000
4034#endif
4035 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004036 {
4037 semsg(_(e_expression_too_recursive_str), *arg);
4038 return FAIL;
4039 }
4040 ++recurse;
4041
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 switch (**arg)
4043 {
4044 /*
4045 * Number constant.
4046 */
4047 case '0':
4048 case '1':
4049 case '2':
4050 case '3':
4051 case '4':
4052 case '5':
4053 case '6':
4054 case '7':
4055 case '8':
4056 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004057 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004058
4059 // Apply prefixed "-" and "+" now. Matters especially when
4060 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004061 if (ret == OK && evaluate && end_leader > start_leader
4062 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004063 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 break;
4065
4066 /*
4067 * String constant: "string".
4068 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004069 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004073 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01004075 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004076 break;
4077
4078 /*
4079 * List: [expr, expr]
4080 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004081 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 break;
4083
4084 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02004085 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004086 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004087 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01004088 {
4089 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
4090 }
4091 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004092 {
4093 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004094 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004095 }
4096 else
4097 ret = NOTDONE;
4098 break;
4099
4100 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004101 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02004102 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004103 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00004104 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004105 ret = NOTDONE;
4106 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00004107 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004108 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02004109 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004110 break;
4111
4112 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004113 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004115 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 break;
4117
4118 /*
4119 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01004120 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 */
LemonBoy2eaef102022-05-06 13:14:50 +01004122 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
4123 ret = eval_interp_string(arg, rettv, evaluate);
4124 else
4125 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 break;
4127
4128 /*
4129 * Register contents: @r.
4130 */
4131 case '@': ++*arg;
4132 if (evaluate)
4133 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004134 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004135 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004136 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02004137 emsg_invreg(**arg);
4138 else
4139 {
4140 rettv->v_type = VAR_STRING;
4141 rettv->vval.v_string = get_reg_contents(**arg,
4142 GREG_EXPR_SRC);
4143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 if (**arg != NUL)
4146 ++*arg;
4147 break;
4148
4149 /*
4150 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004151 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004153 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004154 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004155 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004156 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004157 if (ret == OK && evaluate)
4158 {
4159 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4160
Bram Moolenaara9931532021-06-12 15:58:16 +02004161 // Compile it here to get the return type. The return
4162 // type is optional, when it's missing use t_unknown.
4163 // This is recognized in compile_return().
4164 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4165 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004166 if (compile_def_function(ufunc, FALSE,
4167 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004168 {
4169 clear_tv(rettv);
4170 ret = FAIL;
4171 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004172 }
4173 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004174 if (ret == NOTDONE)
4175 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004176 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004177 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004178
Bram Moolenaar9215f012020-06-27 21:18:00 +02004179 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004180 if (**arg == ')')
4181 ++*arg;
4182 else if (ret == OK)
4183 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004184 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004185 clear_tv(rettv);
4186 ret = FAIL;
4187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 }
4189 break;
4190
Bram Moolenaar8c711452005-01-14 21:53:12 +00004191 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 break;
4193 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004194
4195 if (ret == NOTDONE)
4196 {
4197 /*
4198 * Must be a variable or function name.
4199 * Can also be a curly-braces kind of name: {expr}.
4200 */
4201 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004202 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004203 if (alias != NULL)
4204 s = alias;
4205
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004206 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004207 ret = FAIL;
4208 else
4209 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004210 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4211
Bram Moolenaar4525a572022-02-13 11:57:33 +00004212 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004213 {
4214 emsg(_(e_cannot_use_underscore_here));
4215 ret = FAIL;
4216 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004217 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004218 && s[0] == 's' && s[1] == ':')
4219 {
4220 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4221 ret = FAIL;
4222 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004223 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004224 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004225 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004226 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004227 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004228 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004229 else if (flags & EVAL_CONSTANT)
4230 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004231 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004232 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004233 // get the value of "true", "false", etc. or a variable
4234 ret = FAIL;
4235 if (vim9script)
4236 ret = handle_predefined(s, len, rettv);
4237 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004238 {
4239 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004240 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004241 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004242 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004243 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004244 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004245 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004246 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004247 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004248 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004249 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004250 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004251 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004252 }
4253
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004254 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4255 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004256 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004257 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258
4259 /*
4260 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4261 */
4262 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004263 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004264
4265 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004266 return ret;
4267}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004269/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004270 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004271 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004272 * Adjusts "end_leaderp" until it is at "start_leader".
4273 */
4274 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004275eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004276 typval_T *rettv,
4277 int numeric_only,
4278 char_u *start_leader,
4279 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004280{
4281 char_u *end_leader = *end_leaderp;
4282 int ret = OK;
4283 int error = FALSE;
4284 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004285 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004286 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004287 float_T f = 0.0;
4288
4289 if (rettv->v_type == VAR_FLOAT)
4290 f = rettv->vval.v_float;
4291 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004292 {
4293 while (VIM_ISWHITE(end_leader[-1]))
4294 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004295 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004296 val = tv2bool(rettv);
4297 else
4298 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004299 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004300 if (error)
4301 {
4302 clear_tv(rettv);
4303 ret = FAIL;
4304 }
4305 else
4306 {
4307 while (end_leader > start_leader)
4308 {
4309 --end_leader;
4310 if (*end_leader == '!')
4311 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004312 if (numeric_only)
4313 {
4314 ++end_leader;
4315 break;
4316 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004317 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004318 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004319 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004320 {
4321 rettv->v_type = VAR_BOOL;
4322 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4323 }
4324 else
4325 f = !f;
4326 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004327 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004328 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004329 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004330 type = VAR_BOOL;
4331 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004332 }
4333 else if (*end_leader == '-')
4334 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004335 if (rettv->v_type == VAR_FLOAT)
4336 f = -f;
4337 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004338 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004339 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004340 type = VAR_NUMBER;
4341 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004342 }
4343 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004344 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004347 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004349 else
4350 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004351 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004352 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004353 rettv->v_type = type;
4354 else
4355 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004356 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004359 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 return ret;
4361}
4362
4363/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004364 * Call the function referred to in "rettv".
4365 */
4366 static int
4367call_func_rettv(
4368 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004369 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004370 typval_T *rettv,
4371 int evaluate,
4372 dict_T *selfdict,
4373 typval_T *basetv)
4374{
4375 partial_T *pt = NULL;
4376 funcexe_T funcexe;
4377 typval_T functv;
4378 char_u *s;
4379 int ret;
4380
4381 // need to copy the funcref so that we can clear rettv
4382 if (evaluate)
4383 {
4384 functv = *rettv;
4385 rettv->v_type = VAR_UNKNOWN;
4386
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004387 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004388 if (functv.v_type == VAR_PARTIAL)
4389 {
4390 pt = functv.vval.v_partial;
4391 s = partial_name(pt);
4392 }
4393 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004394 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004395 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004396 if (s == NULL || *s == NUL)
4397 {
4398 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004399 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004400 goto theend;
4401 }
4402 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004403 }
4404 else
4405 s = (char_u *)"";
4406
Bram Moolenaara80faa82020-04-12 19:37:17 +02004407 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004408 funcexe.fe_firstline = curwin->w_cursor.lnum;
4409 funcexe.fe_lastline = curwin->w_cursor.lnum;
4410 funcexe.fe_evaluate = evaluate;
4411 funcexe.fe_partial = pt;
4412 funcexe.fe_selfdict = selfdict;
4413 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004414 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004415
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004416theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004417 // Clear the funcref afterwards, so that deleting it while
4418 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004419 if (evaluate)
4420 clear_tv(&functv);
4421
4422 return ret;
4423}
4424
4425/*
4426 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004427 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004428 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4429 */
4430 static int
4431eval_lambda(
4432 char_u **arg,
4433 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004434 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004435 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004436{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004437 int evaluate = evalarg != NULL
4438 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004439 typval_T base = *rettv;
4440 int ret;
4441
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004442 rettv->v_type = VAR_UNKNOWN;
4443
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004444 if (**arg == '{')
4445 {
4446 // ->{lambda}()
4447 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4448 }
4449 else
4450 {
4451 // ->(lambda)()
4452 ++*arg;
4453 ret = eval1(arg, rettv, evalarg);
4454 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004455 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004456 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004457 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004458 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004459 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004460 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4461 && rettv->v_type != VAR_PARTIAL)
4462 {
4463 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4464 return FAIL;
4465 }
4466 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004467 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004468 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004469 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004470
4471 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004472 {
4473 if (verbose)
4474 {
4475 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004476 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004477 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004478 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004479 }
4480 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004481 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004482 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004483 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004484 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004485
4486 // Clear the funcref afterwards, so that deleting it while
4487 // evaluating the arguments is possible (see test55).
4488 if (evaluate)
4489 clear_tv(&base);
4490
4491 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004492}
4493
4494/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004495 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004496 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004497 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4498 */
4499 static int
4500eval_method(
4501 char_u **arg,
4502 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004503 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004504 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004505{
4506 char_u *name;
4507 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004508 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004509 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004510 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004511 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004512 int evaluate = evalarg != NULL
4513 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004514
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004515 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004516
Bram Moolenaarac92e252019-08-03 21:58:38 +02004517 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004518 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004519 if (alias != NULL)
4520 name = alias;
4521
4522 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004523 {
4524 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004525 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004526 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004527 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004528 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004529 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004530 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004531
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004532 // If there is no "(" immediately following, but there is further on,
4533 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4534 // Does not handle anything where "(" is part of the expression.
4535 *arg = skipwhite(*arg);
4536
4537 if (**arg != '(' && alias == NULL
4538 && (paren = vim_strchr(*arg, '(')) != NULL)
4539 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004540 char_u *deref;
4541
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004542 *arg = name;
4543 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004544 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4545 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004546 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004547 *arg = name + len;
4548 ret = FAIL;
4549 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004550 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004551 {
4552 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004553 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004554 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004555 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004556 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004557
4558 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004559 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004560 *arg = skipwhite(*arg);
4561
4562 if (**arg != '(')
4563 {
4564 if (verbose)
4565 semsg(_(e_missing_parenthesis_str), name);
4566 ret = FAIL;
4567 }
4568 else if (VIM_ISWHITE((*arg)[-1]))
4569 {
4570 if (verbose)
4571 emsg(_(e_no_white_space_allowed_before_parenthesis));
4572 ret = FAIL;
4573 }
4574 else
4575 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004576 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004577 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004578 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004579
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004580 // Clear the funcref afterwards, so that deleting it while
4581 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004582 if (evaluate)
4583 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004584 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004585
Bram Moolenaarac92e252019-08-03 21:58:38 +02004586 return ret;
4587}
4588
4589/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004590 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4591 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004592 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4593 */
4594 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004595eval_index(
4596 char_u **arg,
4597 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004598 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004599 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004600{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004601 int evaluate = evalarg != NULL
4602 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004603 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004605 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004606 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004607 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004608 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004610 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4611 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004613 init_tv(&var1);
4614 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004615 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004616 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004617 /*
4618 * dict.name
4619 */
4620 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004621 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004622 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004623 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004625 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004626 }
4627 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004628 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004629 /*
4630 * something[idx]
4631 *
4632 * Get the (first) variable from inside the [].
4633 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004634 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004635 if (**arg == ':')
4636 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004637 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004638 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004639 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004640 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004641 semsg(_(e_white_space_required_before_and_after_str_at_str),
4642 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004643 clear_tv(&var1);
4644 return FAIL;
4645 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004646 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004647 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004648 int error = FALSE;
4649
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004650 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004651 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004652 && var1.v_type == VAR_FLOAT)
4653 {
4654 var1.vval.v_string = typval_tostring(&var1, TRUE);
4655 var1.v_type = VAR_STRING;
4656 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004657
Bram Moolenaar4525a572022-02-13 11:57:33 +00004658 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004659 tv_get_number_chk(&var1, &error);
4660 else
4661 error = tv_get_string_chk(&var1) == NULL;
4662 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004663 {
4664 // not a number or string
4665 clear_tv(&var1);
4666 return FAIL;
4667 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004669
4670 /*
4671 * Get the second variable from inside the [:].
4672 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004673 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004674 if (**arg == ':')
4675 {
4676 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004677 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004678 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004679 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004680 semsg(_(e_white_space_required_before_and_after_str_at_str),
4681 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004682 if (!empty1)
4683 clear_tv(&var1);
4684 return FAIL;
4685 }
4686 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004687 if (**arg == ']')
4688 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004689 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004690 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004691 if (!empty1)
4692 clear_tv(&var1);
4693 return FAIL;
4694 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004695 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004696 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004697 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004698 if (!empty1)
4699 clear_tv(&var1);
4700 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004701 return FAIL;
4702 }
4703 }
4704
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004705 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004706 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004707 if (**arg != ']')
4708 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004709 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004710 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004711 clear_tv(&var1);
4712 if (range)
4713 clear_tv(&var2);
4714 return FAIL;
4715 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004716 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004717 }
4718
4719 if (evaluate)
4720 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004721 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004722 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004723 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004724
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004725 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004726 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004728 clear_tv(&var2);
4729 return res;
4730 }
4731 return OK;
4732}
4733
4734/*
4735 * Check if "rettv" can have an [index] or [sli:ce]
4736 */
4737 int
4738check_can_index(typval_T *rettv, int evaluate, int verbose)
4739{
4740 switch (rettv->v_type)
4741 {
4742 case VAR_FUNC:
4743 case VAR_PARTIAL:
4744 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004745 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004746 return FAIL;
4747 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004748 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004749 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004750 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004751 case VAR_BOOL:
4752 case VAR_SPECIAL:
4753 case VAR_JOB:
4754 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004755 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004756 case VAR_CLASS:
4757 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004758 if (verbose)
4759 emsg(_(e_cannot_index_special_variable));
4760 return FAIL;
4761 case VAR_UNKNOWN:
4762 case VAR_ANY:
4763 case VAR_VOID:
4764 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004765 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004766 emsg(_(e_cannot_index_special_variable));
4767 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004768 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004769 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004770
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004771 case VAR_STRING:
4772 case VAR_LIST:
4773 case VAR_DICT:
4774 case VAR_BLOB:
4775 break;
4776 case VAR_NUMBER:
4777 if (in_vim9script())
4778 emsg(_(e_cannot_index_number));
4779 break;
4780 }
4781 return OK;
4782}
4783
4784/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004785 * slice() function
4786 */
4787 void
4788f_slice(typval_T *argvars, typval_T *rettv)
4789{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004790 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004791 && ((argvars[0].v_type != VAR_STRING
4792 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004793 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004794 && check_for_list_arg(argvars, 0) == FAIL)
4795 || check_for_number_arg(argvars, 1) == FAIL
4796 || check_for_opt_number_arg(argvars, 2) == FAIL))
4797 return;
4798
Bram Moolenaar6601b622021-01-13 21:47:15 +01004799 if (check_can_index(argvars, TRUE, FALSE) == OK)
4800 {
4801 copy_tv(argvars, rettv);
4802 eval_index_inner(rettv, TRUE, argvars + 1,
4803 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4804 TRUE, NULL, 0, FALSE);
4805 }
4806}
4807
4808/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004809 * Apply index or range to "rettv".
4810 * "var1" is the first index, NULL for [:expr].
4811 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004812 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4813 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004814 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4815 */
4816 int
4817eval_index_inner(
4818 typval_T *rettv,
4819 int is_range,
4820 typval_T *var1,
4821 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004822 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004823 char_u *key,
4824 int keylen,
4825 int verbose)
4826{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004827 varnumber_T n1, n2 = 0;
4828 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004829
4830 n1 = 0;
4831 if (var1 != NULL && rettv->v_type != VAR_DICT)
4832 n1 = tv_get_number(var1);
4833
4834 if (is_range)
4835 {
4836 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004837 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004838 if (verbose)
4839 emsg(_(e_cannot_slice_dictionary));
4840 return FAIL;
4841 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004842 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004843 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004844 else
4845 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004846 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004847
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004848 switch (rettv->v_type)
4849 {
4850 case VAR_UNKNOWN:
4851 case VAR_ANY:
4852 case VAR_VOID:
4853 case VAR_FUNC:
4854 case VAR_PARTIAL:
4855 case VAR_FLOAT:
4856 case VAR_BOOL:
4857 case VAR_SPECIAL:
4858 case VAR_JOB:
4859 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004860 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00004861 case VAR_CLASS:
4862 case VAR_OBJECT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004863 break; // not evaluating, skipping over subscript
4864
4865 case VAR_NUMBER:
4866 case VAR_STRING:
4867 {
4868 char_u *s = tv_get_string(rettv);
4869
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004871 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004872 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004873 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004874 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004875 else
4876 s = char_from_string(s, n1);
4877 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004878 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004879 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004880 // The resulting variable is a substring. If the indexes
4881 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004882 if (n1 < 0)
4883 {
4884 n1 = len + n1;
4885 if (n1 < 0)
4886 n1 = 0;
4887 }
4888 if (n2 < 0)
4889 n2 = len + n2;
4890 else if (n2 >= len)
4891 n2 = len;
4892 if (n1 >= len || n2 < 0 || n1 > n2)
4893 s = NULL;
4894 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004895 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004896 }
4897 else
4898 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004899 // The resulting variable is a string of a single
4900 // character. If the index is too big or negative the
4901 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004902 if (n1 >= len || n1 < 0)
4903 s = NULL;
4904 else
4905 s = vim_strnsave(s + n1, 1);
4906 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004907 clear_tv(rettv);
4908 rettv->v_type = VAR_STRING;
4909 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004910 }
4911 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004912
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004913 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004914 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4915 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004916 break;
4917
4918 case VAR_LIST:
4919 if (var1 == NULL)
4920 n1 = 0;
4921 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004922 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004923 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004924 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004925 return FAIL;
4926 break;
4927
4928 case VAR_DICT:
4929 {
4930 dictitem_T *item;
4931 typval_T tmp;
4932
4933 if (key == NULL)
4934 {
4935 key = tv_get_string_chk(var1);
4936 if (key == NULL)
4937 return FAIL;
4938 }
4939
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004940 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004941
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004942 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004943 {
4944 if (verbose)
4945 {
4946 if (keylen > 0)
4947 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004948 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004949 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004950 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004951 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004952
4953 copy_tv(&item->di_tv, &tmp);
4954 clear_tv(rettv);
4955 *rettv = tmp;
4956 }
4957 break;
4958 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004959 return OK;
4960}
4961
4962/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004963 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004964 */
4965 char_u *
4966partial_name(partial_T *pt)
4967{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004968 if (pt != NULL)
4969 {
4970 if (pt->pt_name != NULL)
4971 return pt->pt_name;
4972 if (pt->pt_func != NULL)
4973 return pt->pt_func->uf_name;
4974 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004975 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004976}
4977
Bram Moolenaarddecc252016-04-06 22:59:37 +02004978 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004979partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004980{
4981 int i;
4982
4983 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004984 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004985 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004986 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004987 if (pt->pt_name != NULL)
4988 {
4989 func_unref(pt->pt_name);
4990 vim_free(pt->pt_name);
4991 }
4992 else
4993 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004994
Bram Moolenaar54656012021-06-09 20:50:46 +02004995 // "out_up" is no longer used, decrement refcount on partial that owns it.
4996 partial_unref(pt->pt_outer.out_up_partial);
4997
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004998 // Using pt_outer from another partial.
4999 partial_unref(pt->pt_outer_partial);
5000
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005001 // Decrease the reference count for the context of a closure. If down
5002 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005003 if (pt->pt_funcstack != NULL)
5004 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005005 --pt->pt_funcstack->fs_refcount;
5006 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005007 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005008 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01005009 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
5010 if (pt->pt_loopvars[i] != NULL)
5011 {
5012 --pt->pt_loopvars[i]->lvs_refcount;
5013 loopvars_check_refcount(pt->pt_loopvars[i]);
5014 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02005015
Bram Moolenaarddecc252016-04-06 22:59:37 +02005016 vim_free(pt);
5017}
5018
5019/*
5020 * Unreference a closure: decrement the reference count and free it when it
5021 * becomes zero.
5022 */
5023 void
5024partial_unref(partial_T *pt)
5025{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005026 if (pt != NULL)
5027 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005028 int done = FALSE;
5029
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005030 if (--pt->pt_refcount <= 0)
5031 partial_free(pt);
5032
5033 // If the reference count goes down to one, the funcstack may be the
5034 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005035 else if (pt->pt_refcount == 1)
5036 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005037 // careful: if the funcstack is freed it may contain this partial
5038 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005039 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01005040 done = funcstack_check_refcount(pt->pt_funcstack);
5041
Bram Moolenaarcc341812022-09-19 15:54:34 +01005042 if (!done)
5043 {
5044 int depth;
5045
5046 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
5047 if (pt->pt_loopvars[depth] != NULL
5048 && loopvars_check_refcount(pt->pt_loopvars[depth]))
5049 break;
5050 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005051 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02005052 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02005053}
5054
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005055/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005056 * Return the next (unique) copy ID.
5057 * Used for serializing nested structures.
5058 */
5059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005060get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005061{
5062 current_copyID += COPYID_INC;
5063 return current_copyID;
5064}
5065
5066/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005067 * Garbage collection for lists and dictionaries.
5068 *
5069 * We use reference counts to be able to free most items right away when they
5070 * are no longer used. But for composite items it's possible that it becomes
5071 * unused while the reference count is > 0: When there is a recursive
5072 * reference. Example:
5073 * :let l = [1, 2, 3]
5074 * :let d = {9: l}
5075 * :let l[1] = d
5076 *
5077 * Since this is quite unusual we handle this with garbage collection: every
5078 * once in a while find out which lists and dicts are not referenced from any
5079 * variable.
5080 *
5081 * Here is a good reference text about garbage collection (refers to Python
5082 * but it applies to all reference-counting mechanisms):
5083 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005084 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005085
5086/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005087 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005088 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005089 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005090 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005091 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005092garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005093{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005094 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005095 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005096 buf_T *buf;
5097 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005098 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005099 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005100
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005101 if (!testing)
5102 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005103 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005104 want_garbage_collect = FALSE;
5105 may_garbage_collect = FALSE;
5106 garbage_collect_at_exit = FALSE;
5107 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005108
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005109 // The execution stack can grow big, limit the size.
5110 if (exestack.ga_maxlen - exestack.ga_len > 500)
5111 {
5112 size_t new_len;
5113 char_u *pp;
5114 int n;
5115
5116 // Keep 150% of the current size, with a minimum of the growth size.
5117 n = exestack.ga_len / 2;
5118 if (n < exestack.ga_growsize)
5119 n = exestack.ga_growsize;
5120
5121 // Don't make it bigger though.
5122 if (exestack.ga_len + n < exestack.ga_maxlen)
5123 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00005124 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01005125 pp = vim_realloc(exestack.ga_data, new_len);
5126 if (pp == NULL)
5127 return FAIL;
5128 exestack.ga_maxlen = exestack.ga_len + n;
5129 exestack.ga_data = pp;
5130 }
5131 }
5132
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005133 // We advance by two because we add one for items referenced through
5134 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005135 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005136
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005137 /*
5138 * 1. Go through all accessible variables and mark all lists and dicts
5139 * with copyID.
5140 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005141
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005142 // Don't free variables in the previous_funccal list unless they are only
5143 // referenced through previous_funccal. This must be first, because if
5144 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005145 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005147 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005148 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005149
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005150 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005151 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005152 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5153 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005154
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005155 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005156 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005157 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5158 NULL, NULL);
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005159 // window-local variables in autocmd windows
Bram Moolenaare76062c2022-11-28 18:51:43 +00005160 for (int i = 0; i < AUCMD_WIN_COUNT; ++i)
Bram Moolenaar84497cd2022-11-28 20:34:52 +00005161 if (aucmd_win[i].auc_win != NULL)
Bram Moolenaare76062c2022-11-28 18:51:43 +00005162 abort = abort || set_ref_in_item(
5163 &aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005164#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005165 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005166 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5167 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005168 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005169 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005170 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5171 NULL, NULL);
5172#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005173
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005174 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005175 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005176 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5177 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005178 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005179 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005180
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005181 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005182 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005183
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005184 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005185 abort = abort || set_ref_in_functions(copyID);
5186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005187 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005188 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005189
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005190 // funcstacks keep variables for closures
5191 abort = abort || set_ref_in_funcstacks(copyID);
5192
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005193 // loopvars keep variables for loop blocks
5194 abort = abort || set_ref_in_loopvars(copyID);
5195
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005196 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005197 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005198
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005199 // callbacks in buffers
5200 abort = abort || set_ref_in_buffers(copyID);
5201
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005202 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5203 abort = abort || set_ref_in_insexpand_funcs(copyID);
5204
5205 // 'operatorfunc' callback
5206 abort = abort || set_ref_in_opfunc(copyID);
5207
5208 // 'tagfunc' callback
5209 abort = abort || set_ref_in_tagfunc(copyID);
5210
5211 // 'imactivatefunc' and 'imstatusfunc' callbacks
5212 abort = abort || set_ref_in_im_funcs(copyID);
5213
Bram Moolenaar1dced572012-04-05 16:54:08 +02005214#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005215 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005216#endif
5217
Bram Moolenaardb913952012-06-29 12:54:53 +02005218#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005219 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005220#endif
5221
5222#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005223 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005224#endif
5225
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005226#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005227 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005228 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005229#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005230#ifdef FEAT_NETBEANS_INTG
5231 abort = abort || set_ref_in_nb_channel(copyID);
5232#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005233
Bram Moolenaare3188e22016-05-31 21:13:04 +02005234#ifdef FEAT_TIMERS
5235 abort = abort || set_ref_in_timer(copyID);
5236#endif
5237
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005238#ifdef FEAT_QUICKFIX
5239 abort = abort || set_ref_in_quickfix(copyID);
5240#endif
5241
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005242#ifdef FEAT_TERMINAL
5243 abort = abort || set_ref_in_term(copyID);
5244#endif
5245
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005246#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005247 abort = abort || set_ref_in_popups(copyID);
5248#endif
5249
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005250 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005251 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 /*
5253 * 2. Free lists and dictionaries that are not referenced.
5254 */
5255 did_free = free_unref_items(copyID);
5256
5257 /*
5258 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005259 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005260 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005261 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005262 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005263 else if (p_verbose > 0)
5264 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005265 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005266 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005267
5268 return did_free;
5269}
5270
5271/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005272 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005273 */
5274 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005275free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005276{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005277 int did_free = FALSE;
5278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005279 // Let all "free" functions know that we are here. This means no
5280 // dictionaries, lists, channels or jobs are to be freed, because we will
5281 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005282 in_free_unref_items = TRUE;
5283
5284 /*
5285 * PASS 1: free the contents of the items. We don't free the items
5286 * themselves yet, so that it is possible to decrement refcount counters
5287 */
5288
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005289 // Go through the list of dicts and free items without this copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005290 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005291
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005292 // Go through the list of lists and free items without this copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005293 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005294
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005295 // Go through the list of objects and free items without this copyID.
5296 did_free |= object_free_nonref(copyID);
5297
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005298#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005299 // Go through the list of jobs and free items without the copyID. This
5300 // must happen before doing channels, because jobs refer to channels, but
5301 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005302 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005304 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005305 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5306#endif
5307
5308 /*
5309 * PASS 2: free the items themselves.
5310 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005311 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005312 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005313
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005314#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005315 // Go through the list of jobs and free items without the copyID. This
5316 // must happen before doing channels, because jobs refer to channels, but
5317 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005318 free_unused_jobs(copyID, COPYID_MASK);
5319
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005320 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005321 free_unused_channels(copyID, COPYID_MASK);
5322#endif
5323
5324 in_free_unref_items = FALSE;
5325
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005326 return did_free;
5327}
5328
5329/*
5330 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005331 * "list_stack" is used to add lists to be marked. Can be NULL.
5332 *
5333 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005334 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005336set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005337{
5338 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005339 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005340 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005341 hashtab_T *cur_ht;
5342 ht_stack_T *ht_stack = NULL;
5343 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005344
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005345 cur_ht = ht;
5346 for (;;)
5347 {
5348 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005349 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005350 // Mark each item in the hashtab. If the item contains a hashtab
5351 // it is added to ht_stack, if it contains a list it is added to
5352 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005353 todo = (int)cur_ht->ht_used;
5354 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5355 if (!HASHITEM_EMPTY(hi))
5356 {
5357 --todo;
5358 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5359 &ht_stack, list_stack);
5360 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005361 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005362
5363 if (ht_stack == NULL)
5364 break;
5365
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005366 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005367 cur_ht = ht_stack->ht;
5368 tempitem = ht_stack;
5369 ht_stack = ht_stack->prev;
5370 free(tempitem);
5371 }
5372
5373 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005374}
5375
Dominique Pelle748b3082022-01-08 12:41:16 +00005376#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5377 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005378/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005379 * Mark a dict and its items with "copyID".
5380 * Returns TRUE if setting references failed somehow.
5381 */
5382 int
5383set_ref_in_dict(dict_T *d, int copyID)
5384{
5385 if (d != NULL && d->dv_copyID != copyID)
5386 {
5387 d->dv_copyID = copyID;
5388 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5389 }
5390 return FALSE;
5391}
Dominique Pelle748b3082022-01-08 12:41:16 +00005392#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005393
5394/*
5395 * Mark a list and its items with "copyID".
5396 * Returns TRUE if setting references failed somehow.
5397 */
5398 int
5399set_ref_in_list(list_T *ll, int copyID)
5400{
5401 if (ll != NULL && ll->lv_copyID != copyID)
5402 {
5403 ll->lv_copyID = copyID;
5404 return set_ref_in_list_items(ll, copyID, NULL);
5405 }
5406 return FALSE;
5407}
5408
5409/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005410 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005411 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5412 *
5413 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005414 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005415 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005416set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005417{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005418 listitem_T *li;
5419 int abort = FALSE;
5420 list_T *cur_l;
5421 list_stack_T *list_stack = NULL;
5422 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005423
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 cur_l = l;
5425 for (;;)
5426 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005427 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005428 // Mark each item in the list. If the item contains a hashtab
5429 // it is added to ht_stack, if it contains a list it is added to
5430 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005431 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5432 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5433 ht_stack, &list_stack);
5434 if (list_stack == NULL)
5435 break;
5436
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005437 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005438 cur_l = list_stack->list;
5439 tempitem = list_stack;
5440 list_stack = list_stack->prev;
5441 free(tempitem);
5442 }
5443
5444 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005445}
5446
5447/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005448 * Mark the partial in callback 'cb' with "copyID".
5449 */
5450 int
5451set_ref_in_callback(callback_T *cb, int copyID)
5452{
5453 typval_T tv;
5454
5455 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5456 return FALSE;
5457
5458 tv.v_type = VAR_PARTIAL;
5459 tv.vval.v_partial = cb->cb_partial;
5460 return set_ref_in_item(&tv, copyID, NULL, NULL);
5461}
5462
5463/*
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005464 * Mark all lists, dicts and other container types referenced through typval
5465 * "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005466 * "list_stack" is used to add lists to be marked. Can be NULL.
5467 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5468 *
5469 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005470 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005471 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005472set_ref_in_item(
5473 typval_T *tv,
5474 int copyID,
5475 ht_stack_T **ht_stack,
5476 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005477{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005478 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005479
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005480 switch (tv->v_type)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005481 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005482 case VAR_DICT:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005484 dict_T *dd = tv->vval.v_dict;
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005485
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005486 if (dd != NULL && dd->dv_copyID != copyID)
5487 {
5488 // Didn't see this dict yet.
5489 dd->dv_copyID = copyID;
5490 if (ht_stack == NULL)
5491 {
5492 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5493 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005494 else
5495 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005496 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005497
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005498 if (newitem == NULL)
5499 abort = TRUE;
5500 else
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005501 {
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005502 newitem->ht = &dd->dv_hashtab;
5503 newitem->prev = *ht_stack;
5504 *ht_stack = newitem;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005505 }
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005506 }
5507 }
5508 break;
5509 }
5510
5511 case VAR_LIST:
5512 {
5513 list_T *ll = tv->vval.v_list;
5514
5515 if (ll != NULL && ll->lv_copyID != copyID)
5516 {
5517 // Didn't see this list yet.
5518 ll->lv_copyID = copyID;
5519 if (list_stack == NULL)
5520 {
5521 abort = set_ref_in_list_items(ll, copyID, ht_stack);
5522 }
5523 else
5524 {
5525 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5526
5527 if (newitem == NULL)
5528 abort = TRUE;
5529 else
5530 {
5531 newitem->list = ll;
5532 newitem->prev = *list_stack;
5533 *list_stack = newitem;
5534 }
5535 }
5536 }
5537 break;
5538 }
5539
5540 case VAR_FUNC:
5541 {
5542 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
5543 break;
5544 }
5545
5546 case VAR_PARTIAL:
5547 {
5548 partial_T *pt = tv->vval.v_partial;
5549 int i;
5550
5551 if (pt != NULL && pt->pt_copyID != copyID)
5552 {
5553 // Didn't see this partial yet.
5554 pt->pt_copyID = copyID;
5555
5556 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
5557
5558 if (pt->pt_dict != NULL)
5559 {
5560 typval_T dtv;
5561
5562 dtv.v_type = VAR_DICT;
5563 dtv.vval.v_dict = pt->pt_dict;
5564 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5565 }
5566
5567 for (i = 0; i < pt->pt_argc; ++i)
5568 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5569 ht_stack, list_stack);
5570 // pt_funcstack is handled in set_ref_in_funcstacks()
5571 // pt_loopvars is handled in set_ref_in_loopvars()
5572 }
5573 break;
5574 }
5575
5576 case VAR_JOB:
5577 {
5578#ifdef FEAT_JOB_CHANNEL
5579 job_T *job = tv->vval.v_job;
5580 typval_T dtv;
5581
5582 if (job != NULL && job->jv_copyID != copyID)
5583 {
5584 job->jv_copyID = copyID;
5585 if (job->jv_channel != NULL)
5586 {
5587 dtv.v_type = VAR_CHANNEL;
5588 dtv.vval.v_channel = job->jv_channel;
5589 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5590 }
5591 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005592 {
5593 dtv.v_type = VAR_PARTIAL;
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005594 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005595 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5596 }
5597 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005598#endif
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005599 break;
5600 }
5601
5602 case VAR_CHANNEL:
5603 {
5604#ifdef FEAT_JOB_CHANNEL
5605 channel_T *ch = tv->vval.v_channel;
5606 ch_part_T part;
5607 typval_T dtv;
5608 jsonq_T *jq;
5609 cbq_T *cq;
5610
5611 if (ch != NULL && ch->ch_copyID != copyID)
5612 {
5613 ch->ch_copyID = copyID;
5614 for (part = PART_SOCK; part < PART_COUNT; ++part)
5615 {
5616 for (jq = ch->ch_part[part].ch_json_head.jq_next;
5617 jq != NULL; jq = jq->jq_next)
5618 set_ref_in_item(jq->jq_value, copyID,
5619 ht_stack, list_stack);
5620 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5621 cq = cq->cq_next)
5622 if (cq->cq_callback.cb_partial != NULL)
5623 {
5624 dtv.v_type = VAR_PARTIAL;
5625 dtv.vval.v_partial = cq->cq_callback.cb_partial;
5626 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5627 }
5628 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
5629 {
5630 dtv.v_type = VAR_PARTIAL;
5631 dtv.vval.v_partial =
5632 ch->ch_part[part].ch_callback.cb_partial;
5633 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5634 }
5635 }
5636 if (ch->ch_callback.cb_partial != NULL)
5637 {
5638 dtv.v_type = VAR_PARTIAL;
5639 dtv.vval.v_partial = ch->ch_callback.cb_partial;
5640 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5641 }
5642 if (ch->ch_close_cb.cb_partial != NULL)
5643 {
5644 dtv.v_type = VAR_PARTIAL;
5645 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
5646 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5647 }
5648 }
5649#endif
5650 break;
5651 }
5652
5653 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005654 // TODO: Mark methods in class_obj_methods ?
5655 // Mark initializer expressions?
Bram Moolenaard28d7b92022-12-08 20:42:00 +00005656 break;
5657
5658 case VAR_OBJECT:
5659 {
5660 object_T *obj = tv->vval.v_object;
5661 if (obj != NULL && obj->obj_copyID != copyID)
5662 {
5663 obj->obj_copyID = copyID;
5664
5665 // The typval_T array is right after the object_T.
5666 typval_T *mtv = (typval_T *)(obj + 1);
5667 for (int i = 0; !abort
5668 && i < obj->obj_class->class_obj_member_count; ++i)
5669 abort = abort || set_ref_in_item(mtv + i, copyID,
5670 ht_stack, list_stack);
5671 }
5672 break;
5673 }
5674
5675 case VAR_UNKNOWN:
5676 case VAR_ANY:
5677 case VAR_VOID:
5678 case VAR_BOOL:
5679 case VAR_SPECIAL:
5680 case VAR_NUMBER:
5681 case VAR_FLOAT:
5682 case VAR_STRING:
5683 case VAR_BLOB:
5684 case VAR_INSTR:
5685 // Types that do not contain any other item
5686 break;
5687 }
5688
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005689 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005690}
5691
Bram Moolenaar8c711452005-01-14 21:53:12 +00005692/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005693 * Return a string with the string representation of a variable.
5694 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005695 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005696 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005697 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005698 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005699 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005700 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5701 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005702 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005703 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005704 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005705echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005706 typval_T *tv,
5707 char_u **tofree,
5708 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005709 int copyID,
5710 int echo_style,
5711 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005712 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005713{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005714 static int recurse = 0;
5715 char_u *r = NULL;
5716
Bram Moolenaar33570922005-01-25 22:26:29 +00005717 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005718 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005719 if (!did_echo_string_emsg)
5720 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005721 // Only give this message once for a recursive call to avoid
5722 // flooding the user with errors. And stop iterating over lists
5723 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005724 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005725 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005726 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005727 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005728 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005729 }
5730 ++recurse;
5731
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005732 switch (tv->v_type)
5733 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005734 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005735 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005736 {
5737 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005738 r = tv->vval.v_string;
5739 if (r == NULL)
5740 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005741 }
5742 else
5743 {
5744 *tofree = string_quote(tv->vval.v_string, FALSE);
5745 r = *tofree;
5746 }
5747 break;
5748
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005749 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005750 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005751 char_u buf[MAX_FUNC_NAME_LEN];
5752
5753 if (echo_style)
5754 {
LemonBoya5d35902022-04-29 21:15:02 +01005755 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5756 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005757 buf, MAX_FUNC_NAME_LEN);
5758 if (r == buf)
5759 {
5760 r = vim_strsave(buf);
5761 *tofree = r;
5762 }
5763 else
5764 *tofree = NULL;
5765 }
5766 else
5767 {
5768 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5769 : make_ufunc_name_readable(
5770 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5771 TRUE);
5772 r = *tofree;
5773 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005774 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005775 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005776
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005777 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005778 {
5779 partial_T *pt = tv->vval.v_partial;
5780 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005781 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005782 garray_T ga;
5783 int i;
5784 char_u *tf;
5785
5786 ga_init2(&ga, 1, 100);
5787 ga_concat(&ga, (char_u *)"function(");
5788 if (fname != NULL)
5789 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005790 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005791 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005792 && vim_isupper(fname[1]))
5793 {
5794 ga_concat(&ga, (char_u *)"'g:");
5795 ga_concat(&ga, fname + 1);
5796 }
5797 else
5798 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005799 vim_free(fname);
5800 }
5801 if (pt != NULL && pt->pt_argc > 0)
5802 {
5803 ga_concat(&ga, (char_u *)", [");
5804 for (i = 0; i < pt->pt_argc; ++i)
5805 {
5806 if (i > 0)
5807 ga_concat(&ga, (char_u *)", ");
5808 ga_concat(&ga,
5809 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5810 vim_free(tf);
5811 }
5812 ga_concat(&ga, (char_u *)"]");
5813 }
5814 if (pt != NULL && pt->pt_dict != NULL)
5815 {
5816 typval_T dtv;
5817
5818 ga_concat(&ga, (char_u *)", ");
5819 dtv.v_type = VAR_DICT;
5820 dtv.vval.v_dict = pt->pt_dict;
5821 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5822 vim_free(tf);
5823 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005824 // terminate with ')' and a NUL
5825 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005826
5827 *tofree = ga.ga_data;
5828 r = *tofree;
5829 break;
5830 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005831
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005832 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005833 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005834 break;
5835
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005837 if (tv->vval.v_list == NULL)
5838 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005839 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005840 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005841 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005842 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005843 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5844 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005845 {
5846 *tofree = NULL;
5847 r = (char_u *)"[...]";
5848 }
5849 else
5850 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005851 int old_copyID = tv->vval.v_list->lv_copyID;
5852
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005853 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005854 *tofree = list2string(tv, copyID, restore_copyID);
5855 if (restore_copyID)
5856 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005857 r = *tofree;
5858 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005859 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005860
Bram Moolenaar8c711452005-01-14 21:53:12 +00005861 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005862 if (tv->vval.v_dict == NULL)
5863 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005864 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005865 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005866 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005867 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005868 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5869 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005870 {
5871 *tofree = NULL;
5872 r = (char_u *)"{...}";
5873 }
5874 else
5875 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005876 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005877
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005878 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005879 *tofree = dict2string(tv, copyID, restore_copyID);
5880 if (restore_copyID)
5881 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005882 r = *tofree;
5883 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005884 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005885
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005886 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005887 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005888 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005889 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005890 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005891 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005892 break;
5893
Bram Moolenaar835dc632016-02-07 14:27:38 +01005894 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005895 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005896#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005897 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005898 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5899 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005900 if (composite_val)
5901 {
5902 *tofree = string_quote(r, FALSE);
5903 r = *tofree;
5904 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005905#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005907
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005908 case VAR_INSTR:
5909 *tofree = NULL;
5910 r = (char_u *)"instructions";
5911 break;
5912
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005913 case VAR_CLASS:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005914 {
5915 class_T *cl = tv->vval.v_class;
5916 size_t len = 6 + (cl == NULL ? 9 : STRLEN(cl->class_name)) + 1;
5917 r = *tofree = alloc(len);
5918 vim_snprintf((char *)r, len, "class %s",
5919 cl == NULL ? "[unknown]" : (char *)cl->class_name);
5920 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005921 break;
5922
5923 case VAR_OBJECT:
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005924 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005925 garray_T ga;
5926 ga_init2(&ga, 1, 50);
5927 ga_concat(&ga, (char_u *)"object of ");
5928 object_T *obj = tv->vval.v_object;
5929 class_T *cl = obj == NULL ? NULL : obj->obj_class;
5930 ga_concat(&ga, cl == NULL ? (char_u *)"[unknown]"
5931 : cl->class_name);
5932 if (cl != NULL)
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005933 {
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005934 ga_concat(&ga, (char_u *)" {");
5935 for (int i = 0; i < cl->class_obj_member_count; ++i)
5936 {
5937 if (i > 0)
5938 ga_concat(&ga, (char_u *)", ");
5939 objmember_T *m = &cl->class_obj_members[i];
5940 ga_concat(&ga, m->om_name);
5941 ga_concat(&ga, (char_u *)": ");
5942 char_u *tf = NULL;
5943 ga_concat(&ga, echo_string_core(
5944 (typval_T *)(obj + 1) + i,
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005945 &tf, numbuf, copyID, echo_style,
5946 restore_copyID, composite_val));
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005947 vim_free(tf);
5948 }
5949 ga_concat(&ga, (char_u *)"}");
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005950 }
Bram Moolenaar91c9d6d2022-12-14 17:30:37 +00005951
Bram Moolenaarf94178db2022-12-14 17:50:00 +00005952 *tofree = r = ga.ga_data;
5953 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00005954 break;
5955
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005956 case VAR_FLOAT:
5957 *tofree = NULL;
5958 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5959 r = numbuf;
5960 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005961
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005962 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005963 case VAR_SPECIAL:
5964 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005965 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005966 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005967 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005968
Bram Moolenaar8502c702014-06-17 12:51:16 +02005969 if (--recurse == 0)
5970 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005971 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005972}
5973
5974/*
5975 * Return a string with the string representation of a variable.
5976 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5977 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005978 * Does not put quotes around strings, as ":echo" displays values.
5979 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5980 * May return NULL.
5981 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005982 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005983echo_string(
5984 typval_T *tv,
5985 char_u **tofree,
5986 char_u *numbuf,
5987 int copyID)
5988{
5989 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5990}
5991
5992/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005993 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5994 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005995 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005996 */
5997 int
5998buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5999{
6000 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01006001 char_u *t;
6002 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006003
6004 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6005 return -1;
6006
6007 if (lnum > buf->b_ml.ml_line_count)
6008 lnum = buf->b_ml.ml_line_count;
6009
6010 str = ml_get_buf(buf, lnum, FALSE);
6011 if (str == NULL)
6012 return -1;
6013
6014 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01006015 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006016
Bram Moolenaar91458462021-01-13 20:08:38 +01006017 // count the number of characters
6018 t = str;
6019 for (count = 0; *t != NUL && t <= str + byteidx; count++)
6020 t += mb_ptr2len(t);
6021
6022 // In insert mode, when the cursor is at the end of a non-empty line,
6023 // byteidx points to the NUL character immediately past the end of the
6024 // string. In this case, add one to the character count.
6025 if (*t == NUL && byteidx != 0 && t == str + byteidx)
6026 count++;
6027
6028 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006029}
6030
6031/*
6032 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01006033 * byte index. Works only for loaded buffers. Returns -1 on failure.
6034 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006035 */
6036 int
6037buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
6038{
6039 char_u *str;
6040 char_u *t;
6041
6042 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6043 return -1;
6044
6045 if (lnum > buf->b_ml.ml_line_count)
6046 lnum = buf->b_ml.ml_line_count;
6047
6048 str = ml_get_buf(buf, lnum, FALSE);
6049 if (str == NULL)
6050 return -1;
6051
6052 // Convert the character offset to a byte offset
6053 t = str;
6054 while (*t != NUL && --charidx > 0)
6055 t += mb_ptr2len(t);
6056
Bram Moolenaar91458462021-01-13 20:08:38 +01006057 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006058}
6059
6060/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006061 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006062 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006064 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006065var2fpos(
6066 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006067 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006068 int *fnum, // set to fnum for '0, 'A, etc.
6069 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006070{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006071 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006073 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006075 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006076 if (varp->v_type == VAR_LIST)
6077 {
6078 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006079 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006080 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006081 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006082
6083 l = varp->vval.v_list;
6084 if (l == NULL)
6085 return NULL;
6086
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006087 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00006088 pos.lnum = list_find_nr(l, 0L, &error);
6089 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006090 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006091 if (charcol)
6092 len = (long)mb_charlen(ml_get(pos.lnum));
6093 else
6094 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006095
Bram Moolenaarec65d772020-08-20 22:29:12 +02006096 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006097 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00006098 li = list_find(l, 1L);
6099 if (li != NULL && li->li_tv.v_type == VAR_STRING
6100 && li->li_tv.vval.v_string != NULL
6101 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02006102 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006103 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02006104 }
6105 else
6106 {
6107 pos.col = list_find_nr(l, 1L, &error);
6108 if (error)
6109 return NULL;
6110 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00006111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006112 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006113 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006114 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00006115 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006116
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006117 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00006118 pos.coladd = list_find_nr(l, 2L, &error);
6119 if (error)
6120 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006121
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006122 return &pos;
6123 }
6124
Bram Moolenaarc5809432021-03-27 21:23:30 +01006125 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
6126 return NULL;
6127
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006128 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006129 if (name == NULL)
6130 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006131
6132 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006133 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006134 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006135 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006136 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006137 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006138 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006139 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006140 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006141 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006142 pos = VIsual;
6143 else
6144 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006145 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006146 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006147 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00006149 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006150 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6152 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006153 pos = *pp;
6154 }
6155 if (pos.lnum != 0)
6156 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006157 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01006158 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
6159 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006161
Bram Moolenaara5525202006-03-02 22:52:09 +00006162 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006163
Bram Moolenaar477933c2007-07-17 14:32:23 +00006164 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006165 {
6166 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006167 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006168 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006169 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006170 // In silent Ex mode topline is zero, but that's not a valid line
6171 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006172 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006173 return &pos;
6174 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006175 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006176 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006177 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006178 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006179 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006180 return &pos;
6181 }
6182 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006183 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00006184 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006185 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186 {
6187 pos.lnum = curbuf->b_ml.ml_line_count;
6188 pos.col = 0;
6189 }
6190 else
6191 {
6192 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006193 if (charcol)
6194 pos.col = (colnr_T)mb_charlen(ml_get_curline());
6195 else
6196 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198 return &pos;
6199 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02006200 if (in_vim9script())
6201 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 return NULL;
6203}
6204
6205/*
Bram Moolenaarcdef1ce2022-10-20 14:17:18 +01006206 * Convert list in "arg" into position "posp" and optional file number "fnump".
Bram Moolenaar79f23442022-10-10 12:42:57 +01006207 * When "fnump" is NULL there is no file number, only 3 items: [lnum, col, off]
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006208 * Note that the column is passed on as-is, the caller may want to decrement
6209 * it to use 1 for the first column.
Bram Moolenaar79f23442022-10-10 12:42:57 +01006210 * If "charcol" is TRUE use the column as the character index instead of the
6211 * byte index.
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006212 * Return FAIL when conversion is not possible, doesn't check the position for
6213 * validity.
6214 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006215 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006216list2fpos(
6217 typval_T *arg,
6218 pos_T *posp,
6219 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006220 colnr_T *curswantp,
6221 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006222{
6223 list_T *l = arg->vval.v_list;
6224 long i = 0;
6225 long n;
6226
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006227 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6228 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00006229 if (arg->v_type != VAR_LIST
6230 || l == NULL
6231 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006232 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006233 return FAIL;
6234
6235 if (fnump != NULL)
6236 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006237 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006238 if (n < 0)
6239 return FAIL;
6240 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006241 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006242 *fnump = n;
6243 }
6244
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006245 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006246 if (n < 0)
6247 return FAIL;
6248 posp->lnum = n;
6249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006250 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006251 if (n < 0)
6252 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006253 // If character position is specified, then convert to byte position
Bram Moolenaar79f23442022-10-10 12:42:57 +01006254 // If the line number is zero use the cursor line.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006255 if (charcol)
6256 {
6257 buf_T *buf;
6258
6259 // Get the text for the specified line in a loaded buffer
6260 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6261 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6262 return FAIL;
6263
Bram Moolenaar79f23442022-10-10 12:42:57 +01006264 n = buf_charidx_to_byteidx(buf,
6265 posp->lnum == 0 ? curwin->w_cursor.lnum : posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006266 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006267 posp->col = n;
6268
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006269 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006270 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006271 posp->coladd = 0;
6272 else
6273 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006274
Bram Moolenaar493c1782014-05-28 14:34:46 +02006275 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006276 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006277
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006278 return OK;
6279}
6280
6281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 * Get the length of an environment variable name.
6283 * Advance "arg" to the first character after the name.
6284 * Return 0 for error.
6285 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006286 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006287get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288{
6289 char_u *p;
6290 int len;
6291
6292 for (p = *arg; vim_isIDc(*p); ++p)
6293 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006295 return 0;
6296
6297 len = (int)(p - *arg);
6298 *arg = p;
6299 return len;
6300}
6301
6302/*
6303 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006304 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006305 * Return 0 if something is wrong.
6306 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006308get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309{
6310 char_u *p;
6311 int len;
6312
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006313 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006314 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006315 {
6316 if (*p == ':')
6317 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006318 // "s:" is start of "s:var", but "n:" is not and can be used in
6319 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006320 len = (int)(p - *arg);
6321 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6322 || len > 1)
6323 break;
6324 }
6325 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006326 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006327 return 0;
6328
6329 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006330 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006331
6332 return len;
6333}
6334
6335/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006336 * Get the length of the name of a variable or function.
6337 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006339 * Return -1 if curly braces expansion failed.
6340 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 * If the name contains 'magic' {}'s, expand them and return the
6342 * expanded name in an allocated string via 'alias' - caller must free.
6343 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006345get_name_len(
6346 char_u **arg,
6347 char_u **alias,
6348 int evaluate,
6349 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350{
6351 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352 char_u *p;
6353 char_u *expr_start;
6354 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006356 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357
6358 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6359 && (*arg)[2] == (int)KE_SNR)
6360 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006361 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362 *arg += 3;
6363 return get_id_len(arg) + 3;
6364 }
6365 len = eval_fname_script(*arg);
6366 if (len > 0)
6367 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006368 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 *arg += len;
6370 }
6371
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006373 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006375 p = find_name_end(*arg, &expr_start, &expr_end,
6376 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 if (expr_start != NULL)
6378 {
6379 char_u *temp_string;
6380
6381 if (!evaluate)
6382 {
6383 len += (int)(p - *arg);
6384 *arg = skipwhite(p);
6385 return len;
6386 }
6387
6388 /*
6389 * Include any <SID> etc in the expanded string:
6390 * Thus the -len here.
6391 */
6392 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6393 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006394 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006395 *alias = temp_string;
6396 *arg = skipwhite(p);
6397 return (int)STRLEN(temp_string);
6398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399
6400 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006401 // Only give an error when there is something, otherwise it will be
6402 // reported at a higher level.
6403 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006404 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006405
6406 return len;
6407}
6408
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006409/*
6410 * Find the end of a variable or function name, taking care of magic braces.
6411 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6412 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006413 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006414 * Return a pointer to just after the name. Equal to "arg" if there is no
6415 * valid name.
6416 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006417 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006418find_name_end(
6419 char_u *arg,
6420 char_u **expr_start,
6421 char_u **expr_end,
6422 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006424 int mb_nest = 0;
6425 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006427 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006428 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006430 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006432 *expr_start = NULL;
6433 *expr_end = NULL;
6434 }
6435
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006436 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006437 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6438 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006439 return arg;
6440
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006441 for (p = arg; *p != NUL
6442 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006443 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006444 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006445 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006446 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006447 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006448 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006449 if (*p == '\'')
6450 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006451 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006452 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006453 ;
6454 if (*p == NUL)
6455 break;
6456 }
6457 else if (*p == '"')
6458 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006459 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006460 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006461 if (*p == '\\' && p[1] != NUL)
6462 ++p;
6463 if (*p == NUL)
6464 break;
6465 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006466 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6467 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006468 // "s:" is start of "s:var", but "n:" is not and can be used in
6469 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006470 len = (int)(p - arg);
6471 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006472 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006473 break;
6474 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006478 if (*p == '[')
6479 ++br_nest;
6480 else if (*p == ']')
6481 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006483
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006484 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006486 if (*p == '{')
6487 {
6488 mb_nest++;
6489 if (expr_start != NULL && *expr_start == NULL)
6490 *expr_start = p;
6491 }
6492 else if (*p == '}')
6493 {
6494 mb_nest--;
6495 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6496 *expr_end = p;
6497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 }
6500
6501 return p;
6502}
6503
6504/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006505 * Expands out the 'magic' {}'s in a variable/function name.
6506 * Note that this can call itself recursively, to deal with
6507 * constructs like foo{bar}{baz}{bam}
6508 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6509 * "in_start" ^
6510 * "expr_start" ^
6511 * "expr_end" ^
6512 * "in_end" ^
6513 *
6514 * Returns a new allocated string, which the caller must free.
6515 * Returns NULL for failure.
6516 */
6517 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518make_expanded_name(
6519 char_u *in_start,
6520 char_u *expr_start,
6521 char_u *expr_end,
6522 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006523{
6524 char_u c1;
6525 char_u *retval = NULL;
6526 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006527
6528 if (expr_end == NULL || in_end == NULL)
6529 return NULL;
6530 *expr_start = NUL;
6531 *expr_end = NUL;
6532 c1 = *in_end;
6533 *in_end = NUL;
6534
Bram Moolenaara4e0b972022-10-01 19:43:52 +01006535 temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006536 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006537 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006538 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6539 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006540 if (retval != NULL)
6541 {
6542 STRCPY(retval, in_start);
6543 STRCAT(retval, temp_result);
6544 STRCAT(retval, expr_end + 1);
6545 }
6546 }
6547 vim_free(temp_result);
6548
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006549 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006550 *expr_start = '{';
6551 *expr_end = '}';
6552
6553 if (retval != NULL)
6554 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006555 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006556 if (expr_start != NULL)
6557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006558 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006559 temp_result = make_expanded_name(retval, expr_start,
6560 expr_end, temp_result);
6561 vim_free(retval);
6562 retval = temp_result;
6563 }
6564 }
6565
6566 return retval;
6567}
6568
6569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006571 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006572 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006573 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006574eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006576 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006577}
6578
6579/*
6580 * Return TRUE if character "c" can be used as the first character in a
6581 * variable or function name (excluding '{' and '}').
6582 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006583 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006585{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006586 return ASCII_ISALPHA(c) || c == '_';
6587}
6588
6589/*
6590 * Return TRUE if character "c" can be used as the first character of a
6591 * dictionary key.
6592 */
6593 int
6594eval_isdictc(int c)
6595{
6596 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597}
6598
6599/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006600 * Handle:
6601 * - expr[expr], expr[expr:expr] subscript
6602 * - ".name" lookup
6603 * - function call with Funcref variable: func(expr)
6604 * - method call: var->method()
6605 *
6606 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006607 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006608 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006609 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610handle_subscript(
6611 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006612 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006613 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006614 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006615 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006616{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006617 int evaluate = evalarg != NULL
6618 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006619 int ret = OK;
6620 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006621 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006622 int getnext;
6623 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006624
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006625 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006626 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006627 // When at the end of the line and ".name" or "->{" or "->X" follows in
6628 // the next line then consume the line break.
6629 p = eval_next_non_blank(*arg, evalarg, &getnext);
6630 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006631 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006632 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6633 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6634 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006635 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006636 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006637 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006638 check_white = FALSE;
6639 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006640
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006641 if (rettv->v_type == VAR_ANY)
6642 {
6643 char_u *exp_name;
6644 int cc;
6645 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006646 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006647 type_T *type;
6648
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006649 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006650 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006651 if (**arg != '.')
6652 {
6653 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006654 semsg(_(e_expected_dot_after_name_str),
6655 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006656 ret = FAIL;
6657 break;
6658 }
6659 ++*arg;
6660 if (IS_WHITE_OR_NUL(**arg))
6661 {
6662 if (verbose)
6663 emsg(_(e_no_white_space_allowed_after_dot));
6664 ret = FAIL;
6665 break;
6666 }
6667
6668 // isolate the name
6669 exp_name = *arg;
6670 while (eval_isnamec(**arg))
6671 ++*arg;
6672 cc = **arg;
6673 **arg = NUL;
6674
6675 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006676 evalarg == NULL ? NULL : evalarg->eval_cctx,
6677 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006678 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006679
6680 if (idx < 0 && ufunc == NULL)
6681 {
6682 ret = FAIL;
6683 break;
6684 }
6685 if (idx >= 0)
6686 {
6687 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6688 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6689
6690 copy_tv(sv->sv_tv, rettv);
6691 }
6692 else
6693 {
6694 rettv->v_type = VAR_FUNC;
6695 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6696 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006697 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006698 }
6699
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006700 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6701 || rettv->v_type == VAR_PARTIAL))
6702 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006703 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006704 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6705 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006706
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006707 // Stop the expression evaluation when immediately aborting on
6708 // error, or when an interrupt occurred or an exception was thrown
6709 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006710 if (aborting())
6711 {
6712 if (ret == OK)
6713 clear_tv(rettv);
6714 ret = FAIL;
6715 }
6716 dict_unref(selfdict);
6717 selfdict = NULL;
6718 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006719 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006720 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006721 if (in_vim9script())
6722 *arg = skipwhite(p + 2);
6723 else
6724 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006725 if (ret == OK)
6726 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006727 if (VIM_ISWHITE(**arg))
6728 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006729 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006730 ret = FAIL;
6731 }
6732 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006733 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006734 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006735 else
6736 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006737 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006738 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006739 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006740 // "." is ".name" lookup when we found a dict or when evaluating and
6741 // scriptversion is at least 2, where string concatenation is "..".
6742 else if (**arg == '['
6743 || (**arg == '.' && (rettv->v_type == VAR_DICT
6744 || (!evaluate
6745 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006746 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006747 {
6748 dict_unref(selfdict);
6749 if (rettv->v_type == VAR_DICT)
6750 {
6751 selfdict = rettv->vval.v_dict;
6752 if (selfdict != NULL)
6753 ++selfdict->dv_refcount;
6754 }
6755 else
6756 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006757 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006758 {
6759 clear_tv(rettv);
6760 ret = FAIL;
6761 }
6762 }
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006763 else if (**arg == '.' && (rettv->v_type == VAR_CLASS
6764 || rettv->v_type == VAR_OBJECT))
6765 {
6766 // class member: SomeClass.varname
6767 // class method: SomeClass.SomeMethod()
6768 // class constructor: SomeClass.new()
6769 // object member: someObject.varname
6770 // object method: someObject.SomeMethod()
6771 if (class_object_index(arg, rettv, evalarg, verbose) == FAIL)
6772 {
6773 clear_tv(rettv);
6774 ret = FAIL;
6775 }
6776 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006777 else
6778 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006779 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006780
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006781 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6782 // Don't do this when "Func" is already a partial that was bound
6783 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006784 if (selfdict != NULL
6785 && (rettv->v_type == VAR_FUNC
6786 || (rettv->v_type == VAR_PARTIAL
6787 && (rettv->vval.v_partial->pt_auto
6788 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006789 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006790
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006791 dict_unref(selfdict);
6792 return ret;
6793}
6794
6795/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006796 * Make a copy of an item.
6797 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006798 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006799 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6800 * reference to an already copied list/dict can be used.
6801 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006803 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006804item_copy(
6805 typval_T *from,
6806 typval_T *to,
6807 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006808 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006809 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006810{
6811 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006812 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006813
Bram Moolenaar33570922005-01-25 22:26:29 +00006814 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006815 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006816 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006817 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006818 }
6819 ++recurse;
6820
6821 switch (from->v_type)
6822 {
6823 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006824 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006825 case VAR_STRING:
6826 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006827 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006828 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006829 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006830 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006831 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006832 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +00006833 case VAR_CLASS:
6834 case VAR_OBJECT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006835 copy_tv(from, to);
6836 break;
6837 case VAR_LIST:
6838 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006839 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006840 if (from->vval.v_list == NULL)
6841 to->vval.v_list = NULL;
6842 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6843 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006844 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006845 to->vval.v_list = from->vval.v_list->lv_copylist;
6846 ++to->vval.v_list->lv_refcount;
6847 }
6848 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006849 to->vval.v_list = list_copy(from->vval.v_list,
6850 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006851 if (to->vval.v_list == NULL)
6852 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006853 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006854 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006855 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006856 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006857 case VAR_DICT:
6858 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006859 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006860 if (from->vval.v_dict == NULL)
6861 to->vval.v_dict = NULL;
6862 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6863 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006864 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006865 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6866 ++to->vval.v_dict->dv_refcount;
6867 }
6868 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006869 to->vval.v_dict = dict_copy(from->vval.v_dict,
6870 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006871 if (to->vval.v_dict == NULL)
6872 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006873 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006874 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006875 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006876 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006877 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006878 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 }
6880 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006881 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006882}
6883
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006884 void
6885echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6886{
6887 char_u *tofree;
6888 char_u numbuf[NUMBUFLEN];
6889 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6890
6891 if (*atstart)
6892 {
6893 *atstart = FALSE;
6894 // Call msg_start() after eval1(), evaluating the expression
6895 // may cause a message to appear.
6896 if (with_space)
6897 {
6898 // Mark the saved text as finishing the line, so that what
6899 // follows is displayed on a new line when scrolling back
6900 // at the more prompt.
6901 msg_sb_eol();
6902 msg_start();
6903 }
6904 }
6905 else if (with_space)
6906 msg_puts_attr(" ", echo_attr);
6907
6908 if (p != NULL)
6909 for ( ; *p != NUL && !got_int; ++p)
6910 {
6911 if (*p == '\n' || *p == '\r' || *p == TAB)
6912 {
6913 if (*p != TAB && *needclr)
6914 {
6915 // remove any text still there from the command
6916 msg_clr_eos();
6917 *needclr = FALSE;
6918 }
6919 msg_putchar_attr(*p, echo_attr);
6920 }
6921 else
6922 {
6923 if (has_mbyte)
6924 {
6925 int i = (*mb_ptr2len)(p);
6926
6927 (void)msg_outtrans_len_attr(p, i, echo_attr);
6928 p += i - 1;
6929 }
6930 else
6931 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6932 }
6933 }
6934 vim_free(tofree);
6935}
6936
Bram Moolenaare9a41262005-01-15 22:18:47 +00006937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 * ":echo expr1 ..." print each argument separated with a space, add a
6939 * newline at the end.
6940 * ":echon expr1 ..." print each argument plain.
6941 */
6942 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006943ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944{
6945 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006946 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006947 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 int needclr = TRUE;
6949 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006950 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006951 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006952 evalarg_T evalarg;
6953
Bram Moolenaare707c882020-07-01 13:07:37 +02006954 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955
6956 if (eap->skip)
6957 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006958 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006960 // If eval1() causes an error message the text from the command may
6961 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006962 need_clr_eos = needclr;
6963
Bram Moolenaar68db9962021-05-09 23:19:22 +02006964 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006965 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006966 {
6967 /*
6968 * Report the invalid expression unless the expression evaluation
6969 * has been cancelled due to an aborting error, an interrupt, or an
6970 * exception.
6971 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006972 if (!aborting() && did_emsg == did_emsg_before
6973 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006974 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006975 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976 break;
6977 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006978 need_clr_eos = FALSE;
6979
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006981 {
6982 if (rettv.v_type == VAR_VOID)
6983 {
6984 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6985 break;
6986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006987 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006988 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006989
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006990 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 arg = skipwhite(arg);
6992 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006993 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006994 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995
6996 if (eap->skip)
6997 --emsg_skip;
6998 else
6999 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007000 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 if (needclr)
7002 msg_clr_eos();
7003 if (eap->cmdidx == CMD_echo)
7004 msg_end();
7005 }
7006}
7007
7008/*
7009 * ":echohl {name}".
7010 */
7011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007012ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02007014 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015}
7016
7017/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02007018 * Returns the :echo attribute
7019 */
7020 int
7021get_echo_attr(void)
7022{
7023 return echo_attr;
7024}
7025
7026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 * ":execute expr1 ..." execute the result of an expression.
7028 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01007029 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01007031 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032 * Each gets spaces around each argument and a newline at the end for
7033 * echo commands
7034 */
7035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007036ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037{
7038 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007039 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 int ret = OK;
7041 char_u *p;
7042 garray_T ga;
7043 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007044 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045
7046 ga_init2(&ga, 1, 80);
7047
7048 if (eap->skip)
7049 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02007050 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00007051 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02007052 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007053 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055
7056 if (!eap->skip)
7057 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007058 char_u buf[NUMBUFLEN];
7059
7060 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01007061 {
7062 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
7063 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02007064 semsg(_(e_using_invalid_value_as_string_str),
7065 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01007066 p = NULL;
7067 }
7068 else
7069 p = tv_get_string_buf(&rettv, buf);
7070 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007071 else
7072 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01007073 if (p == NULL)
7074 {
7075 clear_tv(&rettv);
7076 ret = FAIL;
7077 break;
7078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007079 len = (int)STRLEN(p);
7080 if (ga_grow(&ga, len + 2) == FAIL)
7081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007082 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083 ret = FAIL;
7084 break;
7085 }
7086 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007087 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089 ga.ga_len += len;
7090 }
7091
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007092 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 arg = skipwhite(arg);
7094 }
7095
7096 if (ret != FAIL && ga.ga_data != NULL)
7097 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02007098 // use the first line of continuation lines for messages
7099 SOURCING_LNUM = start_lnum;
7100
Bram Moolenaar37fef162022-08-29 18:16:32 +01007101 if (eap->cmdidx == CMD_echomsg
7102 || eap->cmdidx == CMD_echowindow
7103 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007104 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007105 // Mark the already saved text as finishing the line, so that what
7106 // follows is displayed on a new line when scrolling back at the
7107 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007108 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01007109 }
7110
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007111 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00007112 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01007113 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00007114 out_flush();
7115 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007116 else if (eap->cmdidx == CMD_echowindow)
7117 {
7118#ifdef HAS_MESSAGE_WINDOW
Bram Moolenaarbdc09a12022-10-07 14:31:45 +01007119 start_echowindow(eap->addr_count > 0 ? eap->line2 : 0);
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01007120#endif
7121 msg_attr(ga.ga_data, echo_attr);
7122#ifdef HAS_MESSAGE_WINDOW
7123 end_echowindow();
7124#endif
7125 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01007126 else if (eap->cmdidx == CMD_echoconsole)
7127 {
7128 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
7129 ui_write((char_u *)"\r\n", 2, TRUE);
7130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007131 else if (eap->cmdidx == CMD_echoerr)
7132 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02007133 int save_did_emsg = did_emsg;
7134
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007135 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007136 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 if (!force_abort)
7138 did_emsg = save_did_emsg;
7139 }
7140 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007141 {
7142 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
7143
7144 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
7145 sticky_cmdmod_flags = cmdmod.cmod_flags
7146 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147 do_cmdline((char_u *)ga.ga_data,
7148 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00007149 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
7150 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 }
7152
7153 ga_clear(&ga);
7154
7155 if (eap->skip)
7156 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02007157 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007158}
7159
7160/*
7161 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
7162 * "arg" points to the "&" or '+' when called, to "option" when returning.
7163 * Returns NULL when no option name found. Otherwise pointer to the char
7164 * after the option name.
7165 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02007166 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007167find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 char_u *p = *arg;
7170
7171 ++p;
7172 if (*p == 'g' && p[1] == ':')
7173 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007174 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 p += 2;
7176 }
7177 else if (*p == 'l' && p[1] == ':')
7178 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007179 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 p += 2;
7181 }
7182 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00007183 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184
7185 if (!ASCII_ISALPHA(*p))
7186 return NULL;
7187 *arg = p;
7188
7189 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007190 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 else
7192 while (ASCII_ISALPHA(*p))
7193 ++p;
7194 return p;
7195}
7196
7197/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00007198 * Display script name where an item was last set.
7199 * Should only be invoked when 'verbose' is non-zero.
7200 */
7201 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007202last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007203{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007204 char_u *p;
7205
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007206 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00007207 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007208 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007209 if (p != NULL)
7210 {
7211 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01007212 msg_puts(_("\n\tLast set from "));
7213 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007214 if (script_ctx.sc_lnum > 0)
7215 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01007216 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007217 msg_outnum((long)script_ctx.sc_lnum);
7218 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007219 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007220 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007221 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00007222 }
7223}
7224
Bram Moolenaarb005cd82019-09-04 15:54:55 +02007225#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226
7227/*
7228 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007229 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230 * "flags" can be "g" to do a global substitute.
7231 * Returns an allocated string, NULL for error.
7232 */
7233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234do_string_sub(
7235 char_u *str,
7236 char_u *pat,
7237 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007238 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007239 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007240{
7241 int sublen;
7242 regmatch_T regmatch;
7243 int i;
7244 int do_all;
7245 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007246 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 garray_T ga;
7248 char_u *ret;
7249 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007250 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007252 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00007253 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007254 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255
7256 ga_init2(&ga, 1, 200);
7257
7258 do_all = (flags[0] == 'g');
7259
7260 regmatch.rm_ic = p_ic;
7261 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7262 if (regmatch.regprog != NULL)
7263 {
7264 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007265 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7267 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007268 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007269 if (regmatch.startp[0] == regmatch.endp[0])
7270 {
7271 if (zero_width == regmatch.startp[0])
7272 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007273 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007274 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007275 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7276 (size_t)i);
7277 ga.ga_len += i;
7278 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007279 continue;
7280 }
7281 zero_width = regmatch.startp[0];
7282 }
7283
Bram Moolenaar071d4272004-06-13 20:20:40 +00007284 /*
7285 * Get some space for a temporary buffer to do the substitution
7286 * into. It will contain:
7287 * - The text up to where the match is.
7288 * - The substituted text.
7289 * - The text after the match.
7290 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007291 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007292 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7294 {
7295 ga_clear(&ga);
7296 break;
7297 }
7298
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007299 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 i = (int)(regmatch.startp[0] - tail);
7301 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007302 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007303 (void)vim_regsub(&regmatch, sub, expr,
7304 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7305 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007307 tail = regmatch.endp[0];
7308 if (*tail == NUL)
7309 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007310 if (!do_all)
7311 break;
7312 }
7313
7314 if (ga.ga_data != NULL)
7315 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7316
Bram Moolenaar473de612013-06-08 18:19:48 +02007317 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007318 }
7319
7320 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7321 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007322 if (p_cpo == empty_option)
7323 p_cpo = save_cpo;
7324 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007325 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007326 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007327 // If it's still empty it was changed and restored, need to restore in
7328 // the complicated way.
7329 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007330 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007331 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333
7334 return ret;
7335}