blob: f32092c82164775069a9b601a5c7df0504844716 [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 Moolenaarc70646c2005-01-04 21:52:38 +000023static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020025#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020026static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020027#endif
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +020028static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis");
Bram Moolenaar8c8de832008-06-24 22:58:06 +000029
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010030#define NAMESPACE_CHAR (char_u *)"abglstvw"
31
Bram Moolenaar532c7802005-01-27 14:44:31 +000032/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000033 * When recursively copying lists and dicts we need to remember which ones we
34 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000035 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000036 */
37static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039static int echo_attr = 0; /* attributes used for ":echo" */
40
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000041/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000042static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
43
Bram Moolenaar071d4272004-06-13 20:20:40 +000044/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000045 * Info used by a ":for" loop.
46 */
Bram Moolenaar33570922005-01-25 22:26:29 +000047typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000048{
49 int fi_semicolon; /* TRUE if ending in '; var]' */
50 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000051 listwatch_T fi_lw; /* keep an eye on the item used. */
52 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010053 int fi_bi; /* index of blob */
54 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000055} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000056
Bram Moolenaar48e697e2016-01-23 22:17:30 +010057static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010058static int eval2(char_u **arg, typval_T *rettv, int evaluate);
59static int eval3(char_u **arg, typval_T *rettv, int evaluate);
60static int eval4(char_u **arg, typval_T *rettv, int evaluate);
61static int eval5(char_u **arg, typval_T *rettv, int evaluate);
62static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
63static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +020064static int eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp);
Bram Moolenaara40058a2005-07-11 22:42:07 +000065
Bram Moolenaar48e697e2016-01-23 22:17:30 +010066static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
67static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010068static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +010069static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar5843f5f2019-08-20 20:13:45 +020070static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010071static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar2c704a72010-06-03 21:17:25 +020072
Bram Moolenaare21c1582019-03-02 11:57:09 +010073/*
74 * Return "n1" divided by "n2", taking care of dividing by zero.
75 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020076 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +010077num_divide(varnumber_T n1, varnumber_T n2)
78{
79 varnumber_T result;
80
81 if (n2 == 0) // give an error message?
82 {
83 if (n1 == 0)
84 result = VARNUM_MIN; // similar to NaN
85 else if (n1 < 0)
86 result = -VARNUM_MAX;
87 else
88 result = VARNUM_MAX;
89 }
90 else
91 result = n1 / n2;
92
93 return result;
94}
95
96/*
97 * Return "n1" modulus "n2", taking care of dividing by zero.
98 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +020099 varnumber_T
Bram Moolenaare21c1582019-03-02 11:57:09 +0100100num_modulus(varnumber_T n1, varnumber_T n2)
101{
102 // Give an error when n2 is 0?
103 return (n2 == 0) ? 0 : (n1 % n2);
104}
105
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100106#if defined(EBCDIC) || defined(PROTO)
107/*
108 * Compare struct fst by function name.
109 */
110 static int
111compare_func_name(const void *s1, const void *s2)
112{
113 struct fst *p1 = (struct fst *)s1;
114 struct fst *p2 = (struct fst *)s2;
115
116 return STRCMP(p1->f_name, p2->f_name);
117}
118
119/*
120 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100121 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100122 * On machines using EBCDIC we have to sort it.
123 */
124 static void
125sortFunctions(void)
126{
127 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
128
129 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
130}
131#endif
132
Bram Moolenaar33570922005-01-25 22:26:29 +0000133/*
134 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000135 */
136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000138{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200139 evalvars_init();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200140 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000141
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200142#ifdef EBCDIC
143 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100144 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200145 */
146 sortFunctions();
147#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000148}
149
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000150#if defined(EXITFREE) || defined(PROTO)
151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100152eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000153{
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200154 evalvars_clear();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000155
Bram Moolenaard9fba312005-06-26 22:34:35 +0000156 free_scriptnames();
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200157 free_locales();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000158
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000159 /* autoloaded script names */
160 ga_clear_strings(&ga_loaded);
161
Bram Moolenaar75ee5442019-06-06 18:05:25 +0200162 // unreferenced lists and dicts
163 (void)garbage_collect(FALSE);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +0200164
165 // functions not garbage collected
166 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000167}
168#endif
169
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000170static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200171#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000172static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000173static char_u *redir_endp = NULL;
174static char_u *redir_varname = NULL;
175
176/*
177 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100178 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000179 * Returns OK if successfully completed the setup. FAIL otherwise.
180 */
181 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100182var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000183{
184 int save_emsg;
185 int err;
186 typval_T tv;
187
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000188 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000189 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000190 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100191 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000192 return FAIL;
193 }
194
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000195 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000196 redir_varname = vim_strsave(name);
197 if (redir_varname == NULL)
198 return FAIL;
199
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200200 redir_lval = ALLOC_CLEAR_ONE(lval_T);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000201 if (redir_lval == NULL)
202 {
203 var_redir_stop();
204 return FAIL;
205 }
206
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000207 /* The output is stored in growarray "redir_ga" until redirection ends. */
208 ga_init2(&redir_ga, (int)sizeof(char), 500);
209
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000210 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100211 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000212 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000213 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
214 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200215 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216 if (redir_endp != NULL && *redir_endp != NUL)
217 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100218 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000219 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100220 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000221 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000222 var_redir_stop();
223 return FAIL;
224 }
225
226 /* check if we can write to the variable: set it to or append an empty
227 * string */
228 save_emsg = did_emsg;
229 did_emsg = FALSE;
230 tv.v_type = VAR_STRING;
231 tv.vval.v_string = (char_u *)"";
232 if (append)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200233 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)".");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000234 else
Bram Moolenaar9937a052019-06-15 15:45:06 +0200235 set_var_lval(redir_lval, redir_endp, &tv, TRUE, FALSE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200236 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000237 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000238 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000239 if (err)
240 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000241 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000242 var_redir_stop();
243 return FAIL;
244 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000245
246 return OK;
247}
248
249/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000250 * Append "value[value_len]" to the variable set by var_redir_start().
251 * The actual appending is postponed until redirection ends, because the value
252 * appended may in fact be the string we write to, changing it may cause freed
253 * memory to be used:
254 * :redir => foo
255 * :let foo
256 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000257 */
258 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100259var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000260{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000261 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000262
263 if (redir_lval == NULL)
264 return;
265
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000266 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000267 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000268 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000269 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000270
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000271 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000272 {
273 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000274 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000275 }
276 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000277 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000278}
279
280/*
281 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000282 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000283 */
284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100285var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000286{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000287 typval_T tv;
288
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200289 if (EVALCMD_BUSY)
290 {
291 redir_lval = NULL;
292 return;
293 }
294
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000295 if (redir_lval != NULL)
296 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000297 /* If there was no error: assign the text to the variable. */
298 if (redir_endp != NULL)
299 {
300 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
301 tv.v_type = VAR_STRING;
302 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200303 /* Call get_lval() again, if it's inside a Dict or List it may
304 * have changed. */
305 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100306 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200307 if (redir_endp != NULL && redir_lval->ll_name != NULL)
Bram Moolenaar9937a052019-06-15 15:45:06 +0200308 set_var_lval(redir_lval, redir_endp, &tv, FALSE, FALSE,
309 (char_u *)".");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200310 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000311 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000312
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000313 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100314 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000315
Bram Moolenaard23a8232018-02-10 18:45:26 +0100316 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000317 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100318 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000319}
320
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100322eval_charconvert(
323 char_u *enc_from,
324 char_u *enc_to,
325 char_u *fname_from,
326 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327{
328 int err = FALSE;
329
330 set_vim_var_string(VV_CC_FROM, enc_from, -1);
331 set_vim_var_string(VV_CC_TO, enc_to, -1);
332 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
333 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
334 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
335 err = TRUE;
336 set_vim_var_string(VV_CC_FROM, NULL, -1);
337 set_vim_var_string(VV_CC_TO, NULL, -1);
338 set_vim_var_string(VV_FNAME_IN, NULL, -1);
339 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
340
341 if (err)
342 return FAIL;
343 return OK;
344}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345
346# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
347 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100348eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349{
350 int err = FALSE;
351
352 set_vim_var_string(VV_FNAME_IN, fname, -1);
353 set_vim_var_string(VV_CMDARG, args, -1);
354 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
355 err = TRUE;
356 set_vim_var_string(VV_FNAME_IN, NULL, -1);
357 set_vim_var_string(VV_CMDARG, NULL, -1);
358
359 if (err)
360 {
361 mch_remove(fname);
362 return FAIL;
363 }
364 return OK;
365}
366# endif
367
368# if defined(FEAT_DIFF) || defined(PROTO)
369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370eval_diff(
371 char_u *origfile,
372 char_u *newfile,
373 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374{
375 int err = FALSE;
376
377 set_vim_var_string(VV_FNAME_IN, origfile, -1);
378 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
379 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
380 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
381 set_vim_var_string(VV_FNAME_IN, NULL, -1);
382 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
383 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
384}
385
386 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100387eval_patch(
388 char_u *origfile,
389 char_u *difffile,
390 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391{
392 int err;
393
394 set_vim_var_string(VV_FNAME_IN, origfile, -1);
395 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
396 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
397 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
398 set_vim_var_string(VV_FNAME_IN, NULL, -1);
399 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
400 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
401}
402# endif
403
404/*
405 * Top level evaluation function, returning a boolean.
406 * Sets "error" to TRUE if there was an error.
407 * Return TRUE or FALSE.
408 */
409 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100410eval_to_bool(
411 char_u *arg,
412 int *error,
413 char_u **nextcmd,
414 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415{
Bram Moolenaar33570922005-01-25 22:26:29 +0000416 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200417 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000418
419 if (skip)
420 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000421 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 else
424 {
425 *error = FALSE;
426 if (!skip)
427 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100428 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000429 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430 }
431 }
432 if (skip)
433 --emsg_skip;
434
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200435 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436}
437
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100438/*
439 * Call eval1() and give an error message if not done at a lower level.
440 */
441 static int
442eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
443{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100444 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100445 int ret;
446 int did_emsg_before = did_emsg;
447 int called_emsg_before = called_emsg;
448
449 ret = eval1(arg, rettv, evaluate);
450 if (ret == FAIL)
451 {
452 // Report the invalid expression unless the expression evaluation has
453 // been cancelled due to an aborting error, an interrupt, or an
454 // exception, or we already gave a more specific error.
455 // Also check called_emsg for when using assert_fails().
456 if (!aborting() && did_emsg == did_emsg_before
457 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100458 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100459 }
460 return ret;
461}
462
Bram Moolenaar543c9b12019-04-05 22:50:40 +0200463 int
Bram Moolenaar48570482017-10-30 21:48:41 +0100464eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
465{
466 char_u *s;
Bram Moolenaar48570482017-10-30 21:48:41 +0100467 char_u buf[NUMBUFLEN];
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200468 funcexe_T funcexe;
Bram Moolenaar48570482017-10-30 21:48:41 +0100469
470 if (expr->v_type == VAR_FUNC)
471 {
472 s = expr->vval.v_string;
473 if (s == NULL || *s == NUL)
474 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200475 vim_memset(&funcexe, 0, sizeof(funcexe));
476 funcexe.evaluate = TRUE;
477 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100478 return FAIL;
479 }
480 else if (expr->v_type == VAR_PARTIAL)
481 {
482 partial_T *partial = expr->vval.v_partial;
483
484 s = partial_name(partial);
485 if (s == NULL || *s == NUL)
486 return FAIL;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200487 vim_memset(&funcexe, 0, sizeof(funcexe));
488 funcexe.evaluate = TRUE;
489 funcexe.partial = partial;
490 if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100491 return FAIL;
492 }
493 else
494 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100495 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100496 if (s == NULL)
497 return FAIL;
498 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100499 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100500 return FAIL;
501 if (*s != NUL) /* check for trailing chars after expr */
502 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200503 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100504 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100505 return FAIL;
506 }
507 }
508 return OK;
509}
510
511/*
512 * Like eval_to_bool() but using a typval_T instead of a string.
513 * Works for string, funcref and partial.
514 */
515 int
516eval_expr_to_bool(typval_T *expr, int *error)
517{
518 typval_T rettv;
519 int res;
520
521 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
522 {
523 *error = TRUE;
524 return FALSE;
525 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100526 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100527 clear_tv(&rettv);
528 return res;
529}
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531/*
532 * Top level evaluation function, returning a string. If "skip" is TRUE,
533 * only parsing to "nextcmd" is done, without reporting errors. Return
534 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
535 */
536 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100537eval_to_string_skip(
538 char_u *arg,
539 char_u **nextcmd,
540 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
Bram Moolenaar33570922005-01-25 22:26:29 +0000542 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 char_u *retval;
544
545 if (skip)
546 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000547 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 retval = NULL;
549 else
550 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100551 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000552 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 }
554 if (skip)
555 --emsg_skip;
556
557 return retval;
558}
559
560/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000561 * Skip over an expression at "*pp".
562 * Return FAIL for an error, OK otherwise.
563 */
564 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100565skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000566{
Bram Moolenaar33570922005-01-25 22:26:29 +0000567 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000568
569 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000570 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000571}
572
573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000575 * When "convert" is TRUE convert a List into a sequence of lines and convert
576 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 * Return pointer to allocated memory, or NULL for failure.
578 */
579 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100580eval_to_string(
581 char_u *arg,
582 char_u **nextcmd,
583 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584{
Bram Moolenaar33570922005-01-25 22:26:29 +0000585 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000587 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000588#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000589 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000592 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 retval = NULL;
594 else
595 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000596 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000597 {
598 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000599 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200600 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200601 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200602 if (tv.vval.v_list->lv_len > 0)
603 ga_append(&ga, NL);
604 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000605 ga_append(&ga, NUL);
606 retval = (char_u *)ga.ga_data;
607 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000608#ifdef FEAT_FLOAT
609 else if (convert && tv.v_type == VAR_FLOAT)
610 {
611 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
612 retval = vim_strsave(numbuf);
613 }
614#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000615 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100616 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000617 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618 }
619
620 return retval;
621}
622
623/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000624 * Call eval_to_string() without using current local variables and using
625 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626 */
627 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100628eval_to_string_safe(
629 char_u *arg,
630 char_u **nextcmd,
631 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632{
633 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200634 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200636 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000637 if (use_sandbox)
638 ++sandbox;
639 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000640 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000641 if (use_sandbox)
642 --sandbox;
643 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200644 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 return retval;
646}
647
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648/*
649 * Top level evaluation function, returning a number.
650 * Evaluates "expr" silently.
651 * Returns -1 for an error.
652 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200653 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100654eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
Bram Moolenaar33570922005-01-25 22:26:29 +0000656 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200657 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000658 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
660 ++emsg_off;
661
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000662 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 retval = -1;
664 else
665 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100666 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000667 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 }
669 --emsg_off;
670
671 return retval;
672}
673
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000674#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000675/*
676 * Evaluate an expression to a list with suggestions.
677 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000678 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000679 */
680 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100681eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000682{
683 typval_T save_val;
684 typval_T rettv;
685 list_T *list = NULL;
686 char_u *p = skipwhite(expr);
687
688 /* Set "v:val" to the bad word. */
689 prepare_vimvar(VV_VAL, &save_val);
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200690 set_vim_var_string(VV_VAL, badword, -1);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000691 if (p_verbose == 0)
692 ++emsg_off;
693
694 if (eval1(&p, &rettv, TRUE) == OK)
695 {
696 if (rettv.v_type != VAR_LIST)
697 clear_tv(&rettv);
698 else
699 list = rettv.vval.v_list;
700 }
701
702 if (p_verbose == 0)
703 --emsg_off;
Bram Moolenaare5cdf152019-08-29 22:09:46 +0200704 clear_tv(get_vim_var_tv(VV_VAL));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000705 restore_vimvar(VV_VAL, &save_val);
706
707 return list;
708}
709
710/*
711 * "list" is supposed to contain two items: a word and a number. Return the
712 * word in "pp" and the number as the return value.
713 * Return -1 if anything isn't right.
714 * Used to get the good word and score from the eval_spell_expr() result.
715 */
716 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100717get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000718{
719 listitem_T *li;
720
721 li = list->lv_first;
722 if (li == NULL)
723 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100724 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000725
726 li = li->li_next;
727 if (li == NULL)
728 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100729 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000730}
731#endif
732
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000733/*
Bram Moolenaar4770d092006-01-12 23:22:24 +0000734 * Top level evaluation function.
735 * Returns an allocated typval_T with the result.
736 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000737 */
738 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100739eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000740{
741 typval_T *tv;
742
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200743 tv = ALLOC_ONE(typval_T);
Bram Moolenaar4770d092006-01-12 23:22:24 +0000744 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100745 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +0000746
747 return tv;
748}
749
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100751 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +0200752 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
753 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000754 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 */
Bram Moolenaar82139082011-09-14 16:52:09 +0200756 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100757call_vim_function(
758 char_u *func,
759 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +0200760 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200761 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762{
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000763 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200764 funcexe_T funcexe;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000766 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200767 vim_memset(&funcexe, 0, sizeof(funcexe));
768 funcexe.firstline = curwin->w_cursor.lnum;
769 funcexe.lastline = curwin->w_cursor.lnum;
770 funcexe.evaluate = TRUE;
771 ret = call_func(func, -1, rettv, argc, argv, &funcexe);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000772 if (ret == FAIL)
773 clear_tv(rettv);
774
775 return ret;
776}
777
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100778/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100779 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100780 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200781 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
782 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100783 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200784 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100785call_func_retnr(
786 char_u *func,
787 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200788 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100789{
790 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200791 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100792
Bram Moolenaarded27a12018-08-01 19:06:03 +0200793 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100794 return -1;
795
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100796 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100797 clear_tv(&rettv);
798 return retval;
799}
800
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000801/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100802 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000803 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200804 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
805 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000806 */
807 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100808call_func_retstr(
809 char_u *func,
810 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200811 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000812{
813 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000814 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000815
Bram Moolenaarded27a12018-08-01 19:06:03 +0200816 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000817 return NULL;
818
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100819 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000820 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 return retval;
822}
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000823
Bram Moolenaar25ceb222005-07-30 22:45:36 +0000824/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +0100825 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +0200826 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
827 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000828 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000829 */
830 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100831call_func_retlist(
832 char_u *func,
833 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +0200834 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000835{
836 typval_T rettv;
837
Bram Moolenaarded27a12018-08-01 19:06:03 +0200838 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +0000839 return NULL;
840
841 if (rettv.v_type != VAR_LIST)
842 {
843 clear_tv(&rettv);
844 return NULL;
845 }
846
847 return rettv.vval.v_list;
848}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#ifdef FEAT_FOLDING
851/*
852 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
853 * it in "*cp". Doesn't give error messages.
854 */
855 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100856eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857{
Bram Moolenaar33570922005-01-25 22:26:29 +0000858 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200859 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000861 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
862 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000863
864 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000865 if (use_sandbox)
866 ++sandbox;
867 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000869 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 retval = 0;
871 else
872 {
873 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000874 if (tv.v_type == VAR_NUMBER)
875 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +0000876 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 retval = 0;
878 else
879 {
880 /* If the result is a string, check if there is a non-digit before
881 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 if (!VIM_ISDIGIT(*s) && *s != '-')
884 *cp = *s++;
885 retval = atol((char *)s);
886 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000887 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 }
889 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000890 if (use_sandbox)
891 --sandbox;
892 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200894 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895}
896#endif
897
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000899 * Get an lval: variable, Dict item or List item that can be assigned a value
900 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
901 * "name.key", "name.key[expr]" etc.
902 * Indexing only works if "name" is an existing List or Dictionary.
903 * "name" points to the start of the name.
904 * If "rettv" is not NULL it points to the value to be assigned.
905 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
906 * wrong; must end in space or cmd separator.
907 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100908 * flags:
909 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +0100910 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100911 * GLV_NO_AUTOLOAD: do not use script autoloading
912 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000913 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +0000914 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000915 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000916 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200917 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918get_lval(
919 char_u *name,
920 typval_T *rettv,
921 lval_T *lp,
922 int unlet,
923 int skip,
924 int flags, /* GLV_ values */
925 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000926{
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000927 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000928 char_u *expr_start, *expr_end;
929 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +0000930 dictitem_T *v;
931 typval_T var1;
932 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000933 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +0000934 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000935 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +0000936 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +0000937 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100938 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000939
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000940 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +0000941 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000942
943 if (skip)
944 {
945 /* When skipping just find the end of the name. */
946 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000947 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000948 }
949
950 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000951 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000952 if (expr_start != NULL)
953 {
954 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +0100955 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000956 && *p != '[' && *p != '.')
957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100958 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000959 return NULL;
960 }
961
962 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
963 if (lp->ll_exp_name == NULL)
964 {
965 /* Report an invalid expression in braces, unless the
966 * expression evaluation has been cancelled due to an
967 * aborting error, an interrupt, or an exception. */
968 if (!aborting() && !quiet)
969 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000970 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100971 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000972 return NULL;
973 }
974 }
975 lp->ll_name = lp->ll_exp_name;
976 }
977 else
978 lp->ll_name = name;
979
980 /* Without [idx] or .key we are done. */
981 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
982 return p;
983
984 cc = *p;
985 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +0100986 /* Only pass &ht when we would write to the variable, it prevents autoload
987 * as well. */
988 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
989 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000990 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100991 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000992 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000993 if (v == NULL)
994 return NULL;
995
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000996 /*
997 * Loop until no more [idx] or .key is following.
998 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000999 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001000 var1.v_type = VAR_UNKNOWN;
1001 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001002 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001003 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001004 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1005 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001006 && lp->ll_tv->vval.v_dict != NULL)
1007 && !(lp->ll_tv->v_type == VAR_BLOB
1008 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001010 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001011 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001012 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001014 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001015 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001016 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001017 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001018 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001019 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001020
Bram Moolenaar8c711452005-01-14 21:53:12 +00001021 len = -1;
1022 if (*p == '.')
1023 {
1024 key = p + 1;
1025 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
1026 ;
1027 if (len == 0)
1028 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001029 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001030 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001031 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001032 }
1033 p = key + len;
1034 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001035 else
1036 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001037 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001038 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001039 if (*p == ':')
1040 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001041 else
1042 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001043 empty1 = FALSE;
1044 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001045 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001046 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001047 {
1048 /* not a number or string */
1049 clear_tv(&var1);
1050 return NULL;
1051 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001052 }
1053
1054 /* Optionally get the second index [ :expr]. */
1055 if (*p == ':')
1056 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001057 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001058 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001059 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001060 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001061 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001062 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001063 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001064 if (rettv != NULL
1065 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001066 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001067 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001068 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00001069 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001070 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001071 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001072 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001073 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001074 }
1075 p = skipwhite(p + 1);
1076 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001077 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001078 else
1079 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001080 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001081 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
1082 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001083 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001084 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001085 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001086 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001087 {
1088 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001089 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001090 clear_tv(&var2);
1091 return NULL;
1092 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001093 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001094 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001095 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001096 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001097 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001098
Bram Moolenaar8c711452005-01-14 21:53:12 +00001099 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001100 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001101 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001102 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001103 clear_tv(&var1);
1104 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001105 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001106 }
1107
1108 /* Skip to past ']'. */
1109 ++p;
1110 }
1111
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001112 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001113 {
1114 if (len == -1)
1115 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001116 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001117 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02001118 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001119 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001120 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001121 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001122 }
1123 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001124 lp->ll_list = NULL;
1125 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001126 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001127
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001128 /* When assigning to a scope dictionary check that a function and
1129 * variable name is valid (only variable name unless it is l: or
1130 * g: dictionary). Disallow overwriting a builtin function. */
1131 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001132 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001133 int prevval;
1134 int wrong;
1135
1136 if (len != -1)
1137 {
1138 prevval = key[len];
1139 key[len] = NUL;
1140 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02001141 else
1142 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001143 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
1144 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001145 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001146 || !valid_varname(key);
1147 if (len != -1)
1148 key[len] = prevval;
1149 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001150 return NULL;
1151 }
1152
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001153 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001154 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01001155 // Can't add "v:" or "a:" variable.
1156 if (lp->ll_dict == &vimvardict
1157 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001158 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001159 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01001160 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001161 return NULL;
1162 }
1163
Bram Moolenaar31b81602019-02-10 22:14:27 +01001164 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001165 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001166 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001167 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001168 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001169 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001170 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001171 }
1172 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001173 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001174 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001175 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001176 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001177 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001178 p = NULL;
1179 break;
1180 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001181 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01001182 else if ((flags & GLV_READ_ONLY) == 0
1183 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01001184 {
1185 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001186 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01001187 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02001188
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001189 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001190 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001191 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001192 else if (lp->ll_tv->v_type == VAR_BLOB)
1193 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001194 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
1195
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001196 /*
1197 * Get the number and item for the only or first index of the List.
1198 */
1199 if (empty1)
1200 lp->ll_n1 = 0;
1201 else
1202 // is number or string
1203 lp->ll_n1 = (long)tv_get_number(&var1);
1204 clear_tv(&var1);
1205
1206 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001207 || lp->ll_n1 > bloblen
1208 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001209 {
1210 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001212 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001213 return NULL;
1214 }
1215 if (lp->ll_range && !lp->ll_empty2)
1216 {
1217 lp->ll_n2 = (long)tv_get_number(&var2);
1218 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001219 if (lp->ll_n2 < 0
1220 || lp->ll_n2 >= bloblen
1221 || lp->ll_n2 < lp->ll_n1)
1222 {
1223 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001224 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001225 return NULL;
1226 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001227 }
1228 lp->ll_blob = lp->ll_tv->vval.v_blob;
1229 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01001230 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001231 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00001232 else
1233 {
1234 /*
1235 * Get the number and item for the only or first index of the List.
1236 */
1237 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001238 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001239 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001240 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001241 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001242 clear_tv(&var1);
1243
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001244 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001245 lp->ll_list = lp->ll_tv->vval.v_list;
1246 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1247 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001248 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00001249 if (lp->ll_n1 < 0)
1250 {
1251 lp->ll_n1 = 0;
1252 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
1253 }
1254 }
1255 if (lp->ll_li == NULL)
1256 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001257 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02001258 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001259 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001260 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001261 }
1262
1263 /*
1264 * May need to find the item or absolute index for the second
1265 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001266 * When no index given: "lp->ll_empty2" is TRUE.
1267 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00001268 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001269 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001270 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001271 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001272 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00001273 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001274 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00001275 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001276 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001277 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02001278 {
1279 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001280 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001281 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001282 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001283 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001284 }
1285
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001286 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
1287 if (lp->ll_n1 < 0)
1288 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
1289 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02001290 {
1291 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001292 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001293 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02001294 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001295 }
1296
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001297 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001298 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001299 }
1300
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001301 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001302 return p;
1303}
1304
1305/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001306 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001307 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001310{
1311 vim_free(lp->ll_exp_name);
1312 vim_free(lp->ll_newkey);
1313}
1314
1315/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001316 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001317 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001318 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
1319 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001320 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02001321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001322set_var_lval(
1323 lval_T *lp,
1324 char_u *endp,
1325 typval_T *rettv,
1326 int copy,
Bram Moolenaar9937a052019-06-15 15:45:06 +02001327 int is_const, // Disallow to modify existing variable for :const
Bram Moolenaar7454a062016-01-30 15:14:10 +01001328 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001329{
1330 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 listitem_T *ri;
1332 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001333
1334 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001335 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001336 cc = *endp;
1337 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001338 if (lp->ll_blob != NULL)
1339 {
1340 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001341
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001342 if (op != NULL && *op != '=')
1343 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001344 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001345 return;
1346 }
1347
1348 if (lp->ll_range && rettv->v_type == VAR_BLOB)
1349 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001350 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001351
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001352 if (lp->ll_empty2)
1353 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
1354
1355 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001356 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001357 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001358 return;
1359 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001360 if (lp->ll_empty2)
1361 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001362
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001363 ir = 0;
1364 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
1365 blob_set(lp->ll_blob, il,
1366 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001367 }
1368 else
1369 {
1370 val = (int)tv_get_number_chk(rettv, &error);
1371 if (!error)
1372 {
1373 garray_T *gap = &lp->ll_blob->bv_ga;
1374
1375 // Allow for appending a byte. Setting a byte beyond
1376 // the end is an error otherwise.
1377 if (lp->ll_n1 < gap->ga_len
1378 || (lp->ll_n1 == gap->ga_len
1379 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
1380 {
1381 blob_set(lp->ll_blob, lp->ll_n1, val);
1382 if (lp->ll_n1 == gap->ga_len)
1383 ++gap->ga_len;
1384 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001385 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001386 }
1387 }
1388 }
1389 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001390 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01001391 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001392
Bram Moolenaar9937a052019-06-15 15:45:06 +02001393 if (is_const)
1394 {
1395 emsg(_(e_cannot_mod));
1396 *endp = cc;
1397 return;
1398 }
1399
Bram Moolenaarff697e62019-02-12 22:28:33 +01001400 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01001401 di = NULL;
1402 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
1403 &tv, &di, TRUE, FALSE) == OK)
1404 {
1405 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001406 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
1407 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01001408 && tv_op(&tv, rettv, op) == OK)
1409 set_var(lp->ll_name, &tv, FALSE);
1410 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01001413 else
Bram Moolenaar9937a052019-06-15 15:45:06 +02001414 set_var_const(lp->ll_name, rettv, copy, is_const);
Bram Moolenaar79518e22017-02-17 16:31:35 +01001415 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001416 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001417 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001418 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02001419 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001420 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001421 else if (lp->ll_range)
1422 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001423 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01001424 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001425
Bram Moolenaar9937a052019-06-15 15:45:06 +02001426 if (is_const)
1427 {
1428 emsg(_("E996: Cannot lock a range"));
1429 return;
1430 }
1431
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001432 /*
1433 * Check whether any of the list items is locked
1434 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01001435 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001436 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001437 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02001438 return;
1439 ri = ri->li_next;
1440 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
1441 break;
1442 ll_li = ll_li->li_next;
1443 ++ll_n1;
1444 }
1445
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001446 /*
1447 * Assign the List values to the list items.
1448 */
1449 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001450 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001451 if (op != NULL && *op != '=')
1452 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
1453 else
1454 {
1455 clear_tv(&lp->ll_li->li_tv);
1456 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
1457 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001458 ri = ri->li_next;
1459 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
1460 break;
1461 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001462 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001463 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00001464 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001465 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001466 ri = NULL;
1467 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001468 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001469 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001470 lp->ll_li = lp->ll_li->li_next;
1471 ++lp->ll_n1;
1472 }
1473 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001474 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001475 else if (lp->ll_empty2
1476 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001477 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001478 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001479 }
1480 else
1481 {
1482 /*
1483 * Assign to a List or Dictionary item.
1484 */
Bram Moolenaar9937a052019-06-15 15:45:06 +02001485 if (is_const)
1486 {
1487 emsg(_("E996: Cannot lock a list or dict"));
1488 return;
1489 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001490 if (lp->ll_newkey != NULL)
1491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001492 if (op != NULL && *op != '=')
1493 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001494 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001495 return;
1496 }
1497
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001498 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001499 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001500 if (di == NULL)
1501 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001502 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
1503 {
1504 vim_free(di);
1505 return;
1506 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001507 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00001508 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001509 else if (op != NULL && *op != '=')
1510 {
1511 tv_op(lp->ll_tv, rettv, op);
1512 return;
1513 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001514 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001515 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00001516
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001517 /*
1518 * Assign the value to the variable or list item.
1519 */
1520 if (copy)
1521 copy_tv(rettv, lp->ll_tv);
1522 else
1523 {
1524 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001525 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001526 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001527 }
1528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001529}
1530
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001531/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01001532 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
1533 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001534 * Returns OK or FAIL.
1535 */
1536 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001537tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001538{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001539 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001540 char_u numbuf[NUMBUFLEN];
1541 char_u *s;
1542
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001543 /* Can't do anything with a Funcref, Dict, v:true on the right. */
1544 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
1545 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001546 {
1547 switch (tv1->v_type)
1548 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01001549 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001550 case VAR_DICT:
1551 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001552 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001553 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01001554 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01001555 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001556 break;
1557
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001558 case VAR_BLOB:
1559 if (*op != '+' || tv2->v_type != VAR_BLOB)
1560 break;
1561 // BLOB += BLOB
1562 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
1563 {
1564 blob_T *b1 = tv1->vval.v_blob;
1565 blob_T *b2 = tv2->vval.v_blob;
1566 int i, len = blob_len(b2);
1567 for (i = 0; i < len; i++)
1568 ga_append(&b1->bv_ga, blob_get(b2, i));
1569 }
1570 return OK;
1571
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001572 case VAR_LIST:
1573 if (*op != '+' || tv2->v_type != VAR_LIST)
1574 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001575 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001576 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
1577 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
1578 return OK;
1579
1580 case VAR_NUMBER:
1581 case VAR_STRING:
1582 if (tv2->v_type == VAR_LIST)
1583 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001584 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001585 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001586 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001587 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001588#ifdef FEAT_FLOAT
1589 if (tv2->v_type == VAR_FLOAT)
1590 {
1591 float_T f = n;
1592
Bram Moolenaarff697e62019-02-12 22:28:33 +01001593 if (*op == '%')
1594 break;
1595 switch (*op)
1596 {
1597 case '+': f += tv2->vval.v_float; break;
1598 case '-': f -= tv2->vval.v_float; break;
1599 case '*': f *= tv2->vval.v_float; break;
1600 case '/': f /= tv2->vval.v_float; break;
1601 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001602 clear_tv(tv1);
1603 tv1->v_type = VAR_FLOAT;
1604 tv1->vval.v_float = f;
1605 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001606 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001607#endif
1608 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001609 switch (*op)
1610 {
1611 case '+': n += tv_get_number(tv2); break;
1612 case '-': n -= tv_get_number(tv2); break;
1613 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001614 case '/': n = num_divide(n, tv_get_number(tv2)); break;
1615 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001616 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001617 clear_tv(tv1);
1618 tv1->v_type = VAR_NUMBER;
1619 tv1->vval.v_number = n;
1620 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001621 }
1622 else
1623 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 if (tv2->v_type == VAR_FLOAT)
1625 break;
1626
Bram Moolenaarff697e62019-02-12 22:28:33 +01001627 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001628 s = tv_get_string(tv1);
1629 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001630 clear_tv(tv1);
1631 tv1->v_type = VAR_STRING;
1632 tv1->vval.v_string = s;
1633 }
1634 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001636 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001637#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001638 {
1639 float_T f;
1640
Bram Moolenaarff697e62019-02-12 22:28:33 +01001641 if (*op == '%' || *op == '.'
1642 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001643 && tv2->v_type != VAR_NUMBER
1644 && tv2->v_type != VAR_STRING))
1645 break;
1646 if (tv2->v_type == VAR_FLOAT)
1647 f = tv2->vval.v_float;
1648 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001649 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001650 switch (*op)
1651 {
1652 case '+': tv1->vval.v_float += f; break;
1653 case '-': tv1->vval.v_float -= f; break;
1654 case '*': tv1->vval.v_float *= f; break;
1655 case '/': tv1->vval.v_float /= f; break;
1656 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001657 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001658#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01001659 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001660 }
1661 }
1662
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001663 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001664 return FAIL;
1665}
1666
1667/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001668 * Evaluate the expression used in a ":for var in expr" command.
1669 * "arg" points to "var".
1670 * Set "*errp" to TRUE for an error, FALSE otherwise;
1671 * Return a pointer that holds the info. Null when there is an error.
1672 */
1673 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001674eval_for_line(
1675 char_u *arg,
1676 int *errp,
1677 char_u **nextcmdp,
1678 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001679{
Bram Moolenaar33570922005-01-25 22:26:29 +00001680 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001681 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00001682 typval_T tv;
1683 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001684
1685 *errp = TRUE; /* default: there is an error */
1686
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001687 fi = ALLOC_CLEAR_ONE(forinfo_T);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001688 if (fi == NULL)
1689 return NULL;
1690
1691 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
1692 if (expr == NULL)
1693 return fi;
1694
1695 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001696 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001697 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001698 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001699 return fi;
1700 }
1701
1702 if (skip)
1703 ++emsg_skip;
1704 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
1705 {
1706 *errp = FALSE;
1707 if (!skip)
1708 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001709 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001710 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001711 l = tv.vval.v_list;
1712 if (l == NULL)
1713 {
1714 // a null list is like an empty list: do nothing
1715 clear_tv(&tv);
1716 }
1717 else
1718 {
1719 // No need to increment the refcount, it's already set for
1720 // the list being used in "tv".
1721 fi->fi_list = l;
1722 list_add_watch(l, &fi->fi_lw);
1723 fi->fi_lw.lw_item = l->lv_first;
1724 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001725 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001726 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001727 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001728 fi->fi_bi = 0;
1729 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001730 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001731 typval_T btv;
1732
1733 // Make a copy, so that the iteration still works when the
1734 // blob is changed.
1735 blob_copy(&tv, &btv);
1736 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001737 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01001738 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02001739 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001740 else
1741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001742 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001743 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001744 }
1745 }
1746 }
1747 if (skip)
1748 --emsg_skip;
1749
1750 return fi;
1751}
1752
1753/*
1754 * Use the first item in a ":for" list. Advance to the next.
1755 * Assign the values to the variable (list). "arg" points to the first one.
1756 * Return TRUE when a valid item was found, FALSE when at end of list or
1757 * something wrong.
1758 */
1759 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001760next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001761{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02001762 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001763 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00001764 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001765
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001766 if (fi->fi_blob != NULL)
1767 {
1768 typval_T tv;
1769
1770 if (fi->fi_bi >= blob_len(fi->fi_blob))
1771 return FALSE;
1772 tv.v_type = VAR_NUMBER;
1773 tv.v_lock = VAR_FIXED;
1774 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
1775 ++fi->fi_bi;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001776 return ex_let_vars(arg, &tv, TRUE, fi->fi_semicolon,
1777 fi->fi_varcount, FALSE, NULL) == OK;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001778 }
1779
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001780 item = fi->fi_lw.lw_item;
1781 if (item == NULL)
1782 result = FALSE;
1783 else
1784 {
1785 fi->fi_lw.lw_item = item->li_next;
Bram Moolenaar9937a052019-06-15 15:45:06 +02001786 result = (ex_let_vars(arg, &item->li_tv, TRUE, fi->fi_semicolon,
1787 fi->fi_varcount, FALSE, NULL) == OK);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001788 }
1789 return result;
1790}
1791
1792/*
1793 * Free the structure used to store info used by ":for".
1794 */
1795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001796free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001797{
Bram Moolenaar33570922005-01-25 22:26:29 +00001798 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001799
Bram Moolenaarab7013c2005-01-09 21:23:56 +00001800 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001801 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001802 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00001803 list_unref(fi->fi_list);
1804 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01001805 if (fi != NULL && fi->fi_blob != NULL)
1806 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001807 vim_free(fi);
1808}
1809
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811set_context_for_expression(
1812 expand_T *xp,
1813 char_u *arg,
1814 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815{
1816 int got_eq = FALSE;
1817 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001818 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001820 if (cmdidx == CMD_let)
1821 {
1822 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001823 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001824 {
1825 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001826 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001827 {
1828 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001829 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001830 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001831 break;
1832 }
1833 return;
1834 }
1835 }
1836 else
1837 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
1838 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 while ((xp->xp_pattern = vim_strpbrk(arg,
1840 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
1841 {
1842 c = *xp->xp_pattern;
1843 if (c == '&')
1844 {
1845 c = xp->xp_pattern[1];
1846 if (c == '&')
1847 {
1848 ++xp->xp_pattern;
1849 xp->xp_context = cmdidx != CMD_let || got_eq
1850 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
1851 }
1852 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001853 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00001855 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
1856 xp->xp_pattern += 2;
1857
1858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 }
1860 else if (c == '$')
1861 {
1862 /* environment variable */
1863 xp->xp_context = EXPAND_ENV_VARS;
1864 }
1865 else if (c == '=')
1866 {
1867 got_eq = TRUE;
1868 xp->xp_context = EXPAND_EXPRESSION;
1869 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02001870 else if (c == '#'
1871 && xp->xp_context == EXPAND_EXPRESSION)
1872 {
1873 /* Autoload function/variable contains '#'. */
1874 break;
1875 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001876 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 && xp->xp_context == EXPAND_FUNCTIONS
1878 && vim_strchr(xp->xp_pattern, '(') == NULL)
1879 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01001880 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 break;
1882 }
1883 else if (cmdidx != CMD_let || got_eq)
1884 {
1885 if (c == '"') /* string */
1886 {
1887 while ((c = *++xp->xp_pattern) != NUL && c != '"')
1888 if (c == '\\' && xp->xp_pattern[1] != NUL)
1889 ++xp->xp_pattern;
1890 xp->xp_context = EXPAND_NOTHING;
1891 }
1892 else if (c == '\'') /* literal string */
1893 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00001894 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
1896 /* skip */ ;
1897 xp->xp_context = EXPAND_NOTHING;
1898 }
1899 else if (c == '|')
1900 {
1901 if (xp->xp_pattern[1] == '|')
1902 {
1903 ++xp->xp_pattern;
1904 xp->xp_context = EXPAND_EXPRESSION;
1905 }
1906 else
1907 xp->xp_context = EXPAND_COMMANDS;
1908 }
1909 else
1910 xp->xp_context = EXPAND_EXPRESSION;
1911 }
1912 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 /* Doesn't look like something valid, expand as an expression
1914 * anyway. */
1915 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 arg = xp->xp_pattern;
1917 if (*arg != NUL)
1918 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
1919 /* skip */ ;
1920 }
1921 xp->xp_pattern = arg;
1922}
1923
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
1925/*
1926 * Delete all "menutrans_" variables.
1927 */
1928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001929del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930{
Bram Moolenaar33570922005-01-25 22:26:29 +00001931 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00001932 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933
Bram Moolenaar33570922005-01-25 22:26:29 +00001934 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001935 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00001936 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00001937 {
1938 if (!HASHITEM_EMPTY(hi))
1939 {
1940 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001941 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
1942 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00001943 }
1944 }
Bram Moolenaar33570922005-01-25 22:26:29 +00001945 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946}
1947#endif
1948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001950 * Return TRUE if "pat" matches "text".
1951 * Does not use 'cpo' and always uses 'magic'.
1952 */
Bram Moolenaarecaa70e2019-07-14 14:55:39 +02001953 int
Bram Moolenaarea6553b2016-03-27 15:13:38 +02001954pattern_match(char_u *pat, char_u *text, int ic)
1955{
1956 int matches = FALSE;
1957 char_u *save_cpo;
1958 regmatch_T regmatch;
1959
1960 /* avoid 'l' flag in 'cpoptions' */
1961 save_cpo = p_cpo;
1962 p_cpo = (char_u *)"";
1963 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
1964 if (regmatch.regprog != NULL)
1965 {
1966 regmatch.rm_ic = ic;
1967 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
1968 vim_regfree(regmatch.regprog);
1969 }
1970 p_cpo = save_cpo;
1971 return matches;
1972}
1973
1974/*
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001975 * Handle a name followed by "(". Both for just "name(arg)" and for
1976 * "expr->name(arg)".
1977 * Returns OK or FAIL.
1978 */
1979 static int
1980eval_func(
1981 char_u **arg, // points to "(", will be advanced
1982 char_u *name,
1983 int name_len,
1984 typval_T *rettv,
1985 int evaluate,
1986 typval_T *basetv) // "expr" for "expr->name(arg)"
1987{
1988 char_u *s = name;
1989 int len = name_len;
1990 partial_T *partial;
1991 int ret = OK;
1992
1993 if (!evaluate)
1994 check_vars(s, len);
1995
1996 /* If "s" is the name of a variable of type VAR_FUNC
1997 * use its contents. */
1998 s = deref_func_name(s, &len, &partial, !evaluate);
1999
2000 /* Need to make a copy, in case evaluating the arguments makes
2001 * the name invalid. */
2002 s = vim_strsave(s);
2003 if (s == NULL)
2004 ret = FAIL;
2005 else
2006 {
2007 funcexe_T funcexe;
2008
2009 // Invoke the function.
2010 vim_memset(&funcexe, 0, sizeof(funcexe));
2011 funcexe.firstline = curwin->w_cursor.lnum;
2012 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02002013 funcexe.evaluate = evaluate;
2014 funcexe.partial = partial;
2015 funcexe.basetv = basetv;
2016 ret = get_func_tv(s, len, rettv, arg, &funcexe);
2017 }
2018 vim_free(s);
2019
2020 /* If evaluate is FALSE rettv->v_type was not set in
2021 * get_func_tv, but it's needed in handle_subscript() to parse
2022 * what follows. So set it here. */
2023 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
2024 {
2025 rettv->vval.v_string = NULL;
2026 rettv->v_type = VAR_FUNC;
2027 }
2028
2029 /* Stop the expression evaluation when immediately
2030 * aborting on error, or when an interrupt occurred or
2031 * an exception was thrown but not caught. */
2032 if (evaluate && aborting())
2033 {
2034 if (ret == OK)
2035 clear_tv(rettv);
2036 ret = FAIL;
2037 }
2038 return ret;
2039}
2040
2041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002043 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
2045 */
2046
2047/*
2048 * Handle zero level expression.
2049 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002050 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00002051 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 * Return OK or FAIL.
2053 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002055eval0(
2056 char_u *arg,
2057 typval_T *rettv,
2058 char_u **nextcmd,
2059 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060{
2061 int ret;
2062 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002063 int did_emsg_before = did_emsg;
2064 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065
2066 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002067 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 if (ret == FAIL || !ends_excmd(*p))
2069 {
2070 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002071 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 /*
2073 * Report the invalid expression unless the expression evaluation has
2074 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002075 * exception, or we already gave a more specific error.
2076 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002078 if (!aborting() && did_emsg == did_emsg_before
2079 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002080 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 ret = FAIL;
2082 }
2083 if (nextcmd != NULL)
2084 *nextcmd = check_nextcmd(p);
2085
2086 return ret;
2087}
2088
2089/*
2090 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00002091 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 *
2093 * "arg" must point to the first non-white of the expression.
2094 * "arg" is advanced to the next non-white after the recognized expression.
2095 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00002096 * Note: "rettv.v_lock" is not set.
2097 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00002098 * Return OK or FAIL.
2099 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02002100 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002101eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102{
2103 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002104 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 /*
2107 * Get the first variable.
2108 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002109 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 return FAIL;
2111
2112 if ((*arg)[0] == '?')
2113 {
2114 result = FALSE;
2115 if (evaluate)
2116 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002117 int error = FALSE;
2118
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002119 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002121 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002122 if (error)
2123 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 }
2125
2126 /*
2127 * Get the second variable.
2128 */
2129 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002130 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 return FAIL;
2132
2133 /*
2134 * Check for the ":".
2135 */
2136 if ((*arg)[0] != ':')
2137 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002138 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 return FAIL;
2142 }
2143
2144 /*
2145 * Get the third variable.
2146 */
2147 *arg = skipwhite(*arg + 1);
2148 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
2149 {
2150 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002151 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 return FAIL;
2153 }
2154 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002155 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 }
2157
2158 return OK;
2159}
2160
2161/*
2162 * Handle first level expression:
2163 * expr2 || expr2 || expr2 logical OR
2164 *
2165 * "arg" must point to the first non-white of the expression.
2166 * "arg" is advanced to the next non-white after the recognized expression.
2167 *
2168 * Return OK or FAIL.
2169 */
2170 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002171eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172{
Bram Moolenaar33570922005-01-25 22:26:29 +00002173 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 long result;
2175 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002176 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 /*
2179 * Get the first variable.
2180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002181 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002182 return FAIL;
2183
2184 /*
2185 * Repeat until there is no following "||".
2186 */
2187 first = TRUE;
2188 result = FALSE;
2189 while ((*arg)[0] == '|' && (*arg)[1] == '|')
2190 {
2191 if (evaluate && first)
2192 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002193 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002196 if (error)
2197 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 first = FALSE;
2199 }
2200
2201 /*
2202 * Get the second variable.
2203 */
2204 *arg = skipwhite(*arg + 2);
2205 if (eval3(arg, &var2, evaluate && !result) == FAIL)
2206 return FAIL;
2207
2208 /*
2209 * Compute the result.
2210 */
2211 if (evaluate && !result)
2212 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002213 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002216 if (error)
2217 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 }
2219 if (evaluate)
2220 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221 rettv->v_type = VAR_NUMBER;
2222 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 }
2224 }
2225
2226 return OK;
2227}
2228
2229/*
2230 * Handle second level expression:
2231 * expr3 && expr3 && expr3 logical AND
2232 *
2233 * "arg" must point to the first non-white of the expression.
2234 * "arg" is advanced to the next non-white after the recognized expression.
2235 *
2236 * Return OK or FAIL.
2237 */
2238 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
Bram Moolenaar33570922005-01-25 22:26:29 +00002241 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 long result;
2243 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002244 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 /*
2247 * Get the first variable.
2248 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 return FAIL;
2251
2252 /*
2253 * Repeat until there is no following "&&".
2254 */
2255 first = TRUE;
2256 result = TRUE;
2257 while ((*arg)[0] == '&' && (*arg)[1] == '&')
2258 {
2259 if (evaluate && first)
2260 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002261 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002264 if (error)
2265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 first = FALSE;
2267 }
2268
2269 /*
2270 * Get the second variable.
2271 */
2272 *arg = skipwhite(*arg + 2);
2273 if (eval4(arg, &var2, evaluate && result) == FAIL)
2274 return FAIL;
2275
2276 /*
2277 * Compute the result.
2278 */
2279 if (evaluate && result)
2280 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002281 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002284 if (error)
2285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286 }
2287 if (evaluate)
2288 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 rettv->v_type = VAR_NUMBER;
2290 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 }
2293
2294 return OK;
2295}
2296
2297/*
2298 * Handle third level expression:
2299 * var1 == var2
2300 * var1 =~ var2
2301 * var1 != var2
2302 * var1 !~ var2
2303 * var1 > var2
2304 * var1 >= var2
2305 * var1 < var2
2306 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002307 * var1 is var2
2308 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 *
2310 * "arg" must point to the first non-white of the expression.
2311 * "arg" is advanced to the next non-white after the recognized expression.
2312 *
2313 * Return OK or FAIL.
2314 */
2315 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002316eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317{
Bram Moolenaar33570922005-01-25 22:26:29 +00002318 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 char_u *p;
2320 int i;
2321 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002322 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 /*
2327 * Get the first variable.
2328 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002329 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 return FAIL;
2331
2332 p = *arg;
2333 switch (p[0])
2334 {
2335 case '=': if (p[1] == '=')
2336 type = TYPE_EQUAL;
2337 else if (p[1] == '~')
2338 type = TYPE_MATCH;
2339 break;
2340 case '!': if (p[1] == '=')
2341 type = TYPE_NEQUAL;
2342 else if (p[1] == '~')
2343 type = TYPE_NOMATCH;
2344 break;
2345 case '>': if (p[1] != '=')
2346 {
2347 type = TYPE_GREATER;
2348 len = 1;
2349 }
2350 else
2351 type = TYPE_GEQUAL;
2352 break;
2353 case '<': if (p[1] != '=')
2354 {
2355 type = TYPE_SMALLER;
2356 len = 1;
2357 }
2358 else
2359 type = TYPE_SEQUAL;
2360 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002361 case 'i': if (p[1] == 's')
2362 {
2363 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
2364 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02002365 i = p[len];
2366 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002367 {
2368 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
2369 type_is = TRUE;
2370 }
2371 }
2372 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
2374
2375 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002376 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 */
2378 if (type != TYPE_UNKNOWN)
2379 {
2380 /* extra question mark appended: ignore case */
2381 if (p[len] == '?')
2382 {
2383 ic = TRUE;
2384 ++len;
2385 }
2386 /* extra '#' appended: match case */
2387 else if (p[len] == '#')
2388 {
2389 ic = FALSE;
2390 ++len;
2391 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002392 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 else
2394 ic = p_ic;
2395
2396 /*
2397 * Get the second variable.
2398 */
2399 *arg = skipwhite(p + len);
2400 if (eval5(arg, &var2, evaluate) == FAIL)
2401 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002402 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403 return FAIL;
2404 }
Bram Moolenaar31988702018-02-13 12:57:42 +01002405 if (evaluate)
2406 {
2407 int ret = typval_compare(rettv, &var2, type, type_is, ic);
2408
2409 clear_tv(&var2);
2410 return ret;
2411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 }
2413
2414 return OK;
2415}
2416
2417/*
2418 * Handle fourth level expression:
2419 * + number addition
2420 * - number subtraction
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002421 * . string concatenation (if script version is 1)
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002422 * .. string concatenation
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 *
2424 * "arg" must point to the first non-white of the expression.
2425 * "arg" is advanced to the next non-white after the recognized expression.
2426 *
2427 * Return OK or FAIL.
2428 */
2429 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002430eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431{
Bram Moolenaar33570922005-01-25 22:26:29 +00002432 typval_T var2;
2433 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002435 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002436#ifdef FEAT_FLOAT
2437 float_T f1 = 0, f2 = 0;
2438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 char_u *s1, *s2;
2440 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
2441 char_u *p;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002442 int concat;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
2444 /*
2445 * Get the first variable.
2446 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002447 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 return FAIL;
2449
2450 /*
2451 * Repeat computing, until no '+', '-' or '.' is following.
2452 */
2453 for (;;)
2454 {
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002455 // "." is only string concatenation when scriptversion is 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 op = **arg;
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002457 concat = op == '.'
2458 && (*(*arg + 1) == '.' || current_sctx.sc_version < 2);
2459 if (op != '+' && op != '-' && !concat)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 break;
2461
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002462 if ((op != '+' || (rettv->v_type != VAR_LIST
2463 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002464#ifdef FEAT_FLOAT
2465 && (op == '.' || rettv->v_type != VAR_FLOAT)
2466#endif
2467 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002468 {
2469 /* For "list + ...", an illegal use of the first operand as
2470 * a number cannot be determined before evaluating the 2nd
2471 * operand: if this is also a list, all is ok.
2472 * For "something . ...", "something - ..." or "non-list + ...",
2473 * we know that the first operand needs to be a string or number
2474 * without evaluating the 2nd operand. So check before to avoid
2475 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002476 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 {
2478 clear_tv(rettv);
2479 return FAIL;
2480 }
2481 }
2482
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 /*
2484 * Get the second variable.
2485 */
Bram Moolenaar0f248b02019-04-04 15:36:05 +02002486 if (op == '.' && *(*arg + 1) == '.') // .. string concatenation
2487 ++*arg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002489 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 return FAIL;
2493 }
2494
2495 if (evaluate)
2496 {
2497 /*
2498 * Compute the result.
2499 */
2500 if (op == '.')
2501 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002502 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
2503 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002504 if (s2 == NULL) /* type error ? */
2505 {
2506 clear_tv(rettv);
2507 clear_tv(&var2);
2508 return FAIL;
2509 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 clear_tv(rettv);
2512 rettv->v_type = VAR_STRING;
2513 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002515 else if (op == '+' && rettv->v_type == VAR_BLOB
2516 && var2.v_type == VAR_BLOB)
2517 {
2518 blob_T *b1 = rettv->vval.v_blob;
2519 blob_T *b2 = var2.vval.v_blob;
2520 blob_T *b = blob_alloc();
2521 int i;
2522
2523 if (b != NULL)
2524 {
2525 for (i = 0; i < blob_len(b1); i++)
2526 ga_append(&b->bv_ga, blob_get(b1, i));
2527 for (i = 0; i < blob_len(b2); i++)
2528 ga_append(&b->bv_ga, blob_get(b2, i));
2529
2530 clear_tv(rettv);
2531 rettv_blob_set(rettv, b);
2532 }
2533 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00002534 else if (op == '+' && rettv->v_type == VAR_LIST
2535 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00002536 {
2537 /* concatenate Lists */
2538 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
2539 &var3) == FAIL)
2540 {
2541 clear_tv(rettv);
2542 clear_tv(&var2);
2543 return FAIL;
2544 }
2545 clear_tv(rettv);
2546 *rettv = var3;
2547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 else
2549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002550 int error = FALSE;
2551
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002552#ifdef FEAT_FLOAT
2553 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002554 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002555 f1 = rettv->vval.v_float;
2556 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002557 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002558 else
2559#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002560 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002561 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002562 if (error)
2563 {
2564 /* This can only happen for "list + non-list". For
2565 * "non-list + ..." or "something - ...", we returned
2566 * before evaluating the 2nd operand. */
2567 clear_tv(rettv);
2568 return FAIL;
2569 }
2570#ifdef FEAT_FLOAT
2571 if (var2.v_type == VAR_FLOAT)
2572 f1 = n1;
2573#endif
2574 }
2575#ifdef FEAT_FLOAT
2576 if (var2.v_type == VAR_FLOAT)
2577 {
2578 f2 = var2.vval.v_float;
2579 n2 = 0;
2580 }
2581 else
2582#endif
2583 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002584 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002585 if (error)
2586 {
2587 clear_tv(rettv);
2588 clear_tv(&var2);
2589 return FAIL;
2590 }
2591#ifdef FEAT_FLOAT
2592 if (rettv->v_type == VAR_FLOAT)
2593 f2 = n2;
2594#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597
2598#ifdef FEAT_FLOAT
2599 /* If there is a float on either side the result is a float. */
2600 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
2601 {
2602 if (op == '+')
2603 f1 = f1 + f2;
2604 else
2605 f1 = f1 - f2;
2606 rettv->v_type = VAR_FLOAT;
2607 rettv->vval.v_float = f1;
2608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002610#endif
2611 {
2612 if (op == '+')
2613 n1 = n1 + n2;
2614 else
2615 n1 = n1 - n2;
2616 rettv->v_type = VAR_NUMBER;
2617 rettv->vval.v_number = n1;
2618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002620 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 }
2622 }
2623 return OK;
2624}
2625
2626/*
2627 * Handle fifth level expression:
2628 * * number multiplication
2629 * / number division
2630 * % number modulo
2631 *
2632 * "arg" must point to the first non-white of the expression.
2633 * "arg" is advanced to the next non-white after the recognized expression.
2634 *
2635 * Return OK or FAIL.
2636 */
2637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002638eval6(
2639 char_u **arg,
2640 typval_T *rettv,
2641 int evaluate,
2642 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643{
Bram Moolenaar33570922005-01-25 22:26:29 +00002644 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002646 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002647#ifdef FEAT_FLOAT
2648 int use_float = FALSE;
Bram Moolenaar1f3601e2019-04-26 20:33:00 +02002649 float_T f1 = 0, f2 = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002650#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002651 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652
2653 /*
2654 * Get the first variable.
2655 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002656 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 return FAIL;
2658
2659 /*
2660 * Repeat computing, until no '*', '/' or '%' is following.
2661 */
2662 for (;;)
2663 {
2664 op = **arg;
Bram Moolenaarb8be54d2019-07-14 18:22:59 +02002665 if (op != '*' && op != '/' && op != '%')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 break;
2667
2668 if (evaluate)
2669 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002670#ifdef FEAT_FLOAT
2671 if (rettv->v_type == VAR_FLOAT)
2672 {
2673 f1 = rettv->vval.v_float;
2674 use_float = TRUE;
2675 n1 = 0;
2676 }
2677 else
2678#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002679 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002680 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002681 if (error)
2682 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 }
2684 else
2685 n1 = 0;
2686
2687 /*
2688 * Get the second variable.
2689 */
2690 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002691 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 return FAIL;
2693
2694 if (evaluate)
2695 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002696#ifdef FEAT_FLOAT
2697 if (var2.v_type == VAR_FLOAT)
2698 {
2699 if (!use_float)
2700 {
2701 f1 = n1;
2702 use_float = TRUE;
2703 }
2704 f2 = var2.vval.v_float;
2705 n2 = 0;
2706 }
2707 else
2708#endif
2709 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002710 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002711 clear_tv(&var2);
2712 if (error)
2713 return FAIL;
2714#ifdef FEAT_FLOAT
2715 if (use_float)
2716 f2 = n2;
2717#endif
2718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719
2720 /*
2721 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002722 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002724#ifdef FEAT_FLOAT
2725 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002727 if (op == '*')
2728 f1 = f1 * f2;
2729 else if (op == '/')
2730 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002731# ifdef VMS
2732 /* VMS crashes on divide by zero, work around it */
2733 if (f2 == 0.0)
2734 {
2735 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002736 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002737 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002738 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002739 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02002740 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002741 }
2742 else
2743 f1 = f1 / f2;
2744# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002745 /* We rely on the floating point library to handle divide
2746 * by zero to result in "inf" and not a crash. */
2747 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02002748# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002752 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002753 return FAIL;
2754 }
2755 rettv->v_type = VAR_FLOAT;
2756 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002761 if (op == '*')
2762 n1 = n1 * n2;
2763 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01002764 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01002766 n1 = num_modulus(n1, n2);
2767
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002768 rettv->v_type = VAR_NUMBER;
2769 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 }
2772 }
2773
2774 return OK;
2775}
2776
2777/*
2778 * Handle sixth level expression:
2779 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002780 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002781 * "string" string constant
2782 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 * &option-name option value
2784 * @r register contents
2785 * identifier variable value
2786 * function() function call
2787 * $VAR environment variable
2788 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002789 * [expr, expr] List
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002790 * {key: val, key: val} Dictionary
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002791 * #{key: val, key: val} Dictionary with literal keys
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 *
2793 * Also handle:
2794 * ! in front logical NOT
2795 * - in front unary minus
2796 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 * trailing [] subscript in String or List
2798 * trailing .name entry in Dictionary
Bram Moolenaarac92e252019-08-03 21:58:38 +02002799 * trailing ->name() method call
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 *
2801 * "arg" must point to the first non-white of the expression.
2802 * "arg" is advanced to the next non-white after the recognized expression.
2803 *
2804 * Return OK or FAIL.
2805 */
2806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002807eval7(
2808 char_u **arg,
2809 typval_T *rettv,
2810 int evaluate,
2811 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002813 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 int len;
2815 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 char_u *start_leader, *end_leader;
2817 int ret = OK;
2818 char_u *alias;
2819
2820 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002821 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002822 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002824 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825
2826 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02002827 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 */
2829 start_leader = *arg;
2830 while (**arg == '!' || **arg == '-' || **arg == '+')
2831 *arg = skipwhite(*arg + 1);
2832 end_leader = *arg;
2833
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002834 if (**arg == '.' && (!isdigit(*(*arg + 1))
2835#ifdef FEAT_FLOAT
2836 || current_sctx.sc_version < 2
2837#endif
2838 ))
2839 {
2840 semsg(_(e_invexpr2), *arg);
2841 ++*arg;
2842 return FAIL;
2843 }
2844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 switch (**arg)
2846 {
2847 /*
2848 * Number constant.
2849 */
2850 case '0':
2851 case '1':
2852 case '2':
2853 case '3':
2854 case '4':
2855 case '5':
2856 case '6':
2857 case '7':
2858 case '8':
2859 case '9':
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002860 case '.':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002861 {
2862#ifdef FEAT_FLOAT
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002863 char_u *p;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002864 int get_float = FALSE;
2865
2866 /* We accept a float when the format matches
2867 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002868 * strict to avoid backwards compatibility problems.
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002869 * With script version 2 and later the leading digit can be
2870 * omitted.
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002871 * Don't look for a float after the "." operator, so that
2872 * ":let vers = 1.2.3" doesn't fail. */
Bram Moolenaar558ca4a2019-04-04 18:15:38 +02002873 if (**arg == '.')
2874 p = *arg;
2875 else
2876 p = skipdigits(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00002877 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002879 get_float = TRUE;
2880 p = skipdigits(p + 2);
2881 if (*p == 'e' || *p == 'E')
2882 {
2883 ++p;
2884 if (*p == '-' || *p == '+')
2885 ++p;
2886 if (!vim_isdigit(*p))
2887 get_float = FALSE;
2888 else
2889 p = skipdigits(p + 1);
2890 }
2891 if (ASCII_ISALPHA(*p) || *p == '.')
2892 get_float = FALSE;
2893 }
2894 if (get_float)
2895 {
2896 float_T f;
2897
2898 *arg += string2float(*arg, &f);
2899 if (evaluate)
2900 {
2901 rettv->v_type = VAR_FLOAT;
2902 rettv->vval.v_float = f;
2903 }
2904 }
2905 else
2906#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002907 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002908 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002909 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01002910 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002911
2912 // Blob constant: 0z0123456789abcdef
2913 if (evaluate)
2914 blob = blob_alloc();
2915 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
2916 {
2917 if (!vim_isxdigit(bp[1]))
2918 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002919 if (blob != NULL)
2920 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002921 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002922 ga_clear(&blob->bv_ga);
2923 VIM_CLEAR(blob);
2924 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002925 ret = FAIL;
2926 break;
2927 }
2928 if (blob != NULL)
2929 ga_append(&blob->bv_ga,
2930 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01002931 if (bp[2] == '.' && vim_isxdigit(bp[3]))
2932 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002933 }
2934 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002935 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002936 *arg = bp;
2937 }
2938 else
2939 {
2940 // decimal, hex or octal number
Bram Moolenaar16e9b852019-05-19 19:59:35 +02002941 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0, TRUE);
2942 if (len == 0)
2943 {
2944 semsg(_(e_invexpr2), *arg);
2945 ret = FAIL;
2946 break;
2947 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002948 *arg += len;
2949 if (evaluate)
2950 {
2951 rettv->v_type = VAR_NUMBER;
2952 rettv->vval.v_number = n;
2953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
2955 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
2958 /*
2959 * String constant: "string".
2960 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002961 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 break;
2963
2964 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002965 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002967 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00002968 break;
2969
2970 /*
2971 * List: [expr, expr]
2972 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974 break;
2975
2976 /*
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002977 * Dictionary: #{key: val, key: val}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002978 */
Bram Moolenaar4c6d9042019-07-16 22:04:02 +02002979 case '#': if ((*arg)[1] == '{')
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002980 {
2981 ++*arg;
2982 ret = dict_get_tv(arg, rettv, evaluate, TRUE);
2983 }
2984 else
2985 ret = NOTDONE;
2986 break;
2987
2988 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002989 * Lambda: {arg, arg -> expr}
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002990 * Dictionary: {'key': val, 'key': val}
Bram Moolenaar8c711452005-01-14 21:53:12 +00002991 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02002992 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
2993 if (ret == NOTDONE)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +02002994 ret = dict_get_tv(arg, rettv, evaluate, FALSE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002995 break;
2996
2997 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00002998 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00003000 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 break;
3002
3003 /*
3004 * Environment variable: $VAR.
3005 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003006 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 break;
3008
3009 /*
3010 * Register contents: @r.
3011 */
3012 case '@': ++*arg;
3013 if (evaluate)
3014 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003015 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02003016 rettv->vval.v_string = get_reg_contents(**arg,
3017 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 }
3019 if (**arg != NUL)
3020 ++*arg;
3021 break;
3022
3023 /*
3024 * nested expression: (expression).
3025 */
3026 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003027 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 if (**arg == ')')
3029 ++*arg;
3030 else if (ret == OK)
3031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003032 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003033 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 ret = FAIL;
3035 }
3036 break;
3037
Bram Moolenaar8c711452005-01-14 21:53:12 +00003038 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 break;
3040 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003041
3042 if (ret == NOTDONE)
3043 {
3044 /*
3045 * Must be a variable or function name.
3046 * Can also be a curly-braces kind of name: {expr}.
3047 */
3048 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003049 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003050 if (alias != NULL)
3051 s = alias;
3052
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003053 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003054 ret = FAIL;
3055 else
3056 {
3057 if (**arg == '(') /* recursive! */
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003058 ret = eval_func(arg, s, len, rettv, evaluate, NULL);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003059 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02003060 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003061 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003062 {
3063 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003066 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01003067 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068 }
3069
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 *arg = skipwhite(*arg);
3071
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003072 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02003073 * expr(expr), expr->name(expr) */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003074 if (ret == OK)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003075 ret = handle_subscript(arg, rettv, evaluate, TRUE,
3076 start_leader, &end_leader);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077
3078 /*
3079 * Apply logical NOT and unary '-', from right to left, ignore '+'.
3080 */
3081 if (ret == OK && evaluate && end_leader > start_leader)
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003082 ret = eval7_leader(rettv, start_leader, &end_leader);
3083 return ret;
3084}
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003085
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003086/*
3087 * Apply the leading "!" and "-" before an eval7 expression to "rettv".
3088 * Adjusts "end_leaderp" until it is at "start_leader".
3089 */
3090 static int
3091eval7_leader(typval_T *rettv, char_u *start_leader, char_u **end_leaderp)
3092{
3093 char_u *end_leader = *end_leaderp;
3094 int ret = OK;
3095 int error = FALSE;
3096 varnumber_T val = 0;
3097#ifdef FEAT_FLOAT
3098 float_T f = 0.0;
3099
3100 if (rettv->v_type == VAR_FLOAT)
3101 f = rettv->vval.v_float;
3102 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003103#endif
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003104 val = tv_get_number_chk(rettv, &error);
3105 if (error)
3106 {
3107 clear_tv(rettv);
3108 ret = FAIL;
3109 }
3110 else
3111 {
3112 while (end_leader > start_leader)
3113 {
3114 --end_leader;
3115 if (*end_leader == '!')
3116 {
3117#ifdef FEAT_FLOAT
3118 if (rettv->v_type == VAR_FLOAT)
3119 f = !f;
3120 else
3121#endif
3122 val = !val;
3123 }
3124 else if (*end_leader == '-')
3125 {
3126#ifdef FEAT_FLOAT
3127 if (rettv->v_type == VAR_FLOAT)
3128 f = -f;
3129 else
3130#endif
3131 val = -val;
3132 }
3133 }
3134#ifdef FEAT_FLOAT
3135 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003137 clear_tv(rettv);
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003138 rettv->vval.v_float = f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003140 else
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003141#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003142 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003143 clear_tv(rettv);
3144 rettv->v_type = VAR_NUMBER;
3145 rettv->vval.v_number = val;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 }
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003148 *end_leaderp = end_leader;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149 return ret;
3150}
3151
3152/*
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003153 * Call the function referred to in "rettv".
3154 */
3155 static int
3156call_func_rettv(
3157 char_u **arg,
3158 typval_T *rettv,
3159 int evaluate,
3160 dict_T *selfdict,
3161 typval_T *basetv)
3162{
3163 partial_T *pt = NULL;
3164 funcexe_T funcexe;
3165 typval_T functv;
3166 char_u *s;
3167 int ret;
3168
3169 // need to copy the funcref so that we can clear rettv
3170 if (evaluate)
3171 {
3172 functv = *rettv;
3173 rettv->v_type = VAR_UNKNOWN;
3174
3175 /* Invoke the function. Recursive! */
3176 if (functv.v_type == VAR_PARTIAL)
3177 {
3178 pt = functv.vval.v_partial;
3179 s = partial_name(pt);
3180 }
3181 else
3182 s = functv.vval.v_string;
3183 }
3184 else
3185 s = (char_u *)"";
3186
3187 vim_memset(&funcexe, 0, sizeof(funcexe));
3188 funcexe.firstline = curwin->w_cursor.lnum;
3189 funcexe.lastline = curwin->w_cursor.lnum;
3190 funcexe.evaluate = evaluate;
3191 funcexe.partial = pt;
3192 funcexe.selfdict = selfdict;
3193 funcexe.basetv = basetv;
3194 ret = get_func_tv(s, -1, rettv, arg, &funcexe);
3195
3196 /* Clear the funcref afterwards, so that deleting it while
3197 * evaluating the arguments is possible (see test55). */
3198 if (evaluate)
3199 clear_tv(&functv);
3200
3201 return ret;
3202}
3203
3204/*
3205 * Evaluate "->method()".
3206 * "*arg" points to the '-'.
3207 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3208 */
3209 static int
3210eval_lambda(
3211 char_u **arg,
3212 typval_T *rettv,
3213 int evaluate,
3214 int verbose) /* give error messages */
3215{
3216 typval_T base = *rettv;
3217 int ret;
3218
3219 // Skip over the ->.
3220 *arg += 2;
3221 rettv->v_type = VAR_UNKNOWN;
3222
3223 ret = get_lambda_tv(arg, rettv, evaluate);
3224 if (ret == NOTDONE)
3225 return FAIL;
3226 else if (**arg != '(')
3227 {
3228 if (verbose)
3229 {
3230 if (*skipwhite(*arg) == '(')
3231 semsg(_(e_nowhitespace));
3232 else
3233 semsg(_(e_missingparen), "lambda");
3234 }
3235 clear_tv(rettv);
3236 return FAIL;
3237 }
3238 return call_func_rettv(arg, rettv, evaluate, NULL, &base);
3239}
3240
3241/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02003242 * Evaluate "->method()".
3243 * "*arg" points to the '-'.
3244 * Returns FAIL or OK. "*arg" is advanced to after the ')'.
3245 */
3246 static int
3247eval_method(
3248 char_u **arg,
3249 typval_T *rettv,
3250 int evaluate,
3251 int verbose) /* give error messages */
3252{
3253 char_u *name;
3254 long len;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003255 char_u *alias;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003256 typval_T base = *rettv;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003257 int ret;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003258
3259 // Skip over the ->.
3260 *arg += 2;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003261 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003262
Bram Moolenaarac92e252019-08-03 21:58:38 +02003263 name = *arg;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003264 len = get_name_len(arg, &alias, evaluate, TRUE);
3265 if (alias != NULL)
3266 name = alias;
3267
3268 if (len <= 0)
Bram Moolenaarac92e252019-08-03 21:58:38 +02003269 {
3270 if (verbose)
3271 emsg(_("E260: Missing name after ->"));
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003272 ret = FAIL;
Bram Moolenaarac92e252019-08-03 21:58:38 +02003273 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003274 else
Bram Moolenaarac92e252019-08-03 21:58:38 +02003275 {
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003276 if (**arg != '(')
3277 {
3278 if (verbose)
3279 semsg(_(e_missingparen), name);
3280 ret = FAIL;
3281 }
Bram Moolenaar51841322019-08-08 21:10:01 +02003282 else if (VIM_ISWHITE((*arg)[-1]))
3283 {
3284 if (verbose)
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003285 semsg(_(e_nowhitespace));
Bram Moolenaar51841322019-08-08 21:10:01 +02003286 ret = FAIL;
3287 }
Bram Moolenaar761fdf02019-08-05 23:10:16 +02003288 else
3289 ret = eval_func(arg, name, len, rettv, evaluate, &base);
Bram Moolenaarac92e252019-08-03 21:58:38 +02003290 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02003291
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02003292 // Clear the funcref afterwards, so that deleting it while
3293 // evaluating the arguments is possible (see test55).
Bram Moolenaarac92e252019-08-03 21:58:38 +02003294 if (evaluate)
3295 clear_tv(&base);
3296
Bram Moolenaarac92e252019-08-03 21:58:38 +02003297 return ret;
3298}
3299
3300/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003301 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
3302 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003303 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
3304 */
3305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003306eval_index(
3307 char_u **arg,
3308 typval_T *rettv,
3309 int evaluate,
3310 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003311{
3312 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003313 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003314 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003315 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003316 long len = -1;
3317 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003318 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003319 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003320
Bram Moolenaara03f2332016-02-06 18:09:59 +01003321 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003322 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003323 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003324 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003325 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003326 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003327 return FAIL;
3328 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003329#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01003330 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003331 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003332 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02003333#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01003334 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003335 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003336 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003337 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003338 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01003339 return FAIL;
3340 case VAR_UNKNOWN:
3341 if (evaluate)
3342 return FAIL;
3343 /* FALLTHROUGH */
3344
3345 case VAR_STRING:
3346 case VAR_NUMBER:
3347 case VAR_LIST:
3348 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003349 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003350 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003351 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003352
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02003353 init_tv(&var1);
3354 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003355 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003356 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003357 /*
3358 * dict.name
3359 */
3360 key = *arg + 1;
3361 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
3362 ;
3363 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003364 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003365 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003366 }
3367 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003368 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003369 /*
3370 * something[idx]
3371 *
3372 * Get the (first) variable from inside the [].
3373 */
3374 *arg = skipwhite(*arg + 1);
3375 if (**arg == ':')
3376 empty1 = TRUE;
3377 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
3378 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003379 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003380 {
3381 /* not a number or string */
3382 clear_tv(&var1);
3383 return FAIL;
3384 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003385
3386 /*
3387 * Get the second variable from inside the [:].
3388 */
3389 if (**arg == ':')
3390 {
3391 range = TRUE;
3392 *arg = skipwhite(*arg + 1);
3393 if (**arg == ']')
3394 empty2 = TRUE;
3395 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
3396 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003397 if (!empty1)
3398 clear_tv(&var1);
3399 return FAIL;
3400 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003401 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003402 {
3403 /* not a number or string */
3404 if (!empty1)
3405 clear_tv(&var1);
3406 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003407 return FAIL;
3408 }
3409 }
3410
3411 /* Check for the ']'. */
3412 if (**arg != ']')
3413 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003414 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003415 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003416 clear_tv(&var1);
3417 if (range)
3418 clear_tv(&var2);
3419 return FAIL;
3420 }
3421 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003422 }
3423
3424 if (evaluate)
3425 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003426 n1 = 0;
3427 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003428 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003429 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003430 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003431 }
3432 if (range)
3433 {
3434 if (empty2)
3435 n2 = -1;
3436 else
3437 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003438 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003439 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003440 }
3441 }
3442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003443 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003444 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003445 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003446 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003447 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003448 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003449 case VAR_SPECIAL:
3450 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003451 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003452 break; /* not evaluating, skipping over subscript */
3453
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003454 case VAR_NUMBER:
3455 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003456 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003457 len = (long)STRLEN(s);
3458 if (range)
3459 {
3460 /* The resulting variable is a substring. If the indexes
3461 * are out of range the result is empty. */
3462 if (n1 < 0)
3463 {
3464 n1 = len + n1;
3465 if (n1 < 0)
3466 n1 = 0;
3467 }
3468 if (n2 < 0)
3469 n2 = len + n2;
3470 else if (n2 >= len)
3471 n2 = len;
3472 if (n1 >= len || n2 < 0 || n1 > n2)
3473 s = NULL;
3474 else
3475 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
3476 }
3477 else
3478 {
3479 /* The resulting variable is a string of a single
3480 * character. If the index is too big or negative the
3481 * result is empty. */
3482 if (n1 >= len || n1 < 0)
3483 s = NULL;
3484 else
3485 s = vim_strnsave(s + n1, 1);
3486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003487 clear_tv(rettv);
3488 rettv->v_type = VAR_STRING;
3489 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003490 break;
3491
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003492 case VAR_BLOB:
3493 len = blob_len(rettv->vval.v_blob);
3494 if (range)
3495 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003496 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003497 // are out of range the result is empty.
3498 if (n1 < 0)
3499 {
3500 n1 = len + n1;
3501 if (n1 < 0)
3502 n1 = 0;
3503 }
3504 if (n2 < 0)
3505 n2 = len + n2;
3506 else if (n2 >= len)
3507 n2 = len - 1;
3508 if (n1 >= len || n2 < 0 || n1 > n2)
3509 {
3510 clear_tv(rettv);
3511 rettv->v_type = VAR_BLOB;
3512 rettv->vval.v_blob = NULL;
3513 }
3514 else
3515 {
3516 blob_T *blob = blob_alloc();
3517
3518 if (blob != NULL)
3519 {
3520 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
3521 {
3522 blob_free(blob);
3523 return FAIL;
3524 }
3525 blob->bv_ga.ga_len = n2 - n1 + 1;
3526 for (i = n1; i <= n2; i++)
3527 blob_set(blob, i - n1,
3528 blob_get(rettv->vval.v_blob, i));
3529
3530 clear_tv(rettv);
3531 rettv_blob_set(rettv, blob);
3532 }
3533 }
3534 }
3535 else
3536 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003537 // The resulting variable is a byte value.
3538 // If the index is too big or negative that is an error.
3539 if (n1 < 0)
3540 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003541 if (n1 < len && n1 >= 0)
3542 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01003543 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003544
3545 clear_tv(rettv);
3546 rettv->v_type = VAR_NUMBER;
3547 rettv->vval.v_number = v;
3548 }
3549 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003550 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003551 }
3552 break;
3553
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003554 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003555 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003556 if (n1 < 0)
3557 n1 = len + n1;
3558 if (!empty1 && (n1 < 0 || n1 >= len))
3559 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003560 /* For a range we allow invalid values and return an empty
3561 * list. A list index out of range is an error. */
3562 if (!range)
3563 {
3564 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003565 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003566 return FAIL;
3567 }
3568 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003569 }
3570 if (range)
3571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003572 list_T *l;
3573 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003574
3575 if (n2 < 0)
3576 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003577 else if (n2 >= len)
3578 n2 = len - 1;
3579 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003580 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003581 l = list_alloc();
3582 if (l == NULL)
3583 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003584 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003585 n1 <= n2; ++n1)
3586 {
3587 if (list_append_tv(l, &item->li_tv) == FAIL)
3588 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003589 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003590 return FAIL;
3591 }
3592 item = item->li_next;
3593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003594 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02003595 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003596 }
3597 else
3598 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003599 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003600 clear_tv(rettv);
3601 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003602 }
3603 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003604
3605 case VAR_DICT:
3606 if (range)
3607 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003608 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003609 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00003610 if (len == -1)
3611 clear_tv(&var1);
3612 return FAIL;
3613 }
3614 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003615 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616
3617 if (len == -1)
3618 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003619 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02003620 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003621 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003622 clear_tv(&var1);
3623 return FAIL;
3624 }
3625 }
3626
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003627 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003628
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003629 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003630 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003631 if (len == -1)
3632 clear_tv(&var1);
3633 if (item == NULL)
3634 return FAIL;
3635
3636 copy_tv(&item->di_tv, &var1);
3637 clear_tv(rettv);
3638 *rettv = var1;
3639 }
3640 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003641 }
3642 }
3643
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003644 return OK;
3645}
3646
3647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 * Get an option value.
3649 * "arg" points to the '&' or '+' before the option name.
3650 * "arg" is advanced to character after the option name.
3651 * Return OK or FAIL.
3652 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02003653 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003654get_option_tv(
3655 char_u **arg,
3656 typval_T *rettv, /* when NULL, only check if option exists */
3657 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658{
3659 char_u *option_end;
3660 long numval;
3661 char_u *stringval;
3662 int opt_type;
3663 int c;
3664 int working = (**arg == '+'); /* has("+option") */
3665 int ret = OK;
3666 int opt_flags;
3667
3668 /*
3669 * Isolate the option name and find its value.
3670 */
3671 option_end = find_option_end(arg, &opt_flags);
3672 if (option_end == NULL)
3673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003674 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003675 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677 }
3678
3679 if (!evaluate)
3680 {
3681 *arg = option_end;
3682 return OK;
3683 }
3684
3685 c = *option_end;
3686 *option_end = NUL;
3687 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003688 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689
3690 if (opt_type == -3) /* invalid name */
3691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003692 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003693 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 ret = FAIL;
3695 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003696 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 {
3698 if (opt_type == -2) /* hidden string option */
3699 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003700 rettv->v_type = VAR_STRING;
3701 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 else if (opt_type == -1) /* hidden number option */
3704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003705 rettv->v_type = VAR_NUMBER;
3706 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 }
3708 else if (opt_type == 1) /* number option */
3709 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003710 rettv->v_type = VAR_NUMBER;
3711 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 }
3713 else /* string option */
3714 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003715 rettv->v_type = VAR_STRING;
3716 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718 }
3719 else if (working && (opt_type == -2 || opt_type == -1))
3720 ret = FAIL;
3721
3722 *option_end = c; /* put back for error messages */
3723 *arg = option_end;
3724
3725 return ret;
3726}
3727
3728/*
3729 * Allocate a variable for a string constant.
3730 * Return OK or FAIL.
3731 */
3732 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003733get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
3735 char_u *p;
3736 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 int extra = 0;
3738
3739 /*
3740 * Find the end of the string, skipping backslashed characters.
3741 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003742 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
3744 if (*p == '\\' && p[1] != NUL)
3745 {
3746 ++p;
3747 /* A "\<x>" form occupies at least 4 characters, and produces up
3748 * to 6 characters: reserve space for 2 extra */
3749 if (*p == '<')
3750 extra += 2;
3751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753
3754 if (*p != '"')
3755 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003756 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 return FAIL;
3758 }
3759
3760 /* If only parsing, set *arg and return here */
3761 if (!evaluate)
3762 {
3763 *arg = p + 1;
3764 return OK;
3765 }
3766
3767 /*
3768 * Copy the string into allocated memory, handling backslashed
3769 * characters.
3770 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003771 name = alloc(p - *arg + extra);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 if (name == NULL)
3773 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003774 rettv->v_type = VAR_STRING;
3775 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
Bram Moolenaar8c711452005-01-14 21:53:12 +00003777 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 {
3779 if (*p == '\\')
3780 {
3781 switch (*++p)
3782 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003783 case 'b': *name++ = BS; ++p; break;
3784 case 'e': *name++ = ESC; ++p; break;
3785 case 'f': *name++ = FF; ++p; break;
3786 case 'n': *name++ = NL; ++p; break;
3787 case 'r': *name++ = CAR; ++p; break;
3788 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789
3790 case 'X': /* hex: "\x1", "\x12" */
3791 case 'x':
3792 case 'u': /* Unicode: "\u0023" */
3793 case 'U':
3794 if (vim_isxdigit(p[1]))
3795 {
3796 int n, nr;
3797 int c = toupper(*p);
3798
3799 if (c == 'X')
3800 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003801 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02003803 else
3804 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 nr = 0;
3806 while (--n >= 0 && vim_isxdigit(p[1]))
3807 {
3808 ++p;
3809 nr = (nr << 4) + hex2nr(*p);
3810 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003811 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 /* For "\u" store the number according to
3813 * 'encoding'. */
3814 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00003815 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003817 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 break;
3820
3821 /* octal: "\1", "\12", "\123" */
3822 case '0':
3823 case '1':
3824 case '2':
3825 case '3':
3826 case '4':
3827 case '5':
3828 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00003829 case '7': *name = *p++ - '0';
3830 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003832 *name = (*name << 3) + *p++ - '0';
3833 if (*p >= '0' && *p <= '7')
3834 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003836 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837 break;
3838
3839 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02003840 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 if (extra != 0)
3842 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003843 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 break;
3845 }
3846 /* FALLTHROUGH */
3847
Bram Moolenaar8c711452005-01-14 21:53:12 +00003848 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 break;
3850 }
3851 }
3852 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00003853 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003856 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02003857 if (*p != NUL) /* just in case */
3858 ++p;
3859 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 return OK;
3862}
3863
3864/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003865 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 * Return OK or FAIL.
3867 */
3868 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003869get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870{
3871 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003872 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003873 int reduce = 0;
3874
3875 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003876 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003877 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003878 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003879 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003880 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003881 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003882 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003883 break;
3884 ++reduce;
3885 ++p;
3886 }
3887 }
3888
Bram Moolenaar8c711452005-01-14 21:53:12 +00003889 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003890 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003891 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003892 return FAIL;
3893 }
3894
Bram Moolenaar8c711452005-01-14 21:53:12 +00003895 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003896 if (!evaluate)
3897 {
3898 *arg = p + 1;
3899 return OK;
3900 }
3901
3902 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00003903 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003904 */
Bram Moolenaar964b3742019-05-24 18:54:09 +02003905 str = alloc((p - *arg) - reduce);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003906 if (str == NULL)
3907 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003908 rettv->v_type = VAR_STRING;
3909 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003910
Bram Moolenaar8c711452005-01-14 21:53:12 +00003911 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003912 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003913 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003914 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003915 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003916 break;
3917 ++p;
3918 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003919 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00003921 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003922 *arg = p + 1;
3923
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00003924 return OK;
3925}
3926
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003927/*
3928 * Return the function name of the partial.
3929 */
3930 char_u *
3931partial_name(partial_T *pt)
3932{
3933 if (pt->pt_name != NULL)
3934 return pt->pt_name;
3935 return pt->pt_func->uf_name;
3936}
3937
Bram Moolenaarddecc252016-04-06 22:59:37 +02003938 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003939partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02003940{
3941 int i;
3942
3943 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003944 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003945 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003946 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003947 if (pt->pt_name != NULL)
3948 {
3949 func_unref(pt->pt_name);
3950 vim_free(pt->pt_name);
3951 }
3952 else
3953 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003954 vim_free(pt);
3955}
3956
3957/*
3958 * Unreference a closure: decrement the reference count and free it when it
3959 * becomes zero.
3960 */
3961 void
3962partial_unref(partial_T *pt)
3963{
3964 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02003965 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02003966}
3967
Bram Moolenaar67b3f992010-11-10 20:41:57 +01003968static int tv_equal_recurse_limit;
3969
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003970 static int
3971func_equal(
3972 typval_T *tv1,
3973 typval_T *tv2,
3974 int ic) /* ignore case */
3975{
3976 char_u *s1, *s2;
3977 dict_T *d1, *d2;
3978 int a1, a2;
3979 int i;
3980
3981 /* empty and NULL function name considered the same */
3982 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003983 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003984 if (s1 != NULL && *s1 == NUL)
3985 s1 = NULL;
3986 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003987 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02003988 if (s2 != NULL && *s2 == NUL)
3989 s2 = NULL;
3990 if (s1 == NULL || s2 == NULL)
3991 {
3992 if (s1 != s2)
3993 return FALSE;
3994 }
3995 else if (STRCMP(s1, s2) != 0)
3996 return FALSE;
3997
3998 /* empty dict and NULL dict is different */
3999 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
4000 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
4001 if (d1 == NULL || d2 == NULL)
4002 {
4003 if (d1 != d2)
4004 return FALSE;
4005 }
4006 else if (!dict_equal(d1, d2, ic, TRUE))
4007 return FALSE;
4008
4009 /* empty list and no list considered the same */
4010 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
4011 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
4012 if (a1 != a2)
4013 return FALSE;
4014 for (i = 0; i < a1; ++i)
4015 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
4016 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
4017 return FALSE;
4018
4019 return TRUE;
4020}
4021
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004022/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004023 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004024 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004025 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004026 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004027 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004028tv_equal(
4029 typval_T *tv1,
4030 typval_T *tv2,
4031 int ic, /* ignore case */
4032 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004033{
4034 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004035 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004036 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004037 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004038
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004039 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004040 * recursiveness to a limit. We guess they are equal then.
4041 * A fixed limit has the problem of still taking an awful long time.
4042 * Reduce the limit every time running into it. That should work fine for
4043 * deeply linked structures that are not recursively linked and catch
4044 * recursiveness quickly. */
4045 if (!recursive)
4046 tv_equal_recurse_limit = 1000;
4047 if (recursive_cnt >= tv_equal_recurse_limit)
4048 {
4049 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00004050 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004051 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004052
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02004053 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
4054 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02004055 if ((tv1->v_type == VAR_FUNC
4056 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
4057 && (tv2->v_type == VAR_FUNC
4058 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
4059 {
4060 ++recursive_cnt;
4061 r = func_equal(tv1, tv2, ic);
4062 --recursive_cnt;
4063 return r;
4064 }
4065
4066 if (tv1->v_type != tv2->v_type)
4067 return FALSE;
4068
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004069 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004070 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004071 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004072 ++recursive_cnt;
4073 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
4074 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004075 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004076
4077 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004078 ++recursive_cnt;
4079 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
4080 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00004081 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004082
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004083 case VAR_BLOB:
4084 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
4085
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004086 case VAR_NUMBER:
4087 return tv1->vval.v_number == tv2->vval.v_number;
4088
4089 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004090 s1 = tv_get_string_buf(tv1, buf1);
4091 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004092 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004093
4094 case VAR_SPECIAL:
4095 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01004096
4097 case VAR_FLOAT:
4098#ifdef FEAT_FLOAT
4099 return tv1->vval.v_float == tv2->vval.v_float;
4100#endif
4101 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004102#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01004103 return tv1->vval.v_job == tv2->vval.v_job;
4104#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01004105 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004106#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01004107 return tv1->vval.v_channel == tv2->vval.v_channel;
4108#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004109 case VAR_FUNC:
4110 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004111 case VAR_UNKNOWN:
4112 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004113 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00004114
Bram Moolenaara03f2332016-02-06 18:09:59 +01004115 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
4116 * does not equal anything, not even itself. */
4117 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004118}
4119
4120/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004121 * Return the next (unique) copy ID.
4122 * Used for serializing nested structures.
4123 */
4124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004125get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004126{
4127 current_copyID += COPYID_INC;
4128 return current_copyID;
4129}
4130
4131/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004132 * Garbage collection for lists and dictionaries.
4133 *
4134 * We use reference counts to be able to free most items right away when they
4135 * are no longer used. But for composite items it's possible that it becomes
4136 * unused while the reference count is > 0: When there is a recursive
4137 * reference. Example:
4138 * :let l = [1, 2, 3]
4139 * :let d = {9: l}
4140 * :let l[1] = d
4141 *
4142 * Since this is quite unusual we handle this with garbage collection: every
4143 * once in a while find out which lists and dicts are not referenced from any
4144 * variable.
4145 *
4146 * Here is a good reference text about garbage collection (refers to Python
4147 * but it applies to all reference-counting mechanisms):
4148 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00004149 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00004150
4151/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004152 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02004153 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004154 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00004155 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004156 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004157garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004158{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004159 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004160 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004161 buf_T *buf;
4162 win_T *wp;
Bram Moolenaar934b1362015-02-04 23:06:45 +01004163 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004164 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004165
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004166 if (!testing)
4167 {
4168 /* Only do this once. */
4169 want_garbage_collect = FALSE;
4170 may_garbage_collect = FALSE;
4171 garbage_collect_at_exit = FALSE;
4172 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00004173
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004174 /* We advance by two because we add one for items referenced through
4175 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004176 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004177
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004178 /*
4179 * 1. Go through all accessible variables and mark all lists and dicts
4180 * with copyID.
4181 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004182
4183 /* Don't free variables in the previous_funccal list unless they are only
4184 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00004185 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004186 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004187
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004188 /* script-local variables */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004189 abort = abort || garbage_collect_scriptvars(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004190
4191 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02004192 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004193 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
4194 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004195
4196 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004197 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004198 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4199 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02004200 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004201 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
4202 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004203#ifdef FEAT_TEXT_PROP
4204 for (wp = first_popupwin; wp != NULL; wp = wp->w_next)
4205 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4206 NULL, NULL);
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004207 FOR_ALL_TABPAGES(tp)
Bram Moolenaar9c27b1c2019-05-26 18:48:13 +02004208 for (wp = tp->tp_first_popupwin; wp != NULL; wp = wp->w_next)
Bram Moolenaar4d784b22019-05-25 19:51:39 +02004209 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
4210 NULL, NULL);
4211#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004212
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004213 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02004214 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004215 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
4216 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004217 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004218 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004219
4220 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004221 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004222
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02004223 /* named functions (matters for closures) */
4224 abort = abort || set_ref_in_functions(copyID);
4225
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004226 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004227 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02004228
Bram Moolenaard812df62008-11-09 12:46:09 +00004229 /* v: vars */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02004230 abort = abort || garbage_collect_vimvars(copyID);
Bram Moolenaard812df62008-11-09 12:46:09 +00004231
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004232 // callbacks in buffers
4233 abort = abort || set_ref_in_buffers(copyID);
4234
Bram Moolenaar1dced572012-04-05 16:54:08 +02004235#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004236 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02004237#endif
4238
Bram Moolenaardb913952012-06-29 12:54:53 +02004239#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004240 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004241#endif
4242
4243#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004244 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02004245#endif
4246
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01004247#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02004248 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02004249 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004250#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02004251#ifdef FEAT_NETBEANS_INTG
4252 abort = abort || set_ref_in_nb_channel(copyID);
4253#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01004254
Bram Moolenaare3188e22016-05-31 21:13:04 +02004255#ifdef FEAT_TIMERS
4256 abort = abort || set_ref_in_timer(copyID);
4257#endif
4258
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02004259#ifdef FEAT_QUICKFIX
4260 abort = abort || set_ref_in_quickfix(copyID);
4261#endif
4262
Bram Moolenaara2c45a12017-07-27 22:14:59 +02004263#ifdef FEAT_TERMINAL
4264 abort = abort || set_ref_in_term(copyID);
4265#endif
4266
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004267#ifdef FEAT_TEXT_PROP
4268 abort = abort || set_ref_in_popups(copyID);
4269#endif
4270
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004271 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004272 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004273 /*
4274 * 2. Free lists and dictionaries that are not referenced.
4275 */
4276 did_free = free_unref_items(copyID);
4277
4278 /*
4279 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004280 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004281 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004282 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004283 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004284 else if (p_verbose > 0)
4285 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01004286 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004287 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004288
4289 return did_free;
4290}
4291
4292/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004293 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004294 */
4295 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004296free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004297{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00004298 int did_free = FALSE;
4299
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004300 /* Let all "free" functions know that we are here. This means no
4301 * dictionaries, lists, channels or jobs are to be freed, because we will
4302 * do that here. */
4303 in_free_unref_items = TRUE;
4304
4305 /*
4306 * PASS 1: free the contents of the items. We don't free the items
4307 * themselves yet, so that it is possible to decrement refcount counters
4308 */
4309
Bram Moolenaarcd524592016-07-17 14:57:05 +02004310 /* Go through the list of dicts and free items without the copyID. */
4311 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004312
Bram Moolenaarda861d62016-07-17 15:46:27 +02004313 /* Go through the list of lists and free items without the copyID. */
4314 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004315
4316#ifdef FEAT_JOB_CHANNEL
4317 /* Go through the list of jobs and free items without the copyID. This
4318 * must happen before doing channels, because jobs refer to channels, but
4319 * the reference from the channel to the job isn't tracked. */
4320 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
4321
4322 /* Go through the list of channels and free items without the copyID. */
4323 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
4324#endif
4325
4326 /*
4327 * PASS 2: free the items themselves.
4328 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004329 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02004330 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01004331
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004332#ifdef FEAT_JOB_CHANNEL
4333 /* Go through the list of jobs and free items without the copyID. This
4334 * must happen before doing channels, because jobs refer to channels, but
4335 * the reference from the channel to the job isn't tracked. */
4336 free_unused_jobs(copyID, COPYID_MASK);
4337
4338 /* Go through the list of channels and free items without the copyID. */
4339 free_unused_channels(copyID, COPYID_MASK);
4340#endif
4341
4342 in_free_unref_items = FALSE;
4343
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004344 return did_free;
4345}
4346
4347/*
4348 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004349 * "list_stack" is used to add lists to be marked. Can be NULL.
4350 *
4351 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004352 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004354set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004355{
4356 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004357 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004358 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004359 hashtab_T *cur_ht;
4360 ht_stack_T *ht_stack = NULL;
4361 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004362
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004363 cur_ht = ht;
4364 for (;;)
4365 {
4366 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004367 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004368 /* Mark each item in the hashtab. If the item contains a hashtab
4369 * it is added to ht_stack, if it contains a list it is added to
4370 * list_stack. */
4371 todo = (int)cur_ht->ht_used;
4372 for (hi = cur_ht->ht_array; todo > 0; ++hi)
4373 if (!HASHITEM_EMPTY(hi))
4374 {
4375 --todo;
4376 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
4377 &ht_stack, list_stack);
4378 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004379 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004380
4381 if (ht_stack == NULL)
4382 break;
4383
4384 /* take an item from the stack */
4385 cur_ht = ht_stack->ht;
4386 tempitem = ht_stack;
4387 ht_stack = ht_stack->prev;
4388 free(tempitem);
4389 }
4390
4391 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004392}
4393
4394/*
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004395 * Mark a dict and its items with "copyID".
4396 * Returns TRUE if setting references failed somehow.
4397 */
4398 int
4399set_ref_in_dict(dict_T *d, int copyID)
4400{
4401 if (d != NULL && d->dv_copyID != copyID)
4402 {
4403 d->dv_copyID = copyID;
4404 return set_ref_in_ht(&d->dv_hashtab, copyID, NULL);
4405 }
4406 return FALSE;
4407}
4408
4409/*
4410 * Mark a list and its items with "copyID".
4411 * Returns TRUE if setting references failed somehow.
4412 */
4413 int
4414set_ref_in_list(list_T *ll, int copyID)
4415{
4416 if (ll != NULL && ll->lv_copyID != copyID)
4417 {
4418 ll->lv_copyID = copyID;
4419 return set_ref_in_list_items(ll, copyID, NULL);
4420 }
4421 return FALSE;
4422}
4423
4424/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004425 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004426 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4427 *
4428 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004429 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004430 int
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004431set_ref_in_list_items(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004432{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004433 listitem_T *li;
4434 int abort = FALSE;
4435 list_T *cur_l;
4436 list_stack_T *list_stack = NULL;
4437 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004438
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004439 cur_l = l;
4440 for (;;)
4441 {
4442 if (!abort)
4443 /* Mark each item in the list. If the item contains a hashtab
4444 * it is added to ht_stack, if it contains a list it is added to
4445 * list_stack. */
4446 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
4447 abort = abort || set_ref_in_item(&li->li_tv, copyID,
4448 ht_stack, &list_stack);
4449 if (list_stack == NULL)
4450 break;
4451
4452 /* take an item from the stack */
4453 cur_l = list_stack->list;
4454 tempitem = list_stack;
4455 list_stack = list_stack->prev;
4456 free(tempitem);
4457 }
4458
4459 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004460}
4461
4462/*
4463 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004464 * "list_stack" is used to add lists to be marked. Can be NULL.
4465 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
4466 *
4467 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004468 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004470set_ref_in_item(
4471 typval_T *tv,
4472 int copyID,
4473 ht_stack_T **ht_stack,
4474 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00004475{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004476 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004477
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004478 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004479 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004480 dict_T *dd = tv->vval.v_dict;
4481
Bram Moolenaara03f2332016-02-06 18:09:59 +01004482 if (dd != NULL && dd->dv_copyID != copyID)
4483 {
4484 /* Didn't see this dict yet. */
4485 dd->dv_copyID = copyID;
4486 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004487 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004488 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
4489 }
4490 else
4491 {
4492 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
4493 if (newitem == NULL)
4494 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004495 else
4496 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004497 newitem->ht = &dd->dv_hashtab;
4498 newitem->prev = *ht_stack;
4499 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004500 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004501 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004502 }
4503 }
4504 else if (tv->v_type == VAR_LIST)
4505 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004506 list_T *ll = tv->vval.v_list;
4507
Bram Moolenaara03f2332016-02-06 18:09:59 +01004508 if (ll != NULL && ll->lv_copyID != copyID)
4509 {
4510 /* Didn't see this list yet. */
4511 ll->lv_copyID = copyID;
4512 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00004513 {
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02004514 abort = set_ref_in_list_items(ll, copyID, ht_stack);
Bram Moolenaara03f2332016-02-06 18:09:59 +01004515 }
4516 else
4517 {
4518 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004519 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004520 if (newitem == NULL)
4521 abort = TRUE;
4522 else
4523 {
4524 newitem->list = ll;
4525 newitem->prev = *list_stack;
4526 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004527 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004528 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01004529 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00004530 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004531 else if (tv->v_type == VAR_FUNC)
4532 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004533 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004534 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004535 else if (tv->v_type == VAR_PARTIAL)
4536 {
4537 partial_T *pt = tv->vval.v_partial;
4538 int i;
4539
4540 /* A partial does not have a copyID, because it cannot contain itself.
4541 */
4542 if (pt != NULL)
4543 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004544 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004545
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004546 if (pt->pt_dict != NULL)
4547 {
4548 typval_T dtv;
4549
4550 dtv.v_type = VAR_DICT;
4551 dtv.vval.v_dict = pt->pt_dict;
4552 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4553 }
4554
4555 for (i = 0; i < pt->pt_argc; ++i)
4556 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
4557 ht_stack, list_stack);
4558 }
4559 }
4560#ifdef FEAT_JOB_CHANNEL
4561 else if (tv->v_type == VAR_JOB)
4562 {
4563 job_T *job = tv->vval.v_job;
4564 typval_T dtv;
4565
4566 if (job != NULL && job->jv_copyID != copyID)
4567 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004568 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004569 if (job->jv_channel != NULL)
4570 {
4571 dtv.v_type = VAR_CHANNEL;
4572 dtv.vval.v_channel = job->jv_channel;
4573 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4574 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004575 if (job->jv_exit_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004576 {
4577 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004578 dtv.vval.v_partial = job->jv_exit_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004579 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4580 }
4581 }
4582 }
4583 else if (tv->v_type == VAR_CHANNEL)
4584 {
4585 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004586 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004587 typval_T dtv;
4588 jsonq_T *jq;
4589 cbq_T *cq;
4590
4591 if (ch != NULL && ch->ch_copyID != copyID)
4592 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02004593 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02004594 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004595 {
4596 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
4597 jq = jq->jq_next)
4598 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
4599 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
4600 cq = cq->cq_next)
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004601 if (cq->cq_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004602 {
4603 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004604 dtv.vval.v_partial = cq->cq_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004605 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4606 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004607 if (ch->ch_part[part].ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004608 {
4609 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004610 dtv.vval.v_partial =
4611 ch->ch_part[part].ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004612 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4613 }
4614 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004615 if (ch->ch_callback.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004616 {
4617 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004618 dtv.vval.v_partial = ch->ch_callback.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004619 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4620 }
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004621 if (ch->ch_close_cb.cb_partial != NULL)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004622 {
4623 dtv.v_type = VAR_PARTIAL;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02004624 dtv.vval.v_partial = ch->ch_close_cb.cb_partial;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004625 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
4626 }
4627 }
4628 }
4629#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01004630 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00004631}
4632
Bram Moolenaar17a13432016-01-24 14:22:10 +01004633 static char *
4634get_var_special_name(int nr)
4635{
4636 switch (nr)
4637 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01004638 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01004639 case VVAL_TRUE: return "v:true";
4640 case VVAL_NONE: return "v:none";
4641 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01004642 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01004643 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01004644 return "42";
4645}
4646
Bram Moolenaar8c711452005-01-14 21:53:12 +00004647/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004648 * Return a string with the string representation of a variable.
4649 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004650 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004651 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02004652 * When both "echo_style" and "composite_val" are FALSE, put quotes around
4653 * stings as "string()", otherwise does not put quotes around strings, as
4654 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004655 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
4656 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004657 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004658 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004659 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004660echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01004661 typval_T *tv,
4662 char_u **tofree,
4663 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004664 int copyID,
4665 int echo_style,
4666 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02004667 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004668{
Bram Moolenaare9a41262005-01-15 22:18:47 +00004669 static int recurse = 0;
4670 char_u *r = NULL;
4671
Bram Moolenaar33570922005-01-25 22:26:29 +00004672 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00004673 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02004674 if (!did_echo_string_emsg)
4675 {
4676 /* Only give this message once for a recursive call to avoid
4677 * flooding the user with errors. And stop iterating over lists
4678 * and dicts. */
4679 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004680 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02004681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004682 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02004683 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 }
4685 ++recurse;
4686
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004687 switch (tv->v_type)
4688 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004689 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004690 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004691 {
4692 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02004693 r = tv->vval.v_string;
4694 if (r == NULL)
4695 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004696 }
4697 else
4698 {
4699 *tofree = string_quote(tv->vval.v_string, FALSE);
4700 r = *tofree;
4701 }
4702 break;
4703
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004704 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004705 if (echo_style)
4706 {
4707 *tofree = NULL;
4708 r = tv->vval.v_string;
4709 }
4710 else
4711 {
4712 *tofree = string_quote(tv->vval.v_string, TRUE);
4713 r = *tofree;
4714 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004715 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004716
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004717 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004718 {
4719 partial_T *pt = tv->vval.v_partial;
4720 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02004721 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01004722 garray_T ga;
4723 int i;
4724 char_u *tf;
4725
4726 ga_init2(&ga, 1, 100);
4727 ga_concat(&ga, (char_u *)"function(");
4728 if (fname != NULL)
4729 {
4730 ga_concat(&ga, fname);
4731 vim_free(fname);
4732 }
4733 if (pt != NULL && pt->pt_argc > 0)
4734 {
4735 ga_concat(&ga, (char_u *)", [");
4736 for (i = 0; i < pt->pt_argc; ++i)
4737 {
4738 if (i > 0)
4739 ga_concat(&ga, (char_u *)", ");
4740 ga_concat(&ga,
4741 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
4742 vim_free(tf);
4743 }
4744 ga_concat(&ga, (char_u *)"]");
4745 }
4746 if (pt != NULL && pt->pt_dict != NULL)
4747 {
4748 typval_T dtv;
4749
4750 ga_concat(&ga, (char_u *)", ");
4751 dtv.v_type = VAR_DICT;
4752 dtv.vval.v_dict = pt->pt_dict;
4753 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
4754 vim_free(tf);
4755 }
4756 ga_concat(&ga, (char_u *)")");
4757
4758 *tofree = ga.ga_data;
4759 r = *tofree;
4760 break;
4761 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004762
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004763 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01004764 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004765 break;
4766
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004767 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004768 if (tv->vval.v_list == NULL)
4769 {
4770 *tofree = NULL;
4771 r = NULL;
4772 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004773 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
4774 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004775 {
4776 *tofree = NULL;
4777 r = (char_u *)"[...]";
4778 }
4779 else
4780 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004781 int old_copyID = tv->vval.v_list->lv_copyID;
4782
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004783 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004784 *tofree = list2string(tv, copyID, restore_copyID);
4785 if (restore_copyID)
4786 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004787 r = *tofree;
4788 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004789 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004790
Bram Moolenaar8c711452005-01-14 21:53:12 +00004791 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004792 if (tv->vval.v_dict == NULL)
4793 {
4794 *tofree = NULL;
4795 r = NULL;
4796 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004797 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
4798 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004799 {
4800 *tofree = NULL;
4801 r = (char_u *)"{...}";
4802 }
4803 else
4804 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004805 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004806 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004807 *tofree = dict2string(tv, copyID, restore_copyID);
4808 if (restore_copyID)
4809 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004810 r = *tofree;
4811 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004812 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004813
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004814 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004815 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02004816 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004817 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004818 break;
4819
Bram Moolenaar835dc632016-02-07 14:27:38 +01004820 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004821 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00004822 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004823 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02004824 if (composite_val)
4825 {
4826 *tofree = string_quote(r, FALSE);
4827 r = *tofree;
4828 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004829 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00004830
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004832#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004833 *tofree = NULL;
4834 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
4835 r = numbuf;
4836 break;
4837#endif
4838
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004839 case VAR_SPECIAL:
4840 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01004841 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004842 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004843 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004844
Bram Moolenaar8502c702014-06-17 12:51:16 +02004845 if (--recurse == 0)
4846 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00004847 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004848}
4849
4850/*
4851 * Return a string with the string representation of a variable.
4852 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4853 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004854 * Does not put quotes around strings, as ":echo" displays values.
4855 * When "copyID" is not NULL replace recursive lists and dicts with "...".
4856 * May return NULL.
4857 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02004858 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004859echo_string(
4860 typval_T *tv,
4861 char_u **tofree,
4862 char_u *numbuf,
4863 int copyID)
4864{
4865 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
4866}
4867
4868/*
4869 * Return a string with the string representation of a variable.
4870 * If the memory is allocated "tofree" is set to it, otherwise NULL.
4871 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004872 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00004873 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004874 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02004875 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004876tv2string(
4877 typval_T *tv,
4878 char_u **tofree,
4879 char_u *numbuf,
4880 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004881{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02004882 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004883}
4884
4885/*
Bram Moolenaar33570922005-01-25 22:26:29 +00004886 * Return string "str" in ' quotes, doubling ' characters.
4887 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00004888 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004889 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02004890 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004891string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004892{
Bram Moolenaar33570922005-01-25 22:26:29 +00004893 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004894 char_u *p, *r, *s;
4895
Bram Moolenaar33570922005-01-25 22:26:29 +00004896 len = (function ? 13 : 3);
4897 if (str != NULL)
4898 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004899 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004900 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00004901 if (*p == '\'')
4902 ++len;
4903 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004904 s = r = alloc(len);
4905 if (r != NULL)
4906 {
4907 if (function)
4908 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004909 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004910 r += 10;
4911 }
4912 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00004913 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00004914 if (str != NULL)
4915 for (p = str; *p != NUL; )
4916 {
4917 if (*p == '\'')
4918 *r++ = '\'';
4919 MB_COPY_CHAR(p, r);
4920 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004921 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004922 if (function)
4923 *r++ = ')';
4924 *r++ = NUL;
4925 }
4926 return s;
4927}
4928
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004929#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930/*
4931 * Convert the string "text" to a floating point number.
4932 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
4933 * this always uses a decimal point.
4934 * Returns the length of the text that was consumed.
4935 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004937string2float(
4938 char_u *text,
4939 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004940{
4941 char *s = (char *)text;
4942 float_T f;
4943
Bram Moolenaar62473612017-01-08 19:25:40 +01004944 /* MS-Windows does not deal with "inf" and "nan" properly. */
4945 if (STRNICMP(text, "inf", 3) == 0)
4946 {
4947 *value = INFINITY;
4948 return 3;
4949 }
4950 if (STRNICMP(text, "-inf", 3) == 0)
4951 {
4952 *value = -INFINITY;
4953 return 4;
4954 }
4955 if (STRNICMP(text, "nan", 3) == 0)
4956 {
4957 *value = NAN;
4958 return 3;
4959 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004960 f = strtod(s, &s);
4961 *value = f;
4962 return (int)((char_u *)s - text);
4963}
4964#endif
4965
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00004966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 * Get the value of an environment variable.
4968 * "arg" is pointing to the '$'. It is advanced to after the name.
4969 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004970 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 */
4972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004973get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
4975 char_u *string = NULL;
4976 int len;
4977 int cc;
4978 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00004979 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
4981 ++*arg;
4982 name = *arg;
4983 len = get_env_len(arg);
4984 if (evaluate)
4985 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004986 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01004987 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00004988
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004989 cc = name[len];
4990 name[len] = NUL;
4991 /* first try vim_getenv(), fast for normal environment vars */
4992 string = vim_getenv(name, &mustfree);
4993 if (string != NULL && *string != NUL)
4994 {
4995 if (!mustfree)
4996 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02004998 else
4999 {
5000 if (mustfree)
5001 vim_free(string);
5002
5003 /* next try expanding things like $VIM and ${HOME} */
5004 string = expand_env_save(name - 1);
5005 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01005006 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02005007 }
5008 name[len] = cc;
5009
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005010 rettv->v_type = VAR_STRING;
5011 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
5013
5014 return OK;
5015}
5016
Bram Moolenaard6e256c2011-12-14 15:32:50 +01005017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005019 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005021 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005022var2fpos(
5023 typval_T *varp,
5024 int dollar_lnum, /* TRUE when $ is last line */
5025 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005027 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00005029 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030
Bram Moolenaara5525202006-03-02 22:52:09 +00005031 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005032 if (varp->v_type == VAR_LIST)
5033 {
5034 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005035 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00005036 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00005037 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005038
5039 l = varp->vval.v_list;
5040 if (l == NULL)
5041 return NULL;
5042
5043 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005044 pos.lnum = list_find_nr(l, 0L, &error);
5045 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005046 return NULL; /* invalid line number */
5047
5048 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005049 pos.col = list_find_nr(l, 1L, &error);
5050 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005051 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005052 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00005053
5054 /* We accept "$" for the column number: last column. */
5055 li = list_find(l, 1L);
5056 if (li != NULL && li->li_tv.v_type == VAR_STRING
5057 && li->li_tv.vval.v_string != NULL
5058 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
5059 pos.col = len + 1;
5060
Bram Moolenaara5525202006-03-02 22:52:09 +00005061 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00005062 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005063 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00005064 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005065
Bram Moolenaara5525202006-03-02 22:52:09 +00005066 /* Get the virtual offset. Defaults to zero. */
5067 pos.coladd = list_find_nr(l, 2L, &error);
5068 if (error)
5069 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005070
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005071 return &pos;
5072 }
5073
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005074 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005075 if (name == NULL)
5076 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005077 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005079 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
5080 {
5081 if (VIsual_active)
5082 return &VIsual;
5083 return &curwin->w_cursor;
5084 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00005085 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01005087 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
5089 return NULL;
5090 return pp;
5091 }
Bram Moolenaara5525202006-03-02 22:52:09 +00005092
Bram Moolenaara5525202006-03-02 22:52:09 +00005093 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00005094
Bram Moolenaar477933c2007-07-17 14:32:23 +00005095 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005096 {
5097 pos.col = 0;
5098 if (name[1] == '0') /* "w0": first visible line */
5099 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005100 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005101 /* In silent Ex mode topline is zero, but that's not a valid line
5102 * number; use one instead. */
5103 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005104 return &pos;
5105 }
5106 else if (name[1] == '$') /* "w$": last visible line */
5107 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00005108 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02005109 /* In silent Ex mode botline is zero, return zero then. */
5110 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00005111 return &pos;
5112 }
5113 }
5114 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00005116 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
5118 pos.lnum = curbuf->b_ml.ml_line_count;
5119 pos.col = 0;
5120 }
5121 else
5122 {
5123 pos.lnum = curwin->w_cursor.lnum;
5124 pos.col = (colnr_T)STRLEN(ml_get_curline());
5125 }
5126 return &pos;
5127 }
5128 return NULL;
5129}
5130
5131/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005132 * Convert list in "arg" into a position and optional file number.
5133 * When "fnump" is NULL there is no file number, only 3 items.
5134 * Note that the column is passed on as-is, the caller may want to decrement
5135 * it to use 1 for the first column.
5136 * Return FAIL when conversion is not possible, doesn't check the position for
5137 * validity.
5138 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005140list2fpos(
5141 typval_T *arg,
5142 pos_T *posp,
5143 int *fnump,
5144 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005145{
5146 list_T *l = arg->vval.v_list;
5147 long i = 0;
5148 long n;
5149
Bram Moolenaar493c1782014-05-28 14:34:46 +02005150 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
5151 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00005152 if (arg->v_type != VAR_LIST
5153 || l == NULL
5154 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02005155 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005156 return FAIL;
5157
5158 if (fnump != NULL)
5159 {
5160 n = list_find_nr(l, i++, NULL); /* fnum */
5161 if (n < 0)
5162 return FAIL;
5163 if (n == 0)
5164 n = curbuf->b_fnum; /* current buffer */
5165 *fnump = n;
5166 }
5167
5168 n = list_find_nr(l, i++, NULL); /* lnum */
5169 if (n < 0)
5170 return FAIL;
5171 posp->lnum = n;
5172
5173 n = list_find_nr(l, i++, NULL); /* col */
5174 if (n < 0)
5175 return FAIL;
5176 posp->col = n;
5177
Bram Moolenaar493c1782014-05-28 14:34:46 +02005178 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005179 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00005180 posp->coladd = 0;
5181 else
5182 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005183
Bram Moolenaar493c1782014-05-28 14:34:46 +02005184 if (curswantp != NULL)
5185 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
5186
Bram Moolenaar0e34f622006-03-03 23:00:03 +00005187 return OK;
5188}
5189
5190/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 * Get the length of an environment variable name.
5192 * Advance "arg" to the first character after the name.
5193 * Return 0 for error.
5194 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005195 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005196get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197{
5198 char_u *p;
5199 int len;
5200
5201 for (p = *arg; vim_isIDc(*p); ++p)
5202 ;
5203 if (p == *arg) /* no name found */
5204 return 0;
5205
5206 len = (int)(p - *arg);
5207 *arg = p;
5208 return len;
5209}
5210
5211/*
5212 * Get the length of the name of a function or internal variable.
5213 * "arg" is advanced to the first non-white character after the name.
5214 * Return 0 if something is wrong.
5215 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005216 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005217get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218{
5219 char_u *p;
5220 int len;
5221
5222 /* Find the end of the name. */
5223 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005224 {
5225 if (*p == ':')
5226 {
5227 /* "s:" is start of "s:var", but "n:" is not and can be used in
5228 * slice "[n:]". Also "xx:" is not a namespace. */
5229 len = (int)(p - *arg);
5230 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
5231 || len > 1)
5232 break;
5233 }
5234 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 if (p == *arg) /* no name found */
5236 return 0;
5237
5238 len = (int)(p - *arg);
5239 *arg = skipwhite(p);
5240
5241 return len;
5242}
5243
5244/*
Bram Moolenaara7043832005-01-21 11:56:39 +00005245 * Get the length of the name of a variable or function.
5246 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005248 * Return -1 if curly braces expansion failed.
5249 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 * If the name contains 'magic' {}'s, expand them and return the
5251 * expanded name in an allocated string via 'alias' - caller must free.
5252 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02005253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005254get_name_len(
5255 char_u **arg,
5256 char_u **alias,
5257 int evaluate,
5258 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259{
5260 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 char_u *p;
5262 char_u *expr_start;
5263 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
5265 *alias = NULL; /* default to no alias */
5266
5267 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
5268 && (*arg)[2] == (int)KE_SNR)
5269 {
5270 /* hard coded <SNR>, already translated */
5271 *arg += 3;
5272 return get_id_len(arg) + 3;
5273 }
5274 len = eval_fname_script(*arg);
5275 if (len > 0)
5276 {
5277 /* literal "<SID>", "s:" or "<SNR>" */
5278 *arg += len;
5279 }
5280
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005282 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005284 p = find_name_end(*arg, &expr_start, &expr_end,
5285 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 if (expr_start != NULL)
5287 {
5288 char_u *temp_string;
5289
5290 if (!evaluate)
5291 {
5292 len += (int)(p - *arg);
5293 *arg = skipwhite(p);
5294 return len;
5295 }
5296
5297 /*
5298 * Include any <SID> etc in the expanded string:
5299 * Thus the -len here.
5300 */
5301 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
5302 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005303 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 *alias = temp_string;
5305 *arg = skipwhite(p);
5306 return (int)STRLEN(temp_string);
5307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308
5309 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01005310 // Only give an error when there is something, otherwise it will be
5311 // reported at a higher level.
5312 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005313 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314
5315 return len;
5316}
5317
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005318/*
5319 * Find the end of a variable or function name, taking care of magic braces.
5320 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
5321 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005322 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005323 * Return a pointer to just after the name. Equal to "arg" if there is no
5324 * valid name.
5325 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005326 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005327find_name_end(
5328 char_u *arg,
5329 char_u **expr_start,
5330 char_u **expr_end,
5331 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005333 int mb_nest = 0;
5334 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005336 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005338 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005340 *expr_start = NULL;
5341 *expr_end = NULL;
5342 }
5343
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005344 /* Quick check for valid starting character. */
5345 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
5346 return arg;
5347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005348 for (p = arg; *p != NUL
5349 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005350 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005351 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005352 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005353 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005354 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00005355 if (*p == '\'')
5356 {
5357 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005358 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005359 ;
5360 if (*p == NUL)
5361 break;
5362 }
5363 else if (*p == '"')
5364 {
5365 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005366 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00005367 if (*p == '\\' && p[1] != NUL)
5368 ++p;
5369 if (*p == NUL)
5370 break;
5371 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005372 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
5373 {
5374 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005375 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005376 len = (int)(p - arg);
5377 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01005378 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01005379 break;
5380 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005381
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 if (*p == '[')
5385 ++br_nest;
5386 else if (*p == ']')
5387 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00005389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005390 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005392 if (*p == '{')
5393 {
5394 mb_nest++;
5395 if (expr_start != NULL && *expr_start == NULL)
5396 *expr_start = p;
5397 }
5398 else if (*p == '}')
5399 {
5400 mb_nest--;
5401 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
5402 *expr_end = p;
5403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 }
5406
5407 return p;
5408}
5409
5410/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005411 * Expands out the 'magic' {}'s in a variable/function name.
5412 * Note that this can call itself recursively, to deal with
5413 * constructs like foo{bar}{baz}{bam}
5414 * The four pointer arguments point to "foo{expre}ss{ion}bar"
5415 * "in_start" ^
5416 * "expr_start" ^
5417 * "expr_end" ^
5418 * "in_end" ^
5419 *
5420 * Returns a new allocated string, which the caller must free.
5421 * Returns NULL for failure.
5422 */
5423 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005424make_expanded_name(
5425 char_u *in_start,
5426 char_u *expr_start,
5427 char_u *expr_end,
5428 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005429{
5430 char_u c1;
5431 char_u *retval = NULL;
5432 char_u *temp_result;
5433 char_u *nextcmd = NULL;
5434
5435 if (expr_end == NULL || in_end == NULL)
5436 return NULL;
5437 *expr_start = NUL;
5438 *expr_end = NUL;
5439 c1 = *in_end;
5440 *in_end = NUL;
5441
Bram Moolenaar362e1a32006-03-06 23:29:24 +00005442 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005443 if (temp_result != NULL && nextcmd == NULL)
5444 {
Bram Moolenaar964b3742019-05-24 18:54:09 +02005445 retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
5446 + (in_end - expr_end) + 1);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005447 if (retval != NULL)
5448 {
5449 STRCPY(retval, in_start);
5450 STRCAT(retval, temp_result);
5451 STRCAT(retval, expr_end + 1);
5452 }
5453 }
5454 vim_free(temp_result);
5455
5456 *in_end = c1; /* put char back for error messages */
5457 *expr_start = '{';
5458 *expr_end = '}';
5459
5460 if (retval != NULL)
5461 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005462 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005463 if (expr_start != NULL)
5464 {
5465 /* Further expansion! */
5466 temp_result = make_expanded_name(retval, expr_start,
5467 expr_end, temp_result);
5468 vim_free(retval);
5469 retval = temp_result;
5470 }
5471 }
5472
5473 return retval;
5474}
5475
5476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00005478 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005481eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005483 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
5484}
5485
5486/*
5487 * Return TRUE if character "c" can be used as the first character in a
5488 * variable or function name (excluding '{' and '}').
5489 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005491eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00005492{
5493 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494}
5495
5496/*
Bram Moolenaarac92e252019-08-03 21:58:38 +02005497 * Handle:
5498 * - expr[expr], expr[expr:expr] subscript
5499 * - ".name" lookup
5500 * - function call with Funcref variable: func(expr)
5501 * - method call: var->method()
5502 *
5503 * Can all be combined in any order: dict.func(expr)[idx]['func'](expr)->len()
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005504 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005506handle_subscript(
5507 char_u **arg,
5508 typval_T *rettv,
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005509 int evaluate, // do more than finding the end
5510 int verbose, // give error messages
5511 char_u *start_leader, // start of '!' and '-' prefixes
5512 char_u **end_leaderp) // end of '!' and '-' prefixes
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005513{
5514 int ret = OK;
5515 dict_T *selfdict = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005516
Bram Moolenaar61343f02019-07-20 21:11:13 +02005517 // "." is ".name" lookup when we found a dict or when evaluating and
5518 // scriptversion is at least 2, where string concatenation is "..".
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005519 while (ret == OK
Bram Moolenaarac92e252019-08-03 21:58:38 +02005520 && (((**arg == '['
5521 || (**arg == '.' && (rettv->v_type == VAR_DICT
Bram Moolenaar61343f02019-07-20 21:11:13 +02005522 || (!evaluate
5523 && (*arg)[1] != '.'
5524 && current_sctx.sc_version >= 2)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005525 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005526 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaarac92e252019-08-03 21:58:38 +02005527 && !VIM_ISWHITE(*(*arg - 1)))
5528 || (**arg == '-' && (*arg)[1] == '>')))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005529 {
5530 if (**arg == '(')
5531 {
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005532 ret = call_func_rettv(arg, rettv, evaluate, selfdict, NULL);
Bram Moolenaar3f242a82016-03-18 19:39:25 +01005533
Bram Moolenaar22a0c0c2019-08-09 23:25:08 +02005534 // Stop the expression evaluation when immediately aborting on
5535 // error, or when an interrupt occurred or an exception was thrown
5536 // but not caught.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005537 if (aborting())
5538 {
5539 if (ret == OK)
5540 clear_tv(rettv);
5541 ret = FAIL;
5542 }
5543 dict_unref(selfdict);
5544 selfdict = NULL;
5545 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005546 else if (**arg == '-')
5547 {
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02005548 // Expression "-1.0->method()" applies the leader "-" before
5549 // applying ->.
5550 if (evaluate && *end_leaderp > start_leader)
5551 ret = eval7_leader(rettv, start_leader, end_leaderp);
5552 if (ret == OK)
5553 {
5554 if ((*arg)[2] == '{')
5555 // expr->{lambda}()
5556 ret = eval_lambda(arg, rettv, evaluate, verbose);
5557 else
5558 // expr->name()
5559 ret = eval_method(arg, rettv, evaluate, verbose);
5560 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02005561 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005562 else /* **arg == '[' || **arg == '.' */
5563 {
5564 dict_unref(selfdict);
5565 if (rettv->v_type == VAR_DICT)
5566 {
5567 selfdict = rettv->vval.v_dict;
5568 if (selfdict != NULL)
5569 ++selfdict->dv_refcount;
5570 }
5571 else
5572 selfdict = NULL;
5573 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
5574 {
5575 clear_tv(rettv);
5576 ret = FAIL;
5577 }
5578 }
5579 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005580
Bram Moolenaar1d429612016-05-24 15:44:17 +02005581 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
5582 * Don't do this when "Func" is already a partial that was bound
5583 * explicitly (pt_auto is FALSE). */
5584 if (selfdict != NULL
5585 && (rettv->v_type == VAR_FUNC
5586 || (rettv->v_type == VAR_PARTIAL
5587 && (rettv->vval.v_partial->pt_auto
5588 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005589 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01005590
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005591 dict_unref(selfdict);
5592 return ret;
5593}
5594
5595/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005596 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005597 * value).
5598 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01005599 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005600alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601{
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005602 return ALLOC_CLEAR_ONE(typval_T);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005603}
5604
5605/*
5606 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 * The string "s" must have been allocated, it is consumed.
5608 * Return NULL for out of memory, the variable otherwise.
5609 */
Bram Moolenaare5cdf152019-08-29 22:09:46 +02005610 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005611alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612{
Bram Moolenaar33570922005-01-25 22:26:29 +00005613 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005615 rettv = alloc_tv();
5616 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005618 rettv->v_type = VAR_STRING;
5619 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
5621 else
5622 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005623 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624}
5625
5626/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00005629 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005630free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 if (varp != NULL)
5633 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005634 switch (varp->v_type)
5635 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005636 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005637 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005638 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005639 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005640 vim_free(varp->vval.v_string);
5641 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005642 case VAR_PARTIAL:
5643 partial_unref(varp->vval.v_partial);
5644 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005645 case VAR_BLOB:
5646 blob_unref(varp->vval.v_blob);
5647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 case VAR_LIST:
5649 list_unref(varp->vval.v_list);
5650 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005651 case VAR_DICT:
5652 dict_unref(varp->vval.v_dict);
5653 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005654 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005655#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005656 job_unref(varp->vval.v_job);
5657 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005658#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005659 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005660#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005661 channel_unref(varp->vval.v_channel);
5662 break;
5663#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005664 case VAR_NUMBER:
5665 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005666 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01005667 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00005668 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 vim_free(varp);
5671 }
5672}
5673
5674/*
5675 * Free the memory for a variable value and set the value to NULL or 0.
5676 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00005677 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005678clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679{
5680 if (varp != NULL)
5681 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005682 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005684 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005685 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02005686 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005687 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01005688 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005689 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005690 case VAR_PARTIAL:
5691 partial_unref(varp->vval.v_partial);
5692 varp->vval.v_partial = NULL;
5693 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005694 case VAR_BLOB:
5695 blob_unref(varp->vval.v_blob);
5696 varp->vval.v_blob = NULL;
5697 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005698 case VAR_LIST:
5699 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005700 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005701 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 case VAR_DICT:
5703 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005704 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005707 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005708 varp->vval.v_number = 0;
5709 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005710 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005711#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005712 varp->vval.v_float = 0.0;
5713 break;
5714#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01005715 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005716#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005717 job_unref(varp->vval.v_job);
5718 varp->vval.v_job = NULL;
5719#endif
5720 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005721 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005722#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005723 channel_unref(varp->vval.v_channel);
5724 varp->vval.v_channel = NULL;
5725#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005726 case VAR_UNKNOWN:
5727 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005729 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731}
5732
5733/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005734 * Set the value of a variable to NULL without freeing items.
5735 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005737init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005738{
5739 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00005740 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005741}
5742
5743/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 * Get the number value of a variable.
5745 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005746 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005747 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005748 * caller of incompatible types: it sets *denote to TRUE if "denote"
5749 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005751 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005752tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005754 int error = FALSE;
5755
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005756 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005757}
5758
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005759 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005760tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005761{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005762 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005764 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005766 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005767 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005768 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005769#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005770 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005771 break;
5772#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005773 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005774 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005775 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005776 break;
5777 case VAR_STRING:
5778 if (varp->vval.v_string != NULL)
5779 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar16e9b852019-05-19 19:59:35 +02005780 STR2NR_ALL, &n, NULL, 0, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005781 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005783 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005785 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005786 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005787 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01005788 case VAR_SPECIAL:
5789 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
5790 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005791 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005793 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005794 break;
5795#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005796 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005797#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005798 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005799 break;
5800#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005801 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005802 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005803 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005804 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005805 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005808 if (denote == NULL) /* useful for values that must be unsigned */
5809 n = -1;
5810 else
5811 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813}
5814
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005815#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02005816 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005817tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005818{
5819 switch (varp->v_type)
5820 {
5821 case VAR_NUMBER:
5822 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005823 case VAR_FLOAT:
5824 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005825 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005826 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005827 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005828 break;
5829 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005830 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005831 break;
5832 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005833 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005834 break;
5835 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005836 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005837 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005838 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005839 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005840 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005841 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005842# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005843 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01005844 break;
5845# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005846 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005847# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005848 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01005849 break;
5850# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005851 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005852 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005853 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005854 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005855 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01005856 break;
5857 }
5858 return 0;
5859}
5860#endif
5861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 * Get the string value of a variable.
5864 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005865 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5866 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 * If the String variable has never been set, return an empty string.
5868 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005869 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005870 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005872 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005873tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 static char_u mybuf[NUMBUFLEN];
5876
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005877 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878}
5879
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01005880 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005881tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005883 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005884
5885 return res != NULL ? res : (char_u *)"";
5886}
5887
Bram Moolenaar7d647822014-04-05 21:28:56 +02005888/*
5889 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
5890 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005891 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005892tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005893{
5894 static char_u mybuf[NUMBUFLEN];
5895
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005896 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005897}
5898
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005899 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005900tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005901{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005902 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005904 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02005905 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01005906 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005907 return buf;
5908 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005909 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005910 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911 break;
5912 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005913 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005914 break;
5915 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005916 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005918 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005919#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005920 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005921 break;
5922#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923 case VAR_STRING:
5924 if (varp->vval.v_string != NULL)
5925 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005926 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005927 case VAR_SPECIAL:
5928 STRCPY(buf, get_var_special_name(varp->vval.v_number));
5929 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005930 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005931 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005932 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005933 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005934#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005935 {
5936 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01005937 char *status;
5938
5939 if (job == NULL)
5940 return (char_u *)"no process";
5941 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01005942 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01005943 : "run";
5944# ifdef UNIX
5945 vim_snprintf((char *)buf, NUMBUFLEN,
5946 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01005947# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005948 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01005949 "process %ld %s",
5950 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005951 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005952# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01005953 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01005954 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
5955# endif
5956 return buf;
5957 }
5958#endif
5959 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01005960 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005961#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005962 {
5963 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02005964 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01005965
Bram Moolenaar5cefd402016-02-16 12:44:26 +01005966 if (channel == NULL)
5967 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
5968 else
5969 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01005970 "channel %d %s", channel->ch_id, status);
5971 return buf;
5972 }
5973#endif
5974 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01005975 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005976 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005979 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980}
5981
5982/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005983 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
5984 * string() on Dict, List, etc.
5985 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02005986 static char_u *
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01005987tv_stringify(typval_T *varp, char_u *buf)
5988{
5989 if (varp->v_type == VAR_LIST
5990 || varp->v_type == VAR_DICT
5991 || varp->v_type == VAR_FUNC
5992 || varp->v_type == VAR_PARTIAL
5993 || varp->v_type == VAR_FLOAT)
5994 {
5995 typval_T tmp;
5996
5997 f_string(varp, &tmp);
5998 tv_get_string_buf(&tmp, buf);
5999 clear_tv(varp);
6000 *varp = tmp;
6001 return tmp.vval.v_string;
6002 }
6003 return tv_get_string_buf(varp, buf);
6004}
6005
6006/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01006007 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
6008 * Also give an error message, using "name" or _("name") when use_gettext is
6009 * TRUE.
6010 */
6011 static int
6012tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
6013{
6014 int lock = 0;
6015
6016 switch (tv->v_type)
6017 {
6018 case VAR_BLOB:
6019 if (tv->vval.v_blob != NULL)
6020 lock = tv->vval.v_blob->bv_lock;
6021 break;
6022 case VAR_LIST:
6023 if (tv->vval.v_list != NULL)
6024 lock = tv->vval.v_list->lv_lock;
6025 break;
6026 case VAR_DICT:
6027 if (tv->vval.v_dict != NULL)
6028 lock = tv->vval.v_dict->dv_lock;
6029 break;
6030 default:
6031 break;
6032 }
6033 return var_check_lock(tv->v_lock, name, use_gettext)
6034 || (lock != 0 && var_check_lock(lock, name, use_gettext));
6035}
6036
6037/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006038 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006039 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01006040 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006041 * It is OK for "from" and "to" to point to the same item. This is used to
6042 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006045copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006048 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 switch (from->v_type)
6050 {
6051 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006052 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 to->vval.v_number = from->vval.v_number;
6054 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006055 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006056#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006057 to->vval.v_float = from->vval.v_float;
6058 break;
6059#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006060 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006061#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006062 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01006063 if (to->vval.v_job != NULL)
6064 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006065 break;
6066#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006067 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006068#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006069 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01006070 if (to->vval.v_channel != NULL)
6071 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01006072 break;
6073#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 case VAR_STRING:
6075 case VAR_FUNC:
6076 if (from->vval.v_string == NULL)
6077 to->vval.v_string = NULL;
6078 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006079 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006081 if (from->v_type == VAR_FUNC)
6082 func_ref(to->vval.v_string);
6083 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006085 case VAR_PARTIAL:
6086 if (from->vval.v_partial == NULL)
6087 to->vval.v_partial = NULL;
6088 else
6089 {
6090 to->vval.v_partial = from->vval.v_partial;
6091 ++to->vval.v_partial->pt_refcount;
6092 }
6093 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006094 case VAR_BLOB:
6095 if (from->vval.v_blob == NULL)
6096 to->vval.v_blob = NULL;
6097 else
6098 {
6099 to->vval.v_blob = from->vval.v_blob;
6100 ++to->vval.v_blob->bv_refcount;
6101 }
6102 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006103 case VAR_LIST:
6104 if (from->vval.v_list == NULL)
6105 to->vval.v_list = NULL;
6106 else
6107 {
6108 to->vval.v_list = from->vval.v_list;
6109 ++to->vval.v_list->lv_refcount;
6110 }
6111 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00006112 case VAR_DICT:
6113 if (from->vval.v_dict == NULL)
6114 to->vval.v_dict = NULL;
6115 else
6116 {
6117 to->vval.v_dict = from->vval.v_dict;
6118 ++to->vval.v_dict->dv_refcount;
6119 }
6120 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006121 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01006122 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006123 break;
6124 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006125}
6126
6127/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006128 * Make a copy of an item.
6129 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006130 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
6131 * reference to an already copied list/dict can be used.
6132 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006133 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006134 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135item_copy(
6136 typval_T *from,
6137 typval_T *to,
6138 int deep,
6139 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006140{
6141 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006142 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006143
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006145 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006146 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006147 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006148 }
6149 ++recurse;
6150
6151 switch (from->v_type)
6152 {
6153 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006154 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006155 case VAR_STRING:
6156 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006157 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01006158 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006159 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006160 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006161 copy_tv(from, to);
6162 break;
6163 case VAR_LIST:
6164 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006165 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006166 if (from->vval.v_list == NULL)
6167 to->vval.v_list = NULL;
6168 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
6169 {
6170 /* use the copy made earlier */
6171 to->vval.v_list = from->vval.v_list->lv_copylist;
6172 ++to->vval.v_list->lv_refcount;
6173 }
6174 else
6175 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
6176 if (to->vval.v_list == NULL)
6177 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006178 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006179 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01006180 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01006181 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006182 case VAR_DICT:
6183 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006184 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006185 if (from->vval.v_dict == NULL)
6186 to->vval.v_dict = NULL;
6187 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
6188 {
6189 /* use the copy made earlier */
6190 to->vval.v_dict = from->vval.v_dict->dv_copydict;
6191 ++to->vval.v_dict->dv_refcount;
6192 }
6193 else
6194 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
6195 if (to->vval.v_dict == NULL)
6196 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006197 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01006198 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01006199 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006200 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006201 }
6202 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006203 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006204}
6205
6206/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006207 * This function is used by f_input() and f_inputdialog() functions. The third
6208 * argument to f_input() specifies the type of completion to use at the
6209 * prompt. The third argument to f_inputdialog() specifies the value to return
6210 * when the user cancels the prompt.
6211 */
6212 void
6213get_user_input(
6214 typval_T *argvars,
6215 typval_T *rettv,
6216 int inputdialog,
6217 int secret)
6218{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006219 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006220 char_u *p = NULL;
6221 int c;
6222 char_u buf[NUMBUFLEN];
6223 int cmd_silent_save = cmd_silent;
6224 char_u *defstr = (char_u *)"";
6225 int xp_type = EXPAND_NOTHING;
6226 char_u *xp_arg = NULL;
6227
6228 rettv->v_type = VAR_STRING;
6229 rettv->vval.v_string = NULL;
6230
6231#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02006232 /* While starting up, there is no place to enter text. When running tests
6233 * with --not-a-term we assume feedkeys() will be used. */
6234 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006235 return;
6236#endif
6237
6238 cmd_silent = FALSE; /* Want to see the prompt. */
6239 if (prompt != NULL)
6240 {
6241 /* Only the part of the message after the last NL is considered as
6242 * prompt for the command line */
6243 p = vim_strrchr(prompt, '\n');
6244 if (p == NULL)
6245 p = prompt;
6246 else
6247 {
6248 ++p;
6249 c = *p;
6250 *p = NUL;
6251 msg_start();
6252 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006253 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006254 msg_didout = FALSE;
6255 msg_starthere();
6256 *p = c;
6257 }
6258 cmdline_row = msg_row;
6259
6260 if (argvars[1].v_type != VAR_UNKNOWN)
6261 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006262 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006263 if (defstr != NULL)
6264 stuffReadbuffSpec(defstr);
6265
6266 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
6267 {
6268 char_u *xp_name;
6269 int xp_namelen;
6270 long argt;
6271
6272 /* input() with a third argument: completion */
6273 rettv->vval.v_string = NULL;
6274
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006275 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006276 if (xp_name == NULL)
6277 return;
6278
6279 xp_namelen = (int)STRLEN(xp_name);
6280
6281 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
6282 &xp_arg) == FAIL)
6283 return;
6284 }
6285 }
6286
6287 if (defstr != NULL)
6288 {
6289 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02006290
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006291 ex_normal_busy = 0;
6292 rettv->vval.v_string =
6293 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
6294 xp_type, xp_arg);
6295 ex_normal_busy = save_ex_normal_busy;
6296 }
6297 if (inputdialog && rettv->vval.v_string == NULL
6298 && argvars[1].v_type != VAR_UNKNOWN
6299 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006300 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006301 &argvars[2], buf));
6302
6303 vim_free(xp_arg);
6304
6305 /* since the user typed this, no need to wait for return */
6306 need_wait_return = FALSE;
6307 msg_didout = FALSE;
6308 }
6309 cmd_silent = cmd_silent_save;
6310}
6311
6312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 * ":echo expr1 ..." print each argument separated with a space, add a
6314 * newline at the end.
6315 * ":echon expr1 ..." print each argument plain.
6316 */
6317 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006318ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
6320 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006323 char_u *p;
6324 int needclr = TRUE;
6325 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006326 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01006327 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006328 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006329
6330 if (eap->skip)
6331 ++emsg_skip;
6332 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
6333 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006334 /* If eval1() causes an error message the text from the command may
6335 * still need to be cleared. E.g., "echo 22,44". */
6336 need_clr_eos = needclr;
6337
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006339 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340 {
6341 /*
6342 * Report the invalid expression unless the expression evaluation
6343 * has been cancelled due to an aborting error, an interrupt, or an
6344 * exception.
6345 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01006346 if (!aborting() && did_emsg == did_emsg_before
6347 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006348 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006349 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006350 break;
6351 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006352 need_clr_eos = FALSE;
6353
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 if (!eap->skip)
6355 {
6356 if (atstart)
6357 {
6358 atstart = FALSE;
6359 /* Call msg_start() after eval1(), evaluating the expression
6360 * may cause a message to appear. */
6361 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01006362 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02006363 /* Mark the saved text as finishing the line, so that what
6364 * follows is displayed on a new line when scrolling back
6365 * at the more prompt. */
6366 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01006368 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 }
6370 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01006371 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006372 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006373 if (p != NULL)
6374 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006376 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006378 if (*p != TAB && needclr)
6379 {
6380 /* remove any text still there from the command */
6381 msg_clr_eos();
6382 needclr = FALSE;
6383 }
6384 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006385 }
6386 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006387 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006388 if (has_mbyte)
6389 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006390 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006391
6392 (void)msg_outtrans_len_attr(p, i, echo_attr);
6393 p += i - 1;
6394 }
6395 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006396 (void)msg_outtrans_len_attr(p, 1, echo_attr);
6397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006401 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 arg = skipwhite(arg);
6403 }
6404 eap->nextcmd = check_nextcmd(arg);
6405
6406 if (eap->skip)
6407 --emsg_skip;
6408 else
6409 {
6410 /* remove text that may still be there from the command */
6411 if (needclr)
6412 msg_clr_eos();
6413 if (eap->cmdidx == CMD_echo)
6414 msg_end();
6415 }
6416}
6417
6418/*
6419 * ":echohl {name}".
6420 */
6421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006422ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02006424 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425}
6426
6427/*
6428 * ":execute expr1 ..." execute the result of an expression.
6429 * ":echomsg expr1 ..." Print a message
6430 * ":echoerr expr1 ..." Print an error
6431 * Each gets spaces around each argument and a newline at the end for
6432 * echo commands
6433 */
6434 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006435ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436{
6437 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00006438 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 int ret = OK;
6440 char_u *p;
6441 garray_T ga;
6442 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01006443 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
6445 ga_init2(&ga, 1, 80);
6446
6447 if (eap->skip)
6448 ++emsg_skip;
6449 while (*arg != NUL && *arg != '|' && *arg != '\n')
6450 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01006451 ret = eval1_emsg(&arg, &rettv, !eap->skip);
6452 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006454
6455 if (!eap->skip)
6456 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01006457 char_u buf[NUMBUFLEN];
6458
6459 if (eap->cmdidx == CMD_execute)
6460 p = tv_get_string_buf(&rettv, buf);
6461 else
6462 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 len = (int)STRLEN(p);
6464 if (ga_grow(&ga, len + 2) == FAIL)
6465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006466 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 ret = FAIL;
6468 break;
6469 }
6470 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006473 ga.ga_len += len;
6474 }
6475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006476 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477 arg = skipwhite(arg);
6478 }
6479
6480 if (ret != FAIL && ga.ga_data != NULL)
6481 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006482 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
6483 {
6484 /* Mark the already saved text as finishing the line, so that what
6485 * follows is displayed on a new line when scrolling back at the
6486 * more prompt. */
6487 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01006488 }
6489
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00006491 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006492 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00006493 out_flush();
6494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 else if (eap->cmdidx == CMD_echoerr)
6496 {
6497 /* We don't want to abort following commands, restore did_emsg. */
6498 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006499 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500 if (!force_abort)
6501 did_emsg = save_did_emsg;
6502 }
6503 else if (eap->cmdidx == CMD_execute)
6504 do_cmdline((char_u *)ga.ga_data,
6505 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
6506 }
6507
6508 ga_clear(&ga);
6509
6510 if (eap->skip)
6511 --emsg_skip;
6512
6513 eap->nextcmd = check_nextcmd(arg);
6514}
6515
6516/*
6517 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
6518 * "arg" points to the "&" or '+' when called, to "option" when returning.
6519 * Returns NULL when no option name found. Otherwise pointer to the char
6520 * after the option name.
6521 */
Bram Moolenaar0522ba02019-08-27 22:48:30 +02006522 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006523find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524{
6525 char_u *p = *arg;
6526
6527 ++p;
6528 if (*p == 'g' && p[1] == ':')
6529 {
6530 *opt_flags = OPT_GLOBAL;
6531 p += 2;
6532 }
6533 else if (*p == 'l' && p[1] == ':')
6534 {
6535 *opt_flags = OPT_LOCAL;
6536 p += 2;
6537 }
6538 else
6539 *opt_flags = 0;
6540
6541 if (!ASCII_ISALPHA(*p))
6542 return NULL;
6543 *arg = p;
6544
6545 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
6546 p += 4; /* termcap option */
6547 else
6548 while (ASCII_ISALPHA(*p))
6549 ++p;
6550 return p;
6551}
6552
6553/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006554 * Return the autoload script name for a function or variable name.
6555 * Returns NULL when out of memory.
Bram Moolenaar28fc2472019-07-05 22:14:16 +02006556 * Caller must make sure that "name" contains AUTOLOAD_CHAR.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006557 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02006558 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006559autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02006560{
Bram Moolenaar28fc2472019-07-05 22:14:16 +02006561 char_u *p, *q = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006562 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02006563
Bram Moolenaar28fc2472019-07-05 22:14:16 +02006564 // Get the script file name: replace '#' with '/', append ".vim".
Bram Moolenaar964b3742019-05-24 18:54:09 +02006565 scriptname = alloc(STRLEN(name) + 14);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006566 if (scriptname == NULL)
Bram Moolenaar1058c9d2019-08-20 21:58:00 +02006567 return NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006568 STRCPY(scriptname, "autoload/");
6569 STRCAT(scriptname, name);
Bram Moolenaar28fc2472019-07-05 22:14:16 +02006570 for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
6571 q = p, ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006572 *p = '/';
Bram Moolenaar28fc2472019-07-05 22:14:16 +02006573 STRCPY(q, ".vim");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006574 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006575}
6576
6577/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006578 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006579 * Return TRUE if a package was loaded.
6580 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006582script_autoload(
6583 char_u *name,
6584 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006585{
6586 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006587 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006588 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006589 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006590
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006591 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006592 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006593 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00006594 return FALSE;
6595
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006596 tofree = scriptname = autoload_name(name);
Bram Moolenaar1058c9d2019-08-20 21:58:00 +02006597 if (scriptname == NULL)
6598 return FALSE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006599
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006600 /* Find the name in the list of previously loaded package names. Skip
6601 * "autoload/", it's always the same. */
6602 for (i = 0; i < ga_loaded.ga_len; ++i)
6603 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
6604 break;
6605 if (!reload && i < ga_loaded.ga_len)
6606 ret = FALSE; /* was loaded already */
6607 else
6608 {
6609 /* Remember the name if it wasn't loaded already. */
6610 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
6611 {
6612 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
6613 tofree = NULL;
6614 }
6615
6616 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01006617 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00006618 ret = TRUE;
6619 }
6620
6621 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00006622 return ret;
6623}
6624
Bram Moolenaar661b1822005-07-28 22:36:45 +00006625/*
6626 * Display script name where an item was last set.
6627 * Should only be invoked when 'verbose' is non-zero.
6628 */
6629 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006630last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006631{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006632 char_u *p;
6633
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006634 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00006635 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006636 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006637 if (p != NULL)
6638 {
6639 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01006640 msg_puts(_("\n\tLast set from "));
6641 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006642 if (script_ctx.sc_lnum > 0)
6643 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01006644 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006645 msg_outnum((long)script_ctx.sc_lnum);
6646 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006647 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02006648 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00006649 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00006650 }
6651}
6652
Bram Moolenaar61be3762019-03-19 23:04:17 +01006653/*
Bram Moolenaar31988702018-02-13 12:57:42 +01006654 * Compare "typ1" and "typ2". Put the result in "typ1".
6655 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006656 int
6657typval_compare(
6658 typval_T *typ1, /* first operand */
6659 typval_T *typ2, /* second operand */
6660 exptype_T type, /* operator */
6661 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01006662 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006663{
6664 int i;
6665 varnumber_T n1, n2;
6666 char_u *s1, *s2;
6667 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
6668
Bram Moolenaar31988702018-02-13 12:57:42 +01006669 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006670 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006671 /* For "is" a different type always means FALSE, for "notis"
6672 * it means TRUE. */
6673 n1 = (type == TYPE_NEQUAL);
6674 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006675 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
6676 {
6677 if (type_is)
6678 {
6679 n1 = (typ1->v_type == typ2->v_type
6680 && typ1->vval.v_blob == typ2->vval.v_blob);
6681 if (type == TYPE_NEQUAL)
6682 n1 = !n1;
6683 }
6684 else if (typ1->v_type != typ2->v_type
6685 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
6686 {
6687 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006688 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006689 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006690 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01006691 clear_tv(typ1);
6692 return FAIL;
6693 }
6694 else
6695 {
6696 // Compare two Blobs for being equal or unequal.
6697 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
6698 if (type == TYPE_NEQUAL)
6699 n1 = !n1;
6700 }
6701 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006702 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
6703 {
6704 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006705 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006706 n1 = (typ1->v_type == typ2->v_type
6707 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006708 if (type == TYPE_NEQUAL)
6709 n1 = !n1;
6710 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006711 else if (typ1->v_type != typ2->v_type
6712 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006713 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006714 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006715 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006716 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006717 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006718 clear_tv(typ1);
6719 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006720 }
6721 else
6722 {
Bram Moolenaar31988702018-02-13 12:57:42 +01006723 /* Compare two Lists for being equal or unequal. */
6724 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
6725 ic, FALSE);
6726 if (type == TYPE_NEQUAL)
6727 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006728 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006729 }
Bram Moolenaar31988702018-02-13 12:57:42 +01006730
6731 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
6732 {
6733 if (type_is)
6734 {
6735 n1 = (typ1->v_type == typ2->v_type
6736 && typ1->vval.v_dict == typ2->vval.v_dict);
6737 if (type == TYPE_NEQUAL)
6738 n1 = !n1;
6739 }
6740 else if (typ1->v_type != typ2->v_type
6741 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
6742 {
6743 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006744 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006745 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006746 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006747 clear_tv(typ1);
6748 return FAIL;
6749 }
6750 else
6751 {
6752 /* Compare two Dictionaries for being equal or unequal. */
6753 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
6754 ic, FALSE);
6755 if (type == TYPE_NEQUAL)
6756 n1 = !n1;
6757 }
6758 }
6759
6760 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
6761 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
6762 {
6763 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
6764 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006765 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01006766 clear_tv(typ1);
6767 return FAIL;
6768 }
6769 if ((typ1->v_type == VAR_PARTIAL
6770 && typ1->vval.v_partial == NULL)
6771 || (typ2->v_type == VAR_PARTIAL
6772 && typ2->vval.v_partial == NULL))
6773 /* when a partial is NULL assume not equal */
6774 n1 = FALSE;
6775 else if (type_is)
6776 {
6777 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
6778 /* strings are considered the same if their value is
6779 * the same */
6780 n1 = tv_equal(typ1, typ2, ic, FALSE);
6781 else if (typ1->v_type == VAR_PARTIAL
6782 && typ2->v_type == VAR_PARTIAL)
6783 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
6784 else
6785 n1 = FALSE;
6786 }
6787 else
6788 n1 = tv_equal(typ1, typ2, ic, FALSE);
6789 if (type == TYPE_NEQUAL)
6790 n1 = !n1;
6791 }
6792
6793#ifdef FEAT_FLOAT
6794 /*
6795 * If one of the two variables is a float, compare as a float.
6796 * When using "=~" or "!~", always compare as string.
6797 */
6798 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
6799 && type != TYPE_MATCH && type != TYPE_NOMATCH)
6800 {
6801 float_T f1, f2;
6802
Bram Moolenaar38f08e72019-02-20 22:04:32 +01006803 f1 = tv_get_float(typ1);
6804 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006805 n1 = FALSE;
6806 switch (type)
6807 {
6808 case TYPE_EQUAL: n1 = (f1 == f2); break;
6809 case TYPE_NEQUAL: n1 = (f1 != f2); break;
6810 case TYPE_GREATER: n1 = (f1 > f2); break;
6811 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
6812 case TYPE_SMALLER: n1 = (f1 < f2); break;
6813 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
6814 case TYPE_UNKNOWN:
6815 case TYPE_MATCH:
6816 case TYPE_NOMATCH: break; /* avoid gcc warning */
6817 }
6818 }
6819#endif
6820
6821 /*
6822 * If one of the two variables is a number, compare as a number.
6823 * When using "=~" or "!~", always compare as string.
6824 */
6825 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
6826 && type != TYPE_MATCH && type != TYPE_NOMATCH)
6827 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006828 n1 = tv_get_number(typ1);
6829 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006830 switch (type)
6831 {
6832 case TYPE_EQUAL: n1 = (n1 == n2); break;
6833 case TYPE_NEQUAL: n1 = (n1 != n2); break;
6834 case TYPE_GREATER: n1 = (n1 > n2); break;
6835 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
6836 case TYPE_SMALLER: n1 = (n1 < n2); break;
6837 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
6838 case TYPE_UNKNOWN:
6839 case TYPE_MATCH:
6840 case TYPE_NOMATCH: break; /* avoid gcc warning */
6841 }
6842 }
6843 else
6844 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006845 s1 = tv_get_string_buf(typ1, buf1);
6846 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01006847 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
6848 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
6849 else
6850 i = 0;
6851 n1 = FALSE;
6852 switch (type)
6853 {
6854 case TYPE_EQUAL: n1 = (i == 0); break;
6855 case TYPE_NEQUAL: n1 = (i != 0); break;
6856 case TYPE_GREATER: n1 = (i > 0); break;
6857 case TYPE_GEQUAL: n1 = (i >= 0); break;
6858 case TYPE_SMALLER: n1 = (i < 0); break;
6859 case TYPE_SEQUAL: n1 = (i <= 0); break;
6860
6861 case TYPE_MATCH:
6862 case TYPE_NOMATCH:
6863 n1 = pattern_match(s2, s1, ic);
6864 if (type == TYPE_NOMATCH)
6865 n1 = !n1;
6866 break;
6867
6868 case TYPE_UNKNOWN: break; /* avoid gcc warning */
6869 }
6870 }
6871 clear_tv(typ1);
6872 typ1->v_type = VAR_NUMBER;
6873 typ1->vval.v_number = n1;
6874
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006875 return OK;
6876}
6877
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006878 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02006879typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006880{
6881 char_u *tofree;
6882 char_u numbuf[NUMBUFLEN];
6883 char_u *ret = NULL;
6884
6885 if (arg == NULL)
6886 return vim_strsave((char_u *)"(does not exist)");
6887 ret = tv2string(arg, &tofree, numbuf, 0);
6888 /* Make a copy if we have a value but it's not in allocated memory. */
6889 if (ret != NULL && tofree == NULL)
6890 ret = vim_strsave(ret);
6891 return ret;
6892}
6893
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894#endif /* FEAT_EVAL */
6895
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006896#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
Bram Moolenaar4f974752019-02-17 17:44:42 +01006898#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899/*
6900 * Functions for ":8" filename modifier: get 8.3 version of a filename.
6901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
6903/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006904 * Get the short path (8.3) for the filename in "fnamep".
6905 * Only works for a valid file name.
6906 * When the path gets longer "fnamep" is changed and the allocated buffer
6907 * is put in "bufp".
6908 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
6909 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 */
6911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006912get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006914 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915 char_u *newbuf;
6916
6917 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006918 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 if (l > len - 1)
6920 {
6921 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006922 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 newbuf = vim_strnsave(*fnamep, l);
6924 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926
6927 vim_free(*bufp);
6928 *fnamep = *bufp = newbuf;
6929
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006930 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +01006931 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 }
6933
6934 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006935 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936}
6937
6938/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006939 * Get the short path (8.3) for the filename in "fname". The converted
6940 * path is returned in "bufp".
6941 *
6942 * Some of the directories specified in "fname" may not exist. This function
6943 * will shorten the existing directories at the beginning of the path and then
6944 * append the remaining non-existing path.
6945 *
6946 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +02006947 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006948 * bufp - Pointer to an allocated buffer for the filename.
6949 * fnamelen - Length of the filename pointed to by fname
6950 *
6951 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +00006952 */
6953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006954shortpath_for_invalid_fname(
6955 char_u **fname,
6956 char_u **bufp,
6957 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006959 char_u *short_fname, *save_fname, *pbuf_unused;
6960 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006962 int old_len, len;
6963 int new_len, sfx_len;
6964 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
6966 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006967 old_len = *fnamelen;
6968 save_fname = vim_strnsave(*fname, old_len);
6969 pbuf_unused = NULL;
6970 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006972 endp = save_fname + old_len - 1; /* Find the end of the copy */
6973 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006975 /*
6976 * Try shortening the supplied path till it succeeds by removing one
6977 * directory at a time from the tail of the path.
6978 */
6979 len = 0;
6980 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006981 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006982 /* go back one path-separator */
6983 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
6984 --endp;
6985 if (endp <= save_fname)
6986 break; /* processed the complete path */
6987
6988 /*
6989 * Replace the path separator with a NUL and try to shorten the
6990 * resulting path.
6991 */
6992 ch = *endp;
6993 *endp = 0;
6994 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +00006995 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00006996 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
6997 {
6998 retval = FAIL;
6999 goto theend;
7000 }
7001 *endp = ch; /* preserve the string */
7002
7003 if (len > 0)
7004 break; /* successfully shortened the path */
7005
7006 /* failed to shorten the path. Skip the path separator */
7007 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 }
7009
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007010 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007011 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007012 /*
7013 * Succeeded in shortening the path. Now concatenate the shortened
7014 * path with the remaining path at the tail.
7015 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007017 /* Compute the length of the new path. */
7018 sfx_len = (int)(save_endp - endp) + 1;
7019 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007020
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007021 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007022 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007023 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007025 /* There is not enough space in the currently allocated string,
7026 * copy it to a buffer big enough. */
7027 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007029 {
7030 retval = FAIL;
7031 goto theend;
7032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 }
7034 else
7035 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007036 /* Transfer short_fname to the main buffer (it's big enough),
7037 * unless get_short_pathname() did its work in-place. */
7038 *fname = *bufp = save_fname;
7039 if (short_fname != save_fname)
7040 vim_strncpy(save_fname, short_fname, len);
7041 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007043
7044 /* concat the not-shortened part of the path */
7045 vim_strncpy(*fname + len, endp, sfx_len);
7046 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007048
7049theend:
7050 vim_free(pbuf_unused);
7051 vim_free(save_fname);
7052
7053 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007054}
7055
7056/*
7057 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007058 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 */
7060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007061shortpath_for_partial(
7062 char_u **fnamep,
7063 char_u **bufp,
7064 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 int sepcount, len, tflen;
7067 char_u *p;
7068 char_u *pbuf, *tfname;
7069 int hasTilde;
7070
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007071 /* Count up the path separators from the RHS.. so we know which part
7072 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007074 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 if (vim_ispathsep(*p))
7076 ++sepcount;
7077
7078 /* Need full path first (use expand_env() to remove a "~/") */
7079 hasTilde = (**fnamep == '~');
7080 if (hasTilde)
7081 pbuf = tfname = expand_env_save(*fnamep);
7082 else
7083 pbuf = tfname = FullName_save(*fnamep, FALSE);
7084
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007085 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007087 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
7088 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
7090 if (len == 0)
7091 {
7092 /* Don't have a valid filename, so shorten the rest of the
7093 * path if we can. This CAN give us invalid 8.3 filenames, but
7094 * there's not a lot of point in guessing what it might be.
7095 */
7096 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007097 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
7098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099 }
7100
7101 /* Count the paths backward to find the beginning of the desired string. */
7102 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007103 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007104 if (has_mbyte)
7105 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 if (vim_ispathsep(*p))
7107 {
7108 if (sepcount == 0 || (hasTilde && sepcount == 1))
7109 break;
7110 else
7111 sepcount --;
7112 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007113 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007114 if (hasTilde)
7115 {
7116 --p;
7117 if (p >= tfname)
7118 *p = '~';
7119 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007120 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007121 }
7122 else
7123 ++p;
7124
7125 /* Copy in the string - p indexes into tfname - allocated at pbuf */
7126 vim_free(*bufp);
7127 *fnamelen = (int)STRLEN(p);
7128 *bufp = pbuf;
7129 *fnamep = p;
7130
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007131 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007132}
Bram Moolenaar4f974752019-02-17 17:44:42 +01007133#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007134
7135/*
7136 * Adjust a filename, according to a string of modifiers.
7137 * *fnamep must be NUL terminated when called. When returning, the length is
7138 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007139 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007140 * When there is an error, *fnamep is set to NULL.
7141 */
7142 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007143modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +02007144 char_u *src, // string with modifiers
7145 int tilde_file, // "~" is a file name, not $HOME
7146 int *usedlen, // characters after src that are used
7147 char_u **fnamep, // file name so far
7148 char_u **bufp, // buffer for allocated file name or NULL
7149 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +00007150{
7151 int valid = 0;
7152 char_u *tail;
7153 char_u *s, *p, *pbuf;
7154 char_u dirname[MAXPATHL];
7155 int c;
7156 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +01007157#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +02007158 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159 int has_shortname = 0;
7160#endif
7161
7162repeat:
7163 /* ":p" - full path/file_name */
7164 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
7165 {
7166 has_fullname = 1;
7167
7168 valid |= VALID_PATH;
7169 *usedlen += 2;
7170
7171 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
7172 if ((*fnamep)[0] == '~'
7173#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
7174 && ((*fnamep)[1] == '/'
7175# ifdef BACKSLASH_IN_FILENAME
7176 || (*fnamep)[1] == '\\'
7177# endif
7178 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +02007180 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 )
7182 {
7183 *fnamep = expand_env_save(*fnamep);
7184 vim_free(*bufp); /* free any allocated file name */
7185 *bufp = *fnamep;
7186 if (*fnamep == NULL)
7187 return -1;
7188 }
7189
7190 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007191 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 {
7193 if (vim_ispathsep(*p)
7194 && p[1] == '.'
7195 && (p[2] == NUL
7196 || vim_ispathsep(p[2])
7197 || (p[2] == '.'
7198 && (p[3] == NUL || vim_ispathsep(p[3])))))
7199 break;
7200 }
7201
7202 /* FullName_save() is slow, don't use it when not needed. */
7203 if (*p != NUL || !vim_isAbsName(*fnamep))
7204 {
7205 *fnamep = FullName_save(*fnamep, *p != NUL);
7206 vim_free(*bufp); /* free any allocated file name */
7207 *bufp = *fnamep;
7208 if (*fnamep == NULL)
7209 return -1;
7210 }
7211
Bram Moolenaar4f974752019-02-17 17:44:42 +01007212#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02007213# if _WIN32_WINNT >= 0x0500
7214 if (vim_strchr(*fnamep, '~') != NULL)
7215 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +01007216 // Expand 8.3 filename to full path. Needed to make sure the same
7217 // file does not have two different names.
7218 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
7219 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
7220 WCHAR buf[_MAX_PATH];
7221
7222 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02007223 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +01007224 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02007225 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +01007226 char_u *p = utf16_to_enc(buf, NULL);
7227
7228 if (p != NULL)
7229 {
7230 vim_free(*bufp); // free any allocated file name
7231 *bufp = *fnamep = p;
7232 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02007233 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +01007234 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +02007235 }
7236 }
7237# endif
7238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007239 /* Append a path separator to a directory. */
7240 if (mch_isdir(*fnamep))
7241 {
7242 /* Make room for one or two extra characters. */
7243 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
7244 vim_free(*bufp); /* free any allocated file name */
7245 *bufp = *fnamep;
7246 if (*fnamep == NULL)
7247 return -1;
7248 add_pathsep(*fnamep);
7249 }
7250 }
7251
7252 /* ":." - path relative to the current directory */
7253 /* ":~" - path relative to the home directory */
7254 /* ":8" - shortname path - postponed till after */
7255 while (src[*usedlen] == ':'
7256 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
7257 {
7258 *usedlen += 2;
7259 if (c == '8')
7260 {
Bram Moolenaar4f974752019-02-17 17:44:42 +01007261#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007262 has_shortname = 1; /* Postpone this. */
7263#endif
7264 continue;
7265 }
7266 pbuf = NULL;
7267 /* Need full path first (use expand_env() to remove a "~/") */
7268 if (!has_fullname)
7269 {
7270 if (c == '.' && **fnamep == '~')
7271 p = pbuf = expand_env_save(*fnamep);
7272 else
7273 p = pbuf = FullName_save(*fnamep, FALSE);
7274 }
7275 else
7276 p = *fnamep;
7277
7278 has_fullname = 0;
7279
7280 if (p != NULL)
7281 {
7282 if (c == '.')
7283 {
7284 mch_dirname(dirname, MAXPATHL);
7285 s = shorten_fname(p, dirname);
7286 if (s != NULL)
7287 {
7288 *fnamep = s;
7289 if (pbuf != NULL)
7290 {
7291 vim_free(*bufp); /* free any allocated file name */
7292 *bufp = pbuf;
7293 pbuf = NULL;
7294 }
7295 }
7296 }
7297 else
7298 {
7299 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
7300 /* Only replace it when it starts with '~' */
7301 if (*dirname == '~')
7302 {
7303 s = vim_strsave(dirname);
7304 if (s != NULL)
7305 {
7306 *fnamep = s;
7307 vim_free(*bufp);
7308 *bufp = s;
7309 }
7310 }
7311 }
7312 vim_free(pbuf);
7313 }
7314 }
7315
7316 tail = gettail(*fnamep);
7317 *fnamelen = (int)STRLEN(*fnamep);
7318
7319 /* ":h" - head, remove "/file_name", can be repeated */
7320 /* Don't remove the first "/" or "c:\" */
7321 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
7322 {
7323 valid |= VALID_HEAD;
7324 *usedlen += 2;
7325 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007326 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007327 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 *fnamelen = (int)(tail - *fnamep);
7329#ifdef VMS
7330 if (*fnamelen > 0)
7331 *fnamelen += 1; /* the path separator is part of the path */
7332#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00007333 if (*fnamelen == 0)
7334 {
7335 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
7336 p = vim_strsave((char_u *)".");
7337 if (p == NULL)
7338 return -1;
7339 vim_free(*bufp);
7340 *bufp = *fnamep = tail = p;
7341 *fnamelen = 1;
7342 }
7343 else
7344 {
7345 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007346 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +00007347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348 }
7349
7350 /* ":8" - shortname */
7351 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
7352 {
7353 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +01007354#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355 has_shortname = 1;
7356#endif
7357 }
7358
Bram Moolenaar4f974752019-02-17 17:44:42 +01007359#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +02007360 /*
7361 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 */
7363 if (has_shortname)
7364 {
Bram Moolenaardc935552011-08-17 15:23:23 +02007365 /* Copy the string if it is shortened by :h and when it wasn't copied
7366 * yet, because we are going to change it in place. Avoids changing
7367 * the buffer name for "%:8". */
7368 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369 {
7370 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +02007371 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372 return -1;
7373 vim_free(*bufp);
7374 *bufp = *fnamep = p;
7375 }
7376
7377 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +02007378 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007379 if (!has_fullname && !vim_isAbsName(*fnamep))
7380 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007381 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 return -1;
7383 }
7384 else
7385 {
Bram Moolenaardc935552011-08-17 15:23:23 +02007386 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007387
Bram Moolenaardc935552011-08-17 15:23:23 +02007388 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007389 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007390 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007391 return -1;
7392
7393 if (l == 0)
7394 {
Bram Moolenaardc935552011-08-17 15:23:23 +02007395 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007396 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +00007397 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 return -1;
7399 }
7400 *fnamelen = l;
7401 }
7402 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01007403#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404
7405 /* ":t" - tail, just the basename */
7406 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
7407 {
7408 *usedlen += 2;
7409 *fnamelen -= (int)(tail - *fnamep);
7410 *fnamep = tail;
7411 }
7412
7413 /* ":e" - extension, can be repeated */
7414 /* ":r" - root, without extension, can be repeated */
7415 while (src[*usedlen] == ':'
7416 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
7417 {
7418 /* find a '.' in the tail:
7419 * - for second :e: before the current fname
7420 * - otherwise: The last '.'
7421 */
7422 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
7423 s = *fnamep - 2;
7424 else
7425 s = *fnamep + *fnamelen - 1;
7426 for ( ; s > tail; --s)
7427 if (s[0] == '.')
7428 break;
7429 if (src[*usedlen + 1] == 'e') /* :e */
7430 {
7431 if (s > tail)
7432 {
7433 *fnamelen += (int)(*fnamep - (s + 1));
7434 *fnamep = s + 1;
7435#ifdef VMS
7436 /* cut version from the extension */
7437 s = *fnamep + *fnamelen - 1;
7438 for ( ; s > *fnamep; --s)
7439 if (s[0] == ';')
7440 break;
7441 if (s > *fnamep)
7442 *fnamelen = s - *fnamep;
7443#endif
7444 }
7445 else if (*fnamep <= tail)
7446 *fnamelen = 0;
7447 }
7448 else /* :r */
7449 {
7450 if (s > tail) /* remove one extension */
7451 *fnamelen = (int)(s - *fnamep);
7452 }
7453 *usedlen += 2;
7454 }
7455
7456 /* ":s?pat?foo?" - substitute */
7457 /* ":gs?pat?foo?" - global substitute */
7458 if (src[*usedlen] == ':'
7459 && (src[*usedlen + 1] == 's'
7460 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
7461 {
7462 char_u *str;
7463 char_u *pat;
7464 char_u *sub;
7465 int sep;
7466 char_u *flags;
7467 int didit = FALSE;
7468
7469 flags = (char_u *)"";
7470 s = src + *usedlen + 2;
7471 if (src[*usedlen + 1] == 'g')
7472 {
7473 flags = (char_u *)"g";
7474 ++s;
7475 }
7476
7477 sep = *s++;
7478 if (sep)
7479 {
7480 /* find end of pattern */
7481 p = vim_strchr(s, sep);
7482 if (p != NULL)
7483 {
7484 pat = vim_strnsave(s, (int)(p - s));
7485 if (pat != NULL)
7486 {
7487 s = p + 1;
7488 /* find end of substitution */
7489 p = vim_strchr(s, sep);
7490 if (p != NULL)
7491 {
7492 sub = vim_strnsave(s, (int)(p - s));
7493 str = vim_strnsave(*fnamep, *fnamelen);
7494 if (sub != NULL && str != NULL)
7495 {
7496 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007497 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007498 if (s != NULL)
7499 {
7500 *fnamep = s;
7501 *fnamelen = (int)STRLEN(s);
7502 vim_free(*bufp);
7503 *bufp = s;
7504 didit = TRUE;
7505 }
7506 }
7507 vim_free(sub);
7508 vim_free(str);
7509 }
7510 vim_free(pat);
7511 }
7512 }
7513 /* after using ":s", repeat all the modifiers */
7514 if (didit)
7515 goto repeat;
7516 }
7517 }
7518
Bram Moolenaar26df0922014-02-23 23:39:13 +01007519 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
7520 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +01007521 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +01007522 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01007523 if (c != NUL)
7524 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +01007525 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +01007526 if (c != NUL)
7527 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +01007528 if (p == NULL)
7529 return -1;
7530 vim_free(*bufp);
7531 *bufp = *fnamep = p;
7532 *fnamelen = (int)STRLEN(p);
7533 *usedlen += 2;
7534 }
7535
Bram Moolenaar071d4272004-06-13 20:20:40 +00007536 return valid;
7537}
7538
7539/*
7540 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007541 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 * "flags" can be "g" to do a global substitute.
7543 * Returns an allocated string, NULL for error.
7544 */
7545 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007546do_string_sub(
7547 char_u *str,
7548 char_u *pat,
7549 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007550 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007551 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 int sublen;
7554 regmatch_T regmatch;
7555 int i;
7556 int do_all;
7557 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007558 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 garray_T ga;
7560 char_u *ret;
7561 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007562 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563
7564 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
7565 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007566 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567
7568 ga_init2(&ga, 1, 200);
7569
7570 do_all = (flags[0] == 'g');
7571
7572 regmatch.rm_ic = p_ic;
7573 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
7574 if (regmatch.regprog != NULL)
7575 {
7576 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +01007577 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007578 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
7579 {
Bram Moolenaar8af26912014-01-23 20:09:34 +01007580 /* Skip empty match except for first match. */
7581 if (regmatch.startp[0] == regmatch.endp[0])
7582 {
7583 if (zero_width == regmatch.startp[0])
7584 {
7585 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +02007586 i = MB_PTR2LEN(tail);
7587 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
7588 (size_t)i);
7589 ga.ga_len += i;
7590 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +01007591 continue;
7592 }
7593 zero_width = regmatch.startp[0];
7594 }
7595
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596 /*
7597 * Get some space for a temporary buffer to do the substitution
7598 * into. It will contain:
7599 * - The text up to where the match is.
7600 * - The substituted text.
7601 * - The text after the match.
7602 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007603 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +01007604 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +00007605 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
7606 {
7607 ga_clear(&ga);
7608 break;
7609 }
7610
7611 /* copy the text up to where the match is */
7612 i = (int)(regmatch.startp[0] - tail);
7613 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
7614 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007615 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 + ga.ga_len + i, TRUE, TRUE, FALSE);
7617 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +02007618 tail = regmatch.endp[0];
7619 if (*tail == NUL)
7620 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 if (!do_all)
7622 break;
7623 }
7624
7625 if (ga.ga_data != NULL)
7626 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
7627
Bram Moolenaar473de612013-06-08 18:19:48 +02007628 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 }
7630
7631 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
7632 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007633 if (p_cpo == empty_option)
7634 p_cpo = save_cpo;
7635 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007636 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +00007637 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638
7639 return ret;
7640}
7641
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007642 static int
7643filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
7644{
7645 typval_T rettv;
7646 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007647 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007648
Bram Moolenaare5cdf152019-08-29 22:09:46 +02007649 copy_tv(tv, get_vim_var_tv(VV_VAL));
7650 argv[0] = *get_vim_var_tv(VV_KEY);
7651 argv[1] = *get_vim_var_tv(VV_VAL);
Bram Moolenaar48570482017-10-30 21:48:41 +01007652 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
7653 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007654 if (map)
7655 {
7656 /* map(): replace the list item value */
7657 clear_tv(tv);
7658 rettv.v_lock = 0;
7659 *tv = rettv;
7660 }
7661 else
7662 {
7663 int error = FALSE;
7664
7665 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007666 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007667 clear_tv(&rettv);
7668 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007669 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007670 if (error)
7671 goto theend;
7672 }
7673 retval = OK;
7674theend:
Bram Moolenaare5cdf152019-08-29 22:09:46 +02007675 clear_tv(get_vim_var_tv(VV_VAL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007676 return retval;
7677}
7678
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007679/*
7680 * Implementation of map() and filter().
7681 */
7682 void
7683filter_map(typval_T *argvars, typval_T *rettv, int map)
7684{
7685 typval_T *expr;
7686 listitem_T *li, *nli;
7687 list_T *l = NULL;
7688 dictitem_T *di;
7689 hashtab_T *ht;
7690 hashitem_T *hi;
7691 dict_T *d = NULL;
7692 typval_T save_val;
7693 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007694 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007695 int rem;
7696 int todo;
7697 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
7698 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
7699 : N_("filter() argument"));
7700 int save_did_emsg;
7701 int idx = 0;
7702
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007703 if (argvars[0].v_type == VAR_BLOB)
7704 {
7705 if ((b = argvars[0].vval.v_blob) == NULL)
7706 return;
7707 }
7708 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007709 {
7710 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007711 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007712 return;
7713 }
7714 else if (argvars[0].v_type == VAR_DICT)
7715 {
7716 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007717 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007718 return;
7719 }
7720 else
7721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007722 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007723 return;
7724 }
7725
7726 expr = &argvars[1];
7727 /* On type errors, the preceding call has already displayed an error
7728 * message. Avoid a misleading error message for an empty string that
7729 * was not passed as argument. */
7730 if (expr->v_type != VAR_UNKNOWN)
7731 {
7732 prepare_vimvar(VV_VAL, &save_val);
7733
7734 /* We reset "did_emsg" to be able to detect whether an error
7735 * occurred during evaluation of the expression. */
7736 save_did_emsg = did_emsg;
7737 did_emsg = FALSE;
7738
7739 prepare_vimvar(VV_KEY, &save_key);
7740 if (argvars[0].v_type == VAR_DICT)
7741 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007742 ht = &d->dv_hashtab;
7743 hash_lock(ht);
7744 todo = (int)ht->ht_used;
7745 for (hi = ht->ht_array; todo > 0; ++hi)
7746 {
7747 if (!HASHITEM_EMPTY(hi))
7748 {
7749 int r;
7750
7751 --todo;
7752 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007753 if (map && (var_check_lock(di->di_tv.v_lock,
7754 arg_errmsg, TRUE)
7755 || var_check_ro(di->di_flags,
7756 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007757 break;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02007758 set_vim_var_string(VV_KEY, di->di_key, -1);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007759 r = filter_map_one(&di->di_tv, expr, map, &rem);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02007760 clear_tv(get_vim_var_tv(VV_KEY));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007761 if (r == FAIL || did_emsg)
7762 break;
7763 if (!map && rem)
7764 {
7765 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
7766 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
7767 break;
7768 dictitem_remove(d, di);
7769 }
7770 }
7771 }
7772 hash_unlock(ht);
7773 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007774 else if (argvars[0].v_type == VAR_BLOB)
7775 {
7776 int i;
7777 typval_T tv;
7778
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02007779 set_vim_var_type(VV_KEY, VAR_NUMBER);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007780 for (i = 0; i < b->bv_ga.ga_len; i++)
7781 {
7782 tv.v_type = VAR_NUMBER;
7783 tv.vval.v_number = blob_get(b, i);
Bram Moolenaare5cdf152019-08-29 22:09:46 +02007784 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007785 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
7786 break;
7787 if (tv.v_type != VAR_NUMBER)
7788 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007789 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007790 return;
7791 }
7792 tv.v_type = VAR_NUMBER;
7793 blob_set(b, i, tv.vval.v_number);
7794 if (!map && rem)
7795 {
7796 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
7797
7798 mch_memmove(p + idx, p + i + 1,
7799 (size_t)b->bv_ga.ga_len - i - 1);
7800 --b->bv_ga.ga_len;
7801 --i;
7802 }
7803 }
7804 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007805 else
7806 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01007807 // argvars[0].v_type == VAR_LIST
Bram Moolenaar34ed68d2019-08-29 22:48:24 +02007808 set_vim_var_type(VV_KEY, VAR_NUMBER);
7809
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007810 for (li = l->lv_first; li != NULL; li = nli)
7811 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007812 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007813 break;
7814 nli = li->li_next;
Bram Moolenaare5cdf152019-08-29 22:09:46 +02007815 set_vim_var_nr(VV_KEY, idx);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007816 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
7817 || did_emsg)
7818 break;
7819 if (!map && rem)
7820 listitem_remove(l, li);
7821 ++idx;
7822 }
7823 }
7824
7825 restore_vimvar(VV_KEY, &save_key);
7826 restore_vimvar(VV_VAL, &save_val);
7827
7828 did_emsg |= save_did_emsg;
7829 }
7830
7831 copy_tv(&argvars[0], rettv);
7832}
7833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */