blob: 2330bd6a10682d618b08f0863a641b3f92b8d917 [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 Moolenaar22fcfad2016-07-01 18:17:26 +0200902 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 char_u *s;
Bram Moolenaare70dd112022-01-21 16:31:11 +0000904 sctx_T saved_sctx = current_sctx;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000905 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
906 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907
Bram Moolenaare70dd112022-01-21 16:31:11 +0000908 arg = wp->w_p_fde;
909 current_sctx = wp->w_p_script_ctx[WV_FDE];
910
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000912 if (use_sandbox)
913 ++sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100914 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915 *cp = NUL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200916 if (eval0(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 retval = 0;
918 else
919 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100920 // If the result is a number, just return the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000921 if (tv.v_type == VAR_NUMBER)
922 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000923 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 retval = 0;
925 else
926 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100927 // If the result is a string, check if there is a non-digit before
928 // the number.
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000929 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 if (!VIM_ISDIGIT(*s) && *s != '-')
931 *cp = *s++;
932 retval = atol((char *)s);
933 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000934 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 }
936 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000937 if (use_sandbox)
938 --sandbox;
zeertzjqcfe45652022-05-27 17:26:55 +0100939 --textlock;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +0200940 clear_evalarg(&EVALARG_EVALUATE, NULL);
Bram Moolenaare70dd112022-01-21 16:31:11 +0000941 current_sctx = saved_sctx;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200943 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944}
945#endif
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 * Get an lval: variable, Dict item or List item that can be assigned a value
949 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
950 * "name.key", "name.key[expr]" etc.
951 * Indexing only works if "name" is an existing List or Dictionary.
952 * "name" points to the start of the name.
953 * If "rettv" is not NULL it points to the value to be assigned.
954 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
955 * wrong; must end in space or cmd separator.
956 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100957 * flags:
958 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100959 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100960 * GLV_NO_AUTOLOAD: do not use script autoloading
961 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000962 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000963 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000964 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000965 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200966 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100967get_lval(
968 char_u *name,
969 typval_T *rettv,
970 lval_T *lp,
971 int unlet,
972 int skip,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100973 int flags, // GLV_ values
974 int fne_flags) // flags for find_name_end()
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000976 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000977 char_u *expr_start, *expr_end;
978 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000979 dictitem_T *v;
980 typval_T var1;
981 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000982 int empty1 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000983 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000984 int len;
Bram Moolenaar896ad2c2020-11-21 14:03:43 +0100985 hashtab_T *ht = NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100986 int quiet = flags & GLV_QUIET;
Bram Moolenaar32b3f822021-01-06 21:59:39 +0100987 int writing;
Bram Moolenaar4525a572022-02-13 11:57:33 +0000988 int vim9script = in_vim9script();
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000989
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Clear everything in "lp".
Bram Moolenaara80faa82020-04-12 19:37:17 +0200991 CLEAR_POINTER(lp);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100993 if (skip || (flags & GLV_COMPILING))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000994 {
Bram Moolenaar2ef951d2021-01-03 20:55:26 +0100995 // When skipping or compiling just find the end of the name.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 lp->ll_name = name;
Bram Moolenaar9b5384b2020-06-29 22:31:36 +0200997 lp->ll_name_end = find_name_end(name, NULL, NULL,
998 FNE_INCL_BR | fne_flags);
999 return lp->ll_name_end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001000 }
1001
Bram Moolenaara749a422022-02-12 19:52:25 +00001002 // Cannot use "s:var" at the Vim9 script level. "s: type" is OK.
Bram Moolenaar4525a572022-02-13 11:57:33 +00001003 if (vim9script && at_script_level()
Bram Moolenaara749a422022-02-12 19:52:25 +00001004 && name[0] == 's' && name[1] == ':' && !VIM_ISWHITE(name[2]))
1005 {
1006 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), name);
1007 return NULL;
1008 }
1009
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001010 // Find the end of the name.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001011 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001013 if (expr_start != NULL)
1014 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001015 // Don't expand the name when we already know there is an error.
Bram Moolenaar1c465442017-03-12 20:10:05 +01001016 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001017 && *p != '[' && *p != '.')
1018 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001019 semsg(_(e_trailing_characters_str), p);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001020 return NULL;
1021 }
1022
1023 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1024 if (lp->ll_exp_name == NULL)
1025 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001026 // Report an invalid expression in braces, unless the
1027 // expression evaluation has been cancelled due to an
1028 // aborting error, an interrupt, or an exception.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029 if (!aborting() && !quiet)
1030 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001031 emsg_severe = TRUE;
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001032 semsg(_(e_invalid_argument_str), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001033 return NULL;
1034 }
1035 }
1036 lp->ll_name = lp->ll_exp_name;
1037 }
1038 else
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001040 lp->ll_name = name;
1041
Bram Moolenaar4525a572022-02-13 11:57:33 +00001042 if (vim9script)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 {
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001044 // "a: type" is declaring variable "a" with a type, not "a:".
Bram Moolenaar2e17fef2022-03-18 19:44:48 +00001045 // However, "g:[key]" is indexing a dictionary.
1046 if (p == name + 2 && p[-1] == ':' && *p != '[')
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001047 {
1048 --p;
1049 lp->ll_name_end = p;
1050 }
1051 if (*p == ':')
1052 {
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001053 char_u *tp = skipwhite(p + 1);
Bram Moolenaar60faf862021-08-23 22:22:45 +02001054
Bram Moolenaar9510d222022-09-11 15:14:05 +01001055 if (is_scoped_variable(name))
1056 {
1057 semsg(_(e_cannot_use_type_with_this_variable_str), name);
1058 return NULL;
1059 }
Bram Moolenaar60faf862021-08-23 22:22:45 +02001060 if (tp == p + 1 && !quiet)
1061 {
1062 semsg(_(e_white_space_required_after_str_str), ":", p);
1063 return NULL;
1064 }
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001065 if (!SCRIPT_ID_VALID(current_sctx.sc_sid))
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001066 {
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001067 semsg(_(e_using_type_not_in_script_context_str), p);
1068 return NULL;
Bram Moolenaarc653e4a2022-01-05 10:16:30 +00001069 }
1070
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001071 // parse the type after the name
Bram Moolenaar4c8b5462022-03-16 20:01:39 +00001072 lp->ll_type = parse_type(&tp,
1073 &SCRIPT_ITEM(current_sctx.sc_sid)->sn_type_list,
1074 !quiet);
Bram Moolenaar9e68c322020-12-25 12:38:04 +01001075 if (lp->ll_type == NULL && !quiet)
1076 return NULL;
Bram Moolenaar95dd9f22020-08-07 19:28:08 +02001077 lp->ll_name_end = tp;
1078 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001079 }
1080 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001081 if (lp->ll_name == NULL)
1082 return p;
1083
Bram Moolenaar62aec932022-01-29 21:45:34 +00001084 if (*p == '.')
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001085 {
Bram Moolenaar4b1d9632022-02-13 21:51:08 +00001086 imported_T *import = find_imported(lp->ll_name, p - lp->ll_name, TRUE);
Bram Moolenaardc4451d2022-01-09 21:36:37 +00001087
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001088 if (import != NULL)
1089 {
1090 ufunc_T *ufunc;
1091 type_T *type;
1092
Bram Moolenaar753885b2022-08-24 16:30:36 +01001093 import_check_sourced_sid(&import->imp_sid);
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001094 lp->ll_sid = import->imp_sid;
1095 lp->ll_name = skipwhite(p + 1);
1096 p = find_name_end(lp->ll_name, NULL, NULL, fne_flags);
1097 lp->ll_name_end = p;
1098
1099 // check the item is exported
1100 cc = *p;
1101 *p = NUL;
1102 if (find_exported(import->imp_sid, lp->ll_name, &ufunc, &type,
Bram Moolenaarb6a138e2022-02-08 21:17:22 +00001103 NULL, NULL, TRUE) == -1)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001104 {
1105 *p = cc;
Bram Moolenaar5d982692022-01-12 15:15:27 +00001106 return NULL;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001107 }
1108 *p = cc;
1109 }
1110 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001112 // Without [idx] or .key we are done.
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001113 if ((*p != '[' && *p != '.'))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001114 return p;
1115
Bram Moolenaar4525a572022-02-13 11:57:33 +00001116 if (vim9script && lval_root != NULL)
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001117 {
1118 // using local variable
1119 lp->ll_tv = lval_root;
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001120 v = NULL;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001121 }
1122 else
1123 {
1124 cc = *p;
1125 *p = NUL;
1126 // When we would write to the variable pass &ht and prevent autoload.
1127 writing = !(flags & GLV_READ_ONLY);
1128 v = find_var(lp->ll_name, writing ? &ht : NULL,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01001129 (flags & GLV_NO_AUTOLOAD) || writing);
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001130 if (v == NULL && !quiet)
1131 semsg(_(e_undefined_variable_str), lp->ll_name);
1132 *p = cc;
1133 if (v == NULL)
1134 return NULL;
1135 lp->ll_tv = &v->di_tv;
1136 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001137
Bram Moolenaar4525a572022-02-13 11:57:33 +00001138 if (vim9script && (flags & GLV_NO_DECL) == 0)
Bram Moolenaar8f22f5c2020-12-19 22:10:13 +01001139 {
1140 if (!quiet)
1141 semsg(_(e_variable_already_declared), lp->ll_name);
1142 return NULL;
1143 }
1144
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001145 /*
1146 * Loop until no more [idx] or .key is following.
1147 */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001148 var1.v_type = VAR_UNKNOWN;
1149 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001150 while (*p == '[' || (*p == '.' && p[1] != '=' && p[1] != '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001151 {
Bram Moolenaarf21d5462022-09-10 12:36:00 +01001152 int r = OK;
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001153
Bram Moolenaar3a3b10e2021-06-26 15:00:59 +02001154 if (*p == '.' && lp->ll_tv->v_type != VAR_DICT)
1155 {
1156 if (!quiet)
1157 semsg(_(e_dot_can_only_be_used_on_dictionary_str), name);
1158 return NULL;
1159 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001160 if (lp->ll_tv->v_type != VAR_LIST
1161 && lp->ll_tv->v_type != VAR_DICT
1162 && lp->ll_tv->v_type != VAR_BLOB)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001163 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001164 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001165 emsg(_(e_can_only_index_list_dictionary_or_blob));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001166 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001167 }
Bram Moolenaare65081d2021-06-27 15:04:05 +02001168
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001169 // A NULL list/blob works like an empty list/blob, allocate one now.
Bram Moolenaare65081d2021-06-27 15:04:05 +02001170 if (lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001171 r = rettv_list_alloc(lp->ll_tv);
Bram Moolenaare65081d2021-06-27 15:04:05 +02001172 else if (lp->ll_tv->v_type == VAR_BLOB
1173 && lp->ll_tv->vval.v_blob == NULL)
Bram Moolenaar12553ad2022-09-10 10:42:20 +01001174 r = rettv_blob_alloc(lp->ll_tv);
1175 if (r == FAIL)
1176 return NULL;
Bram Moolenaare65081d2021-06-27 15:04:05 +02001177
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001178 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001179 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001180 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001181 emsg(_(e_slice_must_come_last));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001182 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001183 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001184
Bram Moolenaar4525a572022-02-13 11:57:33 +00001185 if (vim9script && lp->ll_valtype == NULL
Bram Moolenaar78a9c2e2021-08-13 20:12:13 +02001186 && v != NULL
Bram Moolenaar10c65862020-10-08 21:16:42 +02001187 && lp->ll_tv == &v->di_tv
1188 && ht != NULL && ht == get_script_local_ht())
1189 {
Bram Moolenaar7a411a32022-04-04 14:58:06 +01001190 svar_T *sv = find_typval_in_script(lp->ll_tv, 0, TRUE);
Bram Moolenaar10c65862020-10-08 21:16:42 +02001191
1192 // Vim9 script local variable: get the type
1193 if (sv != NULL)
1194 lp->ll_valtype = sv->sv_type;
1195 }
1196
Bram Moolenaar8c711452005-01-14 21:53:12 +00001197 len = -1;
1198 if (*p == '.')
1199 {
1200 key = p + 1;
1201 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1202 ;
1203 if (len == 0)
1204 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001205 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001206 emsg(_(e_cannot_use_empty_key_for_dictionary));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001207 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001208 }
1209 p = key + len;
1210 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001211 else
1212 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001213 // Get the index [expr] or the first index [expr: ].
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001214 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001215 if (*p == ':')
1216 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001217 else
1218 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001219 empty1 = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001220 if (eval1(&p, &var1, &EVALARG_EVALUATE) == FAIL) // recursive!
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001221 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001222 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001223 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001224 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001225 clear_tv(&var1);
1226 return NULL;
1227 }
Bram Moolenaar6a250262020-08-04 15:53:01 +02001228 p = skipwhite(p);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001229 }
1230
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001231 // Optionally get the second index [ :expr].
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 if (*p == ':')
1233 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001234 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001235 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001236 if (!quiet)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02001237 emsg(_(e_cannot_slice_dictionary));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001238 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001239 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001240 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001241 if (rettv != NULL
1242 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001243 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001244 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001245 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001246 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001247 if (!quiet)
Bram Moolenaara6f79292022-01-04 21:30:47 +00001248 emsg(_(e_slice_requires_list_or_blob_value));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001249 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001250 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001251 }
1252 p = skipwhite(p + 1);
1253 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001254 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001255 else
1256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001257 lp->ll_empty2 = FALSE;
Bram Moolenaar32e35112020-05-14 22:41:15 +02001258 // recursive!
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02001259 if (eval1(&p, &var2, &EVALARG_EVALUATE) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001260 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001261 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001262 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001263 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001264 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001265 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001266 // not a number or string
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001267 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001268 clear_tv(&var2);
1269 return NULL;
1270 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001271 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001272 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001273 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001274 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001275 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001276
Bram Moolenaar8c711452005-01-14 21:53:12 +00001277 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001278 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001279 if (!quiet)
Bram Moolenaare1242042021-12-16 20:56:57 +00001280 emsg(_(e_missing_closing_square_brace));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001281 clear_tv(&var1);
1282 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001284 }
1285
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001286 // Skip to past ']'.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001287 ++p;
1288 }
1289
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001290 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001291 {
1292 if (len == -1)
1293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001294 // "[key]": get key from "var1"
1295 key = tv_get_string_chk(&var1); // is number or string
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001296 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001297 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001298 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001299 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001300 }
1301 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001302 lp->ll_list = NULL;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001303
1304 // a NULL dict is equivalent with an empty dict
1305 if (lp->ll_tv->vval.v_dict == NULL)
1306 {
1307 lp->ll_tv->vval.v_dict = dict_alloc();
1308 if (lp->ll_tv->vval.v_dict == NULL)
1309 {
1310 clear_tv(&var1);
1311 return NULL;
1312 }
1313 ++lp->ll_tv->vval.v_dict->dv_refcount;
1314 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001315 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001316
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001317 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001318
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001319 // When assigning to a scope dictionary check that a function and
1320 // variable name is valid (only variable name unless it is l: or
1321 // g: dictionary). Disallow overwriting a builtin function.
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001322 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001323 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001324 int prevval;
1325 int wrong;
1326
1327 if (len != -1)
1328 {
1329 prevval = key[len];
1330 key[len] = NUL;
1331 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001332 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001333 prevval = 0; // avoid compiler warning
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001334 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1335 && rettv->v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001336 && var_wrong_func_name(key, lp->ll_di == NULL))
Bram Moolenaar3b3755f2021-11-22 20:10:18 +00001337 || !valid_varname(key, -1, TRUE);
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001338 if (len != -1)
1339 key[len] = prevval;
1340 if (wrong)
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001341 {
1342 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001343 return NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001344 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001345 }
1346
Bram Moolenaar10c65862020-10-08 21:16:42 +02001347 if (lp->ll_valtype != NULL)
1348 // use the type of the member
1349 lp->ll_valtype = lp->ll_valtype->tt_member;
1350
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001351 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001352 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001353 // Can't add "v:" or "a:" variable.
Bram Moolenaarda6c0332019-09-01 16:01:30 +02001354 if (lp->ll_dict == get_vimvar_dict()
Bram Moolenaar31b81602019-02-10 22:14:27 +01001355 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001356 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001357 semsg(_(e_illegal_variable_name_str), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001358 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001359 return NULL;
1360 }
1361
Bram Moolenaar31b81602019-02-10 22:14:27 +01001362 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001363 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001364 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001365 if (!quiet)
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001366 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001367 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001368 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001369 }
1370 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001371 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001372 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001373 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001374 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001375 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001376 p = NULL;
1377 break;
1378 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001379 // existing variable, need to check if it can be changed
Bram Moolenaar3a257732017-02-21 20:47:13 +01001380 else if ((flags & GLV_READ_ONLY) == 0
Bram Moolenaara187c432020-09-16 21:08:28 +02001381 && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
1382 || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001383 {
1384 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001385 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001386 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001387
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001388 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001389 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001390 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001391 else if (lp->ll_tv->v_type == VAR_BLOB)
1392 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001393 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1394
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001395 /*
1396 * Get the number and item for the only or first index of the List.
1397 */
1398 if (empty1)
1399 lp->ll_n1 = 0;
1400 else
1401 // is number or string
1402 lp->ll_n1 = (long)tv_get_number(&var1);
1403 clear_tv(&var1);
1404
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02001405 if (check_blob_index(bloblen, lp->ll_n1, quiet) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001406 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001407 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001408 return NULL;
1409 }
1410 if (lp->ll_range && !lp->ll_empty2)
1411 {
1412 lp->ll_n2 = (long)tv_get_number(&var2);
1413 clear_tv(&var2);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02001414 if (check_blob_range(bloblen, lp->ll_n1, lp->ll_n2, quiet)
1415 == FAIL)
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001416 return NULL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001417 }
1418 lp->ll_blob = lp->ll_tv->vval.v_blob;
1419 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001420 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001422 else
1423 {
1424 /*
1425 * Get the number and item for the only or first index of the List.
1426 */
1427 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001428 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001429 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001430 // is number or string
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001431 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001432 clear_tv(&var1);
1433
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001434 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001435 lp->ll_list = lp->ll_tv->vval.v_list;
Bram Moolenaar22ebd172022-04-01 15:26:58 +01001436 lp->ll_li = check_range_index_one(lp->ll_list, &lp->ll_n1,
1437 (flags & GLV_ASSIGN_WITH_OP) == 0, quiet);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001438 if (lp->ll_li == NULL)
1439 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001440 clear_tv(&var2);
1441 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001442 }
1443
Bram Moolenaar10c65862020-10-08 21:16:42 +02001444 if (lp->ll_valtype != NULL)
1445 // use the type of the member
1446 lp->ll_valtype = lp->ll_valtype->tt_member;
1447
Bram Moolenaar8c711452005-01-14 21:53:12 +00001448 /*
1449 * May need to find the item or absolute index for the second
1450 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001451 * When no index given: "lp->ll_empty2" is TRUE.
1452 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001453 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001454 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001455 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001456 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001457 // is number or string
Bram Moolenaar8c711452005-01-14 21:53:12 +00001458 clear_tv(&var2);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001459 if (check_range_index_two(lp->ll_list,
1460 &lp->ll_n1, lp->ll_li,
1461 &lp->ll_n2, quiet) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001462 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001463 }
1464
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001465 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001466 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001467 }
1468
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001469 clear_tv(&var1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 lp->ll_name_end = p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001471 return p;
1472}
1473
1474/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001475 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001476 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001478clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479{
1480 vim_free(lp->ll_exp_name);
1481 vim_free(lp->ll_newkey);
1482}
1483
1484/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001485 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001486 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001487 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1488 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001489 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001490 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001491set_var_lval(
1492 lval_T *lp,
1493 char_u *endp,
1494 typval_T *rettv,
1495 int copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001496 int flags, // ASSIGN_CONST, ASSIGN_NO_DECL
1497 char_u *op,
1498 int var_idx) // index for "let [a, b] = list"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001499{
1500 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001502
1503 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001505 cc = *endp;
1506 *endp = NUL;
Bram Moolenaard0edaf92021-05-28 21:06:08 +02001507 if (in_vim9script() && check_reserved_name(lp->ll_name) == FAIL)
1508 return;
1509
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001510 if (lp->ll_blob != NULL)
1511 {
1512 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001513
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001514 if (op != NULL && *op != '=')
1515 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001516 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001517 return;
1518 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001519 if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
Bram Moolenaar021bda52020-08-17 21:07:22 +02001520 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001521
1522 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1523 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001524 if (lp->ll_empty2)
1525 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1526
Bram Moolenaar68452172021-04-12 21:21:02 +02001527 if (blob_set_range(lp->ll_blob, lp->ll_n1, lp->ll_n2,
1528 rettv) == FAIL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001529 return;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001530 }
1531 else
1532 {
1533 val = (int)tv_get_number_chk(rettv, &error);
1534 if (!error)
Bram Moolenaare8209b92021-04-18 16:08:52 +02001535 blob_set_append(lp->ll_blob, lp->ll_n1, val);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001536 }
1537 }
1538 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001539 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001540 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001541
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001542 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1543 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001544 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001545 emsg(_(e_cannot_modify_existing_variable));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001546 *endp = cc;
1547 return;
1548 }
1549
Bram Moolenaarff697e62019-02-12 22:28:33 +01001550 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001551 di = NULL;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001552 if (eval_variable(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001553 lp->ll_sid, &tv, &di, EVAL_VAR_VERBOSE) == OK)
Bram Moolenaar79518e22017-02-17 16:31:35 +01001554 {
1555 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001556 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1557 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001558 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001559 set_var_const(lp->ll_name, lp->ll_sid, NULL, &tv, FALSE,
Bram Moolenaar24e93162021-07-18 20:40:33 +02001560 ASSIGN_NO_DECL, 0);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001561 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001563 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001564 else
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001565 {
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001566 if (lp->ll_type != NULL && check_typval_arg_type(lp->ll_type, rettv,
1567 NULL, 0) == FAIL)
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001568 return;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001569 set_var_const(lp->ll_name, lp->ll_sid, lp->ll_type, rettv, copy,
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001570 flags, var_idx);
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +02001571 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001572 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001573 }
Bram Moolenaara187c432020-09-16 21:08:28 +02001574 else if (value_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001575 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001576 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001577 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001578 else if (lp->ll_range)
1579 {
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001580 if ((flags & (ASSIGN_CONST | ASSIGN_FINAL))
1581 && (flags & ASSIGN_FOR_LOOP) == 0)
Bram Moolenaar9937a052019-06-15 15:45:06 +02001582 {
Bram Moolenaard5f400c2022-01-06 21:10:28 +00001583 emsg(_(e_cannot_lock_range));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001584 return;
1585 }
1586
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001587 (void)list_assign_range(lp->ll_list, rettv->vval.v_list,
1588 lp->ll_n1, lp->ll_n2, lp->ll_empty2, op, lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001589 }
1590 else
1591 {
1592 /*
1593 * Assign to a List or Dictionary item.
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_list_or_dict));
Bram Moolenaar9937a052019-06-15 15:45:06 +02001599 return;
1600 }
Bram Moolenaar10c65862020-10-08 21:16:42 +02001601
1602 if (lp->ll_valtype != NULL
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001603 && check_typval_arg_type(lp->ll_valtype, rettv,
1604 NULL, 0) == FAIL)
Bram Moolenaar10c65862020-10-08 21:16:42 +02001605 return;
1606
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001607 if (lp->ll_newkey != NULL)
1608 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001609 if (op != NULL && *op != '=')
1610 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001611 semsg(_(e_key_not_present_in_dictionary), lp->ll_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001612 return;
1613 }
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001614 if (dict_wrong_func_name(lp->ll_tv->vval.v_dict, rettv,
1615 lp->ll_newkey))
Bram Moolenaar3d9c4ee2021-05-31 22:15:26 +02001616 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001617
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001618 // Need to add an item to the Dictionary.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001619 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001620 if (di == NULL)
1621 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001622 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1623 {
1624 vim_free(di);
1625 return;
1626 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001627 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001628 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001629 else if (op != NULL && *op != '=')
1630 {
1631 tv_op(lp->ll_tv, rettv, op);
1632 return;
1633 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001634 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001635 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001636
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001637 /*
1638 * Assign the value to the variable or list item.
1639 */
1640 if (copy)
1641 copy_tv(rettv, lp->ll_tv);
1642 else
1643 {
1644 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001645 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001646 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001647 }
1648 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001649}
1650
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001651/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001652 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1653 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001654 * Returns OK or FAIL.
1655 */
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02001656 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001657tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001658{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001659 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 char_u numbuf[NUMBUFLEN];
1661 char_u *s;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001662 int failed = FALSE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001663
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001664 // Can't do anything with a Funcref or Dict on the right.
1665 // v:true and friends only work with "..=".
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001666 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
Bram Moolenaarf24f51d2021-08-01 12:01:49 +02001667 && ((tv2->v_type != VAR_BOOL && tv2->v_type != VAR_SPECIAL)
1668 || *op == '.'))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001669 {
1670 switch (tv1->v_type)
1671 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001672 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02001673 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 case VAR_VOID:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001675 case VAR_DICT:
1676 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001677 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01001678 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001679 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001680 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001681 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001682 case VAR_INSTR:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001683 break;
1684
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001685 case VAR_BLOB:
1686 if (*op != '+' || tv2->v_type != VAR_BLOB)
1687 break;
1688 // BLOB += BLOB
1689 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1690 {
1691 blob_T *b1 = tv1->vval.v_blob;
1692 blob_T *b2 = tv2->vval.v_blob;
1693 int i, len = blob_len(b2);
1694 for (i = 0; i < len; i++)
1695 ga_append(&b1->bv_ga, blob_get(b2, i));
1696 }
1697 return OK;
1698
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001699 case VAR_LIST:
1700 if (*op != '+' || tv2->v_type != VAR_LIST)
1701 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001702 // List += List
Bram Moolenaar81ed4962020-09-23 15:56:50 +02001703 if (tv2->vval.v_list != NULL)
1704 {
1705 if (tv1->vval.v_list == NULL)
1706 {
1707 tv1->vval.v_list = tv2->vval.v_list;
1708 ++tv1->vval.v_list->lv_refcount;
1709 }
1710 else
1711 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1712 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001713 return OK;
1714
1715 case VAR_NUMBER:
1716 case VAR_STRING:
1717 if (tv2->v_type == VAR_LIST)
1718 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001719 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001720 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001721 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001722 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001723 if (tv2->v_type == VAR_FLOAT)
1724 {
1725 float_T f = n;
1726
Bram Moolenaarff697e62019-02-12 22:28:33 +01001727 if (*op == '%')
1728 break;
1729 switch (*op)
1730 {
1731 case '+': f += tv2->vval.v_float; break;
1732 case '-': f -= tv2->vval.v_float; break;
1733 case '*': f *= tv2->vval.v_float; break;
1734 case '/': f /= tv2->vval.v_float; break;
1735 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001736 clear_tv(tv1);
1737 tv1->v_type = VAR_FLOAT;
1738 tv1->vval.v_float = f;
1739 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001740 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001741 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001742 switch (*op)
1743 {
1744 case '+': n += tv_get_number(tv2); break;
1745 case '-': n -= tv_get_number(tv2); break;
1746 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001747 case '/': n = num_divide(n, tv_get_number(tv2),
1748 &failed); break;
1749 case '%': n = num_modulus(n, tv_get_number(tv2),
1750 &failed); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001751 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001752 clear_tv(tv1);
1753 tv1->v_type = VAR_NUMBER;
1754 tv1->vval.v_number = n;
1755 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001756 }
1757 else
1758 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001759 if (tv2->v_type == VAR_FLOAT)
1760 break;
1761
Bram Moolenaarff697e62019-02-12 22:28:33 +01001762 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001763 s = tv_get_string(tv1);
1764 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001765 clear_tv(tv1);
1766 tv1->v_type = VAR_STRING;
1767 tv1->vval.v_string = s;
1768 }
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01001769 return failed ? FAIL : OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001770
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001771 case VAR_FLOAT:
1772 {
1773 float_T f;
1774
Bram Moolenaarff697e62019-02-12 22:28:33 +01001775 if (*op == '%' || *op == '.'
1776 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001777 && tv2->v_type != VAR_NUMBER
1778 && tv2->v_type != VAR_STRING))
1779 break;
1780 if (tv2->v_type == VAR_FLOAT)
1781 f = tv2->vval.v_float;
1782 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001783 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001784 switch (*op)
1785 {
1786 case '+': tv1->vval.v_float += f; break;
1787 case '-': tv1->vval.v_float -= f; break;
1788 case '*': tv1->vval.v_float *= f; break;
1789 case '/': tv1->vval.v_float /= f; break;
1790 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001791 }
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001792 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001793 }
1794 }
1795
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00001796 semsg(_(e_wrong_variable_type_for_str_equal), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001797 return FAIL;
1798}
1799
1800/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001801 * Evaluate the expression used in a ":for var in expr" command.
1802 * "arg" points to "var".
1803 * Set "*errp" to TRUE for an error, FALSE otherwise;
1804 * Return a pointer that holds the info. Null when there is an error.
1805 */
1806 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001807eval_for_line(
1808 char_u *arg,
1809 int *errp,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02001810 exarg_T *eap,
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001811 evalarg_T *evalarg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001812{
Bram Moolenaar33570922005-01-25 22:26:29 +00001813 forinfo_T *fi;
Bram Moolenaar404557e2021-07-05 21:41:48 +02001814 char_u *var_list_end;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001815 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001816 typval_T tv;
1817 list_T *l;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001818 int skip = !(evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001819
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001820 *errp = TRUE; // default: there is an error
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001821
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001822 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001823 if (fi == NULL)
1824 return NULL;
1825
Bram Moolenaar404557e2021-07-05 21:41:48 +02001826 var_list_end = skip_var_list(arg, TRUE, &fi->fi_varcount,
1827 &fi->fi_semicolon, FALSE);
1828 if (var_list_end == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001829 return fi;
1830
Bram Moolenaar404557e2021-07-05 21:41:48 +02001831 expr = skipwhite_and_linebreak(var_list_end, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001832 if (expr[0] != 'i' || expr[1] != 'n'
1833 || !(expr[2] == NUL || VIM_ISWHITE(expr[2])))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001834 {
Bram Moolenaar404557e2021-07-05 21:41:48 +02001835 if (in_vim9script() && *expr == ':' && expr != var_list_end)
1836 semsg(_(e_no_white_space_allowed_before_colon_str), expr);
1837 else
Bram Moolenaar3a846e62022-01-01 16:21:00 +00001838 emsg(_(e_missing_in_after_for));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001839 return fi;
1840 }
1841
1842 if (skip)
1843 ++emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001844 expr = skipwhite_and_linebreak(expr + 2, evalarg);
1845 if (eval0(expr, &tv, eap, evalarg) == OK)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001846 {
1847 *errp = FALSE;
1848 if (!skip)
1849 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001850 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001851 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001852 l = tv.vval.v_list;
1853 if (l == NULL)
1854 {
1855 // a null list is like an empty list: do nothing
1856 clear_tv(&tv);
1857 }
1858 else
1859 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 // Need a real list here.
Bram Moolenaar7e9f3512020-05-13 22:44:22 +02001861 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001863 // No need to increment the refcount, it's already set for
1864 // the list being used in "tv".
1865 fi->fi_list = l;
1866 list_add_watch(l, &fi->fi_lw);
1867 fi->fi_lw.lw_item = l->lv_first;
1868 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001869 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001870 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001871 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001872 fi->fi_bi = 0;
1873 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001874 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001875 typval_T btv;
1876
1877 // Make a copy, so that the iteration still works when the
1878 // blob is changed.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 blob_copy(tv.vval.v_blob, &btv);
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001880 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001881 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001882 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001883 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001884 else if (tv.v_type == VAR_STRING)
1885 {
1886 fi->fi_byte_idx = 0;
1887 fi->fi_string = tv.vval.v_string;
1888 tv.vval.v_string = NULL;
1889 if (fi->fi_string == NULL)
1890 fi->fi_string = vim_strsave((char_u *)"");
1891 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001892 else
1893 {
Sean Dewar80d73952021-08-04 19:25:54 +02001894 emsg(_(e_string_list_or_blob_required));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001895 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001896 }
1897 }
1898 }
1899 if (skip)
1900 --emsg_skip;
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001901 fi->fi_break_count = evalarg->eval_break_count;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902
1903 return fi;
1904}
1905
1906/*
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001907 * Used when looping over a :for line, skip the "in expr" part.
1908 */
1909 void
1910skip_for_lines(void *fi_void, evalarg_T *evalarg)
1911{
1912 forinfo_T *fi = (forinfo_T *)fi_void;
1913 int i;
1914
1915 for (i = 0; i < fi->fi_break_count; ++i)
Bram Moolenaare442d592022-05-05 12:20:28 +01001916 eval_next_line(NULL, evalarg);
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02001917}
1918
1919/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001920 * Use the first item in a ":for" list. Advance to the next.
1921 * Assign the values to the variable (list). "arg" points to the first one.
1922 * Return TRUE when a valid item was found, FALSE when at end of list or
1923 * something wrong.
1924 */
1925 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001926next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001927{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001928 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 int result;
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001930 int flag = ASSIGN_FOR_LOOP | (in_vim9script()
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001931 ? (ASSIGN_FINAL
1932 // first round: error if variable exists
1933 | (fi->fi_bi == 0 ? 0 : ASSIGN_DECL)
Bram Moolenaar766ae5b2022-09-14 00:30:51 +01001934 | ASSIGN_NO_MEMBER_TYPE
1935 | ASSIGN_UPDATE_BLOCK_ID)
Bram Moolenaarf6a8d422021-04-13 21:48:03 +02001936 : 0);
Bram Moolenaar33570922005-01-25 22:26:29 +00001937 listitem_T *item;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001938 int skip_assign = in_vim9script() && arg[0] == '_'
1939 && !eval_isnamec(arg[1]);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001940
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001941 if (fi->fi_blob != NULL)
1942 {
1943 typval_T tv;
1944
1945 if (fi->fi_bi >= blob_len(fi->fi_blob))
1946 return FALSE;
1947 tv.v_type = VAR_NUMBER;
1948 tv.v_lock = VAR_FIXED;
1949 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1950 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001951 if (skip_assign)
1952 return TRUE;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001953 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001954 fi->fi_varcount, flag, NULL) == OK;
1955 }
1956
1957 if (fi->fi_string != NULL)
1958 {
1959 typval_T tv;
1960 int len;
1961
1962 len = mb_ptr2len(fi->fi_string + fi->fi_byte_idx);
1963 if (len == 0)
1964 return FALSE;
1965 tv.v_type = VAR_STRING;
1966 tv.v_lock = VAR_FIXED;
1967 tv.vval.v_string = vim_strnsave(fi->fi_string + fi->fi_byte_idx, len);
1968 fi->fi_byte_idx += len;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001969 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001970 if (skip_assign)
1971 result = TRUE;
1972 else
1973 result = ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001974 fi->fi_varcount, flag, NULL) == OK;
Bram Moolenaarbb5d87c2021-03-26 22:15:26 +01001975 vim_free(tv.vval.v_string);
1976 return result;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001977 }
1978
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 item = fi->fi_lw.lw_item;
1980 if (item == NULL)
1981 result = FALSE;
1982 else
1983 {
1984 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar442b29c2021-07-05 22:23:00 +02001985 ++fi->fi_bi;
Bram Moolenaarad2d4962021-07-18 17:08:50 +02001986 if (skip_assign)
1987 result = TRUE;
1988 else
1989 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01001990 fi->fi_varcount, flag, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 }
1992 return result;
1993}
1994
1995/*
1996 * Free the structure used to store info used by ":for".
1997 */
1998 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001999free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002000{
Bram Moolenaar33570922005-01-25 22:26:29 +00002001 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002003 if (fi == NULL)
2004 return;
2005 if (fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002006 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002008 list_unref(fi->fi_list);
2009 }
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002010 else if (fi->fi_blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002011 blob_unref(fi->fi_blob);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01002012 else
2013 vim_free(fi->fi_string);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002014 vim_free(fi);
2015}
2016
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002018set_context_for_expression(
2019 expand_T *xp,
2020 char_u *arg,
2021 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022{
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002023 int has_expr = cmdidx != CMD_let && cmdidx != CMD_var;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002027 if (cmdidx == CMD_let || cmdidx == CMD_var
2028 || cmdidx == CMD_const || cmdidx == CMD_final)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 {
2030 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002031 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002033 // ":let var1 var2 ...": find last space.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002034 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002035 {
2036 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002037 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002038 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 break;
2040 }
2041 return;
2042 }
2043 }
2044 else
2045 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2046 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002047 while ((xp->xp_pattern = vim_strpbrk(arg,
2048 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2049 {
2050 c = *xp->xp_pattern;
2051 if (c == '&')
2052 {
2053 c = xp->xp_pattern[1];
2054 if (c == '&')
2055 {
2056 ++xp->xp_pattern;
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002057 xp->xp_context = has_expr ? EXPAND_EXPRESSION : EXPAND_NOTHING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 }
2059 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002060 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002062 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2063 xp->xp_pattern += 2;
2064
2065 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 }
2067 else if (c == '$')
2068 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002069 // environment variable
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 xp->xp_context = EXPAND_ENV_VARS;
2071 }
2072 else if (c == '=')
2073 {
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002074 has_expr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 xp->xp_context = EXPAND_EXPRESSION;
2076 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002077 else if (c == '#'
2078 && xp->xp_context == EXPAND_EXPRESSION)
2079 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002080 // Autoload function/variable contains '#'.
Bram Moolenaara32095f2016-03-28 19:27:13 +02002081 break;
2082 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002083 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 && xp->xp_context == EXPAND_FUNCTIONS
2085 && vim_strchr(xp->xp_pattern, '(') == NULL)
2086 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002087 // Function name can start with "<SNR>" and contain '#'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 break;
2089 }
Bram Moolenaar30fd8202020-09-26 15:09:30 +02002090 else if (has_expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002092 if (c == '"') // string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 {
2094 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2095 if (c == '\\' && xp->xp_pattern[1] != NUL)
2096 ++xp->xp_pattern;
2097 xp->xp_context = EXPAND_NOTHING;
2098 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002099 else if (c == '\'') // literal string
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002101 // Trick: '' is like stopping and starting a literal string.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2103 /* skip */ ;
2104 xp->xp_context = EXPAND_NOTHING;
2105 }
2106 else if (c == '|')
2107 {
2108 if (xp->xp_pattern[1] == '|')
2109 {
2110 ++xp->xp_pattern;
2111 xp->xp_context = EXPAND_EXPRESSION;
2112 }
2113 else
2114 xp->xp_context = EXPAND_COMMANDS;
2115 }
2116 else
2117 xp->xp_context = EXPAND_EXPRESSION;
2118 }
2119 else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002120 // Doesn't look like something valid, expand as an expression
2121 // anyway.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002122 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 arg = xp->xp_pattern;
2124 if (*arg != NUL)
2125 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2126 /* skip */ ;
2127 }
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002128
2129 // ":exe one two" completes "two"
2130 if ((cmdidx == CMD_execute
2131 || cmdidx == CMD_echo
2132 || cmdidx == CMD_echon
Bram Moolenaar37fef162022-08-29 18:16:32 +01002133 || cmdidx == CMD_echomsg
2134 || cmdidx == CMD_echowindow)
Bram Moolenaar4941b5e2020-12-24 17:15:53 +01002135 && xp->xp_context == EXPAND_EXPRESSION)
2136 {
2137 for (;;)
2138 {
2139 char_u *n = skiptowhite(arg);
2140
2141 if (n == arg || IS_WHITE_OR_NUL(*skipwhite(n)))
2142 break;
2143 arg = skipwhite(n);
2144 }
2145 }
2146
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 xp->xp_pattern = arg;
2148}
2149
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002151 * Return TRUE if "pat" matches "text".
2152 * Does not use 'cpo' and always uses 'magic'.
2153 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02002154 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002155pattern_match(char_u *pat, char_u *text, int ic)
2156{
2157 int matches = FALSE;
2158 char_u *save_cpo;
2159 regmatch_T regmatch;
2160
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002161 // avoid 'l' flag in 'cpoptions'
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002162 save_cpo = p_cpo;
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01002163 p_cpo = empty_option;
Bram Moolenaarea6553b2016-03-27 15:13:38 +02002164 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
2165 if (regmatch.regprog != NULL)
2166 {
2167 regmatch.rm_ic = ic;
2168 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
2169 vim_regfree(regmatch.regprog);
2170 }
2171 p_cpo = save_cpo;
2172 return matches;
2173}
2174
2175/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002176 * Handle a name followed by "(". Both for just "name(arg)" and for
2177 * "expr->name(arg)".
2178 * Returns OK or FAIL.
2179 */
2180 static int
2181eval_func(
2182 char_u **arg, // points to "(", will be advanced
Bram Moolenaare6b53242020-07-01 17:28:33 +02002183 evalarg_T *evalarg,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002184 char_u *name,
2185 int name_len,
2186 typval_T *rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02002187 int flags,
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002188 typval_T *basetv) // "expr" for "expr->name(arg)"
2189{
Bram Moolenaar32e35112020-05-14 22:41:15 +02002190 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002191 char_u *s = name;
2192 int len = name_len;
2193 partial_T *partial;
2194 int ret = OK;
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002195 type_T *type = NULL;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002196 int found_var = FALSE;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002197
2198 if (!evaluate)
2199 check_vars(s, len);
2200
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002201 // If "s" is the name of a variable of type VAR_FUNC
2202 // use its contents.
Bram Moolenaar32b3f822021-01-06 21:59:39 +01002203 s = deref_func_name(s, &len, &partial,
Bram Moolenaar937610b2022-01-19 17:21:29 +00002204 in_vim9script() ? &type : NULL, !evaluate, FALSE, &found_var);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002205
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002206 // Need to make a copy, in case evaluating the arguments makes
2207 // the name invalid.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002208 s = vim_strsave(s);
Bram Moolenaarb3341372021-12-14 18:57:45 +00002209 if (s == NULL || (evaluate && (*s == NUL || (flags & EVAL_CONSTANT))))
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002210 ret = FAIL;
2211 else
2212 {
2213 funcexe_T funcexe;
2214
2215 // Invoke the function.
Bram Moolenaara80faa82020-04-12 19:37:17 +02002216 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00002217 funcexe.fe_firstline = curwin->w_cursor.lnum;
2218 funcexe.fe_lastline = curwin->w_cursor.lnum;
2219 funcexe.fe_evaluate = evaluate;
2220 funcexe.fe_partial = partial;
2221 funcexe.fe_basetv = basetv;
2222 funcexe.fe_check_type = type;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002223 funcexe.fe_found_var = found_var;
Bram Moolenaare6b53242020-07-01 17:28:33 +02002224 ret = get_func_tv(s, len, rettv, arg, evalarg, &funcexe);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002225 }
2226 vim_free(s);
2227
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002228 // If evaluate is FALSE rettv->v_type was not set in
2229 // get_func_tv, but it's needed in handle_subscript() to parse
2230 // what follows. So set it here.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002231 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2232 {
2233 rettv->vval.v_string = NULL;
2234 rettv->v_type = VAR_FUNC;
2235 }
2236
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01002237 // Stop the expression evaluation when immediately
2238 // aborting on error, or when an interrupt occurred or
2239 // an exception was thrown but not caught.
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002240 if (evaluate && aborting())
2241 {
2242 if (ret == OK)
2243 clear_tv(rettv);
2244 ret = FAIL;
2245 }
2246 return ret;
2247}
2248
2249/*
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002250 * After a NL, skip over empty lines and comment-only lines.
2251 */
2252 static char_u *
2253newline_skip_comments(char_u *arg)
2254{
2255 char_u *p = arg + 1;
2256
2257 for (;;)
2258 {
2259 p = skipwhite(p);
2260
2261 if (*p == NUL)
2262 break;
2263 if (vim9_comment_start(p))
2264 {
2265 char_u *nl = vim_strchr(p, NL);
2266
2267 if (nl == NULL)
2268 break;
2269 p = nl;
2270 }
2271 if (*p != NL)
2272 break;
2273 ++p; // skip another NL
2274 }
2275 return p;
2276}
2277
2278/*
Bram Moolenaar93be1642020-10-11 21:34:41 +02002279 * Get the next line source line without advancing. But do skip over comment
2280 * lines.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002281 * Only called for Vim9 script.
Bram Moolenaar93be1642020-10-11 21:34:41 +02002282 */
2283 static char_u *
2284getline_peek_skip_comments(evalarg_T *evalarg)
2285{
2286 for (;;)
2287 {
2288 char_u *next = getline_peek(evalarg->eval_getline,
2289 evalarg->eval_cookie);
2290 char_u *p;
2291
2292 if (next == NULL)
2293 break;
2294 p = skipwhite(next);
2295 if (*p != NUL && !vim9_comment_start(p))
2296 return next;
Bram Moolenaare442d592022-05-05 12:20:28 +01002297 if (eval_next_line(NULL, evalarg) == NULL)
Bram Moolenaar43216612022-03-25 11:16:28 +00002298 break;
Bram Moolenaar93be1642020-10-11 21:34:41 +02002299 }
2300 return NULL;
2301}
2302
2303/*
Bram Moolenaar962d7212020-07-04 14:15:00 +02002304 * If inside Vim9 script, "arg" points to the end of a line (ignoring a #
2305 * comment) and there is a next line, return the next line (skipping blanks)
2306 * and set "getnext".
Bram Moolenaara6aa1642021-04-23 19:32:23 +02002307 * Otherwise return the next non-white at or after "arg" and set "getnext" to
2308 * FALSE.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002309 * "arg" must point somewhere inside a line, not at the start.
2310 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002311 char_u *
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002312eval_next_non_blank(char_u *arg, evalarg_T *evalarg, int *getnext)
2313{
Bram Moolenaar9d489562020-07-30 20:08:50 +02002314 char_u *p = skipwhite(arg);
2315
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002316 *getnext = FALSE;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02002317 if (in_vim9script()
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002318 && evalarg != NULL
Bram Moolenaara13e7ac2022-05-06 21:24:31 +01002319 && (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL
2320 || *p == NL)
Bram Moolenaare442d592022-05-05 12:20:28 +01002321 && (*p == NUL || *p == NL
2322 || (vim9_comment_start(p) && VIM_ISWHITE(p[-1]))))
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002323 {
Bram Moolenaar9d489562020-07-30 20:08:50 +02002324 char_u *next;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002325
Bram Moolenaare442d592022-05-05 12:20:28 +01002326 if (*p == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002327 next = newline_skip_comments(p);
Bram Moolenaare442d592022-05-05 12:20:28 +01002328 else if (evalarg->eval_cookie != NULL)
Bram Moolenaar93be1642020-10-11 21:34:41 +02002329 next = getline_peek_skip_comments(evalarg);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002330 else
Bram Moolenaar9d489562020-07-30 20:08:50 +02002331 next = peek_next_line_from_context(evalarg->eval_cctx);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002332
Bram Moolenaar9d489562020-07-30 20:08:50 +02002333 if (next != NULL)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002334 {
Bram Moolenaar69082912022-09-22 21:35:19 +01002335 *getnext = *p != NL;
Bram Moolenaar9d489562020-07-30 20:08:50 +02002336 return skipwhite(next);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002337 }
2338 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002339 return p;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002340}
2341
2342/*
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002343 * To be called after eval_next_non_blank() sets "getnext" to TRUE.
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002344 * Only called for Vim9 script.
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002345 */
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002346 char_u *
Bram Moolenaare442d592022-05-05 12:20:28 +01002347eval_next_line(char_u *arg, evalarg_T *evalarg)
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002348{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002349 garray_T *gap = &evalarg->eval_ga;
2350 char_u *line;
2351
Bram Moolenaar39be4982022-05-06 21:51:50 +01002352 if (arg != NULL)
2353 {
2354 if (*arg == NL)
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01002355 return newline_skip_comments(arg);
Bram Moolenaar39be4982022-05-06 21:51:50 +01002356 // Truncate before a trailing comment, so that concatenating the lines
2357 // won't turn the rest into a comment.
2358 if (*skipwhite(arg) == '#')
2359 *arg = NUL;
2360 }
Bram Moolenaare442d592022-05-05 12:20:28 +01002361
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002362 if (evalarg->eval_cookie != NULL)
Bram Moolenaar825b5442020-08-20 15:52:21 +02002363 line = evalarg->eval_getline(0, evalarg->eval_cookie, 0,
2364 GETLINE_CONCAT_ALL);
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02002365 else
2366 line = next_line_from_context(evalarg->eval_cctx, TRUE);
Bram Moolenaar43216612022-03-25 11:16:28 +00002367 if (line == NULL)
2368 return NULL;
2369
Bram Moolenaarb7a78f72020-06-28 18:43:40 +02002370 ++evalarg->eval_break_count;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002371 if (gap->ga_itemsize > 0 && ga_grow(gap, 1) == OK)
2372 {
Bram Moolenaar03717bf2021-04-28 20:00:40 +02002373 char_u *p = skipwhite(line);
2374
2375 // Going to concatenate the lines after parsing. For an empty or
2376 // comment line use an empty string.
2377 if (*p == NUL || vim9_comment_start(p))
2378 {
2379 vim_free(line);
2380 line = vim_strsave((char_u *)"");
2381 }
2382
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002383 ((char_u **)gap->ga_data)[gap->ga_len] = line;
2384 ++gap->ga_len;
2385 }
Bram Moolenaar8e2730a2020-07-08 22:01:49 +02002386 else if (evalarg->eval_cookie != NULL)
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002387 {
Bram Moolenaar91c7cbf2022-08-18 13:28:31 +01002388 free_eval_tofree_later(evalarg);
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002389 evalarg->eval_tofree = line;
2390 }
Bram Moolenaarc6ba2f92021-07-18 13:42:29 +02002391
2392 // Advanced to the next line, "arg" no longer points into the previous
2393 // line.
Bram Moolenaar844fb642021-10-23 13:32:30 +01002394 evalarg->eval_using_cmdline = FALSE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02002395 return skipwhite(line);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002396}
2397
Bram Moolenaare6b53242020-07-01 17:28:33 +02002398/*
2399 * Call eval_next_non_blank() and get the next line if needed.
2400 */
Bram Moolenaar9215f012020-06-27 21:18:00 +02002401 char_u *
2402skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg)
2403{
2404 int getnext;
Bram Moolenaarce7eada2021-12-15 15:41:44 +00002405 char_u *p = skipwhite_and_nl(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002406
Bram Moolenaar962d7212020-07-04 14:15:00 +02002407 if (evalarg == NULL)
2408 return skipwhite(arg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002409 eval_next_non_blank(p, evalarg, &getnext);
2410 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002411 return eval_next_line(arg, evalarg);
Bram Moolenaar9215f012020-06-27 21:18:00 +02002412 return p;
2413}
2414
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2419 */
2420
2421/*
2422 * Handle zero level expression.
2423 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002425 * Note: "rettv.v_lock" is not set.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002426 * "evalarg" can be NULL, EVALARG_EVALUATE or a pointer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 * Return OK or FAIL.
2428 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002430eval0(
2431 char_u *arg,
2432 typval_T *rettv,
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002433 exarg_T *eap,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002434 evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435{
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002436 return eval0_retarg(arg, rettv, eap, evalarg, NULL);
2437}
2438
2439/*
2440 * Like eval0() but when "retarg" is not NULL store the pointer to after the
2441 * expression and don't check what comes after the expression.
2442 */
2443 int
2444eval0_retarg(
2445 char_u *arg,
2446 typval_T *rettv,
2447 exarg_T *eap,
2448 evalarg_T *evalarg,
2449 char_u **retarg)
2450{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 int ret;
2452 char_u *p;
Bram Moolenaar02929a32021-12-17 14:46:12 +00002453 char_u *expr_end;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002454 int did_emsg_before = did_emsg;
2455 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002456 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002457 int check_for_end = retarg == NULL;
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002458 int end_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 p = skipwhite(arg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002461 ret = eval1(&p, rettv, evalarg);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002462
Bram Moolenaar79481362022-06-27 20:15:10 +01002463 if (ret != FAIL)
2464 {
2465 expr_end = p;
2466 p = skipwhite(p);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002467
Bram Moolenaar79481362022-06-27 20:15:10 +01002468 // In Vim9 script a command block is not split at NL characters for
2469 // commands using an expression argument. Skip over a '#' comment to
2470 // check for a following NL. Require white space before the '#'.
2471 if (in_vim9script() && p > expr_end && retarg == NULL)
2472 while (*p == '#')
2473 {
2474 char_u *nl = vim_strchr(p, NL);
Bram Moolenaar02929a32021-12-17 14:46:12 +00002475
Bram Moolenaar79481362022-06-27 20:15:10 +01002476 if (nl == NULL)
2477 break;
2478 p = skipwhite(nl + 1);
2479 if (eap != NULL && *p != NUL)
2480 eap->nextcmd = p;
2481 check_for_end = FALSE;
2482 }
2483
2484 if (check_for_end)
2485 end_error = !ends_excmd2(arg, p);
2486 }
2487
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002488 if (ret == FAIL || end_error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 /*
2493 * Report the invalid expression unless the expression evaluation has
2494 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002495 * exception, or we already gave a more specific error.
2496 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 */
Bram Moolenaar32e35112020-05-14 22:41:15 +02002498 if (!aborting()
2499 && did_emsg == did_emsg_before
2500 && called_emsg == called_emsg_before
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01002501 && (flags & EVAL_CONSTANT) == 0
2502 && (!in_vim9script() || !vim9_bad_comment(p)))
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002503 {
2504 if (end_error)
Bram Moolenaar74409f62022-01-01 15:58:22 +00002505 semsg(_(e_trailing_characters_str), p);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002506 else
Bram Moolenaar108010a2021-06-27 22:03:33 +02002507 semsg(_(e_invalid_expression_str), arg);
Bram Moolenaarfae55a92021-06-17 22:08:30 +02002508 }
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002509
2510 // Some of the expression may not have been consumed. Do not check for
Bram Moolenaar8143a532020-12-13 20:26:29 +01002511 // a next command to avoid more errors, unless "|" is following, which
2512 // could only be a command separator.
Bram Moolenaar79481362022-06-27 20:15:10 +01002513 if (eap != NULL && p != NULL
2514 && skipwhite(p)[0] == '|' && skipwhite(p)[1] != '|')
Bram Moolenaar8143a532020-12-13 20:26:29 +01002515 eap->nextcmd = check_nextcmd(p);
Bram Moolenaard0fe6202020-12-05 17:11:12 +01002516 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 }
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002518
Bram Moolenaard5f400c2022-01-06 21:10:28 +00002519 if (retarg != NULL)
2520 *retarg = p;
2521 else if (check_for_end && eap != NULL)
Bram Moolenaar63b91732021-08-05 20:40:03 +02002522 set_nextcmd(eap, p);
Bram Moolenaarb171fb12020-06-24 20:34:03 +02002523
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 return ret;
2525}
2526
2527/*
2528 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002529 * expr2 ? expr1 : expr1
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002530 * expr2 ?? expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 *
2532 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002533 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002535 * Note: "rettv.v_lock" is not set.
2536 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 * Return OK or FAIL.
2538 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002539 int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002540eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541{
Bram Moolenaar793648f2020-06-26 21:28:25 +02002542 char_u *p;
2543 int getnext;
2544
Bram Moolenaar4a091b92020-09-12 22:10:00 +02002545 CLEAR_POINTER(rettv);
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 /*
2548 * Get the first variable.
2549 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002550 if (eval2(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 return FAIL;
2552
Bram Moolenaar793648f2020-06-26 21:28:25 +02002553 p = eval_next_non_blank(*arg, evalarg, &getnext);
2554 if (*p == '?')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002556 int op_falsy = p[1] == '?';
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002557 int result;
2558 typval_T var2;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002559 evalarg_T *evalarg_used = evalarg;
2560 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002561 int orig_flags;
Bram Moolenaar7acde512020-06-24 23:02:40 +02002562 int evaluate;
Bram Moolenaar13106602020-10-04 16:06:05 +02002563 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002564
2565 if (evalarg == NULL)
2566 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002567 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002568 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002569 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002570 orig_flags = evalarg_used->eval_flags;
2571 evaluate = evalarg_used->eval_flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002572
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002573 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002574 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002575 else
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002576 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002577 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002578 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02002579 error_white_both(p, op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002580 clear_tv(rettv);
2581 return FAIL;
2582 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002583 *arg = p;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002584 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002585
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 result = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002587 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002589 int error = FALSE;
2590
Bram Moolenaar13106602020-10-04 16:06:05 +02002591 if (op_falsy)
Bram Moolenaar56acb092020-08-16 14:48:19 +02002592 result = tv2bool(rettv);
Bram Moolenaar13106602020-10-04 16:06:05 +02002593 else if (vim9script)
2594 result = tv_get_bool_chk(rettv, &error);
Bram Moolenaar56acb092020-08-16 14:48:19 +02002595 else if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 result = TRUE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002597 if (error || !op_falsy || !result)
2598 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002599 if (error)
2600 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602
2603 /*
Bram Moolenaar32e35112020-05-14 22:41:15 +02002604 * Get the second variable. Recursive!
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605 */
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002606 if (op_falsy)
2607 ++*arg;
Bram Moolenaar13106602020-10-04 16:06:05 +02002608 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002609 {
mityu4ac198c2021-05-28 17:52:40 +02002610 error_white_both(*arg - (op_falsy ? 1 : 0), op_falsy ? 2 : 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002611 clear_tv(rettv);
2612 return FAIL;
2613 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002614 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002615 evalarg_used->eval_flags = (op_falsy ? !result : result)
2616 ? orig_flags : orig_flags & ~EVAL_EVALUATE;
2617 if (eval1(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar69e445522020-08-22 22:37:20 +02002618 {
2619 evalarg_used->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 return FAIL;
Bram Moolenaar69e445522020-08-22 22:37:20 +02002621 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002622 if (!op_falsy || !result)
2623 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002625 if (!op_falsy)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 {
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002627 /*
2628 * Check for the ":".
2629 */
2630 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
2631 if (*p != ':')
2632 {
Bram Moolenaare1242042021-12-16 20:56:57 +00002633 emsg(_(e_missing_colon_after_questionmark));
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002634 if (evaluate && result)
2635 clear_tv(rettv);
2636 evalarg_used->eval_flags = orig_flags;
2637 return FAIL;
2638 }
2639 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002640 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002641 else
2642 {
Bram Moolenaar13106602020-10-04 16:06:05 +02002643 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002644 {
2645 error_white_both(p, 1);
2646 clear_tv(rettv);
2647 evalarg_used->eval_flags = orig_flags;
2648 return FAIL;
2649 }
2650 *arg = p;
2651 }
2652
2653 /*
2654 * Get the third variable. Recursive!
2655 */
Bram Moolenaar13106602020-10-04 16:06:05 +02002656 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[1]))
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002657 {
mityu4ac198c2021-05-28 17:52:40 +02002658 error_white_both(*arg, 1);
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002659 clear_tv(rettv);
Bram Moolenaar69e445522020-08-22 22:37:20 +02002660 evalarg_used->eval_flags = orig_flags;
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002661 return FAIL;
2662 }
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002663 *arg = skipwhite_and_linebreak(*arg + 1, evalarg_used);
2664 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002665 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar92f26c22020-10-03 20:17:30 +02002666 if (eval1(arg, &var2, evalarg_used) == FAIL)
2667 {
2668 if (evaluate && result)
2669 clear_tv(rettv);
2670 evalarg_used->eval_flags = orig_flags;
2671 return FAIL;
2672 }
2673 if (evaluate && !result)
2674 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002676
2677 if (evalarg == NULL)
2678 clear_evalarg(&local_evalarg, NULL);
2679 else
2680 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 }
2682
2683 return OK;
2684}
2685
2686/*
2687 * Handle first level expression:
2688 * expr2 || expr2 || expr2 logical OR
2689 *
2690 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002691 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 *
2693 * Return OK or FAIL.
2694 */
2695 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002696eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002698 char_u *p;
2699 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002702 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002704 if (eval3(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 return FAIL;
2706
2707 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002708 * Handle the "||" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002710 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002711 if (p[0] == '|' && p[1] == '|')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002713 evalarg_T *evalarg_used = evalarg;
2714 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002715 int evaluate;
2716 int orig_flags;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002717 long result = FALSE;
2718 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002719 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002720 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002721
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002722 if (evalarg == NULL)
2723 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002724 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002725 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002726 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002727 orig_flags = evalarg_used->eval_flags;
2728 evaluate = orig_flags & EVAL_EVALUATE;
2729 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002730 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002731 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002732 result = tv_get_bool_chk(rettv, &error);
2733 else if (tv_get_number_chk(rettv, &error) != 0)
2734 result = TRUE;
2735 clear_tv(rettv);
2736 if (error)
2737 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739
2740 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002741 * Repeat until there is no following "||".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002743 while (p[0] == '|' && p[1] == '|')
2744 {
2745 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002746 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002747 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002748 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00002749 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002750 {
2751 error_white_both(p, 2);
2752 clear_tv(rettv);
2753 return FAIL;
2754 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002755 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002756 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002757
2758 /*
2759 * Get the second variable.
2760 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002761 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002762 {
mityu4ac198c2021-05-28 17:52:40 +02002763 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002764 clear_tv(rettv);
2765 return FAIL;
2766 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002767 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2768 evalarg_used->eval_flags = !result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002769 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002770 if (eval3(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002771 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002772
2773 /*
2774 * Compute the result.
2775 */
2776 if (evaluate && !result)
2777 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002778 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002779 result = tv_get_bool_chk(&var2, &error);
2780 else if (tv_get_number_chk(&var2, &error) != 0)
2781 result = TRUE;
2782 clear_tv(&var2);
2783 if (error)
2784 return FAIL;
2785 }
2786 if (evaluate)
2787 {
2788 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002789 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002790 rettv->v_type = VAR_BOOL;
2791 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002792 }
2793 else
2794 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002795 rettv->v_type = VAR_NUMBER;
2796 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002797 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002798 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002799
2800 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002802
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002803 if (evalarg == NULL)
2804 clear_evalarg(&local_evalarg, NULL);
2805 else
2806 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 }
2808
2809 return OK;
2810}
2811
2812/*
2813 * Handle second level expression:
2814 * expr3 && expr3 && expr3 logical AND
2815 *
2816 * "arg" must point to the first non-white of the expression.
Bram Moolenaarfdac71c2020-08-05 12:44:41 +02002817 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 *
2819 * Return OK or FAIL.
2820 */
2821 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002822eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823{
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002824 char_u *p;
2825 int getnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002826
2827 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002828 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002830 if (eval4(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 return FAIL;
2832
2833 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002834 * Handle the "&&" operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835 */
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002836 p = eval_next_non_blank(*arg, evalarg, &getnext);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002837 if (p[0] == '&' && p[1] == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 {
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002839 evalarg_T *evalarg_used = evalarg;
2840 evalarg_T local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002841 int orig_flags;
2842 int evaluate;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002843 long result = TRUE;
2844 typval_T var2;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002845 int error = FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002846 int vim9script = in_vim9script();
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002847
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002848 if (evalarg == NULL)
2849 {
Bram Moolenaar844fb642021-10-23 13:32:30 +01002850 init_evalarg(&local_evalarg);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002851 evalarg_used = &local_evalarg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002852 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002853 orig_flags = evalarg_used->eval_flags;
2854 evaluate = orig_flags & EVAL_EVALUATE;
2855 if (evaluate)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002856 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002857 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002858 result = tv_get_bool_chk(rettv, &error);
2859 else if (tv_get_number_chk(rettv, &error) == 0)
2860 result = FALSE;
2861 clear_tv(rettv);
2862 if (error)
2863 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 }
2865
2866 /*
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002867 * Repeat until there is no following "&&".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 */
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002869 while (p[0] == '&' && p[1] == '&')
2870 {
2871 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01002872 *arg = eval_next_line(*arg, evalarg_used);
Bram Moolenaar9d489562020-07-30 20:08:50 +02002873 else
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002874 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002875 if (evaluate && vim9script && !VIM_ISWHITE(p[-1]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002876 {
2877 error_white_both(p, 2);
2878 clear_tv(rettv);
2879 return FAIL;
2880 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02002881 *arg = p;
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002882 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002883
2884 /*
2885 * Get the second variable.
2886 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00002887 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[2]))
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002888 {
mityu4ac198c2021-05-28 17:52:40 +02002889 error_white_both(*arg, 2);
Bram Moolenaar3c1c9fd2020-08-05 12:32:38 +02002890 clear_tv(rettv);
2891 return FAIL;
2892 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002893 *arg = skipwhite_and_linebreak(*arg + 2, evalarg_used);
2894 evalarg_used->eval_flags = result ? orig_flags
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002895 : orig_flags & ~EVAL_EVALUATE;
Bram Moolenaarc1ec0422020-09-09 22:27:58 +02002896 CLEAR_FIELD(var2);
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002897 if (eval4(arg, &var2, evalarg_used) == FAIL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002898 return FAIL;
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002899
2900 /*
2901 * Compute the result.
2902 */
2903 if (evaluate && result)
2904 {
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002905 if (vim9script)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002906 result = tv_get_bool_chk(&var2, &error);
2907 else if (tv_get_number_chk(&var2, &error) == 0)
2908 result = FALSE;
2909 clear_tv(&var2);
2910 if (error)
2911 return FAIL;
2912 }
2913 if (evaluate)
2914 {
2915 if (vim9script)
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002916 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002917 rettv->v_type = VAR_BOOL;
2918 rettv->vval.v_number = result ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002919 }
2920 else
2921 {
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002922 rettv->v_type = VAR_NUMBER;
2923 rettv->vval.v_number = result;
Bram Moolenaar8c34ea52020-07-13 22:29:02 +02002924 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002925 }
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002926
2927 p = eval_next_non_blank(*arg, evalarg_used, &getnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 }
Bram Moolenaarbe7ee482020-06-26 21:38:51 +02002929
Bram Moolenaar8af81d62020-07-12 16:32:19 +02002930 if (evalarg == NULL)
2931 clear_evalarg(&local_evalarg, NULL);
2932 else
2933 evalarg->eval_flags = orig_flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 }
2935
2936 return OK;
2937}
2938
2939/*
2940 * Handle third level expression:
2941 * var1 == var2
2942 * var1 =~ var2
2943 * var1 != var2
2944 * var1 !~ var2
2945 * var1 > var2
2946 * var1 >= var2
2947 * var1 < var2
2948 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002949 * var1 is var2
2950 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951 *
2952 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002953 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 *
2955 * Return OK or FAIL.
2956 */
2957 static int
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002958eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 char_u *p;
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002961 int getnext;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002962 exprtype_T type = EXPR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 int len = 2;
Bram Moolenaar696ba232020-07-29 21:20:41 +02002964 int type_is = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965
2966 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002967 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 */
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02002969 if (eval5(arg, rettv, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 return FAIL;
2971
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002972 p = eval_next_non_blank(*arg, evalarg, &getnext);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01002973
Bram Moolenaar696ba232020-07-29 21:20:41 +02002974 type = get_compare_type(p, &len, &type_is);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002977 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 */
Bram Moolenaar87396072019-12-31 22:36:18 +01002979 if (type != EXPR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980 {
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002981 typval_T var2;
2982 int ic;
2983 int vim9script = in_vim9script();
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002984 int evaluate = evalarg == NULL
2985 ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar1b1df952022-03-10 20:01:50 +00002986 long comp_lnum = SOURCING_LNUM;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02002987
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002988 if (getnext)
mityu4ac198c2021-05-28 17:52:40 +02002989 {
Bram Moolenaare442d592022-05-05 12:20:28 +01002990 *arg = eval_next_line(*arg, evalarg);
mityu4ac198c2021-05-28 17:52:40 +02002991 p = *arg;
2992 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002993 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
2994 {
mityu4ac198c2021-05-28 17:52:40 +02002995 error_white_both(*arg, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02002996 clear_tv(rettv);
2997 return FAIL;
2998 }
Bram Moolenaare6536aa2020-06-26 22:00:38 +02002999
Bram Moolenaar696ba232020-07-29 21:20:41 +02003000 if (vim9script && type_is && (p[len] == '?' || p[len] == '#'))
3001 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003002 semsg(_(e_invalid_expression_str), p);
Bram Moolenaar696ba232020-07-29 21:20:41 +02003003 clear_tv(rettv);
3004 return FAIL;
3005 }
3006
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003007 // extra question mark appended: ignore case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 if (p[len] == '?')
3009 {
3010 ic = TRUE;
3011 ++len;
3012 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003013 // extra '#' appended: match case
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 else if (p[len] == '#')
3015 {
3016 ic = FALSE;
3017 ++len;
3018 }
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003019 // nothing appended: use 'ignorecase' if not in Vim script
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 else
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003021 ic = vim9script ? FALSE : p_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022
3023 /*
3024 * Get the second variable.
3025 */
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003026 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[len]))
3027 {
Bram Moolenaar90193e62021-04-04 20:49:50 +02003028 error_white_both(p, len);
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003029 clear_tv(rettv);
3030 return FAIL;
3031 }
Bram Moolenaar9215f012020-06-27 21:18:00 +02003032 *arg = skipwhite_and_linebreak(p + len, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003033 if (eval5(arg, &var2, evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003035 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 return FAIL;
3037 }
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003038 if (evaluate)
Bram Moolenaar31988702018-02-13 12:57:42 +01003039 {
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003040 int ret;
Bram Moolenaar31988702018-02-13 12:57:42 +01003041
Bram Moolenaar1b1df952022-03-10 20:01:50 +00003042 // use the line of the comparison for messages
3043 SOURCING_LNUM = comp_lnum;
Bram Moolenaarc71f36a2020-07-21 21:31:00 +02003044 if (vim9script && check_compare_types(type, rettv, &var2) == FAIL)
Bram Moolenaar543e6f32020-07-10 22:45:38 +02003045 {
3046 ret = FAIL;
3047 clear_tv(rettv);
3048 }
3049 else
3050 ret = typval_compare(rettv, &var2, type, ic);
Bram Moolenaar31988702018-02-13 12:57:42 +01003051 clear_tv(&var2);
3052 return ret;
3053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 }
3055
3056 return OK;
3057}
3058
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003059/*
3060 * Make a copy of blob "tv1" and append blob "tv2".
3061 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 void
3063eval_addblob(typval_T *tv1, typval_T *tv2)
3064{
3065 blob_T *b1 = tv1->vval.v_blob;
3066 blob_T *b2 = tv2->vval.v_blob;
3067 blob_T *b = blob_alloc();
3068 int i;
3069
3070 if (b != NULL)
3071 {
3072 for (i = 0; i < blob_len(b1); i++)
3073 ga_append(&b->bv_ga, blob_get(b1, i));
3074 for (i = 0; i < blob_len(b2); i++)
3075 ga_append(&b->bv_ga, blob_get(b2, i));
3076
3077 clear_tv(tv1);
3078 rettv_blob_set(tv1, b);
3079 }
3080}
3081
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003082/*
3083 * Make a copy of list "tv1" and append list "tv2".
3084 */
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 int
3086eval_addlist(typval_T *tv1, typval_T *tv2)
3087{
3088 typval_T var3;
3089
3090 // concatenate Lists
3091 if (list_concat(tv1->vval.v_list, tv2->vval.v_list, &var3) == FAIL)
3092 {
3093 clear_tv(tv1);
3094 clear_tv(tv2);
3095 return FAIL;
3096 }
3097 clear_tv(tv1);
3098 *tv1 = var3;
3099 return OK;
3100}
3101
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003103 * Handle the bitwise left/right shift operator expression:
3104 * var1 << var2
3105 * var1 >> var2
3106 *
3107 * "arg" must point to the first non-white of the expression.
3108 * "arg" is advanced to just after the recognized expression.
3109 *
3110 * Return OK or FAIL.
3111 */
3112 static int
3113eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
3114{
3115 /*
3116 * Get the first expression.
3117 */
3118 if (eval6(arg, rettv, evalarg) == FAIL)
3119 return FAIL;
3120
3121 /*
3122 * Repeat computing, until no '<<' or '>>' is following.
3123 */
3124 for (;;)
3125 {
3126 char_u *p;
3127 int getnext;
3128 exprtype_T type;
3129 int evaluate;
3130 typval_T var2;
3131 int vim9script;
3132
3133 p = eval_next_non_blank(*arg, evalarg, &getnext);
3134 if (p[0] == '<' && p[1] == '<')
3135 type = EXPR_LSHIFT;
3136 else if (p[0] == '>' && p[1] == '>')
3137 type = EXPR_RSHIFT;
3138 else
3139 return OK;
3140
3141 // Handle a bitwise left or right shift operator
3142 if (rettv->v_type != VAR_NUMBER)
3143 {
3144 // left operand should be a number
3145 emsg(_(e_bitshift_ops_must_be_number));
3146 clear_tv(rettv);
3147 return FAIL;
3148 }
3149
3150 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
3151 vim9script = in_vim9script();
3152 if (getnext)
3153 {
3154 *arg = eval_next_line(*arg, evalarg);
3155 p = *arg;
3156 }
3157 else if (evaluate && vim9script && !VIM_ISWHITE(**arg))
3158 {
3159 error_white_both(*arg, 2);
3160 clear_tv(rettv);
3161 return FAIL;
3162 }
3163
3164 /*
3165 * Get the second variable.
3166 */
3167 if (evaluate && vim9script && !IS_WHITE_OR_NUL(p[2]))
3168 {
3169 error_white_both(p, 2);
3170 clear_tv(rettv);
3171 return FAIL;
3172 }
3173 *arg = skipwhite_and_linebreak(p + 2, evalarg);
3174 if (eval6(arg, &var2, evalarg) == FAIL)
3175 {
3176 clear_tv(rettv);
3177 return FAIL;
3178 }
3179
3180 if (var2.v_type != VAR_NUMBER || var2.vval.v_number < 0)
3181 {
3182 // right operand should be a positive number
3183 if (var2.v_type != VAR_NUMBER)
3184 emsg(_(e_bitshift_ops_must_be_number));
3185 else
3186 emsg(_(e_bitshift_ops_must_be_postive));
3187 clear_tv(rettv);
3188 clear_tv(&var2);
3189 return FAIL;
3190 }
3191
3192 if (evaluate)
3193 {
3194 if (var2.vval.v_number > MAX_LSHIFT_BITS)
3195 // shifting more bits than we have always results in zero
3196 rettv->vval.v_number = 0;
3197 else if (type == EXPR_LSHIFT)
3198 rettv->vval.v_number =
Bram Moolenaar68e64d22022-05-22 22:07:52 +01003199 (uvarnumber_T)rettv->vval.v_number << var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003200 else
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003201 rettv->vval.v_number =
Bram Moolenaar338bf582022-05-22 20:16:32 +01003202 (uvarnumber_T)rettv->vval.v_number >> var2.vval.v_number;
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003203 }
3204
3205 clear_tv(&var2);
3206 }
3207
3208 return OK;
3209}
3210
3211/*
3212 * Handle fifth level expression:
Bram Moolenaar54969f42022-02-07 13:56:44 +00003213 * + number addition, concatenation of list or blob
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003215 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02003216 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 *
3218 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003219 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 *
3221 * Return OK or FAIL.
3222 */
3223 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003224eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003227 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003229 if (eval7(arg, rettv, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 return FAIL;
3231
3232 /*
3233 * Repeat computing, until no '+', '-' or '.' is following.
3234 */
3235 for (;;)
3236 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003237 int evaluate;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003238 int getnext;
3239 char_u *p;
3240 int op;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003241 int oplen;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003242 int concat;
3243 typval_T var2;
Bram Moolenaar2e086612020-08-25 22:37:48 +02003244 int vim9script = in_vim9script();
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003245
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003246 // "." is only string concatenation when scriptversion is 1
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003247 // "+=", "-=" and "..=" are assignments
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003248 // "++" and "--" on the next line are a separate command.
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003249 p = eval_next_non_blank(*arg, evalarg, &getnext);
3250 op = *p;
Bram Moolenaardd9de502021-08-15 13:49:42 +02003251 concat = op == '.' && (*(p + 1) == '.' || in_old_script(2));
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003252 if ((op != '+' && op != '-' && !concat) || p[1] == '='
3253 || (p[1] == '.' && p[2] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 break;
Bram Moolenaarbdc0f1c2021-04-24 19:08:24 +02003255 if (getnext && (op == '+' || op == '-') && p[0] == p[1])
3256 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003257
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003258 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003259 oplen = (concat && p[1] == '.') ? 2 : 1;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003260 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003261 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003262 else
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003263 {
Bram Moolenaar2e086612020-08-25 22:37:48 +02003264 if (evaluate && vim9script && !VIM_ISWHITE(**arg))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003265 {
mityu4ac198c2021-05-28 17:52:40 +02003266 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003267 clear_tv(rettv);
3268 return FAIL;
3269 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003270 *arg = p;
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003271 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003272 if ((op != '+' || (rettv->v_type != VAR_LIST
3273 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003274 && (op == '.' || rettv->v_type != VAR_FLOAT)
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003275 && evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003276 {
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003277 int error = FALSE;
3278
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003279 // For "list + ...", an illegal use of the first operand as
3280 // a number cannot be determined before evaluating the 2nd
3281 // operand: if this is also a list, all is ok.
3282 // For "something . ...", "something - ..." or "non-list + ...",
3283 // we know that the first operand needs to be a string or number
3284 // without evaluating the 2nd operand. So check before to avoid
3285 // side effects after an error.
Bram Moolenaar081db1a2020-10-22 20:09:43 +02003286 if (op != '.')
3287 tv_get_number_chk(rettv, &error);
3288 if ((op == '.' && tv_get_string_chk(rettv) == NULL) || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003289 {
3290 clear_tv(rettv);
3291 return FAIL;
3292 }
3293 }
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 /*
3296 * Get the second variable.
3297 */
Bram Moolenaar2e086612020-08-25 22:37:48 +02003298 if (evaluate && vim9script && !IS_WHITE_OR_NUL((*arg)[oplen]))
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003299 {
mityu89dcb4d2021-05-28 13:50:17 +02003300 error_white_both(*arg, oplen);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02003301 clear_tv(rettv);
3302 return FAIL;
3303 }
3304 *arg = skipwhite_and_linebreak(*arg + oplen, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003305 if (eval7(arg, &var2, evalarg, !vim9script && op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003307 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 return FAIL;
3309 }
3310
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003311 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 {
3313 /*
3314 * Compute the result.
3315 */
3316 if (op == '.')
3317 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003318 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3319 char_u *s1 = tv_get_string_buf(rettv, buf1);
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003320 char_u *s2 = NULL;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003321
Bram Moolenaar2e086612020-08-25 22:37:48 +02003322 if (vim9script && (var2.v_type == VAR_VOID
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003323 || var2.v_type == VAR_CHANNEL
3324 || var2.v_type == VAR_JOB))
Bram Moolenaar68db9962021-05-09 23:19:22 +02003325 semsg(_(e_using_invalid_value_as_string_str),
3326 vartype_name(var2.v_type));
Bram Moolenaar2e086612020-08-25 22:37:48 +02003327 else if (vim9script && var2.v_type == VAR_FLOAT)
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003328 {
3329 vim_snprintf((char *)buf2, NUMBUFLEN, "%g",
3330 var2.vval.v_float);
3331 s2 = buf2;
3332 }
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003333 else
3334 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003335 if (s2 == NULL) // type error ?
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003336 {
3337 clear_tv(rettv);
3338 clear_tv(&var2);
3339 return FAIL;
3340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003341 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003342 clear_tv(rettv);
3343 rettv->v_type = VAR_STRING;
3344 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003346 else if (op == '+' && rettv->v_type == VAR_BLOB
3347 && var2.v_type == VAR_BLOB)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 eval_addblob(rettv, &var2);
Bram Moolenaare9a41262005-01-15 22:18:47 +00003349 else if (op == '+' && rettv->v_type == VAR_LIST
3350 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003351 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003352 if (eval_addlist(rettv, &var2) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003353 return FAIL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 else
3356 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003357 int error = FALSE;
3358 varnumber_T n1, n2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003359 float_T f1 = 0, f2 = 0;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003360
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003361 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003362 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003363 f1 = rettv->vval.v_float;
3364 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003365 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003366 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003367 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003368 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003369 if (error)
3370 {
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003371 // This can only happen for "list + non-list" or
3372 // "blob + non-blob". For "non-list + ..." or
3373 // "something - ...", we returned before evaluating the
3374 // 2nd operand.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003375 clear_tv(rettv);
Bram Moolenaarf2dd9cb2021-04-04 21:55:23 +02003376 clear_tv(&var2);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003377 return FAIL;
3378 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003379 if (var2.v_type == VAR_FLOAT)
3380 f1 = n1;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003381 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003382 if (var2.v_type == VAR_FLOAT)
3383 {
3384 f2 = var2.vval.v_float;
3385 n2 = 0;
3386 }
3387 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003388 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003389 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003390 if (error)
3391 {
3392 clear_tv(rettv);
3393 clear_tv(&var2);
3394 return FAIL;
3395 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003396 if (rettv->v_type == VAR_FLOAT)
3397 f2 = n2;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003399 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003400
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003401 // If there is a float on either side the result is a float.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003402 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3403 {
3404 if (op == '+')
3405 f1 = f1 + f2;
3406 else
3407 f1 = f1 - f2;
3408 rettv->v_type = VAR_FLOAT;
3409 rettv->vval.v_float = f1;
3410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003412 {
3413 if (op == '+')
3414 n1 = n1 + n2;
3415 else
3416 n1 = n1 - n2;
3417 rettv->v_type = VAR_NUMBER;
3418 rettv->vval.v_number = n1;
3419 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003421 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423 }
3424 return OK;
3425}
3426
3427/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003428 * Handle sixth level expression:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 * * number multiplication
3430 * / number division
3431 * % number modulo
3432 *
3433 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003434 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 *
3436 * Return OK or FAIL.
3437 */
3438 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003439eval7(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003440 char_u **arg,
3441 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003442 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003443 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444{
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003445 int use_float = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 /*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003448 * Get the first expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 */
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003450 if (eval8(arg, rettv, evalarg, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 return FAIL;
3452
3453 /*
3454 * Repeat computing, until no '*', '/' or '%' is following.
3455 */
3456 for (;;)
3457 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003458 int evaluate;
3459 int getnext;
3460 typval_T var2;
Bram Moolenaar9d489562020-07-30 20:08:50 +02003461 char_u *p;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003462 int op;
3463 varnumber_T n1, n2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003464 float_T f1, f2;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003465 int error;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003466
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003467 // "*=", "/=" and "%=" are assignments
Bram Moolenaar9d489562020-07-30 20:08:50 +02003468 p = eval_next_non_blank(*arg, evalarg, &getnext);
3469 op = *p;
Bram Moolenaarf76ec1e2021-03-03 17:58:16 +01003470 if ((op != '*' && op != '/' && op != '%') || p[1] == '=')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 break;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003472
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003473 evaluate = evalarg == NULL ? 0 : (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003474 if (getnext)
Bram Moolenaare442d592022-05-05 12:20:28 +01003475 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaar9d489562020-07-30 20:08:50 +02003476 else
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003477 {
3478 if (evaluate && in_vim9script() && !VIM_ISWHITE(**arg))
3479 {
mityu4ac198c2021-05-28 17:52:40 +02003480 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003481 clear_tv(rettv);
3482 return FAIL;
3483 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02003484 *arg = p;
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003487 f1 = 0;
3488 f2 = 0;
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02003489 error = FALSE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003490 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003492 if (rettv->v_type == VAR_FLOAT)
3493 {
3494 f1 = rettv->vval.v_float;
3495 use_float = TRUE;
3496 n1 = 0;
3497 }
3498 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003499 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003500 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003501 if (error)
3502 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 }
3504 else
3505 n1 = 0;
3506
3507 /*
3508 * Get the second variable.
3509 */
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003510 if (evaluate && in_vim9script() && !IS_WHITE_OR_NUL((*arg)[1]))
3511 {
Bram Moolenaara9749532021-03-06 18:18:19 +01003512 error_white_both(*arg, 1);
Bram Moolenaarb4caa162020-08-05 11:36:52 +02003513 clear_tv(rettv);
3514 return FAIL;
3515 }
3516 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003517 if (eval8(arg, &var2, evalarg, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 return FAIL;
3519
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003520 if (evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003522 if (var2.v_type == VAR_FLOAT)
3523 {
3524 if (!use_float)
3525 {
3526 f1 = n1;
3527 use_float = TRUE;
3528 }
3529 f2 = var2.vval.v_float;
3530 n2 = 0;
3531 }
3532 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003533 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003534 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003535 clear_tv(&var2);
3536 if (error)
3537 return FAIL;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003538 if (use_float)
3539 f2 = n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541
3542 /*
3543 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003544 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003546 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003548 if (op == '*')
3549 f1 = f1 * f2;
3550 else if (op == '/')
3551 {
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003552#ifdef VMS
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003553 // VMS crashes on divide by zero, work around it
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003554 if (f2 == 0.0)
3555 {
3556 if (f1 == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003557 f1 = -1 * __F_FLT_MAX - 1L; // similar to NaN
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003558 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003559 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003560 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02003561 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02003562 }
3563 else
3564 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003565#else
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01003566 // We rely on the floating point library to handle divide
3567 // by zero to result in "inf" and not a crash.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003568 f1 = f1 / f2;
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003569#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003570 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003572 {
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00003573 emsg(_(e_cannot_use_percent_with_float));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003574 return FAIL;
3575 }
3576 rettv->v_type = VAR_FLOAT;
3577 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 }
3579 else
3580 {
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003581 int failed = FALSE;
3582
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003583 if (op == '*')
3584 n1 = n1 * n2;
3585 else if (op == '/')
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003586 n1 = num_divide(n1, n2, &failed);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 else
Bram Moolenaarc5f59fa2021-01-21 12:34:14 +01003588 n1 = num_modulus(n1, n2, &failed);
3589 if (failed)
3590 return FAIL;
Bram Moolenaare21c1582019-03-02 11:57:09 +01003591
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003592 rettv->v_type = VAR_NUMBER;
3593 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 }
3596 }
3597
3598 return OK;
3599}
3600
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003601/*
3602 * Handle a type cast before a base level expression.
3603 * "arg" must point to the first non-white of the expression.
3604 * "arg" is advanced to just after the recognized expression.
3605 * Return OK or FAIL.
3606 */
3607 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003608eval8(
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003609 char_u **arg,
3610 typval_T *rettv,
3611 evalarg_T *evalarg,
3612 int want_string) // after "." operator
3613{
3614 type_T *want_type = NULL;
3615 garray_T type_list; // list of pointers to allocated types
3616 int res;
3617 int evaluate = evalarg == NULL ? 0
3618 : (evalarg->eval_flags & EVAL_EVALUATE);
3619
3620 // Recognize <type> in Vim9 script only.
Bram Moolenaar678b2072021-07-26 21:10:11 +02003621 if (in_vim9script() && **arg == '<' && eval_isnamec1((*arg)[1])
3622 && STRNCMP(*arg, "<SNR>", 5) != 0)
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003623 {
3624 ++*arg;
3625 ga_init2(&type_list, sizeof(type_T *), 10);
3626 want_type = parse_type(arg, &type_list, TRUE);
3627 if (want_type == NULL && (evaluate || **arg != '>'))
3628 {
3629 clear_type_list(&type_list);
3630 return FAIL;
3631 }
3632
3633 if (**arg != '>')
3634 {
3635 if (*skipwhite(*arg) == '>')
3636 semsg(_(e_no_white_space_allowed_before_str_str), ">", *arg);
3637 else
3638 emsg(_(e_missing_gt));
3639 clear_type_list(&type_list);
3640 return FAIL;
3641 }
3642 ++*arg;
3643 *arg = skipwhite_and_linebreak(*arg, evalarg);
3644 }
3645
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003646 res = eval9(arg, rettv, evalarg, want_string);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003647
3648 if (want_type != NULL && evaluate)
3649 {
3650 if (res == OK)
3651 {
Bram Moolenaar114dbda2022-01-03 12:28:03 +00003652 type_T *actual = typval2type(rettv, get_copyID(), &type_list,
3653 TVTT_DO_MEMBER);
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003654
Bram Moolenaar60dc8272021-07-29 22:48:54 +02003655 if (!equal_type(want_type, actual, 0))
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003656 {
3657 if (want_type == &t_bool && actual != &t_bool
3658 && (actual->tt_flags & TTFLAG_BOOL_OK))
3659 {
3660 int n = tv2bool(rettv);
3661
3662 // can use "0" and "1" for boolean in some places
3663 clear_tv(rettv);
3664 rettv->v_type = VAR_BOOL;
3665 rettv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3666 }
3667 else
3668 {
Bram Moolenaar2b59df02021-07-22 15:14:25 +02003669 where_T where = WHERE_INIT;
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003670
Bram Moolenaar459fbdb2021-04-21 17:57:26 +02003671 where.wt_variable = TRUE;
3672 res = check_type(want_type, actual, TRUE, where);
3673 }
3674 }
3675 }
3676 clear_type_list(&type_list);
3677 }
3678
3679 return res;
3680}
3681
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003682 int
3683eval_leader(char_u **arg, int vim9)
3684{
3685 char_u *s = *arg;
3686 char_u *p = *arg;
3687
3688 while (*p == '!' || *p == '-' || *p == '+')
3689 {
3690 char_u *n = skipwhite(p + 1);
3691
3692 // ++, --, -+ and +- are not accepted in Vim9 script
3693 if (vim9 && (*p == '-' || *p == '+') && (*n == '-' || *n == '+'))
3694 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003695 semsg(_(e_invalid_expression_str), s);
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003696 return FAIL;
3697 }
3698 p = n;
3699 }
3700 *arg = p;
3701 return OK;
3702}
3703
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704/*
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003705 * Check for a predefined value "true", "false" and "null.*".
3706 * Return OK when recognized.
3707 */
3708 int
3709handle_predefined(char_u *s, int len, typval_T *rettv)
3710{
3711 switch (len)
3712 {
3713 case 4: if (STRNCMP(s, "true", 4) == 0)
3714 {
3715 rettv->v_type = VAR_BOOL;
3716 rettv->vval.v_number = VVAL_TRUE;
3717 return OK;
3718 }
3719 if (STRNCMP(s, "null", 4) == 0)
3720 {
3721 rettv->v_type = VAR_SPECIAL;
3722 rettv->vval.v_number = VVAL_NULL;
3723 return OK;
3724 }
3725 break;
3726 case 5: if (STRNCMP(s, "false", 5) == 0)
3727 {
3728 rettv->v_type = VAR_BOOL;
3729 rettv->vval.v_number = VVAL_FALSE;
3730 return OK;
3731 }
3732 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003733 case 8: if (STRNCMP(s, "null_job", 8) == 0)
3734 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003735#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003736 rettv->v_type = VAR_JOB;
3737 rettv->vval.v_job = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003738#else
3739 rettv->v_type = VAR_SPECIAL;
3740 rettv->vval.v_number = VVAL_NULL;
3741#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003742 return OK;
3743 }
3744 break;
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003745 case 9:
3746 if (STRNCMP(s, "null_", 5) != 0)
3747 break;
3748 if (STRNCMP(s + 5, "list", 4) == 0)
3749 {
3750 rettv->v_type = VAR_LIST;
3751 rettv->vval.v_list = NULL;
3752 return OK;
3753 }
3754 if (STRNCMP(s + 5, "dict", 4) == 0)
3755 {
3756 rettv->v_type = VAR_DICT;
3757 rettv->vval.v_dict = NULL;
3758 return OK;
3759 }
3760 if (STRNCMP(s + 5, "blob", 4) == 0)
3761 {
3762 rettv->v_type = VAR_BLOB;
3763 rettv->vval.v_blob = NULL;
3764 return OK;
3765 }
3766 break;
3767 case 11: if (STRNCMP(s, "null_string", 11) == 0)
3768 {
3769 rettv->v_type = VAR_STRING;
3770 rettv->vval.v_string = NULL;
3771 return OK;
3772 }
3773 break;
3774 case 12:
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003775 if (STRNCMP(s, "null_channel", 12) == 0)
3776 {
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003777#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003778 rettv->v_type = VAR_CHANNEL;
3779 rettv->vval.v_channel = NULL;
Bram Moolenaar4f3321f2022-03-13 13:12:27 +00003780#else
3781 rettv->v_type = VAR_SPECIAL;
3782 rettv->vval.v_number = VVAL_NULL;
3783#endif
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003784 return OK;
3785 }
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00003786 if (STRNCMP(s, "null_partial", 12) == 0)
3787 {
3788 rettv->v_type = VAR_PARTIAL;
3789 rettv->vval.v_partial = NULL;
3790 return OK;
3791 }
3792 break;
3793 case 13: if (STRNCMP(s, "null_function", 13) == 0)
3794 {
3795 rettv->v_type = VAR_FUNC;
3796 rettv->vval.v_string = NULL;
3797 return OK;
3798 }
3799 break;
3800 }
3801 return FAIL;
3802}
3803
3804/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 * Handle sixth level expression:
3806 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003807 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00003808 * "string" string constant
3809 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 * &option-name option value
3811 * @r register contents
3812 * identifier variable value
3813 * function() function call
3814 * $VAR environment variable
3815 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003816 * [expr, expr] List
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 * {arg, arg -> expr} Lambda
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003818 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003819 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 *
3821 * Also handle:
3822 * ! in front logical NOT
3823 * - in front unary minus
3824 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003825 * trailing [] subscript in String or List
3826 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02003827 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 *
3829 * "arg" must point to the first non-white of the expression.
Bram Moolenaarff1cd392020-08-05 11:51:30 +02003830 * "arg" is advanced to just after the recognized expression.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 *
3832 * Return OK or FAIL.
3833 */
3834 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003835eval9(
Bram Moolenaar7454a062016-01-30 15:14:10 +01003836 char_u **arg,
3837 typval_T *rettv,
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003838 evalarg_T *evalarg,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003839 int want_string) // after "." operator
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840{
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02003841 int evaluate = evalarg != NULL
3842 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 int len;
3844 char_u *s;
Bram Moolenaar32884ad2022-01-07 12:45:29 +00003845 char_u *name_start = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 char_u *start_leader, *end_leader;
3847 int ret = OK;
3848 char_u *alias;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003849 static int recurse = 0;
3850 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851
3852 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003853 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003854 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003856 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857
3858 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02003859 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 */
3861 start_leader = *arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00003862 if (eval_leader(arg, vim9script) == FAIL)
Bram Moolenaarb23279d2021-01-05 22:08:20 +01003863 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 end_leader = *arg;
3865
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01003866 if (**arg == '.' && (!isdigit(*(*arg + 1)) || in_old_script(2)))
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003867 {
Bram Moolenaar108010a2021-06-27 22:03:33 +02003868 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02003869 ++*arg;
3870 return FAIL;
3871 }
3872
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003873 // Limit recursion to 1000 levels. At least at 10000 we run out of stack
Bram Moolenaar50e05252022-01-24 18:36:39 +00003874 // and crash. With MSVC the stack is smaller.
3875 if (recurse ==
3876#ifdef _MSC_VER
3877 300
3878#else
3879 1000
3880#endif
3881 )
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00003882 {
3883 semsg(_(e_expression_too_recursive_str), *arg);
3884 return FAIL;
3885 }
3886 ++recurse;
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 switch (**arg)
3889 {
3890 /*
3891 * Number constant.
3892 */
3893 case '0':
3894 case '1':
3895 case '2':
3896 case '3':
3897 case '4':
3898 case '5':
3899 case '6':
3900 case '7':
3901 case '8':
3902 case '9':
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003903 case '.': ret = eval_number(arg, rettv, evaluate, want_string);
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02003904
3905 // Apply prefixed "-" and "+" now. Matters especially when
3906 // "->" follows.
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02003907 if (ret == OK && evaluate && end_leader > start_leader
3908 && rettv->v_type != VAR_BLOB)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01003909 ret = eval9_leader(rettv, TRUE, start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 break;
3911
3912 /*
3913 * String constant: "string".
3914 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003915 case '"': ret = eval_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917
3918 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003919 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 */
Bram Moolenaar0abc2872022-05-10 13:24:30 +01003921 case '\'': ret = eval_lit_string(arg, rettv, evaluate, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003922 break;
3923
3924 /*
3925 * List: [expr, expr]
3926 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003927 case '[': ret = eval_list(arg, rettv, evalarg, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 break;
3929
3930 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02003931 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003932 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003933 case '#': if (vim9script)
Bram Moolenaar5c7a2992021-03-20 13:29:38 +01003934 {
3935 ret = vim9_bad_comment(*arg) ? FAIL : NOTDONE;
3936 }
3937 else if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003938 {
3939 ++*arg;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003940 ret = eval_dict(arg, rettv, evalarg, TRUE);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003941 }
3942 else
3943 ret = NOTDONE;
3944 break;
3945
3946 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003947 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02003948 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00003949 */
Bram Moolenaar4525a572022-02-13 11:57:33 +00003950 case '{': if (vim9script)
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01003951 ret = NOTDONE;
3952 else
Bram Moolenaar4525a572022-02-13 11:57:33 +00003953 ret = get_lambda_tv(arg, rettv, vim9script, evalarg);
Bram Moolenaar069c1e72016-07-15 21:25:08 +02003954 if (ret == NOTDONE)
Bram Moolenaar8ea93902020-06-27 14:11:53 +02003955 ret = eval_dict(arg, rettv, evalarg, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003956 break;
3957
3958 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00003959 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 */
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02003961 case '&': ret = eval_option(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 break;
3963
3964 /*
3965 * Environment variable: $VAR.
LemonBoy2eaef102022-05-06 13:14:50 +01003966 * Interpolated string: $"string" or $'string'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 */
LemonBoy2eaef102022-05-06 13:14:50 +01003968 case '$': if ((*arg)[1] == '"' || (*arg)[1] == '\'')
3969 ret = eval_interp_string(arg, rettv, evaluate);
3970 else
3971 ret = eval_env_var(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 break;
3973
3974 /*
3975 * Register contents: @r.
3976 */
3977 case '@': ++*arg;
3978 if (evaluate)
3979 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00003980 if (vim9script && IS_WHITE_OR_NUL(**arg))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003981 semsg(_(e_syntax_error_at_str), *arg);
Bram Moolenaar4525a572022-02-13 11:57:33 +00003982 else if (vim9script && !valid_yank_reg(**arg, FALSE))
Bram Moolenaar90193e62021-04-04 20:49:50 +02003983 emsg_invreg(**arg);
3984 else
3985 {
3986 rettv->v_type = VAR_STRING;
3987 rettv->vval.v_string = get_reg_contents(**arg,
3988 GREG_EXPR_SRC);
3989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 }
3991 if (**arg != NUL)
3992 ++*arg;
3993 break;
3994
3995 /*
3996 * nested expression: (expression).
Bram Moolenaarecb66452021-05-18 15:09:18 +02003997 * or lambda: (arg) => expr
Bram Moolenaar071d4272004-06-13 20:20:40 +00003998 */
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01003999 case '(': ret = NOTDONE;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004000 if (vim9script)
Bram Moolenaar06409502021-02-17 17:00:27 +01004001 {
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004002 ret = get_lambda_tv(arg, rettv, TRUE, evalarg);
Bram Moolenaar06409502021-02-17 17:00:27 +01004003 if (ret == OK && evaluate)
4004 {
4005 ufunc_T *ufunc = rettv->vval.v_partial->pt_func;
4006
Bram Moolenaara9931532021-06-12 15:58:16 +02004007 // Compile it here to get the return type. The return
4008 // type is optional, when it's missing use t_unknown.
4009 // This is recognized in compile_return().
4010 if (ufunc->uf_ret_type->tt_type == VAR_VOID)
4011 ufunc->uf_ret_type = &t_unknown;
Bram Moolenaar139575d2022-03-15 19:29:30 +00004012 if (compile_def_function(ufunc, FALSE,
4013 get_compile_type(ufunc), NULL) == FAIL)
Bram Moolenaarc7dac852021-02-17 18:49:11 +01004014 {
4015 clear_tv(rettv);
4016 ret = FAIL;
4017 }
Bram Moolenaar06409502021-02-17 17:00:27 +01004018 }
4019 }
Bram Moolenaarc754b4c2020-12-25 15:24:23 +01004020 if (ret == NOTDONE)
4021 {
Bram Moolenaar9215f012020-06-27 21:18:00 +02004022 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004023 ret = eval1(arg, rettv, evalarg); // recursive!
Bram Moolenaar7a4981b2020-06-27 20:46:29 +02004024
Bram Moolenaar9215f012020-06-27 21:18:00 +02004025 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004026 if (**arg == ')')
4027 ++*arg;
4028 else if (ret == OK)
4029 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004030 emsg(_(e_missing_closing_paren));
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02004031 clear_tv(rettv);
4032 ret = FAIL;
4033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 break;
4036
Bram Moolenaar8c711452005-01-14 21:53:12 +00004037 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 break;
4039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004040
4041 if (ret == NOTDONE)
4042 {
4043 /*
4044 * Must be a variable or function name.
4045 * Can also be a curly-braces kind of name: {expr}.
4046 */
4047 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004048 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004049 if (alias != NULL)
4050 s = alias;
4051
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004052 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004053 ret = FAIL;
4054 else
4055 {
Bram Moolenaar3ac9c472020-07-13 21:28:03 +02004056 int flags = evalarg == NULL ? 0 : evalarg->eval_flags;
4057
Bram Moolenaar4525a572022-02-13 11:57:33 +00004058 if (evaluate && vim9script && len == 1 && *s == '_')
Bram Moolenaar962c43b2021-04-10 17:18:09 +02004059 {
4060 emsg(_(e_cannot_use_underscore_here));
4061 ret = FAIL;
4062 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004063 else if (evaluate && vim9script && len > 2
Bram Moolenaara749a422022-02-12 19:52:25 +00004064 && s[0] == 's' && s[1] == ':')
4065 {
4066 semsg(_(e_cannot_use_s_colon_in_vim9_script_str), s);
4067 ret = FAIL;
4068 }
Bram Moolenaar4525a572022-02-13 11:57:33 +00004069 else if ((vim9script ? **arg : *skipwhite(*arg)) == '(')
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004070 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004071 // "name(..." recursive!
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004072 *arg = skipwhite(*arg);
Bram Moolenaare6b53242020-07-01 17:28:33 +02004073 ret = eval_func(arg, evalarg, s, len, rettv, flags, NULL);
Bram Moolenaarbbd3e3c2020-08-06 11:23:36 +02004074 }
Bram Moolenaar227a69d2020-05-15 18:17:28 +02004075 else if (flags & EVAL_CONSTANT)
4076 ret = FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004077 else if (evaluate)
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004078 {
Bram Moolenaar8acb9cc2022-03-08 13:18:55 +00004079 // get the value of "true", "false", etc. or a variable
4080 ret = FAIL;
4081 if (vim9script)
4082 ret = handle_predefined(s, len, rettv);
4083 if (ret == FAIL)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004084 {
4085 name_start = s;
Bram Moolenaard5f400c2022-01-06 21:10:28 +00004086 ret = eval_variable(s, len, 0, rettv, NULL,
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01004087 EVAL_VAR_VERBOSE + EVAL_VAR_IMPORT);
Bram Moolenaar32884ad2022-01-07 12:45:29 +00004088 }
Bram Moolenaar5d2eb0f2020-07-13 21:59:33 +02004089 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004090 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004091 {
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02004092 // skip the name
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004093 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004094 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004095 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004096 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004097 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004098 }
4099
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004100 // Handle following '[', '(' and '.' for expr[expr], expr.name,
4101 // expr(expr), expr->name(expr)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004102 if (ret == OK)
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004103 ret = handle_subscript(arg, name_start, rettv, evalarg, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104
4105 /*
4106 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4107 */
4108 if (ret == OK && evaluate && end_leader > start_leader)
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004109 ret = eval9_leader(rettv, FALSE, start_leader, &end_leader);
Bram Moolenaarfe6fb262022-01-24 18:16:12 +00004110
4111 --recurse;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004112 return ret;
4113}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004114
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004115/*
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004116 * Apply the leading "!" and "-" before an eval9 expression to "rettv".
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004117 * When "numeric_only" is TRUE only handle "+" and "-".
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004118 * Adjusts "end_leaderp" until it is at "start_leader".
4119 */
4120 static int
Yegappan Lakshmanana061f342022-05-22 19:13:49 +01004121eval9_leader(
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004122 typval_T *rettv,
4123 int numeric_only,
4124 char_u *start_leader,
4125 char_u **end_leaderp)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004126{
4127 char_u *end_leader = *end_leaderp;
4128 int ret = OK;
4129 int error = FALSE;
4130 varnumber_T val = 0;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004131 vartype_T type = rettv->v_type;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004132 int vim9script = in_vim9script();
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004133 float_T f = 0.0;
4134
4135 if (rettv->v_type == VAR_FLOAT)
4136 f = rettv->vval.v_float;
4137 else
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004138 {
4139 while (VIM_ISWHITE(end_leader[-1]))
4140 --end_leader;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004141 if (vim9script && end_leader[-1] == '!')
Bram Moolenaar3e06a1e2020-08-10 21:57:54 +02004142 val = tv2bool(rettv);
4143 else
4144 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar27491cd2020-10-15 21:54:56 +02004145 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004146 if (error)
4147 {
4148 clear_tv(rettv);
4149 ret = FAIL;
4150 }
4151 else
4152 {
4153 while (end_leader > start_leader)
4154 {
4155 --end_leader;
4156 if (*end_leader == '!')
4157 {
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02004158 if (numeric_only)
4159 {
4160 ++end_leader;
4161 break;
4162 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004163 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004164 {
Bram Moolenaar4525a572022-02-13 11:57:33 +00004165 if (vim9script)
Bram Moolenaar659bb222020-11-12 20:16:39 +01004166 {
4167 rettv->v_type = VAR_BOOL;
4168 val = f == 0.0 ? VVAL_TRUE : VVAL_FALSE;
4169 }
4170 else
4171 f = !f;
4172 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004173 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004174 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004175 val = !val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004176 type = VAR_BOOL;
4177 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004178 }
4179 else if (*end_leader == '-')
4180 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004181 if (rettv->v_type == VAR_FLOAT)
4182 f = -f;
4183 else
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004184 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004185 val = -val;
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004186 type = VAR_NUMBER;
4187 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004188 }
4189 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004190 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004193 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 else
4196 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004197 clear_tv(rettv);
Bram Moolenaar4525a572022-02-13 11:57:33 +00004198 if (vim9script)
Bram Moolenaar6e4cfff2020-08-09 22:17:55 +02004199 rettv->v_type = type;
4200 else
4201 rettv->v_type = VAR_NUMBER;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004202 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02004205 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return ret;
4207}
4208
4209/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004210 * Call the function referred to in "rettv".
4211 */
4212 static int
4213call_func_rettv(
4214 char_u **arg,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004215 evalarg_T *evalarg,
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004216 typval_T *rettv,
4217 int evaluate,
4218 dict_T *selfdict,
4219 typval_T *basetv)
4220{
4221 partial_T *pt = NULL;
4222 funcexe_T funcexe;
4223 typval_T functv;
4224 char_u *s;
4225 int ret;
4226
4227 // need to copy the funcref so that we can clear rettv
4228 if (evaluate)
4229 {
4230 functv = *rettv;
4231 rettv->v_type = VAR_UNKNOWN;
4232
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004233 // Invoke the function. Recursive!
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004234 if (functv.v_type == VAR_PARTIAL)
4235 {
4236 pt = functv.vval.v_partial;
4237 s = partial_name(pt);
4238 }
4239 else
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004240 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004241 s = functv.vval.v_string;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004242 if (s == NULL || *s == NUL)
4243 {
4244 emsg(_(e_empty_function_name));
Bram Moolenaar744aecf2021-06-12 12:33:48 +02004245 ret = FAIL;
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004246 goto theend;
4247 }
4248 }
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004249 }
4250 else
4251 s = (char_u *)"";
4252
Bram Moolenaara80faa82020-04-12 19:37:17 +02004253 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +00004254 funcexe.fe_firstline = curwin->w_cursor.lnum;
4255 funcexe.fe_lastline = curwin->w_cursor.lnum;
4256 funcexe.fe_evaluate = evaluate;
4257 funcexe.fe_partial = pt;
4258 funcexe.fe_selfdict = selfdict;
4259 funcexe.fe_basetv = basetv;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004260 ret = get_func_tv(s, -1, rettv, arg, evalarg, &funcexe);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004261
Bram Moolenaar22db0d52021-06-12 12:16:55 +02004262theend:
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004263 // Clear the funcref afterwards, so that deleting it while
4264 // evaluating the arguments is possible (see test55).
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004265 if (evaluate)
4266 clear_tv(&functv);
4267
4268 return ret;
4269}
4270
4271/*
4272 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004273 * "*arg" points to "method".
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004274 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4275 */
4276 static int
4277eval_lambda(
4278 char_u **arg,
4279 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004280 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004281 int verbose) // give error messages
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004282{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004283 int evaluate = evalarg != NULL
4284 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004285 typval_T base = *rettv;
4286 int ret;
4287
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004288 rettv->v_type = VAR_UNKNOWN;
4289
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004290 if (**arg == '{')
4291 {
4292 // ->{lambda}()
4293 ret = get_lambda_tv(arg, rettv, FALSE, evalarg);
4294 }
4295 else
4296 {
4297 // ->(lambda)()
4298 ++*arg;
4299 ret = eval1(arg, rettv, evalarg);
4300 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004301 if (**arg != ')')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004302 {
Bram Moolenaare1242042021-12-16 20:56:57 +00004303 emsg(_(e_missing_closing_paren));
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004304 return FAIL;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004305 }
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004306 if (rettv->v_type != VAR_STRING && rettv->v_type != VAR_FUNC
4307 && rettv->v_type != VAR_PARTIAL)
4308 {
4309 emsg(_(e_string_or_function_required_for_arrow_parens_expr));
4310 return FAIL;
4311 }
4312 ++*arg;
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01004313 }
Bram Moolenaar0ff822d2019-12-08 18:41:34 +01004314 if (ret != OK)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004315 return FAIL;
Bram Moolenaar8b91e712022-04-17 15:06:35 +01004316
4317 if (**arg != '(')
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004318 {
4319 if (verbose)
4320 {
4321 if (*skipwhite(*arg) == '(')
Bram Moolenaar3a846e62022-01-01 16:21:00 +00004322 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004323 else
Bram Moolenaare1242042021-12-16 20:56:57 +00004324 semsg(_(e_missing_parenthesis_str), "lambda");
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004325 }
4326 clear_tv(rettv);
Bram Moolenaar86173482019-10-01 17:02:16 +02004327 ret = FAIL;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004328 }
Bram Moolenaar86173482019-10-01 17:02:16 +02004329 else
Bram Moolenaare6b53242020-07-01 17:28:33 +02004330 ret = call_func_rettv(arg, evalarg, rettv, evaluate, NULL, &base);
Bram Moolenaar86173482019-10-01 17:02:16 +02004331
4332 // Clear the funcref afterwards, so that deleting it while
4333 // evaluating the arguments is possible (see test55).
4334 if (evaluate)
4335 clear_tv(&base);
4336
4337 return ret;
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004338}
4339
4340/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02004341 * Evaluate "->method()".
Bram Moolenaar7cebe8b2021-01-23 14:22:16 +01004342 * "*arg" points to "method".
Bram Moolenaarac92e252019-08-03 21:58:38 +02004343 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
4344 */
4345 static int
4346eval_method(
4347 char_u **arg,
4348 typval_T *rettv,
Bram Moolenaare6b53242020-07-01 17:28:33 +02004349 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004350 int verbose) // give error messages
Bram Moolenaarac92e252019-08-03 21:58:38 +02004351{
4352 char_u *name;
4353 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004354 char_u *alias;
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004355 char_u *tofree = NULL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004356 typval_T base = *rettv;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004357 int ret = OK;
Bram Moolenaare6b53242020-07-01 17:28:33 +02004358 int evaluate = evalarg != NULL
4359 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004360
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004361 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004362
Bram Moolenaarac92e252019-08-03 21:58:38 +02004363 name = *arg;
Bram Moolenaar6ac69ed2022-09-03 12:09:07 +01004364 len = get_name_len(arg, &alias, evaluate, evaluate);
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004365 if (alias != NULL)
4366 name = alias;
4367
4368 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02004369 {
4370 if (verbose)
Bram Moolenaar9a846fb2022-01-01 21:59:18 +00004371 emsg(_(e_missing_name_after_method));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004372 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02004373 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004374 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02004375 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004376 char_u *paren;
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004377
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004378 // If there is no "(" immediately following, but there is further on,
4379 // it can be "import.Func()", "dict.Func()", "list[nr]", etc.
4380 // Does not handle anything where "(" is part of the expression.
4381 *arg = skipwhite(*arg);
4382
4383 if (**arg != '(' && alias == NULL
4384 && (paren = vim_strchr(*arg, '(')) != NULL)
4385 {
Bram Moolenaar64283d52022-01-18 10:37:29 +00004386 char_u *deref;
4387
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004388 *arg = name;
4389 *paren = NUL;
Bram Moolenaar64283d52022-01-18 10:37:29 +00004390 deref = deref_function_name(arg, &tofree, evalarg, verbose);
4391 if (deref == NULL)
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004392 {
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004393 *arg = name + len;
4394 ret = FAIL;
4395 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004396 else
Bram Moolenaar64283d52022-01-18 10:37:29 +00004397 {
4398 name = deref;
K.Takata1a804522022-01-26 16:45:20 +00004399 len = (long)STRLEN(name);
Bram Moolenaar64283d52022-01-18 10:37:29 +00004400 }
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004401 *paren = '(';
Bram Moolenaar761fdf02019-08-05 23:10:16 +02004402 }
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004403
4404 if (ret == OK)
Bram Moolenaar51841322019-08-08 21:10:01 +02004405 {
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004406 *arg = skipwhite(*arg);
4407
4408 if (**arg != '(')
4409 {
4410 if (verbose)
4411 semsg(_(e_missing_parenthesis_str), name);
4412 ret = FAIL;
4413 }
4414 else if (VIM_ISWHITE((*arg)[-1]))
4415 {
4416 if (verbose)
4417 emsg(_(e_no_white_space_allowed_before_parenthesis));
4418 ret = FAIL;
4419 }
4420 else
4421 ret = eval_func(arg, evalarg, name, len, rettv,
Bram Moolenaar32e35112020-05-14 22:41:15 +02004422 evaluate ? EVAL_EVALUATE : 0, &base);
Bram Moolenaar857c8bb2022-01-15 21:08:19 +00004423 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004424 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02004425
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02004426 // Clear the funcref afterwards, so that deleting it while
4427 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02004428 if (evaluate)
4429 clear_tv(&base);
Bram Moolenaarc665dab2022-01-16 19:38:07 +00004430 vim_free(tofree);
Bram Moolenaarac92e252019-08-03 21:58:38 +02004431
Bram Moolenaarac92e252019-08-03 21:58:38 +02004432 return ret;
4433}
4434
4435/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004436 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4437 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004438 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4439 */
4440 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004441eval_index(
4442 char_u **arg,
4443 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004444 evalarg_T *evalarg,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004445 int verbose) // give error messages
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004446{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004447 int evaluate = evalarg != NULL
4448 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004449 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004450 typval_T var1, var2;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004451 int range = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004452 char_u *key = NULL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004453 int keylen = -1;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004454 int vim9script = in_vim9script();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004455
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004456 if (check_can_index(rettv, evaluate, verbose) == FAIL)
4457 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004458
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004459 init_tv(&var1);
4460 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004461 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004463 /*
4464 * dict.name
4465 */
4466 key = *arg + 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004467 for (keylen = 0; eval_isdictc(key[keylen]); ++keylen)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004468 ;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004469 if (keylen == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004470 return FAIL;
Bram Moolenaarc6e57b72020-09-12 21:27:03 +02004471 *arg = key + keylen;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004472 }
4473 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004474 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004475 /*
4476 * something[idx]
4477 *
4478 * Get the (first) variable from inside the [].
4479 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004480 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004481 if (**arg == ':')
4482 empty1 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004483 else if (eval1(arg, &var1, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004484 return FAIL;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004485 else if (vim9script && **arg == ':')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004486 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004487 semsg(_(e_white_space_required_before_and_after_str_at_str),
4488 ":", *arg);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004489 clear_tv(&var1);
4490 return FAIL;
4491 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004492 else if (evaluate)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004493 {
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004494 int error = FALSE;
4495
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004496 // allow for indexing with float
Bram Moolenaar4525a572022-02-13 11:57:33 +00004497 if (vim9script && rettv->v_type == VAR_DICT
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004498 && var1.v_type == VAR_FLOAT)
4499 {
4500 var1.vval.v_string = typval_tostring(&var1, TRUE);
4501 var1.v_type = VAR_STRING;
4502 }
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01004503
Bram Moolenaar4525a572022-02-13 11:57:33 +00004504 if (vim9script && rettv->v_type == VAR_LIST)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004505 tv_get_number_chk(&var1, &error);
4506 else
4507 error = tv_get_string_chk(&var1) == NULL;
4508 if (error)
Bram Moolenaar2e5910b2021-02-03 17:41:24 +01004509 {
4510 // not a number or string
4511 clear_tv(&var1);
4512 return FAIL;
4513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004514 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004515
4516 /*
4517 * Get the second variable from inside the [:].
4518 */
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004519 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004520 if (**arg == ':')
4521 {
4522 range = TRUE;
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004523 ++*arg;
Bram Moolenaar4525a572022-02-13 11:57:33 +00004524 if (vim9script && !IS_WHITE_OR_NUL(**arg) && **arg != ']')
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004525 {
Bram Moolenaare7a73e02021-01-01 19:17:55 +01004526 semsg(_(e_white_space_required_before_and_after_str_at_str),
4527 ":", *arg - 1);
Bram Moolenaarde4f95b2020-12-30 20:39:21 +01004528 if (!empty1)
4529 clear_tv(&var1);
4530 return FAIL;
4531 }
4532 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004533 if (**arg == ']')
4534 empty2 = TRUE;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02004535 else if (eval1(arg, &var2, evalarg) == FAIL) // recursive!
Bram Moolenaar8c711452005-01-14 21:53:12 +00004536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004537 if (!empty1)
4538 clear_tv(&var1);
4539 return FAIL;
4540 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004541 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004542 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004543 // not a number or string
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004544 if (!empty1)
4545 clear_tv(&var1);
4546 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004547 return FAIL;
4548 }
4549 }
4550
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004551 // Check for the ']'.
Bram Moolenaar442af2f2020-07-03 21:09:52 +02004552 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004553 if (**arg != ']')
4554 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004555 if (verbose)
Bram Moolenaare1242042021-12-16 20:56:57 +00004556 emsg(_(e_missing_closing_square_brace));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004557 clear_tv(&var1);
4558 if (range)
4559 clear_tv(&var2);
4560 return FAIL;
4561 }
Bram Moolenaarf9235712020-08-16 18:42:53 +02004562 *arg = *arg + 1; // skip over the ']'
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004563 }
4564
4565 if (evaluate)
4566 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004567 int res = eval_index_inner(rettv, range,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004568 empty1 ? NULL : &var1, empty2 ? NULL : &var2, FALSE,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004569 key, keylen, verbose);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004570
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004571 if (!empty1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004572 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004573 if (range)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004574 clear_tv(&var2);
4575 return res;
4576 }
4577 return OK;
4578}
4579
4580/*
4581 * Check if "rettv" can have an [index] or [sli:ce]
4582 */
4583 int
4584check_can_index(typval_T *rettv, int evaluate, int verbose)
4585{
4586 switch (rettv->v_type)
4587 {
4588 case VAR_FUNC:
4589 case VAR_PARTIAL:
4590 if (verbose)
Bram Moolenaarf47c5a82021-12-19 15:17:21 +00004591 emsg(_(e_cannot_index_a_funcref));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004592 return FAIL;
4593 case VAR_FLOAT:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004594 if (verbose)
Bram Moolenaar74409f62022-01-01 15:58:22 +00004595 emsg(_(e_using_float_as_string));
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004596 return FAIL;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004597 case VAR_BOOL:
4598 case VAR_SPECIAL:
4599 case VAR_JOB:
4600 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004601 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004602 if (verbose)
4603 emsg(_(e_cannot_index_special_variable));
4604 return FAIL;
4605 case VAR_UNKNOWN:
4606 case VAR_ANY:
4607 case VAR_VOID:
4608 if (evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004609 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004610 emsg(_(e_cannot_index_special_variable));
4611 return FAIL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004612 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004613 // FALLTHROUGH
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004614
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004615 case VAR_STRING:
4616 case VAR_LIST:
4617 case VAR_DICT:
4618 case VAR_BLOB:
4619 break;
4620 case VAR_NUMBER:
4621 if (in_vim9script())
4622 emsg(_(e_cannot_index_number));
4623 break;
4624 }
4625 return OK;
4626}
4627
4628/*
Bram Moolenaar6601b622021-01-13 21:47:15 +01004629 * slice() function
4630 */
4631 void
4632f_slice(typval_T *argvars, typval_T *rettv)
4633{
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004634 if (in_vim9script()
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02004635 && ((argvars[0].v_type != VAR_STRING
4636 && argvars[0].v_type != VAR_LIST
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004637 && argvars[0].v_type != VAR_BLOB
Yegappan Lakshmanan83494b42021-07-20 17:51:51 +02004638 && check_for_list_arg(argvars, 0) == FAIL)
4639 || check_for_number_arg(argvars, 1) == FAIL
4640 || check_for_opt_number_arg(argvars, 2) == FAIL))
4641 return;
4642
Bram Moolenaar6601b622021-01-13 21:47:15 +01004643 if (check_can_index(argvars, TRUE, FALSE) == OK)
4644 {
4645 copy_tv(argvars, rettv);
4646 eval_index_inner(rettv, TRUE, argvars + 1,
4647 argvars[2].v_type == VAR_UNKNOWN ? NULL : argvars + 2,
4648 TRUE, NULL, 0, FALSE);
4649 }
4650}
4651
4652/*
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004653 * Apply index or range to "rettv".
4654 * "var1" is the first index, NULL for [:expr].
4655 * "var2" is the second index, NULL for [expr] and [expr: ]
Bram Moolenaar6601b622021-01-13 21:47:15 +01004656 * "exclusive" is TRUE for slice(): second index is exclusive, use character
4657 * index for string.
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004658 * Alternatively, "key" is not NULL, then key[keylen] is the dict index.
4659 */
4660 int
4661eval_index_inner(
4662 typval_T *rettv,
4663 int is_range,
4664 typval_T *var1,
4665 typval_T *var2,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004666 int exclusive,
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004667 char_u *key,
4668 int keylen,
4669 int verbose)
4670{
Bram Moolenaar6601b622021-01-13 21:47:15 +01004671 varnumber_T n1, n2 = 0;
4672 long len;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004673
4674 n1 = 0;
4675 if (var1 != NULL && rettv->v_type != VAR_DICT)
4676 n1 = tv_get_number(var1);
4677
4678 if (is_range)
4679 {
4680 if (rettv->v_type == VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004681 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004682 if (verbose)
4683 emsg(_(e_cannot_slice_dictionary));
4684 return FAIL;
4685 }
Bram Moolenaar6601b622021-01-13 21:47:15 +01004686 if (var2 != NULL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004687 n2 = tv_get_number(var2);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004688 else
4689 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004690 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004691
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004692 switch (rettv->v_type)
4693 {
4694 case VAR_UNKNOWN:
4695 case VAR_ANY:
4696 case VAR_VOID:
4697 case VAR_FUNC:
4698 case VAR_PARTIAL:
4699 case VAR_FLOAT:
4700 case VAR_BOOL:
4701 case VAR_SPECIAL:
4702 case VAR_JOB:
4703 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004704 case VAR_INSTR:
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004705 break; // not evaluating, skipping over subscript
4706
4707 case VAR_NUMBER:
4708 case VAR_STRING:
4709 {
4710 char_u *s = tv_get_string(rettv);
4711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004712 len = (long)STRLEN(s);
Bram Moolenaar6601b622021-01-13 21:47:15 +01004713 if (in_vim9script() || exclusive)
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004714 {
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004715 if (is_range)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004716 s = string_slice(s, n1, n2, exclusive);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004717 else
4718 s = char_from_string(s, n1);
4719 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004720 else if (is_range)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004721 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004722 // The resulting variable is a substring. If the indexes
4723 // are out of range the result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004724 if (n1 < 0)
4725 {
4726 n1 = len + n1;
4727 if (n1 < 0)
4728 n1 = 0;
4729 }
4730 if (n2 < 0)
4731 n2 = len + n2;
4732 else if (n2 >= len)
4733 n2 = len;
4734 if (n1 >= len || n2 < 0 || n1 > n2)
4735 s = NULL;
4736 else
Bram Moolenaardf44a272020-06-07 20:49:05 +02004737 s = vim_strnsave(s + n1, n2 - n1 + 1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004738 }
4739 else
4740 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004741 // The resulting variable is a string of a single
4742 // character. If the index is too big or negative the
4743 // result is empty.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004744 if (n1 >= len || n1 < 0)
4745 s = NULL;
4746 else
4747 s = vim_strnsave(s + n1, 1);
4748 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004749 clear_tv(rettv);
4750 rettv->v_type = VAR_STRING;
4751 rettv->vval.v_string = s;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004752 }
4753 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004754
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004755 case VAR_BLOB:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004756 blob_slice_or_index(rettv->vval.v_blob, is_range, n1, n2,
4757 exclusive, rettv);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004758 break;
4759
4760 case VAR_LIST:
4761 if (var1 == NULL)
4762 n1 = 0;
4763 if (var2 == NULL)
Bram Moolenaar6601b622021-01-13 21:47:15 +01004764 n2 = VARNUM_MAX;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004765 if (list_slice_or_index(rettv->vval.v_list,
Bram Moolenaar6601b622021-01-13 21:47:15 +01004766 is_range, n1, n2, exclusive, rettv, verbose) == FAIL)
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004767 return FAIL;
4768 break;
4769
4770 case VAR_DICT:
4771 {
4772 dictitem_T *item;
4773 typval_T tmp;
4774
4775 if (key == NULL)
4776 {
4777 key = tv_get_string_chk(var1);
4778 if (key == NULL)
4779 return FAIL;
4780 }
4781
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004782 item = dict_find(rettv->vval.v_dict, key, keylen);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004783
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004784 if (item == NULL)
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004785 {
4786 if (verbose)
4787 {
4788 if (keylen > 0)
4789 key[keylen] = NUL;
Bram Moolenaar460ae5d2022-01-01 14:19:49 +00004790 semsg(_(e_key_not_present_in_dictionary), key);
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004791 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004792 return FAIL;
Bram Moolenaar5c1ec432021-11-29 13:44:55 +00004793 }
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004794
4795 copy_tv(&item->di_tv, &tmp);
4796 clear_tv(rettv);
4797 *rettv = tmp;
4798 }
4799 break;
4800 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004801 return OK;
4802}
4803
4804/*
Bram Moolenaar4c683752020-04-05 21:38:23 +02004805 * Return the function name of partial "pt".
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004806 */
4807 char_u *
4808partial_name(partial_T *pt)
4809{
Dominique Pellefe8ebdb2021-05-13 14:55:55 +02004810 if (pt != NULL)
4811 {
4812 if (pt->pt_name != NULL)
4813 return pt->pt_name;
4814 if (pt->pt_func != NULL)
4815 return pt->pt_func->uf_name;
4816 }
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02004817 return (char_u *)"";
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004818}
4819
Bram Moolenaarddecc252016-04-06 22:59:37 +02004820 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004821partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02004822{
4823 int i;
4824
4825 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004826 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02004827 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004828 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004829 if (pt->pt_name != NULL)
4830 {
4831 func_unref(pt->pt_name);
4832 vim_free(pt->pt_name);
4833 }
4834 else
4835 func_ptr_unref(pt->pt_func);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004836
Bram Moolenaar54656012021-06-09 20:50:46 +02004837 // "out_up" is no longer used, decrement refcount on partial that owns it.
4838 partial_unref(pt->pt_outer.out_up_partial);
4839
Bram Moolenaar7aca5ca2022-02-07 19:56:43 +00004840 // Using pt_outer from another partial.
4841 partial_unref(pt->pt_outer_partial);
4842
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004843 // Decrease the reference count for the context of a closure. If down
4844 // to the minimum it may be time to free it.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004845 if (pt->pt_funcstack != NULL)
4846 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004847 --pt->pt_funcstack->fs_refcount;
4848 funcstack_check_refcount(pt->pt_funcstack);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004849 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004850 // Similarly for loop variables.
Bram Moolenaarcc341812022-09-19 15:54:34 +01004851 for (i = 0; i < MAX_LOOP_DEPTH; ++i)
4852 if (pt->pt_loopvars[i] != NULL)
4853 {
4854 --pt->pt_loopvars[i]->lvs_refcount;
4855 loopvars_check_refcount(pt->pt_loopvars[i]);
4856 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +02004857
Bram Moolenaarddecc252016-04-06 22:59:37 +02004858 vim_free(pt);
4859}
4860
4861/*
4862 * Unreference a closure: decrement the reference count and free it when it
4863 * becomes zero.
4864 */
4865 void
4866partial_unref(partial_T *pt)
4867{
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004868 if (pt != NULL)
4869 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004870 int done = FALSE;
4871
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004872 if (--pt->pt_refcount <= 0)
4873 partial_free(pt);
4874
4875 // If the reference count goes down to one, the funcstack may be the
4876 // only reference and can be freed if no other partials reference it.
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004877 else if (pt->pt_refcount == 1)
4878 {
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004879 // careful: if the funcstack is freed it may contain this partial
4880 // and it gets freed as well
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004881 if (pt->pt_funcstack != NULL)
Bram Moolenaaracd6b992022-09-17 16:27:39 +01004882 done = funcstack_check_refcount(pt->pt_funcstack);
4883
Bram Moolenaarcc341812022-09-19 15:54:34 +01004884 if (!done)
4885 {
4886 int depth;
4887
4888 for (depth = 0; depth < MAX_LOOP_DEPTH; ++depth)
4889 if (pt->pt_loopvars[depth] != NULL
4890 && loopvars_check_refcount(pt->pt_loopvars[depth]))
4891 break;
4892 }
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01004893 }
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +02004894 }
Bram Moolenaarddecc252016-04-06 22:59:37 +02004895}
4896
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004897/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004898 * Return the next (unique) copy ID.
4899 * Used for serializing nested structures.
4900 */
4901 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004902get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004903{
4904 current_copyID += COPYID_INC;
4905 return current_copyID;
4906}
4907
4908/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004909 * Garbage collection for lists and dictionaries.
4910 *
4911 * We use reference counts to be able to free most items right away when they
4912 * are no longer used. But for composite items it's possible that it becomes
4913 * unused while the reference count is > 0: When there is a recursive
4914 * reference. Example:
4915 * :let l = [1, 2, 3]
4916 * :let d = {9: l}
4917 * :let l[1] = d
4918 *
4919 * Since this is quite unusual we handle this with garbage collection: every
4920 * once in a while find out which lists and dicts are not referenced from any
4921 * variable.
4922 *
4923 * Here is a good reference text about garbage collection (refers to Python
4924 * but it applies to all reference-counting mechanisms):
4925 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004926 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004927
4928/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004929 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004930 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004931 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004932 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004933 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004934garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004935{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004936 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004937 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004938 buf_T *buf;
4939 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004940 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004941 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004942
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004943 if (!testing)
4944 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004945 // Only do this once.
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004946 want_garbage_collect = FALSE;
4947 may_garbage_collect = FALSE;
4948 garbage_collect_at_exit = FALSE;
4949 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004950
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004951 // The execution stack can grow big, limit the size.
4952 if (exestack.ga_maxlen - exestack.ga_len > 500)
4953 {
4954 size_t new_len;
4955 char_u *pp;
4956 int n;
4957
4958 // Keep 150% of the current size, with a minimum of the growth size.
4959 n = exestack.ga_len / 2;
4960 if (n < exestack.ga_growsize)
4961 n = exestack.ga_growsize;
4962
4963 // Don't make it bigger though.
4964 if (exestack.ga_len + n < exestack.ga_maxlen)
4965 {
=?UTF-8?q?Dundar=20G=C3=B6c?=d5cec1f2022-01-29 15:19:23 +00004966 new_len = (size_t)exestack.ga_itemsize * (exestack.ga_len + n);
Bram Moolenaar3fbcc122019-12-30 19:19:53 +01004967 pp = vim_realloc(exestack.ga_data, new_len);
4968 if (pp == NULL)
4969 return FAIL;
4970 exestack.ga_maxlen = exestack.ga_len + n;
4971 exestack.ga_data = pp;
4972 }
4973 }
4974
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004975 // We advance by two because we add one for items referenced through
4976 // previous_funccal.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004977 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004978
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004979 /*
4980 * 1. Go through all accessible variables and mark all lists and dicts
4981 * with copyID.
4982 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004983
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004984 // Don't free variables in the previous_funccal list unless they are only
4985 // referenced through previous_funccal. This must be first, because if
4986 // the item is referenced elsewhere the funccal must not be freed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004987 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004988
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004989 // script-local variables
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004990 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004991
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004992 // buffer-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02004993 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004994 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4995 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004996
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01004997 // window-local variables
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004998 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004999 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5000 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005001 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005002 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5003 NULL, NULL);
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005004#ifdef FEAT_PROP_POPUP
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005005 FOR_ALL_POPUPWINS(wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005006 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5007 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005008 FOR_ALL_TABPAGES(tp)
Bram Moolenaaraeea7212020-04-02 18:50:46 +02005009 FOR_ALL_POPUPWINS_IN_TAB(tp, wp)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02005010 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5011 NULL, NULL);
5012#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005013
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005014 // tabpage-local variables
Bram Moolenaar29323592016-07-24 22:04:11 +02005015 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005016 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5017 NULL, NULL);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005018 // global variables
Bram Moolenaarda6c0332019-09-01 16:01:30 +02005019 abort = abort || garbage_collect_globvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005020
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005021 // function-local variables
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005022 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005023
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005024 // named functions (matters for closures)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005025 abort = abort || set_ref_in_functions(copyID);
5026
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005027 // function call arguments, if v:testing is set.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005028 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005029
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005030 // funcstacks keep variables for closures
5031 abort = abort || set_ref_in_funcstacks(copyID);
5032
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005033 // loopvars keep variables for loop blocks
5034 abort = abort || set_ref_in_loopvars(copyID);
5035
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005036 // v: vars
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005037 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00005038
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005039 // callbacks in buffers
5040 abort = abort || set_ref_in_buffers(copyID);
5041
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005042 // 'completefunc', 'omnifunc' and 'thesaurusfunc' callbacks
5043 abort = abort || set_ref_in_insexpand_funcs(copyID);
5044
5045 // 'operatorfunc' callback
5046 abort = abort || set_ref_in_opfunc(copyID);
5047
5048 // 'tagfunc' callback
5049 abort = abort || set_ref_in_tagfunc(copyID);
5050
5051 // 'imactivatefunc' and 'imstatusfunc' callbacks
5052 abort = abort || set_ref_in_im_funcs(copyID);
5053
Bram Moolenaar1dced572012-04-05 16:54:08 +02005054#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005055 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005056#endif
5057
Bram Moolenaardb913952012-06-29 12:54:53 +02005058#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005059 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005060#endif
5061
5062#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005063 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005064#endif
5065
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005067 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005068 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005069#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005070#ifdef FEAT_NETBEANS_INTG
5071 abort = abort || set_ref_in_nb_channel(copyID);
5072#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005073
Bram Moolenaare3188e22016-05-31 21:13:04 +02005074#ifdef FEAT_TIMERS
5075 abort = abort || set_ref_in_timer(copyID);
5076#endif
5077
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005078#ifdef FEAT_QUICKFIX
5079 abort = abort || set_ref_in_quickfix(copyID);
5080#endif
5081
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005082#ifdef FEAT_TERMINAL
5083 abort = abort || set_ref_in_term(copyID);
5084#endif
5085
Bram Moolenaar05ad5ff2019-11-30 22:48:27 +01005086#ifdef FEAT_PROP_POPUP
Bram Moolenaar75a1a942019-06-20 03:45:36 +02005087 abort = abort || set_ref_in_popups(copyID);
5088#endif
5089
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005090 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005091 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005092 /*
5093 * 2. Free lists and dictionaries that are not referenced.
5094 */
5095 did_free = free_unref_items(copyID);
5096
5097 /*
5098 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005099 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005100 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005101 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005102 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005103 else if (p_verbose > 0)
5104 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005105 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005106 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005107
5108 return did_free;
5109}
5110
5111/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005112 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005113 */
5114 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005115free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005116{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005117 int did_free = FALSE;
5118
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005119 // Let all "free" functions know that we are here. This means no
5120 // dictionaries, lists, channels or jobs are to be freed, because we will
5121 // do that here.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005122 in_free_unref_items = TRUE;
5123
5124 /*
5125 * PASS 1: free the contents of the items. We don't free the items
5126 * themselves yet, so that it is possible to decrement refcount counters
5127 */
5128
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005129 // Go through the list of dicts and free items without the copyID.
Bram Moolenaarcd524592016-07-17 14:57:05 +02005130 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005131
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005132 // Go through the list of lists and free items without the copyID.
Bram Moolenaarda861d62016-07-17 15:46:27 +02005133 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005134
5135#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005136 // Go through the list of jobs and free items without the copyID. This
5137 // must happen before doing channels, because jobs refer to channels, but
5138 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005139 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5140
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005141 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005142 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5143#endif
5144
5145 /*
5146 * PASS 2: free the items themselves.
5147 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005148 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005149 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005150
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005151#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005152 // Go through the list of jobs and free items without the copyID. This
5153 // must happen before doing channels, because jobs refer to channels, but
5154 // the reference from the channel to the job isn't tracked.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005155 free_unused_jobs(copyID, COPYID_MASK);
5156
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005157 // Go through the list of channels and free items without the copyID.
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005158 free_unused_channels(copyID, COPYID_MASK);
5159#endif
5160
5161 in_free_unref_items = FALSE;
5162
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005163 return did_free;
5164}
5165
5166/*
5167 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005168 * "list_stack" is used to add lists to be marked. Can be NULL.
5169 *
5170 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005171 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005172 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005173set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005174{
5175 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005176 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005177 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005178 hashtab_T *cur_ht;
5179 ht_stack_T *ht_stack = NULL;
5180 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005181
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005182 cur_ht = ht;
5183 for (;;)
5184 {
5185 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005187 // Mark each item in the hashtab. If the item contains a hashtab
5188 // it is added to ht_stack, if it contains a list it is added to
5189 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005190 todo = (int)cur_ht->ht_used;
5191 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5192 if (!HASHITEM_EMPTY(hi))
5193 {
5194 --todo;
5195 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5196 &ht_stack, list_stack);
5197 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005198 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005199
5200 if (ht_stack == NULL)
5201 break;
5202
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005203 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005204 cur_ht = ht_stack->ht;
5205 tempitem = ht_stack;
5206 ht_stack = ht_stack->prev;
5207 free(tempitem);
5208 }
5209
5210 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005211}
5212
Dominique Pelle748b3082022-01-08 12:41:16 +00005213#if defined(FEAT_LUA) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5214 || defined(PROTO)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005215/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005216 * Mark a dict and its items with "copyID".
5217 * Returns TRUE if setting references failed somehow.
5218 */
5219 int
5220set_ref_in_dict(dict_T *d, int copyID)
5221{
5222 if (d != NULL && d->dv_copyID != copyID)
5223 {
5224 d->dv_copyID = copyID;
5225 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
5226 }
5227 return FALSE;
5228}
Dominique Pelle748b3082022-01-08 12:41:16 +00005229#endif
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005230
5231/*
5232 * Mark a list and its items with "copyID".
5233 * Returns TRUE if setting references failed somehow.
5234 */
5235 int
5236set_ref_in_list(list_T *ll, int copyID)
5237{
5238 if (ll != NULL && ll->lv_copyID != copyID)
5239 {
5240 ll->lv_copyID = copyID;
5241 return set_ref_in_list_items(ll, copyID, NULL);
5242 }
5243 return FALSE;
5244}
5245
5246/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005247 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005248 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5249 *
5250 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005251 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005252 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005253set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005254{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005255 listitem_T *li;
5256 int abort = FALSE;
5257 list_T *cur_l;
5258 list_stack_T *list_stack = NULL;
5259 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005260
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005261 cur_l = l;
5262 for (;;)
5263 {
Bram Moolenaar50985eb2020-01-27 22:09:39 +01005264 if (!abort && cur_l->lv_first != &range_list_item)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005265 // Mark each item in the list. If the item contains a hashtab
5266 // it is added to ht_stack, if it contains a list it is added to
5267 // list_stack.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005268 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5269 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5270 ht_stack, &list_stack);
5271 if (list_stack == NULL)
5272 break;
5273
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005274 // take an item from the stack
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005275 cur_l = list_stack->list;
5276 tempitem = list_stack;
5277 list_stack = list_stack->prev;
5278 free(tempitem);
5279 }
5280
5281 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005282}
5283
5284/*
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +00005285 * Mark the partial in callback 'cb' with "copyID".
5286 */
5287 int
5288set_ref_in_callback(callback_T *cb, int copyID)
5289{
5290 typval_T tv;
5291
5292 if (cb->cb_name == NULL || *cb->cb_name == NUL || cb->cb_partial == NULL)
5293 return FALSE;
5294
5295 tv.v_type = VAR_PARTIAL;
5296 tv.vval.v_partial = cb->cb_partial;
5297 return set_ref_in_item(&tv, copyID, NULL, NULL);
5298}
5299
5300/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005301 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005302 * "list_stack" is used to add lists to be marked. Can be NULL.
5303 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5304 *
5305 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005306 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005307 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005308set_ref_in_item(
5309 typval_T *tv,
5310 int copyID,
5311 ht_stack_T **ht_stack,
5312 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005313{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005314 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005315
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005316 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005317 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005318 dict_T *dd = tv->vval.v_dict;
5319
Bram Moolenaara03f2332016-02-06 18:09:59 +01005320 if (dd != NULL && dd->dv_copyID != copyID)
5321 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005322 // Didn't see this dict yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005323 dd->dv_copyID = copyID;
5324 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005325 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005326 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5327 }
5328 else
5329 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005330 ht_stack_T *newitem = ALLOC_ONE(ht_stack_T);
5331
Bram Moolenaara03f2332016-02-06 18:09:59 +01005332 if (newitem == NULL)
5333 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005334 else
5335 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005336 newitem->ht = &dd->dv_hashtab;
5337 newitem->prev = *ht_stack;
5338 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005339 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005340 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005341 }
5342 }
5343 else if (tv->v_type == VAR_LIST)
5344 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005345 list_T *ll = tv->vval.v_list;
5346
Bram Moolenaara03f2332016-02-06 18:09:59 +01005347 if (ll != NULL && ll->lv_copyID != copyID)
5348 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005349 // Didn't see this list yet.
Bram Moolenaara03f2332016-02-06 18:09:59 +01005350 ll->lv_copyID = copyID;
5351 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005352 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02005353 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01005354 }
5355 else
5356 {
Bram Moolenaar51b6eb42020-08-22 15:19:18 +02005357 list_stack_T *newitem = ALLOC_ONE(list_stack_T);
5358
Bram Moolenaara03f2332016-02-06 18:09:59 +01005359 if (newitem == NULL)
5360 abort = TRUE;
5361 else
5362 {
5363 newitem->list = ll;
5364 newitem->prev = *list_stack;
5365 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005366 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005367 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005369 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005370 else if (tv->v_type == VAR_FUNC)
5371 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005372 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005373 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005374 else if (tv->v_type == VAR_PARTIAL)
5375 {
5376 partial_T *pt = tv->vval.v_partial;
5377 int i;
5378
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005379 if (pt != NULL && pt->pt_copyID != copyID)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005380 {
Bram Moolenaarb68b3462020-05-06 21:06:30 +02005381 // Didn't see this partial yet.
5382 pt->pt_copyID = copyID;
5383
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005384 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005385
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005386 if (pt->pt_dict != NULL)
5387 {
5388 typval_T dtv;
5389
5390 dtv.v_type = VAR_DICT;
5391 dtv.vval.v_dict = pt->pt_dict;
5392 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5393 }
5394
5395 for (i = 0; i < pt->pt_argc; ++i)
5396 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5397 ht_stack, list_stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +00005398 // pt_funcstack is handled in set_ref_in_funcstacks()
Bram Moolenaar0cdfb7c2022-09-17 15:44:52 +01005399 // pt_loopvars is handled in set_ref_in_loopvars()
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005400 }
5401 }
5402#ifdef FEAT_JOB_CHANNEL
5403 else if (tv->v_type == VAR_JOB)
5404 {
5405 job_T *job = tv->vval.v_job;
5406 typval_T dtv;
5407
5408 if (job != NULL && job->jv_copyID != copyID)
5409 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005410 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005411 if (job->jv_channel != NULL)
5412 {
5413 dtv.v_type = VAR_CHANNEL;
5414 dtv.vval.v_channel = job->jv_channel;
5415 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5416 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005417 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005418 {
5419 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005420 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005421 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5422 }
5423 }
5424 }
5425 else if (tv->v_type == VAR_CHANNEL)
5426 {
5427 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005428 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005429 typval_T dtv;
5430 jsonq_T *jq;
5431 cbq_T *cq;
5432
5433 if (ch != NULL && ch->ch_copyID != copyID)
5434 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005435 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005436 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005437 {
5438 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5439 jq = jq->jq_next)
5440 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5441 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5442 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005443 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005444 {
5445 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005446 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005447 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5448 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005449 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005450 {
5451 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005452 dtv.vval.v_partial =
5453 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005454 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5455 }
5456 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005457 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005458 {
5459 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005460 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005461 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5462 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005463 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005464 {
5465 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02005466 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005467 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5468 }
5469 }
5470 }
5471#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005472 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005473}
5474
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 * Return a string with the string representation of a variable.
5477 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005478 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005479 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005480 * When both "echo_style" and "composite_val" are FALSE, put quotes around
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005481 * strings as "string()", otherwise does not put quotes around strings, as
Bram Moolenaar35422f42017-08-05 16:33:56 +02005482 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005483 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5484 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005485 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005486 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005487 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005488echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005489 typval_T *tv,
5490 char_u **tofree,
5491 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005492 int copyID,
5493 int echo_style,
5494 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005495 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005497 static int recurse = 0;
5498 char_u *r = NULL;
5499
Bram Moolenaar33570922005-01-25 22:26:29 +00005500 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005501 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005502 if (!did_echo_string_emsg)
5503 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005504 // Only give this message once for a recursive call to avoid
5505 // flooding the user with errors. And stop iterating over lists
5506 // and dicts.
Bram Moolenaar8502c702014-06-17 12:51:16 +02005507 did_echo_string_emsg = TRUE;
Bram Moolenaara6f79292022-01-04 21:30:47 +00005508 emsg(_(e_variable_nested_too_deep_for_displaying));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005509 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005510 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005511 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005512 }
5513 ++recurse;
5514
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005515 switch (tv->v_type)
5516 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005517 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005518 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005519 {
5520 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005521 r = tv->vval.v_string;
5522 if (r == NULL)
5523 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005524 }
5525 else
5526 {
5527 *tofree = string_quote(tv->vval.v_string, FALSE);
5528 r = *tofree;
5529 }
5530 break;
5531
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005533 {
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005534 char_u buf[MAX_FUNC_NAME_LEN];
5535
5536 if (echo_style)
5537 {
LemonBoya5d35902022-04-29 21:15:02 +01005538 r = tv->vval.v_string == NULL ? (char_u *)"function()"
5539 : make_ufunc_name_readable(tv->vval.v_string,
Bram Moolenaara6c18d32022-03-31 20:02:56 +01005540 buf, MAX_FUNC_NAME_LEN);
5541 if (r == buf)
5542 {
5543 r = vim_strsave(buf);
5544 *tofree = r;
5545 }
5546 else
5547 *tofree = NULL;
5548 }
5549 else
5550 {
5551 *tofree = string_quote(tv->vval.v_string == NULL ? NULL
5552 : make_ufunc_name_readable(
5553 tv->vval.v_string, buf, MAX_FUNC_NAME_LEN),
5554 TRUE);
5555 r = *tofree;
5556 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005557 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005558 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005559
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005560 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005561 {
5562 partial_T *pt = tv->vval.v_partial;
5563 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005564 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005565 garray_T ga;
5566 int i;
5567 char_u *tf;
5568
5569 ga_init2(&ga, 1, 100);
5570 ga_concat(&ga, (char_u *)"function(");
5571 if (fname != NULL)
5572 {
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005573 // When using uf_name prepend "g:" for a global function.
Bram Moolenaare8a92b62021-12-09 17:44:01 +00005574 if (pt != NULL && pt->pt_name == NULL && fname[0] == '\''
Bram Moolenaarc4ec3382021-12-09 16:40:18 +00005575 && vim_isupper(fname[1]))
5576 {
5577 ga_concat(&ga, (char_u *)"'g:");
5578 ga_concat(&ga, fname + 1);
5579 }
5580 else
5581 ga_concat(&ga, fname);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005582 vim_free(fname);
5583 }
5584 if (pt != NULL && pt->pt_argc > 0)
5585 {
5586 ga_concat(&ga, (char_u *)", [");
5587 for (i = 0; i < pt->pt_argc; ++i)
5588 {
5589 if (i > 0)
5590 ga_concat(&ga, (char_u *)", ");
5591 ga_concat(&ga,
5592 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5593 vim_free(tf);
5594 }
5595 ga_concat(&ga, (char_u *)"]");
5596 }
5597 if (pt != NULL && pt->pt_dict != NULL)
5598 {
5599 typval_T dtv;
5600
5601 ga_concat(&ga, (char_u *)", ");
5602 dtv.v_type = VAR_DICT;
5603 dtv.vval.v_dict = pt->pt_dict;
5604 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5605 vim_free(tf);
5606 }
Bram Moolenaar2de53712021-12-19 11:06:35 +00005607 // terminate with ')' and a NUL
5608 ga_concat_len(&ga, (char_u *)")", 2);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005609
5610 *tofree = ga.ga_data;
5611 r = *tofree;
5612 break;
5613 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005614
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005615 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005616 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005617 break;
5618
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005620 if (tv->vval.v_list == NULL)
5621 {
Bram Moolenaardb950e42020-04-22 19:13:19 +02005622 // NULL list is equivalent to empty list.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005623 *tofree = NULL;
Bram Moolenaardb950e42020-04-22 19:13:19 +02005624 r = (char_u *)"[]";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005625 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005626 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5627 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005628 {
5629 *tofree = NULL;
5630 r = (char_u *)"[...]";
5631 }
5632 else
5633 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005634 int old_copyID = tv->vval.v_list->lv_copyID;
5635
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005636 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005637 *tofree = list2string(tv, copyID, restore_copyID);
5638 if (restore_copyID)
5639 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005640 r = *tofree;
5641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005642 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005643
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005645 if (tv->vval.v_dict == NULL)
5646 {
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005647 // NULL dict is equivalent to empty dict.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005648 *tofree = NULL;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005649 r = (char_u *)"{}";
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005650 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005651 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5652 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005653 {
5654 *tofree = NULL;
5655 r = (char_u *)"{...}";
5656 }
5657 else
5658 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005659 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02005660
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005661 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005662 *tofree = dict2string(tv, copyID, restore_copyID);
5663 if (restore_copyID)
5664 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005665 r = *tofree;
5666 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005667 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005669 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005670 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02005671 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005672 case VAR_VOID:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005673 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005674 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005675 break;
5676
Bram Moolenaar835dc632016-02-07 14:27:38 +01005677 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005678 case VAR_CHANNEL:
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005679#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare9a41262005-01-15 22:18:47 +00005680 *tofree = NULL;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005681 r = tv->v_type == VAR_JOB ? job_to_string_buf(tv, numbuf)
5682 : channel_to_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02005683 if (composite_val)
5684 {
5685 *tofree = string_quote(r, FALSE);
5686 r = *tofree;
5687 }
Bram Moolenaarf5bfa8f2021-06-06 12:07:54 +02005688#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005690
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005691 case VAR_INSTR:
5692 *tofree = NULL;
5693 r = (char_u *)"instructions";
5694 break;
5695
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005696 case VAR_FLOAT:
5697 *tofree = NULL;
5698 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
5699 r = numbuf;
5700 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005701
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01005702 case VAR_BOOL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005703 case VAR_SPECIAL:
5704 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005705 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005706 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005707 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005708
Bram Moolenaar8502c702014-06-17 12:51:16 +02005709 if (--recurse == 0)
5710 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00005711 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005712}
5713
5714/*
5715 * Return a string with the string representation of a variable.
5716 * If the memory is allocated "tofree" is set to it, otherwise NULL.
5717 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005718 * Does not put quotes around strings, as ":echo" displays values.
5719 * When "copyID" is not NULL replace recursive lists and dicts with "...".
5720 * May return NULL.
5721 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005722 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005723echo_string(
5724 typval_T *tv,
5725 char_u **tofree,
5726 char_u *numbuf,
5727 int copyID)
5728{
5729 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
5730}
5731
5732/*
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005733 * Convert the specified byte index of line 'lnum' in buffer 'buf' to a
5734 * character index. Works only for loaded buffers. Returns -1 on failure.
Bram Moolenaar91458462021-01-13 20:08:38 +01005735 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005736 */
5737 int
5738buf_byteidx_to_charidx(buf_T *buf, int lnum, int byteidx)
5739{
5740 char_u *str;
Bram Moolenaar91458462021-01-13 20:08:38 +01005741 char_u *t;
5742 int count;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005743
5744 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5745 return -1;
5746
5747 if (lnum > buf->b_ml.ml_line_count)
5748 lnum = buf->b_ml.ml_line_count;
5749
5750 str = ml_get_buf(buf, lnum, FALSE);
5751 if (str == NULL)
5752 return -1;
5753
5754 if (*str == NUL)
Bram Moolenaar91458462021-01-13 20:08:38 +01005755 return 0;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005756
Bram Moolenaar91458462021-01-13 20:08:38 +01005757 // count the number of characters
5758 t = str;
5759 for (count = 0; *t != NUL && t <= str + byteidx; count++)
5760 t += mb_ptr2len(t);
5761
5762 // In insert mode, when the cursor is at the end of a non-empty line,
5763 // byteidx points to the NUL character immediately past the end of the
5764 // string. In this case, add one to the character count.
5765 if (*t == NUL && byteidx != 0 && t == str + byteidx)
5766 count++;
5767
5768 return count - 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005769}
5770
5771/*
5772 * Convert the specified character index of line 'lnum' in buffer 'buf' to a
Bram Moolenaar91458462021-01-13 20:08:38 +01005773 * byte index. Works only for loaded buffers. Returns -1 on failure.
5774 * The index of the first byte and the first character is zero.
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005775 */
5776 int
5777buf_charidx_to_byteidx(buf_T *buf, int lnum, int charidx)
5778{
5779 char_u *str;
5780 char_u *t;
5781
5782 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5783 return -1;
5784
5785 if (lnum > buf->b_ml.ml_line_count)
5786 lnum = buf->b_ml.ml_line_count;
5787
5788 str = ml_get_buf(buf, lnum, FALSE);
5789 if (str == NULL)
5790 return -1;
5791
5792 // Convert the character offset to a byte offset
5793 t = str;
5794 while (*t != NUL && --charidx > 0)
5795 t += mb_ptr2len(t);
5796
Bram Moolenaar91458462021-01-13 20:08:38 +01005797 return t - str;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005798}
5799
5800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005802 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005804 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005805var2fpos(
5806 typval_T *varp,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005807 int dollar_lnum, // TRUE when $ is last line
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005808 int *fnum, // set to fnum for '0, 'A, etc.
5809 int charcol) // return character column
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005811 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005813 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005815 // Argument can be [lnum, col, coladd].
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005816 if (varp->v_type == VAR_LIST)
5817 {
5818 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005819 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005820 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005821 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005822
5823 l = varp->vval.v_list;
5824 if (l == NULL)
5825 return NULL;
5826
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005827 // Get the line number
Bram Moolenaara5525202006-03-02 22:52:09 +00005828 pos.lnum = list_find_nr(l, 0L, &error);
5829 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005830 return NULL; // invalid line number
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005831 if (charcol)
5832 len = (long)mb_charlen(ml_get(pos.lnum));
5833 else
5834 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005835
Bram Moolenaarec65d772020-08-20 22:29:12 +02005836 // Get the column number
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005837 // We accept "$" for the column number: last column.
Bram Moolenaar477933c2007-07-17 14:32:23 +00005838 li = list_find(l, 1L);
5839 if (li != NULL && li->li_tv.v_type == VAR_STRING
5840 && li->li_tv.vval.v_string != NULL
5841 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
Bram Moolenaarec65d772020-08-20 22:29:12 +02005842 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005843 pos.col = len + 1;
Bram Moolenaarec65d772020-08-20 22:29:12 +02005844 }
5845 else
5846 {
5847 pos.col = list_find_nr(l, 1L, &error);
5848 if (error)
5849 return NULL;
5850 }
Bram Moolenaar477933c2007-07-17 14:32:23 +00005851
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005852 // Accept a position up to the NUL after the line.
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005853 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005854 return NULL; // invalid column number
Bram Moolenaara5525202006-03-02 22:52:09 +00005855 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005856
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005857 // Get the virtual offset. Defaults to zero.
Bram Moolenaara5525202006-03-02 22:52:09 +00005858 pos.coladd = list_find_nr(l, 2L, &error);
5859 if (error)
5860 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005861
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005862 return &pos;
5863 }
5864
Bram Moolenaarc5809432021-03-27 21:23:30 +01005865 if (in_vim9script() && check_for_string_arg(varp, 0) == FAIL)
5866 return NULL;
5867
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005868 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005869 if (name == NULL)
5870 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005871
5872 pos.lnum = 0;
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005873 if (name[0] == '.' && (!in_vim9script() || name[1] == NUL))
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005874 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005875 // cursor
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005876 pos = curwin->w_cursor;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005877 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005878 else if (name[0] == 'v' && name[1] == NUL)
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005879 {
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005880 // Visual start
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005881 if (VIsual_active)
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005882 pos = VIsual;
5883 else
5884 pos = curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005885 }
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005886 else if (name[0] == '\'' && (!in_vim9script()
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005887 || (name[1] != NUL && name[2] == NUL)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 {
Bram Moolenaar8dac2ac2021-12-27 20:57:06 +00005889 // mark
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005890 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5892 return NULL;
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005893 pos = *pp;
5894 }
5895 if (pos.lnum != 0)
5896 {
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005897 if (charcol)
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +01005898 pos.col = buf_byteidx_to_charidx(curbuf, pos.lnum, pos.col);
5899 return &pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005901
Bram Moolenaara5525202006-03-02 22:52:09 +00005902 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005903
Bram Moolenaar477933c2007-07-17 14:32:23 +00005904 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005905 {
5906 pos.col = 0;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005907 if (name[1] == '0') // "w0": first visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005908 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005909 update_topline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005910 // In silent Ex mode topline is zero, but that's not a valid line
5911 // number; use one instead.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005912 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005913 return &pos;
5914 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005915 else if (name[1] == '$') // "w$": last visible line
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005916 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005917 validate_botline();
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005918 // In silent Ex mode botline is zero, return zero then.
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005919 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005920 return &pos;
5921 }
5922 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005923 else if (name[0] == '$') // last column or line
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005925 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 {
5927 pos.lnum = curbuf->b_ml.ml_line_count;
5928 pos.col = 0;
5929 }
5930 else
5931 {
5932 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005933 if (charcol)
5934 pos.col = (colnr_T)mb_charlen(ml_get_curline());
5935 else
5936 pos.col = (colnr_T)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 }
5938 return &pos;
5939 }
Bram Moolenaar0f1227f2021-07-11 16:01:58 +02005940 if (in_vim9script())
5941 semsg(_(e_invalid_value_for_line_number_str), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 return NULL;
5943}
5944
5945/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005946 * Convert list in "arg" into a position and optional file number.
5947 * When "fnump" is NULL there is no file number, only 3 items.
5948 * Note that the column is passed on as-is, the caller may want to decrement
5949 * it to use 1 for the first column.
5950 * Return FAIL when conversion is not possible, doesn't check the position for
5951 * validity.
5952 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005954list2fpos(
5955 typval_T *arg,
5956 pos_T *posp,
5957 int *fnump,
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005958 colnr_T *curswantp,
5959 int charcol)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005960{
5961 list_T *l = arg->vval.v_list;
5962 long i = 0;
5963 long n;
5964
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005965 // List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5966 // there when "fnump" isn't NULL; "coladd" and "curswant" are optional.
Bram Moolenaarbde35262006-07-23 20:12:24 +00005967 if (arg->v_type != VAR_LIST
5968 || l == NULL
5969 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005970 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005971 return FAIL;
5972
5973 if (fnump != NULL)
5974 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005975 n = list_find_nr(l, i++, NULL); // fnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005976 if (n < 0)
5977 return FAIL;
5978 if (n == 0)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005979 n = curbuf->b_fnum; // current buffer
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005980 *fnump = n;
5981 }
5982
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005983 n = list_find_nr(l, i++, NULL); // lnum
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005984 if (n < 0)
5985 return FAIL;
5986 posp->lnum = n;
5987
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01005988 n = list_find_nr(l, i++, NULL); // col
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005989 if (n < 0)
5990 return FAIL;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01005991 // If character position is specified, then convert to byte position
5992 if (charcol)
5993 {
5994 buf_T *buf;
5995
5996 // Get the text for the specified line in a loaded buffer
5997 buf = buflist_findnr(fnump == NULL ? curbuf->b_fnum : *fnump);
5998 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
5999 return FAIL;
6000
Bram Moolenaar91458462021-01-13 20:08:38 +01006001 n = buf_charidx_to_byteidx(buf, posp->lnum, n) + 1;
Bram Moolenaar6f02b002021-01-10 20:22:54 +01006002 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006003 posp->col = n;
6004
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006005 n = list_find_nr(l, i, NULL); // off
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006006 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006007 posp->coladd = 0;
6008 else
6009 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006010
Bram Moolenaar493c1782014-05-28 14:34:46 +02006011 if (curswantp != NULL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006012 *curswantp = list_find_nr(l, i + 1, NULL); // curswant
Bram Moolenaar493c1782014-05-28 14:34:46 +02006013
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006014 return OK;
6015}
6016
6017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 * Get the length of an environment variable name.
6019 * Advance "arg" to the first character after the name.
6020 * Return 0 for error.
6021 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
6025 char_u *p;
6026 int len;
6027
6028 for (p = *arg; vim_isIDc(*p); ++p)
6029 ;
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006030 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 return 0;
6032
6033 len = (int)(p - *arg);
6034 *arg = p;
6035 return len;
6036}
6037
6038/*
6039 * Get the length of the name of a function or internal variable.
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006040 * "arg" is advanced to after the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 * Return 0 if something is wrong.
6042 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006043 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006044get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 char_u *p;
6047 int len;
6048
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006049 // Find the end of the name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006051 {
6052 if (*p == ':')
6053 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006054 // "s:" is start of "s:var", but "n:" is not and can be used in
6055 // slice "[n:]". Also "xx:" is not a namespace.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006056 len = (int)(p - *arg);
6057 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6058 || len > 1)
6059 break;
6060 }
6061 }
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006062 if (p == *arg) // no name found
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 return 0;
6064
6065 len = (int)(p - *arg);
Bram Moolenaarbb1b5e22020-08-05 10:53:21 +02006066 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068 return len;
6069}
6070
6071/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006072 * Get the length of the name of a variable or function.
6073 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006075 * Return -1 if curly braces expansion failed.
6076 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 * If the name contains 'magic' {}'s, expand them and return the
6078 * expanded name in an allocated string via 'alias' - caller must free.
6079 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006081get_name_len(
6082 char_u **arg,
6083 char_u **alias,
6084 int evaluate,
6085 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086{
6087 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 char_u *p;
6089 char_u *expr_start;
6090 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006092 *alias = NULL; // default to no alias
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093
6094 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6095 && (*arg)[2] == (int)KE_SNR)
6096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006097 // hard coded <SNR>, already translated
Bram Moolenaar071d4272004-06-13 20:20:40 +00006098 *arg += 3;
6099 return get_id_len(arg) + 3;
6100 }
6101 len = eval_fname_script(*arg);
6102 if (len > 0)
6103 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006104 // literal "<SID>", "s:" or "<SNR>"
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 *arg += len;
6106 }
6107
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006109 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006111 p = find_name_end(*arg, &expr_start, &expr_end,
6112 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113 if (expr_start != NULL)
6114 {
6115 char_u *temp_string;
6116
6117 if (!evaluate)
6118 {
6119 len += (int)(p - *arg);
6120 *arg = skipwhite(p);
6121 return len;
6122 }
6123
6124 /*
6125 * Include any <SID> etc in the expanded string:
6126 * Thus the -len here.
6127 */
6128 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6129 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006130 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006131 *alias = temp_string;
6132 *arg = skipwhite(p);
6133 return (int)STRLEN(temp_string);
6134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135
6136 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006137 // Only give an error when there is something, otherwise it will be
6138 // reported at a higher level.
6139 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006140 semsg(_(e_invalid_expression_str), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141
6142 return len;
6143}
6144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006145/*
6146 * Find the end of a variable or function name, taking care of magic braces.
6147 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6148 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006149 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006150 * Return a pointer to just after the name. Equal to "arg" if there is no
6151 * valid name.
6152 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006153 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006154find_name_end(
6155 char_u *arg,
6156 char_u **expr_start,
6157 char_u **expr_end,
6158 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006160 int mb_nest = 0;
6161 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006163 int len;
Bram Moolenaareb6880b2020-07-12 17:07:05 +02006164 int vim9script = in_vim9script();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006166 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006168 *expr_start = NULL;
6169 *expr_end = NULL;
6170 }
6171
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006172 // Quick check for valid starting character.
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006173 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg)
6174 && (*arg != '{' || vim9script))
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006175 return arg;
6176
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006177 for (p = arg; *p != NUL
6178 && (eval_isnamec(*p)
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006179 || (*p == '{' && !vim9script)
Bram Moolenaar63be3d42020-07-23 13:11:37 +02006180 || ((flags & FNE_INCL_BR) && (*p == '['
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006181 || (*p == '.' && eval_isdictc(p[1]))))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006182 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006183 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006184 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006185 if (*p == '\'')
6186 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006187 // skip over 'string' to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006188 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006189 ;
6190 if (*p == NUL)
6191 break;
6192 }
6193 else if (*p == '"')
6194 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006195 // skip over "str\"ing" to avoid counting [ and ] inside it.
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006196 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006197 if (*p == '\\' && p[1] != NUL)
6198 ++p;
6199 if (*p == NUL)
6200 break;
6201 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006202 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6203 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006204 // "s:" is start of "s:var", but "n:" is not and can be used in
6205 // slice "[n:]". Also "xx:" is not a namespace. But {ns}: is.
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006206 len = (int)(p - arg);
6207 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006208 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006209 break;
6210 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006211
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006212 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006214 if (*p == '[')
6215 ++br_nest;
6216 else if (*p == ']')
6217 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006219
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02006220 if (br_nest == 0 && !vim9script)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006222 if (*p == '{')
6223 {
6224 mb_nest++;
6225 if (expr_start != NULL && *expr_start == NULL)
6226 *expr_start = p;
6227 }
6228 else if (*p == '}')
6229 {
6230 mb_nest--;
6231 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6232 *expr_end = p;
6233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
6236
6237 return p;
6238}
6239
6240/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006241 * Expands out the 'magic' {}'s in a variable/function name.
6242 * Note that this can call itself recursively, to deal with
6243 * constructs like foo{bar}{baz}{bam}
6244 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6245 * "in_start" ^
6246 * "expr_start" ^
6247 * "expr_end" ^
6248 * "in_end" ^
6249 *
6250 * Returns a new allocated string, which the caller must free.
6251 * Returns NULL for failure.
6252 */
6253 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006254make_expanded_name(
6255 char_u *in_start,
6256 char_u *expr_start,
6257 char_u *expr_end,
6258 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006259{
6260 char_u c1;
6261 char_u *retval = NULL;
6262 char_u *temp_result;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006263
6264 if (expr_end == NULL || in_end == NULL)
6265 return NULL;
6266 *expr_start = NUL;
6267 *expr_end = NUL;
6268 c1 = *in_end;
6269 *in_end = NUL;
6270
Bram Moolenaarb171fb12020-06-24 20:34:03 +02006271 temp_result = eval_to_string(expr_start + 1, FALSE);
6272 if (temp_result != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006273 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02006274 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
6275 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006276 if (retval != NULL)
6277 {
6278 STRCPY(retval, in_start);
6279 STRCAT(retval, temp_result);
6280 STRCAT(retval, expr_end + 1);
6281 }
6282 }
6283 vim_free(temp_result);
6284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006285 *in_end = c1; // put char back for error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006286 *expr_start = '{';
6287 *expr_end = '}';
6288
6289 if (retval != NULL)
6290 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006291 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006292 if (expr_start != NULL)
6293 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006294 // Further expansion!
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006295 temp_result = make_expanded_name(retval, expr_start,
6296 expr_end, temp_result);
6297 vim_free(retval);
6298 retval = temp_result;
6299 }
6300 }
6301
6302 return retval;
6303}
6304
6305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006307 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006308 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006310eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006312 return ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006313}
6314
6315/*
6316 * Return TRUE if character "c" can be used as the first character in a
6317 * variable or function name (excluding '{' and '}').
6318 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006320eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006321{
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006322 return ASCII_ISALPHA(c) || c == '_';
6323}
6324
6325/*
6326 * Return TRUE if character "c" can be used as the first character of a
6327 * dictionary key.
6328 */
6329 int
6330eval_isdictc(int c)
6331{
6332 return ASCII_ISALNUM(c) || c == '_';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333}
6334
6335/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02006336 * Handle:
6337 * - expr[expr], expr[expr:expr] subscript
6338 * - ".name" lookup
6339 * - function call with Funcref variable: func(expr)
6340 * - method call: var->method()
6341 *
6342 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006343 * "name_start" points to a variable before the subscript or is NULL.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006344 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006345 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006346handle_subscript(
6347 char_u **arg,
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006348 char_u *name_start,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006349 typval_T *rettv,
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006350 evalarg_T *evalarg,
Bram Moolenaar0b1cd522020-06-27 13:11:50 +02006351 int verbose) // give error messages
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006352{
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006353 int evaluate = evalarg != NULL
6354 && (evalarg->eval_flags & EVAL_EVALUATE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006355 int ret = OK;
6356 dict_T *selfdict = NULL;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006357 int check_white = TRUE;
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006358 int getnext;
6359 char_u *p;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006360
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006361 while (ret == OK)
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006362 {
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006363 // When at the end of the line and ".name" or "->{" or "->X" follows in
6364 // the next line then consume the line break.
6365 p = eval_next_non_blank(*arg, evalarg, &getnext);
6366 if (getnext
Bram Moolenaarb13ab992020-07-27 21:43:28 +02006367 && ((rettv->v_type == VAR_DICT && *p == '.' && eval_isdictc(p[1]))
Bram Moolenaara7330422021-06-08 20:46:45 +02006368 || (p[0] == '-' && p[1] == '>' && (p[2] == '{'
6369 || ASCII_ISALPHA(in_vim9script() ? *skipwhite(p + 2)
6370 : p[2])))))
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006371 {
Bram Moolenaare442d592022-05-05 12:20:28 +01006372 *arg = eval_next_line(*arg, evalarg);
Bram Moolenaaraeb2bdd2020-08-18 22:32:03 +02006373 p = *arg;
Bram Moolenaar442af2f2020-07-03 21:09:52 +02006374 check_white = FALSE;
6375 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006376
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006377 if (rettv->v_type == VAR_ANY)
6378 {
6379 char_u *exp_name;
6380 int cc;
6381 int idx;
LemonBoyaf59e342022-04-24 21:55:00 +01006382 ufunc_T *ufunc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006383 type_T *type;
6384
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006385 // Found script from "import {name} as name", script item name must
Bram Moolenaar5d982692022-01-12 15:15:27 +00006386 // follow. "rettv->vval.v_number" has the script ID.
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006387 if (**arg != '.')
6388 {
6389 if (verbose)
Bram Moolenaar32884ad2022-01-07 12:45:29 +00006390 semsg(_(e_expected_dot_after_name_str),
6391 name_start != NULL ? name_start: *arg);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006392 ret = FAIL;
6393 break;
6394 }
6395 ++*arg;
6396 if (IS_WHITE_OR_NUL(**arg))
6397 {
6398 if (verbose)
6399 emsg(_(e_no_white_space_allowed_after_dot));
6400 ret = FAIL;
6401 break;
6402 }
6403
6404 // isolate the name
6405 exp_name = *arg;
6406 while (eval_isnamec(**arg))
6407 ++*arg;
6408 cc = **arg;
6409 **arg = NUL;
6410
6411 idx = find_exported(rettv->vval.v_number, exp_name, &ufunc, &type,
Bram Moolenaar7f9a5a62022-09-23 16:37:18 +01006412 evalarg == NULL ? NULL : evalarg->eval_cctx,
6413 evalarg == NULL ? NULL : evalarg->eval_cstack, verbose);
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006414 **arg = cc;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006415
6416 if (idx < 0 && ufunc == NULL)
6417 {
6418 ret = FAIL;
6419 break;
6420 }
6421 if (idx >= 0)
6422 {
6423 scriptitem_T *si = SCRIPT_ITEM(rettv->vval.v_number);
6424 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data) + idx;
6425
6426 copy_tv(sv->sv_tv, rettv);
6427 }
6428 else
6429 {
6430 rettv->v_type = VAR_FUNC;
6431 rettv->vval.v_string = vim_strsave(ufunc->uf_name);
6432 }
Bram Moolenaard5f400c2022-01-06 21:10:28 +00006433 continue;
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01006434 }
6435
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006436 if ((**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
6437 || rettv->v_type == VAR_PARTIAL))
6438 && (!check_white || !VIM_ISWHITE(*(*arg - 1))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006439 {
Bram Moolenaare6b53242020-07-01 17:28:33 +02006440 ret = call_func_rettv(arg, evalarg, rettv, evaluate,
6441 selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01006442
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02006443 // Stop the expression evaluation when immediately aborting on
6444 // error, or when an interrupt occurred or an exception was thrown
6445 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006446 if (aborting())
6447 {
6448 if (ret == OK)
6449 clear_tv(rettv);
6450 ret = FAIL;
6451 }
6452 dict_unref(selfdict);
6453 selfdict = NULL;
6454 }
Bram Moolenaar9d489562020-07-30 20:08:50 +02006455 else if (p[0] == '-' && p[1] == '>')
Bram Moolenaarac92e252019-08-03 21:58:38 +02006456 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006457 if (in_vim9script())
6458 *arg = skipwhite(p + 2);
6459 else
6460 *arg = p + 2;
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006461 if (ret == OK)
6462 {
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006463 if (VIM_ISWHITE(**arg))
6464 {
Bram Moolenaar3a846e62022-01-01 16:21:00 +00006465 emsg(_(e_no_white_space_allowed_before_parenthesis));
Bram Moolenaar0820f4d2021-05-15 20:06:58 +02006466 ret = FAIL;
6467 }
6468 else if ((**arg == '{' && !in_vim9script()) || **arg == '(')
Bram Moolenaar2949cfd2020-12-31 21:28:47 +01006469 // expr->{lambda}() or expr->(lambda)()
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006470 ret = eval_lambda(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006471 else
6472 // expr->name()
Bram Moolenaare6b53242020-07-01 17:28:33 +02006473 ret = eval_method(arg, rettv, evalarg, verbose);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02006474 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02006475 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006476 // "." is ".name" lookup when we found a dict or when evaluating and
6477 // scriptversion is at least 2, where string concatenation is "..".
6478 else if (**arg == '['
6479 || (**arg == '.' && (rettv->v_type == VAR_DICT
6480 || (!evaluate
6481 && (*arg)[1] != '.'
Bram Moolenaardd9de502021-08-15 13:49:42 +02006482 && !in_old_script(2)))))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006483 {
6484 dict_unref(selfdict);
6485 if (rettv->v_type == VAR_DICT)
6486 {
6487 selfdict = rettv->vval.v_dict;
6488 if (selfdict != NULL)
6489 ++selfdict->dv_refcount;
6490 }
6491 else
6492 selfdict = NULL;
Bram Moolenaare40fbc22020-06-27 18:06:45 +02006493 if (eval_index(arg, rettv, evalarg, verbose) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006494 {
6495 clear_tv(rettv);
6496 ret = FAIL;
6497 }
6498 }
Bram Moolenaar7a4b8982020-07-08 17:36:21 +02006499 else
6500 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006501 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006502
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006503 // Turn "dict.Func" into a partial for "Func" bound to "dict".
6504 // Don't do this when "Func" is already a partial that was bound
6505 // explicitly (pt_auto is FALSE).
Bram Moolenaar1d429612016-05-24 15:44:17 +02006506 if (selfdict != NULL
6507 && (rettv->v_type == VAR_FUNC
6508 || (rettv->v_type == VAR_PARTIAL
6509 && (rettv->vval.v_partial->pt_auto
6510 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006511 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01006512
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006513 dict_unref(selfdict);
6514 return ret;
6515}
6516
6517/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006518 * Make a copy of an item.
6519 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar381692b2022-02-02 20:01:27 +00006520 * "top" is TRUE for the toplevel of copy().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006521 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6522 * reference to an already copied list/dict can be used.
6523 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006525 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006526item_copy(
6527 typval_T *from,
6528 typval_T *to,
6529 int deep,
Bram Moolenaar381692b2022-02-02 20:01:27 +00006530 int top,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006531 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006532{
6533 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006534 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006535
Bram Moolenaar33570922005-01-25 22:26:29 +00006536 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006537 {
Bram Moolenaara6f79292022-01-04 21:30:47 +00006538 emsg(_(e_variable_nested_too_deep_for_making_copy));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006539 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006540 }
6541 ++recurse;
6542
6543 switch (from->v_type)
6544 {
6545 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006546 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006547 case VAR_STRING:
6548 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006549 case VAR_PARTIAL:
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +01006550 case VAR_BOOL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006551 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006552 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006553 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006554 case VAR_INSTR:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006555 copy_tv(from, to);
6556 break;
6557 case VAR_LIST:
6558 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006559 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006560 if (from->vval.v_list == NULL)
6561 to->vval.v_list = NULL;
6562 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6563 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006564 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006565 to->vval.v_list = from->vval.v_list->lv_copylist;
6566 ++to->vval.v_list->lv_refcount;
6567 }
6568 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006569 to->vval.v_list = list_copy(from->vval.v_list,
6570 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006571 if (to->vval.v_list == NULL)
6572 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006573 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006574 case VAR_BLOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006575 ret = blob_copy(from->vval.v_blob, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006576 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006577 case VAR_DICT:
6578 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006579 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 if (from->vval.v_dict == NULL)
6581 to->vval.v_dict = NULL;
6582 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6583 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006584 // use the copy made earlier
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006585 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6586 ++to->vval.v_dict->dv_refcount;
6587 }
6588 else
Bram Moolenaar381692b2022-02-02 20:01:27 +00006589 to->vval.v_dict = dict_copy(from->vval.v_dict,
6590 deep, top, copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006591 if (to->vval.v_dict == NULL)
6592 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006593 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006594 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006595 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006596 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +01006597 internal_error_no_abort("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006598 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006599 }
6600 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006602}
6603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006604 void
6605echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr)
6606{
6607 char_u *tofree;
6608 char_u numbuf[NUMBUFLEN];
6609 char_u *p = echo_string(rettv, &tofree, numbuf, get_copyID());
6610
6611 if (*atstart)
6612 {
6613 *atstart = FALSE;
6614 // Call msg_start() after eval1(), evaluating the expression
6615 // may cause a message to appear.
6616 if (with_space)
6617 {
6618 // Mark the saved text as finishing the line, so that what
6619 // follows is displayed on a new line when scrolling back
6620 // at the more prompt.
6621 msg_sb_eol();
6622 msg_start();
6623 }
6624 }
6625 else if (with_space)
6626 msg_puts_attr(" ", echo_attr);
6627
6628 if (p != NULL)
6629 for ( ; *p != NUL && !got_int; ++p)
6630 {
6631 if (*p == '\n' || *p == '\r' || *p == TAB)
6632 {
6633 if (*p != TAB && *needclr)
6634 {
6635 // remove any text still there from the command
6636 msg_clr_eos();
6637 *needclr = FALSE;
6638 }
6639 msg_putchar_attr(*p, echo_attr);
6640 }
6641 else
6642 {
6643 if (has_mbyte)
6644 {
6645 int i = (*mb_ptr2len)(p);
6646
6647 (void)msg_outtrans_len_attr(p, i, echo_attr);
6648 p += i - 1;
6649 }
6650 else
6651 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6652 }
6653 }
6654 vim_free(tofree);
6655}
6656
Bram Moolenaare9a41262005-01-15 22:18:47 +00006657/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006658 * ":echo expr1 ..." print each argument separated with a space, add a
6659 * newline at the end.
6660 * ":echon expr1 ..." print each argument plain.
6661 */
6662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006663ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664{
6665 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006666 typval_T rettv;
Bram Moolenaar68db9962021-05-09 23:19:22 +02006667 char_u *arg_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 int needclr = TRUE;
6669 int atstart = TRUE;
Bram Moolenaar76a63452018-11-28 20:38:37 +01006670 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006671 int called_emsg_before = called_emsg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006672 evalarg_T evalarg;
6673
Bram Moolenaare707c882020-07-01 13:07:37 +02006674 fill_evalarg_from_eap(&evalarg, eap, eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
6676 if (eap->skip)
6677 ++emsg_skip;
Bram Moolenaar7a092242020-04-16 22:10:49 +02006678 while ((!ends_excmd2(eap->cmd, arg) || *arg == '"') && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006680 // If eval1() causes an error message the text from the command may
6681 // still need to be cleared. E.g., "echo 22,44".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006682 need_clr_eos = needclr;
6683
Bram Moolenaar68db9962021-05-09 23:19:22 +02006684 arg_start = arg;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +02006685 if (eval1(&arg, &rettv, &evalarg) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686 {
6687 /*
6688 * Report the invalid expression unless the expression evaluation
6689 * has been cancelled due to an aborting error, an interrupt, or an
6690 * exception.
6691 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006692 if (!aborting() && did_emsg == did_emsg_before
6693 && called_emsg == called_emsg_before)
Bram Moolenaar108010a2021-06-27 22:03:33 +02006694 semsg(_(e_invalid_expression_str), arg_start);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006695 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 break;
6697 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006698 need_clr_eos = FALSE;
6699
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 if (!eap->skip)
Bram Moolenaar68db9962021-05-09 23:19:22 +02006701 {
6702 if (rettv.v_type == VAR_VOID)
6703 {
6704 semsg(_(e_expression_does_not_result_in_value_str), arg_start);
6705 break;
6706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006707 echo_one(&rettv, eap->cmdidx == CMD_echo, &atstart, &needclr);
Bram Moolenaar68db9962021-05-09 23:19:22 +02006708 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006710 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 arg = skipwhite(arg);
6712 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02006713 set_nextcmd(eap, arg);
Bram Moolenaarfaf86262020-06-27 23:07:36 +02006714 clear_evalarg(&evalarg, eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716 if (eap->skip)
6717 --emsg_skip;
6718 else
6719 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006720 // remove text that may still be there from the command
Bram Moolenaar071d4272004-06-13 20:20:40 +00006721 if (needclr)
6722 msg_clr_eos();
6723 if (eap->cmdidx == CMD_echo)
6724 msg_end();
6725 }
6726}
6727
6728/*
6729 * ":echohl {name}".
6730 */
6731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006732ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006734 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735}
6736
6737/*
Bram Moolenaarda6c0332019-09-01 16:01:30 +02006738 * Returns the :echo attribute
6739 */
6740 int
6741get_echo_attr(void)
6742{
6743 return echo_attr;
6744}
6745
6746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 * ":execute expr1 ..." execute the result of an expression.
6748 * ":echomsg expr1 ..." Print a message
Bram Moolenaar37fef162022-08-29 18:16:32 +01006749 * ":echowindow expr1 ..." Print a message in the messages window
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 * ":echoerr expr1 ..." Print an error
Bram Moolenaar4c868302021-03-22 16:19:45 +01006751 * ":echoconsole expr1 ..." Print a message on stdout
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 * Each gets spaces around each argument and a newline at the end for
6753 * echo commands
6754 */
6755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006756ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757{
6758 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006759 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 int ret = OK;
6761 char_u *p;
6762 garray_T ga;
6763 int len;
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006764 long start_lnum = SOURCING_LNUM;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765
6766 ga_init2(&ga, 1, 80);
6767
6768 if (eap->skip)
6769 ++emsg_skip;
Bram Moolenaar4a8d9f22020-04-16 22:54:32 +02006770 while (!ends_excmd2(eap->cmd, arg) || *arg == '"')
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 {
Bram Moolenaar47e880d2020-06-30 22:02:02 +02006772 ret = eval1_emsg(&arg, &rettv, eap);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006773 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775
6776 if (!eap->skip)
6777 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006778 char_u buf[NUMBUFLEN];
6779
6780 if (eap->cmdidx == CMD_execute)
Bram Moolenaarb6625912020-01-08 20:09:01 +01006781 {
6782 if (rettv.v_type == VAR_CHANNEL || rettv.v_type == VAR_JOB)
6783 {
Bram Moolenaar68db9962021-05-09 23:19:22 +02006784 semsg(_(e_using_invalid_value_as_string_str),
6785 vartype_name(rettv.v_type));
Bram Moolenaarb6625912020-01-08 20:09:01 +01006786 p = NULL;
6787 }
6788 else
6789 p = tv_get_string_buf(&rettv, buf);
6790 }
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006791 else
6792 p = tv_stringify(&rettv, buf);
Bram Moolenaar9db2afe2020-01-08 18:56:20 +01006793 if (p == NULL)
6794 {
6795 clear_tv(&rettv);
6796 ret = FAIL;
6797 break;
6798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 len = (int)STRLEN(p);
6800 if (ga_grow(&ga, len + 2) == FAIL)
6801 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006802 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 ret = FAIL;
6804 break;
6805 }
6806 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 ga.ga_len += len;
6810 }
6811
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006812 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813 arg = skipwhite(arg);
6814 }
6815
6816 if (ret != FAIL && ga.ga_data != NULL)
6817 {
Bram Moolenaarc03fe662021-07-11 16:52:45 +02006818 // use the first line of continuation lines for messages
6819 SOURCING_LNUM = start_lnum;
6820
Bram Moolenaar37fef162022-08-29 18:16:32 +01006821 if (eap->cmdidx == CMD_echomsg
6822 || eap->cmdidx == CMD_echowindow
6823 || eap->cmdidx == CMD_echoerr)
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006824 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006825 // Mark the already saved text as finishing the line, so that what
6826 // follows is displayed on a new line when scrolling back at the
6827 // more prompt.
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006828 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006829 }
6830
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006831 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006832 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006833 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006834 out_flush();
6835 }
Yasuhiro Matsumotoa02a8a42022-09-02 12:16:21 +01006836 else if (eap->cmdidx == CMD_echowindow)
6837 {
6838#ifdef HAS_MESSAGE_WINDOW
6839 start_echowindow();
6840#endif
6841 msg_attr(ga.ga_data, echo_attr);
6842#ifdef HAS_MESSAGE_WINDOW
6843 end_echowindow();
6844#endif
6845 }
Bram Moolenaar4c868302021-03-22 16:19:45 +01006846 else if (eap->cmdidx == CMD_echoconsole)
6847 {
6848 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data), TRUE);
6849 ui_write((char_u *)"\r\n", 2, TRUE);
6850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006851 else if (eap->cmdidx == CMD_echoerr)
6852 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02006853 int save_did_emsg = did_emsg;
6854
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006855 // We don't want to abort following commands, restore did_emsg.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006856 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (!force_abort)
6858 did_emsg = save_did_emsg;
6859 }
6860 else if (eap->cmdidx == CMD_execute)
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006861 {
6862 int save_sticky_cmdmod_flags = sticky_cmdmod_flags;
6863
6864 // "legacy exe cmd" and "vim9cmd exe cmd" applies to "cmd".
6865 sticky_cmdmod_flags = cmdmod.cmod_flags
6866 & (CMOD_LEGACY | CMOD_VIM9CMD);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867 do_cmdline((char_u *)ga.ga_data,
6868 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
Bram Moolenaar5b1d6e92022-02-11 20:33:48 +00006869 sticky_cmdmod_flags = save_sticky_cmdmod_flags;
6870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
6872
6873 ga_clear(&ga);
6874
6875 if (eap->skip)
6876 --emsg_skip;
Bram Moolenaar63b91732021-08-05 20:40:03 +02006877 set_nextcmd(eap, arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006878}
6879
6880/*
6881 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6882 * "arg" points to the "&" or '+' when called, to "option" when returning.
6883 * Returns NULL when no option name found. Otherwise pointer to the char
6884 * after the option name.
6885 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006886 char_u *
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006887find_option_end(char_u **arg, int *scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888{
6889 char_u *p = *arg;
6890
6891 ++p;
6892 if (*p == 'g' && p[1] == ':')
6893 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006894 *scope = OPT_GLOBAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006895 p += 2;
6896 }
6897 else if (*p == 'l' && p[1] == ':')
6898 {
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006899 *scope = OPT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 p += 2;
6901 }
6902 else
Yegappan Lakshmanan64095532021-12-06 11:03:55 +00006903 *scope = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904
6905 if (!ASCII_ISALPHA(*p))
6906 return NULL;
6907 *arg = p;
6908
6909 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006910 p += 4; // termcap option
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 else
6912 while (ASCII_ISALPHA(*p))
6913 ++p;
6914 return p;
6915}
6916
6917/*
Bram Moolenaar661b1822005-07-28 22:36:45 +00006918 * Display script name where an item was last set.
6919 * Should only be invoked when 'verbose' is non-zero.
6920 */
6921 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006922last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006923{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006924 char_u *p;
6925
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006926 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006927 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006928 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006929 if (p != NULL)
6930 {
6931 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006932 msg_puts(_("\n\tLast set from "));
6933 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006934 if (script_ctx.sc_lnum > 0)
6935 {
Bram Moolenaar64e74c92019-12-22 15:38:06 +01006936 msg_puts(_(line_msg));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006937 msg_outnum((long)script_ctx.sc_lnum);
6938 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006939 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006940 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006941 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006942 }
6943}
6944
Bram Moolenaarb005cd82019-09-04 15:54:55 +02006945#endif // FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946
6947/*
6948 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006949 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 * "flags" can be "g" to do a global substitute.
6951 * Returns an allocated string, NULL for error.
6952 */
6953 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006954do_string_sub(
6955 char_u *str,
6956 char_u *pat,
6957 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02006958 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01006959 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960{
6961 int sublen;
6962 regmatch_T regmatch;
6963 int i;
6964 int do_all;
6965 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006966 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 garray_T ga;
6968 char_u *ret;
6969 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006970 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006972 // Make 'cpoptions' empty, so that the 'l' flag doesn't work here
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00006974 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975
6976 ga_init2(&ga, 1, 200);
6977
6978 do_all = (flags[0] == 'g');
6979
6980 regmatch.rm_ic = p_ic;
6981 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
6982 if (regmatch.regprog != NULL)
6983 {
6984 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01006985 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
6987 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006988 // Skip empty match except for first match.
Bram Moolenaar8af26912014-01-23 20:09:34 +01006989 if (regmatch.startp[0] == regmatch.endp[0])
6990 {
6991 if (zero_width == regmatch.startp[0])
6992 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01006993 // avoid getting stuck on a match with an empty string
Bram Moolenaar1614a142019-10-06 22:00:13 +02006994 i = mb_ptr2len(tail);
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02006995 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
6996 (size_t)i);
6997 ga.ga_len += i;
6998 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01006999 continue;
7000 }
7001 zero_width = regmatch.startp[0];
7002 }
7003
Bram Moolenaar071d4272004-06-13 20:20:40 +00007004 /*
7005 * Get some space for a temporary buffer to do the substitution
7006 * into. It will contain:
7007 * - The text up to where the match is.
7008 * - The substituted text.
7009 * - The text after the match.
7010 */
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007011 sublen = vim_regsub(&regmatch, sub, expr, tail, 0, REGSUB_MAGIC);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007012 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007013 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7014 {
7015 ga_clear(&ga);
7016 break;
7017 }
7018
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007019 // copy the text up to where the match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020 i = (int)(regmatch.startp[0] - tail);
7021 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007022 // add the substituted text
Bram Moolenaar4aaf3e72022-05-30 20:58:55 +01007023 (void)vim_regsub(&regmatch, sub, expr,
7024 (char_u *)ga.ga_data + ga.ga_len + i, sublen,
7025 REGSUB_COPY | REGSUB_MAGIC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007027 tail = regmatch.endp[0];
7028 if (*tail == NUL)
7029 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 if (!do_all)
7031 break;
7032 }
7033
7034 if (ga.ga_data != NULL)
7035 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7036
Bram Moolenaar473de612013-06-08 18:19:48 +02007037 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039
7040 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7041 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007042 if (p_cpo == empty_option)
7043 p_cpo = save_cpo;
7044 else
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007045 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01007046 // Darn, evaluating {sub} expression or {expr} changed the value.
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007047 // If it's still empty it was changed and restored, need to restore in
7048 // the complicated way.
7049 if (*p_cpo == NUL)
Bram Moolenaar31e5c602022-04-15 13:53:33 +01007050 set_option_value_give_err((char_u *)"cpo", 0L, save_cpo, 0);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007051 free_string_option(save_cpo);
Bram Moolenaare5a2dc82021-01-03 19:52:05 +01007052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053
7054 return ret;
7055}