blob: aeb8fee7ad3fe2a0c6781b671b112f267a475e59 [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 }
69 else
70 result = n1 / n2;
71
72 return result;
73}
74
75/*
76 * Return "n1" modulus "n2", taking care of dividing by zero.
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010077 * If "failed" is not NULL set it to TRUE when dividing by zero fails.
Bram Moolenaare21c1582019-03-02 11:57:09 +010078 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020079 varnumber_T
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010080num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
Bram Moolenaare21c1582019-03-02 11:57:09 +010081{
Bram Moolenaar99880f92021-01-20 21:23:14 +010082 if (n2 == 0 && in_vim9script())
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010083 {
Bram Moolenaar99880f92021-01-20 21:23:14 +010084 emsg(_(e_divide_by_zero));
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +010085 if (failed != NULL)
86 *failed = TRUE;
87 }
Bram Moolenaare21c1582019-03-02 11:57:09 +010088 return (n2 == 0) ? 0 : (n1 % n2);
89}
90
Bram Moolenaar33570922005-01-25 22:26:29 +000091/*
92 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +000093 */
94 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010095eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +000096{
Bram Moolenaare5cdf152019-08-29 22:09:46 +020097 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +020098 func_init();
Bram Moolenaara7043832005-01-21 11:56:39 +000099}
100
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000101#if defined(EXITFREE) || defined(PROTO)
102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100103eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000104{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200105 evalvars_clear();
Bram Moolenaar7ebcba62020-01-12 17:42:55 +0100106 free_scriptnames(); // must come after evalvars_clear().
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200107 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000108
Bram Moolenaarda6c0332019-09-01 16:01:30 +0200109 // autoloaded script names
110 free_autoload_scriptnames();
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000111
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200112 // unreferenced lists and dicts
113 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200114
115 // functions not garbage collected
116 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000117}
118#endif
119
Bram Moolenaare6b53242020-07-01 17:28:33 +0200120 void
Bram Moolenaar37c83712020-06-30 21:18:36 +0200121fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
122{
Bram Moolenaar844fb642021-10-23 13:32:30 +0100123 init_evalarg(evalarg);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200124 evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200125 if (eap != NULL)
Bram Moolenaar37c83712020-06-30 21:18:36 +0200126 {
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200127 evalarg->eval_cstack = eap->cstack;
Bram Moolenaara7583c42022-05-07 21:14:05 +0100128 if (sourcing_a_script(eap) || eap->getline == get_list_line)
Bram Moolenaar2eb6fc32021-07-25 14:13:53 +0200129 {
130 evalarg->eval_getline = eap->getline;
131 evalarg->eval_cookie = eap->cookie;
132 }
Bram Moolenaar37c83712020-06-30 21:18:36 +0200133 }
134}
135
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136/*
137 * Top level evaluation function, returning a boolean.
138 * Sets "error" to TRUE if there was an error.
139 * Return TRUE or FALSE.
140 */
141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100142eval_to_bool(
143 char_u *arg,
144 int *error,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200145 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100146 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
Bram Moolenaar33570922005-01-25 22:26:29 +0000148 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200149 varnumber_T retval = FALSE;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200150 evalarg_T evalarg;
151
Bram Moolenaar37c83712020-06-30 21:18:36 +0200152 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 if (skip)
155 ++emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200156 if (eval0(arg, &tv, eap, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 else
159 {
160 *error = FALSE;
161 if (!skip)
162 {
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200163 if (in_vim9script())
Bram Moolenaar13106602020-10-04 16:06:05 +0200164 retval = tv_get_bool_chk(&tv, error);
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +0200165 else
166 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000167 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 }
169 }
170 if (skip)
171 --emsg_skip;
Bram Moolenaarfaf86262020-06-27 23:07:36 +0200172 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200174 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175}
176
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100177/*
178 * Call eval1() and give an error message if not done at a lower level.
179 */
180 static int
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200181eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100182{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100183 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100184 int ret;
185 int did_emsg_before = did_emsg;
186 int called_emsg_before = called_emsg;
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200187 evalarg_T evalarg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100188
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200189 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
190
191 ret = eval1(arg, rettv, &evalarg);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100192 if (ret == FAIL)
193 {
194 // Report the invalid expression unless the expression evaluation has
195 // been cancelled due to an aborting error, an interrupt, or an
196 // exception, or we already gave a more specific error.
197 // Also check called_emsg for when using assert_fails().
198 if (!aborting() && did_emsg == did_emsg_before
199 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +0200200 semsg(_(e_invalid_expression_str), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100201 }
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200202 clear_evalarg(&evalarg, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100203 return ret;
204}
205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206/*
Bram Moolenaara9c01042020-06-07 14:50:50 +0200207 * Return whether a typval is a valid expression to pass to eval_expr_typval()
208 * or eval_expr_to_bool(). An empty string returns FALSE;
209 */
210 int
211eval_expr_valid_arg(typval_T *tv)
212{
213 return tv->v_type != VAR_UNKNOWN
214 && (tv->v_type != VAR_STRING
215 || (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
216}
217
218/*
Bram Moolenaar82418262022-09-28 16:16:15 +0100219 * When calling eval_expr_typval() many times we only need one funccall_T.
220 * Returns NULL when no funccall_T is to be used.
221 * When returning non-NULL remove_funccal() must be called later.
222 */
223 funccall_T *
224eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
225{
226 if (expr->v_type != VAR_PARTIAL)
227 return NULL;
228
229 partial_T *partial = expr->vval.v_partial;
230 if (partial == NULL)
231 return NULL;
232 if (partial->pt_func == NULL
233 || partial->pt_func->uf_def_status == UF_NOT_COMPILED)
234 return NULL;
235
236 return create_funccal(partial->pt_func, rettv);
237}
238
239/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 * Evaluate an expression, which can be a function, partial or string.
241 * Pass arguments "argv[argc]".
Bram Moolenaar82418262022-09-28 16:16:15 +0100242 * "fc_arg" is from eval_expr_get_funccal() or NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 * Return the result in "rettv" and OK or FAIL.
244 */
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200245 int
Bram Moolenaar82418262022-09-28 16:16:15 +0100246eval_expr_typval(
247 typval_T *expr,
248 typval_T *argv,
249 int argc,
250 funccall_T *fc_arg,
251 typval_T *rettv)
Bram Moolenaar48570482017-10-30 21:48:41 +0100252{
253 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100254 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200255 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100256
257 if (expr->v_type == VAR_FUNC)
258 {
259 s = expr->vval.v_string;
260 if (s == NULL || *s == NUL)
261 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200262 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000263 funcexe.fe_evaluate = TRUE;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200264 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100265 return FAIL;
266 }
267 else if (expr->v_type == VAR_PARTIAL)
268 {
269 partial_T *partial = expr->vval.v_partial;
270
Bram Moolenaar9d8d0b52020-04-24 22:47:31 +0200271 if (partial == NULL)
272 return FAIL;
273
Bram Moolenaar822ba242020-05-24 23:00:18 +0200274 if (partial->pt_func != NULL
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200275 && partial->pt_func->uf_def_status != UF_NOT_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 {
Bram Moolenaar82418262022-09-28 16:16:15 +0100277 funccall_T *fc = fc_arg != NULL ? fc_arg
278 : create_funccal(partial->pt_func, rettv);
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100279 int r;
280
281 if (fc == NULL)
282 return FAIL;
283
Bram Moolenaar69082912022-09-22 21:35:19 +0100284 // Shortcut to call a compiled function with minimal overhead.
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100285 r = call_def_function(partial->pt_func, argc, argv,
286 DEF_USE_PT_ARGV, partial, fc, rettv);
Bram Moolenaar82418262022-09-28 16:16:15 +0100287 if (fc_arg == NULL)
288 remove_funccal();
Bram Moolenaar9667b2c2022-09-07 17:28:09 +0100289 if (r == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100290 return FAIL;
291 }
292 else
293 {
294 s = partial_name(partial);
295 if (s == NULL || *s == NUL)
296 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200297 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000298 funcexe.fe_evaluate = TRUE;
299 funcexe.fe_partial = partial;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100300 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
301 return FAIL;
302 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100303 }
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200304 else if (expr->v_type == VAR_INSTR)
305 {
306 return exe_typval_instr(expr, rettv);
307 }
Bram Moolenaar48570482017-10-30 21:48:41 +0100308 else
309 {
Bram Moolenaarfc4c4482022-01-26 21:17:04 +0000310 s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
Bram Moolenaar48570482017-10-30 21:48:41 +0100311 if (s == NULL)
312 return FAIL;
313 s = skipwhite(s);
Bram Moolenaar47e880d2020-06-30 22:02:02 +0200314 if (eval1_emsg(&s, rettv, NULL) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100315 return FAIL;
Bram Moolenaarf96e9de2020-08-03 22:39:28 +0200316 if (*skipwhite(s) != NUL) // check for trailing chars after expr
Bram Moolenaar48570482017-10-30 21:48:41 +0100317 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200318 clear_tv(rettv);
Bram Moolenaar108010a2021-06-27 22:03:33 +0200319 semsg(_(e_invalid_expression_str), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100320 return FAIL;
321 }
322 }
323 return OK;
324}
325
326/*
327 * Like eval_to_bool() but using a typval_T instead of a string.
328 * Works for string, funcref and partial.
329 */
330 int
331eval_expr_to_bool(typval_T *expr, int *error)
332{
333 typval_T rettv;
334 int res;
335
Bram Moolenaar82418262022-09-28 16:16:15 +0100336 if (eval_expr_typval(expr, NULL, 0, NULL, &rettv) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100337 {
338 *error = TRUE;
339 return FALSE;
340 }
Bram Moolenaare15eebd2020-08-18 19:11:38 +0200341 res = (tv_get_bool_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100342 clear_tv(&rettv);
343 return res;
344}
345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346/*
347 * Top level evaluation function, returning a string. If "skip" is TRUE,
348 * only parsing to "nextcmd" is done, without reporting errors. Return
349 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
350 */
351 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100352eval_to_string_skip(
353 char_u *arg,
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200354 exarg_T *eap,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100355 int skip) // only parse, don't execute
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356{
Bram Moolenaar33570922005-01-25 22:26:29 +0000357 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 char_u *retval;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200359 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
Bram Moolenaar37c83712020-06-30 21:18:36 +0200361 fill_evalarg_from_eap(&evalarg, eap, skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 if (skip)
363 ++emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200364 if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 retval = NULL;
366 else
367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100368 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000369 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 }
371 if (skip)
372 --emsg_skip;
Bram Moolenaar006ad482020-06-30 20:55:15 +0200373 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375 return retval;
376}
377
378/*
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100379 * Initialize "evalarg" for use.
380 */
381 void
382init_evalarg(evalarg_T *evalarg)
383{
384 CLEAR_POINTER(evalarg);
385 ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
386}
387
388/*
389 * If "evalarg->eval_tofree" is not NULL free it later.
390 * Caller is expected to overwrite "evalarg->eval_tofree" next.
391 */
392 static void
393free_eval_tofree_later(evalarg_T *evalarg)
394{
395 if (evalarg->eval_tofree != NULL)
396 {
397 if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
398 ((char_u **)evalarg->eval_tofree_ga.ga_data)
399 [evalarg->eval_tofree_ga.ga_len++]
400 = evalarg->eval_tofree;
401 else
402 vim_free(evalarg->eval_tofree);
403 }
404}
405
406/*
407 * After using "evalarg" filled from "eap": free the memory.
408 */
409 void
410clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
411{
412 if (evalarg != NULL)
413 {
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100414 garray_T *etga = &evalarg->eval_tofree_ga;
415
416 if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100417 {
418 if (eap != NULL)
419 {
420 // We may need to keep the original command line, e.g. for
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100421 // ":let" it has the variable names. But we may also need
422 // the new one, "nextcmd" points into it. Keep both.
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100423 vim_free(eap->cmdline_tofree);
424 eap->cmdline_tofree = *eap->cmdlinep;
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100425
426 if (evalarg->eval_using_cmdline && etga->ga_len > 0)
427 {
428 // "nextcmd" points into the last line in eval_tofree_ga,
429 // need to keep it around.
430 --etga->ga_len;
431 *eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
Bram Moolenaar86fb3f82022-09-23 13:27:57 +0100432 vim_free(evalarg->eval_tofree);
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100433 }
434 else
435 *eap->cmdlinep = evalarg->eval_tofree;
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100436 }
437 else
438 vim_free(evalarg->eval_tofree);
439 evalarg->eval_tofree = NULL;
440 }
441
Bram Moolenaarf8addf12022-09-23 12:44:25 +0100442 ga_clear_strings(etga);
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100443 VIM_CLEAR(evalarg->eval_tofree_lambda);
444 }
445}
446
447/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000448 * Skip over an expression at "*pp".
449 * Return FAIL for an error, OK otherwise.
450 */
451 int
Bram Moolenaar683581e2020-10-22 21:22:58 +0200452skip_expr(char_u **pp, evalarg_T *evalarg)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000453{
Bram Moolenaar33570922005-01-25 22:26:29 +0000454 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000455
456 *pp = skipwhite(*pp);
Bram Moolenaar683581e2020-10-22 21:22:58 +0200457 return eval1(pp, &rettv, evalarg);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000458}
459
460/*
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200461 * Skip over an expression at "*arg".
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200462 * If in Vim9 script and line breaks are encountered, the lines are
463 * concatenated. "evalarg->eval_tofree" will be set accordingly.
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200464 * "arg" is advanced to just after the expression.
465 * "start" is set to the start of the expression, "end" to just after the end.
466 * Also when the expression is copied to allocated memory.
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200467 * Return FAIL for an error, OK otherwise.
468 */
469 int
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200470skip_expr_concatenate(
471 char_u **arg,
472 char_u **start,
473 char_u **end,
474 evalarg_T *evalarg)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200475{
476 typval_T rettv;
477 int res;
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200478 int vim9script = in_vim9script();
Bram Moolenaar9c2b0662020-09-01 19:56:15 +0200479 garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200480 garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200481 int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200482 int evaluate = evalarg == NULL
483 ? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200484
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200485 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200486 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200487 {
488 ga_init2(gap, sizeof(char_u *), 10);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200489 // leave room for "start"
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200490 if (ga_grow(gap, 1) == OK)
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200491 ++gap->ga_len;
Bram Moolenaarecb66452021-05-18 15:09:18 +0200492 ga_init2(freegap, sizeof(char_u *), 10);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200493 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200494 *start = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200495
496 // Don't evaluate the expression.
497 if (evalarg != NULL)
498 evalarg->eval_flags &= ~EVAL_EVALUATE;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200499 *arg = skipwhite(*arg);
500 res = eval1(arg, &rettv, evalarg);
501 *end = *arg;
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200502 if (evalarg != NULL)
503 evalarg->eval_flags = save_flags;
504
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +0200505 if (vim9script && evaluate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200506 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200507 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200508 if (evalarg->eval_ga.ga_len == 1)
509 {
Bram Moolenaarecb66452021-05-18 15:09:18 +0200510 // just the one line, no need to concatenate
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200511 ga_clear(gap);
512 gap->ga_itemsize = 0;
513 }
514 else
515 {
516 char_u *p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200517 size_t endoff = STRLEN(*arg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200518
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200519 // Line breaks encountered, concatenate all the lines.
520 *((char_u **)gap->ga_data) = *start;
Bram Moolenaar03717bf2021-04-28 20:00:40 +0200521 p = ga_concat_strings(gap, " ");
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200522
523 // free the lines only when using getsourceline()
524 if (evalarg->eval_cookie != NULL)
525 {
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200526 // Do not free the first line, the caller can still use it.
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200527 *((char_u **)gap->ga_data) = NULL;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200528 // Do not free the last line, "arg" points into it, free it
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +0100529 // later. Also free "eval_tofree" later if needed.
530 free_eval_tofree_later(evalarg);
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200531 evalarg->eval_tofree =
532 ((char_u **)gap->ga_data)[gap->ga_len - 1];
533 ((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200534 ga_clear_strings(gap);
535 }
536 else
Bram Moolenaarecb66452021-05-18 15:09:18 +0200537 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200538 ga_clear(gap);
Bram Moolenaarecb66452021-05-18 15:09:18 +0200539
540 // free lines that were explicitly marked for freeing
541 ga_clear_strings(freegap);
542 }
543
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200544 gap->ga_itemsize = 0;
545 if (p == NULL)
546 return FAIL;
547 *start = p;
Bram Moolenaar8e2730a2020-07-08 22:01:49 +0200548 vim_free(evalarg->eval_tofree_lambda);
549 evalarg->eval_tofree_lambda = p;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200550 // Compute "end" relative to the end.
551 *end = *start + STRLEN(*start) - endoff;
552 }
Bram Moolenaare40fbc22020-06-27 18:06:45 +0200553 }
554
555 return res;
556}
557
558/*
Bram Moolenaar883cf972021-01-15 18:04:43 +0100559 * Convert "tv" to a string.
560 * When "convert" is TRUE convert a List into a sequence of lines and convert
561 * a Float to a String.
562 * Returns an allocated string (NULL when out of memory).
563 */
564 char_u *
565typval2string(typval_T *tv, int convert)
566{
567 garray_T ga;
568 char_u *retval;
Bram Moolenaar883cf972021-01-15 18:04:43 +0100569 char_u numbuf[NUMBUFLEN];
Bram Moolenaar883cf972021-01-15 18:04:43 +0100570
571 if (convert && tv->v_type == VAR_LIST)
572 {
Bram Moolenaar04935fb2022-01-08 16:19:22 +0000573 ga_init2(&ga, sizeof(char), 80);
Bram Moolenaar883cf972021-01-15 18:04:43 +0100574 if (tv->vval.v_list != NULL)
575 {
576 list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
577 if (tv->vval.v_list->lv_len > 0)
578 ga_append(&ga, NL);
579 }
580 ga_append(&ga, NUL);
581 retval = (char_u *)ga.ga_data;
582 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100583 else if (convert && tv->v_type == VAR_FLOAT)
584 {
585 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
586 retval = vim_strsave(numbuf);
587 }
Bram Moolenaar883cf972021-01-15 18:04:43 +0100588 else
589 retval = vim_strsave(tv_get_string(tv));
590 return retval;
591}
592
593/*
Bram Moolenaar7a4b8982020-07-08 17:36:21 +0200594 * Top level evaluation function, returning a string. Does not handle line
595 * breaks.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000596 * When "convert" is TRUE convert a List into a sequence of lines and convert
597 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 * Return pointer to allocated memory, or NULL for failure.
599 */
600 char_u *
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100601eval_to_string_eap(
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602 char_u *arg,
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100603 int convert,
604 exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605{
Bram Moolenaar33570922005-01-25 22:26:29 +0000606 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 char_u *retval;
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100608 evalarg_T evalarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100610 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
611 if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 retval = NULL;
613 else
614 {
Bram Moolenaar883cf972021-01-15 18:04:43 +0100615 retval = typval2string(&tv, convert);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000616 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 }
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100618 clear_evalarg(&evalarg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
620 return retval;
621}
622
Bram Moolenaarb4bcea42020-10-28 13:53:50 +0100623 char_u *
624eval_to_string(
625 char_u *arg,
626 int convert)
627{
628 return eval_to_string_eap(arg, convert, NULL);
629}
630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000632 * Call eval_to_string() without using current local variables and using
zeertzjqcfe45652022-05-27 17:26:55 +0100633 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200634 * Use legacy Vim script syntax.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635 */
636 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100637eval_to_string_safe(
638 char_u *arg,
Bram Moolenaar9530b582022-01-22 13:39:08 +0000639 int use_sandbox,
640 int keep_script_version)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200643 funccal_entry_T funccal_entry;
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200644 int save_sc_version = current_sctx.sc_version;
Christian Brabandt070ac342021-09-09 12:12:03 +0200645 int save_garbage = may_garbage_collect;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646
Bram Moolenaar9530b582022-01-22 13:39:08 +0000647 if (!keep_script_version)
648 current_sctx.sc_version = 1;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200649 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000650 if (use_sandbox)
651 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100652 ++textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200653 may_garbage_collect = FALSE;
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200654 retval = eval_to_string(arg, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000655 if (use_sandbox)
656 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100657 --textlock;
Christian Brabandt070ac342021-09-09 12:12:03 +0200658 may_garbage_collect = save_garbage;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200659 restore_funccal();
Bram Moolenaarc9edd6b2020-08-12 22:18:23 +0200660 current_sctx.sc_version = save_sc_version;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 return retval;
662}
663
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664/*
665 * Top level evaluation function, returning a number.
666 * Evaluates "expr" silently.
667 * Returns -1 for an error.
668 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200669 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100670eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671{
Bram Moolenaar33570922005-01-25 22:26:29 +0000672 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200673 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000674 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675
676 ++emsg_off;
677
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200678 if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 retval = -1;
680 else
681 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100682 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000683 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685 --emsg_off;
686
687 return retval;
688}
689
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000690/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000691 * Top level evaluation function.
692 * Returns an allocated typval_T with the result.
693 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000694 */
695 typval_T *
Bram Moolenaarb171fb12020-06-24 20:34:03 +0200696eval_expr(char_u *arg, exarg_T *eap)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000697{
698 typval_T *tv;
Bram Moolenaar37c83712020-06-30 21:18:36 +0200699 evalarg_T evalarg;
700
701 fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000702
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200703 tv = ALLOC_ONE(typval_T);
Bram Moolenaar37c83712020-06-30 21:18:36 +0200704 if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100705 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000706
Bram Moolenaar37c83712020-06-30 21:18:36 +0200707 clear_evalarg(&evalarg, eap);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000708 return tv;
709}
710
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711/*
Bram Moolenaar15d16352022-01-17 20:09:08 +0000712 * "*arg" points to what can be a function name in the form of "import.Name" or
713 * "Funcref". Return the name of the function. Set "tofree" to something that
714 * was allocated.
715 * If "verbose" is FALSE no errors are given.
716 * Return NULL for any failure.
717 */
718 static char_u *
719deref_function_name(
Bram Moolenaar54969f42022-02-07 13:56:44 +0000720 char_u **arg,
721 char_u **tofree,
722 evalarg_T *evalarg,
723 int verbose)
Bram Moolenaar15d16352022-01-17 20:09:08 +0000724{
725 typval_T ref;
726 char_u *name = *arg;
Bram Moolenaar06fef1b2022-09-03 21:53:28 +0100727 int save_flags = 0;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000728
729 ref.v_type = VAR_UNKNOWN;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100730 if (evalarg != NULL)
731 {
732 // need to evaluate this to get an import, like in "a.Func"
733 save_flags = evalarg->eval_flags;
734 evalarg->eval_flags |= EVAL_EVALUATE;
735 }
Yegappan Lakshmanana061f342022-05-22 19:13:49 +0100736 if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100737 {
738 dictitem_T *v;
739
740 // If <SID>VarName was used it would not be found, try another way.
741 v = find_var_also_in_script(name, NULL, FALSE);
742 if (v == NULL)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100743 {
744 name = NULL;
745 goto theend;
746 }
Bram Moolenaar12eb2eb2022-04-15 22:57:09 +0100747 copy_tv(&v->di_tv, &ref);
748 }
Bram Moolenaar15d16352022-01-17 20:09:08 +0000749 if (*skipwhite(*arg) != NUL)
750 {
751 if (verbose)
752 semsg(_(e_trailing_characters_str), *arg);
753 name = NULL;
754 }
755 else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
756 {
757 name = ref.vval.v_string;
758 ref.vval.v_string = NULL;
759 *tofree = name;
760 }
761 else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
762 {
763 if (ref.vval.v_partial->pt_argc > 0
764 || ref.vval.v_partial->pt_dict != NULL)
765 {
766 if (verbose)
767 emsg(_(e_cannot_use_partial_here));
768 name = NULL;
769 }
770 else
771 {
772 name = vim_strsave(partial_name(ref.vval.v_partial));
773 *tofree = name;
774 }
775 }
776 else
777 {
778 if (verbose)
779 semsg(_(e_not_callable_type_str), name);
780 name = NULL;
781 }
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100782
783theend:
Bram Moolenaar15d16352022-01-17 20:09:08 +0000784 clear_tv(&ref);
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +0100785 if (evalarg != NULL)
786 evalarg->eval_flags = save_flags;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000787 return name;
788}
789
790/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100791 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200792 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
793 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000794 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200796 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797call_vim_function(
798 char_u *func,
799 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200800 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200801 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000803 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200804 funcexe_T funcexe;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000805 char_u *arg;
806 char_u *name;
807 char_u *tofree = NULL;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000808 int ignore_errors;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000809
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100810 rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara80faa82020-04-12 19:37:17 +0200811 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000812 funcexe.fe_firstline = curwin->w_cursor.lnum;
813 funcexe.fe_lastline = curwin->w_cursor.lnum;
814 funcexe.fe_evaluate = TRUE;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000815
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000816 // The name might be "import.Func" or "Funcref". We don't know, we need to
817 // ignore errors for an undefined name. But we do want errors when an
Bram Moolenaarda6d42c2022-03-17 11:46:55 +0000818 // autoload script has errors. Guess that when there is a dot in the name
819 // showing errors is the right choice.
820 ignore_errors = vim_strchr(func, '.') == NULL;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000821 arg = func;
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000822 if (ignore_errors)
823 ++emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000824 name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
Bram Moolenaarfe8e9f62022-03-16 13:09:15 +0000825 if (ignore_errors)
826 --emsg_off;
Bram Moolenaar15d16352022-01-17 20:09:08 +0000827 if (name == NULL)
828 name = func;
829
830 ret = call_func(name, -1, rettv, argc, argv, &funcexe);
831
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000832 if (ret == FAIL)
833 clear_tv(rettv);
Bram Moolenaar15d16352022-01-17 20:09:08 +0000834 vim_free(tofree);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000835
836 return ret;
837}
838
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100839/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100840 * Call Vim script function "func" and return the result as a string.
Dominique Pelle748b3082022-01-08 12:41:16 +0000841 * Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
842 * should have type VAR_UNKNOWN.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000843 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000844 */
845 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100846call_func_retstr(
847 char_u *func,
848 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200849 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000850{
851 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000852 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000853
Bram Moolenaarded27a12018-08-01 19:06:03 +0200854 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000855 return NULL;
856
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100857 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000858 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 return retval;
860}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000861
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000862/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100863 * Call Vim script function "func" and return the result as a List.
Dominique Pelle748b3082022-01-08 12:41:16 +0000864 * Uses "argv" and "argc" as call_func_retstr().
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000865 * Returns NULL when there is something wrong.
Bram Moolenaar55e93662022-09-10 13:52:26 +0100866 * Gives an error when the returned value is not a list.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000867 */
868 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100869call_func_retlist(
870 char_u *func,
871 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200872 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000873{
874 typval_T rettv;
875
Bram Moolenaarded27a12018-08-01 19:06:03 +0200876 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000877 return NULL;
878
879 if (rettv.v_type != VAR_LIST)
880 {
Bram Moolenaar55e93662022-09-10 13:52:26 +0100881 semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
882 vartype_name(rettv.v_type));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000883 clear_tv(&rettv);
884 return NULL;
885 }
886
887 return rettv.vval.v_list;
888}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889
Bram Moolenaare70dd112022-01-21 16:31:11 +0000890#if defined(FEAT_FOLDING) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891/*
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100892 * Evaluate "arg", which is 'foldexpr'.
893 * Note: caller must set "curwin" to match "arg".
894 * Returns the foldlevel, and any character preceding it in "*cp". Doesn't
895 * give error messages.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 */
897 int
Bram Moolenaare70dd112022-01-21 16:31:11 +0000898eval_foldexpr(win_T *wp, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899{
Bram Moolenaare70dd112022-01-21 16:31:11 +0000900 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +0000901 typval_T tv;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100902 int r = NOTDONE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200903 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000905 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000906 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
907 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100909 arg = skipwhite(wp->w_p_fde);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000910 current_sctx = wp->w_p_script_ctx[WV_FDE];
911
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000913 if (use_sandbox)
914 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100915 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 *cp = NUL;
Bram Moolenaar87b4e5c2022-10-01 15:32:46 +0100917
918 // If the expression is "FuncName()" then we can skip a lot of overhead.
919 char_u *parens = (char_u *)strstr((char *)arg, "()");
920 if (parens != NULL && *skipwhite(parens + 2) == NUL)
921 {
922 char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
923
924 if (to_name_end(p, TRUE) == parens)
925 r = call_simple_func(arg, (int)(parens - arg), &tv);
926 }
927
928 if (r == NOTDONE)
929 r = eval0(arg, &tv, NULL, &EVALARG_EVALUATE);
930
931 if (r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 retval = 0;
933 else
934 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100935 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000936 if (tv.v_type == VAR_NUMBER)
937 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000938 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 retval = 0;
940 else
941 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100942 // If the result is a string, check if there is a non-digit before
943 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000944 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 if (!VIM_ISDIGIT(*s) && *s != '-')
946 *cp = *s++;
947 retval = atol((char *)s);
948 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000949 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 }
951 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000952 if (use_sandbox)
953 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100954 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200955 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000956 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200958 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959}
960#endif
961
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000963 * Get an lval: variable, Dict item or List item that can be assigned a value
964 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
965 * "name.key", "name.key[expr]" etc.
966 * Indexing only works if "name" is an existing List or Dictionary.
967 * "name" points to the start of the name.
968 * If "rettv" is not NULL it points to the value to be assigned.
969 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
970 * wrong; must end in space or cmd separator.
971 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100972 * flags:
973 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100974 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100975 * GLV_NO_AUTOLOAD: do not use script autoloading
976 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000978 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000979 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000980 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200981 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100982get_lval(
983 char_u *name,
984 typval_T *rettv,
985 lval_T *lp,
986 int unlet,
987 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100988 int flags, // GLV_ values
989 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000990{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000991 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 char_u *expr_start, *expr_end;
993 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000994 dictitem_T *v;
995 typval_T var1;
996 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000997 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000998 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000999 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +01001000 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001001 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001002 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +00001003 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001005 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +02001006 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001007
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001008 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001009 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +01001010 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001011 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +02001012 lp->ll_name_end = find_name_end(name, NULL, NULL,
1013 FNE_INCL_BR | fne_flags);
1014 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001015 }
1016
Bram Moolenaara749a422022-02-12 19:52:25 +00001017 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001018 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001019 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1020 {
1021 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1022 return NULL;
1023 }
1024
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001025 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001026 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001028 if (expr_start != NULL)
1029 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001030 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001031 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001032 && *p != '[' && *p != '.')
1033 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001034 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001035 return NULL;
1036 }
1037
1038 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1039 if (lp->ll_exp_name == NULL)
1040 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001041 // Report an invalid expression in braces, unless the
1042 // expression evaluation has been cancelled due to an
1043 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001044 if (!aborting() && !quiet)
1045 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001046 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001047 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001048 return NULL;
1049 }
1050 }
1051 lp->ll_name = lp->ll_exp_name;
1052 }
1053 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001055 lp->ll_name = name;
1056
Bram Moolenaar4525a572022-02-13 11:57:33 +00001057 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001059 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001060 // However, "g:[key]" is indexing a dictionary.
1061 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001062 {
1063 --p;
1064 lp->ll_name_end = p;
1065 }
1066 if (*p == ':')
1067 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001068 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001069
Bram Moolenaar9510d222022-09-11 15:14:05 +01001070 if (is_scoped_variable(name))
1071 {
1072 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1073 return NULL;
1074 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001075 if (tp == p + 1 && !quiet)
1076 {
1077 semsg(_(e_white_space_required_after_str_str), ":", p);
1078 return NULL;
1079 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001080 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001081 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001082 semsg(_(e_using_type_not_in_script_context_str), p);
1083 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001084 }
1085
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001086 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001087 lp->ll_type = parse_type(&tp,
1088 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1089 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001090 if (lp->ll_type == NULL && !quiet)
1091 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001092 lp->ll_name_end = tp;
1093 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 }
1095 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001096 if (lp->ll_name == NULL)
1097 return p;
1098
Bram Moolenaar62aec932022-01-29 21:45:34 +00001099 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001100 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001101 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001102
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001103 if (import != NULL)
1104 {
1105 ufunc_T *ufunc;
1106 type_T *type;
1107
Bram Moolenaar753885b2022-08-24 16:30:36 +01001108 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001109 lp->ll_sid = import->imp_sid;
1110 lp->ll_name = skipwhite(p + 1);
1111 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1112 lp->ll_name_end = p;
1113
1114 // check the item is exported
1115 cc = *p;
1116 *p = NUL;
1117 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001118 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001119 {
1120 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001121 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001122 }
1123 *p = cc;
1124 }
1125 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001127 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001128 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001129 return p;
1130
Bram Moolenaar4525a572022-02-13 11:57:33 +00001131 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001132 {
1133 // using local variable
1134 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001135 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001136 }
1137 else
1138 {
1139 cc = *p;
1140 *p = NUL;
1141 // When we would write to the variable pass &ht and prevent autoload.
1142 writing = !(flags & GLV_READ_ONLY);
1143 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001144 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001145 if (v == NULL && !quiet)
1146 semsg(_(e_undefined_variable_str), lp->ll_name);
1147 *p = cc;
1148 if (v == NULL)
1149 return NULL;
1150 lp->ll_tv = &v->di_tv;
1151 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001152
Bram Moolenaar4525a572022-02-13 11:57:33 +00001153 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001154 {
1155 if (!quiet)
1156 semsg(_(e_variable_already_declared), lp->ll_name);
1157 return NULL;
1158 }
1159
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001160 /*
1161 * Loop until no more [idx] or .key is following.
1162 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001163 var1.v_type = VAR_UNKNOWN;
1164 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001165 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001166 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001167 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001168
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001169 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1170 {
1171 if (!quiet)
1172 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1173 return NULL;
1174 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001175 if (lp->ll_tv->v_type != VAR_LIST
1176 && lp->ll_tv->v_type != VAR_DICT
1177 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001178 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001179 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001180 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001181 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001182 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001183
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001184 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001185 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001186 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001187 else if (lp->ll_tv->v_type == VAR_BLOB
1188 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001189 r = rettv_blob_alloc(lp->ll_tv);
1190 if (r == FAIL)
1191 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001192
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001193 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001194 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001195 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001196 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001197 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001199
Bram Moolenaar4525a572022-02-13 11:57:33 +00001200 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001201 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001202 && lp->ll_tv == &v->di_tv
1203 && ht != NULL && ht == get_script_local_ht())
1204 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001205 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001206
1207 // Vim9 script local variable: get the type
1208 if (sv != NULL)
1209 lp->ll_valtype = sv->sv_type;
1210 }
1211
Bram Moolenaar8c711452005-01-14 21:53:12 +00001212 len = -1;
1213 if (*p == '.')
1214 {
1215 key = p + 1;
1216 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1217 ;
1218 if (len == 0)
1219 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001220 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001221 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001222 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001223 }
1224 p = key + len;
1225 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001226 else
1227 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001228 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001229 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001230 if (*p == ':')
1231 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001232 else
1233 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001234 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001235 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001237 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001238 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001239 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001240 clear_tv(&var1);
1241 return NULL;
1242 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001243 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001244 }
1245
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001246 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001247 if (*p == ':')
1248 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001249 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001250 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001251 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001252 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001253 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001255 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001256 if (rettv != NULL
1257 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001258 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001259 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001260 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001261 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001263 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001264 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001265 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001266 }
1267 p = skipwhite(p + 1);
1268 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 else
1271 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001273 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001274 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001275 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001276 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001277 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001278 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001279 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001280 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001281 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001282 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001283 clear_tv(&var2);
1284 return NULL;
1285 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001286 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001287 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001288 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001289 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001291
Bram Moolenaar8c711452005-01-14 21:53:12 +00001292 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001293 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001294 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001295 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001296 clear_tv(&var1);
1297 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001298 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001299 }
1300
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001301 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001302 ++p;
1303 }
1304
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001305 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001306 {
1307 if (len == -1)
1308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001309 // "[key]": get key from "var1"
1310 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001311 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001312 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001313 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001314 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001315 }
1316 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001317 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001318
1319 // a NULL dict is equivalent with an empty dict
1320 if (lp->ll_tv->vval.v_dict == NULL)
1321 {
1322 lp->ll_tv->vval.v_dict = dict_alloc();
1323 if (lp->ll_tv->vval.v_dict == NULL)
1324 {
1325 clear_tv(&var1);
1326 return NULL;
1327 }
1328 ++lp->ll_tv->vval.v_dict->dv_refcount;
1329 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001330 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001331
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001332 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001333
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001334 // When assigning to a scope dictionary check that a function and
1335 // variable name is valid (only variable name unless it is l: or
1336 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001337 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001338 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001339 int prevval;
1340 int wrong;
1341
1342 if (len != -1)
1343 {
1344 prevval = key[len];
1345 key[len] = NUL;
1346 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001347 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001348 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001349 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1350 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001351 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001352 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001353 if (len != -1)
1354 key[len] = prevval;
1355 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001356 {
1357 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001358 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001359 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001360 }
1361
Bram Moolenaar10c65862020-10-08 21:16:42 +02001362 if (lp->ll_valtype != NULL)
1363 // use the type of the member
1364 lp->ll_valtype = lp->ll_valtype->tt_member;
1365
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001366 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001367 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001368 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001369 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001370 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001371 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001372 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001373 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001374 return NULL;
1375 }
1376
Bram Moolenaar31b81602019-02-10 22:14:27 +01001377 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001378 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001379 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001380 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001381 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001382 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001383 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001384 }
1385 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001386 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001387 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001388 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001389 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001390 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001391 p = NULL;
1392 break;
1393 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001394 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001395 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001396 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1397 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001398 {
1399 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001400 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001401 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001402
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001403 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001404 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001405 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001406 else if (lp->ll_tv->v_type == VAR_BLOB)
1407 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001408 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1409
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001410 /*
1411 * Get the number and item for the only or first index of the List.
1412 */
1413 if (empty1)
1414 lp->ll_n1 = 0;
1415 else
1416 // is number or string
1417 lp->ll_n1 = (long)tv_get_number(&var1);
1418 clear_tv(&var1);
1419
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001420 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001421 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001422 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001423 return NULL;
1424 }
1425 if (lp->ll_range && !lp->ll_empty2)
1426 {
1427 lp->ll_n2 = (long)tv_get_number(&var2);
1428 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001429 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1430 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001431 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001432 }
1433 lp->ll_blob = lp->ll_tv->vval.v_blob;
1434 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001435 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001437 else
1438 {
1439 /*
1440 * Get the number and item for the only or first index of the List.
1441 */
1442 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001443 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001444 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001445 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001446 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001447 clear_tv(&var1);
1448
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001449 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001450 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001451 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1452 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001453 if (lp->ll_li == NULL)
1454 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001455 clear_tv(&var2);
1456 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001457 }
1458
Bram Moolenaar10c65862020-10-08 21:16:42 +02001459 if (lp->ll_valtype != NULL)
1460 // use the type of the member
1461 lp->ll_valtype = lp->ll_valtype->tt_member;
1462
Bram Moolenaar8c711452005-01-14 21:53:12 +00001463 /*
1464 * May need to find the item or absolute index for the second
1465 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001466 * When no index given: "lp->ll_empty2" is TRUE.
1467 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001468 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001469 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001470 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001471 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001472 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001473 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001474 if (check_range_index_two(lp->ll_list,
1475 &lp->ll_n1, lp->ll_li,
1476 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001478 }
1479
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001480 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001482 }
1483
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001484 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 return p;
1487}
1488
1489/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001490 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001491 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001494{
1495 vim_free(lp->ll_exp_name);
1496 vim_free(lp->ll_newkey);
1497}
1498
1499/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001500 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001501 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001502 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1503 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001504 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001505 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001506set_var_lval(
1507 lval_T *lp,
1508 char_u *endp,
1509 typval_T *rettv,
1510 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001511 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1512 char_u *op,
1513 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001514{
1515 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001516 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517
1518 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001519 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001520 cc = *endp;
1521 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001522 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1523 return;
1524
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001525 if (lp->ll_blob != NULL)
1526 {
1527 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001528
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001529 if (op != NULL && *op != '=')
1530 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001531 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001532 return;
1533 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001534 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001535 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001536
1537 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1538 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001539 if (lp->ll_empty2)
1540 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1541
Bram Moolenaar68452172021-04-12 21:21:02 +02001542 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1543 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001544 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001545 }
1546 else
1547 {
1548 val = (int)tv_get_number_chk(rettv, &error);
1549 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001550 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001551 }
1552 }
1553 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001554 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001555 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001557 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1558 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001559 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001560 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001561 *endp = cc;
1562 return;
1563 }
1564
Bram Moolenaarff697e62019-02-12 22:28:33 +01001565 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001566 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001567 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001568 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001569 {
1570 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001571 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1572 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001573 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001574 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001575 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001576 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001577 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001578 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001579 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001580 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001581 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1582 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001583 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001584 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001585 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001586 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001587 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001588 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001589 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001590 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001591 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001592 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001593 else if (lp->ll_range)
1594 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001595 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1596 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001597 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001598 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001599 return;
1600 }
1601
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001602 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1603 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001604 }
1605 else
1606 {
1607 /*
1608 * Assign to a List or Dictionary item.
1609 */
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001610 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1611 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001612 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001613 emsg(_(e_cannot_lock_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001614 return;
1615 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001616
1617 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001618 && check_typval_arg_type(lp->ll_valtype, rettv,
1619 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001620 return;
1621
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001622 if (lp->ll_newkey != NULL)
1623 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001624 if (op != NULL && *op != '=')
1625 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001626 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001627 return;
1628 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001629 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1630 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001631 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001632
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001633 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001634 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001635 if (di == NULL)
1636 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001637 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1638 {
1639 vim_free(di);
1640 return;
1641 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001642 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001643 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001644 else if (op != NULL && *op != '=')
1645 {
1646 tv_op(lp->ll_tv, rettv, op);
1647 return;
1648 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001650 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001651
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001652 /*
1653 * Assign the value to the variable or list item.
1654 */
1655 if (copy)
1656 copy_tv(rettv, lp->ll_tv);
1657 else
1658 {
1659 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001660 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001661 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001662 }
1663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664}
1665
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001666/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001667 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1668 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001669 * Returns OK or FAIL.
1670 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001671 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001672tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001673{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001674 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 char_u numbuf[NUMBUFLEN];
1676 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001677 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001678
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001679 // Can't do anything with a Funcref or Dict on the right.
1680 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001681 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001682 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1683 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001684 {
1685 switch (tv1->v_type)
1686 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001687 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001688 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001690 case VAR_DICT:
1691 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001692 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001693 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001694 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001695 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001696 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001697 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001698 break;
1699
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001700 case VAR_BLOB:
1701 if (*op != '+' || tv2->v_type != VAR_BLOB)
1702 break;
1703 // BLOB += BLOB
1704 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1705 {
1706 blob_T *b1 = tv1->vval.v_blob;
1707 blob_T *b2 = tv2->vval.v_blob;
1708 int i, len = blob_len(b2);
1709 for (i = 0; i < len; i++)
1710 ga_append(&b1->bv_ga, blob_get(b2, i));
1711 }
1712 return OK;
1713
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001714 case VAR_LIST:
1715 if (*op != '+' || tv2->v_type != VAR_LIST)
1716 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001717 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001718 if (tv2->vval.v_list != NULL)
1719 {
1720 if (tv1->vval.v_list == NULL)
1721 {
1722 tv1->vval.v_list = tv2->vval.v_list;
1723 ++tv1->vval.v_list->lv_refcount;
1724 }
1725 else
1726 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001728 return OK;
1729
1730 case VAR_NUMBER:
1731 case VAR_STRING:
1732 if (tv2->v_type == VAR_LIST)
1733 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001734 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001735 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001736 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001737 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001738 if (tv2->v_type == VAR_FLOAT)
1739 {
1740 float_T f = n;
1741
Bram Moolenaarff697e62019-02-12 22:28:33 +01001742 if (*op == '%')
1743 break;
1744 switch (*op)
1745 {
1746 case '+': f += tv2->vval.v_float; break;
1747 case '-': f -= tv2->vval.v_float; break;
1748 case '*': f *= tv2->vval.v_float; break;
1749 case '/': f /= tv2->vval.v_float; break;
1750 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001751 clear_tv(tv1);
1752 tv1->v_type = VAR_FLOAT;
1753 tv1->vval.v_float = f;
1754 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001755 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001756 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001757 switch (*op)
1758 {
1759 case '+': n += tv_get_number(tv2); break;
1760 case '-': n -= tv_get_number(tv2); break;
1761 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001762 case '/': n = num_divide(n, tv_get_number(tv2),
1763 &failed); break;
1764 case '%': n = num_modulus(n, tv_get_number(tv2),
1765 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001766 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001767 clear_tv(tv1);
1768 tv1->v_type = VAR_NUMBER;
1769 tv1->vval.v_number = n;
1770 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001771 }
1772 else
1773 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001774 if (tv2->v_type == VAR_FLOAT)
1775 break;
1776
Bram Moolenaarff697e62019-02-12 22:28:33 +01001777 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001778 s = tv_get_string(tv1);
1779 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001780 clear_tv(tv1);
1781 tv1->v_type = VAR_STRING;
1782 tv1->vval.v_string = s;
1783 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001784 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001786 case VAR_FLOAT:
1787 {
1788 float_T f;
1789
Bram Moolenaarff697e62019-02-12 22:28:33 +01001790 if (*op == '%' || *op == '.'
1791 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001792 && tv2->v_type != VAR_NUMBER
1793 && tv2->v_type != VAR_STRING))
1794 break;
1795 if (tv2->v_type == VAR_FLOAT)
1796 f = tv2->vval.v_float;
1797 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001798 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001799 switch (*op)
1800 {
1801 case '+': tv1->vval.v_float += f; break;
1802 case '-': tv1->vval.v_float -= f; break;
1803 case '*': tv1->vval.v_float *= f; break;
1804 case '/': tv1->vval.v_float /= f; break;
1805 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001806 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001807 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001808 }
1809 }
1810
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001811 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 return FAIL;
1813}
1814
1815/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001816 * Evaluate the expression used in a ":for var in expr" command.
1817 * "arg" points to "var".
1818 * Set "*errp" to TRUE for an error, FALSE otherwise;
1819 * Return a pointer that holds the info. Null when there is an error.
1820 */
1821 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001822eval_for_line(
1823 char_u *arg,
1824 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001825 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001826 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827{
Bram Moolenaar33570922005-01-25 22:26:29 +00001828 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001829 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001830 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001831 typval_T tv;
1832 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001833 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001835 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001836
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001837 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001838 if (fi == NULL)
1839 return NULL;
1840
Bram Moolenaar404557e2021-07-05 21:41:48 +02001841 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1842 &fi->fi_semicolon, FALSE);
1843 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001844 return fi;
1845
Bram Moolenaar404557e2021-07-05 21:41:48 +02001846 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001847 if (expr[0] != 'i' || expr[1] != 'n'
1848 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001850 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1851 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1852 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001853 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001854 return fi;
1855 }
1856
1857 if (skip)
1858 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001859 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1860 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001861 {
1862 *errp = FALSE;
1863 if (!skip)
1864 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001865 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001866 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001867 l = tv.vval.v_list;
1868 if (l == NULL)
1869 {
1870 // a null list is like an empty list: do nothing
1871 clear_tv(&tv);
1872 }
1873 else
1874 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001876 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001878 // No need to increment the refcount, it's already set for
1879 // the list being used in "tv".
1880 fi->fi_list = l;
1881 list_add_watch(l, &fi->fi_lw);
1882 fi->fi_lw.lw_item = l->lv_first;
1883 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001884 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001885 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001886 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001887 fi->fi_bi = 0;
1888 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001889 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001890 typval_T btv;
1891
1892 // Make a copy, so that the iteration still works when the
1893 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001895 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001896 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001897 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001898 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001899 else if (tv.v_type == VAR_STRING)
1900 {
1901 fi->fi_byte_idx = 0;
1902 fi->fi_string = tv.vval.v_string;
1903 tv.vval.v_string = NULL;
1904 if (fi->fi_string == NULL)
1905 fi->fi_string = vim_strsave((char_u *)"");
1906 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907 else
1908 {
Sean Dewar80d73952021-08-04 19:25:54 +02001909 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001910 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001911 }
1912 }
1913 }
1914 if (skip)
1915 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001916 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001917
1918 return fi;
1919}
1920
1921/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001922 * Used when looping over a :for line, skip the "in expr" part.
1923 */
1924 void
1925skip_for_lines(void *fi_void, evalarg_T *evalarg)
1926{
1927 forinfo_T *fi = (forinfo_T *)fi_void;
1928 int i;
1929
1930 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001931 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001932}
1933
1934/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 * Use the first item in a ":for" list. Advance to the next.
1936 * Assign the values to the variable (list). "arg" points to the first one.
1937 * Return TRUE when a valid item was found, FALSE when at end of list or
1938 * something wrong.
1939 */
1940 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001941next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001942{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001943 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001945 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001946 ? (ASSIGN_FINAL
1947 // first round: error if variable exists
1948 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001949 | ASSIGN_NO_MEMBER_TYPE
1950 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001951 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001952 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001953 int skip_assign = in_vim9script() && arg[0] == '_'
1954 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001956 if (fi->fi_blob != NULL)
1957 {
1958 typval_T tv;
1959
1960 if (fi->fi_bi >= blob_len(fi->fi_blob))
1961 return FALSE;
1962 tv.v_type = VAR_NUMBER;
1963 tv.v_lock = VAR_FIXED;
1964 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1965 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001966 if (skip_assign)
1967 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001968 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001969 fi->fi_varcount, flag, NULL) == OK;
1970 }
1971
1972 if (fi->fi_string != NULL)
1973 {
1974 typval_T tv;
1975 int len;
1976
1977 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1978 if (len == 0)
1979 return FALSE;
1980 tv.v_type = VAR_STRING;
1981 tv.v_lock = VAR_FIXED;
1982 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1983 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001984 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001985 if (skip_assign)
1986 result = TRUE;
1987 else
1988 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001989 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001990 vim_free(tv.vval.v_string);
1991 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001992 }
1993
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 item = fi->fi_lw.lw_item;
1995 if (item == NULL)
1996 result = FALSE;
1997 else
1998 {
1999 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02002000 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02002001 if (skip_assign)
2002 result = TRUE;
2003 else
2004 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002005 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002006 }
2007 return result;
2008}
2009
2010/*
2011 * Free the structure used to store info used by ":for".
2012 */
2013 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002014free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002015{
Bram Moolenaar33570922005-01-25 22:26:29 +00002016 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002018 if (fi == NULL)
2019 return;
2020 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002021 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002022 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002023 list_unref(fi->fi_list);
2024 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002025 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002026 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002027 else
2028 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 vim_free(fi);
2030}
2031
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002033set_context_for_expression(
2034 expand_T *xp,
2035 char_u *arg,
2036 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002038 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002040 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002042 if (cmdidx == CMD_let || cmdidx == CMD_var
2043 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002044 {
2045 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002046 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002047 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002048 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002049 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002050 {
2051 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002052 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002053 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 break;
2055 }
2056 return;
2057 }
2058 }
2059 else
2060 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2061 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 while ((xp->xp_pattern = vim_strpbrk(arg,
2063 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2064 {
2065 c = *xp->xp_pattern;
2066 if (c == '&')
2067 {
2068 c = xp->xp_pattern[1];
2069 if (c == '&')
2070 {
2071 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002072 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 }
2074 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002075 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002077 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2078 xp->xp_pattern += 2;
2079
2080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 }
2082 else if (c == '$')
2083 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002084 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 xp->xp_context = EXPAND_ENV_VARS;
2086 }
2087 else if (c == '=')
2088 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002089 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 xp->xp_context = EXPAND_EXPRESSION;
2091 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002092 else if (c == '#'
2093 && xp->xp_context == EXPAND_EXPRESSION)
2094 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002095 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002096 break;
2097 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002098 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 && xp->xp_context == EXPAND_FUNCTIONS
2100 && vim_strchr(xp->xp_pattern, '(') == NULL)
2101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002102 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 break;
2104 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002105 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002107 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108 {
2109 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2110 if (c == '\\' && xp->xp_pattern[1] != NUL)
2111 ++xp->xp_pattern;
2112 xp->xp_context = EXPAND_NOTHING;
2113 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002114 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002116 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2118 /* skip */ ;
2119 xp->xp_context = EXPAND_NOTHING;
2120 }
2121 else if (c == '|')
2122 {
2123 if (xp->xp_pattern[1] == '|')
2124 {
2125 ++xp->xp_pattern;
2126 xp->xp_context = EXPAND_EXPRESSION;
2127 }
2128 else
2129 xp->xp_context = EXPAND_COMMANDS;
2130 }
2131 else
2132 xp->xp_context = EXPAND_EXPRESSION;
2133 }
2134 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002135 // Doesn't look like something valid, expand as an expression
2136 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138 arg = xp->xp_pattern;
2139 if (*arg != NUL)
2140 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2141 /* skip */ ;
2142 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002143
2144 // ":exe one two" completes "two"
2145 if ((cmdidx == CMD_execute
2146 || cmdidx == CMD_echo
2147 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002148 || cmdidx == CMD_echomsg
2149 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002150 && xp->xp_context == EXPAND_EXPRESSION)
2151 {
2152 for (;;)
2153 {
2154 char_u *n = skiptowhite(arg);
2155
2156 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2157 break;
2158 arg = skipwhite(n);
2159 }
2160 }
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 xp->xp_pattern = arg;
2163}
2164
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002166 * Return TRUE if "pat" matches "text".
2167 * Does not use 'cpo' and always uses 'magic'.
2168 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002169 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002170pattern_match(char_u *pat, char_u *text, int ic)
2171{
2172 int matches = FALSE;
2173 char_u *save_cpo;
2174 regmatch_T regmatch;
2175
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002176 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002177 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002178 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002179 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2180 if (regmatch.regprog != NULL)
2181 {
2182 regmatch.rm_ic = ic;
2183 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2184 vim_regfree(regmatch.regprog);
2185 }
2186 p_cpo = save_cpo;
2187 return matches;
2188}
2189
2190/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002191 * Handle a name followed by "(". Both for just "name(arg)" and for
2192 * "expr->name(arg)".
2193 * Returns OK or FAIL.
2194 */
2195 static int
2196eval_func(
2197 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002198 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002199 char_u *name,
2200 int name_len,
2201 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002202 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002203 typval_T *basetv) // "expr" for "expr->name(arg)"
2204{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002205 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002206 char_u *s = name;
2207 int len = name_len;
2208 partial_T *partial;
2209 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002210 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002211 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002212
2213 if (!evaluate)
2214 check_vars(s, len);
2215
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002216 // If "s" is the name of a variable of type VAR_FUNC
2217 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002218 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002219 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002220
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002221 // Need to make a copy, in case evaluating the arguments makes
2222 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002223 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002224 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002225 ret = FAIL;
2226 else
2227 {
2228 funcexe_T funcexe;
2229
2230 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002231 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002232 funcexe.fe_firstline = curwin->w_cursor.lnum;
2233 funcexe.fe_lastline = curwin->w_cursor.lnum;
2234 funcexe.fe_evaluate = evaluate;
2235 funcexe.fe_partial = partial;
2236 funcexe.fe_basetv = basetv;
2237 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002238 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002239 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002240 }
2241 vim_free(s);
2242
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002243 // If evaluate is FALSE rettv->v_type was not set in
2244 // get_func_tv, but it's needed in handle_subscript() to parse
2245 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2247 {
2248 rettv->vval.v_string = NULL;
2249 rettv->v_type = VAR_FUNC;
2250 }
2251
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002252 // Stop the expression evaluation when immediately
2253 // aborting on error, or when an interrupt occurred or
2254 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002255 if (evaluate && aborting())
2256 {
2257 if (ret == OK)
2258 clear_tv(rettv);
2259 ret = FAIL;
2260 }
2261 return ret;
2262}
2263
2264/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002265 * After a NL, skip over empty lines and comment-only lines.
2266 */
2267 static char_u *
2268newline_skip_comments(char_u *arg)
2269{
2270 char_u *p = arg + 1;
2271
2272 for (;;)
2273 {
2274 p = skipwhite(p);
2275
2276 if (*p == NUL)
2277 break;
2278 if (vim9_comment_start(p))
2279 {
2280 char_u *nl = vim_strchr(p, NL);
2281
2282 if (nl == NULL)
2283 break;
2284 p = nl;
2285 }
2286 if (*p != NL)
2287 break;
2288 ++p; // skip another NL
2289 }
2290 return p;
2291}
2292
2293/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002294 * Get the next line source line without advancing. But do skip over comment
2295 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002296 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002297 */
2298 static char_u *
2299getline_peek_skip_comments(evalarg_T *evalarg)
2300{
2301 for (;;)
2302 {
2303 char_u *next = getline_peek(evalarg->eval_getline,
2304 evalarg->eval_cookie);
2305 char_u *p;
2306
2307 if (next == NULL)
2308 break;
2309 p = skipwhite(next);
2310 if (*p != NUL && !vim9_comment_start(p))
2311 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002312 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002313 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002314 }
2315 return NULL;
2316}
2317
2318/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002319 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2320 * comment) and there is a next line, return the next line (skipping blanks)
2321 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002322 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2323 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002324 * "arg" must point somewhere inside a line, not at the start.
2325 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002326 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002327eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2328{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002329 char_u *p = skipwhite(arg);
2330
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002331 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002332 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002333 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002334 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2335 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002336 && (*p == NUL || *p == NL
2337 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002338 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002339 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002340
Bram Moolenaare442d592022-05-05 12:20:28 +01002341 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002342 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002343 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002344 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002345 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002346 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002347
Bram Moolenaar9d489562020-07-30 20:08:50 +02002348 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002349 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002350 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002351 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002352 }
2353 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002354 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002355}
2356
2357/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002358 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002359 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002360 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002361 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002362eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002363{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002364 garray_T *gap = &evalarg->eval_ga;
2365 char_u *line;
2366
Bram Moolenaar39be4982022-05-06 21:51:50 +01002367 if (arg != NULL)
2368 {
2369 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002370 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002371 // Truncate before a trailing comment, so that concatenating the lines
2372 // won't turn the rest into a comment.
2373 if (*skipwhite(arg) == '#')
2374 *arg = NUL;
2375 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002376
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002377 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002378 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2379 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002380 else
2381 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002382 if (line == NULL)
2383 return NULL;
2384
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002385 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002386 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2387 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002388 char_u *p = skipwhite(line);
2389
2390 // Going to concatenate the lines after parsing. For an empty or
2391 // comment line use an empty string.
2392 if (*p == NUL || vim9_comment_start(p))
2393 {
2394 vim_free(line);
2395 line = vim_strsave((char_u *)"");
2396 }
2397
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002398 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2399 ++gap->ga_len;
2400 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002401 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002402 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002403 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002404 evalarg->eval_tofree = line;
2405 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002406
2407 // Advanced to the next line, "arg" no longer points into the previous
2408 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002409 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002410 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002411}
2412
Bram Moolenaare6b53242020-07-01 17:28:33 +02002413/*
2414 * Call eval_next_non_blank() and get the next line if needed.
2415 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002416 char_u *
2417skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2418{
2419 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002420 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002421
Bram Moolenaar962d7212020-07-04 14:15:00 +02002422 if (evalarg == NULL)
2423 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002424 eval_next_non_blank(p, evalarg, &getnext);
2425 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002426 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002427 return p;
2428}
2429
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002432 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2434 */
2435
2436/*
2437 * Handle zero level expression.
2438 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002440 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002441 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 * Return OK or FAIL.
2443 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002444 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002445eval0(
2446 char_u *arg,
2447 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002448 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002449 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002451 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2452}
2453
2454/*
2455 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2456 * expression and don't check what comes after the expression.
2457 */
2458 int
2459eval0_retarg(
2460 char_u *arg,
2461 typval_T *rettv,
2462 exarg_T *eap,
2463 evalarg_T *evalarg,
2464 char_u **retarg)
2465{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 int ret;
2467 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002468 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002469 int did_emsg_before = did_emsg;
2470 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002471 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002472 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002473 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474
2475 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002476 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002477
Bram Moolenaar79481362022-06-27 20:15:10 +01002478 if (ret != FAIL)
2479 {
2480 expr_end = p;
2481 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002482
Bram Moolenaar79481362022-06-27 20:15:10 +01002483 // In Vim9 script a command block is not split at NL characters for
2484 // commands using an expression argument. Skip over a '#' comment to
2485 // check for a following NL. Require white space before the '#'.
2486 if (in_vim9script() && p > expr_end && retarg == NULL)
2487 while (*p == '#')
2488 {
2489 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002490
Bram Moolenaar79481362022-06-27 20:15:10 +01002491 if (nl == NULL)
2492 break;
2493 p = skipwhite(nl + 1);
2494 if (eap != NULL && *p != NUL)
2495 eap->nextcmd = p;
2496 check_for_end = FALSE;
2497 }
2498
2499 if (check_for_end)
2500 end_error = !ends_excmd2(arg, p);
2501 }
2502
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002503 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 {
2505 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002506 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 /*
2508 * Report the invalid expression unless the expression evaluation has
2509 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002510 * exception, or we already gave a more specific error.
2511 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002513 if (!aborting()
2514 && did_emsg == did_emsg_before
2515 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002516 && (flags & EVAL_CONSTANT) == 0
2517 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002518 {
2519 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002520 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002521 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002522 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002523 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002524
2525 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002526 // a next command to avoid more errors, unless "|" is following, which
2527 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002528 if (eap != NULL && p != NULL
2529 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002530 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002531 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002533
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002534 if (retarg != NULL)
2535 *retarg = p;
2536 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002537 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002538
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 return ret;
2540}
2541
2542/*
2543 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002544 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002545 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 *
2547 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002548 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002550 * Note: "rettv.v_lock" is not set.
2551 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 * Return OK or FAIL.
2553 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002554 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002555eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002557 char_u *p;
2558 int getnext;
2559
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002560 CLEAR_POINTER(rettv);
2561
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 /*
2563 * Get the first variable.
2564 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002565 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 return FAIL;
2567
Bram Moolenaar793648f2020-06-26 21:28:25 +02002568 p = eval_next_non_blank(*arg, evalarg, &getnext);
2569 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002571 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002572 int result;
2573 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002574 evalarg_T *evalarg_used = evalarg;
2575 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002576 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002577 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002578 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002579
2580 if (evalarg == NULL)
2581 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002582 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002583 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585 orig_flags = evalarg_used->eval_flags;
2586 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002588 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002589 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002590 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002591 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002592 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002593 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002594 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002595 clear_tv(rettv);
2596 return FAIL;
2597 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002598 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002599 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002600
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002602 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002604 int error = FALSE;
2605
Bram Moolenaar13106602020-10-04 16:06:05 +02002606 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002607 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002608 else if (vim9script)
2609 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002610 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002612 if (error || !op_falsy || !result)
2613 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002614 if (error)
2615 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 }
2617
2618 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002619 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002621 if (op_falsy)
2622 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002623 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002624 {
mityu4ac198c2021-05-28 17:52:40 +02002625 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002626 clear_tv(rettv);
2627 return FAIL;
2628 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002629 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002630 evalarg_used->eval_flags = (op_falsy ? !result : result)
2631 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2632 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002633 {
2634 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002636 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002637 if (!op_falsy || !result)
2638 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002640 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002642 /*
2643 * Check for the ":".
2644 */
2645 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2646 if (*p != ':')
2647 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002648 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002649 if (evaluate && result)
2650 clear_tv(rettv);
2651 evalarg_used->eval_flags = orig_flags;
2652 return FAIL;
2653 }
2654 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002655 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002656 else
2657 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002658 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002659 {
2660 error_white_both(p, 1);
2661 clear_tv(rettv);
2662 evalarg_used->eval_flags = orig_flags;
2663 return FAIL;
2664 }
2665 *arg = p;
2666 }
2667
2668 /*
2669 * Get the third variable. Recursive!
2670 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002671 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002672 {
mityu4ac198c2021-05-28 17:52:40 +02002673 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002674 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002675 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002676 return FAIL;
2677 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002678 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2679 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002680 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002681 if (eval1(arg, &var2, evalarg_used) == FAIL)
2682 {
2683 if (evaluate && result)
2684 clear_tv(rettv);
2685 evalarg_used->eval_flags = orig_flags;
2686 return FAIL;
2687 }
2688 if (evaluate && !result)
2689 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002691
2692 if (evalarg == NULL)
2693 clear_evalarg(&local_evalarg, NULL);
2694 else
2695 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 }
2697
2698 return OK;
2699}
2700
2701/*
2702 * Handle first level expression:
2703 * expr2 || expr2 || expr2 logical OR
2704 *
2705 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002706 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 *
2708 * Return OK or FAIL.
2709 */
2710 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002711eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002713 char_u *p;
2714 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002717 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002719 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 return FAIL;
2721
2722 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002723 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002725 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002726 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002728 evalarg_T *evalarg_used = evalarg;
2729 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002730 int evaluate;
2731 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002732 long result = FALSE;
2733 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002734 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002735 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002736
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002737 if (evalarg == NULL)
2738 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002739 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002740 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002741 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002742 orig_flags = evalarg_used->eval_flags;
2743 evaluate = orig_flags & EVAL_EVALUATE;
2744 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002745 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002746 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002747 result = tv_get_bool_chk(rettv, &error);
2748 else if (tv_get_number_chk(rettv, &error) != 0)
2749 result = TRUE;
2750 clear_tv(rettv);
2751 if (error)
2752 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 }
2754
2755 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002756 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002758 while (p[0] == '|' && p[1] == '|')
2759 {
2760 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002761 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002762 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002763 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002764 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002765 {
2766 error_white_both(p, 2);
2767 clear_tv(rettv);
2768 return FAIL;
2769 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002770 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002771 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002772
2773 /*
2774 * Get the second variable.
2775 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002776 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002777 {
mityu4ac198c2021-05-28 17:52:40 +02002778 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002779 clear_tv(rettv);
2780 return FAIL;
2781 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002782 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2783 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002784 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002785 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002786 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002787
2788 /*
2789 * Compute the result.
2790 */
2791 if (evaluate && !result)
2792 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002793 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002794 result = tv_get_bool_chk(&var2, &error);
2795 else if (tv_get_number_chk(&var2, &error) != 0)
2796 result = TRUE;
2797 clear_tv(&var2);
2798 if (error)
2799 return FAIL;
2800 }
2801 if (evaluate)
2802 {
2803 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002804 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002805 rettv->v_type = VAR_BOOL;
2806 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002807 }
2808 else
2809 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002810 rettv->v_type = VAR_NUMBER;
2811 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002812 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002813 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002814
2815 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002817
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002818 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 second level expression:
2829 * expr3 && expr3 && expr3 logical AND
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 +02002837eval3(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 (eval4(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 orig_flags;
2857 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002858 long result = TRUE;
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 = FALSE;
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 Moolenaar2bb26582020-10-03 22:52:39 +02002890 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 Moolenaarc1ec0422020-09-09 22:27:58 +02002911 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002912 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002913 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002914
2915 /*
2916 * Compute the result.
2917 */
2918 if (evaluate && result)
2919 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002920 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002921 result = tv_get_bool_chk(&var2, &error);
2922 else if (tv_get_number_chk(&var2, &error) == 0)
2923 result = FALSE;
2924 clear_tv(&var2);
2925 if (error)
2926 return FAIL;
2927 }
2928 if (evaluate)
2929 {
2930 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002931 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002932 rettv->v_type = VAR_BOOL;
2933 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002934 }
2935 else
2936 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002937 rettv->v_type = VAR_NUMBER;
2938 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002939 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002940 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002941
2942 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002943 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002944
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002945 if (evalarg == NULL)
2946 clear_evalarg(&local_evalarg, NULL);
2947 else
2948 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 }
2950
2951 return OK;
2952}
2953
2954/*
2955 * Handle third level expression:
2956 * var1 == var2
2957 * var1 =~ var2
2958 * var1 != var2
2959 * var1 !~ var2
2960 * var1 > var2
2961 * var1 >= var2
2962 * var1 < var2
2963 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002964 * var1 is var2
2965 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 *
2967 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002968 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 *
2970 * Return OK or FAIL.
2971 */
2972 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002973eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002976 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002977 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002979 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980
2981 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002982 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002984 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 return FAIL;
2986
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002987 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002988
Bram Moolenaar696ba232020-07-29 21:20:41 +02002989 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
2991 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002992 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002994 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002996 typval_T var2;
2997 int ic;
2998 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002999 int evaluate = evalarg == NULL
3000 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003001 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003002
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003003 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02003004 {
Bram Moolenaare442d592022-05-05 12:20:28 +01003005 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02003006 p = *arg;
3007 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003008 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3009 {
mityu4ac198c2021-05-28 17:52:40 +02003010 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003011 clear_tv(rettv);
3012 return FAIL;
3013 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02003014
Bram Moolenaar696ba232020-07-29 21:20:41 +02003015 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3016 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003017 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003018 clear_tv(rettv);
3019 return FAIL;
3020 }
3021
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003022 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 if (p[len] == '?')
3024 {
3025 ic = TRUE;
3026 ++len;
3027 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003028 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 else if (p[len] == '#')
3030 {
3031 ic = FALSE;
3032 ++len;
3033 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003034 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003036 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
3038 /*
3039 * Get the second variable.
3040 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003041 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3042 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003043 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003044 clear_tv(rettv);
3045 return FAIL;
3046 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003047 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003048 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003050 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 return FAIL;
3052 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003053 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003054 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003055 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003056
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003057 // use the line of the comparison for messages
3058 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003059 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003060 {
3061 ret = FAIL;
3062 clear_tv(rettv);
3063 }
3064 else
3065 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003066 clear_tv(&var2);
3067 return ret;
3068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 }
3070
3071 return OK;
3072}
3073
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003074/*
3075 * Make a copy of blob "tv1" and append blob "tv2".
3076 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 void
3078eval_addblob(typval_T *tv1, typval_T *tv2)
3079{
3080 blob_T *b1 = tv1->vval.v_blob;
3081 blob_T *b2 = tv2->vval.v_blob;
3082 blob_T *b = blob_alloc();
3083 int i;
3084
3085 if (b != NULL)
3086 {
3087 for (i = 0; i < blob_len(b1); i++)
3088 ga_append(&b->bv_ga, blob_get(b1, i));
3089 for (i = 0; i < blob_len(b2); i++)
3090 ga_append(&b->bv_ga, blob_get(b2, i));
3091
3092 clear_tv(tv1);
3093 rettv_blob_set(tv1, b);
3094 }
3095}
3096
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003097/*
3098 * Make a copy of list "tv1" and append list "tv2".
3099 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 int
3101eval_addlist(typval_T *tv1, typval_T *tv2)
3102{
3103 typval_T var3;
3104
3105 // concatenate Lists
3106 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3107 {
3108 clear_tv(tv1);
3109 clear_tv(tv2);
3110 return FAIL;
3111 }
3112 clear_tv(tv1);
3113 *tv1 = var3;
3114 return OK;
3115}
3116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003118 * Handle the bitwise left/right shift operator expression:
3119 * var1 << var2
3120 * var1 >> var2
3121 *
3122 * "arg" must point to the first non-white of the expression.
3123 * "arg" is advanced to just after the recognized expression.
3124 *
3125 * Return OK or FAIL.
3126 */
3127 static int
3128eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3129{
3130 /*
3131 * Get the first expression.
3132 */
3133 if (eval6(arg, rettv, evalarg) == FAIL)
3134 return FAIL;
3135
3136 /*
3137 * Repeat computing, until no '<<' or '>>' is following.
3138 */
3139 for (;;)
3140 {
3141 char_u *p;
3142 int getnext;
3143 exprtype_T type;
3144 int evaluate;
3145 typval_T var2;
3146 int vim9script;
3147
3148 p = eval_next_non_blank(*arg, evalarg, &getnext);
3149 if (p[0] == '<' && p[1] == '<')
3150 type = EXPR_LSHIFT;
3151 else if (p[0] == '>' && p[1] == '>')
3152 type = EXPR_RSHIFT;
3153 else
3154 return OK;
3155
3156 // Handle a bitwise left or right shift operator
3157 if (rettv->v_type != VAR_NUMBER)
3158 {
3159 // left operand should be a number
3160 emsg(_(e_bitshift_ops_must_be_number));
3161 clear_tv(rettv);
3162 return FAIL;
3163 }
3164
3165 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3166 vim9script = in_vim9script();
3167 if (getnext)
3168 {
3169 *arg = eval_next_line(*arg, evalarg);
3170 p = *arg;
3171 }
3172 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3173 {
3174 error_white_both(*arg, 2);
3175 clear_tv(rettv);
3176 return FAIL;
3177 }
3178
3179 /*
3180 * Get the second variable.
3181 */
3182 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3183 {
3184 error_white_both(p, 2);
3185 clear_tv(rettv);
3186 return FAIL;
3187 }
3188 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3189 if (eval6(arg, &var2, evalarg) == FAIL)
3190 {
3191 clear_tv(rettv);
3192 return FAIL;
3193 }
3194
3195 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3196 {
3197 // right operand should be a positive number
3198 if (var2.v_type != VAR_NUMBER)
3199 emsg(_(e_bitshift_ops_must_be_number));
3200 else
3201 emsg(_(e_bitshift_ops_must_be_postive));
3202 clear_tv(rettv);
3203 clear_tv(&var2);
3204 return FAIL;
3205 }
3206
3207 if (evaluate)
3208 {
3209 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3210 // shifting more bits than we have always results in zero
3211 rettv->vval.v_number = 0;
3212 else if (type == EXPR_LSHIFT)
3213 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003214 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003215 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003216 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003217 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003218 }
3219
3220 clear_tv(&var2);
3221 }
3222
3223 return OK;
3224}
3225
3226/*
3227 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003228 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003230 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003231 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 *
3233 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003234 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 *
3236 * Return OK or FAIL.
3237 */
3238 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003239eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003242 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003244 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 return FAIL;
3246
3247 /*
3248 * Repeat computing, until no '+', '-' or '.' is following.
3249 */
3250 for (;;)
3251 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003252 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003253 int getnext;
3254 char_u *p;
3255 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003256 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003257 int concat;
3258 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003259 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003260
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003261 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003262 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003263 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003264 p = eval_next_non_blank(*arg, evalarg, &getnext);
3265 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003266 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003267 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3268 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003270 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3271 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003272
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003273 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003274 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003275 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003276 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003277 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003278 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003279 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003280 {
mityu4ac198c2021-05-28 17:52:40 +02003281 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003282 clear_tv(rettv);
3283 return FAIL;
3284 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003285 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003286 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003287 if ((op != '+' || (rettv->v_type != VAR_LIST
3288 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003289 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003290 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003291 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003292 int error = FALSE;
3293
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003294 // For "list + ...", an illegal use of the first operand as
3295 // a number cannot be determined before evaluating the 2nd
3296 // operand: if this is also a list, all is ok.
3297 // For "something . ...", "something - ..." or "non-list + ...",
3298 // we know that the first operand needs to be a string or number
3299 // without evaluating the 2nd operand. So check before to avoid
3300 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003301 if (op != '.')
3302 tv_get_number_chk(rettv, &error);
3303 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003304 {
3305 clear_tv(rettv);
3306 return FAIL;
3307 }
3308 }
3309
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 /*
3311 * Get the second variable.
3312 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003313 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003314 {
mityu89dcb4d2021-05-28 13:50:17 +02003315 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003316 clear_tv(rettv);
3317 return FAIL;
3318 }
3319 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003320 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003322 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 return FAIL;
3324 }
3325
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003326 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 {
3328 /*
3329 * Compute the result.
3330 */
3331 if (op == '.')
3332 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003333 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3334 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003335 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003336
Bram Moolenaar2e086612020-08-25 22:37:48 +02003337 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003338 || var2.v_type == VAR_CHANNEL
3339 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003340 semsg(_(e_using_invalid_value_as_string_str),
3341 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003342 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003343 {
3344 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3345 var2.vval.v_float);
3346 s2 = buf2;
3347 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003348 else
3349 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003350 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003351 {
3352 clear_tv(rettv);
3353 clear_tv(&var2);
3354 return FAIL;
3355 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003356 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003357 clear_tv(rettv);
3358 rettv->v_type = VAR_STRING;
3359 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003361 else if (op == '+' && rettv->v_type == VAR_BLOB
3362 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003364 else if (op == '+' && rettv->v_type == VAR_LIST
3365 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003366 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003368 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 else
3371 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003372 int error = FALSE;
3373 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003374 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003375
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003376 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003377 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003378 f1 = rettv->vval.v_float;
3379 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003380 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003381 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003382 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003383 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003384 if (error)
3385 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003386 // This can only happen for "list + non-list" or
3387 // "blob + non-blob". For "non-list + ..." or
3388 // "something - ...", we returned before evaluating the
3389 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003390 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003391 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003392 return FAIL;
3393 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003394 if (var2.v_type == VAR_FLOAT)
3395 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003396 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003397 if (var2.v_type == VAR_FLOAT)
3398 {
3399 f2 = var2.vval.v_float;
3400 n2 = 0;
3401 }
3402 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003403 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003404 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003405 if (error)
3406 {
3407 clear_tv(rettv);
3408 clear_tv(&var2);
3409 return FAIL;
3410 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003411 if (rettv->v_type == VAR_FLOAT)
3412 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003414 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003415
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003416 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003417 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3418 {
3419 if (op == '+')
3420 f1 = f1 + f2;
3421 else
3422 f1 = f1 - f2;
3423 rettv->v_type = VAR_FLOAT;
3424 rettv->vval.v_float = f1;
3425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003427 {
3428 if (op == '+')
3429 n1 = n1 + n2;
3430 else
3431 n1 = n1 - n2;
3432 rettv->v_type = VAR_NUMBER;
3433 rettv->vval.v_number = n1;
3434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003436 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 }
3438 }
3439 return OK;
3440}
3441
3442/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003443 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 * * number multiplication
3445 * / number division
3446 * % number modulo
3447 *
3448 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003449 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 *
3451 * Return OK or FAIL.
3452 */
3453 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003454eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455 char_u **arg,
3456 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003457 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003458 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003460 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461
3462 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003463 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003465 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 return FAIL;
3467
3468 /*
3469 * Repeat computing, until no '*', '/' or '%' is following.
3470 */
3471 for (;;)
3472 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003473 int evaluate;
3474 int getnext;
3475 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003476 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003477 int op;
3478 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003479 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003480 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003481
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003482 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003483 p = eval_next_non_blank(*arg, evalarg, &getnext);
3484 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003485 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003487
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003488 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003489 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003490 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003491 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003492 {
3493 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3494 {
mityu4ac198c2021-05-28 17:52:40 +02003495 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003496 clear_tv(rettv);
3497 return FAIL;
3498 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003499 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003502 f1 = 0;
3503 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003504 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003505 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003507 if (rettv->v_type == VAR_FLOAT)
3508 {
3509 f1 = rettv->vval.v_float;
3510 use_float = TRUE;
3511 n1 = 0;
3512 }
3513 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003514 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003515 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003516 if (error)
3517 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519 else
3520 n1 = 0;
3521
3522 /*
3523 * Get the second variable.
3524 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003525 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3526 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003527 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003528 clear_tv(rettv);
3529 return FAIL;
3530 }
3531 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003532 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 return FAIL;
3534
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003535 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003537 if (var2.v_type == VAR_FLOAT)
3538 {
3539 if (!use_float)
3540 {
3541 f1 = n1;
3542 use_float = TRUE;
3543 }
3544 f2 = var2.vval.v_float;
3545 n2 = 0;
3546 }
3547 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003549 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003550 clear_tv(&var2);
3551 if (error)
3552 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003553 if (use_float)
3554 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556
3557 /*
3558 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003559 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003561 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003563 if (op == '*')
3564 f1 = f1 * f2;
3565 else if (op == '/')
3566 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003567#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003568 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003569 if (f2 == 0.0)
3570 {
3571 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003572 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003573 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003574 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003575 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003576 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003577 }
3578 else
3579 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003580#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003581 // We rely on the floating point library to handle divide
3582 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003583 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003584#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003587 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003588 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003589 return FAIL;
3590 }
3591 rettv->v_type = VAR_FLOAT;
3592 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 }
3594 else
3595 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003596 int failed = FALSE;
3597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003598 if (op == '*')
3599 n1 = n1 * n2;
3600 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003601 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003603 n1 = num_modulus(n1, n2, &failed);
3604 if (failed)
3605 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003606
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003607 rettv->v_type = VAR_NUMBER;
3608 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 }
3611 }
3612
3613 return OK;
3614}
3615
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003616/*
3617 * Handle a type cast before a base level expression.
3618 * "arg" must point to the first non-white of the expression.
3619 * "arg" is advanced to just after the recognized expression.
3620 * Return OK or FAIL.
3621 */
3622 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003623eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003624 char_u **arg,
3625 typval_T *rettv,
3626 evalarg_T *evalarg,
3627 int want_string) // after "." operator
3628{
3629 type_T *want_type = NULL;
3630 garray_T type_list; // list of pointers to allocated types
3631 int res;
3632 int evaluate = evalarg == NULL ? 0
3633 : (evalarg->eval_flags & EVAL_EVALUATE);
3634
3635 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003636 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3637 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003638 {
3639 ++*arg;
3640 ga_init2(&type_list, sizeof(type_T *), 10);
3641 want_type = parse_type(arg, &type_list, TRUE);
3642 if (want_type == NULL && (evaluate || **arg != '>'))
3643 {
3644 clear_type_list(&type_list);
3645 return FAIL;
3646 }
3647
3648 if (**arg != '>')
3649 {
3650 if (*skipwhite(*arg) == '>')
3651 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3652 else
3653 emsg(_(e_missing_gt));
3654 clear_type_list(&type_list);
3655 return FAIL;
3656 }
3657 ++*arg;
3658 *arg = skipwhite_and_linebreak(*arg, evalarg);
3659 }
3660
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003661 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003662
3663 if (want_type != NULL && evaluate)
3664 {
3665 if (res == OK)
3666 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003667 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3668 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003669
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003670 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003671 {
3672 if (want_type == &t_bool && actual != &t_bool
3673 && (actual->tt_flags & TTFLAG_BOOL_OK))
3674 {
3675 int n = tv2bool(rettv);
3676
3677 // can use "0" and "1" for boolean in some places
3678 clear_tv(rettv);
3679 rettv->v_type = VAR_BOOL;
3680 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3681 }
3682 else
3683 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003684 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003685
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003686 where.wt_variable = TRUE;
3687 res = check_type(want_type, actual, TRUE, where);
3688 }
3689 }
3690 }
3691 clear_type_list(&type_list);
3692 }
3693
3694 return res;
3695}
3696
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003697 int
3698eval_leader(char_u **arg, int vim9)
3699{
3700 char_u *s = *arg;
3701 char_u *p = *arg;
3702
3703 while (*p == '!' || *p == '-' || *p == '+')
3704 {
3705 char_u *n = skipwhite(p + 1);
3706
3707 // ++, --, -+ and +- are not accepted in Vim9 script
3708 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3709 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003710 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003711 return FAIL;
3712 }
3713 p = n;
3714 }
3715 *arg = p;
3716 return OK;
3717}
3718
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003720 * Check for a predefined value "true", "false" and "null.*".
3721 * Return OK when recognized.
3722 */
3723 int
3724handle_predefined(char_u *s, int len, typval_T *rettv)
3725{
3726 switch (len)
3727 {
3728 case 4: if (STRNCMP(s, "true", 4) == 0)
3729 {
3730 rettv->v_type = VAR_BOOL;
3731 rettv->vval.v_number = VVAL_TRUE;
3732 return OK;
3733 }
3734 if (STRNCMP(s, "null", 4) == 0)
3735 {
3736 rettv->v_type = VAR_SPECIAL;
3737 rettv->vval.v_number = VVAL_NULL;
3738 return OK;
3739 }
3740 break;
3741 case 5: if (STRNCMP(s, "false", 5) == 0)
3742 {
3743 rettv->v_type = VAR_BOOL;
3744 rettv->vval.v_number = VVAL_FALSE;
3745 return OK;
3746 }
3747 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003748 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3749 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003750#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003751 rettv->v_type = VAR_JOB;
3752 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003753#else
3754 rettv->v_type = VAR_SPECIAL;
3755 rettv->vval.v_number = VVAL_NULL;
3756#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003757 return OK;
3758 }
3759 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003760 case 9:
3761 if (STRNCMP(s, "null_", 5) != 0)
3762 break;
3763 if (STRNCMP(s + 5, "list", 4) == 0)
3764 {
3765 rettv->v_type = VAR_LIST;
3766 rettv->vval.v_list = NULL;
3767 return OK;
3768 }
3769 if (STRNCMP(s + 5, "dict", 4) == 0)
3770 {
3771 rettv->v_type = VAR_DICT;
3772 rettv->vval.v_dict = NULL;
3773 return OK;
3774 }
3775 if (STRNCMP(s + 5, "blob", 4) == 0)
3776 {
3777 rettv->v_type = VAR_BLOB;
3778 rettv->vval.v_blob = NULL;
3779 return OK;
3780 }
3781 break;
3782 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3783 {
3784 rettv->v_type = VAR_STRING;
3785 rettv->vval.v_string = NULL;
3786 return OK;
3787 }
3788 break;
3789 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003790 if (STRNCMP(s, "null_channel", 12) == 0)
3791 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003793 rettv->v_type = VAR_CHANNEL;
3794 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003795#else
3796 rettv->v_type = VAR_SPECIAL;
3797 rettv->vval.v_number = VVAL_NULL;
3798#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003799 return OK;
3800 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003801 if (STRNCMP(s, "null_partial", 12) == 0)
3802 {
3803 rettv->v_type = VAR_PARTIAL;
3804 rettv->vval.v_partial = NULL;
3805 return OK;
3806 }
3807 break;
3808 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3809 {
3810 rettv->v_type = VAR_FUNC;
3811 rettv->vval.v_string = NULL;
3812 return OK;
3813 }
3814 break;
3815 }
3816 return FAIL;
3817}
3818
3819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 * Handle sixth level expression:
3821 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003822 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003823 * "string" string constant
3824 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 * &option-name option value
3826 * @r register contents
3827 * identifier variable value
3828 * function() function call
3829 * $VAR environment variable
3830 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003831 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003833 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003834 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 *
3836 * Also handle:
3837 * ! in front logical NOT
3838 * - in front unary minus
3839 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003840 * trailing [] subscript in String or List
3841 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003842 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 *
3844 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003845 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 *
3847 * Return OK or FAIL.
3848 */
3849 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003850eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003851 char_u **arg,
3852 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003853 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003856 int evaluate = evalarg != NULL
3857 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 int len;
3859 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003860 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 char_u *start_leader, *end_leader;
3862 int ret = OK;
3863 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003864 static int recurse = 0;
3865 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866
3867 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003868 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003869 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003871 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872
3873 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003874 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 */
3876 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003877 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003878 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 end_leader = *arg;
3880
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003881 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003882 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003883 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003884 ++*arg;
3885 return FAIL;
3886 }
3887
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003888 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003889 // and crash. With MSVC the stack is smaller.
3890 if (recurse ==
3891#ifdef _MSC_VER
3892 300
3893#else
3894 1000
3895#endif
3896 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003897 {
3898 semsg(_(e_expression_too_recursive_str), *arg);
3899 return FAIL;
3900 }
3901 ++recurse;
3902
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 switch (**arg)
3904 {
3905 /*
3906 * Number constant.
3907 */
3908 case '0':
3909 case '1':
3910 case '2':
3911 case '3':
3912 case '4':
3913 case '5':
3914 case '6':
3915 case '7':
3916 case '8':
3917 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003918 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003919
3920 // Apply prefixed "-" and "+" now. Matters especially when
3921 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003922 if (ret == OK && evaluate && end_leader > start_leader
3923 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003924 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 break;
3926
3927 /*
3928 * String constant: "string".
3929 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003930 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 break;
3932
3933 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003934 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003936 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003937 break;
3938
3939 /*
3940 * List: [expr, expr]
3941 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003942 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 break;
3944
3945 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003946 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003947 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003948 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003949 {
3950 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3951 }
3952 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003953 {
3954 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003955 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003956 }
3957 else
3958 ret = NOTDONE;
3959 break;
3960
3961 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003962 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003963 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003964 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003965 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003966 ret = NOTDONE;
3967 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003968 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003969 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003970 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003971 break;
3972
3973 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003974 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003976 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 break;
3978
3979 /*
3980 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003981 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 */
LemonBoy2eaef102022-05-06 13:14:50 +01003983 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3984 ret = eval_interp_string(arg, rettv, evaluate);
3985 else
3986 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 break;
3988
3989 /*
3990 * Register contents: @r.
3991 */
3992 case '@': ++*arg;
3993 if (evaluate)
3994 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003995 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003996 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003997 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003998 emsg_invreg(**arg);
3999 else
4000 {
4001 rettv->v_type = VAR_STRING;
4002 rettv->vval.v_string = get_reg_contents(**arg,
4003 GREG_EXPR_SRC);
4004 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005 }
4006 if (**arg != NUL)
4007 ++*arg;
4008 break;
4009
4010 /*
4011 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02004012 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004014 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004015 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004016 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004017 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004018 if (ret == OK && evaluate)
4019 {
4020 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4021
Bram Moolenaara9931532021-06-12 15:58:16 +02004022 // Compile it here to get the return type. The return
4023 // type is optional, when it's missing use t_unknown.
4024 // This is recognized in compile_return().
4025 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4026 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004027 if (compile_def_function(ufunc, FALSE,
4028 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004029 {
4030 clear_tv(rettv);
4031 ret = FAIL;
4032 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004033 }
4034 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004035 if (ret == NOTDONE)
4036 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004037 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004038 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004039
Bram Moolenaar9215f012020-06-27 21:18:00 +02004040 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004041 if (**arg == ')')
4042 ++*arg;
4043 else if (ret == OK)
4044 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004045 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004046 clear_tv(rettv);
4047 ret = FAIL;
4048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
4050 break;
4051
Bram Moolenaar8c711452005-01-14 21:53:12 +00004052 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 break;
4054 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004055
4056 if (ret == NOTDONE)
4057 {
4058 /*
4059 * Must be a variable or function name.
4060 * Can also be a curly-braces kind of name: {expr}.
4061 */
4062 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004063 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004064 if (alias != NULL)
4065 s = alias;
4066
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004067 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004068 ret = FAIL;
4069 else
4070 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004071 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4072
Bram Moolenaar4525a572022-02-13 11:57:33 +00004073 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004074 {
4075 emsg(_(e_cannot_use_underscore_here));
4076 ret = FAIL;
4077 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004078 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004079 && s[0] == 's' && s[1] == ':')
4080 {
4081 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4082 ret = FAIL;
4083 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004084 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004085 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004086 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004087 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004088 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004089 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004090 else if (flags & EVAL_CONSTANT)
4091 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004092 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004093 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004094 // get the value of "true", "false", etc. or a variable
4095 ret = FAIL;
4096 if (vim9script)
4097 ret = handle_predefined(s, len, rettv);
4098 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004099 {
4100 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004101 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004102 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004103 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004105 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004106 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004107 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004108 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004109 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004110 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004111 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004112 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004113 }
4114
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004115 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4116 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004117 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004118 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119
4120 /*
4121 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4122 */
4123 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004124 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004125
4126 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004127 return ret;
4128}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004129
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004130/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004131 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004132 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004133 * Adjusts "end_leaderp" until it is at "start_leader".
4134 */
4135 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004136eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004137 typval_T *rettv,
4138 int numeric_only,
4139 char_u *start_leader,
4140 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004141{
4142 char_u *end_leader = *end_leaderp;
4143 int ret = OK;
4144 int error = FALSE;
4145 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004146 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004147 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004148 float_T f = 0.0;
4149
4150 if (rettv->v_type == VAR_FLOAT)
4151 f = rettv->vval.v_float;
4152 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004153 {
4154 while (VIM_ISWHITE(end_leader[-1]))
4155 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004156 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004157 val = tv2bool(rettv);
4158 else
4159 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004160 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004161 if (error)
4162 {
4163 clear_tv(rettv);
4164 ret = FAIL;
4165 }
4166 else
4167 {
4168 while (end_leader > start_leader)
4169 {
4170 --end_leader;
4171 if (*end_leader == '!')
4172 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004173 if (numeric_only)
4174 {
4175 ++end_leader;
4176 break;
4177 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004178 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004179 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004180 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004181 {
4182 rettv->v_type = VAR_BOOL;
4183 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4184 }
4185 else
4186 f = !f;
4187 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004188 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004189 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004190 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004191 type = VAR_BOOL;
4192 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004193 }
4194 else if (*end_leader == '-')
4195 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004196 if (rettv->v_type == VAR_FLOAT)
4197 f = -f;
4198 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004199 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004200 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004201 type = VAR_NUMBER;
4202 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004203 }
4204 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004205 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004207 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004208 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004210 else
4211 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004212 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004213 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004214 rettv->v_type = type;
4215 else
4216 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004217 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004220 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 return ret;
4222}
4223
4224/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004225 * Call the function referred to in "rettv".
4226 */
4227 static int
4228call_func_rettv(
4229 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004230 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004231 typval_T *rettv,
4232 int evaluate,
4233 dict_T *selfdict,
4234 typval_T *basetv)
4235{
4236 partial_T *pt = NULL;
4237 funcexe_T funcexe;
4238 typval_T functv;
4239 char_u *s;
4240 int ret;
4241
4242 // need to copy the funcref so that we can clear rettv
4243 if (evaluate)
4244 {
4245 functv = *rettv;
4246 rettv->v_type = VAR_UNKNOWN;
4247
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004248 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004249 if (functv.v_type == VAR_PARTIAL)
4250 {
4251 pt = functv.vval.v_partial;
4252 s = partial_name(pt);
4253 }
4254 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004255 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004256 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004257 if (s == NULL || *s == NUL)
4258 {
4259 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004260 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004261 goto theend;
4262 }
4263 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004264 }
4265 else
4266 s = (char_u *)"";
4267
Bram Moolenaara80faa82020-04-12 19:37:17 +02004268 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004269 funcexe.fe_firstline = curwin->w_cursor.lnum;
4270 funcexe.fe_lastline = curwin->w_cursor.lnum;
4271 funcexe.fe_evaluate = evaluate;
4272 funcexe.fe_partial = pt;
4273 funcexe.fe_selfdict = selfdict;
4274 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004275 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004276
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004277theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004278 // Clear the funcref afterwards, so that deleting it while
4279 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004280 if (evaluate)
4281 clear_tv(&functv);
4282
4283 return ret;
4284}
4285
4286/*
4287 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004288 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004289 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4290 */
4291 static int
4292eval_lambda(
4293 char_u **arg,
4294 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004295 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004296 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004297{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004298 int evaluate = evalarg != NULL
4299 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004300 typval_T base = *rettv;
4301 int ret;
4302
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004303 rettv->v_type = VAR_UNKNOWN;
4304
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004305 if (**arg == '{')
4306 {
4307 // ->{lambda}()
4308 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4309 }
4310 else
4311 {
4312 // ->(lambda)()
4313 ++*arg;
4314 ret = eval1(arg, rettv, evalarg);
4315 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004316 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004317 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004318 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004319 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004320 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004321 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4322 && rettv->v_type != VAR_PARTIAL)
4323 {
4324 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4325 return FAIL;
4326 }
4327 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004328 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004329 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004330 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004331
4332 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004333 {
4334 if (verbose)
4335 {
4336 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004337 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004338 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004339 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004340 }
4341 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004342 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004343 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004344 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004345 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004346
4347 // Clear the funcref afterwards, so that deleting it while
4348 // evaluating the arguments is possible (see test55).
4349 if (evaluate)
4350 clear_tv(&base);
4351
4352 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004353}
4354
4355/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004356 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004357 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004358 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4359 */
4360 static int
4361eval_method(
4362 char_u **arg,
4363 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004364 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004365 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004366{
4367 char_u *name;
4368 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004369 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004370 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004371 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004372 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004373 int evaluate = evalarg != NULL
4374 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004375
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004376 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004377
Bram Moolenaarac92e252019-08-03 21:58:38 +02004378 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004379 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004380 if (alias != NULL)
4381 name = alias;
4382
4383 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004384 {
4385 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004386 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004387 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004388 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004389 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004390 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004391 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004392
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004393 // If there is no "(" immediately following, but there is further on,
4394 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4395 // Does not handle anything where "(" is part of the expression.
4396 *arg = skipwhite(*arg);
4397
4398 if (**arg != '(' && alias == NULL
4399 && (paren = vim_strchr(*arg, '(')) != NULL)
4400 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004401 char_u *deref;
4402
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004403 *arg = name;
4404 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004405 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4406 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004407 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004408 *arg = name + len;
4409 ret = FAIL;
4410 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004411 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004412 {
4413 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004414 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004415 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004416 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004417 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004418
4419 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004420 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004421 *arg = skipwhite(*arg);
4422
4423 if (**arg != '(')
4424 {
4425 if (verbose)
4426 semsg(_(e_missing_parenthesis_str), name);
4427 ret = FAIL;
4428 }
4429 else if (VIM_ISWHITE((*arg)[-1]))
4430 {
4431 if (verbose)
4432 emsg(_(e_no_white_space_allowed_before_parenthesis));
4433 ret = FAIL;
4434 }
4435 else
4436 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004437 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004438 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004439 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004440
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004441 // Clear the funcref afterwards, so that deleting it while
4442 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004443 if (evaluate)
4444 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004445 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004446
Bram Moolenaarac92e252019-08-03 21:58:38 +02004447 return ret;
4448}
4449
4450/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004451 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4452 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004453 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4454 */
4455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004456eval_index(
4457 char_u **arg,
4458 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004459 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004460 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004461{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004462 int evaluate = evalarg != NULL
4463 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004464 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004465 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004466 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004467 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004468 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004469 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004471 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4472 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004473
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004474 init_tv(&var1);
4475 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004476 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004477 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004478 /*
4479 * dict.name
4480 */
4481 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004482 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004483 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004484 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004485 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004486 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004487 }
4488 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004489 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004490 /*
4491 * something[idx]
4492 *
4493 * Get the (first) variable from inside the [].
4494 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004495 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004496 if (**arg == ':')
4497 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004498 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004499 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004500 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004501 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004502 semsg(_(e_white_space_required_before_and_after_str_at_str),
4503 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004504 clear_tv(&var1);
4505 return FAIL;
4506 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004507 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004508 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004509 int error = FALSE;
4510
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004511 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004512 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004513 && var1.v_type == VAR_FLOAT)
4514 {
4515 var1.vval.v_string = typval_tostring(&var1, TRUE);
4516 var1.v_type = VAR_STRING;
4517 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004518
Bram Moolenaar4525a572022-02-13 11:57:33 +00004519 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004520 tv_get_number_chk(&var1, &error);
4521 else
4522 error = tv_get_string_chk(&var1) == NULL;
4523 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004524 {
4525 // not a number or string
4526 clear_tv(&var1);
4527 return FAIL;
4528 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004529 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004530
4531 /*
4532 * Get the second variable from inside the [:].
4533 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004534 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004535 if (**arg == ':')
4536 {
4537 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004538 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004539 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004540 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004541 semsg(_(e_white_space_required_before_and_after_str_at_str),
4542 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004543 if (!empty1)
4544 clear_tv(&var1);
4545 return FAIL;
4546 }
4547 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 if (**arg == ']')
4549 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004550 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004552 if (!empty1)
4553 clear_tv(&var1);
4554 return FAIL;
4555 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004556 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004557 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004558 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004559 if (!empty1)
4560 clear_tv(&var1);
4561 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004562 return FAIL;
4563 }
4564 }
4565
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004566 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004567 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004568 if (**arg != ']')
4569 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004570 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004571 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004572 clear_tv(&var1);
4573 if (range)
4574 clear_tv(&var2);
4575 return FAIL;
4576 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004577 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004578 }
4579
4580 if (evaluate)
4581 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004582 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004583 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004584 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004585
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004586 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004587 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004588 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004589 clear_tv(&var2);
4590 return res;
4591 }
4592 return OK;
4593}
4594
4595/*
4596 * Check if "rettv" can have an [index] or [sli:ce]
4597 */
4598 int
4599check_can_index(typval_T *rettv, int evaluate, int verbose)
4600{
4601 switch (rettv->v_type)
4602 {
4603 case VAR_FUNC:
4604 case VAR_PARTIAL:
4605 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004606 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004607 return FAIL;
4608 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004609 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004610 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004611 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004612 case VAR_BOOL:
4613 case VAR_SPECIAL:
4614 case VAR_JOB:
4615 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004616 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004617 if (verbose)
4618 emsg(_(e_cannot_index_special_variable));
4619 return FAIL;
4620 case VAR_UNKNOWN:
4621 case VAR_ANY:
4622 case VAR_VOID:
4623 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004624 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004625 emsg(_(e_cannot_index_special_variable));
4626 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004627 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004628 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004629
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004630 case VAR_STRING:
4631 case VAR_LIST:
4632 case VAR_DICT:
4633 case VAR_BLOB:
4634 break;
4635 case VAR_NUMBER:
4636 if (in_vim9script())
4637 emsg(_(e_cannot_index_number));
4638 break;
4639 }
4640 return OK;
4641}
4642
4643/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004644 * slice() function
4645 */
4646 void
4647f_slice(typval_T *argvars, typval_T *rettv)
4648{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004649 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004650 && ((argvars[0].v_type != VAR_STRING
4651 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004652 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004653 && check_for_list_arg(argvars, 0) == FAIL)
4654 || check_for_number_arg(argvars, 1) == FAIL
4655 || check_for_opt_number_arg(argvars, 2) == FAIL))
4656 return;
4657
Bram Moolenaar6601b622021-01-13 21:47:15 +01004658 if (check_can_index(argvars, TRUE, FALSE) == OK)
4659 {
4660 copy_tv(argvars, rettv);
4661 eval_index_inner(rettv, TRUE, argvars + 1,
4662 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4663 TRUE, NULL, 0, FALSE);
4664 }
4665}
4666
4667/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004668 * Apply index or range to "rettv".
4669 * "var1" is the first index, NULL for [:expr].
4670 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004671 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4672 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004673 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4674 */
4675 int
4676eval_index_inner(
4677 typval_T *rettv,
4678 int is_range,
4679 typval_T *var1,
4680 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004681 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004682 char_u *key,
4683 int keylen,
4684 int verbose)
4685{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004686 varnumber_T n1, n2 = 0;
4687 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004688
4689 n1 = 0;
4690 if (var1 != NULL && rettv->v_type != VAR_DICT)
4691 n1 = tv_get_number(var1);
4692
4693 if (is_range)
4694 {
4695 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004696 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004697 if (verbose)
4698 emsg(_(e_cannot_slice_dictionary));
4699 return FAIL;
4700 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004701 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004702 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004703 else
4704 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004705 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004706
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004707 switch (rettv->v_type)
4708 {
4709 case VAR_UNKNOWN:
4710 case VAR_ANY:
4711 case VAR_VOID:
4712 case VAR_FUNC:
4713 case VAR_PARTIAL:
4714 case VAR_FLOAT:
4715 case VAR_BOOL:
4716 case VAR_SPECIAL:
4717 case VAR_JOB:
4718 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004719 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004720 break; // not evaluating, skipping over subscript
4721
4722 case VAR_NUMBER:
4723 case VAR_STRING:
4724 {
4725 char_u *s = tv_get_string(rettv);
4726
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004727 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004728 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004729 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004730 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004731 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004732 else
4733 s = char_from_string(s, n1);
4734 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004735 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004736 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004737 // The resulting variable is a substring. If the indexes
4738 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004739 if (n1 < 0)
4740 {
4741 n1 = len + n1;
4742 if (n1 < 0)
4743 n1 = 0;
4744 }
4745 if (n2 < 0)
4746 n2 = len + n2;
4747 else if (n2 >= len)
4748 n2 = len;
4749 if (n1 >= len || n2 < 0 || n1 > n2)
4750 s = NULL;
4751 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004752 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004753 }
4754 else
4755 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004756 // The resulting variable is a string of a single
4757 // character. If the index is too big or negative the
4758 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004759 if (n1 >= len || n1 < 0)
4760 s = NULL;
4761 else
4762 s = vim_strnsave(s + n1, 1);
4763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004767 }
4768 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004769
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004770 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004771 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4772 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004773 break;
4774
4775 case VAR_LIST:
4776 if (var1 == NULL)
4777 n1 = 0;
4778 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004779 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004780 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004781 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004782 return FAIL;
4783 break;
4784
4785 case VAR_DICT:
4786 {
4787 dictitem_T *item;
4788 typval_T tmp;
4789
4790 if (key == NULL)
4791 {
4792 key = tv_get_string_chk(var1);
4793 if (key == NULL)
4794 return FAIL;
4795 }
4796
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004797 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004798
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004799 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004800 {
4801 if (verbose)
4802 {
4803 if (keylen > 0)
4804 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004805 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004806 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004807 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004808 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004809
4810 copy_tv(&item->di_tv, &tmp);
4811 clear_tv(rettv);
4812 *rettv = tmp;
4813 }
4814 break;
4815 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004816 return OK;
4817}
4818
4819/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004820 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004821 */
4822 char_u *
4823partial_name(partial_T *pt)
4824{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004825 if (pt != NULL)
4826 {
4827 if (pt->pt_name != NULL)
4828 return pt->pt_name;
4829 if (pt->pt_func != NULL)
4830 return pt->pt_func->uf_name;
4831 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004832 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004833}
4834
Bram Moolenaarddecc252016-04-06 22:59:37 +02004835 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004836partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004837{
4838 int i;
4839
4840 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004841 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004842 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004843 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004844 if (pt->pt_name != NULL)
4845 {
4846 func_unref(pt->pt_name);
4847 vim_free(pt->pt_name);
4848 }
4849 else
4850 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004851
Bram Moolenaar54656012021-06-09 20:50:46 +02004852 // "out_up" is no longer used, decrement refcount on partial that owns it.
4853 partial_unref(pt->pt_outer.out_up_partial);
4854
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004855 // Using pt_outer from another partial.
4856 partial_unref(pt->pt_outer_partial);
4857
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004858 // Decrease the reference count for the context of a closure. If down
4859 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004860 if (pt->pt_funcstack != NULL)
4861 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004862 --pt->pt_funcstack->fs_refcount;
4863 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004864 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004865 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004866 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4867 if (pt->pt_loopvars[i] != NULL)
4868 {
4869 --pt->pt_loopvars[i]->lvs_refcount;
4870 loopvars_check_refcount(pt->pt_loopvars[i]);
4871 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004872
Bram Moolenaarddecc252016-04-06 22:59:37 +02004873 vim_free(pt);
4874}
4875
4876/*
4877 * Unreference a closure: decrement the reference count and free it when it
4878 * becomes zero.
4879 */
4880 void
4881partial_unref(partial_T *pt)
4882{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004883 if (pt != NULL)
4884 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004885 int done = FALSE;
4886
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004887 if (--pt->pt_refcount <= 0)
4888 partial_free(pt);
4889
4890 // If the reference count goes down to one, the funcstack may be the
4891 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004892 else if (pt->pt_refcount == 1)
4893 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004894 // careful: if the funcstack is freed it may contain this partial
4895 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004896 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004897 done = funcstack_check_refcount(pt->pt_funcstack);
4898
Bram Moolenaarcc341812022-09-19 15:54:34 +01004899 if (!done)
4900 {
4901 int depth;
4902
4903 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4904 if (pt->pt_loopvars[depth] != NULL
4905 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4906 break;
4907 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004908 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004909 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004910}
4911
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004912/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004913 * Return the next (unique) copy ID.
4914 * Used for serializing nested structures.
4915 */
4916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004917get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004918{
4919 current_copyID += COPYID_INC;
4920 return current_copyID;
4921}
4922
4923/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004924 * Garbage collection for lists and dictionaries.
4925 *
4926 * We use reference counts to be able to free most items right away when they
4927 * are no longer used. But for composite items it's possible that it becomes
4928 * unused while the reference count is > 0: When there is a recursive
4929 * reference. Example:
4930 * :let l = [1, 2, 3]
4931 * :let d = {9: l}
4932 * :let l[1] = d
4933 *
4934 * Since this is quite unusual we handle this with garbage collection: every
4935 * once in a while find out which lists and dicts are not referenced from any
4936 * variable.
4937 *
4938 * Here is a good reference text about garbage collection (refers to Python
4939 * but it applies to all reference-counting mechanisms):
4940 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004941 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004942
4943/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004944 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004945 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004946 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004947 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004948 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004949garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004950{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004951 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004952 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004953 buf_T *buf;
4954 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004955 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004956 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004957
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004958 if (!testing)
4959 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004960 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004961 want_garbage_collect = FALSE;
4962 may_garbage_collect = FALSE;
4963 garbage_collect_at_exit = FALSE;
4964 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004965
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004966 // The execution stack can grow big, limit the size.
4967 if (exestack.ga_maxlen - exestack.ga_len > 500)
4968 {
4969 size_t new_len;
4970 char_u *pp;
4971 int n;
4972
4973 // Keep 150% of the current size, with a minimum of the growth size.
4974 n = exestack.ga_len / 2;
4975 if (n < exestack.ga_growsize)
4976 n = exestack.ga_growsize;
4977
4978 // Don't make it bigger though.
4979 if (exestack.ga_len + n < exestack.ga_maxlen)
4980 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004981 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004982 pp = vim_realloc(exestack.ga_data, new_len);
4983 if (pp == NULL)
4984 return FAIL;
4985 exestack.ga_maxlen = exestack.ga_len + n;
4986 exestack.ga_data = pp;
4987 }
4988 }
4989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004990 // We advance by two because we add one for items referenced through
4991 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004992 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004993
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004994 /*
4995 * 1. Go through all accessible variables and mark all lists and dicts
4996 * with copyID.
4997 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004998
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004999 // Don't free variables in the previous_funccal list unless they are only
5000 // referenced through previous_funccal. This must be first, because if
5001 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005002 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005003
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005004 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005005 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005007 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005008 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005009 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5010 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005011
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005012 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005013 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005014 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5015 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005016 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005017 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5018 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005019#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005020 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005021 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5022 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005023 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005024 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005025 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5026 NULL, NULL);
5027#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005028
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005029 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005030 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005031 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5032 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005033 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005034 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005037 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005038
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005039 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005040 abort = abort || set_ref_in_functions(copyID);
5041
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005042 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005043 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005044
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005045 // funcstacks keep variables for closures
5046 abort = abort || set_ref_in_funcstacks(copyID);
5047
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005048 // loopvars keep variables for loop blocks
5049 abort = abort || set_ref_in_loopvars(copyID);
5050
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005051 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005052 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005053
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005054 // callbacks in buffers
5055 abort = abort || set_ref_in_buffers(copyID);
5056
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005057 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5058 abort = abort || set_ref_in_insexpand_funcs(copyID);
5059
5060 // 'operatorfunc' callback
5061 abort = abort || set_ref_in_opfunc(copyID);
5062
5063 // 'tagfunc' callback
5064 abort = abort || set_ref_in_tagfunc(copyID);
5065
5066 // 'imactivatefunc' and 'imstatusfunc' callbacks
5067 abort = abort || set_ref_in_im_funcs(copyID);
5068
Bram Moolenaar1dced572012-04-05 16:54:08 +02005069#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005070 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005071#endif
5072
Bram Moolenaardb913952012-06-29 12:54:53 +02005073#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005074 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005075#endif
5076
5077#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005078 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005079#endif
5080
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005081#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005082 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005083 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005084#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005085#ifdef FEAT_NETBEANS_INTG
5086 abort = abort || set_ref_in_nb_channel(copyID);
5087#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005088
Bram Moolenaare3188e22016-05-31 21:13:04 +02005089#ifdef FEAT_TIMERS
5090 abort = abort || set_ref_in_timer(copyID);
5091#endif
5092
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005093#ifdef FEAT_QUICKFIX
5094 abort = abort || set_ref_in_quickfix(copyID);
5095#endif
5096
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005097#ifdef FEAT_TERMINAL
5098 abort = abort || set_ref_in_term(copyID);
5099#endif
5100
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005101#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005102 abort = abort || set_ref_in_popups(copyID);
5103#endif
5104
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005105 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005106 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005107 /*
5108 * 2. Free lists and dictionaries that are not referenced.
5109 */
5110 did_free = free_unref_items(copyID);
5111
5112 /*
5113 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005114 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005115 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005116 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005117 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005118 else if (p_verbose > 0)
5119 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005120 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005121 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005122
5123 return did_free;
5124}
5125
5126/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005127 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005128 */
5129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005130free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005131{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005132 int did_free = FALSE;
5133
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005134 // Let all "free" functions know that we are here. This means no
5135 // dictionaries, lists, channels or jobs are to be freed, because we will
5136 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005137 in_free_unref_items = TRUE;
5138
5139 /*
5140 * PASS 1: free the contents of the items. We don't free the items
5141 * themselves yet, so that it is possible to decrement refcount counters
5142 */
5143
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005144 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005145 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005146
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005147 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005148 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005149
5150#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005151 // Go through the list of jobs and free items without the copyID. This
5152 // must happen before doing channels, because jobs refer to channels, but
5153 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005154 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5155
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005156 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005157 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5158#endif
5159
5160 /*
5161 * PASS 2: free the items themselves.
5162 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005163 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005164 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005165
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005166#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005167 // Go through the list of jobs and free items without the copyID. This
5168 // must happen before doing channels, because jobs refer to channels, but
5169 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005170 free_unused_jobs(copyID, COPYID_MASK);
5171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005172 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005173 free_unused_channels(copyID, COPYID_MASK);
5174#endif
5175
5176 in_free_unref_items = FALSE;
5177
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005178 return did_free;
5179}
5180
5181/*
5182 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005183 * "list_stack" is used to add lists to be marked. Can be NULL.
5184 *
5185 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005186 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005187 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005188set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005189{
5190 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005191 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005192 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005193 hashtab_T *cur_ht;
5194 ht_stack_T *ht_stack = NULL;
5195 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005196
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005197 cur_ht = ht;
5198 for (;;)
5199 {
5200 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005202 // Mark each item in the hashtab. If the item contains a hashtab
5203 // it is added to ht_stack, if it contains a list it is added to
5204 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005205 todo = (int)cur_ht->ht_used;
5206 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5207 if (!HASHITEM_EMPTY(hi))
5208 {
5209 --todo;
5210 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5211 &ht_stack, list_stack);
5212 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005213 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005214
5215 if (ht_stack == NULL)
5216 break;
5217
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005218 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005219 cur_ht = ht_stack->ht;
5220 tempitem = ht_stack;
5221 ht_stack = ht_stack->prev;
5222 free(tempitem);
5223 }
5224
5225 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005226}
5227
Dominique Pelle748b3082022-01-08 12:41:16 +00005228#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5229 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005230/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005231 * Mark a dict and its items with "copyID".
5232 * Returns TRUE if setting references failed somehow.
5233 */
5234 int
5235set_ref_in_dict(dict_T *d, int copyID)
5236{
5237 if (d != NULL && d->dv_copyID != copyID)
5238 {
5239 d->dv_copyID = copyID;
5240 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5241 }
5242 return FALSE;
5243}
Dominique Pelle748b3082022-01-08 12:41:16 +00005244#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005245
5246/*
5247 * Mark a list and its items with "copyID".
5248 * Returns TRUE if setting references failed somehow.
5249 */
5250 int
5251set_ref_in_list(list_T *ll, int copyID)
5252{
5253 if (ll != NULL && ll->lv_copyID != copyID)
5254 {
5255 ll->lv_copyID = copyID;
5256 return set_ref_in_list_items(ll, copyID, NULL);
5257 }
5258 return FALSE;
5259}
5260
5261/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005262 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005263 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5264 *
5265 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005266 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005267 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005268set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005269{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005270 listitem_T *li;
5271 int abort = FALSE;
5272 list_T *cur_l;
5273 list_stack_T *list_stack = NULL;
5274 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005275
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005276 cur_l = l;
5277 for (;;)
5278 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005279 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005280 // Mark each item in the list. If the item contains a hashtab
5281 // it is added to ht_stack, if it contains a list it is added to
5282 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005283 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5284 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5285 ht_stack, &list_stack);
5286 if (list_stack == NULL)
5287 break;
5288
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005289 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005290 cur_l = list_stack->list;
5291 tempitem = list_stack;
5292 list_stack = list_stack->prev;
5293 free(tempitem);
5294 }
5295
5296 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005297}
5298
5299/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005300 * Mark the partial in callback 'cb' with "copyID".
5301 */
5302 int
5303set_ref_in_callback(callback_T *cb, int copyID)
5304{
5305 typval_T tv;
5306
5307 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5308 return FALSE;
5309
5310 tv.v_type = VAR_PARTIAL;
5311 tv.vval.v_partial = cb->cb_partial;
5312 return set_ref_in_item(&tv, copyID, NULL, NULL);
5313}
5314
5315/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005316 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005317 * "list_stack" is used to add lists to be marked. Can be NULL.
5318 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5319 *
5320 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005321 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005322 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005323set_ref_in_item(
5324 typval_T *tv,
5325 int copyID,
5326 ht_stack_T **ht_stack,
5327 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005328{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005329 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005330
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005331 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005332 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005333 dict_T *dd = tv->vval.v_dict;
5334
Bram Moolenaara03f2332016-02-06 18:09:59 +01005335 if (dd != NULL && dd->dv_copyID != copyID)
5336 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005337 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005338 dd->dv_copyID = copyID;
5339 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005340 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5342 }
5343 else
5344 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005345 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5346
Bram Moolenaara03f2332016-02-06 18:09:59 +01005347 if (newitem == NULL)
5348 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005349 else
5350 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005351 newitem->ht = &dd->dv_hashtab;
5352 newitem->prev = *ht_stack;
5353 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005354 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005355 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005356 }
5357 }
5358 else if (tv->v_type == VAR_LIST)
5359 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005360 list_T *ll = tv->vval.v_list;
5361
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 if (ll != NULL && ll->lv_copyID != copyID)
5363 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005364 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005365 ll->lv_copyID = copyID;
5366 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005367 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005368 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005369 }
5370 else
5371 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005372 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5373
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 if (newitem == NULL)
5375 abort = TRUE;
5376 else
5377 {
5378 newitem->list = ll;
5379 newitem->prev = *list_stack;
5380 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005381 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005382 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005383 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005384 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005385 else if (tv->v_type == VAR_FUNC)
5386 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005387 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005388 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005389 else if (tv->v_type == VAR_PARTIAL)
5390 {
5391 partial_T *pt = tv->vval.v_partial;
5392 int i;
5393
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005394 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005395 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005396 // Didn't see this partial yet.
5397 pt->pt_copyID = copyID;
5398
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005399 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005400
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005401 if (pt->pt_dict != NULL)
5402 {
5403 typval_T dtv;
5404
5405 dtv.v_type = VAR_DICT;
5406 dtv.vval.v_dict = pt->pt_dict;
5407 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5408 }
5409
5410 for (i = 0; i < pt->pt_argc; ++i)
5411 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5412 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005413 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005414 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005415 }
5416 }
5417#ifdef FEAT_JOB_CHANNEL
5418 else if (tv->v_type == VAR_JOB)
5419 {
5420 job_T *job = tv->vval.v_job;
5421 typval_T dtv;
5422
5423 if (job != NULL && job->jv_copyID != copyID)
5424 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005425 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005426 if (job->jv_channel != NULL)
5427 {
5428 dtv.v_type = VAR_CHANNEL;
5429 dtv.vval.v_channel = job->jv_channel;
5430 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5431 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005432 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005433 {
5434 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005435 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005436 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5437 }
5438 }
5439 }
5440 else if (tv->v_type == VAR_CHANNEL)
5441 {
5442 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005443 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005444 typval_T dtv;
5445 jsonq_T *jq;
5446 cbq_T *cq;
5447
5448 if (ch != NULL && ch->ch_copyID != copyID)
5449 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005450 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005451 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005452 {
5453 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5454 jq = jq->jq_next)
5455 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5456 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5457 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005458 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005459 {
5460 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005461 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005462 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5463 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005464 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005465 {
5466 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005467 dtv.vval.v_partial =
5468 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005469 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5470 }
5471 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005472 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005473 {
5474 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005475 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005476 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5477 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005478 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005479 {
5480 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005481 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005482 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5483 }
5484 }
5485 }
5486#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005487 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005488}
5489
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005491 * Return a string with the string representation of a variable.
5492 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005493 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005494 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005495 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005496 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005497 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005498 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5499 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005500 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005502 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005503echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005504 typval_T *tv,
5505 char_u **tofree,
5506 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005507 int copyID,
5508 int echo_style,
5509 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005510 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005512 static int recurse = 0;
5513 char_u *r = NULL;
5514
Bram Moolenaar33570922005-01-25 22:26:29 +00005515 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005516 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005517 if (!did_echo_string_emsg)
5518 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005519 // Only give this message once for a recursive call to avoid
5520 // flooding the user with errors. And stop iterating over lists
5521 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005522 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005523 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005524 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005525 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005526 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005527 }
5528 ++recurse;
5529
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 switch (tv->v_type)
5531 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005532 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005533 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005534 {
5535 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005536 r = tv->vval.v_string;
5537 if (r == NULL)
5538 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005539 }
5540 else
5541 {
5542 *tofree = string_quote(tv->vval.v_string, FALSE);
5543 r = *tofree;
5544 }
5545 break;
5546
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005548 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005549 char_u buf[MAX_FUNC_NAME_LEN];
5550
5551 if (echo_style)
5552 {
LemonBoya5d35902022-04-29 21:15:02 +01005553 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5554 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005555 buf, MAX_FUNC_NAME_LEN);
5556 if (r == buf)
5557 {
5558 r = vim_strsave(buf);
5559 *tofree = r;
5560 }
5561 else
5562 *tofree = NULL;
5563 }
5564 else
5565 {
5566 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5567 : make_ufunc_name_readable(
5568 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5569 TRUE);
5570 r = *tofree;
5571 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005573 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005574
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005575 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005576 {
5577 partial_T *pt = tv->vval.v_partial;
5578 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005579 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005580 garray_T ga;
5581 int i;
5582 char_u *tf;
5583
5584 ga_init2(&ga, 1, 100);
5585 ga_concat(&ga, (char_u *)"function(");
5586 if (fname != NULL)
5587 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005588 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005589 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005590 && vim_isupper(fname[1]))
5591 {
5592 ga_concat(&ga, (char_u *)"'g:");
5593 ga_concat(&ga, fname + 1);
5594 }
5595 else
5596 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005597 vim_free(fname);
5598 }
5599 if (pt != NULL && pt->pt_argc > 0)
5600 {
5601 ga_concat(&ga, (char_u *)", [");
5602 for (i = 0; i < pt->pt_argc; ++i)
5603 {
5604 if (i > 0)
5605 ga_concat(&ga, (char_u *)", ");
5606 ga_concat(&ga,
5607 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5608 vim_free(tf);
5609 }
5610 ga_concat(&ga, (char_u *)"]");
5611 }
5612 if (pt != NULL && pt->pt_dict != NULL)
5613 {
5614 typval_T dtv;
5615
5616 ga_concat(&ga, (char_u *)", ");
5617 dtv.v_type = VAR_DICT;
5618 dtv.vval.v_dict = pt->pt_dict;
5619 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5620 vim_free(tf);
5621 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005622 // terminate with ')' and a NUL
5623 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005624
5625 *tofree = ga.ga_data;
5626 r = *tofree;
5627 break;
5628 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005629
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005630 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005631 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005632 break;
5633
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005635 if (tv->vval.v_list == NULL)
5636 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005637 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005638 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005639 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005640 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005641 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5642 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005643 {
5644 *tofree = NULL;
5645 r = (char_u *)"[...]";
5646 }
5647 else
5648 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005649 int old_copyID = tv->vval.v_list->lv_copyID;
5650
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005651 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005652 *tofree = list2string(tv, copyID, restore_copyID);
5653 if (restore_copyID)
5654 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005655 r = *tofree;
5656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005657 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005658
Bram Moolenaar8c711452005-01-14 21:53:12 +00005659 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005660 if (tv->vval.v_dict == NULL)
5661 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005662 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005663 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005664 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005665 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005666 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5667 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005668 {
5669 *tofree = NULL;
5670 r = (char_u *)"{...}";
5671 }
5672 else
5673 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005674 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005675
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005676 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005677 *tofree = dict2string(tv, copyID, restore_copyID);
5678 if (restore_copyID)
5679 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005680 r = *tofree;
5681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005682 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005683
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005685 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005686 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005687 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005688 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005689 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005690 break;
5691
Bram Moolenaar835dc632016-02-07 14:27:38 +01005692 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005693 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005694#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005695 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005696 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5697 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005698 if (composite_val)
5699 {
5700 *tofree = string_quote(r, FALSE);
5701 r = *tofree;
5702 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005703#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005704 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005705
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005706 case VAR_INSTR:
5707 *tofree = NULL;
5708 r = (char_u *)"instructions";
5709 break;
5710
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005711 case VAR_FLOAT:
5712 *tofree = NULL;
5713 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5714 r = numbuf;
5715 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005716
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005717 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005718 case VAR_SPECIAL:
5719 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005720 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005721 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005722 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005723
Bram Moolenaar8502c702014-06-17 12:51:16 +02005724 if (--recurse == 0)
5725 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005726 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005727}
5728
5729/*
5730 * Return a string with the string representation of a variable.
5731 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5732 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005733 * Does not put quotes around strings, as ":echo" displays values.
5734 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5735 * May return NULL.
5736 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005737 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005738echo_string(
5739 typval_T *tv,
5740 char_u **tofree,
5741 char_u *numbuf,
5742 int copyID)
5743{
5744 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5745}
5746
5747/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005748 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5749 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005750 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005751 */
5752 int
5753buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5754{
5755 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005756 char_u *t;
5757 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005758
5759 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5760 return -1;
5761
5762 if (lnum > buf->b_ml.ml_line_count)
5763 lnum = buf->b_ml.ml_line_count;
5764
5765 str = ml_get_buf(buf, lnum, FALSE);
5766 if (str == NULL)
5767 return -1;
5768
5769 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005770 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005771
Bram Moolenaar91458462021-01-13 20:08:38 +01005772 // count the number of characters
5773 t = str;
5774 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5775 t += mb_ptr2len(t);
5776
5777 // In insert mode, when the cursor is at the end of a non-empty line,
5778 // byteidx points to the NUL character immediately past the end of the
5779 // string. In this case, add one to the character count.
5780 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5781 count++;
5782
5783 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005784}
5785
5786/*
5787 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005788 * byte index. Works only for loaded buffers. Returns -1 on failure.
5789 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005790 */
5791 int
5792buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5793{
5794 char_u *str;
5795 char_u *t;
5796
5797 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5798 return -1;
5799
5800 if (lnum > buf->b_ml.ml_line_count)
5801 lnum = buf->b_ml.ml_line_count;
5802
5803 str = ml_get_buf(buf, lnum, FALSE);
5804 if (str == NULL)
5805 return -1;
5806
5807 // Convert the character offset to a byte offset
5808 t = str;
5809 while (*t != NUL && --charidx > 0)
5810 t += mb_ptr2len(t);
5811
Bram Moolenaar91458462021-01-13 20:08:38 +01005812 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005813}
5814
5815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005817 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005819 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005820var2fpos(
5821 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005822 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005823 int *fnum, // set to fnum for '0, 'A, etc.
5824 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005826 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005828 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005830 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005831 if (varp->v_type == VAR_LIST)
5832 {
5833 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005834 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005835 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005836 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005837
5838 l = varp->vval.v_list;
5839 if (l == NULL)
5840 return NULL;
5841
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005842 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005843 pos.lnum = list_find_nr(l, 0L, &error);
5844 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005845 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005846 if (charcol)
5847 len = (long)mb_charlen(ml_get(pos.lnum));
5848 else
5849 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005850
Bram Moolenaarec65d772020-08-20 22:29:12 +02005851 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005852 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005853 li = list_find(l, 1L);
5854 if (li != NULL && li->li_tv.v_type == VAR_STRING
5855 && li->li_tv.vval.v_string != NULL
5856 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005857 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005858 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005859 }
5860 else
5861 {
5862 pos.col = list_find_nr(l, 1L, &error);
5863 if (error)
5864 return NULL;
5865 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005866
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005867 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005868 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005869 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005870 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005871
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005872 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005873 pos.coladd = list_find_nr(l, 2L, &error);
5874 if (error)
5875 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005876
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005877 return &pos;
5878 }
5879
Bram Moolenaarc5809432021-03-27 21:23:30 +01005880 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5881 return NULL;
5882
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005883 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005884 if (name == NULL)
5885 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005886
5887 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005888 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005889 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005890 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005891 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005892 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005893 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005894 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005895 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005896 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005897 pos = VIsual;
5898 else
5899 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005900 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005901 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005902 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005904 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005905 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5907 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005908 pos = *pp;
5909 }
5910 if (pos.lnum != 0)
5911 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005912 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005913 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5914 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005916
Bram Moolenaara5525202006-03-02 22:52:09 +00005917 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005918
Bram Moolenaar477933c2007-07-17 14:32:23 +00005919 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005920 {
5921 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005922 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005923 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005924 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005925 // In silent Ex mode topline is zero, but that's not a valid line
5926 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005927 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005928 return &pos;
5929 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005930 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005931 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005932 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005933 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005934 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005935 return &pos;
5936 }
5937 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005938 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005940 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 {
5942 pos.lnum = curbuf->b_ml.ml_line_count;
5943 pos.col = 0;
5944 }
5945 else
5946 {
5947 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005948 if (charcol)
5949 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5950 else
5951 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 }
5953 return &pos;
5954 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005955 if (in_vim9script())
5956 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 return NULL;
5958}
5959
5960/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005961 * Convert list in "arg" into a position and optional file number.
5962 * When "fnump" is NULL there is no file number, only 3 items.
5963 * Note that the column is passed on as-is, the caller may want to decrement
5964 * it to use 1 for the first column.
5965 * Return FAIL when conversion is not possible, doesn't check the position for
5966 * validity.
5967 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005968 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969list2fpos(
5970 typval_T *arg,
5971 pos_T *posp,
5972 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005973 colnr_T *curswantp,
5974 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005975{
5976 list_T *l = arg->vval.v_list;
5977 long i = 0;
5978 long n;
5979
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005980 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5981 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005982 if (arg->v_type != VAR_LIST
5983 || l == NULL
5984 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005985 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005986 return FAIL;
5987
5988 if (fnump != NULL)
5989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005990 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005991 if (n < 0)
5992 return FAIL;
5993 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005994 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005995 *fnump = n;
5996 }
5997
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005998 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005999 if (n < 0)
6000 return FAIL;
6001 posp->lnum = n;
6002
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006003 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006004 if (n < 0)
6005 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006006 // If character position is specified, then convert to byte position
6007 if (charcol)
6008 {
6009 buf_T *buf;
6010
6011 // Get the text for the specified line in a loaded buffer
6012 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
6013 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
6014 return FAIL;
6015
Bram Moolenaar91458462021-01-13 20:08:38 +01006016 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006017 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006018 posp->col = n;
6019
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006020 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006021 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006022 posp->coladd = 0;
6023 else
6024 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006025
Bram Moolenaar493c1782014-05-28 14:34:46 +02006026 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006027 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006028
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006029 return OK;
6030}
6031
6032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 * Get the length of an environment variable name.
6034 * Advance "arg" to the first character after the name.
6035 * Return 0 for error.
6036 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006038get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039{
6040 char_u *p;
6041 int len;
6042
6043 for (p = *arg; vim_isIDc(*p); ++p)
6044 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006045 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 return 0;
6047
6048 len = (int)(p - *arg);
6049 *arg = p;
6050 return len;
6051}
6052
6053/*
6054 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006055 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056 * Return 0 if something is wrong.
6057 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006058 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006059get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006060{
6061 char_u *p;
6062 int len;
6063
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006064 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006066 {
6067 if (*p == ':')
6068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006069 // "s:" is start of "s:var", but "n:" is not and can be used in
6070 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006071 len = (int)(p - *arg);
6072 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6073 || len > 1)
6074 break;
6075 }
6076 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006077 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 return 0;
6079
6080 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006081 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082
6083 return len;
6084}
6085
6086/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006087 * Get the length of the name of a variable or function.
6088 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006089 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006090 * Return -1 if curly braces expansion failed.
6091 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092 * If the name contains 'magic' {}'s, expand them and return the
6093 * expanded name in an allocated string via 'alias' - caller must free.
6094 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006095 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006096get_name_len(
6097 char_u **arg,
6098 char_u **alias,
6099 int evaluate,
6100 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101{
6102 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 char_u *p;
6104 char_u *expr_start;
6105 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006107 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108
6109 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6110 && (*arg)[2] == (int)KE_SNR)
6111 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006112 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 *arg += 3;
6114 return get_id_len(arg) + 3;
6115 }
6116 len = eval_fname_script(*arg);
6117 if (len > 0)
6118 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006119 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120 *arg += len;
6121 }
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006124 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006126 p = find_name_end(*arg, &expr_start, &expr_end,
6127 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006128 if (expr_start != NULL)
6129 {
6130 char_u *temp_string;
6131
6132 if (!evaluate)
6133 {
6134 len += (int)(p - *arg);
6135 *arg = skipwhite(p);
6136 return len;
6137 }
6138
6139 /*
6140 * Include any <SID> etc in the expanded string:
6141 * Thus the -len here.
6142 */
6143 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6144 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006145 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006146 *alias = temp_string;
6147 *arg = skipwhite(p);
6148 return (int)STRLEN(temp_string);
6149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150
6151 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006152 // Only give an error when there is something, otherwise it will be
6153 // reported at a higher level.
6154 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006155 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 return len;
6158}
6159
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006160/*
6161 * Find the end of a variable or function name, taking care of magic braces.
6162 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6163 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006164 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006165 * Return a pointer to just after the name. Equal to "arg" if there is no
6166 * valid name.
6167 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006168 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006169find_name_end(
6170 char_u *arg,
6171 char_u **expr_start,
6172 char_u **expr_end,
6173 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006175 int mb_nest = 0;
6176 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006178 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006179 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006181 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006183 *expr_start = NULL;
6184 *expr_end = NULL;
6185 }
6186
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006187 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006188 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6189 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006190 return arg;
6191
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006192 for (p = arg; *p != NUL
6193 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006194 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006195 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006196 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006197 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006198 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006199 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006200 if (*p == '\'')
6201 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006202 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006203 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006204 ;
6205 if (*p == NUL)
6206 break;
6207 }
6208 else if (*p == '"')
6209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006210 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006211 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006212 if (*p == '\\' && p[1] != NUL)
6213 ++p;
6214 if (*p == NUL)
6215 break;
6216 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006217 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6218 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006219 // "s:" is start of "s:var", but "n:" is not and can be used in
6220 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006221 len = (int)(p - arg);
6222 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006223 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006224 break;
6225 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006226
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006227 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006228 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006229 if (*p == '[')
6230 ++br_nest;
6231 else if (*p == ']')
6232 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006233 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006234
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006235 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006237 if (*p == '{')
6238 {
6239 mb_nest++;
6240 if (expr_start != NULL && *expr_start == NULL)
6241 *expr_start = p;
6242 }
6243 else if (*p == '}')
6244 {
6245 mb_nest--;
6246 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6247 *expr_end = p;
6248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006250 }
6251
6252 return p;
6253}
6254
6255/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006256 * Expands out the 'magic' {}'s in a variable/function name.
6257 * Note that this can call itself recursively, to deal with
6258 * constructs like foo{bar}{baz}{bam}
6259 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6260 * "in_start" ^
6261 * "expr_start" ^
6262 * "expr_end" ^
6263 * "in_end" ^
6264 *
6265 * Returns a new allocated string, which the caller must free.
6266 * Returns NULL for failure.
6267 */
6268 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006269make_expanded_name(
6270 char_u *in_start,
6271 char_u *expr_start,
6272 char_u *expr_end,
6273 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006274{
6275 char_u c1;
6276 char_u *retval = NULL;
6277 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006278
6279 if (expr_end == NULL || in_end == NULL)
6280 return NULL;
6281 *expr_start = NUL;
6282 *expr_end = NUL;
6283 c1 = *in_end;
6284 *in_end = NUL;
6285
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006286 temp_result = eval_to_string(expr_start + 1, FALSE);
6287 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006288 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006289 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6290 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006291 if (retval != NULL)
6292 {
6293 STRCPY(retval, in_start);
6294 STRCAT(retval, temp_result);
6295 STRCAT(retval, expr_end + 1);
6296 }
6297 }
6298 vim_free(temp_result);
6299
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006300 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006301 *expr_start = '{';
6302 *expr_end = '}';
6303
6304 if (retval != NULL)
6305 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006306 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006307 if (expr_start != NULL)
6308 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006309 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006310 temp_result = make_expanded_name(retval, expr_start,
6311 expr_end, temp_result);
6312 vim_free(retval);
6313 retval = temp_result;
6314 }
6315 }
6316
6317 return retval;
6318}
6319
6320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006322 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006325eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006327 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006328}
6329
6330/*
6331 * Return TRUE if character "c" can be used as the first character in a
6332 * variable or function name (excluding '{' and '}').
6333 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006335eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006336{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006337 return ASCII_ISALPHA(c) || c == '_';
6338}
6339
6340/*
6341 * Return TRUE if character "c" can be used as the first character of a
6342 * dictionary key.
6343 */
6344 int
6345eval_isdictc(int c)
6346{
6347 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006348}
6349
6350/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006351 * Handle:
6352 * - expr[expr], expr[expr:expr] subscript
6353 * - ".name" lookup
6354 * - function call with Funcref variable: func(expr)
6355 * - method call: var->method()
6356 *
6357 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006358 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006359 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006360 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006361handle_subscript(
6362 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006363 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006364 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006365 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006366 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006367{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006368 int evaluate = evalarg != NULL
6369 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006370 int ret = OK;
6371 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006372 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006373 int getnext;
6374 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006375
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006376 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006377 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006378 // When at the end of the line and ".name" or "->{" or "->X" follows in
6379 // the next line then consume the line break.
6380 p = eval_next_non_blank(*arg, evalarg, &getnext);
6381 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006382 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006383 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6384 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6385 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006386 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006387 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006388 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006389 check_white = FALSE;
6390 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006391
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006392 if (rettv->v_type == VAR_ANY)
6393 {
6394 char_u *exp_name;
6395 int cc;
6396 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006397 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006398 type_T *type;
6399
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006400 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006401 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006402 if (**arg != '.')
6403 {
6404 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006405 semsg(_(e_expected_dot_after_name_str),
6406 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006407 ret = FAIL;
6408 break;
6409 }
6410 ++*arg;
6411 if (IS_WHITE_OR_NUL(**arg))
6412 {
6413 if (verbose)
6414 emsg(_(e_no_white_space_allowed_after_dot));
6415 ret = FAIL;
6416 break;
6417 }
6418
6419 // isolate the name
6420 exp_name = *arg;
6421 while (eval_isnamec(**arg))
6422 ++*arg;
6423 cc = **arg;
6424 **arg = NUL;
6425
6426 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006427 evalarg == NULL ? NULL : evalarg->eval_cctx,
6428 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006429 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006430
6431 if (idx < 0 && ufunc == NULL)
6432 {
6433 ret = FAIL;
6434 break;
6435 }
6436 if (idx >= 0)
6437 {
6438 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6439 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6440
6441 copy_tv(sv->sv_tv, rettv);
6442 }
6443 else
6444 {
6445 rettv->v_type = VAR_FUNC;
6446 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6447 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006448 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006449 }
6450
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006451 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6452 || rettv->v_type == VAR_PARTIAL))
6453 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006454 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006455 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6456 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006457
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006458 // Stop the expression evaluation when immediately aborting on
6459 // error, or when an interrupt occurred or an exception was thrown
6460 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006461 if (aborting())
6462 {
6463 if (ret == OK)
6464 clear_tv(rettv);
6465 ret = FAIL;
6466 }
6467 dict_unref(selfdict);
6468 selfdict = NULL;
6469 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006470 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006471 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006472 if (in_vim9script())
6473 *arg = skipwhite(p + 2);
6474 else
6475 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006476 if (ret == OK)
6477 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006478 if (VIM_ISWHITE(**arg))
6479 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006480 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006481 ret = FAIL;
6482 }
6483 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006484 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006485 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006486 else
6487 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006488 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006489 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006490 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006491 // "." is ".name" lookup when we found a dict or when evaluating and
6492 // scriptversion is at least 2, where string concatenation is "..".
6493 else if (**arg == '['
6494 || (**arg == '.' && (rettv->v_type == VAR_DICT
6495 || (!evaluate
6496 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006497 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006498 {
6499 dict_unref(selfdict);
6500 if (rettv->v_type == VAR_DICT)
6501 {
6502 selfdict = rettv->vval.v_dict;
6503 if (selfdict != NULL)
6504 ++selfdict->dv_refcount;
6505 }
6506 else
6507 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006508 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006509 {
6510 clear_tv(rettv);
6511 ret = FAIL;
6512 }
6513 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006514 else
6515 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006516 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006517
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006518 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6519 // Don't do this when "Func" is already a partial that was bound
6520 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006521 if (selfdict != NULL
6522 && (rettv->v_type == VAR_FUNC
6523 || (rettv->v_type == VAR_PARTIAL
6524 && (rettv->vval.v_partial->pt_auto
6525 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006526 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006527
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006528 dict_unref(selfdict);
6529 return ret;
6530}
6531
6532/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006533 * Make a copy of an item.
6534 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006535 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6537 * reference to an already copied list/dict can be used.
6538 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006539 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006540 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541item_copy(
6542 typval_T *from,
6543 typval_T *to,
6544 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006545 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006546 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006547{
6548 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006549 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006550
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006552 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006553 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006554 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 }
6556 ++recurse;
6557
6558 switch (from->v_type)
6559 {
6560 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006561 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006562 case VAR_STRING:
6563 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006564 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006565 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006566 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006567 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006568 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006569 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006570 copy_tv(from, to);
6571 break;
6572 case VAR_LIST:
6573 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006574 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 if (from->vval.v_list == NULL)
6576 to->vval.v_list = NULL;
6577 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6578 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006579 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 to->vval.v_list = from->vval.v_list->lv_copylist;
6581 ++to->vval.v_list->lv_refcount;
6582 }
6583 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006584 to->vval.v_list = list_copy(from->vval.v_list,
6585 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 if (to->vval.v_list == NULL)
6587 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006588 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006589 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006590 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006591 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 case VAR_DICT:
6593 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006594 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 if (from->vval.v_dict == NULL)
6596 to->vval.v_dict = NULL;
6597 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6598 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006599 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6601 ++to->vval.v_dict->dv_refcount;
6602 }
6603 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006604 to->vval.v_dict = dict_copy(from->vval.v_dict,
6605 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006606 if (to->vval.v_dict == NULL)
6607 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006609 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006610 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006611 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006612 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006614 }
6615 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006617}
6618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006619 void
6620echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6621{
6622 char_u *tofree;
6623 char_u numbuf[NUMBUFLEN];
6624 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6625
6626 if (*atstart)
6627 {
6628 *atstart = FALSE;
6629 // Call msg_start() after eval1(), evaluating the expression
6630 // may cause a message to appear.
6631 if (with_space)
6632 {
6633 // Mark the saved text as finishing the line, so that what
6634 // follows is displayed on a new line when scrolling back
6635 // at the more prompt.
6636 msg_sb_eol();
6637 msg_start();
6638 }
6639 }
6640 else if (with_space)
6641 msg_puts_attr(" ", echo_attr);
6642
6643 if (p != NULL)
6644 for ( ; *p != NUL && !got_int; ++p)
6645 {
6646 if (*p == '\n' || *p == '\r' || *p == TAB)
6647 {
6648 if (*p != TAB && *needclr)
6649 {
6650 // remove any text still there from the command
6651 msg_clr_eos();
6652 *needclr = FALSE;
6653 }
6654 msg_putchar_attr(*p, echo_attr);
6655 }
6656 else
6657 {
6658 if (has_mbyte)
6659 {
6660 int i = (*mb_ptr2len)(p);
6661
6662 (void)msg_outtrans_len_attr(p, i, echo_attr);
6663 p += i - 1;
6664 }
6665 else
6666 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6667 }
6668 }
6669 vim_free(tofree);
6670}
6671
Bram Moolenaare9a41262005-01-15 22:18:47 +00006672/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 * ":echo expr1 ..." print each argument separated with a space, add a
6674 * newline at the end.
6675 * ":echon expr1 ..." print each argument plain.
6676 */
6677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006678ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679{
6680 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006682 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 int needclr = TRUE;
6684 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006685 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006686 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006687 evalarg_T evalarg;
6688
Bram Moolenaare707c882020-07-01 13:07:37 +02006689 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 if (eap->skip)
6692 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006693 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006694 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006695 // If eval1() causes an error message the text from the command may
6696 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006697 need_clr_eos = needclr;
6698
Bram Moolenaar68db9962021-05-09 23:19:22 +02006699 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006700 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {
6702 /*
6703 * Report the invalid expression unless the expression evaluation
6704 * has been cancelled due to an aborting error, an interrupt, or an
6705 * exception.
6706 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006707 if (!aborting() && did_emsg == did_emsg_before
6708 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006709 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006710 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 break;
6712 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006713 need_clr_eos = FALSE;
6714
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006716 {
6717 if (rettv.v_type == VAR_VOID)
6718 {
6719 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6720 break;
6721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006722 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006723 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006724
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006725 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726 arg = skipwhite(arg);
6727 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006728 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006729 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730
6731 if (eap->skip)
6732 --emsg_skip;
6733 else
6734 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006735 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 if (needclr)
6737 msg_clr_eos();
6738 if (eap->cmdidx == CMD_echo)
6739 msg_end();
6740 }
6741}
6742
6743/*
6744 * ":echohl {name}".
6745 */
6746 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006747ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006749 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750}
6751
6752/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006753 * Returns the :echo attribute
6754 */
6755 int
6756get_echo_attr(void)
6757{
6758 return echo_attr;
6759}
6760
6761/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006762 * ":execute expr1 ..." execute the result of an expression.
6763 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006764 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006766 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 * Each gets spaces around each argument and a newline at the end for
6768 * echo commands
6769 */
6770 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006771ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006772{
6773 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006774 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 int ret = OK;
6776 char_u *p;
6777 garray_T ga;
6778 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006779 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780
6781 ga_init2(&ga, 1, 80);
6782
6783 if (eap->skip)
6784 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006785 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006787 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006788 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790
6791 if (!eap->skip)
6792 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006793 char_u buf[NUMBUFLEN];
6794
6795 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006796 {
6797 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6798 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006799 semsg(_(e_using_invalid_value_as_string_str),
6800 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006801 p = NULL;
6802 }
6803 else
6804 p = tv_get_string_buf(&rettv, buf);
6805 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006806 else
6807 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006808 if (p == NULL)
6809 {
6810 clear_tv(&rettv);
6811 ret = FAIL;
6812 break;
6813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 len = (int)STRLEN(p);
6815 if (ga_grow(&ga, len + 2) == FAIL)
6816 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006817 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818 ret = FAIL;
6819 break;
6820 }
6821 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006823 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 ga.ga_len += len;
6825 }
6826
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006827 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 arg = skipwhite(arg);
6829 }
6830
6831 if (ret != FAIL && ga.ga_data != NULL)
6832 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006833 // use the first line of continuation lines for messages
6834 SOURCING_LNUM = start_lnum;
6835
Bram Moolenaar37fef162022-08-29 18:16:32 +01006836 if (eap->cmdidx == CMD_echomsg
6837 || eap->cmdidx == CMD_echowindow
6838 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006839 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006840 // Mark the already saved text as finishing the line, so that what
6841 // follows is displayed on a new line when scrolling back at the
6842 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006843 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006844 }
6845
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006846 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006847 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006848 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006849 out_flush();
6850 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006851 else if (eap->cmdidx == CMD_echowindow)
6852 {
6853#ifdef HAS_MESSAGE_WINDOW
6854 start_echowindow();
6855#endif
6856 msg_attr(ga.ga_data, echo_attr);
6857#ifdef HAS_MESSAGE_WINDOW
6858 end_echowindow();
6859#endif
6860 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006861 else if (eap->cmdidx == CMD_echoconsole)
6862 {
6863 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6864 ui_write((char_u *)"\r\n", 2, TRUE);
6865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 else if (eap->cmdidx == CMD_echoerr)
6867 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006868 int save_did_emsg = did_emsg;
6869
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006870 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006871 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006872 if (!force_abort)
6873 did_emsg = save_did_emsg;
6874 }
6875 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006876 {
6877 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6878
6879 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6880 sticky_cmdmod_flags = cmdmod.cmod_flags
6881 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 do_cmdline((char_u *)ga.ga_data,
6883 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006884 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006886 }
6887
6888 ga_clear(&ga);
6889
6890 if (eap->skip)
6891 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006892 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893}
6894
6895/*
6896 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6897 * "arg" points to the "&" or '+' when called, to "option" when returning.
6898 * Returns NULL when no option name found. Otherwise pointer to the char
6899 * after the option name.
6900 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006901 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006902find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903{
6904 char_u *p = *arg;
6905
6906 ++p;
6907 if (*p == 'g' && p[1] == ':')
6908 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006909 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 p += 2;
6911 }
6912 else if (*p == 'l' && p[1] == ':')
6913 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006914 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 p += 2;
6916 }
6917 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006918 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919
6920 if (!ASCII_ISALPHA(*p))
6921 return NULL;
6922 *arg = p;
6923
6924 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006925 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 else
6927 while (ASCII_ISALPHA(*p))
6928 ++p;
6929 return p;
6930}
6931
6932/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006933 * Display script name where an item was last set.
6934 * Should only be invoked when 'verbose' is non-zero.
6935 */
6936 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006937last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006938{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006939 char_u *p;
6940
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006941 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006942 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006943 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006944 if (p != NULL)
6945 {
6946 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006947 msg_puts(_("\n\tLast set from "));
6948 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006949 if (script_ctx.sc_lnum > 0)
6950 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006951 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006952 msg_outnum((long)script_ctx.sc_lnum);
6953 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006954 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006955 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006956 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006957 }
6958}
6959
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006960#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961
6962/*
6963 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006964 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 * "flags" can be "g" to do a global substitute.
6966 * Returns an allocated string, NULL for error.
6967 */
6968 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006969do_string_sub(
6970 char_u *str,
6971 char_u *pat,
6972 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006973 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006974 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975{
6976 int sublen;
6977 regmatch_T regmatch;
6978 int i;
6979 int do_all;
6980 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006981 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 garray_T ga;
6983 char_u *ret;
6984 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006985 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006987 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006989 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990
6991 ga_init2(&ga, 1, 200);
6992
6993 do_all = (flags[0] == 'g');
6994
6995 regmatch.rm_ic = p_ic;
6996 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6997 if (regmatch.regprog != NULL)
6998 {
6999 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007000 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007001 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7002 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007003 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01007004 if (regmatch.startp[0] == regmatch.endp[0])
7005 {
7006 if (zero_width == regmatch.startp[0])
7007 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007008 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02007009 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007010 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7011 (size_t)i);
7012 ga.ga_len += i;
7013 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007014 continue;
7015 }
7016 zero_width = regmatch.startp[0];
7017 }
7018
Bram Moolenaar071d4272004-06-13 20:20:40 +00007019 /*
7020 * Get some space for a temporary buffer to do the substitution
7021 * into. It will contain:
7022 * - The text up to where the match is.
7023 * - The substituted text.
7024 * - The text after the match.
7025 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007026 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007027 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7029 {
7030 ga_clear(&ga);
7031 break;
7032 }
7033
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007034 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 i = (int)(regmatch.startp[0] - tail);
7036 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007037 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007038 (void)vim_regsub(&regmatch, sub, expr,
7039 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7040 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007042 tail = regmatch.endp[0];
7043 if (*tail == NUL)
7044 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 if (!do_all)
7046 break;
7047 }
7048
7049 if (ga.ga_data != NULL)
7050 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7051
Bram Moolenaar473de612013-06-08 18:19:48 +02007052 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 }
7054
7055 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7056 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007057 if (p_cpo == empty_option)
7058 p_cpo = save_cpo;
7059 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007060 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007061 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007062 // If it's still empty it was changed and restored, need to restore in
7063 // the complicated way.
7064 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007065 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007066 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007067 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068
7069 return ret;
7070}