blob: ec2dc9aa4aa7423a13a4ee1368670ff4e05feefe [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 Moolenaar33570922005-01-25 22:26:29 +000023#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000026static char *e_undefvar = N_("E121: Undefined variable: %s");
27static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000028static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
29static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar92124a32005-06-17 22:03:40 +000030static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020031#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020032static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020033#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000034
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010035#define NAMESPACE_CHAR (char_u *)"abglstvw"
36
Bram Moolenaar230bb3f2013-04-24 14:07:45 +020037static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +000038#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
40/*
Bram Moolenaar532c7802005-01-27 14:44:31 +000041 * Old Vim variables such as "v:version" are also available without the "v:".
42 * Also in functions. We need a special hashtable for them.
43 */
Bram Moolenaar4debb442005-06-01 21:57:40 +000044static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +000045
46/*
Bram Moolenaard9fba312005-06-26 22:34:35 +000047 * When recursively copying lists and dicts we need to remember which ones we
48 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000049 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +000050 */
51static int current_copyID = 0;
Bram Moolenaar8502c702014-06-17 12:51:16 +020052
Bram Moolenaard9fba312005-06-26 22:34:35 +000053/*
Bram Moolenaar33570922005-01-25 22:26:29 +000054 * Array to hold the hashtab with variables local to each sourced script.
55 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +000056 */
Bram Moolenaar33570922005-01-25 22:26:29 +000057typedef struct
58{
59 dictitem_T sv_var;
60 dict_T sv_dict;
61} scriptvar_T;
62
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020063static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
64#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
65#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
67static int echo_attr = 0; /* attributes used for ":echo" */
68
Bram Moolenaarc9b4b052006-04-30 18:54:39 +000069/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000070static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
71
Bram Moolenaar071d4272004-06-13 20:20:40 +000072/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000073 * Info used by a ":for" loop.
74 */
Bram Moolenaar33570922005-01-25 22:26:29 +000075typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000076{
77 int fi_semicolon; /* TRUE if ending in '; var]' */
78 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 listwatch_T fi_lw; /* keep an eye on the item used. */
80 list_T *fi_list; /* list being used */
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010081 int fi_bi; /* index of blob */
82 blob_T *fi_blob; /* blob being used */
Bram Moolenaar33570922005-01-25 22:26:29 +000083} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000084
Bram Moolenaara7043832005-01-21 11:56:39 +000085
86/*
Bram Moolenaar33570922005-01-25 22:26:29 +000087 * Array to hold the value of v: variables.
88 * The value is in a dictitem, so that it can also be used in the v: scope.
89 * The reason to use this table anyway is for very quick access to the
90 * variables with the VV_ defines.
91 */
Bram Moolenaar33570922005-01-25 22:26:29 +000092
93/* values for vv_flags: */
94#define VV_COMPAT 1 /* compatible, also used without "v:" */
95#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000096#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +000097
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +010098#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}
Bram Moolenaar33570922005-01-25 22:26:29 +000099
100static struct vimvar
101{
102 char *vv_name; /* name of variable, without v: */
Bram Moolenaarbee6c0c2016-03-25 15:40:50 +0100103 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000104 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
105} vimvars[VV_LEN] =
106{
107 /*
108 * The order here must match the VV_ defines in vim.h!
109 * Initializing a union does not work, leave tv.vval empty to get zero's.
110 */
111 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
112 {VV_NAME("count1", VAR_NUMBER), VV_RO},
113 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
114 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
115 {VV_NAME("warningmsg", VAR_STRING), 0},
116 {VV_NAME("statusmsg", VAR_STRING), 0},
117 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
118 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
119 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
120 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
121 {VV_NAME("termresponse", VAR_STRING), VV_RO},
122 {VV_NAME("fname", VAR_STRING), VV_RO},
123 {VV_NAME("lang", VAR_STRING), VV_RO},
124 {VV_NAME("lc_time", VAR_STRING), VV_RO},
125 {VV_NAME("ctype", VAR_STRING), VV_RO},
126 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
127 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
128 {VV_NAME("fname_in", VAR_STRING), VV_RO},
129 {VV_NAME("fname_out", VAR_STRING), VV_RO},
130 {VV_NAME("fname_new", VAR_STRING), VV_RO},
131 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
132 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
133 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
134 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
135 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
136 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
137 {VV_NAME("progname", VAR_STRING), VV_RO},
138 {VV_NAME("servername", VAR_STRING), VV_RO},
139 {VV_NAME("dying", VAR_NUMBER), VV_RO},
140 {VV_NAME("exception", VAR_STRING), VV_RO},
141 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
142 {VV_NAME("register", VAR_STRING), VV_RO},
143 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
144 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000145 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
146 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000147 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000148 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
149 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000150 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
151 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
Bram Moolenaarc9721bd2016-06-04 17:41:03 +0200152 {VV_NAME("beval_winid", VAR_NUMBER), VV_RO},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000153 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
154 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
155 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000156 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000157 {VV_NAME("swapname", VAR_STRING), VV_RO},
158 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000159 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200160 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000161 {VV_NAME("mouse_win", VAR_NUMBER), 0},
Bram Moolenaar511972d2016-06-04 18:09:59 +0200162 {VV_NAME("mouse_winid", VAR_NUMBER), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000163 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
164 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000165 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000166 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100167 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000168 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200169 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200170 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200171 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200172 {VV_NAME("option_new", VAR_STRING), VV_RO},
173 {VV_NAME("option_old", VAR_STRING), VV_RO},
174 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100175 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100176 {VV_NAME("false", VAR_SPECIAL), VV_RO},
177 {VV_NAME("true", VAR_SPECIAL), VV_RO},
178 {VV_NAME("null", VAR_SPECIAL), VV_RO},
179 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100180 {VV_NAME("vim_did_enter", VAR_NUMBER), VV_RO},
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200181 {VV_NAME("testing", VAR_NUMBER), 0},
Bram Moolenaarf562e722016-07-19 17:25:25 +0200182 {VV_NAME("t_number", VAR_NUMBER), VV_RO},
183 {VV_NAME("t_string", VAR_NUMBER), VV_RO},
184 {VV_NAME("t_func", VAR_NUMBER), VV_RO},
185 {VV_NAME("t_list", VAR_NUMBER), VV_RO},
186 {VV_NAME("t_dict", VAR_NUMBER), VV_RO},
187 {VV_NAME("t_float", VAR_NUMBER), VV_RO},
188 {VV_NAME("t_bool", VAR_NUMBER), VV_RO},
189 {VV_NAME("t_none", VAR_NUMBER), VV_RO},
190 {VV_NAME("t_job", VAR_NUMBER), VV_RO},
191 {VV_NAME("t_channel", VAR_NUMBER), VV_RO},
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 {VV_NAME("t_blob", VAR_NUMBER), VV_RO},
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +0200193 {VV_NAME("termrfgresp", VAR_STRING), VV_RO},
194 {VV_NAME("termrbgresp", VAR_STRING), VV_RO},
Bram Moolenaarf3af54e2017-08-30 14:53:06 +0200195 {VV_NAME("termu7resp", VAR_STRING), VV_RO},
196 {VV_NAME("termstyleresp", VAR_STRING), VV_RO},
197 {VV_NAME("termblinkresp", VAR_STRING), VV_RO},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100198 {VV_NAME("event", VAR_DICT), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000199};
200
201/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000202#define vv_type vv_di.di_tv.v_type
203#define vv_nr vv_di.di_tv.vval.v_number
204#define vv_float vv_di.di_tv.vval.v_float
205#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000206#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200207#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100208#define vv_blob vv_di.di_tv.vval.v_blob
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000209#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000210
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200211static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000212#define vimvarht vimvardict.dv_hashtab
213
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100214static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
215static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
216static char_u *skip_var_one(char_u *arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100217static void list_glob_vars(int *first);
218static void list_buf_vars(int *first);
219static void list_win_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100220static void list_tab_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100221static void list_vim_vars(int *first);
222static void list_script_vars(int *first);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100223static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
224static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100225static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
226static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100227static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
228static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
229static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
230static void item_lock(typval_T *tv, int deep, int lock);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000231
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100232static int eval2(char_u **arg, typval_T *rettv, int evaluate);
233static int eval3(char_u **arg, typval_T *rettv, int evaluate);
234static int eval4(char_u **arg, typval_T *rettv, int evaluate);
235static int eval5(char_u **arg, typval_T *rettv, int evaluate);
236static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
237static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000238
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100239static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
240static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100241static int free_unref_items(int copyID);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100242static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100243static int get_env_len(char_u **arg);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100244static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200245static void check_vars(char_u *name, int len);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100246static typval_T *alloc_string_tv(char_u *string);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100247static void delete_var(hashtab_T *ht, hashitem_T *hi);
Bram Moolenaar32526b32019-01-19 17:43:09 +0100248static void list_one_var(dictitem_T *v, char *prefix, int *first);
249static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first);
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100250static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100251static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200252
Bram Moolenaar73dad1e2016-07-17 22:13:49 +0200253/* for VIM_VERSION_ defines */
254#include "version.h"
255
Bram Moolenaare21c1582019-03-02 11:57:09 +0100256/*
257 * Return "n1" divided by "n2", taking care of dividing by zero.
258 */
259 static varnumber_T
260num_divide(varnumber_T n1, varnumber_T n2)
261{
262 varnumber_T result;
263
264 if (n2 == 0) // give an error message?
265 {
266 if (n1 == 0)
267 result = VARNUM_MIN; // similar to NaN
268 else if (n1 < 0)
269 result = -VARNUM_MAX;
270 else
271 result = VARNUM_MAX;
272 }
273 else
274 result = n1 / n2;
275
276 return result;
277}
278
279/*
280 * Return "n1" modulus "n2", taking care of dividing by zero.
281 */
282 static varnumber_T
283num_modulus(varnumber_T n1, varnumber_T n2)
284{
285 // Give an error when n2 is 0?
286 return (n2 == 0) ? 0 : (n1 % n2);
287}
288
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100289
290#if defined(EBCDIC) || defined(PROTO)
291/*
292 * Compare struct fst by function name.
293 */
294 static int
295compare_func_name(const void *s1, const void *s2)
296{
297 struct fst *p1 = (struct fst *)s1;
298 struct fst *p2 = (struct fst *)s2;
299
300 return STRCMP(p1->f_name, p2->f_name);
301}
302
303/*
304 * Sort the function table by function name.
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100305 * The sorting of the table above is ASCII dependent.
Bram Moolenaara1fa8922017-01-12 20:06:33 +0100306 * On machines using EBCDIC we have to sort it.
307 */
308 static void
309sortFunctions(void)
310{
311 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
312
313 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
314}
315#endif
316
317
Bram Moolenaar33570922005-01-25 22:26:29 +0000318/*
319 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000320 */
321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100322eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000323{
Bram Moolenaar33570922005-01-25 22:26:29 +0000324 int i;
325 struct vimvar *p;
326
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200327 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
328 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200329 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000330 hash_init(&compat_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200331 func_init();
Bram Moolenaar33570922005-01-25 22:26:29 +0000332
333 for (i = 0; i < VV_LEN; ++i)
334 {
335 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200336 if (STRLEN(p->vv_name) > 16)
337 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100338 iemsg("INTERNAL: name too long, increase size of dictitem16_T");
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200339 getout(1);
340 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000341 STRCPY(p->vv_di.di_key, p->vv_name);
342 if (p->vv_flags & VV_RO)
343 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
344 else if (p->vv_flags & VV_RO_SBX)
345 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
346 else
347 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000348
349 /* add to v: scope dict, unless the value is not always available */
350 if (p->vv_type != VAR_UNKNOWN)
351 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000352 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000353 /* add to compat scope dict */
354 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000355 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100356 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
357
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100359 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100360 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100361 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100362 set_vim_var_dict(VV_EVENT, dict_alloc_lock(VAR_FIXED));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100363
364 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
365 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
366 set_vim_var_nr(VV_NONE, VVAL_NONE);
367 set_vim_var_nr(VV_NULL, VVAL_NULL);
368
Bram Moolenaarf562e722016-07-19 17:25:25 +0200369 set_vim_var_nr(VV_TYPE_NUMBER, VAR_TYPE_NUMBER);
370 set_vim_var_nr(VV_TYPE_STRING, VAR_TYPE_STRING);
371 set_vim_var_nr(VV_TYPE_FUNC, VAR_TYPE_FUNC);
372 set_vim_var_nr(VV_TYPE_LIST, VAR_TYPE_LIST);
373 set_vim_var_nr(VV_TYPE_DICT, VAR_TYPE_DICT);
374 set_vim_var_nr(VV_TYPE_FLOAT, VAR_TYPE_FLOAT);
375 set_vim_var_nr(VV_TYPE_BOOL, VAR_TYPE_BOOL);
376 set_vim_var_nr(VV_TYPE_NONE, VAR_TYPE_NONE);
377 set_vim_var_nr(VV_TYPE_JOB, VAR_TYPE_JOB);
378 set_vim_var_nr(VV_TYPE_CHANNEL, VAR_TYPE_CHANNEL);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100379 set_vim_var_nr(VV_TYPE_BLOB, VAR_TYPE_BLOB);
Bram Moolenaarf562e722016-07-19 17:25:25 +0200380
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200381 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200382
383#ifdef EBCDIC
384 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100385 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200386 */
387 sortFunctions();
388#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000389}
390
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000391#if defined(EXITFREE) || defined(PROTO)
392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100393eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000394{
395 int i;
396 struct vimvar *p;
397
398 for (i = 0; i < VV_LEN; ++i)
399 {
400 p = &vimvars[i];
401 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard23a8232018-02-10 18:45:26 +0100402 VIM_CLEAR(p->vv_str);
Bram Moolenaard812df62008-11-09 12:46:09 +0000403 else if (p->vv_di.di_tv.v_type == VAR_LIST)
404 {
405 list_unref(p->vv_list);
406 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000407 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000408 }
409 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000410 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000411 hash_clear(&compat_hashtab);
412
Bram Moolenaard9fba312005-06-26 22:34:35 +0000413 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100414# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200415 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100416# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000417
418 /* global variables */
419 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000420
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000421 /* autoloaded script names */
422 ga_clear_strings(&ga_loaded);
423
Bram Moolenaarcca74132013-09-25 21:00:28 +0200424 /* Script-local variables. First clear all the variables and in a second
425 * loop free the scriptvar_T, because a variable in one script might hold
426 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200427 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200428 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200429 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200430 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200431 ga_clear(&ga_scripts);
432
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000433 /* unreferenced lists and dicts */
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +0200434 (void)garbage_collect(FALSE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000435
436 /* functions */
437 free_all_functions();
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000438}
439#endif
440
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 * Set an internal variable to a string value. Creates the variable if it does
444 * not already exist.
445 */
446 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100447set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000449 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +0000450 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452 val = vim_strsave(value);
453 if (val != NULL)
454 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000455 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000456 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +0000458 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000459 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460 }
461 }
462}
463
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000464static lval_T *redir_lval = NULL;
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200465#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000466static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000467static char_u *redir_endp = NULL;
468static char_u *redir_varname = NULL;
469
470/*
471 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100472 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000473 * Returns OK if successfully completed the setup. FAIL otherwise.
474 */
475 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100476var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000477{
478 int save_emsg;
479 int err;
480 typval_T tv;
481
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000482 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000483 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000484 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100485 emsg(_(e_invarg));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000486 return FAIL;
487 }
488
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000489 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000490 redir_varname = vim_strsave(name);
491 if (redir_varname == NULL)
492 return FAIL;
493
494 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
495 if (redir_lval == NULL)
496 {
497 var_redir_stop();
498 return FAIL;
499 }
500
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000501 /* The output is stored in growarray "redir_ga" until redirection ends. */
502 ga_init2(&redir_ga, (int)sizeof(char), 500);
503
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000504 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100505 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000506 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000507 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
508 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200509 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000510 if (redir_endp != NULL && *redir_endp != NUL)
511 /* Trailing characters are present after the variable name */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100512 emsg(_(e_trailing));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000513 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100514 emsg(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000515 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000516 var_redir_stop();
517 return FAIL;
518 }
519
520 /* check if we can write to the variable: set it to or append an empty
521 * string */
522 save_emsg = did_emsg;
523 did_emsg = FALSE;
524 tv.v_type = VAR_STRING;
525 tv.vval.v_string = (char_u *)"";
526 if (append)
527 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
528 else
529 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200530 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000531 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000532 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000533 if (err)
534 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000535 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000536 var_redir_stop();
537 return FAIL;
538 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000539
540 return OK;
541}
542
543/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000544 * Append "value[value_len]" to the variable set by var_redir_start().
545 * The actual appending is postponed until redirection ends, because the value
546 * appended may in fact be the string we write to, changing it may cause freed
547 * memory to be used:
548 * :redir => foo
549 * :let foo
550 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000551 */
552 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100553var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000554{
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000555 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000556
557 if (redir_lval == NULL)
558 return;
559
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000560 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000561 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000562 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000563 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000564
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000565 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000566 {
567 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +0000568 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000569 }
570 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000571 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000572}
573
574/*
575 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000576 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000577 */
578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100579var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000580{
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000581 typval_T tv;
582
Bram Moolenaar1e5e1232016-07-07 17:33:02 +0200583 if (EVALCMD_BUSY)
584 {
585 redir_lval = NULL;
586 return;
587 }
588
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000589 if (redir_lval != NULL)
590 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000591 /* If there was no error: assign the text to the variable. */
592 if (redir_endp != NULL)
593 {
594 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
595 tv.v_type = VAR_STRING;
596 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200597 /* Call get_lval() again, if it's inside a Dict or List it may
598 * have changed. */
599 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100600 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +0200601 if (redir_endp != NULL && redir_lval->ll_name != NULL)
602 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
603 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000604 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000605
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +0000606 /* free the collected output */
Bram Moolenaard23a8232018-02-10 18:45:26 +0100607 VIM_CLEAR(redir_ga.ga_data);
Bram Moolenaar863b53b2007-01-14 14:28:34 +0000608
Bram Moolenaard23a8232018-02-10 18:45:26 +0100609 VIM_CLEAR(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000610 }
Bram Moolenaard23a8232018-02-10 18:45:26 +0100611 VIM_CLEAR(redir_varname);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000612}
613
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +0100615eval_charconvert(
616 char_u *enc_from,
617 char_u *enc_to,
618 char_u *fname_from,
619 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620{
621 int err = FALSE;
622
623 set_vim_var_string(VV_CC_FROM, enc_from, -1);
624 set_vim_var_string(VV_CC_TO, enc_to, -1);
625 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
626 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
627 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
628 err = TRUE;
629 set_vim_var_string(VV_CC_FROM, NULL, -1);
630 set_vim_var_string(VV_CC_TO, NULL, -1);
631 set_vim_var_string(VV_FNAME_IN, NULL, -1);
632 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
633
634 if (err)
635 return FAIL;
636 return OK;
637}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638
639# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100641eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642{
643 int err = FALSE;
644
645 set_vim_var_string(VV_FNAME_IN, fname, -1);
646 set_vim_var_string(VV_CMDARG, args, -1);
647 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
648 err = TRUE;
649 set_vim_var_string(VV_FNAME_IN, NULL, -1);
650 set_vim_var_string(VV_CMDARG, NULL, -1);
651
652 if (err)
653 {
654 mch_remove(fname);
655 return FAIL;
656 }
657 return OK;
658}
659# endif
660
661# if defined(FEAT_DIFF) || defined(PROTO)
662 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100663eval_diff(
664 char_u *origfile,
665 char_u *newfile,
666 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667{
668 int err = FALSE;
669
670 set_vim_var_string(VV_FNAME_IN, origfile, -1);
671 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
672 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
673 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
674 set_vim_var_string(VV_FNAME_IN, NULL, -1);
675 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
676 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
677}
678
679 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100680eval_patch(
681 char_u *origfile,
682 char_u *difffile,
683 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684{
685 int err;
686
687 set_vim_var_string(VV_FNAME_IN, origfile, -1);
688 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
689 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
690 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
691 set_vim_var_string(VV_FNAME_IN, NULL, -1);
692 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
693 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
694}
695# endif
696
697/*
698 * Top level evaluation function, returning a boolean.
699 * Sets "error" to TRUE if there was an error.
700 * Return TRUE or FALSE.
701 */
702 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100703eval_to_bool(
704 char_u *arg,
705 int *error,
706 char_u **nextcmd,
707 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708{
Bram Moolenaar33570922005-01-25 22:26:29 +0000709 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200710 varnumber_T retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711
712 if (skip)
713 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000714 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 else
717 {
718 *error = FALSE;
719 if (!skip)
720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100721 retval = (tv_get_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000722 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 }
724 }
725 if (skip)
726 --emsg_skip;
727
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200728 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729}
730
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100731/*
732 * Call eval1() and give an error message if not done at a lower level.
733 */
734 static int
735eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
736{
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100737 char_u *start = *arg;
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100738 int ret;
739 int did_emsg_before = did_emsg;
740 int called_emsg_before = called_emsg;
741
742 ret = eval1(arg, rettv, evaluate);
743 if (ret == FAIL)
744 {
745 // Report the invalid expression unless the expression evaluation has
746 // been cancelled due to an aborting error, an interrupt, or an
747 // exception, or we already gave a more specific error.
748 // Also check called_emsg for when using assert_fails().
749 if (!aborting() && did_emsg == did_emsg_before
750 && called_emsg == called_emsg_before)
Bram Moolenaar6acc79f2019-01-14 22:53:31 +0100751 semsg(_(e_invexpr2), start);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100752 }
753 return ret;
754}
755
Bram Moolenaar48570482017-10-30 21:48:41 +0100756 static int
757eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv)
758{
759 char_u *s;
760 int dummy;
761 char_u buf[NUMBUFLEN];
762
763 if (expr->v_type == VAR_FUNC)
764 {
765 s = expr->vval.v_string;
766 if (s == NULL || *s == NUL)
767 return FAIL;
768 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
769 0L, 0L, &dummy, TRUE, NULL, NULL) == FAIL)
770 return FAIL;
771 }
772 else if (expr->v_type == VAR_PARTIAL)
773 {
774 partial_T *partial = expr->vval.v_partial;
775
776 s = partial_name(partial);
777 if (s == NULL || *s == NUL)
778 return FAIL;
779 if (call_func(s, (int)STRLEN(s), rettv, argc, argv, NULL,
780 0L, 0L, &dummy, TRUE, partial, NULL) == FAIL)
781 return FAIL;
782 }
783 else
784 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100785 s = tv_get_string_buf_chk(expr, buf);
Bram Moolenaar48570482017-10-30 21:48:41 +0100786 if (s == NULL)
787 return FAIL;
788 s = skipwhite(s);
Bram Moolenaarce9d50d2019-01-14 22:22:29 +0100789 if (eval1_emsg(&s, rettv, TRUE) == FAIL)
Bram Moolenaar48570482017-10-30 21:48:41 +0100790 return FAIL;
791 if (*s != NUL) /* check for trailing chars after expr */
792 {
Bram Moolenaara43ebe92018-07-14 17:25:01 +0200793 clear_tv(rettv);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100794 semsg(_(e_invexpr2), s);
Bram Moolenaar48570482017-10-30 21:48:41 +0100795 return FAIL;
796 }
797 }
798 return OK;
799}
800
801/*
802 * Like eval_to_bool() but using a typval_T instead of a string.
803 * Works for string, funcref and partial.
804 */
805 int
806eval_expr_to_bool(typval_T *expr, int *error)
807{
808 typval_T rettv;
809 int res;
810
811 if (eval_expr_typval(expr, NULL, 0, &rettv) == FAIL)
812 {
813 *error = TRUE;
814 return FALSE;
815 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100816 res = (tv_get_number_chk(&rettv, error) != 0);
Bram Moolenaar48570482017-10-30 21:48:41 +0100817 clear_tv(&rettv);
818 return res;
819}
820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821/*
822 * Top level evaluation function, returning a string. If "skip" is TRUE,
823 * only parsing to "nextcmd" is done, without reporting errors. Return
824 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
825 */
826 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100827eval_to_string_skip(
828 char_u *arg,
829 char_u **nextcmd,
830 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831{
Bram Moolenaar33570922005-01-25 22:26:29 +0000832 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 char_u *retval;
834
835 if (skip)
836 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000837 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 retval = NULL;
839 else
840 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100841 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000842 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 }
844 if (skip)
845 --emsg_skip;
846
847 return retval;
848}
849
850/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000851 * Skip over an expression at "*pp".
852 * Return FAIL for an error, OK otherwise.
853 */
854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100855skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000856{
Bram Moolenaar33570922005-01-25 22:26:29 +0000857 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000858
859 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000860 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +0000861}
862
863/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +0000865 * When "convert" is TRUE convert a List into a sequence of lines and convert
866 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 * Return pointer to allocated memory, or NULL for failure.
868 */
869 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100870eval_to_string(
871 char_u *arg,
872 char_u **nextcmd,
873 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874{
Bram Moolenaar33570922005-01-25 22:26:29 +0000875 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000877 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000878#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +0000879 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +0000880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000882 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 retval = NULL;
884 else
885 {
Bram Moolenaara85fb752008-09-07 11:55:43 +0000886 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000887 {
888 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +0000889 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200890 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +0200891 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +0200892 if (tv.vval.v_list->lv_len > 0)
893 ga_append(&ga, NL);
894 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000895 ga_append(&ga, NUL);
896 retval = (char_u *)ga.ga_data;
897 }
Bram Moolenaara85fb752008-09-07 11:55:43 +0000898#ifdef FEAT_FLOAT
899 else if (convert && tv.v_type == VAR_FLOAT)
900 {
901 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
902 retval = vim_strsave(numbuf);
903 }
904#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000905 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100906 retval = vim_strsave(tv_get_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000907 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909
910 return retval;
911}
912
913/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000914 * Call eval_to_string() without using current local variables and using
915 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 */
917 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918eval_to_string_safe(
919 char_u *arg,
920 char_u **nextcmd,
921 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922{
923 char_u *retval;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200924 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200926 save_funccal(&funccal_entry);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000927 if (use_sandbox)
928 ++sandbox;
929 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000930 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000931 if (use_sandbox)
932 --sandbox;
933 --textlock;
Bram Moolenaar27e80c82018-10-14 21:41:01 +0200934 restore_funccal();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 return retval;
936}
937
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938/*
939 * Top level evaluation function, returning a number.
940 * Evaluates "expr" silently.
941 * Returns -1 for an error.
942 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200943 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +0100944eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945{
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200947 varnumber_T retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +0000948 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 ++emsg_off;
951
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000952 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 retval = -1;
954 else
955 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100956 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 --emsg_off;
960
961 return retval;
962}
963
Bram Moolenaara40058a2005-07-11 22:42:07 +0000964/*
965 * Prepare v: variable "idx" to be used.
966 * Save the current typeval in "save_tv".
967 * When not used yet add the variable to the v: hashtable.
968 */
969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100970prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000971{
972 *save_tv = vimvars[idx].vv_tv;
973 if (vimvars[idx].vv_type == VAR_UNKNOWN)
974 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
975}
976
977/*
978 * Restore v: variable "idx" to typeval "save_tv".
979 * When no longer defined, remove the variable from the v: hashtable.
980 */
981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100982restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +0000983{
984 hashitem_T *hi;
985
Bram Moolenaara40058a2005-07-11 22:42:07 +0000986 vimvars[idx].vv_tv = *save_tv;
987 if (vimvars[idx].vv_type == VAR_UNKNOWN)
988 {
989 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
990 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100991 internal_error("restore_vimvar()");
Bram Moolenaara40058a2005-07-11 22:42:07 +0000992 else
993 hash_remove(&vimvarht, hi);
994 }
995}
996
Bram Moolenaar3c56a962006-03-12 22:19:04 +0000997#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000998/*
999 * Evaluate an expression to a list with suggestions.
1000 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001001 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001002 */
1003 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001004eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001005{
1006 typval_T save_val;
1007 typval_T rettv;
1008 list_T *list = NULL;
1009 char_u *p = skipwhite(expr);
1010
1011 /* Set "v:val" to the bad word. */
1012 prepare_vimvar(VV_VAL, &save_val);
1013 vimvars[VV_VAL].vv_type = VAR_STRING;
1014 vimvars[VV_VAL].vv_str = badword;
1015 if (p_verbose == 0)
1016 ++emsg_off;
1017
1018 if (eval1(&p, &rettv, TRUE) == OK)
1019 {
1020 if (rettv.v_type != VAR_LIST)
1021 clear_tv(&rettv);
1022 else
1023 list = rettv.vval.v_list;
1024 }
1025
1026 if (p_verbose == 0)
1027 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001028 restore_vimvar(VV_VAL, &save_val);
1029
1030 return list;
1031}
1032
1033/*
1034 * "list" is supposed to contain two items: a word and a number. Return the
1035 * word in "pp" and the number as the return value.
1036 * Return -1 if anything isn't right.
1037 * Used to get the good word and score from the eval_spell_expr() result.
1038 */
1039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001041{
1042 listitem_T *li;
1043
1044 li = list->lv_first;
1045 if (li == NULL)
1046 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001047 *pp = tv_get_string(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001048
1049 li = li->li_next;
1050 if (li == NULL)
1051 return -1;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001052 return (int)tv_get_number(&li->li_tv);
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001053}
1054#endif
1055
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001056/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001057 * Top level evaluation function.
1058 * Returns an allocated typval_T with the result.
1059 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001060 */
1061 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001062eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001063{
1064 typval_T *tv;
1065
1066 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001067 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaard23a8232018-02-10 18:45:26 +01001068 VIM_CLEAR(tv);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001069
1070 return tv;
1071}
1072
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001075 * Call some Vim script function and return the result in "*rettv".
Bram Moolenaarffa96842018-06-12 22:05:14 +02001076 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
1077 * should have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001078 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001081call_vim_function(
1082 char_u *func,
1083 int argc,
Bram Moolenaarffa96842018-06-12 22:05:14 +02001084 typval_T *argv,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001085 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 int doesrange;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001088 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001090 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaarffa96842018-06-12 22:05:14 +02001091 ret = call_func(func, (int)STRLEN(func), rettv, argc, argv, NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001093 &doesrange, TRUE, NULL, NULL);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001094 if (ret == FAIL)
1095 clear_tv(rettv);
1096
1097 return ret;
1098}
1099
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001100/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001101 * Call Vim script function "func" and return the result as a number.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001102 * Returns -1 when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001103 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1104 * have type VAR_UNKNOWN.
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001105 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001106 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01001107call_func_retnr(
1108 char_u *func,
1109 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001110 typval_T *argv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001111{
1112 typval_T rettv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001113 varnumber_T retval;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001114
Bram Moolenaarded27a12018-08-01 19:06:03 +02001115 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001116 return -1;
1117
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001118 retval = tv_get_number_chk(&rettv, NULL);
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001119 clear_tv(&rettv);
1120 return retval;
1121}
1122
1123#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1124 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1125
Bram Moolenaar4f688582007-07-24 12:34:30 +00001126# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001127/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001128 * Call Vim script function "func" and return the result as a string.
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001129 * Returns NULL when calling the function fails.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001130 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1131 * have type VAR_UNKNOWN.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001132 */
1133 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001134call_func_retstr(
1135 char_u *func,
1136 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001137 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001138{
1139 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001140 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001141
Bram Moolenaarded27a12018-08-01 19:06:03 +02001142 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001143 return NULL;
1144
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001145 retval = vim_strsave(tv_get_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001146 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147 return retval;
1148}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001149# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001150
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001151/*
Bram Moolenaarb544f3c2017-02-23 19:03:28 +01001152 * Call Vim script function "func" and return the result as a List.
Bram Moolenaarffa96842018-06-12 22:05:14 +02001153 * Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc] should
1154 * have type VAR_UNKNOWN.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001155 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001156 */
1157 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001158call_func_retlist(
1159 char_u *func,
1160 int argc,
Bram Moolenaarded27a12018-08-01 19:06:03 +02001161 typval_T *argv)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001162{
1163 typval_T rettv;
1164
Bram Moolenaarded27a12018-08-01 19:06:03 +02001165 if (call_vim_function(func, argc, argv, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001166 return NULL;
1167
1168 if (rettv.v_type != VAR_LIST)
1169 {
1170 clear_tv(&rettv);
1171 return NULL;
1172 }
1173
1174 return rettv.vval.v_list;
1175}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#endif
1177
Bram Moolenaar05159a02005-02-26 23:04:13 +00001178
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179#ifdef FEAT_FOLDING
1180/*
1181 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1182 * it in "*cp". Doesn't give error messages.
1183 */
1184 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001185eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186{
Bram Moolenaar33570922005-01-25 22:26:29 +00001187 typval_T tv;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001188 varnumber_T retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001190 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1191 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
1193 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001194 if (use_sandbox)
1195 ++sandbox;
1196 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001198 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 retval = 0;
1200 else
1201 {
1202 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001203 if (tv.v_type == VAR_NUMBER)
1204 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001205 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 retval = 0;
1207 else
1208 {
1209 /* If the result is a string, check if there is a non-digit before
1210 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001211 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 if (!VIM_ISDIGIT(*s) && *s != '-')
1213 *cp = *s++;
1214 retval = atol((char *)s);
1215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001216 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 }
1218 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001219 if (use_sandbox)
1220 --sandbox;
1221 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02001223 return (int)retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224}
1225#endif
1226
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001228 * ":let" list all variable values
1229 * ":let var1 var2" list variable values
1230 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001231 * ":let var += expr" assignment command.
1232 * ":let var -= expr" assignment command.
Bram Moolenaarff697e62019-02-12 22:28:33 +01001233 * ":let var *= expr" assignment command.
1234 * ":let var /= expr" assignment command.
1235 * ":let var %= expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001236 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001237 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 */
1239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001240ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241{
1242 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001243 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001244 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001245 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001246 int var_count = 0;
1247 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001248 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001249 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001250 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251
Bram Moolenaardb552d602006-03-23 22:59:57 +00001252 argend = skip_var_list(arg, &var_count, &semicolon);
1253 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001254 return;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001255 if (argend > arg && argend[-1] == '.') // for var.='str'
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001256 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001257 expr = skipwhite(argend);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001258 if (*expr != '=' && !(vim_strchr((char_u *)"+-*/%.", *expr) != NULL
Bram Moolenaara3920382014-03-30 16:49:09 +02001259 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001261 /*
1262 * ":let" without "=": list variables
1263 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001264 if (*arg == '[')
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001265 emsg(_(e_invarg));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001266 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001267 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001268 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001270 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001271 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001272 list_glob_vars(&first);
1273 list_buf_vars(&first);
1274 list_win_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001275 list_tab_vars(&first);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001276 list_script_vars(&first);
1277 list_func_vars(&first);
1278 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001280 eap->nextcmd = check_nextcmd(arg);
1281 }
1282 else
1283 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001284 op[0] = '=';
1285 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001286 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001287 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001288 if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL)
1289 op[0] = *expr; // +=, -=, *=, /=, %= or .=
Bram Moolenaara3920382014-03-30 16:49:09 +02001290 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001291 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001292 else
1293 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001294
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 if (eap->skip)
1296 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001297 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 if (eap->skip)
1299 {
1300 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001301 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 --emsg_skip;
1303 }
1304 else if (i != FAIL)
1305 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001306 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001307 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001308 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 }
1311}
1312
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001313/*
1314 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1315 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001316 * When "nextchars" is not NULL it points to a string with characters that
1317 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1318 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001319 * Returns OK or FAIL;
1320 */
1321 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001322ex_let_vars(
1323 char_u *arg_start,
1324 typval_T *tv,
1325 int copy, /* copy values from "tv", don't move */
1326 int semicolon, /* from skip_var_list() */
1327 int var_count, /* from skip_var_list() */
1328 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001329{
1330 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001331 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001332 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001333 listitem_T *item;
1334 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001335
1336 if (*arg != '[')
1337 {
1338 /*
1339 * ":let var = expr" or ":for var in list"
1340 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001341 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001342 return FAIL;
1343 return OK;
1344 }
1345
1346 /*
1347 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1348 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001349 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001350 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001351 emsg(_(e_listreq));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001352 return FAIL;
1353 }
1354
1355 i = list_len(l);
1356 if (semicolon == 0 && var_count < i)
1357 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001358 emsg(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001359 return FAIL;
1360 }
1361 if (var_count - semicolon > i)
1362 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001363 emsg(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001364 return FAIL;
1365 }
1366
1367 item = l->lv_first;
1368 while (*arg != ']')
1369 {
1370 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001371 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001372 item = item->li_next;
1373 if (arg == NULL)
1374 return FAIL;
1375
1376 arg = skipwhite(arg);
1377 if (*arg == ';')
1378 {
1379 /* Put the rest of the list (may be empty) in the var after ';'.
1380 * Create a new list for this. */
1381 l = list_alloc();
1382 if (l == NULL)
1383 return FAIL;
1384 while (item != NULL)
1385 {
1386 list_append_tv(l, &item->li_tv);
1387 item = item->li_next;
1388 }
1389
1390 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001391 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001392 ltv.vval.v_list = l;
1393 l->lv_refcount = 1;
1394
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001395 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1396 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001397 clear_tv(&ltv);
1398 if (arg == NULL)
1399 return FAIL;
1400 break;
1401 }
1402 else if (*arg != ',' && *arg != ']')
1403 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01001404 internal_error("ex_let_vars()");
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001405 return FAIL;
1406 }
1407 }
1408
1409 return OK;
1410}
1411
1412/*
1413 * Skip over assignable variable "var" or list of variables "[var, var]".
1414 * Used for ":let varvar = expr" and ":for varvar in expr".
1415 * For "[var, var]" increment "*var_count" for each variable.
1416 * for "[var, var; var]" set "semicolon".
1417 * Return NULL for an error.
1418 */
1419 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420skip_var_list(
1421 char_u *arg,
1422 int *var_count,
1423 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001424{
1425 char_u *p, *s;
1426
1427 if (*arg == '[')
1428 {
1429 /* "[var, var]": find the matching ']'. */
1430 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001431 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001432 {
1433 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
1434 s = skip_var_one(p);
1435 if (s == p)
1436 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001437 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001438 return NULL;
1439 }
1440 ++*var_count;
1441
1442 p = skipwhite(s);
1443 if (*p == ']')
1444 break;
1445 else if (*p == ';')
1446 {
1447 if (*semicolon == 1)
1448 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001449 emsg(_("Double ; in list of variables"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001450 return NULL;
1451 }
1452 *semicolon = 1;
1453 }
1454 else if (*p != ',')
1455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001456 semsg(_(e_invarg2), p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001457 return NULL;
1458 }
1459 }
1460 return p + 1;
1461 }
1462 else
1463 return skip_var_one(arg);
1464}
1465
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001466/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00001467 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00001468 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001469 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001470 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001471skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001472{
Bram Moolenaar92124a32005-06-17 22:03:40 +00001473 if (*arg == '@' && arg[1] != NUL)
1474 return arg + 2;
1475 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
1476 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001477}
1478
Bram Moolenaara7043832005-01-21 11:56:39 +00001479/*
Bram Moolenaar33570922005-01-25 22:26:29 +00001480 * List variables for hashtab "ht" with prefix "prefix".
1481 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00001482 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001483 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001484list_hashtable_vars(
1485 hashtab_T *ht,
Bram Moolenaar32526b32019-01-19 17:43:09 +01001486 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01001487 int empty,
1488 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001489{
Bram Moolenaar33570922005-01-25 22:26:29 +00001490 hashitem_T *hi;
1491 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00001492 int todo;
Bram Moolenaarf86db782018-10-25 13:31:37 +02001493 char_u buf[IOSIZE];
Bram Moolenaara7043832005-01-21 11:56:39 +00001494
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001495 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00001496 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
1497 {
1498 if (!HASHITEM_EMPTY(hi))
1499 {
1500 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00001501 di = HI2DI(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001502
1503 // apply :filter /pat/ to variable name
Bram Moolenaar32526b32019-01-19 17:43:09 +01001504 vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1);
1505 vim_strcat((char_u *)buf, di->di_key, IOSIZE);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001506 if (message_filtered(buf))
1507 continue;
1508
Bram Moolenaar33570922005-01-25 22:26:29 +00001509 if (empty || di->di_tv.v_type != VAR_STRING
1510 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001511 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001512 }
1513 }
1514}
1515
1516/*
1517 * List global variables.
1518 */
1519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001520list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001521{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001522 list_hashtable_vars(&globvarht, "", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001523}
1524
1525/*
1526 * List buffer variables.
1527 */
1528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001530{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001531 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001532}
1533
1534/*
1535 * List window variables.
1536 */
1537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001538list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00001539{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001540 list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001541}
1542
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001543/*
1544 * List tab page variables.
1545 */
1546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001547list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001548{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001549 list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001550}
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001551
Bram Moolenaara7043832005-01-21 11:56:39 +00001552/*
1553 * List Vim variables.
1554 */
1555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001557{
Bram Moolenaar32526b32019-01-19 17:43:09 +01001558 list_hashtable_vars(&vimvarht, "v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001559}
1560
1561/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001562 * List script-local variables, if there is a script.
1563 */
1564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001565list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001566{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001567 if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
1568 list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid),
Bram Moolenaar32526b32019-01-19 17:43:09 +01001569 "s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00001570}
1571
1572/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001573 * List variables in "arg".
1574 */
1575 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001576list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001577{
1578 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001579 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001580 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001581 char_u *name_start;
1582 char_u *arg_subsc;
1583 char_u *tofree;
1584 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001585
1586 while (!ends_excmd(*arg) && !got_int)
1587 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001588 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001589 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001590 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar1c465442017-03-12 20:10:05 +01001591 if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001592 {
1593 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001594 emsg(_(e_trailing));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001595 break;
1596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001597 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001598 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001599 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001600 /* get_name_len() takes care of expanding curly braces */
1601 name_start = name = arg;
1602 len = get_name_len(&arg, &tofree, TRUE, TRUE);
1603 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001604 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001605 /* This is mainly to keep test 49 working: when expanding
1606 * curly braces fails overrule the exception error message. */
1607 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001608 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001609 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001610 semsg(_(e_invarg2), arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001611 break;
1612 }
1613 error = TRUE;
1614 }
1615 else
1616 {
1617 if (tofree != NULL)
1618 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02001619 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001620 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001621 else
1622 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001623 /* handle d.key, l[idx], f(expr) */
1624 arg_subsc = arg;
1625 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00001626 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001627 else
Bram Moolenaara7043832005-01-21 11:56:39 +00001628 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001629 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00001630 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001631 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00001632 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001633 case 'g': list_glob_vars(first); break;
1634 case 'b': list_buf_vars(first); break;
1635 case 'w': list_win_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001636 case 't': list_tab_vars(first); break;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001637 case 'v': list_vim_vars(first); break;
1638 case 's': list_script_vars(first); break;
1639 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001640 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001641 semsg(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00001642 }
Bram Moolenaara7043832005-01-21 11:56:39 +00001643 }
1644 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001645 {
1646 char_u numbuf[NUMBUFLEN];
1647 char_u *tf;
1648 int c;
1649 char_u *s;
1650
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001651 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001652 c = *arg;
1653 *arg = NUL;
Bram Moolenaar32526b32019-01-19 17:43:09 +01001654 list_one_var_a("",
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001655 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001656 tv.v_type,
1657 s == NULL ? (char_u *)"" : s,
1658 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001659 *arg = c;
1660 vim_free(tf);
1661 }
1662 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00001663 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001664 }
1665 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001666
1667 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001668 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00001669
1670 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001671 }
1672
1673 return arg;
1674}
1675
1676/*
1677 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
1678 * Returns a pointer to the char just after the var name.
1679 * Returns NULL if there is an error.
1680 */
1681 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001682ex_let_one(
1683 char_u *arg, /* points to variable name */
1684 typval_T *tv, /* value to assign to variable */
1685 int copy, /* copy value from "tv" */
1686 char_u *endchars, /* valid chars after variable name or NULL */
1687 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001688{
1689 int c1;
1690 char_u *name;
1691 char_u *p;
1692 char_u *arg_end = NULL;
1693 int len;
1694 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001695 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001696
1697 /*
1698 * ":let $VAR = expr": Set environment variable.
1699 */
1700 if (*arg == '$')
1701 {
1702 /* Find the end of the name. */
1703 ++arg;
1704 name = arg;
1705 len = get_env_len(&arg);
1706 if (len == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001707 semsg(_(e_invarg2), name - 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001708 else
1709 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001710 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001711 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001712 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001713 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001714 emsg(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01001715 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001716 {
1717 c1 = name[len];
1718 name[len] = NUL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001719 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001720 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001721 {
1722 int mustfree = FALSE;
1723 char_u *s = vim_getenv(name, &mustfree);
1724
1725 if (s != NULL)
1726 {
1727 p = tofree = concat_str(s, p);
1728 if (mustfree)
1729 vim_free(s);
1730 }
1731 }
1732 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001733 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001734 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001735 if (STRICMP(name, "HOME") == 0)
1736 init_homedir();
1737 else if (didset_vim && STRICMP(name, "VIM") == 0)
1738 didset_vim = FALSE;
1739 else if (didset_vimruntime
1740 && STRICMP(name, "VIMRUNTIME") == 0)
1741 didset_vimruntime = FALSE;
1742 arg_end = arg;
1743 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001744 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001745 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001746 }
1747 }
1748 }
1749
1750 /*
1751 * ":let &option = expr": Set option value.
1752 * ":let &l:option = expr": Set local option value.
1753 * ":let &g:option = expr": Set global option value.
1754 */
1755 else if (*arg == '&')
1756 {
1757 /* Find the end of the name. */
1758 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001759 if (p == NULL || (endchars != NULL
1760 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001761 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001762 else
1763 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001764 long n;
1765 int opt_type;
1766 long numval;
1767 char_u *stringval = NULL;
1768 char_u *s;
1769
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001770 c1 = *p;
1771 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001772
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001773 n = (long)tv_get_number(tv);
1774 s = tv_get_string_chk(tv); /* != NULL if number or string */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001775 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001776 {
1777 opt_type = get_option_value(arg, &numval,
1778 &stringval, opt_flags);
1779 if ((opt_type == 1 && *op == '.')
1780 || (opt_type == 0 && *op != '.'))
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001781 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001782 semsg(_(e_letwrong), op);
Bram Moolenaarff697e62019-02-12 22:28:33 +01001783 s = NULL; // don't set the value
Bram Moolenaar2a6a6c32017-10-02 19:29:48 +02001784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001785 else
1786 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001787 if (opt_type == 1) // number
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001788 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01001789 switch (*op)
1790 {
1791 case '+': n = numval + n; break;
1792 case '-': n = numval - n; break;
1793 case '*': n = numval * n; break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01001794 case '/': n = (long)num_divide(numval, n); break;
1795 case '%': n = (long)num_modulus(numval, n); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001796 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001797 }
Bram Moolenaarff697e62019-02-12 22:28:33 +01001798 else if (opt_type == 0 && stringval != NULL) // string
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001799 {
1800 s = concat_str(stringval, s);
1801 vim_free(stringval);
1802 stringval = s;
1803 }
1804 }
1805 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001806 if (s != NULL)
1807 {
1808 set_option_value(arg, n, s, opt_flags);
1809 arg_end = p;
1810 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001811 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001812 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 }
1814 }
1815
1816 /*
1817 * ":let @r = expr": Set register contents.
1818 */
1819 else if (*arg == '@')
1820 {
1821 ++arg;
Bram Moolenaarff697e62019-02-12 22:28:33 +01001822 if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001823 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001824 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001825 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001826 emsg(_(e_letunexp));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 else
1828 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001829 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 char_u *s;
1831
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001832 p = tv_get_string_chk(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001833 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001834 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02001835 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001836 if (s != NULL)
1837 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00001838 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001839 vim_free(s);
1840 }
1841 }
1842 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001843 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001844 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001845 arg_end = arg + 1;
1846 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00001847 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001848 }
1849 }
1850
1851 /*
1852 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001853 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001854 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001855 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 {
Bram Moolenaar33570922005-01-25 22:26:29 +00001857 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001859 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001860 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001862 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001863 emsg(_(e_letunexp));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001864 else
1865 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001866 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001867 arg_end = p;
1868 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001870 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 }
1872
1873 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001874 semsg(_(e_invarg2), arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001875
1876 return arg_end;
1877}
1878
1879/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001880 * Get an lval: variable, Dict item or List item that can be assigned a value
1881 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
1882 * "name.key", "name.key[expr]" etc.
1883 * Indexing only works if "name" is an existing List or Dictionary.
1884 * "name" points to the start of the name.
1885 * If "rettv" is not NULL it points to the value to be assigned.
1886 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
1887 * wrong; must end in space or cmd separator.
1888 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001889 * flags:
1890 * GLV_QUIET: do not give error messages
Bram Moolenaar3a257732017-02-21 20:47:13 +01001891 * GLV_READ_ONLY: will not change the variable
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001892 * GLV_NO_AUTOLOAD: do not use script autoloading
1893 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001894 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00001895 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001896 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001898 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001899get_lval(
1900 char_u *name,
1901 typval_T *rettv,
1902 lval_T *lp,
1903 int unlet,
1904 int skip,
1905 int flags, /* GLV_ values */
1906 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001907{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001909 char_u *expr_start, *expr_end;
1910 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00001911 dictitem_T *v;
1912 typval_T var1;
1913 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001914 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00001915 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001916 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00001917 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001919 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001920
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001921 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00001922 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001923
1924 if (skip)
1925 {
1926 /* When skipping just find the end of the name. */
1927 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001928 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001929 }
1930
1931 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001932 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001933 if (expr_start != NULL)
1934 {
1935 /* Don't expand the name when we already know there is an error. */
Bram Moolenaar1c465442017-03-12 20:10:05 +01001936 if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001937 && *p != '[' && *p != '.')
1938 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001939 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001940 return NULL;
1941 }
1942
1943 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
1944 if (lp->ll_exp_name == NULL)
1945 {
1946 /* Report an invalid expression in braces, unless the
1947 * expression evaluation has been cancelled due to an
1948 * aborting error, an interrupt, or an exception. */
1949 if (!aborting() && !quiet)
1950 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00001951 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001952 semsg(_(e_invarg2), name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001953 return NULL;
1954 }
1955 }
1956 lp->ll_name = lp->ll_exp_name;
1957 }
1958 else
1959 lp->ll_name = name;
1960
1961 /* Without [idx] or .key we are done. */
1962 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
1963 return p;
1964
1965 cc = *p;
1966 *p = NUL;
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001967 /* Only pass &ht when we would write to the variable, it prevents autoload
1968 * as well. */
1969 v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
1970 flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001971 if (v == NULL && !quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001972 semsg(_(e_undefvar), lp->ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001973 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001974 if (v == NULL)
1975 return NULL;
1976
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001977 /*
1978 * Loop until no more [idx] or .key is following.
1979 */
Bram Moolenaar33570922005-01-25 22:26:29 +00001980 lp->ll_tv = &v->di_tv;
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01001981 var1.v_type = VAR_UNKNOWN;
1982 var2.v_type = VAR_UNKNOWN;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001983 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001984 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001985 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
1986 && !(lp->ll_tv->v_type == VAR_DICT
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01001987 && lp->ll_tv->vval.v_dict != NULL)
1988 && !(lp->ll_tv->v_type == VAR_BLOB
1989 && lp->ll_tv->vval.v_blob != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001990 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001991 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001992 emsg(_("E689: Can only index a List, Dictionary or Blob"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001993 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001995 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001996 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001997 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 emsg(_("E708: [:] must come last"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00001999 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002000 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002001
Bram Moolenaar8c711452005-01-14 21:53:12 +00002002 len = -1;
2003 if (*p == '.')
2004 {
2005 key = p + 1;
2006 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2007 ;
2008 if (len == 0)
2009 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002010 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002011 emsg(_(e_emptykey));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002012 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002013 }
2014 p = key + len;
2015 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002016 else
2017 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002018 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002019 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002020 if (*p == ':')
2021 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002022 else
2023 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002024 empty1 = FALSE;
2025 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002026 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002027 if (tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002028 {
2029 /* not a number or string */
2030 clear_tv(&var1);
2031 return NULL;
2032 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002033 }
2034
2035 /* Optionally get the second index [ :expr]. */
2036 if (*p == ':')
2037 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002038 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002039 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002040 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002041 emsg(_(e_dictrange));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002042 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002043 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002044 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002045 if (rettv != NULL
2046 && !(rettv->v_type == VAR_LIST
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002047 && rettv->vval.v_list != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002048 && !(rettv->v_type == VAR_BLOB
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002049 && rettv->vval.v_blob != NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002050 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002051 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002052 emsg(_("E709: [:] requires a List or Blob value"));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002053 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002054 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002055 }
2056 p = skipwhite(p + 1);
2057 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002058 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002059 else
2060 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002061 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002062 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2063 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002064 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002065 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002066 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002067 if (tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002068 {
2069 /* not a number or string */
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002070 clear_tv(&var1);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002071 clear_tv(&var2);
2072 return NULL;
2073 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002074 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002075 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002076 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002077 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002078 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002079
Bram Moolenaar8c711452005-01-14 21:53:12 +00002080 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002081 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002082 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002083 emsg(_(e_missbrac));
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002084 clear_tv(&var1);
2085 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002086 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002087 }
2088
2089 /* Skip to past ']'. */
2090 ++p;
2091 }
2092
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002093 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002094 {
2095 if (len == -1)
2096 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002097 /* "[key]": get key from "var1" */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002098 key = tv_get_string_chk(&var1); /* is number or string */
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02002099 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002100 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002101 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002102 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002103 }
2104 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002105 lp->ll_list = NULL;
2106 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002107 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002108
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002109 /* When assigning to a scope dictionary check that a function and
2110 * variable name is valid (only variable name unless it is l: or
2111 * g: dictionary). Disallow overwriting a builtin function. */
2112 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002113 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002114 int prevval;
2115 int wrong;
2116
2117 if (len != -1)
2118 {
2119 prevval = key[len];
2120 key[len] = NUL;
2121 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002122 else
2123 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002124 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2125 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002126 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002127 || !valid_varname(key);
2128 if (len != -1)
2129 key[len] = prevval;
2130 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002131 return NULL;
2132 }
2133
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002134 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002135 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01002136 // Can't add "v:" or "a:" variable.
2137 if (lp->ll_dict == &vimvardict
2138 || &lp->ll_dict->dv_hashtab == get_funccal_args_ht())
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002139 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002140 semsg(_(e_illvar), name);
Bram Moolenaarab89d7a2019-03-17 14:43:31 +01002141 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002142 return NULL;
2143 }
2144
Bram Moolenaar31b81602019-02-10 22:14:27 +01002145 // Key does not exist in dict: may need to add it.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002146 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002147 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002148 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002149 semsg(_(e_dictkey), key);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002150 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002151 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002152 }
2153 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002154 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002155 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002156 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002157 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002158 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002159 p = NULL;
2160 break;
2161 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002162 /* existing variable, need to check if it can be changed */
Bram Moolenaar3a257732017-02-21 20:47:13 +01002163 else if ((flags & GLV_READ_ONLY) == 0
2164 && var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar49439c42017-02-20 23:07:05 +01002165 {
2166 clear_tv(&var1);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002167 return NULL;
Bram Moolenaar49439c42017-02-20 23:07:05 +01002168 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002169
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002170 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002171 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002172 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002173 else if (lp->ll_tv->v_type == VAR_BLOB)
2174 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002175 long bloblen = blob_len(lp->ll_tv->vval.v_blob);
2176
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002177 /*
2178 * Get the number and item for the only or first index of the List.
2179 */
2180 if (empty1)
2181 lp->ll_n1 = 0;
2182 else
2183 // is number or string
2184 lp->ll_n1 = (long)tv_get_number(&var1);
2185 clear_tv(&var1);
2186
2187 if (lp->ll_n1 < 0
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002188 || lp->ll_n1 > bloblen
2189 || (lp->ll_range && lp->ll_n1 == bloblen))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002190 {
2191 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002192 semsg(_(e_blobidx), lp->ll_n1);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002193 clear_tv(&var2);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002194 return NULL;
2195 }
2196 if (lp->ll_range && !lp->ll_empty2)
2197 {
2198 lp->ll_n2 = (long)tv_get_number(&var2);
2199 clear_tv(&var2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002200 if (lp->ll_n2 < 0
2201 || lp->ll_n2 >= bloblen
2202 || lp->ll_n2 < lp->ll_n1)
2203 {
2204 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002205 semsg(_(e_blobidx), lp->ll_n2);
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002206 return NULL;
2207 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002208 }
2209 lp->ll_blob = lp->ll_tv->vval.v_blob;
2210 lp->ll_tv = NULL;
Bram Moolenaar61be3762019-03-19 23:04:17 +01002211 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002212 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002213 else
2214 {
2215 /*
2216 * Get the number and item for the only or first index of the List.
2217 */
2218 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002219 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002220 else
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002221 /* is number or string */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002222 lp->ll_n1 = (long)tv_get_number(&var1);
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002223 clear_tv(&var1);
2224
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002225 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002226 lp->ll_list = lp->ll_tv->vval.v_list;
2227 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2228 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002229 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002230 if (lp->ll_n1 < 0)
2231 {
2232 lp->ll_n1 = 0;
2233 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2234 }
2235 }
2236 if (lp->ll_li == NULL)
2237 {
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002238 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002239 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002240 semsg(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002241 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002242 }
2243
2244 /*
2245 * May need to find the item or absolute index for the second
2246 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002247 * When no index given: "lp->ll_empty2" is TRUE.
2248 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002249 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002250 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002251 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002252 lp->ll_n2 = (long)tv_get_number(&var2);
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002253 /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002254 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002255 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002256 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002257 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002258 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002259 {
2260 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002261 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002262 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002263 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002264 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002265 }
2266
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002267 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2268 if (lp->ll_n1 < 0)
2269 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2270 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002271 {
2272 if (!quiet)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002273 semsg(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002274 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002275 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002276 }
2277
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002278 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002279 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 }
2281
Bram Moolenaarf06e5a52017-02-23 14:25:17 +01002282 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002283 return p;
2284}
2285
2286/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002287 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002288 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002290clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002291{
2292 vim_free(lp->ll_exp_name);
2293 vim_free(lp->ll_newkey);
2294}
2295
2296/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002297 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002298 * "endp" points to just after the parsed name.
Bram Moolenaarff697e62019-02-12 22:28:33 +01002299 * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=",
2300 * "%" for "%=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002301 */
2302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002303set_var_lval(
2304 lval_T *lp,
2305 char_u *endp,
2306 typval_T *rettv,
2307 int copy,
2308 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002309{
2310 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002311 listitem_T *ri;
2312 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002313
2314 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002315 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002316 cc = *endp;
2317 *endp = NUL;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002318 if (lp->ll_blob != NULL)
2319 {
2320 int error = FALSE, val;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002321
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002322 if (op != NULL && *op != '=')
2323 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002324 semsg(_(e_letwrong), op);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002325 return;
2326 }
2327
2328 if (lp->ll_range && rettv->v_type == VAR_BLOB)
2329 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002330 int il, ir;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002331
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002332 if (lp->ll_empty2)
2333 lp->ll_n2 = blob_len(lp->ll_blob) - 1;
2334
2335 if (lp->ll_n2 - lp->ll_n1 + 1 != blob_len(rettv->vval.v_blob))
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002336 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002337 emsg(_("E972: Blob value does not have the right number of bytes"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002338 return;
2339 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002340 if (lp->ll_empty2)
2341 lp->ll_n2 = blob_len(lp->ll_blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002342
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002343 ir = 0;
2344 for (il = lp->ll_n1; il <= lp->ll_n2; il++)
2345 blob_set(lp->ll_blob, il,
2346 blob_get(rettv->vval.v_blob, ir++));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002347 }
2348 else
2349 {
2350 val = (int)tv_get_number_chk(rettv, &error);
2351 if (!error)
2352 {
2353 garray_T *gap = &lp->ll_blob->bv_ga;
2354
2355 // Allow for appending a byte. Setting a byte beyond
2356 // the end is an error otherwise.
2357 if (lp->ll_n1 < gap->ga_len
2358 || (lp->ll_n1 == gap->ga_len
2359 && ga_grow(&lp->ll_blob->bv_ga, 1) == OK))
2360 {
2361 blob_set(lp->ll_blob, lp->ll_n1, val);
2362 if (lp->ll_n1 == gap->ga_len)
2363 ++gap->ga_len;
2364 }
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002365 // error for invalid range was already given in get_lval()
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002366 }
2367 }
2368 }
2369 else if (op != NULL && *op != '=')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002370 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01002371 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372
Bram Moolenaarff697e62019-02-12 22:28:33 +01002373 // handle +=, -=, *=, /=, %= and .=
Bram Moolenaar79518e22017-02-17 16:31:35 +01002374 di = NULL;
2375 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
2376 &tv, &di, TRUE, FALSE) == OK)
2377 {
2378 if ((di == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002379 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2380 && !tv_check_lock(&di->di_tv, lp->ll_name, FALSE)))
Bram Moolenaar79518e22017-02-17 16:31:35 +01002381 && tv_op(&tv, rettv, op) == OK)
2382 set_var(lp->ll_name, &tv, FALSE);
2383 clear_tv(&tv);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002385 }
Bram Moolenaar79518e22017-02-17 16:31:35 +01002386 else
2387 set_var(lp->ll_name, rettv, copy);
2388 *endp = cc;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002389 }
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002390 else if (var_check_lock(lp->ll_newkey == NULL
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002391 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002392 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002393 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002394 else if (lp->ll_range)
2395 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002396 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01002397 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002398
2399 /*
2400 * Check whether any of the list items is locked
2401 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002402 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002403 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002404 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002405 return;
2406 ri = ri->li_next;
2407 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2408 break;
2409 ll_li = ll_li->li_next;
2410 ++ll_n1;
2411 }
2412
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002413 /*
2414 * Assign the List values to the list items.
2415 */
2416 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002417 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 if (op != NULL && *op != '=')
2419 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2420 else
2421 {
2422 clear_tv(&lp->ll_li->li_tv);
2423 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2424 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002425 ri = ri->li_next;
2426 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2427 break;
2428 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002429 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002430 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002431 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002432 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002433 ri = NULL;
2434 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002435 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002436 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002437 lp->ll_li = lp->ll_li->li_next;
2438 ++lp->ll_n1;
2439 }
2440 if (ri != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002441 emsg(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 else if (lp->ll_empty2
2443 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002444 : lp->ll_n1 != lp->ll_n2)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002445 emsg(_("E711: List value has not enough items"));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002446 }
2447 else
2448 {
2449 /*
2450 * Assign to a List or Dictionary item.
2451 */
2452 if (lp->ll_newkey != NULL)
2453 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 if (op != NULL && *op != '=')
2455 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002456 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 return;
2458 }
2459
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002460 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002461 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002462 if (di == NULL)
2463 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002464 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2465 {
2466 vim_free(di);
2467 return;
2468 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002470 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002471 else if (op != NULL && *op != '=')
2472 {
2473 tv_op(lp->ll_tv, rettv, op);
2474 return;
2475 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002477 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002478
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002479 /*
2480 * Assign the value to the variable or list item.
2481 */
2482 if (copy)
2483 copy_tv(rettv, lp->ll_tv);
2484 else
2485 {
2486 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002487 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002488 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
2490 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002491}
2492
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002493/*
Bram Moolenaarff697e62019-02-12 22:28:33 +01002494 * Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
2495 * and "tv1 .= tv2"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 * Returns OK or FAIL.
2497 */
2498 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002499tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02002501 varnumber_T n;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 char_u numbuf[NUMBUFLEN];
2503 char_u *s;
2504
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002505 /* Can't do anything with a Funcref, Dict, v:true on the right. */
2506 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
2507 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 {
2509 switch (tv1->v_type)
2510 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01002511 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 case VAR_DICT:
2513 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01002514 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002515 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01002516 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01002517 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002518 break;
2519
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002520 case VAR_BLOB:
2521 if (*op != '+' || tv2->v_type != VAR_BLOB)
2522 break;
2523 // BLOB += BLOB
2524 if (tv1->vval.v_blob != NULL && tv2->vval.v_blob != NULL)
2525 {
2526 blob_T *b1 = tv1->vval.v_blob;
2527 blob_T *b2 = tv2->vval.v_blob;
2528 int i, len = blob_len(b2);
2529 for (i = 0; i < len; i++)
2530 ga_append(&b1->bv_ga, blob_get(b2, i));
2531 }
2532 return OK;
2533
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002534 case VAR_LIST:
2535 if (*op != '+' || tv2->v_type != VAR_LIST)
2536 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002537 // List += List
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002538 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
2539 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
2540 return OK;
2541
2542 case VAR_NUMBER:
2543 case VAR_STRING:
2544 if (tv2->v_type == VAR_LIST)
2545 break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002546 if (vim_strchr((char_u *)"+-*/%", *op) != NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002547 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002548 // nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002549 n = tv_get_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002550#ifdef FEAT_FLOAT
2551 if (tv2->v_type == VAR_FLOAT)
2552 {
2553 float_T f = n;
2554
Bram Moolenaarff697e62019-02-12 22:28:33 +01002555 if (*op == '%')
2556 break;
2557 switch (*op)
2558 {
2559 case '+': f += tv2->vval.v_float; break;
2560 case '-': f -= tv2->vval.v_float; break;
2561 case '*': f *= tv2->vval.v_float; break;
2562 case '/': f /= tv2->vval.v_float; break;
2563 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002564 clear_tv(tv1);
2565 tv1->v_type = VAR_FLOAT;
2566 tv1->vval.v_float = f;
2567 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002568 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002569#endif
2570 {
Bram Moolenaarff697e62019-02-12 22:28:33 +01002571 switch (*op)
2572 {
2573 case '+': n += tv_get_number(tv2); break;
2574 case '-': n -= tv_get_number(tv2); break;
2575 case '*': n *= tv_get_number(tv2); break;
Bram Moolenaare21c1582019-03-02 11:57:09 +01002576 case '/': n = num_divide(n, tv_get_number(tv2)); break;
2577 case '%': n = num_modulus(n, tv_get_number(tv2)); break;
Bram Moolenaarff697e62019-02-12 22:28:33 +01002578 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002579 clear_tv(tv1);
2580 tv1->v_type = VAR_NUMBER;
2581 tv1->vval.v_number = n;
2582 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002583 }
2584 else
2585 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002586 if (tv2->v_type == VAR_FLOAT)
2587 break;
2588
Bram Moolenaarff697e62019-02-12 22:28:33 +01002589 // str .= str
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002590 s = tv_get_string(tv1);
2591 s = concat_str(s, tv_get_string_buf(tv2, numbuf));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002592 clear_tv(tv1);
2593 tv1->v_type = VAR_STRING;
2594 tv1->vval.v_string = s;
2595 }
2596 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002598 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002599#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002600 {
2601 float_T f;
2602
Bram Moolenaarff697e62019-02-12 22:28:33 +01002603 if (*op == '%' || *op == '.'
2604 || (tv2->v_type != VAR_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002605 && tv2->v_type != VAR_NUMBER
2606 && tv2->v_type != VAR_STRING))
2607 break;
2608 if (tv2->v_type == VAR_FLOAT)
2609 f = tv2->vval.v_float;
2610 else
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002611 f = tv_get_number(tv2);
Bram Moolenaarff697e62019-02-12 22:28:33 +01002612 switch (*op)
2613 {
2614 case '+': tv1->vval.v_float += f; break;
2615 case '-': tv1->vval.v_float -= f; break;
2616 case '*': tv1->vval.v_float *= f; break;
2617 case '/': tv1->vval.v_float /= f; break;
2618 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002619 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00002620#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01002621 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002622 }
2623 }
2624
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002625 semsg(_(e_letwrong), op);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002626 return FAIL;
2627}
2628
2629/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002630 * Evaluate the expression used in a ":for var in expr" command.
2631 * "arg" points to "var".
2632 * Set "*errp" to TRUE for an error, FALSE otherwise;
2633 * Return a pointer that holds the info. Null when there is an error.
2634 */
2635 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002636eval_for_line(
2637 char_u *arg,
2638 int *errp,
2639 char_u **nextcmdp,
2640 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002641{
Bram Moolenaar33570922005-01-25 22:26:29 +00002642 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002643 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00002644 typval_T tv;
2645 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002646
2647 *errp = TRUE; /* default: there is an error */
2648
Bram Moolenaar33570922005-01-25 22:26:29 +00002649 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002650 if (fi == NULL)
2651 return NULL;
2652
2653 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
2654 if (expr == NULL)
2655 return fi;
2656
2657 expr = skipwhite(expr);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002658 if (expr[0] != 'i' || expr[1] != 'n' || !VIM_ISWHITE(expr[2]))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002659 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002660 emsg(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002661 return fi;
2662 }
2663
2664 if (skip)
2665 ++emsg_skip;
2666 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
2667 {
2668 *errp = FALSE;
2669 if (!skip)
2670 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002671 if (tv.v_type == VAR_LIST)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002672 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002673 l = tv.vval.v_list;
2674 if (l == NULL)
2675 {
2676 // a null list is like an empty list: do nothing
2677 clear_tv(&tv);
2678 }
2679 else
2680 {
2681 // No need to increment the refcount, it's already set for
2682 // the list being used in "tv".
2683 fi->fi_list = l;
2684 list_add_watch(l, &fi->fi_lw);
2685 fi->fi_lw.lw_item = l->lv_first;
2686 }
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002687 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002688 else if (tv.v_type == VAR_BLOB)
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002689 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002690 fi->fi_bi = 0;
2691 if (tv.vval.v_blob != NULL)
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002692 {
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002693 typval_T btv;
2694
2695 // Make a copy, so that the iteration still works when the
2696 // blob is changed.
2697 blob_copy(&tv, &btv);
2698 fi->fi_blob = btv.vval.v_blob;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002699 }
Bram Moolenaardd29ea12019-01-23 21:56:21 +01002700 clear_tv(&tv);
Bram Moolenaard8585ed2016-05-01 23:05:53 +02002701 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002702 else
2703 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002704 emsg(_(e_listreq));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002705 clear_tv(&tv);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002706 }
2707 }
2708 }
2709 if (skip)
2710 --emsg_skip;
2711
2712 return fi;
2713}
2714
2715/*
2716 * Use the first item in a ":for" list. Advance to the next.
2717 * Assign the values to the variable (list). "arg" points to the first one.
2718 * Return TRUE when a valid item was found, FALSE when at end of list or
2719 * something wrong.
2720 */
2721 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002722next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002723{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02002724 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002725 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00002726 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002727
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01002728 if (fi->fi_blob != NULL)
2729 {
2730 typval_T tv;
2731
2732 if (fi->fi_bi >= blob_len(fi->fi_blob))
2733 return FALSE;
2734 tv.v_type = VAR_NUMBER;
2735 tv.v_lock = VAR_FIXED;
2736 tv.vval.v_number = blob_get(fi->fi_blob, fi->fi_bi);
2737 ++fi->fi_bi;
2738 return ex_let_vars(arg, &tv, TRUE,
2739 fi->fi_semicolon, fi->fi_varcount, NULL) == OK;
2740 }
2741
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002742 item = fi->fi_lw.lw_item;
2743 if (item == NULL)
2744 result = FALSE;
2745 else
2746 {
2747 fi->fi_lw.lw_item = item->li_next;
2748 result = (ex_let_vars(arg, &item->li_tv, TRUE,
2749 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
2750 }
2751 return result;
2752}
2753
2754/*
2755 * Free the structure used to store info used by ":for".
2756 */
2757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002758free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002759{
Bram Moolenaar33570922005-01-25 22:26:29 +00002760 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002761
Bram Moolenaarab7013c2005-01-09 21:23:56 +00002762 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002763 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002764 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002765 list_unref(fi->fi_list);
2766 }
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01002767 if (fi != NULL && fi->fi_blob != NULL)
2768 blob_unref(fi->fi_blob);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002769 vim_free(fi);
2770}
2771
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2773
2774 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002775set_context_for_expression(
2776 expand_T *xp,
2777 char_u *arg,
2778 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779{
2780 int got_eq = FALSE;
2781 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002782 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002784 if (cmdidx == CMD_let)
2785 {
2786 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002787 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002788 {
2789 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002790 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002791 {
2792 xp->xp_pattern = p;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01002793 MB_PTR_BACK(arg, p);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002794 if (VIM_ISWHITE(*p))
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002795 break;
2796 }
2797 return;
2798 }
2799 }
2800 else
2801 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
2802 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002803 while ((xp->xp_pattern = vim_strpbrk(arg,
2804 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
2805 {
2806 c = *xp->xp_pattern;
2807 if (c == '&')
2808 {
2809 c = xp->xp_pattern[1];
2810 if (c == '&')
2811 {
2812 ++xp->xp_pattern;
2813 xp->xp_context = cmdidx != CMD_let || got_eq
2814 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
2815 }
2816 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002817 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002818 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00002819 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
2820 xp->xp_pattern += 2;
2821
2822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002823 }
2824 else if (c == '$')
2825 {
2826 /* environment variable */
2827 xp->xp_context = EXPAND_ENV_VARS;
2828 }
2829 else if (c == '=')
2830 {
2831 got_eq = TRUE;
2832 xp->xp_context = EXPAND_EXPRESSION;
2833 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02002834 else if (c == '#'
2835 && xp->xp_context == EXPAND_EXPRESSION)
2836 {
2837 /* Autoload function/variable contains '#'. */
2838 break;
2839 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002840 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 && xp->xp_context == EXPAND_FUNCTIONS
2842 && vim_strchr(xp->xp_pattern, '(') == NULL)
2843 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01002844 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 break;
2846 }
2847 else if (cmdidx != CMD_let || got_eq)
2848 {
2849 if (c == '"') /* string */
2850 {
2851 while ((c = *++xp->xp_pattern) != NUL && c != '"')
2852 if (c == '\\' && xp->xp_pattern[1] != NUL)
2853 ++xp->xp_pattern;
2854 xp->xp_context = EXPAND_NOTHING;
2855 }
2856 else if (c == '\'') /* literal string */
2857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
2860 /* skip */ ;
2861 xp->xp_context = EXPAND_NOTHING;
2862 }
2863 else if (c == '|')
2864 {
2865 if (xp->xp_pattern[1] == '|')
2866 {
2867 ++xp->xp_pattern;
2868 xp->xp_context = EXPAND_EXPRESSION;
2869 }
2870 else
2871 xp->xp_context = EXPAND_COMMANDS;
2872 }
2873 else
2874 xp->xp_context = EXPAND_EXPRESSION;
2875 }
2876 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002877 /* Doesn't look like something valid, expand as an expression
2878 * anyway. */
2879 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 arg = xp->xp_pattern;
2881 if (*arg != NUL)
2882 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
2883 /* skip */ ;
2884 }
2885 xp->xp_pattern = arg;
2886}
2887
2888#endif /* FEAT_CMDL_COMPL */
2889
2890/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891 * ":unlet[!] var1 ... " command.
2892 */
2893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002896 ex_unletlock(eap, eap->arg, 0);
2897}
2898
2899/*
2900 * ":lockvar" and ":unlockvar" commands
2901 */
2902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002903ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002904{
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002906 int deep = 2;
2907
2908 if (eap->forceit)
2909 deep = -1;
2910 else if (vim_isdigit(*arg))
2911 {
2912 deep = getdigits(&arg);
2913 arg = skipwhite(arg);
2914 }
2915
2916 ex_unletlock(eap, arg, deep);
2917}
2918
2919/*
2920 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
2921 */
2922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923ex_unletlock(
2924 exarg_T *eap,
2925 char_u *argstart,
2926 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927{
2928 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002931 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932
2933 do
2934 {
Bram Moolenaar137374f2018-05-13 15:59:50 +02002935 if (*arg == '$')
2936 {
2937 char_u *name = ++arg;
2938
2939 if (get_env_len(&arg) == 0)
2940 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002941 semsg(_(e_invarg2), name - 1);
Bram Moolenaar137374f2018-05-13 15:59:50 +02002942 return;
2943 }
2944 vim_unsetenv(name);
2945 arg = skipwhite(arg);
2946 continue;
2947 }
2948
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002950 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002951 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 if (lv.ll_name == NULL)
2953 error = TRUE; /* error but continue parsing */
Bram Moolenaar1c465442017-03-12 20:10:05 +01002954 if (name_end == NULL || (!VIM_ISWHITE(*name_end)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002957 if (name_end != NULL)
2958 {
2959 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002960 emsg(_(e_trailing));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002961 }
2962 if (!(eap->skip || error))
2963 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 break;
2965 }
2966
2967 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002968 {
2969 if (eap->cmdidx == CMD_unlet)
2970 {
2971 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
2972 error = TRUE;
2973 }
2974 else
2975 {
2976 if (do_lock_var(&lv, name_end, deep,
2977 eap->cmdidx == CMD_lockvar) == FAIL)
2978 error = TRUE;
2979 }
2980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 if (!eap->skip)
2983 clear_lval(&lv);
2984
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 arg = skipwhite(name_end);
2986 } while (!ends_excmd(*arg));
2987
2988 eap->nextcmd = check_nextcmd(arg);
2989}
2990
Bram Moolenaar8c711452005-01-14 21:53:12 +00002991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002992do_unlet_var(
2993 lval_T *lp,
2994 char_u *name_end,
2995 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002996{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002997 int ret = OK;
2998 int cc;
2999
3000 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003001 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 cc = *name_end;
3003 *name_end = NUL;
3004
3005 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003006 if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003009 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003010 else if ((lp->ll_list != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003011 && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003012 || (lp->ll_dict != NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003013 && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003014 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 else if (lp->ll_range)
3016 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003017 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003018 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003019 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003020
3021 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3022 {
3023 li = ll_li->li_next;
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003024 if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003025 return FAIL;
3026 ll_li = li;
3027 ++ll_n1;
3028 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029
3030 /* Delete a range of List items. */
3031 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3032 {
3033 li = lp->ll_li->li_next;
3034 listitem_remove(lp->ll_list, lp->ll_li);
3035 lp->ll_li = li;
3036 ++lp->ll_n1;
3037 }
3038 }
3039 else
3040 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003041 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /* unlet a List item. */
3043 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 }
3048
3049 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003050}
3051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052/*
3053 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003054 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 */
3056 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058{
Bram Moolenaar33570922005-01-25 22:26:29 +00003059 hashtab_T *ht;
3060 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003061 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003062 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003063 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064
Bram Moolenaar33570922005-01-25 22:26:29 +00003065 ht = find_var_ht(name, &varname);
3066 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068 d = get_current_funccal_dict(ht);
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003069 if (d == NULL)
3070 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003071 if (ht == &globvarht)
3072 d = &globvardict;
3073 else if (ht == &compat_hashtab)
3074 d = &vimvardict;
3075 else
3076 {
3077 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3078 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3079 }
3080 if (d == NULL)
3081 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01003082 internal_error("do_unlet()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 return FAIL;
3084 }
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003085 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003086 hi = hash_find(ht, varname);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003087 if (HASHITEM_EMPTY(hi))
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003088 hi = find_hi_in_scoped_ht(name, &ht);
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003089 if (hi != NULL && !HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003090 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003091 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003092 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003093 || var_check_ro(di->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01003094 || var_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003095 return FAIL;
3096
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003097 delete_var(ht, hi);
3098 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003101 if (forceit)
3102 return OK;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003103 semsg(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 return FAIL;
3105}
3106
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003107/*
3108 * Lock or unlock variable indicated by "lp".
3109 * "deep" is the levels to go (-1 for unlimited);
3110 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3111 */
3112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113do_lock_var(
3114 lval_T *lp,
3115 char_u *name_end,
3116 int deep,
3117 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003118{
3119 int ret = OK;
3120 int cc;
3121 dictitem_T *di;
3122
3123 if (deep == 0) /* nothing to do */
3124 return OK;
3125
3126 if (lp->ll_tv == NULL)
3127 {
3128 cc = *name_end;
3129 *name_end = NUL;
3130
3131 /* Normal name or expanded name. */
Bram Moolenaar79518e22017-02-17 16:31:35 +01003132 di = find_var(lp->ll_name, NULL, TRUE);
3133 if (di == NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003134 ret = FAIL;
Bram Moolenaare7877fe2017-02-20 22:35:33 +01003135 else if ((di->di_flags & DI_FLAGS_FIX)
3136 && di->di_tv.v_type != VAR_DICT
3137 && di->di_tv.v_type != VAR_LIST)
3138 /* For historic reasons this error is not given for a list or dict.
3139 * E.g., the b: dict could be locked/unlocked. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003140 semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003141 else
3142 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01003143 if (lock)
3144 di->di_flags |= DI_FLAGS_LOCK;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003145 else
Bram Moolenaar79518e22017-02-17 16:31:35 +01003146 di->di_flags &= ~DI_FLAGS_LOCK;
3147 item_lock(&di->di_tv, deep, lock);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003148 }
3149 *name_end = cc;
3150 }
3151 else if (lp->ll_range)
3152 {
3153 listitem_T *li = lp->ll_li;
3154
3155 /* (un)lock a range of List items. */
3156 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3157 {
3158 item_lock(&li->li_tv, deep, lock);
3159 li = li->li_next;
3160 ++lp->ll_n1;
3161 }
3162 }
3163 else if (lp->ll_list != NULL)
3164 /* (un)lock a List item. */
3165 item_lock(&lp->ll_li->li_tv, deep, lock);
3166 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003167 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003168 item_lock(&lp->ll_di->di_tv, deep, lock);
3169
3170 return ret;
3171}
3172
3173/*
3174 * Lock or unlock an item. "deep" is nr of levels to go.
3175 */
3176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003177item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003178{
3179 static int recurse = 0;
3180 list_T *l;
3181 listitem_T *li;
3182 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003183 blob_T *b;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003184 hashitem_T *hi;
3185 int todo;
3186
3187 if (recurse >= DICT_MAXNEST)
3188 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003189 emsg(_("E743: variable nested too deep for (un)lock"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003190 return;
3191 }
3192 if (deep == 0)
3193 return;
3194 ++recurse;
3195
3196 /* lock/unlock the item itself */
3197 if (lock)
3198 tv->v_lock |= VAR_LOCKED;
3199 else
3200 tv->v_lock &= ~VAR_LOCKED;
3201
3202 switch (tv->v_type)
3203 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003204 case VAR_UNKNOWN:
3205 case VAR_NUMBER:
3206 case VAR_STRING:
3207 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003208 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003209 case VAR_FLOAT:
3210 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003211 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003212 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003213 break;
3214
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003215 case VAR_BLOB:
3216 if ((b = tv->vval.v_blob) != NULL)
3217 {
3218 if (lock)
3219 b->bv_lock |= VAR_LOCKED;
3220 else
3221 b->bv_lock &= ~VAR_LOCKED;
3222 }
3223 break;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003224 case VAR_LIST:
3225 if ((l = tv->vval.v_list) != NULL)
3226 {
3227 if (lock)
3228 l->lv_lock |= VAR_LOCKED;
3229 else
3230 l->lv_lock &= ~VAR_LOCKED;
3231 if (deep < 0 || deep > 1)
3232 /* recursive: lock/unlock the items the List contains */
3233 for (li = l->lv_first; li != NULL; li = li->li_next)
3234 item_lock(&li->li_tv, deep - 1, lock);
3235 }
3236 break;
3237 case VAR_DICT:
3238 if ((d = tv->vval.v_dict) != NULL)
3239 {
3240 if (lock)
3241 d->dv_lock |= VAR_LOCKED;
3242 else
3243 d->dv_lock &= ~VAR_LOCKED;
3244 if (deep < 0 || deep > 1)
3245 {
3246 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003247 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003248 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3249 {
3250 if (!HASHITEM_EMPTY(hi))
3251 {
3252 --todo;
3253 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3254 }
3255 }
3256 }
3257 }
3258 }
3259 --recurse;
3260}
3261
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3263/*
3264 * Delete all "menutrans_" variables.
3265 */
3266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003267del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
Bram Moolenaar33570922005-01-25 22:26:29 +00003269 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003270 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271
Bram Moolenaar33570922005-01-25 22:26:29 +00003272 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003273 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003274 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003275 {
3276 if (!HASHITEM_EMPTY(hi))
3277 {
3278 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003279 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3280 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003281 }
3282 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003283 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285#endif
3286
3287#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3288
3289/*
3290 * Local string buffer for the next two functions to store a variable name
3291 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3292 * get_user_var_name().
3293 */
3294
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295static char_u *varnamebuf = NULL;
3296static int varnamebuflen = 0;
3297
3298/*
3299 * Function to concatenate a prefix and a variable name.
3300 */
3301 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304 int len;
3305
3306 len = (int)STRLEN(name) + 3;
3307 if (len > varnamebuflen)
3308 {
3309 vim_free(varnamebuf);
3310 len += 10; /* some additional space */
3311 varnamebuf = alloc(len);
3312 if (varnamebuf == NULL)
3313 {
3314 varnamebuflen = 0;
3315 return NULL;
3316 }
3317 varnamebuflen = len;
3318 }
3319 *varnamebuf = prefix;
3320 varnamebuf[1] = ':';
3321 STRCPY(varnamebuf + 2, name);
3322 return varnamebuf;
3323}
3324
3325/*
3326 * Function given to ExpandGeneric() to obtain the list of user defined
3327 * (global/buffer/window/built-in) variable names.
3328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003330get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003332 static long_u gdone;
3333 static long_u bdone;
3334 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003335 static long_u tdone;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003336 static int vidx;
3337 static hashitem_T *hi;
3338 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339
3340 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003341 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003342 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003343 tdone = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003344 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003345
3346 /* Global variables */
3347 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003349 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003350 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003351 else
3352 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003353 while (HASHITEM_EMPTY(hi))
3354 ++hi;
3355 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3356 return cat_prefix_varname('g', hi->hi_key);
3357 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003359
3360 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003361 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003362 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003364 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003365 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003366 else
3367 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003368 while (HASHITEM_EMPTY(hi))
3369 ++hi;
3370 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003372
3373 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003374 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003375 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003377 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003378 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003379 else
3380 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003381 while (HASHITEM_EMPTY(hi))
3382 ++hi;
3383 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003385
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003386 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003387 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003388 if (tdone < ht->ht_used)
3389 {
3390 if (tdone++ == 0)
3391 hi = ht->ht_array;
3392 else
3393 ++hi;
3394 while (HASHITEM_EMPTY(hi))
3395 ++hi;
3396 return cat_prefix_varname('t', hi->hi_key);
3397 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003398
Bram Moolenaar33570922005-01-25 22:26:29 +00003399 /* v: variables */
3400 if (vidx < VV_LEN)
3401 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402
Bram Moolenaard23a8232018-02-10 18:45:26 +01003403 VIM_CLEAR(varnamebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 varnamebuflen = 0;
3405 return NULL;
3406}
3407
3408#endif /* FEAT_CMDL_COMPL */
3409
3410/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02003411 * Return TRUE if "pat" matches "text".
3412 * Does not use 'cpo' and always uses 'magic'.
3413 */
3414 static int
3415pattern_match(char_u *pat, char_u *text, int ic)
3416{
3417 int matches = FALSE;
3418 char_u *save_cpo;
3419 regmatch_T regmatch;
3420
3421 /* avoid 'l' flag in 'cpoptions' */
3422 save_cpo = p_cpo;
3423 p_cpo = (char_u *)"";
3424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
3425 if (regmatch.regprog != NULL)
3426 {
3427 regmatch.rm_ic = ic;
3428 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
3429 vim_regfree(regmatch.regprog);
3430 }
3431 p_cpo = save_cpo;
3432 return matches;
3433}
3434
3435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003437 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
3439 */
3440
3441/*
3442 * Handle zero level expression.
3443 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003444 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00003445 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 * Return OK or FAIL.
3447 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003449eval0(
3450 char_u *arg,
3451 typval_T *rettv,
3452 char_u **nextcmd,
3453 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454{
3455 int ret;
3456 char_u *p;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003457 int did_emsg_before = did_emsg;
3458 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
3460 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003461 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 if (ret == FAIL || !ends_excmd(*p))
3463 {
3464 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003465 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 /*
3467 * Report the invalid expression unless the expression evaluation has
3468 * been cancelled due to an aborting error, an interrupt, or an
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003469 * exception, or we already gave a more specific error.
3470 * Also check called_emsg for when using assert_fails().
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01003472 if (!aborting() && did_emsg == did_emsg_before
3473 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003474 semsg(_(e_invexpr2), arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 ret = FAIL;
3476 }
3477 if (nextcmd != NULL)
3478 *nextcmd = check_nextcmd(p);
3479
3480 return ret;
3481}
3482
3483/*
3484 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00003485 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 *
3487 * "arg" must point to the first non-white of the expression.
3488 * "arg" is advanced to the next non-white after the recognized expression.
3489 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00003490 * Note: "rettv.v_lock" is not set.
3491 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 * Return OK or FAIL.
3493 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02003494 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003495eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496{
3497 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003498 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500 /*
3501 * Get the first variable.
3502 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 return FAIL;
3505
3506 if ((*arg)[0] == '?')
3507 {
3508 result = FALSE;
3509 if (evaluate)
3510 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003511 int error = FALSE;
3512
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003513 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003515 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003516 if (error)
3517 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 }
3519
3520 /*
3521 * Get the second variable.
3522 */
3523 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003524 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 return FAIL;
3526
3527 /*
3528 * Check for the ":".
3529 */
3530 if ((*arg)[0] != ':')
3531 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003532 emsg(_("E109: Missing ':' after '?'"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003534 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 return FAIL;
3536 }
3537
3538 /*
3539 * Get the third variable.
3540 */
3541 *arg = skipwhite(*arg + 1);
3542 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
3543 {
3544 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003545 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 return FAIL;
3547 }
3548 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003549 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 }
3551
3552 return OK;
3553}
3554
3555/*
3556 * Handle first level expression:
3557 * expr2 || expr2 || expr2 logical OR
3558 *
3559 * "arg" must point to the first non-white of the expression.
3560 * "arg" is advanced to the next non-white after the recognized expression.
3561 *
3562 * Return OK or FAIL.
3563 */
3564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003565eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566{
Bram Moolenaar33570922005-01-25 22:26:29 +00003567 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 long result;
3569 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003570 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571
3572 /*
3573 * Get the first variable.
3574 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003575 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 return FAIL;
3577
3578 /*
3579 * Repeat until there is no following "||".
3580 */
3581 first = TRUE;
3582 result = FALSE;
3583 while ((*arg)[0] == '|' && (*arg)[1] == '|')
3584 {
3585 if (evaluate && first)
3586 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003587 if (tv_get_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003589 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003590 if (error)
3591 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 first = FALSE;
3593 }
3594
3595 /*
3596 * Get the second variable.
3597 */
3598 *arg = skipwhite(*arg + 2);
3599 if (eval3(arg, &var2, evaluate && !result) == FAIL)
3600 return FAIL;
3601
3602 /*
3603 * Compute the result.
3604 */
3605 if (evaluate && !result)
3606 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003607 if (tv_get_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003609 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003610 if (error)
3611 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 if (evaluate)
3614 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003615 rettv->v_type = VAR_NUMBER;
3616 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 }
3619
3620 return OK;
3621}
3622
3623/*
3624 * Handle second level expression:
3625 * expr3 && expr3 && expr3 logical AND
3626 *
3627 * "arg" must point to the first non-white of the expression.
3628 * "arg" is advanced to the next non-white after the recognized expression.
3629 *
3630 * Return OK or FAIL.
3631 */
3632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003633eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634{
Bram Moolenaar33570922005-01-25 22:26:29 +00003635 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 long result;
3637 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003638 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 /*
3641 * Get the first variable.
3642 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003643 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 return FAIL;
3645
3646 /*
3647 * Repeat until there is no following "&&".
3648 */
3649 first = TRUE;
3650 result = TRUE;
3651 while ((*arg)[0] == '&' && (*arg)[1] == '&')
3652 {
3653 if (evaluate && first)
3654 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003655 if (tv_get_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003657 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003658 if (error)
3659 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 first = FALSE;
3661 }
3662
3663 /*
3664 * Get the second variable.
3665 */
3666 *arg = skipwhite(*arg + 2);
3667 if (eval4(arg, &var2, evaluate && result) == FAIL)
3668 return FAIL;
3669
3670 /*
3671 * Compute the result.
3672 */
3673 if (evaluate && result)
3674 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003675 if (tv_get_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003677 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003678 if (error)
3679 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 }
3681 if (evaluate)
3682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003683 rettv->v_type = VAR_NUMBER;
3684 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686 }
3687
3688 return OK;
3689}
3690
3691/*
3692 * Handle third level expression:
3693 * var1 == var2
3694 * var1 =~ var2
3695 * var1 != var2
3696 * var1 !~ var2
3697 * var1 > var2
3698 * var1 >= var2
3699 * var1 < var2
3700 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003701 * var1 is var2
3702 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 *
3704 * "arg" must point to the first non-white of the expression.
3705 * "arg" is advanced to the next non-white after the recognized expression.
3706 *
3707 * Return OK or FAIL.
3708 */
3709 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003710eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711{
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 char_u *p;
3714 int i;
3715 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003716 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 int len = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719
3720 /*
3721 * Get the first variable.
3722 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003723 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 return FAIL;
3725
3726 p = *arg;
3727 switch (p[0])
3728 {
3729 case '=': if (p[1] == '=')
3730 type = TYPE_EQUAL;
3731 else if (p[1] == '~')
3732 type = TYPE_MATCH;
3733 break;
3734 case '!': if (p[1] == '=')
3735 type = TYPE_NEQUAL;
3736 else if (p[1] == '~')
3737 type = TYPE_NOMATCH;
3738 break;
3739 case '>': if (p[1] != '=')
3740 {
3741 type = TYPE_GREATER;
3742 len = 1;
3743 }
3744 else
3745 type = TYPE_GEQUAL;
3746 break;
3747 case '<': if (p[1] != '=')
3748 {
3749 type = TYPE_SMALLER;
3750 len = 1;
3751 }
3752 else
3753 type = TYPE_SEQUAL;
3754 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003755 case 'i': if (p[1] == 's')
3756 {
3757 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
3758 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02003759 i = p[len];
3760 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003761 {
3762 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
3763 type_is = TRUE;
3764 }
3765 }
3766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 }
3768
3769 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003770 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 */
3772 if (type != TYPE_UNKNOWN)
3773 {
3774 /* extra question mark appended: ignore case */
3775 if (p[len] == '?')
3776 {
3777 ic = TRUE;
3778 ++len;
3779 }
3780 /* extra '#' appended: match case */
3781 else if (p[len] == '#')
3782 {
3783 ic = FALSE;
3784 ++len;
3785 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003786 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 else
3788 ic = p_ic;
3789
3790 /*
3791 * Get the second variable.
3792 */
3793 *arg = skipwhite(p + len);
3794 if (eval5(arg, &var2, evaluate) == FAIL)
3795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003796 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 return FAIL;
3798 }
Bram Moolenaar31988702018-02-13 12:57:42 +01003799 if (evaluate)
3800 {
3801 int ret = typval_compare(rettv, &var2, type, type_is, ic);
3802
3803 clear_tv(&var2);
3804 return ret;
3805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 }
3807
3808 return OK;
3809}
3810
3811/*
3812 * Handle fourth level expression:
3813 * + number addition
3814 * - number subtraction
3815 * . string concatenation
3816 *
3817 * "arg" must point to the first non-white of the expression.
3818 * "arg" is advanced to the next non-white after the recognized expression.
3819 *
3820 * Return OK or FAIL.
3821 */
3822 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003823eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824{
Bram Moolenaar33570922005-01-25 22:26:29 +00003825 typval_T var2;
3826 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02003828 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003829#ifdef FEAT_FLOAT
3830 float_T f1 = 0, f2 = 0;
3831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 char_u *s1, *s2;
3833 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
3834 char_u *p;
3835
3836 /*
3837 * Get the first variable.
3838 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003839 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 return FAIL;
3841
3842 /*
3843 * Repeat computing, until no '+', '-' or '.' is following.
3844 */
3845 for (;;)
3846 {
3847 op = **arg;
3848 if (op != '+' && op != '-' && op != '.')
3849 break;
3850
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003851 if ((op != '+' || (rettv->v_type != VAR_LIST
3852 && rettv->v_type != VAR_BLOB))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003853#ifdef FEAT_FLOAT
3854 && (op == '.' || rettv->v_type != VAR_FLOAT)
3855#endif
3856 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003857 {
3858 /* For "list + ...", an illegal use of the first operand as
3859 * a number cannot be determined before evaluating the 2nd
3860 * operand: if this is also a list, all is ok.
3861 * For "something . ...", "something - ..." or "non-list + ...",
3862 * we know that the first operand needs to be a string or number
3863 * without evaluating the 2nd operand. So check before to avoid
3864 * side effects after an error. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003865 if (evaluate && tv_get_string_chk(rettv) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003866 {
3867 clear_tv(rettv);
3868 return FAIL;
3869 }
3870 }
3871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 /*
3873 * Get the second variable.
3874 */
3875 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00003876 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003878 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 return FAIL;
3880 }
3881
3882 if (evaluate)
3883 {
3884 /*
3885 * Compute the result.
3886 */
3887 if (op == '.')
3888 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003889 s1 = tv_get_string_buf(rettv, buf1); /* already checked */
3890 s2 = tv_get_string_buf_chk(&var2, buf2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003891 if (s2 == NULL) /* type error ? */
3892 {
3893 clear_tv(rettv);
3894 clear_tv(&var2);
3895 return FAIL;
3896 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003897 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003898 clear_tv(rettv);
3899 rettv->v_type = VAR_STRING;
3900 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01003902 else if (op == '+' && rettv->v_type == VAR_BLOB
3903 && var2.v_type == VAR_BLOB)
3904 {
3905 blob_T *b1 = rettv->vval.v_blob;
3906 blob_T *b2 = var2.vval.v_blob;
3907 blob_T *b = blob_alloc();
3908 int i;
3909
3910 if (b != NULL)
3911 {
3912 for (i = 0; i < blob_len(b1); i++)
3913 ga_append(&b->bv_ga, blob_get(b1, i));
3914 for (i = 0; i < blob_len(b2); i++)
3915 ga_append(&b->bv_ga, blob_get(b2, i));
3916
3917 clear_tv(rettv);
3918 rettv_blob_set(rettv, b);
3919 }
3920 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00003921 else if (op == '+' && rettv->v_type == VAR_LIST
3922 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00003923 {
3924 /* concatenate Lists */
3925 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
3926 &var3) == FAIL)
3927 {
3928 clear_tv(rettv);
3929 clear_tv(&var2);
3930 return FAIL;
3931 }
3932 clear_tv(rettv);
3933 *rettv = var3;
3934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 else
3936 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003937 int error = FALSE;
3938
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003939#ifdef FEAT_FLOAT
3940 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003941 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003942 f1 = rettv->vval.v_float;
3943 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003944 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003945 else
3946#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003947 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003948 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003949 if (error)
3950 {
3951 /* This can only happen for "list + non-list". For
3952 * "non-list + ..." or "something - ...", we returned
3953 * before evaluating the 2nd operand. */
3954 clear_tv(rettv);
3955 return FAIL;
3956 }
3957#ifdef FEAT_FLOAT
3958 if (var2.v_type == VAR_FLOAT)
3959 f1 = n1;
3960#endif
3961 }
3962#ifdef FEAT_FLOAT
3963 if (var2.v_type == VAR_FLOAT)
3964 {
3965 f2 = var2.vval.v_float;
3966 n2 = 0;
3967 }
3968 else
3969#endif
3970 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003971 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003972 if (error)
3973 {
3974 clear_tv(rettv);
3975 clear_tv(&var2);
3976 return FAIL;
3977 }
3978#ifdef FEAT_FLOAT
3979 if (rettv->v_type == VAR_FLOAT)
3980 f2 = n2;
3981#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00003982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003983 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003984
3985#ifdef FEAT_FLOAT
3986 /* If there is a float on either side the result is a float. */
3987 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
3988 {
3989 if (op == '+')
3990 f1 = f1 + f2;
3991 else
3992 f1 = f1 - f2;
3993 rettv->v_type = VAR_FLOAT;
3994 rettv->vval.v_float = f1;
3995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003997#endif
3998 {
3999 if (op == '+')
4000 n1 = n1 + n2;
4001 else
4002 n1 = n1 - n2;
4003 rettv->v_type = VAR_NUMBER;
4004 rettv->vval.v_number = n1;
4005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004007 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 }
4009 }
4010 return OK;
4011}
4012
4013/*
4014 * Handle fifth level expression:
4015 * * number multiplication
4016 * / number division
4017 * % number modulo
4018 *
4019 * "arg" must point to the first non-white of the expression.
4020 * "arg" is advanced to the next non-white after the recognized expression.
4021 *
4022 * Return OK or FAIL.
4023 */
4024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004025eval6(
4026 char_u **arg,
4027 typval_T *rettv,
4028 int evaluate,
4029 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
Bram Moolenaar33570922005-01-25 22:26:29 +00004031 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 int op;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004033 varnumber_T n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004034#ifdef FEAT_FLOAT
4035 int use_float = FALSE;
4036 float_T f1 = 0, f2;
4037#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004038 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039
4040 /*
4041 * Get the first variable.
4042 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004043 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 return FAIL;
4045
4046 /*
4047 * Repeat computing, until no '*', '/' or '%' is following.
4048 */
4049 for (;;)
4050 {
4051 op = **arg;
4052 if (op != '*' && op != '/' && op != '%')
4053 break;
4054
4055 if (evaluate)
4056 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004057#ifdef FEAT_FLOAT
4058 if (rettv->v_type == VAR_FLOAT)
4059 {
4060 f1 = rettv->vval.v_float;
4061 use_float = TRUE;
4062 n1 = 0;
4063 }
4064 else
4065#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004066 n1 = tv_get_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004067 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004068 if (error)
4069 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 }
4071 else
4072 n1 = 0;
4073
4074 /*
4075 * Get the second variable.
4076 */
4077 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004078 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 return FAIL;
4080
4081 if (evaluate)
4082 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004083#ifdef FEAT_FLOAT
4084 if (var2.v_type == VAR_FLOAT)
4085 {
4086 if (!use_float)
4087 {
4088 f1 = n1;
4089 use_float = TRUE;
4090 }
4091 f2 = var2.vval.v_float;
4092 n2 = 0;
4093 }
4094 else
4095#endif
4096 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004097 n2 = tv_get_number_chk(&var2, &error);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004098 clear_tv(&var2);
4099 if (error)
4100 return FAIL;
4101#ifdef FEAT_FLOAT
4102 if (use_float)
4103 f2 = n2;
4104#endif
4105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 /*
4108 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004109 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004111#ifdef FEAT_FLOAT
4112 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004114 if (op == '*')
4115 f1 = f1 * f2;
4116 else if (op == '/')
4117 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004118# ifdef VMS
4119 /* VMS crashes on divide by zero, work around it */
4120 if (f2 == 0.0)
4121 {
4122 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004123 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004124 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004125 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004126 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004127 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004128 }
4129 else
4130 f1 = f1 / f2;
4131# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004132 /* We rely on the floating point library to handle divide
4133 * by zero to result in "inf" and not a crash. */
4134 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004135# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004138 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004139 emsg(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004140 return FAIL;
4141 }
4142 rettv->v_type = VAR_FLOAT;
4143 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004146#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004148 if (op == '*')
4149 n1 = n1 * n2;
4150 else if (op == '/')
Bram Moolenaare21c1582019-03-02 11:57:09 +01004151 n1 = num_divide(n1, n2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 else
Bram Moolenaare21c1582019-03-02 11:57:09 +01004153 n1 = num_modulus(n1, n2);
4154
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004155 rettv->v_type = VAR_NUMBER;
4156 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
4159 }
4160
4161 return OK;
4162}
4163
4164/*
4165 * Handle sixth level expression:
4166 * number number constant
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004167 * 0zFFFFFFFF Blob constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004168 * "string" string constant
4169 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 * &option-name option value
4171 * @r register contents
4172 * identifier variable value
4173 * function() function call
4174 * $VAR environment variable
4175 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004176 * [expr, expr] List
4177 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 *
4179 * Also handle:
4180 * ! in front logical NOT
4181 * - in front unary minus
4182 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004183 * trailing [] subscript in String or List
4184 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 *
4186 * "arg" must point to the first non-white of the expression.
4187 * "arg" is advanced to the next non-white after the recognized expression.
4188 *
4189 * Return OK or FAIL.
4190 */
4191 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004192eval7(
4193 char_u **arg,
4194 typval_T *rettv,
4195 int evaluate,
4196 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004198 varnumber_T n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 int len;
4200 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 char_u *start_leader, *end_leader;
4202 int ret = OK;
4203 char_u *alias;
4204
4205 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004206 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004207 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
4211 /*
Bram Moolenaaredf3f972016-08-29 22:49:24 +02004212 * Skip '!', '-' and '+' characters. They are handled later.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 */
4214 start_leader = *arg;
4215 while (**arg == '!' || **arg == '-' || **arg == '+')
4216 *arg = skipwhite(*arg + 1);
4217 end_leader = *arg;
4218
4219 switch (**arg)
4220 {
4221 /*
4222 * Number constant.
4223 */
4224 case '0':
4225 case '1':
4226 case '2':
4227 case '3':
4228 case '4':
4229 case '5':
4230 case '6':
4231 case '7':
4232 case '8':
4233 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004234 {
4235#ifdef FEAT_FLOAT
4236 char_u *p = skipdigits(*arg + 1);
4237 int get_float = FALSE;
4238
4239 /* We accept a float when the format matches
4240 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004241 * strict to avoid backwards compatibility problems.
4242 * Don't look for a float after the "." operator, so that
4243 * ":let vers = 1.2.3" doesn't fail. */
4244 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004246 get_float = TRUE;
4247 p = skipdigits(p + 2);
4248 if (*p == 'e' || *p == 'E')
4249 {
4250 ++p;
4251 if (*p == '-' || *p == '+')
4252 ++p;
4253 if (!vim_isdigit(*p))
4254 get_float = FALSE;
4255 else
4256 p = skipdigits(p + 1);
4257 }
4258 if (ASCII_ISALPHA(*p) || *p == '.')
4259 get_float = FALSE;
4260 }
4261 if (get_float)
4262 {
4263 float_T f;
4264
4265 *arg += string2float(*arg, &f);
4266 if (evaluate)
4267 {
4268 rettv->v_type = VAR_FLOAT;
4269 rettv->vval.v_float = f;
4270 }
4271 }
4272 else
4273#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004274 if (**arg == '0' && ((*arg)[1] == 'z' || (*arg)[1] == 'Z'))
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004275 {
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004276 char_u *bp;
Bram Moolenaare519dfd2019-01-13 15:42:02 +01004277 blob_T *blob = NULL; // init for gcc
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004278
4279 // Blob constant: 0z0123456789abcdef
4280 if (evaluate)
4281 blob = blob_alloc();
4282 for (bp = *arg + 2; vim_isxdigit(bp[0]); bp += 2)
4283 {
4284 if (!vim_isxdigit(bp[1]))
4285 {
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004286 if (blob != NULL)
4287 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004288 emsg(_("E973: Blob literal should have an even number of hex characters"));
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004289 ga_clear(&blob->bv_ga);
4290 VIM_CLEAR(blob);
4291 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004292 ret = FAIL;
4293 break;
4294 }
4295 if (blob != NULL)
4296 ga_append(&blob->bv_ga,
4297 (hex2nr(*bp) << 4) + hex2nr(*(bp+1)));
Bram Moolenaar4131fd52019-01-17 16:32:53 +01004298 if (bp[2] == '.' && vim_isxdigit(bp[3]))
4299 ++bp;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004300 }
4301 if (blob != NULL)
Bram Moolenaarecc8bc42019-01-13 16:07:21 +01004302 rettv_blob_set(rettv, blob);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004303 *arg = bp;
4304 }
4305 else
4306 {
4307 // decimal, hex or octal number
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01004308 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004309 *arg += len;
4310 if (evaluate)
4311 {
4312 rettv->v_type = VAR_NUMBER;
4313 rettv->vval.v_number = n;
4314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 /*
4320 * String constant: "string".
4321 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 break;
4324
4325 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00004326 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004329 break;
4330
4331 /*
4332 * List: [expr, expr]
4333 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004334 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 break;
4336
4337 /*
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004338 * Lambda: {arg, arg -> expr}
Bram Moolenaar8c711452005-01-14 21:53:12 +00004339 * Dictionary: {key: val, key: val}
4340 */
Bram Moolenaar069c1e72016-07-15 21:25:08 +02004341 case '{': ret = get_lambda_tv(arg, rettv, evaluate);
4342 if (ret == NOTDONE)
Bram Moolenaar8f667172018-12-14 15:38:31 +01004343 ret = dict_get_tv(arg, rettv, evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004344 break;
4345
4346 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00004347 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00004349 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 break;
4351
4352 /*
4353 * Environment variable: $VAR.
4354 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004355 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 break;
4357
4358 /*
4359 * Register contents: @r.
4360 */
4361 case '@': ++*arg;
4362 if (evaluate)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02004365 rettv->vval.v_string = get_reg_contents(**arg,
4366 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 if (**arg != NUL)
4369 ++*arg;
4370 break;
4371
4372 /*
4373 * nested expression: (expression).
4374 */
4375 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004376 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 if (**arg == ')')
4378 ++*arg;
4379 else if (ret == OK)
4380 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004381 emsg(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004382 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 ret = FAIL;
4384 }
4385 break;
4386
Bram Moolenaar8c711452005-01-14 21:53:12 +00004387 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 break;
4389 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004390
4391 if (ret == NOTDONE)
4392 {
4393 /*
4394 * Must be a variable or function name.
4395 * Can also be a curly-braces kind of name: {expr}.
4396 */
4397 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004398 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004399 if (alias != NULL)
4400 s = alias;
4401
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004402 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004403 ret = FAIL;
4404 else
4405 {
4406 if (**arg == '(') /* recursive! */
4407 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004408 partial_T *partial;
4409
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004410 if (!evaluate)
4411 check_vars(s, len);
4412
Bram Moolenaar8c711452005-01-14 21:53:12 +00004413 /* If "s" is the name of a variable of type VAR_FUNC
4414 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004415 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004416
Bram Moolenaar8a01f962016-11-14 21:50:00 +01004417 /* Need to make a copy, in case evaluating the arguments makes
4418 * the name invalid. */
4419 s = vim_strsave(s);
4420 if (s == NULL)
4421 ret = FAIL;
4422 else
4423 /* Invoke the function. */
4424 ret = get_func_tv(s, len, rettv, arg,
4425 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
4426 &len, evaluate, partial, NULL);
4427 vim_free(s);
Bram Moolenaare17c2602013-02-26 19:36:15 +01004428
4429 /* If evaluate is FALSE rettv->v_type was not set in
4430 * get_func_tv, but it's needed in handle_subscript() to parse
4431 * what follows. So set it here. */
4432 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
4433 {
Bram Moolenaar9ad73232016-06-01 22:08:17 +02004434 rettv->vval.v_string = NULL;
Bram Moolenaare17c2602013-02-26 19:36:15 +01004435 rettv->v_type = VAR_FUNC;
4436 }
4437
Bram Moolenaar8c711452005-01-14 21:53:12 +00004438 /* Stop the expression evaluation when immediately
4439 * aborting on error, or when an interrupt occurred or
4440 * an exception was thrown but not caught. */
4441 if (aborting())
4442 {
4443 if (ret == OK)
4444 clear_tv(rettv);
4445 ret = FAIL;
4446 }
4447 }
4448 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02004449 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004450 else
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004451 {
4452 check_vars(s, len);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004453 ret = OK;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02004454 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004455 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01004456 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004457 }
4458
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 *arg = skipwhite(*arg);
4460
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004461 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
4462 * expr(expr). */
4463 if (ret == OK)
4464 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465
4466 /*
4467 * Apply logical NOT and unary '-', from right to left, ignore '+'.
4468 */
4469 if (ret == OK && evaluate && end_leader > start_leader)
4470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004471 int error = FALSE;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02004472 varnumber_T val = 0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004473#ifdef FEAT_FLOAT
4474 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004475
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004476 if (rettv->v_type == VAR_FLOAT)
4477 f = rettv->vval.v_float;
4478 else
4479#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004480 val = tv_get_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004481 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004483 clear_tv(rettv);
4484 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004486 else
4487 {
4488 while (end_leader > start_leader)
4489 {
4490 --end_leader;
4491 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004492 {
4493#ifdef FEAT_FLOAT
4494 if (rettv->v_type == VAR_FLOAT)
4495 f = !f;
4496 else
4497#endif
4498 val = !val;
4499 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004500 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004501 {
4502#ifdef FEAT_FLOAT
4503 if (rettv->v_type == VAR_FLOAT)
4504 f = -f;
4505 else
4506#endif
4507 val = -val;
4508 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004509 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004510#ifdef FEAT_FLOAT
4511 if (rettv->v_type == VAR_FLOAT)
4512 {
4513 clear_tv(rettv);
4514 rettv->vval.v_float = f;
4515 }
4516 else
4517#endif
4518 {
4519 clear_tv(rettv);
4520 rettv->v_type = VAR_NUMBER;
4521 rettv->vval.v_number = val;
4522 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 }
4525
4526 return ret;
4527}
4528
4529/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004530 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
4531 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004532 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
4533 */
4534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004535eval_index(
4536 char_u **arg,
4537 typval_T *rettv,
4538 int evaluate,
4539 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004540{
4541 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00004542 typval_T var1, var2;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004543 long i;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004544 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004545 long len = -1;
4546 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004547 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004548 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004549
Bram Moolenaara03f2332016-02-06 18:09:59 +01004550 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004551 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01004552 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004553 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004554 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004555 emsg(_("E695: Cannot index a Funcref"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004556 return FAIL;
4557 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004558#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01004559 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004560 emsg(_(e_float_as_string));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004561 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02004562#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01004563 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004564 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004565 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004566 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004567 emsg(_("E909: Cannot index a special variable"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01004568 return FAIL;
4569 case VAR_UNKNOWN:
4570 if (evaluate)
4571 return FAIL;
4572 /* FALLTHROUGH */
4573
4574 case VAR_STRING:
4575 case VAR_NUMBER:
4576 case VAR_LIST:
4577 case VAR_DICT:
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004578 case VAR_BLOB:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004579 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01004580 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004581
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02004582 init_tv(&var1);
4583 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004584 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004585 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004586 /*
4587 * dict.name
4588 */
4589 key = *arg + 1;
4590 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
4591 ;
4592 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004593 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004594 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004595 }
4596 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004597 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004598 /*
4599 * something[idx]
4600 *
4601 * Get the (first) variable from inside the [].
4602 */
4603 *arg = skipwhite(*arg + 1);
4604 if (**arg == ':')
4605 empty1 = TRUE;
4606 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
4607 return FAIL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004608 else if (evaluate && tv_get_string_chk(&var1) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004609 {
4610 /* not a number or string */
4611 clear_tv(&var1);
4612 return FAIL;
4613 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00004614
4615 /*
4616 * Get the second variable from inside the [:].
4617 */
4618 if (**arg == ':')
4619 {
4620 range = TRUE;
4621 *arg = skipwhite(*arg + 1);
4622 if (**arg == ']')
4623 empty2 = TRUE;
4624 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
4625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004626 if (!empty1)
4627 clear_tv(&var1);
4628 return FAIL;
4629 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004630 else if (evaluate && tv_get_string_chk(&var2) == NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004631 {
4632 /* not a number or string */
4633 if (!empty1)
4634 clear_tv(&var1);
4635 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004636 return FAIL;
4637 }
4638 }
4639
4640 /* Check for the ']'. */
4641 if (**arg != ']')
4642 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004643 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004644 emsg(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004645 clear_tv(&var1);
4646 if (range)
4647 clear_tv(&var2);
4648 return FAIL;
4649 }
4650 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004651 }
4652
4653 if (evaluate)
4654 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004655 n1 = 0;
4656 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004657 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004658 n1 = tv_get_number(&var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004660 }
4661 if (range)
4662 {
4663 if (empty2)
4664 n2 = -1;
4665 else
4666 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004667 n2 = tv_get_number(&var2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004669 }
4670 }
4671
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004672 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004673 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01004674 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004675 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004676 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004677 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01004678 case VAR_SPECIAL:
4679 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01004680 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01004681 break; /* not evaluating, skipping over subscript */
4682
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004683 case VAR_NUMBER:
4684 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004685 s = tv_get_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004686 len = (long)STRLEN(s);
4687 if (range)
4688 {
4689 /* The resulting variable is a substring. If the indexes
4690 * are out of range the result is empty. */
4691 if (n1 < 0)
4692 {
4693 n1 = len + n1;
4694 if (n1 < 0)
4695 n1 = 0;
4696 }
4697 if (n2 < 0)
4698 n2 = len + n2;
4699 else if (n2 >= len)
4700 n2 = len;
4701 if (n1 >= len || n2 < 0 || n1 > n2)
4702 s = NULL;
4703 else
4704 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
4705 }
4706 else
4707 {
4708 /* The resulting variable is a string of a single
4709 * character. If the index is too big or negative the
4710 * result is empty. */
4711 if (n1 >= len || n1 < 0)
4712 s = NULL;
4713 else
4714 s = vim_strnsave(s + n1, 1);
4715 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004716 clear_tv(rettv);
4717 rettv->v_type = VAR_STRING;
4718 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004719 break;
4720
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004721 case VAR_BLOB:
4722 len = blob_len(rettv->vval.v_blob);
4723 if (range)
4724 {
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01004725 // The resulting variable is a sub-blob. If the indexes
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004726 // are out of range the result is empty.
4727 if (n1 < 0)
4728 {
4729 n1 = len + n1;
4730 if (n1 < 0)
4731 n1 = 0;
4732 }
4733 if (n2 < 0)
4734 n2 = len + n2;
4735 else if (n2 >= len)
4736 n2 = len - 1;
4737 if (n1 >= len || n2 < 0 || n1 > n2)
4738 {
4739 clear_tv(rettv);
4740 rettv->v_type = VAR_BLOB;
4741 rettv->vval.v_blob = NULL;
4742 }
4743 else
4744 {
4745 blob_T *blob = blob_alloc();
4746
4747 if (blob != NULL)
4748 {
4749 if (ga_grow(&blob->bv_ga, n2 - n1 + 1) == FAIL)
4750 {
4751 blob_free(blob);
4752 return FAIL;
4753 }
4754 blob->bv_ga.ga_len = n2 - n1 + 1;
4755 for (i = n1; i <= n2; i++)
4756 blob_set(blob, i - n1,
4757 blob_get(rettv->vval.v_blob, i));
4758
4759 clear_tv(rettv);
4760 rettv_blob_set(rettv, blob);
4761 }
4762 }
4763 }
4764 else
4765 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004766 // The resulting variable is a byte value.
4767 // If the index is too big or negative that is an error.
4768 if (n1 < 0)
4769 n1 = len + n1;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004770 if (n1 < len && n1 >= 0)
4771 {
Bram Moolenaara5be9b62019-01-24 12:31:44 +01004772 int v = blob_get(rettv->vval.v_blob, n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004773
4774 clear_tv(rettv);
4775 rettv->v_type = VAR_NUMBER;
4776 rettv->vval.v_number = v;
4777 }
4778 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004779 semsg(_(e_blobidx), n1);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01004780 }
4781 break;
4782
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004783 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004784 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004785 if (n1 < 0)
4786 n1 = len + n1;
4787 if (!empty1 && (n1 < 0 || n1 >= len))
4788 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004789 /* For a range we allow invalid values and return an empty
4790 * list. A list index out of range is an error. */
4791 if (!range)
4792 {
4793 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004794 semsg(_(e_listidx), n1);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004795 return FAIL;
4796 }
4797 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004798 }
4799 if (range)
4800 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004801 list_T *l;
4802 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004803
4804 if (n2 < 0)
4805 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00004806 else if (n2 >= len)
4807 n2 = len - 1;
4808 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004809 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004810 l = list_alloc();
4811 if (l == NULL)
4812 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004813 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004814 n1 <= n2; ++n1)
4815 {
4816 if (list_append_tv(l, &item->li_tv) == FAIL)
4817 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02004818 list_free(l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004819 return FAIL;
4820 }
4821 item = item->li_next;
4822 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004823 clear_tv(rettv);
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02004824 rettv_list_set(rettv, l);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004825 }
4826 else
4827 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00004828 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
4830 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004831 }
4832 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004833
4834 case VAR_DICT:
4835 if (range)
4836 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004837 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004838 emsg(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00004839 if (len == -1)
4840 clear_tv(&var1);
4841 return FAIL;
4842 }
4843 {
Bram Moolenaar33570922005-01-25 22:26:29 +00004844 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00004845
4846 if (len == -1)
4847 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004848 key = tv_get_string_chk(&var1);
Bram Moolenaar0921ecf2016-04-03 22:44:36 +02004849 if (key == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004850 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00004851 clear_tv(&var1);
4852 return FAIL;
4853 }
4854 }
4855
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00004856 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004857
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00004858 if (item == NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004859 semsg(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00004860 if (len == -1)
4861 clear_tv(&var1);
4862 if (item == NULL)
4863 return FAIL;
4864
4865 copy_tv(&item->di_tv, &var1);
4866 clear_tv(rettv);
4867 *rettv = var1;
4868 }
4869 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004870 }
4871 }
4872
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004873 return OK;
4874}
4875
4876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 * Get an option value.
4878 * "arg" points to the '&' or '+' before the option name.
4879 * "arg" is advanced to character after the option name.
4880 * Return OK or FAIL.
4881 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02004882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004883get_option_tv(
4884 char_u **arg,
4885 typval_T *rettv, /* when NULL, only check if option exists */
4886 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887{
4888 char_u *option_end;
4889 long numval;
4890 char_u *stringval;
4891 int opt_type;
4892 int c;
4893 int working = (**arg == '+'); /* has("+option") */
4894 int ret = OK;
4895 int opt_flags;
4896
4897 /*
4898 * Isolate the option name and find its value.
4899 */
4900 option_end = find_option_end(arg, &opt_flags);
4901 if (option_end == NULL)
4902 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004903 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004904 semsg(_("E112: Option name missing: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 return FAIL;
4906 }
4907
4908 if (!evaluate)
4909 {
4910 *arg = option_end;
4911 return OK;
4912 }
4913
4914 c = *option_end;
4915 *option_end = NUL;
4916 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004917 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918
4919 if (opt_type == -3) /* invalid name */
4920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004921 if (rettv != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004922 semsg(_("E113: Unknown option: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 ret = FAIL;
4924 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004925 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
4927 if (opt_type == -2) /* hidden string option */
4928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004929 rettv->v_type = VAR_STRING;
4930 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 }
4932 else if (opt_type == -1) /* hidden number option */
4933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004934 rettv->v_type = VAR_NUMBER;
4935 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 else if (opt_type == 1) /* number option */
4938 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 rettv->v_type = VAR_NUMBER;
4940 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
4942 else /* string option */
4943 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004944 rettv->v_type = VAR_STRING;
4945 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947 }
4948 else if (working && (opt_type == -2 || opt_type == -1))
4949 ret = FAIL;
4950
4951 *option_end = c; /* put back for error messages */
4952 *arg = option_end;
4953
4954 return ret;
4955}
4956
4957/*
4958 * Allocate a variable for a string constant.
4959 * Return OK or FAIL.
4960 */
4961 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004962get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963{
4964 char_u *p;
4965 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 int extra = 0;
4967
4968 /*
4969 * Find the end of the string, skipping backslashed characters.
4970 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004971 for (p = *arg + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 {
4973 if (*p == '\\' && p[1] != NUL)
4974 {
4975 ++p;
4976 /* A "\<x>" form occupies at least 4 characters, and produces up
4977 * to 6 characters: reserve space for 2 extra */
4978 if (*p == '<')
4979 extra += 2;
4980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 }
4982
4983 if (*p != '"')
4984 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004985 semsg(_("E114: Missing quote: %s"), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 return FAIL;
4987 }
4988
4989 /* If only parsing, set *arg and return here */
4990 if (!evaluate)
4991 {
4992 *arg = p + 1;
4993 return OK;
4994 }
4995
4996 /*
4997 * Copy the string into allocated memory, handling backslashed
4998 * characters.
4999 */
5000 name = alloc((unsigned)(p - *arg + extra));
5001 if (name == NULL)
5002 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005003 rettv->v_type = VAR_STRING;
5004 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005
Bram Moolenaar8c711452005-01-14 21:53:12 +00005006 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
5008 if (*p == '\\')
5009 {
5010 switch (*++p)
5011 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005012 case 'b': *name++ = BS; ++p; break;
5013 case 'e': *name++ = ESC; ++p; break;
5014 case 'f': *name++ = FF; ++p; break;
5015 case 'n': *name++ = NL; ++p; break;
5016 case 'r': *name++ = CAR; ++p; break;
5017 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
5019 case 'X': /* hex: "\x1", "\x12" */
5020 case 'x':
5021 case 'u': /* Unicode: "\u0023" */
5022 case 'U':
5023 if (vim_isxdigit(p[1]))
5024 {
5025 int n, nr;
5026 int c = toupper(*p);
5027
5028 if (c == 'X')
5029 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005030 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005032 else
5033 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 nr = 0;
5035 while (--n >= 0 && vim_isxdigit(p[1]))
5036 {
5037 ++p;
5038 nr = (nr << 4) + hex2nr(*p);
5039 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005040 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 /* For "\u" store the number according to
5042 * 'encoding'. */
5043 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005044 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005046 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 break;
5049
5050 /* octal: "\1", "\12", "\123" */
5051 case '0':
5052 case '1':
5053 case '2':
5054 case '3':
5055 case '4':
5056 case '5':
5057 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005058 case '7': *name = *p++ - '0';
5059 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005061 *name = (*name << 3) + *p++ - '0';
5062 if (*p >= '0' && *p <= '7')
5063 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005065 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 break;
5067
5068 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +02005069 case '<': extra = trans_special(&p, name, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (extra != 0)
5071 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005072 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 break;
5074 }
5075 /* FALLTHROUGH */
5076
Bram Moolenaar8c711452005-01-14 21:53:12 +00005077 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 break;
5079 }
5080 }
5081 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 *name = NUL;
Bram Moolenaardb249f22016-08-26 16:29:47 +02005086 if (*p != NUL) /* just in case */
5087 ++p;
5088 *arg = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 return OK;
5091}
5092
5093/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005094 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 * Return OK or FAIL.
5096 */
5097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005098get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099{
5100 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005101 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005102 int reduce = 0;
5103
5104 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005105 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005106 */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005107 for (p = *arg + 1; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005108 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005109 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005110 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005111 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005112 break;
5113 ++reduce;
5114 ++p;
5115 }
5116 }
5117
Bram Moolenaar8c711452005-01-14 21:53:12 +00005118 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005120 semsg(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005121 return FAIL;
5122 }
5123
Bram Moolenaar8c711452005-01-14 21:53:12 +00005124 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005125 if (!evaluate)
5126 {
5127 *arg = p + 1;
5128 return OK;
5129 }
5130
5131 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005132 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005133 */
5134 str = alloc((unsigned)((p - *arg) - reduce));
5135 if (str == NULL)
5136 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005137 rettv->v_type = VAR_STRING;
5138 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005139
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005141 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005143 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005145 break;
5146 ++p;
5147 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005148 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005149 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005150 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005151 *arg = p + 1;
5152
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005153 return OK;
5154}
5155
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005156/*
5157 * Return the function name of the partial.
5158 */
5159 char_u *
5160partial_name(partial_T *pt)
5161{
5162 if (pt->pt_name != NULL)
5163 return pt->pt_name;
5164 return pt->pt_func->uf_name;
5165}
5166
Bram Moolenaarddecc252016-04-06 22:59:37 +02005167 static void
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005168partial_free(partial_T *pt)
Bram Moolenaarddecc252016-04-06 22:59:37 +02005169{
5170 int i;
5171
5172 for (i = 0; i < pt->pt_argc; ++i)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005173 clear_tv(&pt->pt_argv[i]);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005174 vim_free(pt->pt_argv);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005175 dict_unref(pt->pt_dict);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005176 if (pt->pt_name != NULL)
5177 {
5178 func_unref(pt->pt_name);
5179 vim_free(pt->pt_name);
5180 }
5181 else
5182 func_ptr_unref(pt->pt_func);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005183 vim_free(pt);
5184}
5185
5186/*
5187 * Unreference a closure: decrement the reference count and free it when it
5188 * becomes zero.
5189 */
5190 void
5191partial_unref(partial_T *pt)
5192{
5193 if (pt != NULL && --pt->pt_refcount <= 0)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005194 partial_free(pt);
Bram Moolenaarddecc252016-04-06 22:59:37 +02005195}
5196
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005197static int tv_equal_recurse_limit;
5198
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005199 static int
5200func_equal(
5201 typval_T *tv1,
5202 typval_T *tv2,
5203 int ic) /* ignore case */
5204{
5205 char_u *s1, *s2;
5206 dict_T *d1, *d2;
5207 int a1, a2;
5208 int i;
5209
5210 /* empty and NULL function name considered the same */
5211 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005212 : partial_name(tv1->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005213 if (s1 != NULL && *s1 == NUL)
5214 s1 = NULL;
5215 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005216 : partial_name(tv2->vval.v_partial);
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005217 if (s2 != NULL && *s2 == NUL)
5218 s2 = NULL;
5219 if (s1 == NULL || s2 == NULL)
5220 {
5221 if (s1 != s2)
5222 return FALSE;
5223 }
5224 else if (STRCMP(s1, s2) != 0)
5225 return FALSE;
5226
5227 /* empty dict and NULL dict is different */
5228 d1 = tv1->v_type == VAR_FUNC ? NULL : tv1->vval.v_partial->pt_dict;
5229 d2 = tv2->v_type == VAR_FUNC ? NULL : tv2->vval.v_partial->pt_dict;
5230 if (d1 == NULL || d2 == NULL)
5231 {
5232 if (d1 != d2)
5233 return FALSE;
5234 }
5235 else if (!dict_equal(d1, d2, ic, TRUE))
5236 return FALSE;
5237
5238 /* empty list and no list considered the same */
5239 a1 = tv1->v_type == VAR_FUNC ? 0 : tv1->vval.v_partial->pt_argc;
5240 a2 = tv2->v_type == VAR_FUNC ? 0 : tv2->vval.v_partial->pt_argc;
5241 if (a1 != a2)
5242 return FALSE;
5243 for (i = 0; i < a1; ++i)
5244 if (!tv_equal(tv1->vval.v_partial->pt_argv + i,
5245 tv2->vval.v_partial->pt_argv + i, ic, TRUE))
5246 return FALSE;
5247
5248 return TRUE;
5249}
5250
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005251/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005252 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005253 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005254 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005255 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005256 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005257tv_equal(
5258 typval_T *tv1,
5259 typval_T *tv2,
5260 int ic, /* ignore case */
5261 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005262{
5263 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005264 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005265 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005266 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005267
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005268 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005269 * recursiveness to a limit. We guess they are equal then.
5270 * A fixed limit has the problem of still taking an awful long time.
5271 * Reduce the limit every time running into it. That should work fine for
5272 * deeply linked structures that are not recursively linked and catch
5273 * recursiveness quickly. */
5274 if (!recursive)
5275 tv_equal_recurse_limit = 1000;
5276 if (recursive_cnt >= tv_equal_recurse_limit)
5277 {
5278 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00005279 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005280 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005281
Bram Moolenaarb33c7eb2016-07-04 22:29:49 +02005282 /* For VAR_FUNC and VAR_PARTIAL compare the function name, bound dict and
5283 * arguments. */
Bram Moolenaar8e759ba2016-06-02 17:46:20 +02005284 if ((tv1->v_type == VAR_FUNC
5285 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
5286 && (tv2->v_type == VAR_FUNC
5287 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
5288 {
5289 ++recursive_cnt;
5290 r = func_equal(tv1, tv2, ic);
5291 --recursive_cnt;
5292 return r;
5293 }
5294
5295 if (tv1->v_type != tv2->v_type)
5296 return FALSE;
5297
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005298 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005299 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005300 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005301 ++recursive_cnt;
5302 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
5303 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005304 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005305
5306 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005307 ++recursive_cnt;
5308 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
5309 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00005310 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005311
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005312 case VAR_BLOB:
5313 return blob_equal(tv1->vval.v_blob, tv2->vval.v_blob);
5314
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005315 case VAR_NUMBER:
5316 return tv1->vval.v_number == tv2->vval.v_number;
5317
5318 case VAR_STRING:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005319 s1 = tv_get_string_buf(tv1, buf1);
5320 s2 = tv_get_string_buf(tv2, buf2);
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005321 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005322
5323 case VAR_SPECIAL:
5324 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01005325
5326 case VAR_FLOAT:
5327#ifdef FEAT_FLOAT
5328 return tv1->vval.v_float == tv2->vval.v_float;
5329#endif
5330 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005331#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01005332 return tv1->vval.v_job == tv2->vval.v_job;
5333#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01005334 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005335#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01005336 return tv1->vval.v_channel == tv2->vval.v_channel;
5337#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01005338 case VAR_FUNC:
5339 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005340 case VAR_UNKNOWN:
5341 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005342 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00005343
Bram Moolenaara03f2332016-02-06 18:09:59 +01005344 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
5345 * does not equal anything, not even itself. */
5346 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005347}
5348
5349/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005350 * Return the next (unique) copy ID.
5351 * Used for serializing nested structures.
5352 */
5353 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005354get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005355{
5356 current_copyID += COPYID_INC;
5357 return current_copyID;
5358}
5359
5360/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005361 * Garbage collection for lists and dictionaries.
5362 *
5363 * We use reference counts to be able to free most items right away when they
5364 * are no longer used. But for composite items it's possible that it becomes
5365 * unused while the reference count is > 0: When there is a recursive
5366 * reference. Example:
5367 * :let l = [1, 2, 3]
5368 * :let d = {9: l}
5369 * :let l[1] = d
5370 *
5371 * Since this is quite unusual we handle this with garbage collection: every
5372 * once in a while find out which lists and dicts are not referenced from any
5373 * variable.
5374 *
5375 * Here is a good reference text about garbage collection (refers to Python
5376 * but it applies to all reference-counting mechanisms):
5377 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00005378 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00005379
5380/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005381 * Do garbage collection for lists and dicts.
Bram Moolenaar574860b2016-05-24 17:33:34 +02005382 * When "testing" is TRUE this is called from test_garbagecollect_now().
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005383 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00005384 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005385 int
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005386garbage_collect(int testing)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005387{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005388 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005389 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005390 buf_T *buf;
5391 win_T *wp;
5392 int i;
Bram Moolenaar934b1362015-02-04 23:06:45 +01005393 int did_free = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005394 tabpage_T *tp;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005395
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005396 if (!testing)
5397 {
5398 /* Only do this once. */
5399 want_garbage_collect = FALSE;
5400 may_garbage_collect = FALSE;
5401 garbage_collect_at_exit = FALSE;
5402 }
Bram Moolenaar9fecb462006-09-05 10:59:47 +00005403
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005404 /* We advance by two because we add one for items referenced through
5405 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005406 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005407
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005408 /*
5409 * 1. Go through all accessible variables and mark all lists and dicts
5410 * with copyID.
5411 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005412
5413 /* Don't free variables in the previous_funccal list unless they are only
5414 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00005415 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005416 abort = abort || set_ref_in_previous_funccal(copyID);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005417
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005418 /* script-local variables */
5419 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005420 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005421
5422 /* buffer-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005423 FOR_ALL_BUFFERS(buf)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005424 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
5425 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005426
5427 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005428 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005429 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
5430 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02005431 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005432 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
5433 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005434
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005435 /* tabpage-local variables */
Bram Moolenaar29323592016-07-24 22:04:11 +02005436 FOR_ALL_TABPAGES(tp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005437 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
5438 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005439 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005440 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005441
5442 /* function-local variables */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005443 abort = abort || set_ref_in_call_stack(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005444
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02005445 /* named functions (matters for closures) */
5446 abort = abort || set_ref_in_functions(copyID);
5447
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005448 /* function call arguments, if v:testing is set. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005449 abort = abort || set_ref_in_func_args(copyID);
Bram Moolenaarebf7dfa2016-04-14 12:46:51 +02005450
Bram Moolenaard812df62008-11-09 12:46:09 +00005451 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005452 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00005453
Bram Moolenaar1dced572012-04-05 16:54:08 +02005454#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005455 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02005456#endif
5457
Bram Moolenaardb913952012-06-29 12:54:53 +02005458#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005459 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005460#endif
5461
5462#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005463 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02005464#endif
5465
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01005466#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar3780bb92016-04-12 22:18:53 +02005467 abort = abort || set_ref_in_channel(copyID);
Bram Moolenaarb8d49052016-05-01 14:22:16 +02005468 abort = abort || set_ref_in_job(copyID);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005469#endif
Bram Moolenaar3266c852016-04-30 18:07:05 +02005470#ifdef FEAT_NETBEANS_INTG
5471 abort = abort || set_ref_in_nb_channel(copyID);
5472#endif
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01005473
Bram Moolenaare3188e22016-05-31 21:13:04 +02005474#ifdef FEAT_TIMERS
5475 abort = abort || set_ref_in_timer(copyID);
5476#endif
5477
Bram Moolenaar8f77c5a2017-04-30 14:21:00 +02005478#ifdef FEAT_QUICKFIX
5479 abort = abort || set_ref_in_quickfix(copyID);
5480#endif
5481
Bram Moolenaara2c45a12017-07-27 22:14:59 +02005482#ifdef FEAT_TERMINAL
5483 abort = abort || set_ref_in_term(copyID);
5484#endif
5485
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005486 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005487 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005488 /*
5489 * 2. Free lists and dictionaries that are not referenced.
5490 */
5491 did_free = free_unref_items(copyID);
5492
5493 /*
5494 * 3. Check if any funccal can be freed now.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005495 * This may call us back recursively.
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005496 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02005497 free_unref_funccal(copyID, testing);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005498 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005499 else if (p_verbose > 0)
5500 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01005501 verb_msg(_("Not enough memory to set references, garbage collection aborted!"));
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005502 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005503
5504 return did_free;
5505}
5506
5507/*
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005508 * Free lists, dictionaries, channels and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005509 */
5510 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005511free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005512{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00005513 int did_free = FALSE;
5514
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005515 /* Let all "free" functions know that we are here. This means no
5516 * dictionaries, lists, channels or jobs are to be freed, because we will
5517 * do that here. */
5518 in_free_unref_items = TRUE;
5519
5520 /*
5521 * PASS 1: free the contents of the items. We don't free the items
5522 * themselves yet, so that it is possible to decrement refcount counters
5523 */
5524
Bram Moolenaarcd524592016-07-17 14:57:05 +02005525 /* Go through the list of dicts and free items without the copyID. */
5526 did_free |= dict_free_nonref(copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005527
Bram Moolenaarda861d62016-07-17 15:46:27 +02005528 /* Go through the list of lists and free items without the copyID. */
5529 did_free |= list_free_nonref(copyID);
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005530
5531#ifdef FEAT_JOB_CHANNEL
5532 /* Go through the list of jobs and free items without the copyID. This
5533 * must happen before doing channels, because jobs refer to channels, but
5534 * the reference from the channel to the job isn't tracked. */
5535 did_free |= free_unused_jobs_contents(copyID, COPYID_MASK);
5536
5537 /* Go through the list of channels and free items without the copyID. */
5538 did_free |= free_unused_channels_contents(copyID, COPYID_MASK);
5539#endif
5540
5541 /*
5542 * PASS 2: free the items themselves.
5543 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005544 dict_free_items(copyID);
Bram Moolenaarda861d62016-07-17 15:46:27 +02005545 list_free_items(copyID);
Bram Moolenaar835dc632016-02-07 14:27:38 +01005546
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005547#ifdef FEAT_JOB_CHANNEL
5548 /* Go through the list of jobs and free items without the copyID. This
5549 * must happen before doing channels, because jobs refer to channels, but
5550 * the reference from the channel to the job isn't tracked. */
5551 free_unused_jobs(copyID, COPYID_MASK);
5552
5553 /* Go through the list of channels and free items without the copyID. */
5554 free_unused_channels(copyID, COPYID_MASK);
5555#endif
5556
5557 in_free_unref_items = FALSE;
5558
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005559 return did_free;
5560}
5561
5562/*
5563 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005564 * "list_stack" is used to add lists to be marked. Can be NULL.
5565 *
5566 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005567 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005568 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005569set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005570{
5571 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005572 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005573 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005574 hashtab_T *cur_ht;
5575 ht_stack_T *ht_stack = NULL;
5576 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005577
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005578 cur_ht = ht;
5579 for (;;)
5580 {
5581 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005582 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005583 /* Mark each item in the hashtab. If the item contains a hashtab
5584 * it is added to ht_stack, if it contains a list it is added to
5585 * list_stack. */
5586 todo = (int)cur_ht->ht_used;
5587 for (hi = cur_ht->ht_array; todo > 0; ++hi)
5588 if (!HASHITEM_EMPTY(hi))
5589 {
5590 --todo;
5591 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
5592 &ht_stack, list_stack);
5593 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005594 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005595
5596 if (ht_stack == NULL)
5597 break;
5598
5599 /* take an item from the stack */
5600 cur_ht = ht_stack->ht;
5601 tempitem = ht_stack;
5602 ht_stack = ht_stack->prev;
5603 free(tempitem);
5604 }
5605
5606 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005607}
5608
5609/*
5610 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005611 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5612 *
5613 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005614 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005616set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005617{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005618 listitem_T *li;
5619 int abort = FALSE;
5620 list_T *cur_l;
5621 list_stack_T *list_stack = NULL;
5622 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005623
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005624 cur_l = l;
5625 for (;;)
5626 {
5627 if (!abort)
5628 /* Mark each item in the list. If the item contains a hashtab
5629 * it is added to ht_stack, if it contains a list it is added to
5630 * list_stack. */
5631 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
5632 abort = abort || set_ref_in_item(&li->li_tv, copyID,
5633 ht_stack, &list_stack);
5634 if (list_stack == NULL)
5635 break;
5636
5637 /* take an item from the stack */
5638 cur_l = list_stack->list;
5639 tempitem = list_stack;
5640 list_stack = list_stack->prev;
5641 free(tempitem);
5642 }
5643
5644 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005645}
5646
5647/*
5648 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005649 * "list_stack" is used to add lists to be marked. Can be NULL.
5650 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
5651 *
5652 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005653 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005654 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005655set_ref_in_item(
5656 typval_T *tv,
5657 int copyID,
5658 ht_stack_T **ht_stack,
5659 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005660{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005661 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005662
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005663 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005664 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005665 dict_T *dd = tv->vval.v_dict;
5666
Bram Moolenaara03f2332016-02-06 18:09:59 +01005667 if (dd != NULL && dd->dv_copyID != copyID)
5668 {
5669 /* Didn't see this dict yet. */
5670 dd->dv_copyID = copyID;
5671 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005672 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005673 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
5674 }
5675 else
5676 {
5677 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
5678 if (newitem == NULL)
5679 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005680 else
5681 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005682 newitem->ht = &dd->dv_hashtab;
5683 newitem->prev = *ht_stack;
5684 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005685 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005686 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005687 }
5688 }
5689 else if (tv->v_type == VAR_LIST)
5690 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005691 list_T *ll = tv->vval.v_list;
5692
Bram Moolenaara03f2332016-02-06 18:09:59 +01005693 if (ll != NULL && ll->lv_copyID != copyID)
5694 {
5695 /* Didn't see this list yet. */
5696 ll->lv_copyID = copyID;
5697 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00005698 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005699 abort = set_ref_in_list(ll, copyID, ht_stack);
5700 }
5701 else
5702 {
5703 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005704 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01005705 if (newitem == NULL)
5706 abort = TRUE;
5707 else
5708 {
5709 newitem->list = ll;
5710 newitem->prev = *list_stack;
5711 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005712 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005713 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01005714 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00005715 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005716 else if (tv->v_type == VAR_FUNC)
5717 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005718 abort = set_ref_in_func(tv->vval.v_string, NULL, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005719 }
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005720 else if (tv->v_type == VAR_PARTIAL)
5721 {
5722 partial_T *pt = tv->vval.v_partial;
5723 int i;
5724
5725 /* A partial does not have a copyID, because it cannot contain itself.
5726 */
5727 if (pt != NULL)
5728 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005729 abort = set_ref_in_func(pt->pt_name, pt->pt_func, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02005730
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005731 if (pt->pt_dict != NULL)
5732 {
5733 typval_T dtv;
5734
5735 dtv.v_type = VAR_DICT;
5736 dtv.vval.v_dict = pt->pt_dict;
5737 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5738 }
5739
5740 for (i = 0; i < pt->pt_argc; ++i)
5741 abort = abort || set_ref_in_item(&pt->pt_argv[i], copyID,
5742 ht_stack, list_stack);
5743 }
5744 }
5745#ifdef FEAT_JOB_CHANNEL
5746 else if (tv->v_type == VAR_JOB)
5747 {
5748 job_T *job = tv->vval.v_job;
5749 typval_T dtv;
5750
5751 if (job != NULL && job->jv_copyID != copyID)
5752 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005753 job->jv_copyID = copyID;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005754 if (job->jv_channel != NULL)
5755 {
5756 dtv.v_type = VAR_CHANNEL;
5757 dtv.vval.v_channel = job->jv_channel;
5758 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5759 }
5760 if (job->jv_exit_partial != NULL)
5761 {
5762 dtv.v_type = VAR_PARTIAL;
5763 dtv.vval.v_partial = job->jv_exit_partial;
5764 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5765 }
5766 }
5767 }
5768 else if (tv->v_type == VAR_CHANNEL)
5769 {
5770 channel_T *ch =tv->vval.v_channel;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005771 ch_part_T part;
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005772 typval_T dtv;
5773 jsonq_T *jq;
5774 cbq_T *cq;
5775
5776 if (ch != NULL && ch->ch_copyID != copyID)
5777 {
Bram Moolenaar0239acb2016-04-11 21:02:54 +02005778 ch->ch_copyID = copyID;
Bram Moolenaardc0ccae2016-10-09 17:28:01 +02005779 for (part = PART_SOCK; part < PART_COUNT; ++part)
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02005780 {
5781 for (jq = ch->ch_part[part].ch_json_head.jq_next; jq != NULL;
5782 jq = jq->jq_next)
5783 set_ref_in_item(jq->jq_value, copyID, ht_stack, list_stack);
5784 for (cq = ch->ch_part[part].ch_cb_head.cq_next; cq != NULL;
5785 cq = cq->cq_next)
5786 if (cq->cq_partial != NULL)
5787 {
5788 dtv.v_type = VAR_PARTIAL;
5789 dtv.vval.v_partial = cq->cq_partial;
5790 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5791 }
5792 if (ch->ch_part[part].ch_partial != NULL)
5793 {
5794 dtv.v_type = VAR_PARTIAL;
5795 dtv.vval.v_partial = ch->ch_part[part].ch_partial;
5796 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5797 }
5798 }
5799 if (ch->ch_partial != NULL)
5800 {
5801 dtv.v_type = VAR_PARTIAL;
5802 dtv.vval.v_partial = ch->ch_partial;
5803 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5804 }
5805 if (ch->ch_close_partial != NULL)
5806 {
5807 dtv.v_type = VAR_PARTIAL;
5808 dtv.vval.v_partial = ch->ch_close_partial;
5809 set_ref_in_item(&dtv, copyID, ht_stack, list_stack);
5810 }
5811 }
5812 }
5813#endif
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01005814 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00005815}
5816
Bram Moolenaar17a13432016-01-24 14:22:10 +01005817 static char *
5818get_var_special_name(int nr)
5819{
5820 switch (nr)
5821 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01005822 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01005823 case VVAL_TRUE: return "v:true";
5824 case VVAL_NONE: return "v:none";
5825 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01005826 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01005827 internal_error("get_var_special_name()");
Bram Moolenaar17a13432016-01-24 14:22:10 +01005828 return "42";
5829}
5830
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 * Return a string with the string representation of a variable.
5833 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005834 * "numbuf" is used for a number.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005835 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar35422f42017-08-05 16:33:56 +02005836 * When both "echo_style" and "composite_val" are FALSE, put quotes around
5837 * stings as "string()", otherwise does not put quotes around strings, as
5838 * ":echo" displays values.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005839 * When "restore_copyID" is FALSE, repeated items in dictionaries and lists
5840 * are replaced with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00005841 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02005843 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005844echo_string_core(
Bram Moolenaar7454a062016-01-30 15:14:10 +01005845 typval_T *tv,
5846 char_u **tofree,
5847 char_u *numbuf,
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005848 int copyID,
5849 int echo_style,
5850 int restore_copyID,
Bram Moolenaar35422f42017-08-05 16:33:56 +02005851 int composite_val)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005852{
Bram Moolenaare9a41262005-01-15 22:18:47 +00005853 static int recurse = 0;
5854 char_u *r = NULL;
5855
Bram Moolenaar33570922005-01-25 22:26:29 +00005856 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00005857 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02005858 if (!did_echo_string_emsg)
5859 {
5860 /* Only give this message once for a recursive call to avoid
5861 * flooding the user with errors. And stop iterating over lists
5862 * and dicts. */
5863 did_echo_string_emsg = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005864 emsg(_("E724: variable nested too deep for displaying"));
Bram Moolenaar8502c702014-06-17 12:51:16 +02005865 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005866 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02005867 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00005868 }
5869 ++recurse;
5870
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 switch (tv->v_type)
5872 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005873 case VAR_STRING:
Bram Moolenaar35422f42017-08-05 16:33:56 +02005874 if (echo_style && !composite_val)
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005875 {
5876 *tofree = NULL;
Bram Moolenaar35422f42017-08-05 16:33:56 +02005877 r = tv->vval.v_string;
5878 if (r == NULL)
5879 r = (char_u *)"";
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005880 }
5881 else
5882 {
5883 *tofree = string_quote(tv->vval.v_string, FALSE);
5884 r = *tofree;
5885 }
5886 break;
5887
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888 case VAR_FUNC:
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005889 if (echo_style)
5890 {
5891 *tofree = NULL;
5892 r = tv->vval.v_string;
5893 }
5894 else
5895 {
5896 *tofree = string_quote(tv->vval.v_string, TRUE);
5897 r = *tofree;
5898 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005899 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005900
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005901 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005902 {
5903 partial_T *pt = tv->vval.v_partial;
5904 char_u *fname = string_quote(pt == NULL ? NULL
Bram Moolenaar437bafe2016-08-01 15:40:54 +02005905 : partial_name(pt), FALSE);
Bram Moolenaar24c77a12016-03-24 21:23:06 +01005906 garray_T ga;
5907 int i;
5908 char_u *tf;
5909
5910 ga_init2(&ga, 1, 100);
5911 ga_concat(&ga, (char_u *)"function(");
5912 if (fname != NULL)
5913 {
5914 ga_concat(&ga, fname);
5915 vim_free(fname);
5916 }
5917 if (pt != NULL && pt->pt_argc > 0)
5918 {
5919 ga_concat(&ga, (char_u *)", [");
5920 for (i = 0; i < pt->pt_argc; ++i)
5921 {
5922 if (i > 0)
5923 ga_concat(&ga, (char_u *)", ");
5924 ga_concat(&ga,
5925 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
5926 vim_free(tf);
5927 }
5928 ga_concat(&ga, (char_u *)"]");
5929 }
5930 if (pt != NULL && pt->pt_dict != NULL)
5931 {
5932 typval_T dtv;
5933
5934 ga_concat(&ga, (char_u *)", ");
5935 dtv.v_type = VAR_DICT;
5936 dtv.vval.v_dict = pt->pt_dict;
5937 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
5938 vim_free(tf);
5939 }
5940 ga_concat(&ga, (char_u *)")");
5941
5942 *tofree = ga.ga_data;
5943 r = *tofree;
5944 break;
5945 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005946
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005947 case VAR_BLOB:
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01005948 r = blob2string(tv->vval.v_blob, tofree, numbuf);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01005949 break;
5950
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005951 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005952 if (tv->vval.v_list == NULL)
5953 {
5954 *tofree = NULL;
5955 r = NULL;
5956 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005957 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID
5958 && tv->vval.v_list->lv_len > 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005959 {
5960 *tofree = NULL;
5961 r = (char_u *)"[...]";
5962 }
5963 else
5964 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005965 int old_copyID = tv->vval.v_list->lv_copyID;
5966
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005967 tv->vval.v_list->lv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005968 *tofree = list2string(tv, copyID, restore_copyID);
5969 if (restore_copyID)
5970 tv->vval.v_list->lv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005971 r = *tofree;
5972 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005973 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005974
Bram Moolenaar8c711452005-01-14 21:53:12 +00005975 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005976 if (tv->vval.v_dict == NULL)
5977 {
5978 *tofree = NULL;
5979 r = NULL;
5980 }
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005981 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID
5982 && tv->vval.v_dict->dv_hashtab.ht_used != 0)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005983 {
5984 *tofree = NULL;
5985 r = (char_u *)"{...}";
5986 }
5987 else
5988 {
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005989 int old_copyID = tv->vval.v_dict->dv_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005990 tv->vval.v_dict->dv_copyID = copyID;
Bram Moolenaar18dfb442016-05-31 22:31:23 +02005991 *tofree = dict2string(tv, copyID, restore_copyID);
5992 if (restore_copyID)
5993 tv->vval.v_dict->dv_copyID = old_copyID;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005994 r = *tofree;
5995 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00005996 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00005997
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005998 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005999 case VAR_UNKNOWN:
Bram Moolenaar35422f42017-08-05 16:33:56 +02006000 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006001 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006002 break;
6003
Bram Moolenaar835dc632016-02-07 14:27:38 +01006004 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01006005 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00006006 *tofree = NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006007 r = tv_get_string_buf(tv, numbuf);
Bram Moolenaar35422f42017-08-05 16:33:56 +02006008 if (composite_val)
6009 {
6010 *tofree = string_quote(r, FALSE);
6011 r = *tofree;
6012 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006013 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006014
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006015 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006016#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006017 *tofree = NULL;
6018 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
6019 r = numbuf;
6020 break;
6021#endif
6022
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006023 case VAR_SPECIAL:
6024 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01006025 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006026 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006027 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00006028
Bram Moolenaar8502c702014-06-17 12:51:16 +02006029 if (--recurse == 0)
6030 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006031 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006032}
6033
6034/*
6035 * Return a string with the string representation of a variable.
6036 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6037 * "numbuf" is used for a number.
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006038 * Does not put quotes around strings, as ":echo" displays values.
6039 * When "copyID" is not NULL replace recursive lists and dicts with "...".
6040 * May return NULL.
6041 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006042 char_u *
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006043echo_string(
6044 typval_T *tv,
6045 char_u **tofree,
6046 char_u *numbuf,
6047 int copyID)
6048{
6049 return echo_string_core(tv, tofree, numbuf, copyID, TRUE, FALSE, FALSE);
6050}
6051
6052/*
6053 * Return a string with the string representation of a variable.
6054 * If the memory is allocated "tofree" is set to it, otherwise NULL.
6055 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006056 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00006057 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006058 */
Bram Moolenaar8110a092016-04-14 15:56:09 +02006059 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006060tv2string(
6061 typval_T *tv,
6062 char_u **tofree,
6063 char_u *numbuf,
6064 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006065{
Bram Moolenaar18dfb442016-05-31 22:31:23 +02006066 return echo_string_core(tv, tofree, numbuf, copyID, FALSE, TRUE, FALSE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067}
6068
6069/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006070 * Return string "str" in ' quotes, doubling ' characters.
6071 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00006072 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006073 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02006074 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006076{
Bram Moolenaar33570922005-01-25 22:26:29 +00006077 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006078 char_u *p, *r, *s;
6079
Bram Moolenaar33570922005-01-25 22:26:29 +00006080 len = (function ? 13 : 3);
6081 if (str != NULL)
6082 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006083 len += (unsigned)STRLEN(str);
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006084 for (p = str; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar33570922005-01-25 22:26:29 +00006085 if (*p == '\'')
6086 ++len;
6087 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006088 s = r = alloc(len);
6089 if (r != NULL)
6090 {
6091 if (function)
6092 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00006093 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006094 r += 10;
6095 }
6096 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00006097 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00006098 if (str != NULL)
6099 for (p = str; *p != NUL; )
6100 {
6101 if (*p == '\'')
6102 *r++ = '\'';
6103 MB_COPY_CHAR(p, r);
6104 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00006105 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006106 if (function)
6107 *r++ = ')';
6108 *r++ = NUL;
6109 }
6110 return s;
6111}
6112
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006113#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006114/*
6115 * Convert the string "text" to a floating point number.
6116 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
6117 * this always uses a decimal point.
6118 * Returns the length of the text that was consumed.
6119 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006120 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006121string2float(
6122 char_u *text,
6123 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006124{
6125 char *s = (char *)text;
6126 float_T f;
6127
Bram Moolenaar62473612017-01-08 19:25:40 +01006128 /* MS-Windows does not deal with "inf" and "nan" properly. */
6129 if (STRNICMP(text, "inf", 3) == 0)
6130 {
6131 *value = INFINITY;
6132 return 3;
6133 }
6134 if (STRNICMP(text, "-inf", 3) == 0)
6135 {
6136 *value = -INFINITY;
6137 return 4;
6138 }
6139 if (STRNICMP(text, "nan", 3) == 0)
6140 {
6141 *value = NAN;
6142 return 3;
6143 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006144 f = strtod(s, &s);
6145 *value = f;
6146 return (int)((char_u *)s - text);
6147}
6148#endif
6149
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 * Get the value of an environment variable.
6152 * "arg" is pointing to the '$'. It is advanced to after the name.
6153 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006154 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 */
6156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158{
6159 char_u *string = NULL;
6160 int len;
6161 int cc;
6162 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006163 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164
6165 ++*arg;
6166 name = *arg;
6167 len = get_env_len(arg);
6168 if (evaluate)
6169 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006170 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01006171 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006172
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006173 cc = name[len];
6174 name[len] = NUL;
6175 /* first try vim_getenv(), fast for normal environment vars */
6176 string = vim_getenv(name, &mustfree);
6177 if (string != NULL && *string != NUL)
6178 {
6179 if (!mustfree)
6180 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006182 else
6183 {
6184 if (mustfree)
6185 vim_free(string);
6186
6187 /* next try expanding things like $VIM and ${HOME} */
6188 string = expand_env_save(name - 1);
6189 if (string != NULL && *string == '$')
Bram Moolenaard23a8232018-02-10 18:45:26 +01006190 VIM_CLEAR(string);
Bram Moolenaare512c8c2014-04-29 17:41:22 +02006191 }
6192 name[len] = cc;
6193
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006194 rettv->v_type = VAR_STRING;
6195 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 }
6197
6198 return OK;
6199}
6200
Bram Moolenaard6e256c2011-12-14 15:32:50 +01006201
6202
6203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006204 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006205 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006207 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006208var2fpos(
6209 typval_T *varp,
6210 int dollar_lnum, /* TRUE when $ is last line */
6211 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006212{
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006213 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +00006215 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216
Bram Moolenaara5525202006-03-02 22:52:09 +00006217 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006218 if (varp->v_type == VAR_LIST)
6219 {
6220 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006221 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +00006222 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +00006223 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006224
6225 l = varp->vval.v_list;
6226 if (l == NULL)
6227 return NULL;
6228
6229 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006230 pos.lnum = list_find_nr(l, 0L, &error);
6231 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006232 return NULL; /* invalid line number */
6233
6234 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006235 pos.col = list_find_nr(l, 1L, &error);
6236 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006237 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006238 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +00006239
6240 /* We accept "$" for the column number: last column. */
6241 li = list_find(l, 1L);
6242 if (li != NULL && li->li_tv.v_type == VAR_STRING
6243 && li->li_tv.vval.v_string != NULL
6244 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
6245 pos.col = len + 1;
6246
Bram Moolenaara5525202006-03-02 22:52:09 +00006247 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00006248 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006249 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +00006250 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006251
Bram Moolenaara5525202006-03-02 22:52:09 +00006252 /* Get the virtual offset. Defaults to zero. */
6253 pos.coladd = list_find_nr(l, 2L, &error);
6254 if (error)
6255 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006256
Bram Moolenaar32466aa2006-02-24 23:53:04 +00006257 return &pos;
6258 }
6259
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006260 name = tv_get_string_chk(varp);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006261 if (name == NULL)
6262 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006263 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006265 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
6266 {
6267 if (VIsual_active)
6268 return &VIsual;
6269 return &curwin->w_cursor;
6270 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +00006271 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01006273 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006274 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
6275 return NULL;
6276 return pp;
6277 }
Bram Moolenaara5525202006-03-02 22:52:09 +00006278
Bram Moolenaara5525202006-03-02 22:52:09 +00006279 pos.coladd = 0;
Bram Moolenaara5525202006-03-02 22:52:09 +00006280
Bram Moolenaar477933c2007-07-17 14:32:23 +00006281 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006282 {
6283 pos.col = 0;
6284 if (name[1] == '0') /* "w0": first visible line */
6285 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006286 update_topline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006287 /* In silent Ex mode topline is zero, but that's not a valid line
6288 * number; use one instead. */
6289 pos.lnum = curwin->w_topline > 0 ? curwin->w_topline : 1;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006290 return &pos;
6291 }
6292 else if (name[1] == '$') /* "w$": last visible line */
6293 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00006294 validate_botline();
Bram Moolenaara1d5fa62017-04-03 22:02:55 +02006295 /* In silent Ex mode botline is zero, return zero then. */
6296 pos.lnum = curwin->w_botline > 0 ? curwin->w_botline - 1 : 0;
Bram Moolenaarf52c7252006-02-10 23:23:57 +00006297 return &pos;
6298 }
6299 }
6300 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006301 {
Bram Moolenaar477933c2007-07-17 14:32:23 +00006302 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006303 {
6304 pos.lnum = curbuf->b_ml.ml_line_count;
6305 pos.col = 0;
6306 }
6307 else
6308 {
6309 pos.lnum = curwin->w_cursor.lnum;
6310 pos.col = (colnr_T)STRLEN(ml_get_curline());
6311 }
6312 return &pos;
6313 }
6314 return NULL;
6315}
6316
6317/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006318 * Convert list in "arg" into a position and optional file number.
6319 * When "fnump" is NULL there is no file number, only 3 items.
6320 * Note that the column is passed on as-is, the caller may want to decrement
6321 * it to use 1 for the first column.
6322 * Return FAIL when conversion is not possible, doesn't check the position for
6323 * validity.
6324 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006326list2fpos(
6327 typval_T *arg,
6328 pos_T *posp,
6329 int *fnump,
6330 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006331{
6332 list_T *l = arg->vval.v_list;
6333 long i = 0;
6334 long n;
6335
Bram Moolenaar493c1782014-05-28 14:34:46 +02006336 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
6337 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +00006338 if (arg->v_type != VAR_LIST
6339 || l == NULL
6340 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +02006341 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006342 return FAIL;
6343
6344 if (fnump != NULL)
6345 {
6346 n = list_find_nr(l, i++, NULL); /* fnum */
6347 if (n < 0)
6348 return FAIL;
6349 if (n == 0)
6350 n = curbuf->b_fnum; /* current buffer */
6351 *fnump = n;
6352 }
6353
6354 n = list_find_nr(l, i++, NULL); /* lnum */
6355 if (n < 0)
6356 return FAIL;
6357 posp->lnum = n;
6358
6359 n = list_find_nr(l, i++, NULL); /* col */
6360 if (n < 0)
6361 return FAIL;
6362 posp->col = n;
6363
Bram Moolenaar493c1782014-05-28 14:34:46 +02006364 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006365 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +00006366 posp->coladd = 0;
6367 else
6368 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006369
Bram Moolenaar493c1782014-05-28 14:34:46 +02006370 if (curswantp != NULL)
6371 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
6372
Bram Moolenaar0e34f622006-03-03 23:00:03 +00006373 return OK;
6374}
6375
6376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006377 * Get the length of an environment variable name.
6378 * Advance "arg" to the first character after the name.
6379 * Return 0 for error.
6380 */
6381 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006382get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 char_u *p;
6385 int len;
6386
6387 for (p = *arg; vim_isIDc(*p); ++p)
6388 ;
6389 if (p == *arg) /* no name found */
6390 return 0;
6391
6392 len = (int)(p - *arg);
6393 *arg = p;
6394 return len;
6395}
6396
6397/*
6398 * Get the length of the name of a function or internal variable.
6399 * "arg" is advanced to the first non-white character after the name.
6400 * Return 0 if something is wrong.
6401 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006402 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006403get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404{
6405 char_u *p;
6406 int len;
6407
6408 /* Find the end of the name. */
6409 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006410 {
6411 if (*p == ':')
6412 {
6413 /* "s:" is start of "s:var", but "n:" is not and can be used in
6414 * slice "[n:]". Also "xx:" is not a namespace. */
6415 len = (int)(p - *arg);
6416 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
6417 || len > 1)
6418 break;
6419 }
6420 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421 if (p == *arg) /* no name found */
6422 return 0;
6423
6424 len = (int)(p - *arg);
6425 *arg = skipwhite(p);
6426
6427 return len;
6428}
6429
6430/*
Bram Moolenaara7043832005-01-21 11:56:39 +00006431 * Get the length of the name of a variable or function.
6432 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006434 * Return -1 if curly braces expansion failed.
6435 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 * If the name contains 'magic' {}'s, expand them and return the
6437 * expanded name in an allocated string via 'alias' - caller must free.
6438 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006439 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006440get_name_len(
6441 char_u **arg,
6442 char_u **alias,
6443 int evaluate,
6444 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445{
6446 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006447 char_u *p;
6448 char_u *expr_start;
6449 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450
6451 *alias = NULL; /* default to no alias */
6452
6453 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
6454 && (*arg)[2] == (int)KE_SNR)
6455 {
6456 /* hard coded <SNR>, already translated */
6457 *arg += 3;
6458 return get_id_len(arg) + 3;
6459 }
6460 len = eval_fname_script(*arg);
6461 if (len > 0)
6462 {
6463 /* literal "<SID>", "s:" or "<SNR>" */
6464 *arg += len;
6465 }
6466
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006468 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006470 p = find_name_end(*arg, &expr_start, &expr_end,
6471 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006472 if (expr_start != NULL)
6473 {
6474 char_u *temp_string;
6475
6476 if (!evaluate)
6477 {
6478 len += (int)(p - *arg);
6479 *arg = skipwhite(p);
6480 return len;
6481 }
6482
6483 /*
6484 * Include any <SID> etc in the expanded string:
6485 * Thus the -len here.
6486 */
6487 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
6488 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006489 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 *alias = temp_string;
6491 *arg = skipwhite(p);
6492 return (int)STRLEN(temp_string);
6493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006494
6495 len += get_id_len(arg);
Bram Moolenaar8309b052019-01-13 16:46:22 +01006496 // Only give an error when there is something, otherwise it will be
6497 // reported at a higher level.
6498 if (len == 0 && verbose && **arg != NUL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006499 semsg(_(e_invexpr2), *arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501 return len;
6502}
6503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006504/*
6505 * Find the end of a variable or function name, taking care of magic braces.
6506 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
6507 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006508 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006509 * Return a pointer to just after the name. Equal to "arg" if there is no
6510 * valid name.
6511 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006512 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006513find_name_end(
6514 char_u *arg,
6515 char_u **expr_start,
6516 char_u **expr_end,
6517 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006519 int mb_nest = 0;
6520 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006522 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006524 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006526 *expr_start = NULL;
6527 *expr_end = NULL;
6528 }
6529
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006530 /* Quick check for valid starting character. */
6531 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
6532 return arg;
6533
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006534 for (p = arg; *p != NUL
6535 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006536 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006537 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006538 || mb_nest != 0
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006539 || br_nest != 0); MB_PTR_ADV(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006540 {
Bram Moolenaar8af24422005-08-08 22:06:28 +00006541 if (*p == '\'')
6542 {
6543 /* skip over 'string' to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006544 for (p = p + 1; *p != NUL && *p != '\''; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006545 ;
6546 if (*p == NUL)
6547 break;
6548 }
6549 else if (*p == '"')
6550 {
6551 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01006552 for (p = p + 1; *p != NUL && *p != '"'; MB_PTR_ADV(p))
Bram Moolenaar8af24422005-08-08 22:06:28 +00006553 if (*p == '\\' && p[1] != NUL)
6554 ++p;
6555 if (*p == NUL)
6556 break;
6557 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006558 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
6559 {
6560 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006561 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006562 len = (int)(p - arg);
6563 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +01006564 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +01006565 break;
6566 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006567
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006568 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006570 if (*p == '[')
6571 ++br_nest;
6572 else if (*p == ']')
6573 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574 }
Bram Moolenaar8af24422005-08-08 22:06:28 +00006575
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006576 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006578 if (*p == '{')
6579 {
6580 mb_nest++;
6581 if (expr_start != NULL && *expr_start == NULL)
6582 *expr_start = p;
6583 }
6584 else if (*p == '}')
6585 {
6586 mb_nest--;
6587 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
6588 *expr_end = p;
6589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 }
6592
6593 return p;
6594}
6595
6596/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006597 * Expands out the 'magic' {}'s in a variable/function name.
6598 * Note that this can call itself recursively, to deal with
6599 * constructs like foo{bar}{baz}{bam}
6600 * The four pointer arguments point to "foo{expre}ss{ion}bar"
6601 * "in_start" ^
6602 * "expr_start" ^
6603 * "expr_end" ^
6604 * "in_end" ^
6605 *
6606 * Returns a new allocated string, which the caller must free.
6607 * Returns NULL for failure.
6608 */
6609 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006610make_expanded_name(
6611 char_u *in_start,
6612 char_u *expr_start,
6613 char_u *expr_end,
6614 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006615{
6616 char_u c1;
6617 char_u *retval = NULL;
6618 char_u *temp_result;
6619 char_u *nextcmd = NULL;
6620
6621 if (expr_end == NULL || in_end == NULL)
6622 return NULL;
6623 *expr_start = NUL;
6624 *expr_end = NUL;
6625 c1 = *in_end;
6626 *in_end = NUL;
6627
Bram Moolenaar362e1a32006-03-06 23:29:24 +00006628 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006629 if (temp_result != NULL && nextcmd == NULL)
6630 {
6631 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
6632 + (in_end - expr_end) + 1));
6633 if (retval != NULL)
6634 {
6635 STRCPY(retval, in_start);
6636 STRCAT(retval, temp_result);
6637 STRCAT(retval, expr_end + 1);
6638 }
6639 }
6640 vim_free(temp_result);
6641
6642 *in_end = c1; /* put char back for error messages */
6643 *expr_start = '{';
6644 *expr_end = '}';
6645
6646 if (retval != NULL)
6647 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006648 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006649 if (expr_start != NULL)
6650 {
6651 /* Further expansion! */
6652 temp_result = make_expanded_name(retval, expr_start,
6653 expr_end, temp_result);
6654 vim_free(retval);
6655 retval = temp_result;
6656 }
6657 }
6658
6659 return retval;
6660}
6661
6662/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +00006664 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006665 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006666 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006667eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006669 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
6670}
6671
6672/*
6673 * Return TRUE if character "c" can be used as the first character in a
6674 * variable or function name (excluding '{' and '}').
6675 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02006676 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006677eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00006678{
6679 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680}
6681
6682/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683 * Set number v: variable to "val".
6684 */
6685 void
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006686set_vim_var_nr(int idx, varnumber_T val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006688 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689}
6690
6691/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006692 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02006694 varnumber_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01006695get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696{
Bram Moolenaare9a41262005-01-15 22:18:47 +00006697 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698}
6699
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006700/*
6701 * Get string v: variable value. Uses a static buffer, can only be used once.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01006702 * If the String variable has never been set, return an empty string.
6703 * Never returns NULL;
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006704 */
6705 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006707{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01006708 return tv_get_string(&vimvars[idx].vv_tv);
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006709}
Bram Moolenaar19a09a12005-03-04 23:39:37 +00006710
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006712 * Get List v: variable value. Caller must take care of reference count when
6713 * needed.
6714 */
6715 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006716get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006717{
6718 return vimvars[idx].vv_list;
6719}
6720
6721/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006722 * Get Dict v: variable value. Caller must take care of reference count when
6723 * needed.
6724 */
6725 dict_T *
6726get_vim_var_dict(int idx)
6727{
6728 return vimvars[idx].vv_dict;
6729}
6730
6731/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006732 * Set v:char to character "c".
6733 */
6734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006735set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006736{
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006737 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006738
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006739 if (has_mbyte)
6740 buf[(*mb_char2bytes)(c, buf)] = NUL;
6741 else
Bram Moolenaarda9591e2009-09-30 13:17:02 +00006742 {
6743 buf[0] = c;
6744 buf[1] = NUL;
6745 }
6746 set_vim_var_string(VV_CHAR, buf, -1);
6747}
6748
6749/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006750 * Set v:count to "count" and v:count1 to "count1".
6751 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006752 */
6753 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006754set_vcount(
6755 long count,
6756 long count1,
6757 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758{
Bram Moolenaar8df74be2008-11-20 15:12:02 +00006759 if (set_prevcount)
6760 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006761 vimvars[VV_COUNT].vv_nr = count;
6762 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763}
6764
6765/*
Bram Moolenaarb0f42ba2018-05-12 15:38:26 +02006766 * Save variables that might be changed as a side effect. Used when executing
6767 * a timer callback.
6768 */
6769 void
6770save_vimvars(vimvars_save_T *vvsave)
6771{
6772 vvsave->vv_prevcount = vimvars[VV_PREVCOUNT].vv_nr;
6773 vvsave->vv_count = vimvars[VV_COUNT].vv_nr;
6774 vvsave->vv_count1 = vimvars[VV_COUNT1].vv_nr;
6775}
6776
6777/*
6778 * Restore variables saved by save_vimvars().
6779 */
6780 void
6781restore_vimvars(vimvars_save_T *vvsave)
6782{
6783 vimvars[VV_PREVCOUNT].vv_nr = vvsave->vv_prevcount;
6784 vimvars[VV_COUNT].vv_nr = vvsave->vv_count;
6785 vimvars[VV_COUNT1].vv_nr = vvsave->vv_count1;
6786}
6787
6788/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 * Set string v: variable to a copy of "val".
6790 */
6791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006792set_vim_var_string(
6793 int idx,
6794 char_u *val,
6795 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
Bram Moolenaara542c682016-01-31 16:28:04 +01006797 clear_tv(&vimvars[idx].vv_di.di_tv);
6798 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006800 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006802 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 else
Bram Moolenaare9a41262005-01-15 22:18:47 +00006804 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805}
6806
6807/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006808 * Set List v: variable to "val".
6809 */
6810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006811set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +00006812{
Bram Moolenaara542c682016-01-31 16:28:04 +01006813 clear_tv(&vimvars[idx].vv_di.di_tv);
6814 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +00006815 vimvars[idx].vv_list = val;
6816 if (val != NULL)
6817 ++val->lv_refcount;
6818}
6819
6820/*
Bram Moolenaar42a45122015-07-10 17:56:23 +02006821 * Set Dictionary v: variable to "val".
6822 */
6823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006824set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +02006825{
Bram Moolenaara542c682016-01-31 16:28:04 +01006826 clear_tv(&vimvars[idx].vv_di.di_tv);
6827 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +02006828 vimvars[idx].vv_dict = val;
6829 if (val != NULL)
6830 {
6831 ++val->dv_refcount;
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01006832 dict_set_items_ro(val);
Bram Moolenaar42a45122015-07-10 17:56:23 +02006833 }
6834}
6835
6836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 * Set v:register if needed.
6838 */
6839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841{
6842 char_u regname;
6843
6844 if (c == 0 || c == ' ')
6845 regname = '"';
6846 else
6847 regname = c;
6848 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +00006849 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006850 set_vim_var_string(VV_REG, &regname, 1);
6851}
6852
6853/*
6854 * Get or set v:exception. If "oldval" == NULL, return the current value.
6855 * Otherwise, restore the value to "oldval" and return NULL.
6856 * Must always be called in pairs to save and restore v:exception! Does not
6857 * take care of memory allocations.
6858 */
6859 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006860v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006861{
6862 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006863 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006864
Bram Moolenaare9a41262005-01-15 22:18:47 +00006865 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 return NULL;
6867}
6868
6869/*
6870 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
6871 * Otherwise, restore the value to "oldval" and return NULL.
6872 * Must always be called in pairs to save and restore v:throwpoint! Does not
6873 * take care of memory allocations.
6874 */
6875 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006876v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006877{
6878 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006879 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880
Bram Moolenaare9a41262005-01-15 22:18:47 +00006881 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882 return NULL;
6883}
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885/*
6886 * Set v:cmdarg.
6887 * If "eap" != NULL, use "eap" to generate the value and return the old value.
6888 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
6889 * Must always be called in pairs!
6890 */
6891 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006892set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893{
6894 char_u *oldval;
6895 char_u *newval;
6896 unsigned len;
6897
Bram Moolenaare9a41262005-01-15 22:18:47 +00006898 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006899 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006901 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006902 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006903 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006906 if (eap->force_bin == FORCE_BIN)
6907 len = 6;
6908 else if (eap->force_bin == FORCE_NOBIN)
6909 len = 8;
6910 else
6911 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006912
6913 if (eap->read_edit)
6914 len += 7;
6915
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006916 if (eap->force_ff != 0)
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006917 len += 10; /* " ++ff=unix" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006918 if (eap->force_enc != 0)
6919 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006920 if (eap->bad_char != 0)
6921 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006922
6923 newval = alloc(len + 1);
6924 if (newval == NULL)
6925 return NULL;
6926
6927 if (eap->force_bin == FORCE_BIN)
6928 sprintf((char *)newval, " ++bin");
6929 else if (eap->force_bin == FORCE_NOBIN)
6930 sprintf((char *)newval, " ++nobin");
6931 else
6932 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006933
6934 if (eap->read_edit)
6935 STRCAT(newval, " ++edit");
6936
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006937 if (eap->force_ff != 0)
6938 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
Bram Moolenaar333b80a2018-04-04 22:57:29 +02006939 eap->force_ff == 'u' ? "unix"
6940 : eap->force_ff == 'd' ? "dos"
6941 : "mac");
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006942 if (eap->force_enc != 0)
6943 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
6944 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +02006945 if (eap->bad_char == BAD_KEEP)
6946 STRCPY(newval + STRLEN(newval), " ++bad=keep");
6947 else if (eap->bad_char == BAD_DROP)
6948 STRCPY(newval + STRLEN(newval), " ++bad=drop");
6949 else if (eap->bad_char != 0)
6950 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaare9a41262005-01-15 22:18:47 +00006951 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +00006952 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
6955/*
6956 * Get the value of internal variable "name".
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01006957 * Return OK or FAIL. If OK is returned "rettv" must be cleared.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02006959 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006960get_var_tv(
6961 char_u *name,
6962 int len, /* length of "name" */
6963 typval_T *rettv, /* NULL when only checking existence */
6964 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
6965 int verbose, /* may give error message */
6966 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
6968 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +00006969 typval_T *tv = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00006970 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972
6973 /* truncate the name, so that we can use strcmp() */
6974 cc = name[len];
6975 name[len] = NUL;
6976
6977 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 * Check for user-defined variables.
6979 */
Bram Moolenaar79518e22017-02-17 16:31:35 +01006980 v = find_var(name, NULL, no_autoload);
6981 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006982 {
Bram Moolenaar79518e22017-02-17 16:31:35 +01006983 tv = &v->di_tv;
6984 if (dip != NULL)
6985 *dip = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986 }
6987
Bram Moolenaare9a41262005-01-15 22:18:47 +00006988 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00006990 if (rettv != NULL && verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006991 semsg(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 ret = FAIL;
6993 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006994 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00006995 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006996
6997 name[len] = cc;
6998
6999 return ret;
7000}
7001
7002/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007003 * Check if variable "name[len]" is a local variable or an argument.
7004 * If so, "*eval_lavars_used" is set to TRUE.
7005 */
7006 static void
7007check_vars(char_u *name, int len)
7008{
7009 int cc;
7010 char_u *varname;
7011 hashtab_T *ht;
7012
7013 if (eval_lavars_used == NULL)
7014 return;
7015
7016 /* truncate the name, so that we can use strcmp() */
7017 cc = name[len];
7018 name[len] = NUL;
7019
7020 ht = find_var_ht(name, &varname);
7021 if (ht == get_funccal_local_ht() || ht == get_funccal_args_ht())
7022 {
7023 if (find_var(name, NULL, TRUE) != NULL)
7024 *eval_lavars_used = TRUE;
7025 }
7026
7027 name[len] = cc;
7028}
7029
7030/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007031 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
7032 * Also handle function call with Funcref variable: func(expr)
7033 * Can all be combined: dict.func(expr)[idx]['func'](expr)
7034 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007036handle_subscript(
7037 char_u **arg,
7038 typval_T *rettv,
7039 int evaluate, /* do more than finding the end */
7040 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007041{
7042 int ret = OK;
7043 dict_T *selfdict = NULL;
7044 char_u *s;
7045 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007047
7048 while (ret == OK
7049 && (**arg == '['
7050 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007051 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
7052 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar1c465442017-03-12 20:10:05 +01007053 && !VIM_ISWHITE(*(*arg - 1)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007054 {
7055 if (**arg == '(')
7056 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +01007057 partial_T *pt = NULL;
7058
Bram Moolenaard9fba312005-06-26 22:34:35 +00007059 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007060 if (evaluate)
7061 {
7062 functv = *rettv;
7063 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007064
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007065 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007066 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007067 {
7068 pt = functv.vval.v_partial;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02007069 s = partial_name(pt);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007070 }
7071 else
7072 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007073 }
7074 else
7075 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007076 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +00007077 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007078 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +00007079
7080 /* Clear the funcref afterwards, so that deleting it while
7081 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +01007082 if (evaluate)
7083 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007084
7085 /* Stop the expression evaluation when immediately aborting on
7086 * error, or when an interrupt occurred or an exception was thrown
7087 * but not caught. */
7088 if (aborting())
7089 {
7090 if (ret == OK)
7091 clear_tv(rettv);
7092 ret = FAIL;
7093 }
7094 dict_unref(selfdict);
7095 selfdict = NULL;
7096 }
7097 else /* **arg == '[' || **arg == '.' */
7098 {
7099 dict_unref(selfdict);
7100 if (rettv->v_type == VAR_DICT)
7101 {
7102 selfdict = rettv->vval.v_dict;
7103 if (selfdict != NULL)
7104 ++selfdict->dv_refcount;
7105 }
7106 else
7107 selfdict = NULL;
7108 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
7109 {
7110 clear_tv(rettv);
7111 ret = FAIL;
7112 }
7113 }
7114 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007115
Bram Moolenaar1d429612016-05-24 15:44:17 +02007116 /* Turn "dict.Func" into a partial for "Func" bound to "dict".
7117 * Don't do this when "Func" is already a partial that was bound
7118 * explicitly (pt_auto is FALSE). */
7119 if (selfdict != NULL
7120 && (rettv->v_type == VAR_FUNC
7121 || (rettv->v_type == VAR_PARTIAL
7122 && (rettv->vval.v_partial->pt_auto
7123 || rettv->vval.v_partial->pt_dict == NULL))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007124 selfdict = make_partial(selfdict, rettv);
Bram Moolenaarab1fa392016-03-15 19:33:34 +01007125
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00007126 dict_unref(selfdict);
7127 return ret;
7128}
7129
7130/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007131 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007132 * value).
7133 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +01007134 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007135alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007136{
Bram Moolenaar33570922005-01-25 22:26:29 +00007137 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007138}
7139
7140/*
7141 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 * The string "s" must have been allocated, it is consumed.
7143 * Return NULL for out of memory, the variable otherwise.
7144 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007146alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007147{
Bram Moolenaar33570922005-01-25 22:26:29 +00007148 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007150 rettv = alloc_tv();
7151 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007153 rettv->v_type = VAR_STRING;
7154 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155 }
7156 else
7157 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007158 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007159}
7160
7161/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007162 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 */
Bram Moolenaar4770d092006-01-12 23:22:24 +00007164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007165free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166{
7167 if (varp != NULL)
7168 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007169 switch (varp->v_type)
7170 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007171 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007172 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007173 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007174 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007175 vim_free(varp->vval.v_string);
7176 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007177 case VAR_PARTIAL:
7178 partial_unref(varp->vval.v_partial);
7179 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007180 case VAR_BLOB:
7181 blob_unref(varp->vval.v_blob);
7182 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007183 case VAR_LIST:
7184 list_unref(varp->vval.v_list);
7185 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007186 case VAR_DICT:
7187 dict_unref(varp->vval.v_dict);
7188 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007189 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007190#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007191 job_unref(varp->vval.v_job);
7192 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007193#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007194 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007195#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007196 channel_unref(varp->vval.v_channel);
7197 break;
7198#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007199 case VAR_NUMBER:
7200 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007201 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +01007202 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +00007203 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007205 vim_free(varp);
7206 }
7207}
7208
7209/*
7210 * Free the memory for a variable value and set the value to NULL or 0.
7211 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007212 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214{
7215 if (varp != NULL)
7216 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007217 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007219 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007220 func_unref(varp->vval.v_string);
Bram Moolenaar2f40d122017-10-24 21:49:36 +02007221 /* FALLTHROUGH */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007222 case VAR_STRING:
Bram Moolenaard23a8232018-02-10 18:45:26 +01007223 VIM_CLEAR(varp->vval.v_string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007224 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007225 case VAR_PARTIAL:
7226 partial_unref(varp->vval.v_partial);
7227 varp->vval.v_partial = NULL;
7228 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007229 case VAR_BLOB:
7230 blob_unref(varp->vval.v_blob);
7231 varp->vval.v_blob = NULL;
7232 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007233 case VAR_LIST:
7234 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007235 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007236 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007237 case VAR_DICT:
7238 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007239 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007241 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007242 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007243 varp->vval.v_number = 0;
7244 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007245 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007246#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007247 varp->vval.v_float = 0.0;
7248 break;
7249#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01007250 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007251#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007252 job_unref(varp->vval.v_job);
7253 varp->vval.v_job = NULL;
7254#endif
7255 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007256 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007257#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007258 channel_unref(varp->vval.v_channel);
7259 varp->vval.v_channel = NULL;
7260#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007261 case VAR_UNKNOWN:
7262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007264 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007265 }
7266}
7267
7268/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007269 * Set the value of a variable to NULL without freeing items.
7270 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007272init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007273{
7274 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007275 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007276}
7277
7278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279 * Get the number value of a variable.
7280 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007281 * For incompatible types, return 0.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007282 * tv_get_number_chk() is similar to tv_get_number(), but informs the
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007283 * caller of incompatible types: it sets *denote to TRUE if "denote"
7284 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007285 */
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007286 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007287tv_get_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007288{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007289 int error = FALSE;
7290
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007291 return tv_get_number_chk(varp, &error); /* return 0L on error */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007292}
7293
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007294 varnumber_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007295tv_get_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007296{
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007297 varnumber_T n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007299 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007301 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007302 return varp->vval.v_number;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007303 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007304#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007305 emsg(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007306 break;
7307#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007308 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007309 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007310 emsg(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007311 break;
7312 case VAR_STRING:
7313 if (varp->vval.v_string != NULL)
7314 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01007315 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007316 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007317 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007318 emsg(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007319 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007320 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007321 emsg(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007322 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007323 case VAR_SPECIAL:
7324 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
7325 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007326 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007327#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007328 emsg(_("E910: Using a Job as a Number"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007329 break;
7330#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007331 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007332#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007333 emsg(_("E913: Using a Channel as a Number"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007334 break;
7335#endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007336 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007337 emsg(_("E974: Using a Blob as a Number"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007338 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007339 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007340 internal_error("tv_get_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007341 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007343 if (denote == NULL) /* useful for values that must be unsigned */
7344 n = -1;
7345 else
7346 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007347 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348}
7349
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007350#ifdef FEAT_FLOAT
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007351 float_T
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007352tv_get_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007353{
7354 switch (varp->v_type)
7355 {
7356 case VAR_NUMBER:
7357 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007358 case VAR_FLOAT:
7359 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007360 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007361 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007362 emsg(_("E891: Using a Funcref as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007363 break;
7364 case VAR_STRING:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007365 emsg(_("E892: Using a String as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007366 break;
7367 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007368 emsg(_("E893: Using a List as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007369 break;
7370 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007371 emsg(_("E894: Using a Dictionary as a Float"));
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007372 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007373 case VAR_SPECIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007374 emsg(_("E907: Using a special value as a Float"));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007375 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007376 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007377# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007378 emsg(_("E911: Using a Job as a Float"));
Bram Moolenaar835dc632016-02-07 14:27:38 +01007379 break;
7380# endif
Bram Moolenaar77073442016-02-13 23:23:53 +01007381 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007382# ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007383 emsg(_("E914: Using a Channel as a Float"));
Bram Moolenaar77073442016-02-13 23:23:53 +01007384 break;
7385# endif
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007386 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007387 emsg(_("E975: Using a Blob as a Float"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007388 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007389 case VAR_UNKNOWN:
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007390 internal_error("tv_get_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +01007391 break;
7392 }
7393 return 0;
7394}
7395#endif
7396
Bram Moolenaar071d4272004-06-13 20:20:40 +00007397/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398 * Get the string value of a variable.
7399 * If it is a Number variable, the number is converted into a string.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007400 * tv_get_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7401 * tv_get_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 * If the String variable has never been set, return an empty string.
7403 * Never returns NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007404 * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007405 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007406 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007407 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007408tv_get_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007409{
7410 static char_u mybuf[NUMBUFLEN];
7411
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007412 return tv_get_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413}
7414
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01007415 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007416tv_get_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007417{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007418 char_u *res = tv_get_string_buf_chk(varp, buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007419
7420 return res != NULL ? res : (char_u *)"";
7421}
7422
Bram Moolenaar7d647822014-04-05 21:28:56 +02007423/*
7424 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
7425 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007426 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007427tv_get_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007428{
7429 static char_u mybuf[NUMBUFLEN];
7430
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007431 return tv_get_string_buf_chk(varp, mybuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007432}
7433
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007434 char_u *
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007435tv_get_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007436{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007437 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007438 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007439 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +02007440 vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +01007441 (long_long_T)varp->vval.v_number);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007442 return buf;
7443 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007444 case VAR_PARTIAL:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007445 emsg(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007446 break;
7447 case VAR_LIST:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007448 emsg(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00007449 break;
7450 case VAR_DICT:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007451 emsg(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007452 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007453 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007454#ifdef FEAT_FLOAT
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007455 emsg(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007456 break;
7457#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007458 case VAR_STRING:
7459 if (varp->vval.v_string != NULL)
7460 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007461 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007462 case VAR_SPECIAL:
7463 STRCPY(buf, get_var_special_name(varp->vval.v_number));
7464 return buf;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007465 case VAR_BLOB:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007466 emsg(_("E976: using Blob as a String"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01007467 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +01007468 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007469#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01007470 {
7471 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +01007472 char *status;
7473
7474 if (job == NULL)
7475 return (char_u *)"no process";
7476 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar7df915d2016-11-17 17:25:32 +01007477 : job->jv_status >= JOB_ENDED ? "dead"
Bram Moolenaar835dc632016-02-07 14:27:38 +01007478 : "run";
7479# ifdef UNIX
7480 vim_snprintf((char *)buf, NUMBUFLEN,
7481 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4f974752019-02-17 17:44:42 +01007482# elif defined(MSWIN)
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007483 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +01007484 "process %ld %s",
7485 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007486 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007487# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +01007488 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +01007489 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
7490# endif
7491 return buf;
7492 }
7493#endif
7494 break;
Bram Moolenaar77073442016-02-13 23:23:53 +01007495 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007496#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007497 {
7498 channel_T *channel = varp->vval.v_channel;
Bram Moolenaar0e77b762016-09-26 22:58:58 +02007499 char *status = channel_status(channel, -1);
Bram Moolenaar77073442016-02-13 23:23:53 +01007500
Bram Moolenaar5cefd402016-02-16 12:44:26 +01007501 if (channel == NULL)
7502 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
7503 else
7504 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +01007505 "channel %d %s", channel->ch_id, status);
7506 return buf;
7507 }
7508#endif
7509 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007510 case VAR_UNKNOWN:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007511 emsg(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007512 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00007514 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515}
7516
7517/*
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01007518 * Turn a typeval into a string. Similar to tv_get_string_buf() but uses
7519 * string() on Dict, List, etc.
7520 */
7521 char_u *
7522tv_stringify(typval_T *varp, char_u *buf)
7523{
7524 if (varp->v_type == VAR_LIST
7525 || varp->v_type == VAR_DICT
7526 || varp->v_type == VAR_FUNC
7527 || varp->v_type == VAR_PARTIAL
7528 || varp->v_type == VAR_FLOAT)
7529 {
7530 typval_T tmp;
7531
7532 f_string(varp, &tmp);
7533 tv_get_string_buf(&tmp, buf);
7534 clear_tv(varp);
7535 *varp = tmp;
7536 return tmp.vval.v_string;
7537 }
7538 return tv_get_string_buf(varp, buf);
7539}
7540
7541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 * Find variable "name" in the list of variables.
7543 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007544 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +00007545 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007548 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550{
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007552 hashtab_T *ht;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007553 dictitem_T *ret = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554
Bram Moolenaara7043832005-01-21 11:56:39 +00007555 ht = find_var_ht(name, &varname);
7556 if (htp != NULL)
7557 *htp = ht;
7558 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 return NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007560 ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
7561 if (ret != NULL)
7562 return ret;
7563
7564 /* Search in parent scope for lambda */
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007565 return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566}
7567
7568/*
Bram Moolenaar332ac062013-04-15 13:06:21 +02007569 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +00007570 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007572 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007573find_var_in_ht(
7574 hashtab_T *ht,
7575 int htname,
7576 char_u *varname,
7577 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +00007578{
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 hashitem_T *hi;
7580
7581 if (*varname == NUL)
7582 {
7583 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +02007584 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007586 case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +00007587 case 'g': return &globvars_var;
7588 case 'v': return &vimvars_var;
7589 case 'b': return &curbuf->b_bufvar;
7590 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007591 case 't': return &curtab->tp_winvar;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007592 case 'l': return get_funccal_local_var();
7593 case 'a': return get_funccal_args_var();
Bram Moolenaar33570922005-01-25 22:26:29 +00007594 }
7595 return NULL;
7596 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007597
7598 hi = hash_find(ht, varname);
7599 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007600 {
7601 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00007602 * worked find the variable again. Don't auto-load a script if it was
7603 * loaded already, otherwise it would be loaded every time when
7604 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007605 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007606 {
7607 /* Note: script_autoload() may make "hi" invalid. It must either
7608 * be obtained again or not used. */
7609 if (!script_autoload(varname, FALSE) || aborting())
7610 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007611 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +01007612 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007613 if (HASHITEM_EMPTY(hi))
7614 return NULL;
7615 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007616 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007617}
7618
7619/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007620 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +02007621 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +00007622 * Set "varname" to the start of name without ':'.
7623 */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007624 hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007625find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626{
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007627 hashitem_T *hi;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007628 hashtab_T *ht;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007629
Bram Moolenaar73627d02015-08-11 15:46:09 +02007630 if (name[0] == NUL)
7631 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632 if (name[1] != ':')
7633 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007634 /* The name must not start with a colon or #. */
7635 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636 return NULL;
7637 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +00007638
7639 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +00007640 hi = hash_find(&compat_hashtab, name);
7641 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +00007642 return &compat_hashtab;
7643
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007644 ht = get_funccal_local_ht();
7645 if (ht == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 return &globvarht; /* global variable */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007647 return ht; /* local variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648 }
7649 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007650 if (*name == 'g') /* global variable */
7651 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00007652 /* There must be no ':' or '#' in the rest of the name, unless g: is used
7653 */
7654 if (vim_strchr(name + 2, ':') != NULL
7655 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00007656 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007657 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007658 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007660 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00007661 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +02007662 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00007663 if (*name == 'v') /* v: variable */
7664 return &vimvarht;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007665 if (*name == 'a') /* a: function argument */
7666 return get_funccal_args_ht();
7667 if (*name == 'l') /* l: local function variable */
7668 return get_funccal_local_ht();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669 if (*name == 's' /* script variable */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02007670 && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
7671 return &SCRIPT_VARS(current_sctx.sc_sid);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007672 return NULL;
7673}
7674
7675/*
7676 * Get the string value of a (global/local) variable.
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007677 * Note: see tv_get_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678 * Returns NULL when it doesn't exist.
7679 */
7680 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007681get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007682{
Bram Moolenaar33570922005-01-25 22:26:29 +00007683 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684
Bram Moolenaar6d977d62014-01-14 15:24:39 +01007685 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007686 if (v == NULL)
7687 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007688 return tv_get_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689}
7690
7691/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007692 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 * sourcing this script and when executing functions defined in the script.
7694 */
7695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007696new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697{
Bram Moolenaara7043832005-01-21 11:56:39 +00007698 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00007699 hashtab_T *ht;
7700 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +00007701
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
7703 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007704 /* Re-allocating ga_data means that an ht_array pointing to
7705 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +00007706 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +00007707 for (i = 1; i <= ga_scripts.ga_len; ++i)
7708 {
7709 ht = &SCRIPT_VARS(i);
7710 if (ht->ht_mask == HT_INIT_SIZE - 1)
7711 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007712 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +00007714 }
7715
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 while (ga_scripts.ga_len < id)
7717 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +02007718 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02007719 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007720 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 }
7723 }
7724}
7725
7726/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007727 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
7728 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 */
7730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +02007734 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007735 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00007736 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007737 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00007738 dict_var->di_tv.vval.v_dict = dict;
7739 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007740 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +00007741 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
7742 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743}
7744
7745/*
Bram Moolenaar429fa852013-04-15 12:27:36 +02007746 * Unreference a dictionary initialized by init_var_dict().
7747 */
7748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007749unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +02007750{
7751 /* Now the dict needs to be freed if no one else is using it, go back to
7752 * normal reference counting. */
7753 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
7754 dict_unref(dict);
7755}
7756
7757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +00007759 * Frees all allocated variables and the value they contain.
7760 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007761 */
7762 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007763vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +00007764{
7765 vars_clear_ext(ht, TRUE);
7766}
7767
7768/*
7769 * Like vars_clear(), but only free the value if "free_val" is TRUE.
7770 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02007771 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007772vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773{
Bram Moolenaara7043832005-01-21 11:56:39 +00007774 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 hashitem_T *hi;
7776 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007779 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00007780 for (hi = ht->ht_array; todo > 0; ++hi)
7781 {
7782 if (!HASHITEM_EMPTY(hi))
7783 {
7784 --todo;
7785
Bram Moolenaar33570922005-01-25 22:26:29 +00007786 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +00007787 * ht_array might change then. hash_clear() takes care of it
7788 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +00007789 v = HI2DI(hi);
7790 if (free_val)
7791 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007792 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +00007793 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +00007794 }
7795 }
7796 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007797 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798}
7799
Bram Moolenaara7043832005-01-21 11:56:39 +00007800/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007801 * Delete a variable from hashtab "ht" at item "hi".
7802 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +00007803 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007805delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806{
Bram Moolenaar33570922005-01-25 22:26:29 +00007807 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00007808
7809 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +00007810 clear_tv(&di->di_tv);
7811 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812}
7813
7814/*
7815 * List the value of one internal variable.
7816 */
7817 static void
Bram Moolenaar32526b32019-01-19 17:43:09 +01007818list_one_var(dictitem_T *v, char *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 char_u *tofree;
7821 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007822 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007823
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007824 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +00007825 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007826 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828}
7829
Bram Moolenaar071d4272004-06-13 20:20:40 +00007830 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007831list_one_var_a(
Bram Moolenaar32526b32019-01-19 17:43:09 +01007832 char *prefix,
Bram Moolenaar7454a062016-01-30 15:14:10 +01007833 char_u *name,
7834 int type,
7835 char_u *string,
7836 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
Bram Moolenaar31859182007-08-14 20:41:13 +00007838 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
7839 msg_start();
7840 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 if (name != NULL) /* "a:" vars don't have a name stored */
Bram Moolenaar32526b32019-01-19 17:43:09 +01007842 msg_puts((char *)name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007843 msg_putchar(' ');
7844 msg_advance(22);
7845 if (type == VAR_NUMBER)
7846 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007847 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007848 msg_putchar('*');
7849 else if (type == VAR_LIST)
7850 {
7851 msg_putchar('[');
7852 if (*string == '[')
7853 ++string;
7854 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007855 else if (type == VAR_DICT)
7856 {
7857 msg_putchar('{');
7858 if (*string == '{')
7859 ++string;
7860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 else
7862 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007863
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007866 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar32526b32019-01-19 17:43:09 +01007867 msg_puts("()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +00007868 if (*first)
7869 {
7870 msg_clr_eos();
7871 *first = FALSE;
7872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873}
7874
7875/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007876 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 * If the variable already exists, the value is updated.
7878 * Otherwise the variable is created.
7879 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02007880 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007881set_var(
7882 char_u *name,
7883 typval_T *tv,
7884 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885{
Bram Moolenaar33570922005-01-25 22:26:29 +00007886 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +00007888 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007890 ht = find_var_ht(name, &varname);
7891 if (ht == NULL || *varname == NUL)
7892 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007893 semsg(_(e_illvar), name);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007894 return;
7895 }
Bram Moolenaar332ac062013-04-15 13:06:21 +02007896 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +01007897
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007898 /* Search in parent scope which is possible to reference from lambda */
7899 if (v == NULL)
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02007900 v = find_var_in_scoped_ht(name, TRUE);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02007901
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007902 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
7903 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007904 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007905
Bram Moolenaar33570922005-01-25 22:26:29 +00007906 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007908 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +02007909 if (var_check_ro(v->di_flags, name, FALSE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01007910 || var_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +00007911 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007912
7913 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007914 * Handle setting internal v: variables separately where needed to
7915 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +00007916 */
7917 if (ht == &vimvarht)
7918 {
7919 if (v->di_tv.v_type == VAR_STRING)
7920 {
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007921 VIM_CLEAR(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00007922 if (copy || tv->v_type != VAR_STRING)
Bram Moolenaar4b9e91f2019-01-24 13:58:11 +01007923 {
7924 char_u *val = tv_get_string(tv);
7925
7926 // Careful: when assigning to v:errmsg and tv_get_string()
7927 // causes an error message the variable will alrady be set.
7928 if (v->di_tv.vval.v_string == NULL)
7929 v->di_tv.vval.v_string = vim_strsave(val);
7930 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007931 else
7932 {
7933 /* Take over the string to avoid an extra alloc/free. */
7934 v->di_tv.vval.v_string = tv->vval.v_string;
7935 tv->vval.v_string = NULL;
7936 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007937 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007938 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007939 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007940 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01007941 v->di_tv.vval.v_number = tv_get_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007942 if (STRCMP(varname, "searchforward") == 0)
7943 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +01007944#ifdef FEAT_SEARCH_EXTRA
7945 else if (STRCMP(varname, "hlsearch") == 0)
7946 {
7947 no_hlsearch = !v->di_tv.vval.v_number;
7948 redraw_all_later(SOME_VALID);
7949 }
7950#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007951 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007952 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02007953 else if (v->di_tv.v_type != tv->v_type)
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007954 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007955 semsg(_("E963: setting %s to value with wrong type"), name);
Bram Moolenaar88b53fd2018-12-05 18:43:28 +01007956 return;
7957 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 }
7959
7960 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962 else /* add a new variable */
7963 {
Bram Moolenaar31b81602019-02-10 22:14:27 +01007964 // Can't add "v:" or "a:" variable.
7965 if (ht == &vimvarht || ht == get_funccal_args_ht())
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007966 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01007967 semsg(_(e_illvar), name);
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +00007968 return;
7969 }
7970
Bram Moolenaar31b81602019-02-10 22:14:27 +01007971 // Make sure the variable name is valid.
Bram Moolenaar4228bec2011-03-27 16:03:15 +02007972 if (!valid_varname(varname))
7973 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +00007974
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007975 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7976 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +00007977 if (v == NULL)
7978 return;
Bram Moolenaar33570922005-01-25 22:26:29 +00007979 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +00007980 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {
Bram Moolenaara7043832005-01-21 11:56:39 +00007982 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 return;
7984 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007985 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 }
Bram Moolenaara7043832005-01-21 11:56:39 +00007987
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +00007989 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007990 else
7991 {
Bram Moolenaar33570922005-01-25 22:26:29 +00007992 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007993 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007994 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +00007995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996}
7997
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007998/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00007999 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +00008000 * Also give an error message.
8001 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008002 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008003var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +00008004{
8005 if (flags & DI_FLAGS_RO)
8006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008007 semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008008 return TRUE;
8009 }
8010 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
8011 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008012 semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008013 return TRUE;
8014 }
8015 return FALSE;
8016}
8017
8018/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008019 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
8020 * Also give an error message.
8021 */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008023var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008024{
8025 if (flags & DI_FLAGS_FIX)
8026 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008027 semsg(_("E795: Cannot delete variable %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008028 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +00008029 return TRUE;
8030 }
8031 return FALSE;
8032}
8033
8034/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008035 * Check if a funcref is assigned to a valid variable name.
8036 * Return TRUE and give an error if not.
8037 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008039var_check_func_name(
8040 char_u *name, /* points to start of variable name */
8041 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008042{
Bram Moolenaarcbc67722014-05-22 14:19:56 +02008043 /* Allow for w: b: s: and t:. */
8044 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008045 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
8046 ? name[2] : name[0]))
8047 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008048 semsg(_("E704: Funcref variable name must start with a capital: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008049 name);
8050 return TRUE;
8051 }
8052 /* Don't allow hiding a function. When "v" is not NULL we might be
8053 * assigning another function to the same var, the type is checked
8054 * below. */
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02008055 if (new_var && function_exists(name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008056 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008057 semsg(_("E705: Variable name conflicts with existing function: %s"),
Bram Moolenaar4228bec2011-03-27 16:03:15 +02008058 name);
8059 return TRUE;
8060 }
8061 return FALSE;
8062}
8063
8064/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008065 * Return TRUE if "flags" indicates variable "name" is locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +02008066 * Also give an error message, using "name" or _("name") when use_gettext is
8067 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008068 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008069 int
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008070var_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008071{
8072 if (lock & VAR_LOCKED)
8073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008074 semsg(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008075 name == NULL ? (char_u *)_("Unknown")
8076 : use_gettext ? (char_u *)_(name)
8077 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008078 return TRUE;
8079 }
8080 if (lock & VAR_FIXED)
8081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008082 semsg(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +02008083 name == NULL ? (char_u *)_("Unknown")
8084 : use_gettext ? (char_u *)_(name)
8085 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008086 return TRUE;
8087 }
8088 return FALSE;
8089}
8090
8091/*
Bram Moolenaar05c00c02019-02-11 22:00:11 +01008092 * Return TRUE if typeval "tv" and its value are set to be locked (immutable).
8093 * Also give an error message, using "name" or _("name") when use_gettext is
8094 * TRUE.
8095 */
8096 static int
8097tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
8098{
8099 int lock = 0;
8100
8101 switch (tv->v_type)
8102 {
8103 case VAR_BLOB:
8104 if (tv->vval.v_blob != NULL)
8105 lock = tv->vval.v_blob->bv_lock;
8106 break;
8107 case VAR_LIST:
8108 if (tv->vval.v_list != NULL)
8109 lock = tv->vval.v_list->lv_lock;
8110 break;
8111 case VAR_DICT:
8112 if (tv->vval.v_dict != NULL)
8113 lock = tv->vval.v_dict->dv_lock;
8114 break;
8115 default:
8116 break;
8117 }
8118 return var_check_lock(tv->v_lock, name, use_gettext)
8119 || (lock != 0 && var_check_lock(lock, name, use_gettext));
8120}
8121
8122/*
8123 * Check if a variable name is valid.
8124 * Return FALSE and give an error if not.
8125 */
8126 int
8127valid_varname(char_u *varname)
8128{
8129 char_u *p;
8130
8131 for (p = varname; *p != NUL; ++p)
8132 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
8133 && *p != AUTOLOAD_CHAR)
8134 {
8135 semsg(_(e_illvar), varname);
8136 return FALSE;
8137 }
8138 return TRUE;
8139}
8140
8141/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008142 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008143 * When needed allocates string or increases reference count.
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008144 * Does not make a copy of a list, blob or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00008145 * It is OK for "from" and "to" to point to the same item. This is used to
8146 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008147 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008148 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008149copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008151 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008152 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008153 switch (from->v_type)
8154 {
8155 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008156 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008157 to->vval.v_number = from->vval.v_number;
8158 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008160#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161 to->vval.v_float = from->vval.v_float;
8162 break;
8163#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01008164 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008165#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01008166 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01008167 if (to->vval.v_job != NULL)
8168 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +01008169 break;
8170#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01008171 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008172#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01008173 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +01008174 if (to->vval.v_channel != NULL)
8175 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +01008176 break;
8177#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008178 case VAR_STRING:
8179 case VAR_FUNC:
8180 if (from->vval.v_string == NULL)
8181 to->vval.v_string = NULL;
8182 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008183 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008184 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008185 if (from->v_type == VAR_FUNC)
8186 func_ref(to->vval.v_string);
8187 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008188 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008189 case VAR_PARTIAL:
8190 if (from->vval.v_partial == NULL)
8191 to->vval.v_partial = NULL;
8192 else
8193 {
8194 to->vval.v_partial = from->vval.v_partial;
8195 ++to->vval.v_partial->pt_refcount;
8196 }
8197 break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008198 case VAR_BLOB:
8199 if (from->vval.v_blob == NULL)
8200 to->vval.v_blob = NULL;
8201 else
8202 {
8203 to->vval.v_blob = from->vval.v_blob;
8204 ++to->vval.v_blob->bv_refcount;
8205 }
8206 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008207 case VAR_LIST:
8208 if (from->vval.v_list == NULL)
8209 to->vval.v_list = NULL;
8210 else
8211 {
8212 to->vval.v_list = from->vval.v_list;
8213 ++to->vval.v_list->lv_refcount;
8214 }
8215 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008216 case VAR_DICT:
8217 if (from->vval.v_dict == NULL)
8218 to->vval.v_dict = NULL;
8219 else
8220 {
8221 to->vval.v_dict = from->vval.v_dict;
8222 ++to->vval.v_dict->dv_refcount;
8223 }
8224 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008225 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008226 internal_error("copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008227 break;
8228 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229}
8230
8231/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00008232 * Make a copy of an item.
8233 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008234 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
8235 * reference to an already copied list/dict can be used.
8236 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +00008237 */
Bram Moolenaarcd524592016-07-17 14:57:05 +02008238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008239item_copy(
8240 typval_T *from,
8241 typval_T *to,
8242 int deep,
8243 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008244{
8245 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008246 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008247
Bram Moolenaar33570922005-01-25 22:26:29 +00008248 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008249 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008250 emsg(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008251 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008252 }
8253 ++recurse;
8254
8255 switch (from->v_type)
8256 {
8257 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008258 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008259 case VAR_STRING:
8260 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008261 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +01008262 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008263 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008264 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008265 copy_tv(from, to);
8266 break;
8267 case VAR_LIST:
8268 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008269 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008270 if (from->vval.v_list == NULL)
8271 to->vval.v_list = NULL;
8272 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
8273 {
8274 /* use the copy made earlier */
8275 to->vval.v_list = from->vval.v_list->lv_copylist;
8276 ++to->vval.v_list->lv_refcount;
8277 }
8278 else
8279 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
8280 if (to->vval.v_list == NULL)
8281 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008282 break;
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008283 case VAR_BLOB:
Bram Moolenaardd29ea12019-01-23 21:56:21 +01008284 ret = blob_copy(from, to);
Bram Moolenaar3d28b582019-01-15 22:44:17 +01008285 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008286 case VAR_DICT:
8287 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008288 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008289 if (from->vval.v_dict == NULL)
8290 to->vval.v_dict = NULL;
8291 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
8292 {
8293 /* use the copy made earlier */
8294 to->vval.v_dict = from->vval.v_dict->dv_copydict;
8295 ++to->vval.v_dict->dv_refcount;
8296 }
8297 else
8298 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
8299 if (to->vval.v_dict == NULL)
8300 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008301 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008302 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +01008303 internal_error("item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008304 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008305 }
8306 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008307 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008308}
8309
8310/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008311 * This function is used by f_input() and f_inputdialog() functions. The third
8312 * argument to f_input() specifies the type of completion to use at the
8313 * prompt. The third argument to f_inputdialog() specifies the value to return
8314 * when the user cancels the prompt.
8315 */
8316 void
8317get_user_input(
8318 typval_T *argvars,
8319 typval_T *rettv,
8320 int inputdialog,
8321 int secret)
8322{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008323 char_u *prompt = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008324 char_u *p = NULL;
8325 int c;
8326 char_u buf[NUMBUFLEN];
8327 int cmd_silent_save = cmd_silent;
8328 char_u *defstr = (char_u *)"";
8329 int xp_type = EXPAND_NOTHING;
8330 char_u *xp_arg = NULL;
8331
8332 rettv->v_type = VAR_STRING;
8333 rettv->vval.v_string = NULL;
8334
8335#ifdef NO_CONSOLE_INPUT
Bram Moolenaar91d348a2017-07-29 20:16:03 +02008336 /* While starting up, there is no place to enter text. When running tests
8337 * with --not-a-term we assume feedkeys() will be used. */
8338 if (no_console_input() && !is_not_a_term())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008339 return;
8340#endif
8341
8342 cmd_silent = FALSE; /* Want to see the prompt. */
8343 if (prompt != NULL)
8344 {
8345 /* Only the part of the message after the last NL is considered as
8346 * prompt for the command line */
8347 p = vim_strrchr(prompt, '\n');
8348 if (p == NULL)
8349 p = prompt;
8350 else
8351 {
8352 ++p;
8353 c = *p;
8354 *p = NUL;
8355 msg_start();
8356 msg_clr_eos();
Bram Moolenaar32526b32019-01-19 17:43:09 +01008357 msg_puts_attr((char *)prompt, echo_attr);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008358 msg_didout = FALSE;
8359 msg_starthere();
8360 *p = c;
8361 }
8362 cmdline_row = msg_row;
8363
8364 if (argvars[1].v_type != VAR_UNKNOWN)
8365 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008366 defstr = tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008367 if (defstr != NULL)
8368 stuffReadbuffSpec(defstr);
8369
8370 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
8371 {
8372 char_u *xp_name;
8373 int xp_namelen;
8374 long argt;
8375
8376 /* input() with a third argument: completion */
8377 rettv->vval.v_string = NULL;
8378
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008379 xp_name = tv_get_string_buf_chk(&argvars[2], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008380 if (xp_name == NULL)
8381 return;
8382
8383 xp_namelen = (int)STRLEN(xp_name);
8384
8385 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
8386 &xp_arg) == FAIL)
8387 return;
8388 }
8389 }
8390
8391 if (defstr != NULL)
8392 {
8393 int save_ex_normal_busy = ex_normal_busy;
Bram Moolenaar6dff58f2018-09-30 21:43:26 +02008394
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008395 ex_normal_busy = 0;
8396 rettv->vval.v_string =
8397 getcmdline_prompt(secret ? NUL : '@', p, echo_attr,
8398 xp_type, xp_arg);
8399 ex_normal_busy = save_ex_normal_busy;
8400 }
8401 if (inputdialog && rettv->vval.v_string == NULL
8402 && argvars[1].v_type != VAR_UNKNOWN
8403 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008404 rettv->vval.v_string = vim_strsave(tv_get_string_buf(
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008405 &argvars[2], buf));
8406
8407 vim_free(xp_arg);
8408
8409 /* since the user typed this, no need to wait for return */
8410 need_wait_return = FALSE;
8411 msg_didout = FALSE;
8412 }
8413 cmd_silent = cmd_silent_save;
8414}
8415
8416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 * ":echo expr1 ..." print each argument separated with a space, add a
8418 * newline at the end.
8419 * ":echon expr1 ..." print each argument plain.
8420 */
8421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008422ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423{
8424 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008425 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008426 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 char_u *p;
8428 int needclr = TRUE;
8429 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008430 char_u numbuf[NUMBUFLEN];
Bram Moolenaar76a63452018-11-28 20:38:37 +01008431 int did_emsg_before = did_emsg;
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008432 int called_emsg_before = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433
8434 if (eap->skip)
8435 ++emsg_skip;
8436 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
8437 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008438 /* If eval1() causes an error message the text from the command may
8439 * still need to be cleared. E.g., "echo 22,44". */
8440 need_clr_eos = needclr;
8441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008443 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {
8445 /*
8446 * Report the invalid expression unless the expression evaluation
8447 * has been cancelled due to an aborting error, an interrupt, or an
8448 * exception.
8449 */
Bram Moolenaarc0f5a782019-01-13 15:16:13 +01008450 if (!aborting() && did_emsg == did_emsg_before
8451 && called_emsg == called_emsg_before)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008452 semsg(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008453 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 break;
8455 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008456 need_clr_eos = FALSE;
8457
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 if (!eap->skip)
8459 {
8460 if (atstart)
8461 {
8462 atstart = FALSE;
8463 /* Call msg_start() after eval1(), evaluating the expression
8464 * may cause a message to appear. */
8465 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +01008466 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +02008467 /* Mark the saved text as finishing the line, so that what
8468 * follows is displayed on a new line when scrolling back
8469 * at the more prompt. */
8470 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +01008472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 }
8474 else if (eap->cmdidx == CMD_echo)
Bram Moolenaar32526b32019-01-19 17:43:09 +01008475 msg_puts_attr(" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008476 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008477 if (p != NULL)
8478 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008480 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008482 if (*p != TAB && needclr)
8483 {
8484 /* remove any text still there from the command */
8485 msg_clr_eos();
8486 needclr = FALSE;
8487 }
8488 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 }
8490 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008491 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008492 if (has_mbyte)
8493 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00008494 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008495
8496 (void)msg_outtrans_len_attr(p, i, echo_attr);
8497 p += i - 1;
8498 }
8499 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008500 (void)msg_outtrans_len_attr(p, 1, echo_attr);
8501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 arg = skipwhite(arg);
8507 }
8508 eap->nextcmd = check_nextcmd(arg);
8509
8510 if (eap->skip)
8511 --emsg_skip;
8512 else
8513 {
8514 /* remove text that may still be there from the command */
8515 if (needclr)
8516 msg_clr_eos();
8517 if (eap->cmdidx == CMD_echo)
8518 msg_end();
8519 }
8520}
8521
8522/*
8523 * ":echohl {name}".
8524 */
8525 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008526ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527{
Bram Moolenaar1b9645d2017-09-17 23:03:31 +02008528 echo_attr = syn_name2attr(eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008529}
8530
8531/*
8532 * ":execute expr1 ..." execute the result of an expression.
8533 * ":echomsg expr1 ..." Print a message
8534 * ":echoerr expr1 ..." Print an error
8535 * Each gets spaces around each argument and a newline at the end for
8536 * echo commands
8537 */
8538 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008539ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540{
8541 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00008542 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 int ret = OK;
8544 char_u *p;
8545 garray_T ga;
8546 int len;
Bram Moolenaar2c519cf2019-03-21 21:45:34 +01008547 int save_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548
8549 ga_init2(&ga, 1, 80);
8550
8551 if (eap->skip)
8552 ++emsg_skip;
8553 while (*arg != NUL && *arg != '|' && *arg != '\n')
8554 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +01008555 ret = eval1_emsg(&arg, &rettv, !eap->skip);
8556 if (ret == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558
8559 if (!eap->skip)
8560 {
Bram Moolenaar461a7fc2018-12-22 13:28:07 +01008561 char_u buf[NUMBUFLEN];
8562
8563 if (eap->cmdidx == CMD_execute)
8564 p = tv_get_string_buf(&rettv, buf);
8565 else
8566 p = tv_stringify(&rettv, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 len = (int)STRLEN(p);
8568 if (ga_grow(&ga, len + 2) == FAIL)
8569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008570 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571 ret = FAIL;
8572 break;
8573 }
8574 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008575 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577 ga.ga_len += len;
8578 }
8579
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008580 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 arg = skipwhite(arg);
8582 }
8583
8584 if (ret != FAIL && ga.ga_data != NULL)
8585 {
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008586 if (eap->cmdidx == CMD_echomsg || eap->cmdidx == CMD_echoerr)
8587 {
8588 /* Mark the already saved text as finishing the line, so that what
8589 * follows is displayed on a new line when scrolling back at the
8590 * more prompt. */
8591 msg_sb_eol();
Bram Moolenaar57002ad2017-03-16 19:04:19 +01008592 }
8593
Bram Moolenaar071d4272004-06-13 20:20:40 +00008594 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +00008595 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01008596 msg_attr(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +00008597 out_flush();
8598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 else if (eap->cmdidx == CMD_echoerr)
8600 {
8601 /* We don't want to abort following commands, restore did_emsg. */
8602 save_did_emsg = did_emsg;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01008603 emsg(ga.ga_data);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604 if (!force_abort)
8605 did_emsg = save_did_emsg;
8606 }
8607 else if (eap->cmdidx == CMD_execute)
8608 do_cmdline((char_u *)ga.ga_data,
8609 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
8610 }
8611
8612 ga_clear(&ga);
8613
8614 if (eap->skip)
8615 --emsg_skip;
8616
8617 eap->nextcmd = check_nextcmd(arg);
8618}
8619
8620/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008621 * Find window specified by "vp" in tabpage "tp".
8622 */
8623 win_T *
8624find_win_by_nr(
8625 typval_T *vp,
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01008626 tabpage_T *tp) /* NULL for current tab page */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008627{
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008628 win_T *wp;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008629 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008630
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008631 if (nr < 0)
8632 return NULL;
8633 if (nr == 0)
8634 return curwin;
8635
Bram Moolenaar29323592016-07-24 22:04:11 +02008636 FOR_ALL_WINDOWS_IN_TAB(tp, wp)
8637 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008638 if (nr >= LOWEST_WIN_ID)
8639 {
8640 if (wp->w_id == nr)
8641 return wp;
8642 }
8643 else if (--nr <= 0)
8644 break;
Bram Moolenaar29323592016-07-24 22:04:11 +02008645 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008646 if (nr >= LOWEST_WIN_ID)
8647 return NULL;
8648 return wp;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008649}
8650
8651/*
Bram Moolenaare6e39892018-10-25 12:32:11 +02008652 * Find a window: When using a Window ID in any tab page, when using a number
8653 * in the current tab page.
8654 */
8655 win_T *
8656find_win_by_nr_or_id(typval_T *vp)
8657{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008658 int nr = (int)tv_get_number_chk(vp, NULL);
Bram Moolenaare6e39892018-10-25 12:32:11 +02008659
8660 if (nr >= LOWEST_WIN_ID)
Bram Moolenaareeb1b9c2019-02-10 22:59:04 +01008661 return win_id2wp(tv_get_number(vp));
Bram Moolenaare6e39892018-10-25 12:32:11 +02008662 return find_win_by_nr(vp, NULL);
8663}
8664
8665/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008666 * Find window specified by "wvp" in tabpage "tvp".
8667 */
8668 win_T *
8669find_tabwin(
8670 typval_T *wvp, /* VAR_UNKNOWN for current window */
8671 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
8672{
8673 win_T *wp = NULL;
8674 tabpage_T *tp = NULL;
8675 long n;
8676
8677 if (wvp->v_type != VAR_UNKNOWN)
8678 {
8679 if (tvp->v_type != VAR_UNKNOWN)
8680 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008681 n = (long)tv_get_number(tvp);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008682 if (n >= 0)
8683 tp = find_tabpage(n);
8684 }
8685 else
8686 tp = curtab;
8687
8688 if (tp != NULL)
8689 wp = find_win_by_nr(wvp, tp);
8690 }
8691 else
8692 wp = curwin;
8693
8694 return wp;
8695}
8696
8697/*
8698 * getwinvar() and gettabwinvar()
8699 */
8700 void
8701getwinvar(
8702 typval_T *argvars,
8703 typval_T *rettv,
8704 int off) /* 1 for gettabwinvar() */
8705{
8706 win_T *win;
8707 char_u *varname;
8708 dictitem_T *v;
8709 tabpage_T *tp = NULL;
8710 int done = FALSE;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008711 win_T *oldcurwin;
8712 tabpage_T *oldtabpage;
8713 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008714
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008715 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008716 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008717 else
8718 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008719 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008720 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008721 ++emsg_off;
8722
8723 rettv->v_type = VAR_STRING;
8724 rettv->vval.v_string = NULL;
8725
8726 if (win != NULL && varname != NULL)
8727 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008728 /* Set curwin to be our win, temporarily. Also set the tabpage,
8729 * otherwise the window is not valid. Only do this when needed,
8730 * autocommands get blocked. */
8731 need_switch_win = !(tp == curtab && win == curwin);
8732 if (!need_switch_win
8733 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008734 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008735 if (*varname == '&')
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008736 {
Bram Moolenaar30567352016-08-27 21:25:44 +02008737 if (varname[1] == NUL)
8738 {
8739 /* get all window-local options in a dict */
8740 dict_T *opts = get_winbuf_options(FALSE);
8741
8742 if (opts != NULL)
8743 {
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02008744 rettv_dict_set(rettv, opts);
Bram Moolenaar30567352016-08-27 21:25:44 +02008745 done = TRUE;
8746 }
8747 }
8748 else if (get_option_tv(&varname, rettv, 1) == OK)
8749 /* window-local-option */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008750 done = TRUE;
8751 }
8752 else
8753 {
8754 /* Look up the variable. */
8755 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
8756 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
8757 varname, FALSE);
8758 if (v != NULL)
8759 {
8760 copy_tv(&v->di_tv, rettv);
8761 done = TRUE;
8762 }
8763 }
8764 }
8765
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008766 if (need_switch_win)
8767 /* restore previous notion of curwin */
8768 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008769 }
8770
8771 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
8772 /* use the default return value */
8773 copy_tv(&argvars[off + 2], rettv);
8774
8775 --emsg_off;
8776}
8777
8778/*
8779 * "setwinvar()" and "settabwinvar()" functions
8780 */
8781 void
8782setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
8783{
8784 win_T *win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008785 win_T *save_curwin;
8786 tabpage_T *save_curtab;
8787 int need_switch_win;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008788 char_u *varname, *winvarname;
8789 typval_T *varp;
8790 char_u nbuf[NUMBUFLEN];
8791 tabpage_T *tp = NULL;
8792
Bram Moolenaare0fb7d12019-02-12 20:48:10 +01008793 if (check_secure())
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008794 return;
8795
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008796 if (off == 1)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008797 tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008798 else
8799 tp = curtab;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008800 win = find_win_by_nr(&argvars[off], tp);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008801 varname = tv_get_string_chk(&argvars[off + 1]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008802 varp = &argvars[off + 2];
8803
8804 if (win != NULL && varname != NULL && varp != NULL)
8805 {
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008806 need_switch_win = !(tp == curtab && win == curwin);
8807 if (!need_switch_win
8808 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008809 {
8810 if (*varname == '&')
8811 {
8812 long numval;
8813 char_u *strval;
8814 int error = FALSE;
8815
8816 ++varname;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01008817 numval = (long)tv_get_number_chk(varp, &error);
8818 strval = tv_get_string_buf_chk(varp, nbuf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008819 if (!error && strval != NULL)
8820 set_option_value(varname, numval, strval, OPT_LOCAL);
8821 }
8822 else
8823 {
8824 winvarname = alloc((unsigned)STRLEN(varname) + 3);
8825 if (winvarname != NULL)
8826 {
8827 STRCPY(winvarname, "w:");
8828 STRCPY(winvarname + 2, varname);
8829 set_var(winvarname, varp, TRUE);
8830 vim_free(winvarname);
8831 }
8832 }
8833 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008834 if (need_switch_win)
8835 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02008836 }
8837}
8838
8839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
8841 * "arg" points to the "&" or '+' when called, to "option" when returning.
8842 * Returns NULL when no option name found. Otherwise pointer to the char
8843 * after the option name.
8844 */
8845 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008846find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 char_u *p = *arg;
8849
8850 ++p;
8851 if (*p == 'g' && p[1] == ':')
8852 {
8853 *opt_flags = OPT_GLOBAL;
8854 p += 2;
8855 }
8856 else if (*p == 'l' && p[1] == ':')
8857 {
8858 *opt_flags = OPT_LOCAL;
8859 p += 2;
8860 }
8861 else
8862 *opt_flags = 0;
8863
8864 if (!ASCII_ISALPHA(*p))
8865 return NULL;
8866 *arg = p;
8867
8868 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
8869 p += 4; /* termcap option */
8870 else
8871 while (ASCII_ISALPHA(*p))
8872 ++p;
8873 return p;
8874}
8875
8876/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008877 * Return the autoload script name for a function or variable name.
8878 * Returns NULL when out of memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 */
Bram Moolenaara1544c02013-05-30 12:35:52 +02008880 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008881autoload_name(char_u *name)
Bram Moolenaara1544c02013-05-30 12:35:52 +02008882{
Bram Moolenaara1544c02013-05-30 12:35:52 +02008883 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008884 char_u *scriptname;
Bram Moolenaara1544c02013-05-30 12:35:52 +02008885
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008886 /* Get the script file name: replace '#' with '/', append ".vim". */
8887 scriptname = alloc((unsigned)(STRLEN(name) + 14));
8888 if (scriptname == NULL)
Bram Moolenaar9bdfb002014-04-23 17:43:42 +02008889 return FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008890 STRCPY(scriptname, "autoload/");
8891 STRCAT(scriptname, name);
8892 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
8893 STRCAT(scriptname, ".vim");
8894 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
8895 *p = '/';
8896 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008897}
8898
8899/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008900 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008901 * Return TRUE if a package was loaded.
8902 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02008903 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008904script_autoload(
8905 char_u *name,
8906 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008907{
8908 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008909 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008910 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008911 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008912
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008913 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00008914 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008915 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008916 return FALSE;
8917
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008918 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008919
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008920 /* Find the name in the list of previously loaded package names. Skip
8921 * "autoload/", it's always the same. */
8922 for (i = 0; i < ga_loaded.ga_len; ++i)
8923 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
8924 break;
8925 if (!reload && i < ga_loaded.ga_len)
8926 ret = FALSE; /* was loaded already */
8927 else
8928 {
8929 /* Remember the name if it wasn't loaded already. */
8930 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
8931 {
8932 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
8933 tofree = NULL;
8934 }
8935
8936 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +01008937 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008938 ret = TRUE;
8939 }
8940
8941 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008942 return ret;
8943}
8944
Bram Moolenaar071d4272004-06-13 20:20:40 +00008945#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
8946typedef enum
8947{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008948 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
8949 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
8950 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951} var_flavour_T;
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01008954var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955{
8956 char_u *p = varname;
8957
8958 if (ASCII_ISUPPER(*p))
8959 {
8960 while (*(++p))
8961 if (ASCII_ISLOWER(*p))
8962 return VAR_FLAVOUR_SESSION;
8963 return VAR_FLAVOUR_VIMINFO;
8964 }
8965 else
8966 return VAR_FLAVOUR_DEFAULT;
8967}
8968#endif
8969
8970#if defined(FEAT_VIMINFO) || defined(PROTO)
8971/*
8972 * Restore global vars that start with a capital from the viminfo file
8973 */
8974 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008975read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976{
8977 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008978 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +00008979 typval_T tv;
Bram Moolenaar27e80c82018-10-14 21:41:01 +02008980 funccal_entry_T funccal_entry;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981
8982 if (!writing && (find_viminfo_parameter('!') != NULL))
8983 {
8984 tab = vim_strchr(virp->vir_line + 1, '\t');
8985 if (tab != NULL)
8986 {
8987 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008988 switch (*tab)
8989 {
8990 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008991#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008992 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008993#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008994 case 'D': type = VAR_DICT; break;
8995 case 'L': type = VAR_LIST; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01008996 case 'B': type = VAR_BLOB; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01008997 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02008998 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999
9000 tab = vim_strchr(tab, '\t');
9001 if (tab != NULL)
9002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009003 tv.v_type = type;
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009004 if (type == VAR_STRING || type == VAR_DICT
9005 || type == VAR_LIST || type == VAR_BLOB)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009006 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009008#ifdef FEAT_FLOAT
9009 else if (type == VAR_FLOAT)
9010 (void)string2float(tab + 1, &tv.vval.v_float);
9011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009013 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009014 if (type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009015 {
9016 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
9017
9018 if (etv == NULL)
9019 /* Failed to parse back the dict or list, use it as a
9020 * string. */
9021 tv.v_type = VAR_STRING;
9022 else
9023 {
9024 vim_free(tv.vval.v_string);
9025 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +01009026 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009027 }
9028 }
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009029 else if (type == VAR_BLOB)
9030 {
9031 blob_T *blob = string2blob(tv.vval.v_string);
9032
9033 if (blob == NULL)
9034 // Failed to parse back the blob, use it as a string.
9035 tv.v_type = VAR_STRING;
9036 else
9037 {
9038 vim_free(tv.vval.v_string);
9039 tv.v_type = VAR_BLOB;
9040 tv.vval.v_blob = blob;
9041 }
9042 }
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009043
Bram Moolenaarb20e3342016-01-18 23:29:01 +01009044 /* when in a function use global variables */
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009045 save_funccal(&funccal_entry);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009046 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02009047 restore_funccal();
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009048
9049 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009050 vim_free(tv.vval.v_string);
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009051 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST ||
9052 tv.v_type == VAR_BLOB)
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009053 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 }
9055 }
9056 }
9057
9058 return viminfo_readline(virp);
9059}
9060
9061/*
9062 * Write global vars that start with a capital to the viminfo file
9063 */
9064 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009065write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066{
Bram Moolenaar33570922005-01-25 22:26:29 +00009067 hashitem_T *hi;
9068 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009069 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +01009070 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009071 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009072 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009073 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
9075 if (find_viminfo_parameter('!') == NULL)
9076 return;
9077
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02009078 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +00009079
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009080 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009083 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009085 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 this_var = HI2DI(hi);
9087 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009089 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +00009090 {
9091 case VAR_STRING: s = "STR"; break;
9092 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009093 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +02009094 case VAR_DICT: s = "DIC"; break;
9095 case VAR_LIST: s = "LIS"; break;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009096 case VAR_BLOB: s = "BLO"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +01009097 case VAR_SPECIAL: s = "XPL"; break;
9098
9099 case VAR_UNKNOWN:
9100 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009101 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01009102 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01009103 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01009104 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +00009105 }
Bram Moolenaar33570922005-01-25 22:26:29 +00009106 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaar8c8b8bb2019-01-13 17:48:04 +01009107 if (this_var->di_tv.v_type == VAR_SPECIAL)
9108 {
9109 sprintf((char *)numbuf, "%ld",
9110 (long)this_var->di_tv.vval.v_number);
9111 p = numbuf;
9112 tofree = NULL;
9113 }
9114 else
9115 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009116 if (p != NULL)
9117 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +00009118 vim_free(tofree);
9119 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 }
9121 }
9122}
9123#endif
9124
9125#if defined(FEAT_SESSION) || defined(PROTO)
9126 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009127store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128{
Bram Moolenaar33570922005-01-25 22:26:29 +00009129 hashitem_T *hi;
9130 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +00009131 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 char_u *p, *t;
9133
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009134 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009137 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009139 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00009140 this_var = HI2DI(hi);
9141 if ((this_var->di_tv.v_type == VAR_NUMBER
9142 || this_var->di_tv.v_type == VAR_STRING)
9143 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00009144 {
Bram Moolenaara7043832005-01-21 11:56:39 +00009145 /* Escape special characters with a backslash. Turn a LF and
9146 * CR into \n and \r. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009147 p = vim_strsave_escaped(tv_get_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +00009148 (char_u *)"\\\"\n\r");
9149 if (p == NULL) /* out of memory */
9150 break;
9151 for (t = p; *t != NUL; ++t)
9152 if (*t == '\n')
9153 *t = 'n';
9154 else if (*t == '\r')
9155 *t = 'r';
9156 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +00009157 this_var->di_key,
9158 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9159 : ' ',
9160 p,
9161 (this_var->di_tv.v_type == VAR_STRING) ? '"'
9162 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +00009163 || put_eol(fd) == FAIL)
9164 {
9165 vim_free(p);
9166 return FAIL;
9167 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 vim_free(p);
9169 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009170#ifdef FEAT_FLOAT
9171 else if (this_var->di_tv.v_type == VAR_FLOAT
9172 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
9173 {
9174 float_T f = this_var->di_tv.vval.v_float;
9175 int sign = ' ';
9176
9177 if (f < 0)
9178 {
9179 f = -f;
9180 sign = '-';
9181 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +01009182 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009183 this_var->di_key, sign, f) < 0)
9184 || put_eol(fd) == FAIL)
9185 return FAIL;
9186 }
9187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
9189 }
9190 return OK;
9191}
9192#endif
9193
Bram Moolenaar661b1822005-07-28 22:36:45 +00009194/*
9195 * Display script name where an item was last set.
9196 * Should only be invoked when 'verbose' is non-zero.
9197 */
9198 void
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009199last_set_msg(sctx_T script_ctx)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009200{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009201 char_u *p;
9202
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009203 if (script_ctx.sc_sid != 0)
Bram Moolenaar661b1822005-07-28 22:36:45 +00009204 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009205 p = home_replace_save(NULL, get_scriptname(script_ctx.sc_sid));
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009206 if (p != NULL)
9207 {
9208 verbose_enter();
Bram Moolenaar32526b32019-01-19 17:43:09 +01009209 msg_puts(_("\n\tLast set from "));
9210 msg_puts((char *)p);
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009211 if (script_ctx.sc_lnum > 0)
9212 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01009213 msg_puts(_(" line "));
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009214 msg_outnum((long)script_ctx.sc_lnum);
9215 }
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009216 verbose_leave();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02009217 vim_free(p);
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00009218 }
Bram Moolenaar661b1822005-07-28 22:36:45 +00009219 }
9220}
9221
Bram Moolenaar61be3762019-03-19 23:04:17 +01009222/*
9223 * Reset v:option_new, v:option_old and v:option_type.
9224 */
Bram Moolenaar53744302015-07-17 17:38:22 +02009225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009226reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +02009227{
9228 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
9229 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
9230 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
9231}
9232
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009233/*
9234 * Prepare "gap" for an assert error and add the sourcing position.
9235 */
9236 void
9237prepare_assert_error(garray_T *gap)
9238{
9239 char buf[NUMBUFLEN];
9240
9241 ga_init2(gap, 1, 100);
9242 if (sourcing_name != NULL)
9243 {
9244 ga_concat(gap, sourcing_name);
9245 if (sourcing_lnum > 0)
9246 ga_concat(gap, (char_u *)" ");
9247 }
9248 if (sourcing_lnum > 0)
9249 {
9250 sprintf(buf, "line %ld", (long)sourcing_lnum);
9251 ga_concat(gap, (char_u *)buf);
9252 }
9253 if (sourcing_name != NULL || sourcing_lnum > 0)
9254 ga_concat(gap, (char_u *)": ");
9255}
9256
9257/*
9258 * Add an assert error to v:errors.
9259 */
9260 void
9261assert_error(garray_T *gap)
9262{
9263 struct vimvar *vp = &vimvars[VV_ERRORS];
9264
9265 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9266 /* Make sure v:errors is a list. */
9267 set_vim_var_list(VV_ERRORS, list_alloc());
9268 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9269}
9270
Bram Moolenaar65a54642018-04-28 16:56:53 +02009271 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009272assert_equal_common(typval_T *argvars, assert_type_T atype)
9273{
9274 garray_T ga;
9275
9276 if (tv_equal(&argvars[0], &argvars[1], FALSE, FALSE)
9277 != (atype == ASSERT_EQUAL))
9278 {
9279 prepare_assert_error(&ga);
9280 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9281 atype);
9282 assert_error(&ga);
9283 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009284 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009285 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009286 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009287}
9288
Bram Moolenaar65a54642018-04-28 16:56:53 +02009289 int
Bram Moolenaard96ff162018-02-18 22:13:29 +01009290assert_equalfile(typval_T *argvars)
9291{
9292 char_u buf1[NUMBUFLEN];
9293 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009294 char_u *fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
9295 char_u *fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009296 garray_T ga;
9297 FILE *fd1;
9298 FILE *fd2;
9299
9300 if (fname1 == NULL || fname2 == NULL)
Bram Moolenaar65a54642018-04-28 16:56:53 +02009301 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009302
9303 IObuff[0] = NUL;
9304 fd1 = mch_fopen((char *)fname1, READBIN);
9305 if (fd1 == NULL)
9306 {
9307 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname1);
9308 }
9309 else
9310 {
9311 fd2 = mch_fopen((char *)fname2, READBIN);
9312 if (fd2 == NULL)
9313 {
9314 fclose(fd1);
9315 vim_snprintf((char *)IObuff, IOSIZE, (char *)e_notread, fname2);
9316 }
9317 else
9318 {
9319 int c1, c2;
9320 long count = 0;
9321
9322 for (;;)
9323 {
9324 c1 = fgetc(fd1);
9325 c2 = fgetc(fd2);
9326 if (c1 == EOF)
9327 {
9328 if (c2 != EOF)
9329 STRCPY(IObuff, "first file is shorter");
9330 break;
9331 }
9332 else if (c2 == EOF)
9333 {
9334 STRCPY(IObuff, "second file is shorter");
9335 break;
9336 }
9337 else if (c1 != c2)
9338 {
9339 vim_snprintf((char *)IObuff, IOSIZE,
9340 "difference at byte %ld", count);
9341 break;
9342 }
9343 ++count;
9344 }
Bram Moolenaar30494182018-02-20 21:46:05 +01009345 fclose(fd1);
9346 fclose(fd2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01009347 }
9348 }
9349 if (IObuff[0] != NUL)
9350 {
9351 prepare_assert_error(&ga);
9352 ga_concat(&ga, IObuff);
9353 assert_error(&ga);
9354 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009355 return 1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009356 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009357 return 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01009358}
9359
Bram Moolenaar65a54642018-04-28 16:56:53 +02009360 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009361assert_match_common(typval_T *argvars, assert_type_T atype)
9362{
9363 garray_T ga;
9364 char_u buf1[NUMBUFLEN];
9365 char_u buf2[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009366 char_u *pat = tv_get_string_buf_chk(&argvars[0], buf1);
9367 char_u *text = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009368
9369 if (pat == NULL || text == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009370 emsg(_(e_invarg));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009371 else if (pattern_match(pat, text, FALSE) != (atype == ASSERT_MATCH))
9372 {
9373 prepare_assert_error(&ga);
9374 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9375 atype);
9376 assert_error(&ga);
9377 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009378 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009379 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009380 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009381}
9382
Bram Moolenaar65a54642018-04-28 16:56:53 +02009383 int
Bram Moolenaar61c04492016-07-23 15:35:35 +02009384assert_inrange(typval_T *argvars)
9385{
9386 garray_T ga;
9387 int error = FALSE;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009388 char_u *tofree;
9389 char msg[200];
9390 char_u numbuf[NUMBUFLEN];
9391
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009392#ifdef FEAT_FLOAT
9393 if (argvars[0].v_type == VAR_FLOAT
9394 || argvars[1].v_type == VAR_FLOAT
9395 || argvars[2].v_type == VAR_FLOAT)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009396 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009397 float_T flower = tv_get_float(&argvars[0]);
9398 float_T fupper = tv_get_float(&argvars[1]);
9399 float_T factual = tv_get_float(&argvars[2]);
9400
9401 if (factual < flower || factual > fupper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009402 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009403 prepare_assert_error(&ga);
9404 if (argvars[3].v_type != VAR_UNKNOWN)
9405 {
9406 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9407 vim_free(tofree);
9408 }
9409 else
9410 {
9411 vim_snprintf(msg, 200, "Expected range %g - %g, but got %g",
9412 flower, fupper, factual);
9413 ga_concat(&ga, (char_u *)msg);
9414 }
9415 assert_error(&ga);
9416 ga_clear(&ga);
9417 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009418 }
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009419 }
9420 else
9421#endif
9422 {
9423 varnumber_T lower = tv_get_number_chk(&argvars[0], &error);
9424 varnumber_T upper = tv_get_number_chk(&argvars[1], &error);
9425 varnumber_T actual = tv_get_number_chk(&argvars[2], &error);
9426
9427 if (error)
9428 return 0;
9429 if (actual < lower || actual > upper)
Bram Moolenaar61c04492016-07-23 15:35:35 +02009430 {
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009431 prepare_assert_error(&ga);
9432 if (argvars[3].v_type != VAR_UNKNOWN)
9433 {
9434 ga_concat(&ga, tv2string(&argvars[3], &tofree, numbuf, 0));
9435 vim_free(tofree);
9436 }
9437 else
9438 {
9439 vim_snprintf(msg, 200, "Expected range %ld - %ld, but got %ld",
Bram Moolenaar61c04492016-07-23 15:35:35 +02009440 (long)lower, (long)upper, (long)actual);
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009441 ga_concat(&ga, (char_u *)msg);
9442 }
9443 assert_error(&ga);
9444 ga_clear(&ga);
9445 return 1;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009446 }
Bram Moolenaar61c04492016-07-23 15:35:35 +02009447 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009448 return 0;
Bram Moolenaar61c04492016-07-23 15:35:35 +02009449}
9450
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009451/*
9452 * Common for assert_true() and assert_false().
Bram Moolenaar65a54642018-04-28 16:56:53 +02009453 * Return non-zero for failure.
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009454 */
Bram Moolenaar65a54642018-04-28 16:56:53 +02009455 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009456assert_bool(typval_T *argvars, int isTrue)
9457{
9458 int error = FALSE;
9459 garray_T ga;
9460
9461 if (argvars[0].v_type == VAR_SPECIAL
9462 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar65a54642018-04-28 16:56:53 +02009463 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009464 if (argvars[0].v_type != VAR_NUMBER
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009465 || (tv_get_number_chk(&argvars[0], &error) == 0) == isTrue
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009466 || error)
9467 {
9468 prepare_assert_error(&ga);
9469 fill_assert_error(&ga, &argvars[1],
9470 (char_u *)(isTrue ? "True" : "False"),
9471 NULL, &argvars[0], ASSERT_OTHER);
9472 assert_error(&ga);
9473 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009474 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009475 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009476 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009477}
9478
Bram Moolenaar65a54642018-04-28 16:56:53 +02009479 int
Bram Moolenaar42205552017-03-18 19:42:22 +01009480assert_report(typval_T *argvars)
9481{
9482 garray_T ga;
9483
9484 prepare_assert_error(&ga);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009485 ga_concat(&ga, tv_get_string(&argvars[0]));
Bram Moolenaar42205552017-03-18 19:42:22 +01009486 assert_error(&ga);
9487 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009488 return 1;
Bram Moolenaar42205552017-03-18 19:42:22 +01009489}
9490
Bram Moolenaar65a54642018-04-28 16:56:53 +02009491 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009492assert_exception(typval_T *argvars)
9493{
9494 garray_T ga;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009495 char_u *error = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009496
9497 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9498 {
9499 prepare_assert_error(&ga);
9500 ga_concat(&ga, (char_u *)"v:exception is not set");
9501 assert_error(&ga);
9502 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009503 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009504 }
9505 else if (error != NULL
9506 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, (char *)error) == NULL)
9507 {
9508 prepare_assert_error(&ga);
9509 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9510 &vimvars[VV_EXCEPTION].vv_tv, ASSERT_OTHER);
9511 assert_error(&ga);
9512 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009513 return 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009514 }
Bram Moolenaar65a54642018-04-28 16:56:53 +02009515 return 0;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009516}
9517
Bram Moolenaar65a54642018-04-28 16:56:53 +02009518 int
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009519assert_beeps(typval_T *argvars)
9520{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009521 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009522 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009523 int ret = 0;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009524
9525 called_vim_beep = FALSE;
9526 suppress_errthrow = TRUE;
9527 emsg_silent = FALSE;
9528 do_cmdline_cmd(cmd);
9529 if (!called_vim_beep)
9530 {
9531 prepare_assert_error(&ga);
9532 ga_concat(&ga, (char_u *)"command did not beep: ");
9533 ga_concat(&ga, cmd);
9534 assert_error(&ga);
9535 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009536 ret = 1;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009537 }
9538
9539 suppress_errthrow = FALSE;
9540 emsg_on_display = FALSE;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009541 return ret;
Bram Moolenaarb48e96f2018-02-13 12:26:14 +01009542}
9543
Bram Moolenaar65a54642018-04-28 16:56:53 +02009544 int
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009545assert_fails(typval_T *argvars)
9546{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009547 char_u *cmd = tv_get_string_chk(&argvars[0]);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009548 garray_T ga;
Bram Moolenaar65a54642018-04-28 16:56:53 +02009549 int ret = 0;
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009550 char_u numbuf[NUMBUFLEN];
9551 char_u *tofree;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009552
9553 called_emsg = FALSE;
9554 suppress_errthrow = TRUE;
9555 emsg_silent = TRUE;
9556 do_cmdline_cmd(cmd);
9557 if (!called_emsg)
9558 {
9559 prepare_assert_error(&ga);
9560 ga_concat(&ga, (char_u *)"command did not fail: ");
Bram Moolenaar1307d1c2018-10-07 20:16:49 +02009561 if (argvars[1].v_type != VAR_UNKNOWN
9562 && argvars[2].v_type != VAR_UNKNOWN)
9563 {
9564 ga_concat(&ga, echo_string(&argvars[2], &tofree, numbuf, 0));
9565 vim_free(tofree);
9566 }
9567 else
9568 ga_concat(&ga, cmd);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009569 assert_error(&ga);
9570 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009571 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009572 }
9573 else if (argvars[1].v_type != VAR_UNKNOWN)
9574 {
9575 char_u buf[NUMBUFLEN];
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009576 char *error = (char *)tv_get_string_buf_chk(&argvars[1], buf);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009577
9578 if (error == NULL
9579 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9580 {
9581 prepare_assert_error(&ga);
9582 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9583 &vimvars[VV_ERRMSG].vv_tv, ASSERT_OTHER);
9584 assert_error(&ga);
9585 ga_clear(&ga);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009586 ret = 1;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009587 }
9588 }
9589
9590 called_emsg = FALSE;
9591 suppress_errthrow = FALSE;
9592 emsg_silent = FALSE;
9593 emsg_on_display = FALSE;
9594 set_vim_var_string(VV_ERRMSG, NULL, 0);
Bram Moolenaar65a54642018-04-28 16:56:53 +02009595 return ret;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009596}
9597
9598/*
Bram Moolenaar86576712019-01-25 20:48:33 +01009599 * Append "p[clen]" to "gap", escaping unprintable characters.
9600 * Changes NL to \n, CR to \r, etc.
9601 */
9602 static void
9603ga_concat_esc(garray_T *gap, char_u *p, int clen)
9604{
9605 char_u buf[NUMBUFLEN];
9606
9607 if (clen > 1)
9608 {
9609 mch_memmove(buf, p, clen);
9610 buf[clen] = NUL;
9611 ga_concat(gap, buf);
9612 }
9613 else switch (*p)
9614 {
9615 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9616 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9617 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9618 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9619 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9620 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9621 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9622 default:
9623 if (*p < ' ')
9624 {
9625 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9626 ga_concat(gap, buf);
9627 }
9628 else
9629 ga_append(gap, *p);
9630 break;
9631 }
9632}
9633
9634/*
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009635 * Append "str" to "gap", escaping unprintable characters.
9636 * Changes NL to \n, CR to \r, etc.
9637 */
9638 static void
Bram Moolenaar86576712019-01-25 20:48:33 +01009639ga_concat_shorten_esc(garray_T *gap, char_u *str)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009640{
9641 char_u *p;
Bram Moolenaar86576712019-01-25 20:48:33 +01009642 char_u *s;
9643 int c;
9644 int clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009645 char_u buf[NUMBUFLEN];
Bram Moolenaar86576712019-01-25 20:48:33 +01009646 int same_len;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009647
9648 if (str == NULL)
9649 {
9650 ga_concat(gap, (char_u *)"NULL");
9651 return;
9652 }
9653
9654 for (p = str; *p != NUL; ++p)
Bram Moolenaar86576712019-01-25 20:48:33 +01009655 {
9656 same_len = 1;
9657 s = p;
9658 c = mb_ptr2char_adv(&s);
9659 clen = s - p;
9660 while (*s != NUL && c == mb_ptr2char(s))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009661 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009662 ++same_len;
9663 s += clen;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009664 }
Bram Moolenaar86576712019-01-25 20:48:33 +01009665 if (same_len > 20)
9666 {
9667 ga_concat(gap, (char_u *)"\\[");
9668 ga_concat_esc(gap, p, clen);
9669 ga_concat(gap, (char_u *)" occurs ");
9670 vim_snprintf((char *)buf, NUMBUFLEN, "%d", same_len);
9671 ga_concat(gap, buf);
9672 ga_concat(gap, (char_u *)" times]");
9673 p = s - 1;
9674 }
9675 else
9676 ga_concat_esc(gap, p, clen);
9677 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009678}
9679
9680/*
9681 * Fill "gap" with information about an assert error.
9682 */
9683 void
9684fill_assert_error(
9685 garray_T *gap,
9686 typval_T *opt_msg_tv,
9687 char_u *exp_str,
9688 typval_T *exp_tv,
9689 typval_T *got_tv,
9690 assert_type_T atype)
9691{
9692 char_u numbuf[NUMBUFLEN];
9693 char_u *tofree;
9694
9695 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9696 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009697 ga_concat(gap, echo_string(opt_msg_tv, &tofree, numbuf, 0));
9698 vim_free(tofree);
9699 ga_concat(gap, (char_u *)": ");
9700 }
9701
9702 if (atype == ASSERT_MATCH || atype == ASSERT_NOTMATCH)
9703 ga_concat(gap, (char_u *)"Pattern ");
9704 else if (atype == ASSERT_NOTEQUAL)
9705 ga_concat(gap, (char_u *)"Expected not equal to ");
9706 else
9707 ga_concat(gap, (char_u *)"Expected ");
9708 if (exp_str == NULL)
9709 {
Bram Moolenaar86576712019-01-25 20:48:33 +01009710 ga_concat_shorten_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009711 vim_free(tofree);
9712 }
9713 else
Bram Moolenaar86576712019-01-25 20:48:33 +01009714 ga_concat_shorten_esc(gap, exp_str);
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009715 if (atype != ASSERT_NOTEQUAL)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009716 {
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009717 if (atype == ASSERT_MATCH)
9718 ga_concat(gap, (char_u *)" does not match ");
9719 else if (atype == ASSERT_NOTMATCH)
9720 ga_concat(gap, (char_u *)" does match ");
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009721 else
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009722 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar86576712019-01-25 20:48:33 +01009723 ga_concat_shorten_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarc7b831c2017-01-28 18:08:12 +01009724 vim_free(tofree);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +02009725 }
9726}
9727
Bram Moolenaar31988702018-02-13 12:57:42 +01009728/*
9729 * Compare "typ1" and "typ2". Put the result in "typ1".
9730 */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009731 int
9732typval_compare(
9733 typval_T *typ1, /* first operand */
9734 typval_T *typ2, /* second operand */
9735 exptype_T type, /* operator */
9736 int type_is, /* TRUE for "is" and "isnot" */
Bram Moolenaar31988702018-02-13 12:57:42 +01009737 int ic) /* ignore case */
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009738{
9739 int i;
9740 varnumber_T n1, n2;
9741 char_u *s1, *s2;
9742 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
9743
Bram Moolenaar31988702018-02-13 12:57:42 +01009744 if (type_is && typ1->v_type != typ2->v_type)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009745 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009746 /* For "is" a different type always means FALSE, for "notis"
9747 * it means TRUE. */
9748 n1 = (type == TYPE_NEQUAL);
9749 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009750 else if (typ1->v_type == VAR_BLOB || typ2->v_type == VAR_BLOB)
9751 {
9752 if (type_is)
9753 {
9754 n1 = (typ1->v_type == typ2->v_type
9755 && typ1->vval.v_blob == typ2->vval.v_blob);
9756 if (type == TYPE_NEQUAL)
9757 n1 = !n1;
9758 }
9759 else if (typ1->v_type != typ2->v_type
9760 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9761 {
9762 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009763 emsg(_("E977: Can only compare Blob with Blob"));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009764 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009765 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +01009766 clear_tv(typ1);
9767 return FAIL;
9768 }
9769 else
9770 {
9771 // Compare two Blobs for being equal or unequal.
9772 n1 = blob_equal(typ1->vval.v_blob, typ2->vval.v_blob);
9773 if (type == TYPE_NEQUAL)
9774 n1 = !n1;
9775 }
9776 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009777 else if (typ1->v_type == VAR_LIST || typ2->v_type == VAR_LIST)
9778 {
9779 if (type_is)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009780 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009781 n1 = (typ1->v_type == typ2->v_type
9782 && typ1->vval.v_list == typ2->vval.v_list);
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009783 if (type == TYPE_NEQUAL)
9784 n1 = !n1;
9785 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009786 else if (typ1->v_type != typ2->v_type
9787 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009788 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009789 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009790 emsg(_("E691: Can only compare List with List"));
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009791 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009792 emsg(_("E692: Invalid operation for List"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009793 clear_tv(typ1);
9794 return FAIL;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009795 }
9796 else
9797 {
Bram Moolenaar31988702018-02-13 12:57:42 +01009798 /* Compare two Lists for being equal or unequal. */
9799 n1 = list_equal(typ1->vval.v_list, typ2->vval.v_list,
9800 ic, FALSE);
9801 if (type == TYPE_NEQUAL)
9802 n1 = !n1;
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009803 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009804 }
Bram Moolenaar31988702018-02-13 12:57:42 +01009805
9806 else if (typ1->v_type == VAR_DICT || typ2->v_type == VAR_DICT)
9807 {
9808 if (type_is)
9809 {
9810 n1 = (typ1->v_type == typ2->v_type
9811 && typ1->vval.v_dict == typ2->vval.v_dict);
9812 if (type == TYPE_NEQUAL)
9813 n1 = !n1;
9814 }
9815 else if (typ1->v_type != typ2->v_type
9816 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
9817 {
9818 if (typ1->v_type != typ2->v_type)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009819 emsg(_("E735: Can only compare Dictionary with Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009820 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009821 emsg(_("E736: Invalid operation for Dictionary"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009822 clear_tv(typ1);
9823 return FAIL;
9824 }
9825 else
9826 {
9827 /* Compare two Dictionaries for being equal or unequal. */
9828 n1 = dict_equal(typ1->vval.v_dict, typ2->vval.v_dict,
9829 ic, FALSE);
9830 if (type == TYPE_NEQUAL)
9831 n1 = !n1;
9832 }
9833 }
9834
9835 else if (typ1->v_type == VAR_FUNC || typ2->v_type == VAR_FUNC
9836 || typ1->v_type == VAR_PARTIAL || typ2->v_type == VAR_PARTIAL)
9837 {
9838 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
9839 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01009840 emsg(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar31988702018-02-13 12:57:42 +01009841 clear_tv(typ1);
9842 return FAIL;
9843 }
9844 if ((typ1->v_type == VAR_PARTIAL
9845 && typ1->vval.v_partial == NULL)
9846 || (typ2->v_type == VAR_PARTIAL
9847 && typ2->vval.v_partial == NULL))
9848 /* when a partial is NULL assume not equal */
9849 n1 = FALSE;
9850 else if (type_is)
9851 {
9852 if (typ1->v_type == VAR_FUNC && typ2->v_type == VAR_FUNC)
9853 /* strings are considered the same if their value is
9854 * the same */
9855 n1 = tv_equal(typ1, typ2, ic, FALSE);
9856 else if (typ1->v_type == VAR_PARTIAL
9857 && typ2->v_type == VAR_PARTIAL)
9858 n1 = (typ1->vval.v_partial == typ2->vval.v_partial);
9859 else
9860 n1 = FALSE;
9861 }
9862 else
9863 n1 = tv_equal(typ1, typ2, ic, FALSE);
9864 if (type == TYPE_NEQUAL)
9865 n1 = !n1;
9866 }
9867
9868#ifdef FEAT_FLOAT
9869 /*
9870 * If one of the two variables is a float, compare as a float.
9871 * When using "=~" or "!~", always compare as string.
9872 */
9873 else if ((typ1->v_type == VAR_FLOAT || typ2->v_type == VAR_FLOAT)
9874 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9875 {
9876 float_T f1, f2;
9877
Bram Moolenaar38f08e72019-02-20 22:04:32 +01009878 f1 = tv_get_float(typ1);
9879 f2 = tv_get_float(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009880 n1 = FALSE;
9881 switch (type)
9882 {
9883 case TYPE_EQUAL: n1 = (f1 == f2); break;
9884 case TYPE_NEQUAL: n1 = (f1 != f2); break;
9885 case TYPE_GREATER: n1 = (f1 > f2); break;
9886 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
9887 case TYPE_SMALLER: n1 = (f1 < f2); break;
9888 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
9889 case TYPE_UNKNOWN:
9890 case TYPE_MATCH:
9891 case TYPE_NOMATCH: break; /* avoid gcc warning */
9892 }
9893 }
9894#endif
9895
9896 /*
9897 * If one of the two variables is a number, compare as a number.
9898 * When using "=~" or "!~", always compare as string.
9899 */
9900 else if ((typ1->v_type == VAR_NUMBER || typ2->v_type == VAR_NUMBER)
9901 && type != TYPE_MATCH && type != TYPE_NOMATCH)
9902 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009903 n1 = tv_get_number(typ1);
9904 n2 = tv_get_number(typ2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009905 switch (type)
9906 {
9907 case TYPE_EQUAL: n1 = (n1 == n2); break;
9908 case TYPE_NEQUAL: n1 = (n1 != n2); break;
9909 case TYPE_GREATER: n1 = (n1 > n2); break;
9910 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
9911 case TYPE_SMALLER: n1 = (n1 < n2); break;
9912 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
9913 case TYPE_UNKNOWN:
9914 case TYPE_MATCH:
9915 case TYPE_NOMATCH: break; /* avoid gcc warning */
9916 }
9917 }
9918 else
9919 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01009920 s1 = tv_get_string_buf(typ1, buf1);
9921 s2 = tv_get_string_buf(typ2, buf2);
Bram Moolenaar31988702018-02-13 12:57:42 +01009922 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
9923 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
9924 else
9925 i = 0;
9926 n1 = FALSE;
9927 switch (type)
9928 {
9929 case TYPE_EQUAL: n1 = (i == 0); break;
9930 case TYPE_NEQUAL: n1 = (i != 0); break;
9931 case TYPE_GREATER: n1 = (i > 0); break;
9932 case TYPE_GEQUAL: n1 = (i >= 0); break;
9933 case TYPE_SMALLER: n1 = (i < 0); break;
9934 case TYPE_SEQUAL: n1 = (i <= 0); break;
9935
9936 case TYPE_MATCH:
9937 case TYPE_NOMATCH:
9938 n1 = pattern_match(s2, s1, ic);
9939 if (type == TYPE_NOMATCH)
9940 n1 = !n1;
9941 break;
9942
9943 case TYPE_UNKNOWN: break; /* avoid gcc warning */
9944 }
9945 }
9946 clear_tv(typ1);
9947 typ1->v_type = VAR_NUMBER;
9948 typ1->vval.v_number = n1;
9949
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009950 return OK;
9951}
9952
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009953 char_u *
Bram Moolenaar6f8d2ac2018-07-25 19:49:45 +02009954typval_tostring(typval_T *arg)
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01009955{
9956 char_u *tofree;
9957 char_u numbuf[NUMBUFLEN];
9958 char_u *ret = NULL;
9959
9960 if (arg == NULL)
9961 return vim_strsave((char_u *)"(does not exist)");
9962 ret = tv2string(arg, &tofree, numbuf, 0);
9963 /* Make a copy if we have a value but it's not in allocated memory. */
9964 if (ret != NULL && tofree == NULL)
9965 ret = vim_strsave(ret);
9966 return ret;
9967}
9968
9969 int
9970var_exists(char_u *var)
9971{
9972 char_u *name;
9973 char_u *tofree;
9974 typval_T tv;
9975 int len = 0;
9976 int n = FALSE;
9977
9978 /* get_name_len() takes care of expanding curly braces */
9979 name = var;
9980 len = get_name_len(&var, &tofree, TRUE, FALSE);
9981 if (len > 0)
9982 {
9983 if (tofree != NULL)
9984 name = tofree;
9985 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
9986 if (n)
9987 {
9988 /* handle d.key, l[idx], f(expr) */
9989 n = (handle_subscript(&var, &tv, TRUE, FALSE) == OK);
9990 if (n)
9991 clear_tv(&tv);
9992 }
9993 }
9994 if (*var != NUL)
9995 n = FALSE;
9996
9997 vim_free(tofree);
9998 return n;
9999}
10000
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001#endif /* FEAT_EVAL */
10002
Bram Moolenaar071d4272004-06-13 20:20:40 +000010003
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010004#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005
Bram Moolenaar4f974752019-02-17 17:44:42 +010010006#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007/*
10008 * Functions for ":8" filename modifier: get 8.3 version of a filename.
10009 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010010
10011/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010012 * Get the short path (8.3) for the filename in "fnamep".
10013 * Only works for a valid file name.
10014 * When the path gets longer "fnamep" is changed and the allocated buffer
10015 * is put in "bufp".
10016 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
10017 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010018 */
10019 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010020get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010021{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010022 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023 char_u *newbuf;
10024
10025 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010026 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010027 if (l > len - 1)
10028 {
10029 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010030 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 newbuf = vim_strnsave(*fnamep, l);
10032 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010033 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034
10035 vim_free(*bufp);
10036 *fnamep = *bufp = newbuf;
10037
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010038 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010010039 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 }
10041
10042 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010043 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044}
10045
10046/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010047 * Get the short path (8.3) for the filename in "fname". The converted
10048 * path is returned in "bufp".
10049 *
10050 * Some of the directories specified in "fname" may not exist. This function
10051 * will shorten the existing directories at the beginning of the path and then
10052 * append the remaining non-existing path.
10053 *
10054 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020010055 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010056 * bufp - Pointer to an allocated buffer for the filename.
10057 * fnamelen - Length of the filename pointed to by fname
10058 *
10059 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 */
10061 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010062shortpath_for_invalid_fname(
10063 char_u **fname,
10064 char_u **bufp,
10065 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010067 char_u *short_fname, *save_fname, *pbuf_unused;
10068 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010070 int old_len, len;
10071 int new_len, sfx_len;
10072 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073
10074 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010075 old_len = *fnamelen;
10076 save_fname = vim_strnsave(*fname, old_len);
10077 pbuf_unused = NULL;
10078 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010080 endp = save_fname + old_len - 1; /* Find the end of the copy */
10081 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010082
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010083 /*
10084 * Try shortening the supplied path till it succeeds by removing one
10085 * directory at a time from the tail of the path.
10086 */
10087 len = 0;
10088 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010090 /* go back one path-separator */
10091 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
10092 --endp;
10093 if (endp <= save_fname)
10094 break; /* processed the complete path */
10095
10096 /*
10097 * Replace the path separator with a NUL and try to shorten the
10098 * resulting path.
10099 */
10100 ch = *endp;
10101 *endp = 0;
10102 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010103 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010104 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
10105 {
10106 retval = FAIL;
10107 goto theend;
10108 }
10109 *endp = ch; /* preserve the string */
10110
10111 if (len > 0)
10112 break; /* successfully shortened the path */
10113
10114 /* failed to shorten the path. Skip the path separator */
10115 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 }
10117
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010118 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010120 /*
10121 * Succeeded in shortening the path. Now concatenate the shortened
10122 * path with the remaining path at the tail.
10123 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010124
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010125 /* Compute the length of the new path. */
10126 sfx_len = (int)(save_endp - endp) + 1;
10127 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010129 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010131 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010133 /* There is not enough space in the currently allocated string,
10134 * copy it to a buffer big enough. */
10135 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010137 {
10138 retval = FAIL;
10139 goto theend;
10140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 }
10142 else
10143 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010144 /* Transfer short_fname to the main buffer (it's big enough),
10145 * unless get_short_pathname() did its work in-place. */
10146 *fname = *bufp = save_fname;
10147 if (short_fname != save_fname)
10148 vim_strncpy(save_fname, short_fname, len);
10149 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010151
10152 /* concat the not-shortened part of the path */
10153 vim_strncpy(*fname + len, endp, sfx_len);
10154 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010156
10157theend:
10158 vim_free(pbuf_unused);
10159 vim_free(save_fname);
10160
10161 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162}
10163
10164/*
10165 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010166 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 */
10168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010169shortpath_for_partial(
10170 char_u **fnamep,
10171 char_u **bufp,
10172 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174 int sepcount, len, tflen;
10175 char_u *p;
10176 char_u *pbuf, *tfname;
10177 int hasTilde;
10178
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010179 /* Count up the path separators from the RHS.. so we know which part
10180 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010181 sepcount = 0;
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010182 for (p = *fnamep; p < *fnamep + *fnamelen; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010183 if (vim_ispathsep(*p))
10184 ++sepcount;
10185
10186 /* Need full path first (use expand_env() to remove a "~/") */
10187 hasTilde = (**fnamep == '~');
10188 if (hasTilde)
10189 pbuf = tfname = expand_env_save(*fnamep);
10190 else
10191 pbuf = tfname = FullName_save(*fnamep, FALSE);
10192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010193 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010195 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
10196 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197
10198 if (len == 0)
10199 {
10200 /* Don't have a valid filename, so shorten the rest of the
10201 * path if we can. This CAN give us invalid 8.3 filenames, but
10202 * there's not a lot of point in guessing what it might be.
10203 */
10204 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010205 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
10206 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 }
10208
10209 /* Count the paths backward to find the beginning of the desired string. */
10210 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010211 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010212 if (has_mbyte)
10213 p -= mb_head_off(tfname, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 if (vim_ispathsep(*p))
10215 {
10216 if (sepcount == 0 || (hasTilde && sepcount == 1))
10217 break;
10218 else
10219 sepcount --;
10220 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010222 if (hasTilde)
10223 {
10224 --p;
10225 if (p >= tfname)
10226 *p = '~';
10227 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010228 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010229 }
10230 else
10231 ++p;
10232
10233 /* Copy in the string - p indexes into tfname - allocated at pbuf */
10234 vim_free(*bufp);
10235 *fnamelen = (int)STRLEN(p);
10236 *bufp = pbuf;
10237 *fnamep = p;
10238
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010239 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010240}
Bram Moolenaar4f974752019-02-17 17:44:42 +010010241#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010242
10243/*
10244 * Adjust a filename, according to a string of modifiers.
10245 * *fnamep must be NUL terminated when called. When returning, the length is
10246 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010247 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 * When there is an error, *fnamep is set to NULL.
10249 */
10250 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010251modify_fname(
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010252 char_u *src, // string with modifiers
10253 int tilde_file, // "~" is a file name, not $HOME
10254 int *usedlen, // characters after src that are used
10255 char_u **fnamep, // file name so far
10256 char_u **bufp, // buffer for allocated file name or NULL
10257 int *fnamelen) // length of fnamep
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259 int valid = 0;
10260 char_u *tail;
10261 char_u *s, *p, *pbuf;
10262 char_u dirname[MAXPATHL];
10263 int c;
10264 int has_fullname = 0;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010265#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010266 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 int has_shortname = 0;
10268#endif
10269
10270repeat:
10271 /* ":p" - full path/file_name */
10272 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
10273 {
10274 has_fullname = 1;
10275
10276 valid |= VALID_PATH;
10277 *usedlen += 2;
10278
10279 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
10280 if ((*fnamep)[0] == '~'
10281#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
10282 && ((*fnamep)[1] == '/'
10283# ifdef BACKSLASH_IN_FILENAME
10284 || (*fnamep)[1] == '\\'
10285# endif
10286 || (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010287#endif
Bram Moolenaar00136dc2018-07-25 21:19:13 +020010288 && !(tilde_file && (*fnamep)[1] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010289 )
10290 {
10291 *fnamep = expand_env_save(*fnamep);
10292 vim_free(*bufp); /* free any allocated file name */
10293 *bufp = *fnamep;
10294 if (*fnamep == NULL)
10295 return -1;
10296 }
10297
10298 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010299 for (p = *fnamep; *p != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300 {
10301 if (vim_ispathsep(*p)
10302 && p[1] == '.'
10303 && (p[2] == NUL
10304 || vim_ispathsep(p[2])
10305 || (p[2] == '.'
10306 && (p[3] == NUL || vim_ispathsep(p[3])))))
10307 break;
10308 }
10309
10310 /* FullName_save() is slow, don't use it when not needed. */
10311 if (*p != NUL || !vim_isAbsName(*fnamep))
10312 {
10313 *fnamep = FullName_save(*fnamep, *p != NUL);
10314 vim_free(*bufp); /* free any allocated file name */
10315 *bufp = *fnamep;
10316 if (*fnamep == NULL)
10317 return -1;
10318 }
10319
Bram Moolenaar4f974752019-02-17 17:44:42 +010010320#ifdef MSWIN
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010321# if _WIN32_WINNT >= 0x0500
10322 if (vim_strchr(*fnamep, '~') != NULL)
10323 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010324 // Expand 8.3 filename to full path. Needed to make sure the same
10325 // file does not have two different names.
10326 // Note: problem does not occur if _WIN32_WINNT < 0x0500.
10327 WCHAR *wfname = enc_to_utf16(*fnamep, NULL);
10328 WCHAR buf[_MAX_PATH];
10329
10330 if (wfname != NULL)
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010331 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010332 if (GetLongPathNameW(wfname, buf, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010333 {
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010334 char_u *p = utf16_to_enc(buf, NULL);
10335
10336 if (p != NULL)
10337 {
10338 vim_free(*bufp); // free any allocated file name
10339 *bufp = *fnamep = p;
10340 }
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010341 }
Bram Moolenaar2d04a912019-03-30 20:04:08 +010010342 vim_free(wfname);
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020010343 }
10344 }
10345# endif
10346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010347 /* Append a path separator to a directory. */
10348 if (mch_isdir(*fnamep))
10349 {
10350 /* Make room for one or two extra characters. */
10351 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
10352 vim_free(*bufp); /* free any allocated file name */
10353 *bufp = *fnamep;
10354 if (*fnamep == NULL)
10355 return -1;
10356 add_pathsep(*fnamep);
10357 }
10358 }
10359
10360 /* ":." - path relative to the current directory */
10361 /* ":~" - path relative to the home directory */
10362 /* ":8" - shortname path - postponed till after */
10363 while (src[*usedlen] == ':'
10364 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
10365 {
10366 *usedlen += 2;
10367 if (c == '8')
10368 {
Bram Moolenaar4f974752019-02-17 17:44:42 +010010369#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010370 has_shortname = 1; /* Postpone this. */
10371#endif
10372 continue;
10373 }
10374 pbuf = NULL;
10375 /* Need full path first (use expand_env() to remove a "~/") */
10376 if (!has_fullname)
10377 {
10378 if (c == '.' && **fnamep == '~')
10379 p = pbuf = expand_env_save(*fnamep);
10380 else
10381 p = pbuf = FullName_save(*fnamep, FALSE);
10382 }
10383 else
10384 p = *fnamep;
10385
10386 has_fullname = 0;
10387
10388 if (p != NULL)
10389 {
10390 if (c == '.')
10391 {
10392 mch_dirname(dirname, MAXPATHL);
10393 s = shorten_fname(p, dirname);
10394 if (s != NULL)
10395 {
10396 *fnamep = s;
10397 if (pbuf != NULL)
10398 {
10399 vim_free(*bufp); /* free any allocated file name */
10400 *bufp = pbuf;
10401 pbuf = NULL;
10402 }
10403 }
10404 }
10405 else
10406 {
10407 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
10408 /* Only replace it when it starts with '~' */
10409 if (*dirname == '~')
10410 {
10411 s = vim_strsave(dirname);
10412 if (s != NULL)
10413 {
10414 *fnamep = s;
10415 vim_free(*bufp);
10416 *bufp = s;
10417 }
10418 }
10419 }
10420 vim_free(pbuf);
10421 }
10422 }
10423
10424 tail = gettail(*fnamep);
10425 *fnamelen = (int)STRLEN(*fnamep);
10426
10427 /* ":h" - head, remove "/file_name", can be repeated */
10428 /* Don't remove the first "/" or "c:\" */
10429 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
10430 {
10431 valid |= VALID_HEAD;
10432 *usedlen += 2;
10433 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000010434 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010435 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010436 *fnamelen = (int)(tail - *fnamep);
10437#ifdef VMS
10438 if (*fnamelen > 0)
10439 *fnamelen += 1; /* the path separator is part of the path */
10440#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010441 if (*fnamelen == 0)
10442 {
10443 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
10444 p = vim_strsave((char_u *)".");
10445 if (p == NULL)
10446 return -1;
10447 vim_free(*bufp);
10448 *bufp = *fnamep = tail = p;
10449 *fnamelen = 1;
10450 }
10451 else
10452 {
10453 while (tail > s && !after_pathsep(s, tail))
Bram Moolenaar91acfff2017-03-12 19:22:36 +010010454 MB_PTR_BACK(*fnamep, tail);
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000010455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 }
10457
10458 /* ":8" - shortname */
10459 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
10460 {
10461 *usedlen += 2;
Bram Moolenaar4f974752019-02-17 17:44:42 +010010462#ifdef MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010463 has_shortname = 1;
10464#endif
10465 }
10466
Bram Moolenaar4f974752019-02-17 17:44:42 +010010467#ifdef MSWIN
Bram Moolenaardc935552011-08-17 15:23:23 +020010468 /*
10469 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 */
10471 if (has_shortname)
10472 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010473 /* Copy the string if it is shortened by :h and when it wasn't copied
10474 * yet, because we are going to change it in place. Avoids changing
10475 * the buffer name for "%:8". */
10476 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 {
10478 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020010479 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010480 return -1;
10481 vim_free(*bufp);
10482 *bufp = *fnamep = p;
10483 }
10484
10485 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020010486 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010487 if (!has_fullname && !vim_isAbsName(*fnamep))
10488 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010489 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 return -1;
10491 }
10492 else
10493 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010494 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495
Bram Moolenaardc935552011-08-17 15:23:23 +020010496 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010498 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010499 return -1;
10500
10501 if (l == 0)
10502 {
Bram Moolenaardc935552011-08-17 15:23:23 +020010503 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010504 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000010505 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506 return -1;
10507 }
10508 *fnamelen = l;
10509 }
10510 }
Bram Moolenaar4f974752019-02-17 17:44:42 +010010511#endif // MSWIN
Bram Moolenaar071d4272004-06-13 20:20:40 +000010512
10513 /* ":t" - tail, just the basename */
10514 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
10515 {
10516 *usedlen += 2;
10517 *fnamelen -= (int)(tail - *fnamep);
10518 *fnamep = tail;
10519 }
10520
10521 /* ":e" - extension, can be repeated */
10522 /* ":r" - root, without extension, can be repeated */
10523 while (src[*usedlen] == ':'
10524 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
10525 {
10526 /* find a '.' in the tail:
10527 * - for second :e: before the current fname
10528 * - otherwise: The last '.'
10529 */
10530 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
10531 s = *fnamep - 2;
10532 else
10533 s = *fnamep + *fnamelen - 1;
10534 for ( ; s > tail; --s)
10535 if (s[0] == '.')
10536 break;
10537 if (src[*usedlen + 1] == 'e') /* :e */
10538 {
10539 if (s > tail)
10540 {
10541 *fnamelen += (int)(*fnamep - (s + 1));
10542 *fnamep = s + 1;
10543#ifdef VMS
10544 /* cut version from the extension */
10545 s = *fnamep + *fnamelen - 1;
10546 for ( ; s > *fnamep; --s)
10547 if (s[0] == ';')
10548 break;
10549 if (s > *fnamep)
10550 *fnamelen = s - *fnamep;
10551#endif
10552 }
10553 else if (*fnamep <= tail)
10554 *fnamelen = 0;
10555 }
10556 else /* :r */
10557 {
10558 if (s > tail) /* remove one extension */
10559 *fnamelen = (int)(s - *fnamep);
10560 }
10561 *usedlen += 2;
10562 }
10563
10564 /* ":s?pat?foo?" - substitute */
10565 /* ":gs?pat?foo?" - global substitute */
10566 if (src[*usedlen] == ':'
10567 && (src[*usedlen + 1] == 's'
10568 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
10569 {
10570 char_u *str;
10571 char_u *pat;
10572 char_u *sub;
10573 int sep;
10574 char_u *flags;
10575 int didit = FALSE;
10576
10577 flags = (char_u *)"";
10578 s = src + *usedlen + 2;
10579 if (src[*usedlen + 1] == 'g')
10580 {
10581 flags = (char_u *)"g";
10582 ++s;
10583 }
10584
10585 sep = *s++;
10586 if (sep)
10587 {
10588 /* find end of pattern */
10589 p = vim_strchr(s, sep);
10590 if (p != NULL)
10591 {
10592 pat = vim_strnsave(s, (int)(p - s));
10593 if (pat != NULL)
10594 {
10595 s = p + 1;
10596 /* find end of substitution */
10597 p = vim_strchr(s, sep);
10598 if (p != NULL)
10599 {
10600 sub = vim_strnsave(s, (int)(p - s));
10601 str = vim_strnsave(*fnamep, *fnamelen);
10602 if (sub != NULL && str != NULL)
10603 {
10604 *usedlen = (int)(p + 1 - src);
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010605 s = do_string_sub(str, pat, sub, NULL, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606 if (s != NULL)
10607 {
10608 *fnamep = s;
10609 *fnamelen = (int)STRLEN(s);
10610 vim_free(*bufp);
10611 *bufp = s;
10612 didit = TRUE;
10613 }
10614 }
10615 vim_free(sub);
10616 vim_free(str);
10617 }
10618 vim_free(pat);
10619 }
10620 }
10621 /* after using ":s", repeat all the modifiers */
10622 if (didit)
10623 goto repeat;
10624 }
10625 }
10626
Bram Moolenaar26df0922014-02-23 23:39:13 +010010627 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
10628 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010010629 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010010630 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010631 if (c != NUL)
10632 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010633 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010010634 if (c != NUL)
10635 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010010636 if (p == NULL)
10637 return -1;
10638 vim_free(*bufp);
10639 *bufp = *fnamep = p;
10640 *fnamelen = (int)STRLEN(p);
10641 *usedlen += 2;
10642 }
10643
Bram Moolenaar071d4272004-06-13 20:20:40 +000010644 return valid;
10645}
10646
10647/*
10648 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010649 * When "sub" is NULL "expr" is used, must be a VAR_FUNC or VAR_PARTIAL.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 * "flags" can be "g" to do a global substitute.
10651 * Returns an allocated string, NULL for error.
10652 */
10653 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010654do_string_sub(
10655 char_u *str,
10656 char_u *pat,
10657 char_u *sub,
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010658 typval_T *expr,
Bram Moolenaar7454a062016-01-30 15:14:10 +010010659 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660{
10661 int sublen;
10662 regmatch_T regmatch;
10663 int i;
10664 int do_all;
10665 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010666 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010667 garray_T ga;
10668 char_u *ret;
10669 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010670 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010671
10672 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
10673 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010674 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675
10676 ga_init2(&ga, 1, 200);
10677
10678 do_all = (flags[0] == 'g');
10679
10680 regmatch.rm_ic = p_ic;
10681 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
10682 if (regmatch.regprog != NULL)
10683 {
10684 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010010685 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
10687 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010010688 /* Skip empty match except for first match. */
10689 if (regmatch.startp[0] == regmatch.endp[0])
10690 {
10691 if (zero_width == regmatch.startp[0])
10692 {
10693 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020010694 i = MB_PTR2LEN(tail);
10695 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
10696 (size_t)i);
10697 ga.ga_len += i;
10698 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010010699 continue;
10700 }
10701 zero_width = regmatch.startp[0];
10702 }
10703
Bram Moolenaar071d4272004-06-13 20:20:40 +000010704 /*
10705 * Get some space for a temporary buffer to do the substitution
10706 * into. It will contain:
10707 * - The text up to where the match is.
10708 * - The substituted text.
10709 * - The text after the match.
10710 */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010711 sublen = vim_regsub(&regmatch, sub, expr, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010010712 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
10714 {
10715 ga_clear(&ga);
10716 break;
10717 }
10718
10719 /* copy the text up to where the match is */
10720 i = (int)(regmatch.startp[0] - tail);
10721 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
10722 /* add the substituted text */
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010723 (void)vim_regsub(&regmatch, sub, expr, (char_u *)ga.ga_data
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724 + ga.ga_len + i, TRUE, TRUE, FALSE);
10725 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020010726 tail = regmatch.endp[0];
10727 if (*tail == NUL)
10728 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 if (!do_all)
10730 break;
10731 }
10732
10733 if (ga.ga_data != NULL)
10734 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
10735
Bram Moolenaar473de612013-06-08 18:19:48 +020010736 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010737 }
10738
10739 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
10740 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010741 if (p_cpo == empty_option)
10742 p_cpo = save_cpo;
10743 else
Bram Moolenaar72ab7292016-07-19 19:10:51 +020010744 /* Darn, evaluating {sub} expression or {expr} changed the value. */
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000010745 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010746
10747 return ret;
10748}
10749
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010750 static int
10751filter_map_one(typval_T *tv, typval_T *expr, int map, int *remp)
10752{
10753 typval_T rettv;
10754 typval_T argv[3];
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010755 int retval = FAIL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010756
10757 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
10758 argv[0] = vimvars[VV_KEY].vv_tv;
10759 argv[1] = vimvars[VV_VAL].vv_tv;
Bram Moolenaar48570482017-10-30 21:48:41 +010010760 if (eval_expr_typval(expr, argv, 2, &rettv) == FAIL)
10761 goto theend;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010762 if (map)
10763 {
10764 /* map(): replace the list item value */
10765 clear_tv(tv);
10766 rettv.v_lock = 0;
10767 *tv = rettv;
10768 }
10769 else
10770 {
10771 int error = FALSE;
10772
10773 /* filter(): when expr is zero remove the item */
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010774 *remp = (tv_get_number_chk(&rettv, &error) == 0);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010775 clear_tv(&rettv);
10776 /* On type error, nothing has been removed; return FAIL to stop the
Bram Moolenaard155d7a2018-12-21 16:04:21 +010010777 * loop. The error message was given by tv_get_number_chk(). */
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010778 if (error)
10779 goto theend;
10780 }
10781 retval = OK;
10782theend:
10783 clear_tv(&vimvars[VV_VAL].vv_tv);
10784 return retval;
10785}
10786
10787
10788/*
10789 * Implementation of map() and filter().
10790 */
10791 void
10792filter_map(typval_T *argvars, typval_T *rettv, int map)
10793{
10794 typval_T *expr;
10795 listitem_T *li, *nli;
10796 list_T *l = NULL;
10797 dictitem_T *di;
10798 hashtab_T *ht;
10799 hashitem_T *hi;
10800 dict_T *d = NULL;
10801 typval_T save_val;
10802 typval_T save_key;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010803 blob_T *b = NULL;
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010804 int rem;
10805 int todo;
10806 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10807 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
10808 : N_("filter() argument"));
10809 int save_did_emsg;
10810 int idx = 0;
10811
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010812 if (argvars[0].v_type == VAR_BLOB)
10813 {
10814 if ((b = argvars[0].vval.v_blob) == NULL)
10815 return;
10816 }
10817 else if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010818 {
10819 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010820 || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010821 return;
10822 }
10823 else if (argvars[0].v_type == VAR_DICT)
10824 {
10825 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010826 || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010827 return;
10828 }
10829 else
10830 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010831 semsg(_(e_listdictarg), ermsg);
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010832 return;
10833 }
10834
10835 expr = &argvars[1];
10836 /* On type errors, the preceding call has already displayed an error
10837 * message. Avoid a misleading error message for an empty string that
10838 * was not passed as argument. */
10839 if (expr->v_type != VAR_UNKNOWN)
10840 {
10841 prepare_vimvar(VV_VAL, &save_val);
10842
10843 /* We reset "did_emsg" to be able to detect whether an error
10844 * occurred during evaluation of the expression. */
10845 save_did_emsg = did_emsg;
10846 did_emsg = FALSE;
10847
10848 prepare_vimvar(VV_KEY, &save_key);
10849 if (argvars[0].v_type == VAR_DICT)
10850 {
10851 vimvars[VV_KEY].vv_type = VAR_STRING;
10852
10853 ht = &d->dv_hashtab;
10854 hash_lock(ht);
10855 todo = (int)ht->ht_used;
10856 for (hi = ht->ht_array; todo > 0; ++hi)
10857 {
10858 if (!HASHITEM_EMPTY(hi))
10859 {
10860 int r;
10861
10862 --todo;
10863 di = HI2DI(hi);
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010864 if (map && (var_check_lock(di->di_tv.v_lock,
10865 arg_errmsg, TRUE)
10866 || var_check_ro(di->di_flags,
10867 arg_errmsg, TRUE)))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010868 break;
10869 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
10870 r = filter_map_one(&di->di_tv, expr, map, &rem);
10871 clear_tv(&vimvars[VV_KEY].vv_tv);
10872 if (r == FAIL || did_emsg)
10873 break;
10874 if (!map && rem)
10875 {
10876 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
10877 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
10878 break;
10879 dictitem_remove(d, di);
10880 }
10881 }
10882 }
10883 hash_unlock(ht);
10884 }
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010885 else if (argvars[0].v_type == VAR_BLOB)
10886 {
10887 int i;
10888 typval_T tv;
10889
10890 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10891 for (i = 0; i < b->bv_ga.ga_len; i++)
10892 {
10893 tv.v_type = VAR_NUMBER;
10894 tv.vval.v_number = blob_get(b, i);
10895 vimvars[VV_KEY].vv_nr = idx;
10896 if (filter_map_one(&tv, expr, map, &rem) == FAIL || did_emsg)
10897 break;
10898 if (tv.v_type != VAR_NUMBER)
10899 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +010010900 emsg(_(e_invalblob));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +010010901 return;
10902 }
10903 tv.v_type = VAR_NUMBER;
10904 blob_set(b, i, tv.vval.v_number);
10905 if (!map && rem)
10906 {
10907 char_u *p = (char_u *)argvars[0].vval.v_blob->bv_ga.ga_data;
10908
10909 mch_memmove(p + idx, p + i + 1,
10910 (size_t)b->bv_ga.ga_len - i - 1);
10911 --b->bv_ga.ga_len;
10912 --i;
10913 }
10914 }
10915 }
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010916 else
10917 {
Bram Moolenaarce9d50d2019-01-14 22:22:29 +010010918 // argvars[0].v_type == VAR_LIST
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010919 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10920
10921 for (li = l->lv_first; li != NULL; li = nli)
10922 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +010010923 if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar73dad1e2016-07-17 22:13:49 +020010924 break;
10925 nli = li->li_next;
10926 vimvars[VV_KEY].vv_nr = idx;
10927 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
10928 || did_emsg)
10929 break;
10930 if (!map && rem)
10931 listitem_remove(l, li);
10932 ++idx;
10933 }
10934 }
10935
10936 restore_vimvar(VV_KEY, &save_key);
10937 restore_vimvar(VV_VAL, &save_val);
10938
10939 did_emsg |= save_did_emsg;
10940 }
10941
10942 copy_tv(&argvars[0], rettv);
10943}
10944
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */